Skip to content

Commit 734f018

Browse files
committed
Improve code generation by using a common base class
Disallow NaN for float/double
1 parent 09d598c commit 734f018

File tree

1 file changed

+27
-12
lines changed

1 file changed

+27
-12
lines changed

Shared/mods/deathmatch/logic/lua/CLuaFunctionParser.h

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,7 @@
1616
#include "lua/CLuaStackChecker.h"
1717
#include "lua/LuaBasic.h"
1818

19-
template <bool, auto*>
20-
struct CLuaFunctionParser
21-
{
22-
};
23-
24-
template <bool ErrorOnFailure, typename Ret, typename... Args, auto (*Func)(Args...)->Ret>
25-
struct CLuaFunctionParser<ErrorOnFailure, Func>
19+
struct CLuaFunctionParserBase
2620
{
2721
std::size_t iIndex = 1;
2822
std::string strError = "";
@@ -91,7 +85,8 @@ struct CLuaFunctionParser<ErrorOnFailure, Func>
9185
std::string strValue(szValue, iLen);
9286
if (strValue.length() > 10)
9387
{
94-
strValue.resize(10); // Limit to 10 characters
88+
// Limit to 10 characters
89+
strValue.resize(10);
9590
strValue[9] = '.';
9691
strValue[8] = '.';
9792
strValue[7] = '.';
@@ -216,9 +211,9 @@ struct CLuaFunctionParser<ErrorOnFailure, Func>
216211
// and can be fetched from a userdata
217212
if constexpr (std::is_pointer_v<T> && std::is_class_v<std::remove_pointer_t<T>>)
218213
return iArgument == LUA_TUSERDATA || iArgument == LUA_TLIGHTUSERDATA;
219-
214+
220215
// dummy type is used as overload extension if one overload has fewer arguments
221-
// thus it is only allowed if there are no further args on the Lua side
216+
// thus it is only allowed if there are no further args on the Lua side
222217
if constexpr (std::is_same_v<T, dummy_type>)
223218
return iArgument == LUA_TNONE;
224219
}
@@ -260,9 +255,20 @@ struct CLuaFunctionParser<ErrorOnFailure, Func>
260255
if constexpr (std::is_same_v<T, dummy_type>)
261256
return dummy_type{};
262257
// trivial types are directly popped
263-
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, int> || std::is_same_v<T, float> || std::is_same_v<T, double> ||
264-
std::is_same_v<T, short> || std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short> || std::is_same_v<T, bool>)
258+
else if constexpr (std::is_same_v<T, std::string> || std::is_same_v<T, int> || std::is_same_v<T, short> || std::is_same_v<T, bool> ||
259+
std::is_same_v<T, unsigned int> || std::is_same_v<T, unsigned short>)
265260
return lua::PopTrivial<T>(L, index);
261+
// floats/doubles may not be NaN
262+
else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>)
263+
{
264+
T value = lua::PopTrivial<T>(L, index);
265+
if (std::isnan(value))
266+
{
267+
SString strMessage("Expected number at argument %d, got NaN", index);
268+
strError = strMessage;
269+
}
270+
return value;
271+
}
266272
else if constexpr (std::is_enum_v<T>)
267273
{
268274
// Enums are considered strings in Lua
@@ -368,7 +374,16 @@ struct CLuaFunctionParser<ErrorOnFailure, Func>
368374
return static_cast<T>(result);
369375
}
370376
}
377+
};
378+
379+
template <bool, auto*>
380+
struct CLuaFunctionParser
381+
{
382+
};
371383

384+
template <bool ErrorOnFailure, typename Ret, typename... Args, auto (*Func)(Args...)->Ret>
385+
struct CLuaFunctionParser<ErrorOnFailure, Func> : CLuaFunctionParserBase
386+
{
372387
template <typename... Params>
373388
inline auto Call(lua_State* L, Params&&... ps)
374389
{

0 commit comments

Comments
 (0)