aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp56
1 files changed, 4 insertions, 52 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
index 049a065..302d4cc 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -42,48 +42,6 @@ static constexpr RegistryUniqueKey kLookupCacheRegKey{ 0x9BF75F84E54B691Bull };
42 42
43// ################################################################################################# 43// #################################################################################################
44 44
45// does what the original 'push_registry_subtable' function did, but adds an optional mode argument to it
46void push_registry_subtable_mode(lua_State* L_, RegistryUniqueKey key_, const char* mode_)
47{
48 STACK_GROW(L_, 3);
49 STACK_CHECK_START_REL(L_, 0);
50
51 key_.pushValue(L_); // L_: {}|nil
52 STACK_CHECK(L_, 1);
53
54 if (lua_isnil(L_, -1)) {
55 lua_pop(L_, 1); // L_:
56 lua_newtable(L_); // L_: {}
57 // _R[key_] = {}
58 key_.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); // L_: {}
59 STACK_CHECK(L_, 1);
60
61 // Set its metatable if requested
62 if (mode_) {
63 lua_newtable(L_); // L_: {} mt
64 lua_pushliteral(L_, "__mode"); // L_: {} mt "__mode"
65 lua_pushstring(L_, mode_); // L_: {} mt "__mode" mode
66 lua_rawset(L_, -3); // L_: {} mt
67 lua_setmetatable(L_, -2); // L_: {}
68 }
69 }
70 STACK_CHECK(L_, 1);
71 LUA_ASSERT(L_, lua_istable(L_, -1));
72}
73
74// #################################################################################################
75
76/*
77 * Push a registry subtable (keyed by unique 'key_') onto the stack.
78 * If the subtable does not exist, it is created and chained.
79 */
80void push_registry_subtable(lua_State* L_, RegistryUniqueKey key_)
81{
82 push_registry_subtable_mode(L_, key_, nullptr);
83}
84
85// #################################################################################################
86
87// same as PUC-Lua l_alloc 45// same as PUC-Lua l_alloc
88extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) 46extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_)
89{ 47{
@@ -459,13 +417,7 @@ void populate_func_lookup_table(lua_State* L_, int i_, char const* name_)
459 STACK_CHECK(L_, 2); 417 STACK_CHECK(L_, 2);
460 } 418 }
461 // retrieve the cache, create it if we haven't done it yet 419 // retrieve the cache, create it if we haven't done it yet
462 kLookupCacheRegKey.pushValue(L_); // L_: {} {fqn} {cache}? 420 std::ignore = kLookupCacheRegKey.getSubTable(L_, 0, 0); // L_: {} {fqn} {cache}
463 if (lua_isnil(L_, -1)) {
464 lua_pop(L_, 1); // L_: {} {fqn}
465 lua_newtable(L_); // L_: {} {fqn} {cache}
466 kLookupCacheRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); });
467 STACK_CHECK(L_, 3);
468 }
469 // process everything we find in that table, filling in lookup data for all functions and tables we see there 421 // process everything we find in that table, filling in lookup data for all functions and tables we see there
470 populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(U) L_, dbIdx, in_base, startDepth); 422 populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(U) L_, dbIdx, in_base, startDepth);
471 lua_pop(L_, 3); // L_: 423 lua_pop(L_, 3); // L_:
@@ -491,7 +443,7 @@ static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull };
491 STACK_GROW(L_, 3); 443 STACK_GROW(L_, 3);
492 444
493 STACK_CHECK_START_REL(L_, 0); 445 STACK_CHECK_START_REL(L_, 0);
494 push_registry_subtable(L_, kMtIdRegKey); // L_: ... _R[kMtIdRegKey] 446 std::ignore = kMtIdRegKey.getSubTable(L_, 0, 0); // L_: ... _R[kMtIdRegKey]
495 lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} 447 lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt}
496 lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? 448 lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk?
497 449
@@ -1186,7 +1138,7 @@ void InterCopyContext::copy_cached_func() const
1186 STACK_CHECK_START_REL(L2, 0); 1138 STACK_CHECK_START_REL(L2, 0);
1187 STACK_GROW(L2, 4); 1139 STACK_GROW(L2, 4);
1188 // do we already know this metatable? 1140 // do we already know this metatable?
1189 push_registry_subtable(L2, kMtIdRegKey); // L2: _R[kMtIdRegKey] 1141 std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey]
1190 lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] id 1142 lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] id
1191 lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil 1143 lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil
1192 STACK_CHECK(L2, 2); 1144 STACK_CHECK(L2, 2);
@@ -1830,7 +1782,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const
1830 } 1782 }
1831 return InterCopyResult::Error; 1783 return InterCopyResult::Error;
1832 } 1784 }
1833 if (luaG_getpackage(L2) == LUA_TNIL) { // package library not loaded: do nothing 1785 if (luaG_getmodule(L2, LUA_LOADLIBNAME) == LuaType::NIL) { // package library not loaded: do nothing
1834 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END(U))); 1786 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "'package' not loaded, nothing to do\n" INDENT_END(U)));
1835 STACK_CHECK(L1, 0); 1787 STACK_CHECK(L1, 0);
1836 return InterCopyResult::Success; 1788 return InterCopyResult::Success;