-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modernize lua read (part 2 & 3): C++ templating assurance #7410
Conversation
Implement the boolean reader Also remove unused & unimplemented script_error_handler
src/script/common/helper.cpp
Outdated
template<> | ||
bool LuaHelper::readParam(lua_State *L, int index) | ||
{ | ||
return lua_toboolean(L, index) != 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a question: should i add a throw LuaError with not a bool here to use strong bool or not ? we are lazy on checks
Implement the std::string reader Also be strict with boolean checks, C++ doesn't define false as the boolean default value, it's undef Fix some wrong type checks in some code parts, detected when enabled string and boolean strict type checking
Note, the parts are not to be squashed. I tested it with mtg without having problems, but the strict checking on bool can either fix some random bugs (undefined default bool in some calls) or permit mods to fix some implementation bugs |
Note to @SmallJoker : LuaError is catched by serverstep or client step and shown with a backtrace properly :) |
src/script/common/helper.cpp
Outdated
return ""; | ||
|
||
if (!lua_isstring(L, index)) | ||
throw LuaError(luahelper_type_error(index, "string")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
luaL_checkstring
does the same in one line.
src/script/common/helper.cpp
Outdated
template <> std::string LuaHelper::readParam(lua_State *L, int index) | ||
{ | ||
if (lua_isnil(L, index)) | ||
return ""; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In some places it's best to throw an error since a proper string value is expected.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i agree with you, first i didn't added this but we had many stack traces with regular MTG :) we can remove it later after fixing our upstream calls or i can remove it and we will fix it on each bug reported by end users
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's indeed bad. I would however prefer to fix those issues instead of silently accepting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i remove it, and we are free to fix the engine crashes after :)
src/script/cpp_api/s_security.cpp
Outdated
@@ -649,7 +649,7 @@ int ScriptApiSecurity::sl_g_loadfile(lua_State *L) | |||
lua_pop(L, 1); | |||
|
|||
if (script->getType() == ScriptingType::Client) { | |||
std:: string display_path = lua_tostring(L, 1); | |||
std:: string display_path = readParam<std::string>(L, 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Space after ::
src/script/lua_api/l_object.cpp
Outdated
@@ -243,7 +243,8 @@ int ObjectRef::l_set_hp(lua_State *L) | |||
lua_pushvalue(L, 3); | |||
|
|||
lua_getfield(L, -1, "type"); | |||
if (lua_isstring(L, -1) && !reason.setTypeFromString(lua_tostring(L, -1))) { | |||
if (lua_isstring(L, -1) && | |||
!reason.setTypeFromString(readParam<std::string>(L, -1))) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing indent.
src/script/common/helper.cpp
Outdated
result.append(str); | ||
} | ||
|
||
return str; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be return result;
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oops :) ty
What's the advantage of this, is lua parameter reading faster? |
@paramat it's exactly same performance (in release mode, negligible in debug mode). The advantage if we implement the template on all types is that permit to access to lua reader/writer in a uniform, standardized and securized way. One high level function per type to perform the call with all security/sanitize it needs. it also permits to implement easily a complex reader in the same API (imagine writeParam<Json::Value>(L, json_val), for example) It's basically a code quality implementation, and it can be (at the end) easily tested with unittests if we need it |
Ok thanks, no objection from me. Unfortunately i'm not qualified to officially +1 it. |
@SmallJoker if you are okay, i can merge this. It seems other qualified devs are not concerned about this |
Approved by @SmallJoker on IRC |
*/ | ||
template <> bool LuaHelper::readParam(lua_State *L, int index) | ||
{ | ||
return lua_toboolean(L, index) != 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should have a comment why luaL_checkboolean
is not used here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this function doesn't exists in Lua & has been dropped from the header, as it seems useless currently (i added this in a previous version of this PR)
src/script/common/helper.cpp
Outdated
return lua_toboolean(L, index) != 0; | ||
} | ||
|
||
template <> bool LuaHelper::readParam(lua_State *L, int index, const bool &dv) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
clarity: dv -> default
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
default is a protected keyword in C++11 standard
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also dv is described in the header comments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well then default_
and fallback_value
would still be an option
Implement the boolean reader
Also remove unused & unimplemented script_error_handler
move readers to a new LuaHelper class permitting to share code between Lua interfaces