From b639c229e3fdef21cec4535284eeabbca361dad6 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 22 May 2024 14:43:07 +0200 Subject: lua503_getfield → strong typed luaG_getfield MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/compat.cpp | 4 +-- src/compat.h | 72 ++++++++++++++++++++++-------------------------- src/deep.cpp | 9 +++--- src/intercopycontext.cpp | 12 ++++---- src/lane.cpp | 2 +- src/lanes.cpp | 15 +++++----- src/state.cpp | 5 ++-- src/universe.cpp | 31 ++++++++++----------- 8 files changed, 68 insertions(+), 82 deletions(-) (limited to 'src') diff --git a/src/compat.cpp b/src/compat.cpp index b45cce2..efc2ffd 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -14,12 +14,12 @@ LuaType luaG_getmodule(lua_State* L_, char const* name_) { STACK_CHECK_START_REL(L_, 0); - LuaType _type{ static_cast(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil + LuaType _type{ luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil STACK_CHECK(L_, 1); return _type; } - _type = static_cast(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil + _type = luaG_getfield(L_, -1, name_); // L_: _R._LOADED {module}|nil lua_remove(L_, -2); // L_: {module}|nil STACK_CHECK(L_, 1); return _type; diff --git a/src/compat.h b/src/compat.h index b5afe17..f097fb6 100644 --- a/src/compat.h +++ b/src/compat.h @@ -31,6 +31,33 @@ extern "C" // ################################################################################################# +// a strong-typed wrapper over lua types to see them easier in a debugger +enum class LuaType +{ + NONE = LUA_TNONE, + NIL = LUA_TNIL, + BOOLEAN = LUA_TBOOLEAN, + LIGHTUSERDATA = LUA_TLIGHTUSERDATA, + NUMBER = LUA_TNUMBER, + STRING = LUA_TSTRING, + TABLE = LUA_TTABLE, + FUNCTION = LUA_TFUNCTION, + USERDATA = LUA_TUSERDATA, + THREAD = LUA_TTHREAD, + CDATA = 10 // LuaJIT CDATA +}; + +inline LuaType lua_type_as_enum(lua_State* L_, int idx_) +{ + return static_cast(lua_type(L_, idx_)); +} +inline char const* lua_typename(lua_State* L_, LuaType t_) +{ + return lua_typename(L_, static_cast(t_)); +} + +// ################################################################################################# + // add some Lua 5.3-style API when building for Lua 5.1 #if LUA_VERSION_NUM == 501 @@ -106,22 +133,16 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u // ################################################################################################# -#if LUA_VERSION_NUM < 503 -// starting with Lua 5.3, lua_getfield returns the type of the value it found -inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) +[[nodiscard]] inline LuaType luaG_getfield(lua_State* L_, int idx_, char const* k_) { +// starting with Lua 5.3, lua_getfield returns the type of the value it found +#if LUA_VERSION_NUM < 503 lua_getfield(L_, idx_, k_); - return lua_type(L_, -1); -} - + return lua_type_as_enum(L_, -1); #else // LUA_VERSION_NUM >= 503 - -inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) -{ - return lua_getfield(L_, idx_, k_); -} - + return static_cast(lua_getfield(L_, idx_, k_)); #endif // LUA_VERSION_NUM >= 503 +} // ################################################################################################# @@ -202,33 +223,6 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) // ################################################################################################# -// a strong-typed wrapper over lua types to see them easier in a debugger -enum class LuaType -{ - NONE = LUA_TNONE, - NIL = LUA_TNIL, - BOOLEAN = LUA_TBOOLEAN, - LIGHTUSERDATA = LUA_TLIGHTUSERDATA, - NUMBER = LUA_TNUMBER, - STRING = LUA_TSTRING, - TABLE = LUA_TTABLE, - FUNCTION = LUA_TFUNCTION, - USERDATA = LUA_TUSERDATA, - THREAD = LUA_TTHREAD, - CDATA = 10 // LuaJIT CDATA -}; - -inline LuaType lua_type_as_enum(lua_State* L_, int idx_) -{ - return static_cast(lua_type(L_, idx_)); -} -inline char const* lua_typename(lua_State* L_, LuaType t_) -{ - return lua_typename(L_, static_cast(t_)); -} - -// ################################################################################################# - // a strong-typed wrapper over lua error codes to see them easier in a debugger enum class LuaError { diff --git a/src/deep.cpp b/src/deep.cpp index 51c9250..f23f0be 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -224,7 +224,7 @@ std::string_view DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; } // if the metatable contains a __gc, we will call it from our own - lua_getfield(L_, -1, "__gc"); // L_: DPC proxy metatable __gc + std::ignore = luaG_getfield(L_, -1, "__gc"); // L_: DPC proxy metatable __gc } else { // keepers need a minimal metatable that only contains our own __gc lua_createtable(L_, 0, 1); // L_: DPC proxy metatable @@ -250,12 +250,11 @@ std::string_view DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, // check that the module is already loaded (or being loaded, we are happy either way) if (lua_isfunction(L_, -1)) { lua_pushlstring(L_, _modname.data(), _modname.size()); // L_: DPC proxy metatable require() "module" - lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // L_: DPC proxy metatable require() "module" _R._LOADED - if (lua_istable(L_, -1)) { + if (luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) == LuaType::TABLE) { // L_: DPC proxy metatable require() "module" _R._LOADED lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" lua_rawget(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED module - int const alreadyloaded = lua_toboolean(L_, -1); - if (!alreadyloaded) { // not loaded + int const _alreadyloaded{ lua_toboolean(L_, -1) }; + if (!_alreadyloaded) { // not loaded lua_pop(L_, 2); // L_: DPC proxy metatable require() "module" // require "modname" LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 08709e5..0301382 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -651,8 +651,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const } // no __lanesclone? -> not clonable - lua_getfield(L1, -1, "__lanesclone"); // L1: ... mt __lanesclone? - if (lua_isnil(L1, -1)) { + if (luaG_getfield(L1, -1, "__lanesclone") == LuaType::NIL) { // L1: ... mt nil lua_pop(L1, 2); // L1: ... STACK_CHECK(L1, 0); return false; @@ -865,7 +864,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const // perform the custom cloning part lua_insert(L2, -2); // L2: ... u mt // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with - lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone + std::ignore = luaG_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone lua_remove(L2, -2); // L2: ... u __lanesclone lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source @@ -1083,8 +1082,8 @@ static char const* vt_names[] = { // Non-POD can be skipped if its metatable contains { __lanesignore = true } if (((1 << static_cast(_val_type)) & kPODmask) == 0) { if (lua_getmetatable(L1, L1_i)) { // L1: ... mt - lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? - if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { + LuaType const _type{ luaG_getfield(L1, -1, "__lanesignore") }; // L1: ... mt ignore? + if (_type == LuaType::BOOLEAN && lua_toboolean(L1, -1)) { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); _val_type = LuaType::NIL; } @@ -1195,8 +1194,7 @@ static char const* vt_names[] = { continue; } DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); - lua_getfield(L1, L1_i, _entry); - if (lua_isnil(L1, -1)) { + if (luaG_getfield(L1, L1_i, _entry) == LuaType::NIL) { lua_pop(L1, 1); } else { { diff --git a/src/lane.cpp b/src/lane.cpp index d68da76..5904266 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -340,7 +340,7 @@ static LUAG_FUNC(thread_index) default: // unknown key lua_getmetatable(L_, kSelf); // L_: mt - lua_getfield(L_, -1, "cached_error"); // L_: mt error + std::ignore = luaG_getfield(L_, -1, "cached_error"); // L_: mt error lua_pushliteral(L_, "Unknown key: "); // L_: mt error "Unknown key: " lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k lua_concat(L_, 2); // L_: mt error "Unknown key: " diff --git a/src/lanes.cpp b/src/lanes.cpp index 22391d5..c91256e 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -553,7 +553,7 @@ LUAG_FUNC(wakeup_conv) STACK_CHECK_START_REL(L_, 0); auto _readInteger = [L = L_](char const* name_) { - lua_getfield(L, 1, name_); + std::ignore = luaG_getfield(L, 1, name_); lua_Integer const val{ lua_tointeger(L, -1) }; lua_pop(L, 1); return static_cast(val); @@ -569,8 +569,7 @@ LUAG_FUNC(wakeup_conv) // If Lua table has '.isdst' we trust that. If it does not, we'll let // 'mktime' decide on whether the time is within DST or not (value -1). // - lua_getfield(L_, 1, "isdst"); - int const _isdst{ lua_isboolean(L_, -1) ? lua_toboolean(L_, -1) : -1 }; + int const _isdst{ (luaG_getfield(L_, 1, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 }; lua_pop(L_, 1); STACK_CHECK(L_, 0); @@ -641,20 +640,20 @@ LUAG_FUNC(configure) _U = universe_create(L_); // L_: settings universe DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); lua_createtable(L_, 0, 1); // L_: settings universe {mt} - lua_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout - lua_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode + std::ignore = luaG_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout + std::ignore = luaG_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode lua_pushcclosure(L_, universe_gc, 2); // L_: settings universe {mt} universe_gc lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} lua_setmetatable(L_, -2); // L_: settings universe lua_pop(L_, 1); // L_: settings - lua_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors + std::ignore = luaG_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; lua_pop(L_, 1); // L_: settings - lua_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata + std::ignore = luaG_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata _U->demoteFullUserdata = lua_toboolean(L_, -1) ? true : false; lua_pop(L_, 1); // L_: settings #if HAVE_LANE_TRACKING() - lua_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes + std::ignore = luaG_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes if (lua_toboolean(L_, -1)) { _U->tracker.activate(); } diff --git a/src/state.cpp b/src/state.cpp index e258f9e..90a7c5b 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -218,8 +218,7 @@ static void copy_one_time_settings(Universe* U_, SourceState L1_, DestState L2_) void InitializeOnStateCreate(Universe* U_, lua_State* L_) { STACK_CHECK_START_REL(L_, 1); // L_: settings - lua_getfield(L_, -1, "on_state_create"); // L_: settings on_state_create|nil - if (!lua_isnil(L_, -1)) { + if (luaG_getfield(L_, -1, "on_state_create") != LuaType::NIL) { // L_: settings on_state_create|nil // store C function pointer in an internal variable U_->onStateCreateFunc = lua_tocfunction(L_, -1); // L_: settings on_state_create if (U_->onStateCreateFunc != nullptr) { @@ -289,7 +288,7 @@ void CallOnStateCreate(Universe* U_, lua_State* L_, lua_State* from_, LookupMode } kConfigRegKey.pushValue(L_); // L_: {} STACK_CHECK(L_, 1); - lua_getfield(L_, -1, "on_state_create"); // L_: {} on_state_create() + std::ignore = luaG_getfield(L_, -1, "on_state_create"); // L_: {} on_state_create() lua_remove(L_, -2); // L_: on_state_create() } STACK_CHECK(L_, 1); diff --git a/src/universe.cpp b/src/universe.cpp index 4f21306..548475e 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -150,10 +150,9 @@ void Universe::closeKeepers() void Universe::initializeAllocatorFunction(lua_State* L_) { STACK_CHECK_START_REL(L_, 1); // L_: settings - lua_getfield(L_, -1, "allocator"); // L_: settings allocator|nil|"protected" - if (!lua_isnil(L_, -1)) { + if (luaG_getfield(L_, -1, "allocator") != LuaType::NIL) { // L_: settings allocator|nil|"protected" // store C function pointer in an internal variable - provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator + provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator if (provideAllocator != nullptr) { // make sure the function doesn't have upvalues char const* upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? @@ -179,18 +178,16 @@ void Universe::initializeAllocatorFunction(lua_State* L_) lua_pop(L_, 1); // L_: settings STACK_CHECK(L_, 1); - lua_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" - { - char const* const _allocator{ lua_tostring(L_, -1) }; - if (strcmp(_allocator, "libc") == 0) { - internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; - } else if (provideAllocator == luaG_provide_protected_allocator) { - // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. - internalAllocator = protectedAllocator.makeDefinition(); - } else { - // no protection required, just use whatever we have as-is. - internalAllocator = protectedAllocator; - } + std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" + std::string_view const _allocator{ lua_tostringview(L_, -1) }; + if (_allocator == "libc") { + internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; + } else if (provideAllocator == luaG_provide_protected_allocator) { + // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. + internalAllocator = protectedAllocator.makeDefinition(); + } else { + // no protection required, just use whatever we have as-is. + internalAllocator = protectedAllocator; } lua_pop(L_, 1); // L_: settings STACK_CHECK(L_, 1); @@ -213,7 +210,7 @@ void Universe::initializeKeepers(lua_State* L_) { LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); STACK_CHECK_START_REL(L_, 0); // L_: settings - lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers + std::ignore = luaG_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers int const _nb_keepers{ static_cast(lua_tointeger(L_, -1)) }; lua_pop(L_, 1); // L_: settings if (_nb_keepers < 1) { @@ -221,7 +218,7 @@ void Universe::initializeKeepers(lua_State* L_) } STACK_CHECK(L_, 0); - lua_getfield(L_, 1, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold + std::ignore = luaG_getfield(L_, 1, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold int const keepers_gc_threshold{ static_cast(lua_tointeger(L_, -1)) }; lua_pop(L_, 1); // L_: settings STACK_CHECK(L_, 0); -- cgit v1.2.3-55-g6feb