|
16 | 16 | #include "lua/CLuaStackChecker.h" |
17 | 17 | #include "lua/LuaBasic.h" |
18 | 18 |
|
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 |
26 | 20 | { |
27 | 21 | std::size_t iIndex = 1; |
28 | 22 | std::string strError = ""; |
@@ -91,7 +85,8 @@ struct CLuaFunctionParser<ErrorOnFailure, Func> |
91 | 85 | std::string strValue(szValue, iLen); |
92 | 86 | if (strValue.length() > 10) |
93 | 87 | { |
94 | | - strValue.resize(10); // Limit to 10 characters |
| 88 | + // Limit to 10 characters |
| 89 | + strValue.resize(10); |
95 | 90 | strValue[9] = '.'; |
96 | 91 | strValue[8] = '.'; |
97 | 92 | strValue[7] = '.'; |
@@ -216,9 +211,9 @@ struct CLuaFunctionParser<ErrorOnFailure, Func> |
216 | 211 | // and can be fetched from a userdata |
217 | 212 | if constexpr (std::is_pointer_v<T> && std::is_class_v<std::remove_pointer_t<T>>) |
218 | 213 | return iArgument == LUA_TUSERDATA || iArgument == LUA_TLIGHTUSERDATA; |
219 | | - |
| 214 | + |
220 | 215 | // 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 |
222 | 217 | if constexpr (std::is_same_v<T, dummy_type>) |
223 | 218 | return iArgument == LUA_TNONE; |
224 | 219 | } |
@@ -260,9 +255,20 @@ struct CLuaFunctionParser<ErrorOnFailure, Func> |
260 | 255 | if constexpr (std::is_same_v<T, dummy_type>) |
261 | 256 | return dummy_type{}; |
262 | 257 | // 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>) |
265 | 260 | 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 | + } |
266 | 272 | else if constexpr (std::is_enum_v<T>) |
267 | 273 | { |
268 | 274 | // Enums are considered strings in Lua |
@@ -368,7 +374,16 @@ struct CLuaFunctionParser<ErrorOnFailure, Func> |
368 | 374 | return static_cast<T>(result); |
369 | 375 | } |
370 | 376 | } |
| 377 | +}; |
| 378 | + |
| 379 | +template <bool, auto*> |
| 380 | +struct CLuaFunctionParser |
| 381 | +{ |
| 382 | +}; |
371 | 383 |
|
| 384 | +template <bool ErrorOnFailure, typename Ret, typename... Args, auto (*Func)(Args...)->Ret> |
| 385 | +struct CLuaFunctionParser<ErrorOnFailure, Func> : CLuaFunctionParserBase |
| 386 | +{ |
372 | 387 | template <typename... Params> |
373 | 388 | inline auto Call(lua_State* L, Params&&... ps) |
374 | 389 | { |
|
0 commit comments