diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-22 14:43:07 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-05-22 14:43:07 +0200 |
commit | b639c229e3fdef21cec4535284eeabbca361dad6 (patch) | |
tree | 075fdcdfdf08053a3075fda1d9602eaa2eee1aaa /src | |
parent | e972ee3b65bc85dbee8e1e7f74594490037dcb67 (diff) | |
download | lanes-b639c229e3fdef21cec4535284eeabbca361dad6.tar.gz lanes-b639c229e3fdef21cec4535284eeabbca361dad6.tar.bz2 lanes-b639c229e3fdef21cec4535284eeabbca361dad6.zip |
lua503_getfield → strong typed luaG_getfield
Diffstat (limited to 'src')
-rw-r--r-- | src/compat.cpp | 4 | ||||
-rw-r--r-- | src/compat.h | 72 | ||||
-rw-r--r-- | src/deep.cpp | 9 | ||||
-rw-r--r-- | src/intercopycontext.cpp | 12 | ||||
-rw-r--r-- | src/lane.cpp | 2 | ||||
-rw-r--r-- | src/lanes.cpp | 15 | ||||
-rw-r--r-- | src/state.cpp | 5 | ||||
-rw-r--r-- | src/universe.cpp | 31 |
8 files changed, 68 insertions, 82 deletions
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 @@ | |||
14 | LuaType luaG_getmodule(lua_State* L_, char const* name_) | 14 | LuaType luaG_getmodule(lua_State* L_, char const* name_) |
15 | { | 15 | { |
16 | STACK_CHECK_START_REL(L_, 0); | 16 | STACK_CHECK_START_REL(L_, 0); |
17 | LuaType _type{ static_cast<LuaType>(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil | 17 | LuaType _type{ luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) }; // L_: _R._LOADED|nil |
18 | if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil | 18 | if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil |
19 | STACK_CHECK(L_, 1); | 19 | STACK_CHECK(L_, 1); |
20 | return _type; | 20 | return _type; |
21 | } | 21 | } |
22 | _type = static_cast<LuaType>(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil | 22 | _type = luaG_getfield(L_, -1, name_); // L_: _R._LOADED {module}|nil |
23 | lua_remove(L_, -2); // L_: {module}|nil | 23 | lua_remove(L_, -2); // L_: {module}|nil |
24 | STACK_CHECK(L_, 1); | 24 | STACK_CHECK(L_, 1); |
25 | return _type; | 25 | 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" | |||
31 | 31 | ||
32 | // ################################################################################################# | 32 | // ################################################################################################# |
33 | 33 | ||
34 | // a strong-typed wrapper over lua types to see them easier in a debugger | ||
35 | enum class LuaType | ||
36 | { | ||
37 | NONE = LUA_TNONE, | ||
38 | NIL = LUA_TNIL, | ||
39 | BOOLEAN = LUA_TBOOLEAN, | ||
40 | LIGHTUSERDATA = LUA_TLIGHTUSERDATA, | ||
41 | NUMBER = LUA_TNUMBER, | ||
42 | STRING = LUA_TSTRING, | ||
43 | TABLE = LUA_TTABLE, | ||
44 | FUNCTION = LUA_TFUNCTION, | ||
45 | USERDATA = LUA_TUSERDATA, | ||
46 | THREAD = LUA_TTHREAD, | ||
47 | CDATA = 10 // LuaJIT CDATA | ||
48 | }; | ||
49 | |||
50 | inline LuaType lua_type_as_enum(lua_State* L_, int idx_) | ||
51 | { | ||
52 | return static_cast<LuaType>(lua_type(L_, idx_)); | ||
53 | } | ||
54 | inline char const* lua_typename(lua_State* L_, LuaType t_) | ||
55 | { | ||
56 | return lua_typename(L_, static_cast<int>(t_)); | ||
57 | } | ||
58 | |||
59 | // ################################################################################################# | ||
60 | |||
34 | // add some Lua 5.3-style API when building for Lua 5.1 | 61 | // add some Lua 5.3-style API when building for Lua 5.1 |
35 | #if LUA_VERSION_NUM == 501 | 62 | #if LUA_VERSION_NUM == 501 |
36 | 63 | ||
@@ -106,22 +133,16 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u | |||
106 | 133 | ||
107 | // ################################################################################################# | 134 | // ################################################################################################# |
108 | 135 | ||
109 | #if LUA_VERSION_NUM < 503 | 136 | [[nodiscard]] inline LuaType luaG_getfield(lua_State* L_, int idx_, char const* k_) |
110 | // starting with Lua 5.3, lua_getfield returns the type of the value it found | ||
111 | inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) | ||
112 | { | 137 | { |
138 | // starting with Lua 5.3, lua_getfield returns the type of the value it found | ||
139 | #if LUA_VERSION_NUM < 503 | ||
113 | lua_getfield(L_, idx_, k_); | 140 | lua_getfield(L_, idx_, k_); |
114 | return lua_type(L_, -1); | 141 | return lua_type_as_enum(L_, -1); |
115 | } | ||
116 | |||
117 | #else // LUA_VERSION_NUM >= 503 | 142 | #else // LUA_VERSION_NUM >= 503 |
118 | 143 | return static_cast<LuaType>(lua_getfield(L_, idx_, k_)); | |
119 | inline int lua503_getfield(lua_State* L_, int idx_, char const* k_) | ||
120 | { | ||
121 | return lua_getfield(L_, idx_, k_); | ||
122 | } | ||
123 | |||
124 | #endif // LUA_VERSION_NUM >= 503 | 144 | #endif // LUA_VERSION_NUM >= 503 |
145 | } | ||
125 | 146 | ||
126 | // ################################################################################################# | 147 | // ################################################################################################# |
127 | 148 | ||
@@ -202,33 +223,6 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) | |||
202 | 223 | ||
203 | // ################################################################################################# | 224 | // ################################################################################################# |
204 | 225 | ||
205 | // a strong-typed wrapper over lua types to see them easier in a debugger | ||
206 | enum class LuaType | ||
207 | { | ||
208 | NONE = LUA_TNONE, | ||
209 | NIL = LUA_TNIL, | ||
210 | BOOLEAN = LUA_TBOOLEAN, | ||
211 | LIGHTUSERDATA = LUA_TLIGHTUSERDATA, | ||
212 | NUMBER = LUA_TNUMBER, | ||
213 | STRING = LUA_TSTRING, | ||
214 | TABLE = LUA_TTABLE, | ||
215 | FUNCTION = LUA_TFUNCTION, | ||
216 | USERDATA = LUA_TUSERDATA, | ||
217 | THREAD = LUA_TTHREAD, | ||
218 | CDATA = 10 // LuaJIT CDATA | ||
219 | }; | ||
220 | |||
221 | inline LuaType lua_type_as_enum(lua_State* L_, int idx_) | ||
222 | { | ||
223 | return static_cast<LuaType>(lua_type(L_, idx_)); | ||
224 | } | ||
225 | inline char const* lua_typename(lua_State* L_, LuaType t_) | ||
226 | { | ||
227 | return lua_typename(L_, static_cast<int>(t_)); | ||
228 | } | ||
229 | |||
230 | // ################################################################################################# | ||
231 | |||
232 | // a strong-typed wrapper over lua error codes to see them easier in a debugger | 226 | // a strong-typed wrapper over lua error codes to see them easier in a debugger |
233 | enum class LuaError | 227 | enum class LuaError |
234 | { | 228 | { |
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_, | |||
224 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; | 224 | return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; |
225 | } | 225 | } |
226 | // if the metatable contains a __gc, we will call it from our own | 226 | // if the metatable contains a __gc, we will call it from our own |
227 | lua_getfield(L_, -1, "__gc"); // L_: DPC proxy metatable __gc | 227 | std::ignore = luaG_getfield(L_, -1, "__gc"); // L_: DPC proxy metatable __gc |
228 | } else { | 228 | } else { |
229 | // keepers need a minimal metatable that only contains our own __gc | 229 | // keepers need a minimal metatable that only contains our own __gc |
230 | lua_createtable(L_, 0, 1); // L_: DPC proxy metatable | 230 | lua_createtable(L_, 0, 1); // L_: DPC proxy metatable |
@@ -250,12 +250,11 @@ std::string_view DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, | |||
250 | // check that the module is already loaded (or being loaded, we are happy either way) | 250 | // check that the module is already loaded (or being loaded, we are happy either way) |
251 | if (lua_isfunction(L_, -1)) { | 251 | if (lua_isfunction(L_, -1)) { |
252 | lua_pushlstring(L_, _modname.data(), _modname.size()); // L_: DPC proxy metatable require() "module" | 252 | lua_pushlstring(L_, _modname.data(), _modname.size()); // L_: DPC proxy metatable require() "module" |
253 | lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // L_: DPC proxy metatable require() "module" _R._LOADED | 253 | if (luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) == LuaType::TABLE) { // L_: DPC proxy metatable require() "module" _R._LOADED |
254 | if (lua_istable(L_, -1)) { | ||
255 | lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" | 254 | lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" |
256 | lua_rawget(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED module | 255 | lua_rawget(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED module |
257 | int const alreadyloaded = lua_toboolean(L_, -1); | 256 | int const _alreadyloaded{ lua_toboolean(L_, -1) }; |
258 | if (!alreadyloaded) { // not loaded | 257 | if (!_alreadyloaded) { // not loaded |
259 | lua_pop(L_, 2); // L_: DPC proxy metatable require() "module" | 258 | lua_pop(L_, 2); // L_: DPC proxy metatable require() "module" |
260 | // require "modname" | 259 | // require "modname" |
261 | LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? | 260 | 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 | |||
651 | } | 651 | } |
652 | 652 | ||
653 | // no __lanesclone? -> not clonable | 653 | // no __lanesclone? -> not clonable |
654 | lua_getfield(L1, -1, "__lanesclone"); // L1: ... mt __lanesclone? | 654 | if (luaG_getfield(L1, -1, "__lanesclone") == LuaType::NIL) { // L1: ... mt nil |
655 | if (lua_isnil(L1, -1)) { | ||
656 | lua_pop(L1, 2); // L1: ... | 655 | lua_pop(L1, 2); // L1: ... |
657 | STACK_CHECK(L1, 0); | 656 | STACK_CHECK(L1, 0); |
658 | return false; | 657 | return false; |
@@ -865,7 +864,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const | |||
865 | // perform the custom cloning part | 864 | // perform the custom cloning part |
866 | lua_insert(L2, -2); // L2: ... u mt | 865 | lua_insert(L2, -2); // L2: ... u mt |
867 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with | 866 | // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with |
868 | lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone | 867 | std::ignore = luaG_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone |
869 | lua_remove(L2, -2); // L2: ... u __lanesclone | 868 | lua_remove(L2, -2); // L2: ... u __lanesclone |
870 | lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone | 869 | lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone |
871 | lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source | 870 | lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source |
@@ -1083,8 +1082,8 @@ static char const* vt_names[] = { | |||
1083 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } | 1082 | // Non-POD can be skipped if its metatable contains { __lanesignore = true } |
1084 | if (((1 << static_cast<int>(_val_type)) & kPODmask) == 0) { | 1083 | if (((1 << static_cast<int>(_val_type)) & kPODmask) == 0) { |
1085 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt | 1084 | if (lua_getmetatable(L1, L1_i)) { // L1: ... mt |
1086 | lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? | 1085 | LuaType const _type{ luaG_getfield(L1, -1, "__lanesignore") }; // L1: ... mt ignore? |
1087 | if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { | 1086 | if (_type == LuaType::BOOLEAN && lua_toboolean(L1, -1)) { |
1088 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); | 1087 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); |
1089 | _val_type = LuaType::NIL; | 1088 | _val_type = LuaType::NIL; |
1090 | } | 1089 | } |
@@ -1195,8 +1194,7 @@ static char const* vt_names[] = { | |||
1195 | continue; | 1194 | continue; |
1196 | } | 1195 | } |
1197 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); | 1196 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); |
1198 | lua_getfield(L1, L1_i, _entry); | 1197 | if (luaG_getfield(L1, L1_i, _entry) == LuaType::NIL) { |
1199 | if (lua_isnil(L1, -1)) { | ||
1200 | lua_pop(L1, 1); | 1198 | lua_pop(L1, 1); |
1201 | } else { | 1199 | } else { |
1202 | { | 1200 | { |
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) | |||
340 | 340 | ||
341 | default: // unknown key | 341 | default: // unknown key |
342 | lua_getmetatable(L_, kSelf); // L_: mt | 342 | lua_getmetatable(L_, kSelf); // L_: mt |
343 | lua_getfield(L_, -1, "cached_error"); // L_: mt error | 343 | std::ignore = luaG_getfield(L_, -1, "cached_error"); // L_: mt error |
344 | lua_pushliteral(L_, "Unknown key: "); // L_: mt error "Unknown key: " | 344 | lua_pushliteral(L_, "Unknown key: "); // L_: mt error "Unknown key: " |
345 | lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k | 345 | lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k |
346 | lua_concat(L_, 2); // L_: mt error "Unknown key: <k>" | 346 | lua_concat(L_, 2); // L_: mt error "Unknown key: <k>" |
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) | |||
553 | 553 | ||
554 | STACK_CHECK_START_REL(L_, 0); | 554 | STACK_CHECK_START_REL(L_, 0); |
555 | auto _readInteger = [L = L_](char const* name_) { | 555 | auto _readInteger = [L = L_](char const* name_) { |
556 | lua_getfield(L, 1, name_); | 556 | std::ignore = luaG_getfield(L, 1, name_); |
557 | lua_Integer const val{ lua_tointeger(L, -1) }; | 557 | lua_Integer const val{ lua_tointeger(L, -1) }; |
558 | lua_pop(L, 1); | 558 | lua_pop(L, 1); |
559 | return static_cast<int>(val); | 559 | return static_cast<int>(val); |
@@ -569,8 +569,7 @@ LUAG_FUNC(wakeup_conv) | |||
569 | // If Lua table has '.isdst' we trust that. If it does not, we'll let | 569 | // If Lua table has '.isdst' we trust that. If it does not, we'll let |
570 | // 'mktime' decide on whether the time is within DST or not (value -1). | 570 | // 'mktime' decide on whether the time is within DST or not (value -1). |
571 | // | 571 | // |
572 | lua_getfield(L_, 1, "isdst"); | 572 | int const _isdst{ (luaG_getfield(L_, 1, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 }; |
573 | int const _isdst{ lua_isboolean(L_, -1) ? lua_toboolean(L_, -1) : -1 }; | ||
574 | lua_pop(L_, 1); | 573 | lua_pop(L_, 1); |
575 | STACK_CHECK(L_, 0); | 574 | STACK_CHECK(L_, 0); |
576 | 575 | ||
@@ -641,20 +640,20 @@ LUAG_FUNC(configure) | |||
641 | _U = universe_create(L_); // L_: settings universe | 640 | _U = universe_create(L_); // L_: settings universe |
642 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); | 641 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); |
643 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} | 642 | lua_createtable(L_, 0, 1); // L_: settings universe {mt} |
644 | lua_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout | 643 | std::ignore = luaG_getfield(L_, 1, "shutdown_timeout"); // L_: settings universe {mt} shutdown_timeout |
645 | lua_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode | 644 | std::ignore = luaG_getfield(L_, 1, "shutdown_mode"); // L_: settings universe {mt} shutdown_timeout shutdown_mode |
646 | lua_pushcclosure(L_, universe_gc, 2); // L_: settings universe {mt} universe_gc | 645 | lua_pushcclosure(L_, universe_gc, 2); // L_: settings universe {mt} universe_gc |
647 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} | 646 | lua_setfield(L_, -2, "__gc"); // L_: settings universe {mt} |
648 | lua_setmetatable(L_, -2); // L_: settings universe | 647 | lua_setmetatable(L_, -2); // L_: settings universe |
649 | lua_pop(L_, 1); // L_: settings | 648 | lua_pop(L_, 1); // L_: settings |
650 | lua_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors | 649 | std::ignore = luaG_getfield(L_, 1, "verbose_errors"); // L_: settings verbose_errors |
651 | _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; | 650 | _U->verboseErrors = lua_toboolean(L_, -1) ? true : false; |
652 | lua_pop(L_, 1); // L_: settings | 651 | lua_pop(L_, 1); // L_: settings |
653 | lua_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata | 652 | std::ignore = luaG_getfield(L_, 1, "demote_full_userdata"); // L_: settings demote_full_userdata |
654 | _U->demoteFullUserdata = lua_toboolean(L_, -1) ? true : false; | 653 | _U->demoteFullUserdata = lua_toboolean(L_, -1) ? true : false; |
655 | lua_pop(L_, 1); // L_: settings | 654 | lua_pop(L_, 1); // L_: settings |
656 | #if HAVE_LANE_TRACKING() | 655 | #if HAVE_LANE_TRACKING() |
657 | lua_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes | 656 | std::ignore = luaG_getfield(L_, 1, "track_lanes"); // L_: settings track_lanes |
658 | if (lua_toboolean(L_, -1)) { | 657 | if (lua_toboolean(L_, -1)) { |
659 | _U->tracker.activate(); | 658 | _U->tracker.activate(); |
660 | } | 659 | } |
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_) | |||
218 | void InitializeOnStateCreate(Universe* U_, lua_State* L_) | 218 | void InitializeOnStateCreate(Universe* U_, lua_State* L_) |
219 | { | 219 | { |
220 | STACK_CHECK_START_REL(L_, 1); // L_: settings | 220 | STACK_CHECK_START_REL(L_, 1); // L_: settings |
221 | lua_getfield(L_, -1, "on_state_create"); // L_: settings on_state_create|nil | 221 | if (luaG_getfield(L_, -1, "on_state_create") != LuaType::NIL) { // L_: settings on_state_create|nil |
222 | if (!lua_isnil(L_, -1)) { | ||
223 | // store C function pointer in an internal variable | 222 | // store C function pointer in an internal variable |
224 | U_->onStateCreateFunc = lua_tocfunction(L_, -1); // L_: settings on_state_create | 223 | U_->onStateCreateFunc = lua_tocfunction(L_, -1); // L_: settings on_state_create |
225 | if (U_->onStateCreateFunc != nullptr) { | 224 | if (U_->onStateCreateFunc != nullptr) { |
@@ -289,7 +288,7 @@ void CallOnStateCreate(Universe* U_, lua_State* L_, lua_State* from_, LookupMode | |||
289 | } | 288 | } |
290 | kConfigRegKey.pushValue(L_); // L_: {} | 289 | kConfigRegKey.pushValue(L_); // L_: {} |
291 | STACK_CHECK(L_, 1); | 290 | STACK_CHECK(L_, 1); |
292 | lua_getfield(L_, -1, "on_state_create"); // L_: {} on_state_create() | 291 | std::ignore = luaG_getfield(L_, -1, "on_state_create"); // L_: {} on_state_create() |
293 | lua_remove(L_, -2); // L_: on_state_create() | 292 | lua_remove(L_, -2); // L_: on_state_create() |
294 | } | 293 | } |
295 | STACK_CHECK(L_, 1); | 294 | 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() | |||
150 | void Universe::initializeAllocatorFunction(lua_State* L_) | 150 | void Universe::initializeAllocatorFunction(lua_State* L_) |
151 | { | 151 | { |
152 | STACK_CHECK_START_REL(L_, 1); // L_: settings | 152 | STACK_CHECK_START_REL(L_, 1); // L_: settings |
153 | lua_getfield(L_, -1, "allocator"); // L_: settings allocator|nil|"protected" | 153 | if (luaG_getfield(L_, -1, "allocator") != LuaType::NIL) { // L_: settings allocator|nil|"protected" |
154 | if (!lua_isnil(L_, -1)) { | ||
155 | // store C function pointer in an internal variable | 154 | // store C function pointer in an internal variable |
156 | provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator | 155 | provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator |
157 | if (provideAllocator != nullptr) { | 156 | if (provideAllocator != nullptr) { |
158 | // make sure the function doesn't have upvalues | 157 | // make sure the function doesn't have upvalues |
159 | char const* upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? | 158 | char const* upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? |
@@ -179,18 +178,16 @@ void Universe::initializeAllocatorFunction(lua_State* L_) | |||
179 | lua_pop(L_, 1); // L_: settings | 178 | lua_pop(L_, 1); // L_: settings |
180 | STACK_CHECK(L_, 1); | 179 | STACK_CHECK(L_, 1); |
181 | 180 | ||
182 | lua_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" | 181 | std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" |
183 | { | 182 | std::string_view const _allocator{ lua_tostringview(L_, -1) }; |
184 | char const* const _allocator{ lua_tostring(L_, -1) }; | 183 | if (_allocator == "libc") { |
185 | if (strcmp(_allocator, "libc") == 0) { | 184 | internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; |
186 | internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; | 185 | } else if (provideAllocator == luaG_provide_protected_allocator) { |
187 | } else if (provideAllocator == luaG_provide_protected_allocator) { | 186 | // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. |
188 | // user wants mutex protection on the state's allocator. Use protection for our own allocations too, just in case. | 187 | internalAllocator = protectedAllocator.makeDefinition(); |
189 | internalAllocator = protectedAllocator.makeDefinition(); | 188 | } else { |
190 | } else { | 189 | // no protection required, just use whatever we have as-is. |
191 | // no protection required, just use whatever we have as-is. | 190 | internalAllocator = protectedAllocator; |
192 | internalAllocator = protectedAllocator; | ||
193 | } | ||
194 | } | 191 | } |
195 | lua_pop(L_, 1); // L_: settings | 192 | lua_pop(L_, 1); // L_: settings |
196 | STACK_CHECK(L_, 1); | 193 | STACK_CHECK(L_, 1); |
@@ -213,7 +210,7 @@ void Universe::initializeKeepers(lua_State* L_) | |||
213 | { | 210 | { |
214 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); | 211 | LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); |
215 | STACK_CHECK_START_REL(L_, 0); // L_: settings | 212 | STACK_CHECK_START_REL(L_, 0); // L_: settings |
216 | lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers | 213 | std::ignore = luaG_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers |
217 | int const _nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; | 214 | int const _nb_keepers{ static_cast<int>(lua_tointeger(L_, -1)) }; |
218 | lua_pop(L_, 1); // L_: settings | 215 | lua_pop(L_, 1); // L_: settings |
219 | if (_nb_keepers < 1) { | 216 | if (_nb_keepers < 1) { |
@@ -221,7 +218,7 @@ void Universe::initializeKeepers(lua_State* L_) | |||
221 | } | 218 | } |
222 | STACK_CHECK(L_, 0); | 219 | STACK_CHECK(L_, 0); |
223 | 220 | ||
224 | lua_getfield(L_, 1, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold | 221 | std::ignore = luaG_getfield(L_, 1, "keepers_gc_threshold"); // L_: settings keepers_gc_threshold |
225 | int const keepers_gc_threshold{ static_cast<int>(lua_tointeger(L_, -1)) }; | 222 | int const keepers_gc_threshold{ static_cast<int>(lua_tointeger(L_, -1)) }; |
226 | lua_pop(L_, 1); // L_: settings | 223 | lua_pop(L_, 1); // L_: settings |
227 | STACK_CHECK(L_, 0); | 224 | STACK_CHECK(L_, 0); |