diff options
-rw-r--r-- | src/deep.cpp | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/src/deep.cpp b/src/deep.cpp index bd340f4..398e13f 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -47,8 +47,8 @@ THE SOFTWARE. | |||
47 | * metatable -> factory | 47 | * metatable -> factory |
48 | * factory -> metatable | 48 | * factory -> metatable |
49 | */ | 49 | */ |
50 | // xxh64 of string "kDeepLookupRegKey" generated at https://www.pelock.com/products/hash-calculator | 50 | // xxh64 of string "kDeepFactoryMetatableBijectionRegKey" generated at https://www.pelock.com/products/hash-calculator |
51 | static constexpr RegistryUniqueKey kDeepLookupRegKey{ 0xC6788345703C6059ull }; | 51 | static constexpr RegistryUniqueKey kDeepFactoryMetatableBijectionRegKey{ 0xB4E223E56BF0BCD6ull }; |
52 | 52 | ||
53 | /* | 53 | /* |
54 | * The deep proxy cache is a weak valued table listing all deep UD proxies indexed by the deep UD that they are proxying | 54 | * The deep proxy cache is a weak valued table listing all deep UD proxies indexed by the deep UD that they are proxying |
@@ -65,16 +65,20 @@ namespace { | |||
65 | // ############################################################################################# | 65 | // ############################################################################################# |
66 | 66 | ||
67 | // Pops the key (metatable or factory) off the stack, and replaces with the deep lookup value (factory/metatable/nil). | 67 | // Pops the key (metatable or factory) off the stack, and replaces with the deep lookup value (factory/metatable/nil). |
68 | static void LookupDeep(lua_State* const L_) | 68 | static void LookupDeep(lua_State* const L_, LuaType const expectedType_) |
69 | { | 69 | { |
70 | STACK_GROW(L_, 1); | 70 | STACK_GROW(L_, 1); |
71 | STACK_CHECK_START_REL(L_, 1); // L_: a | 71 | STACK_CHECK_START_REL(L_, 1); // L_: a |
72 | kDeepLookupRegKey.pushValue(L_); // L_: a {} | 72 | kDeepFactoryMetatableBijectionRegKey.pushValue(L_); // L_: a {} |
73 | if (!lua_isnil(L_, -1)) { | 73 | if (!lua_isnil(L_, -1)) { |
74 | lua_insert(L_, -2); // L_: {} a | 74 | lua_insert(L_, -2); // L_: {} a |
75 | lua_rawget(L_, -2); // L_: {} b | 75 | lua_rawget(L_, -2); // L_: {} b |
76 | } | 76 | } |
77 | lua_remove(L_, -2); // L_: a|b | 77 | lua_remove(L_, -2); // L_: a|b |
78 | LuaType const _type{ luaG_type(L_, kIdxTop) }; | ||
79 | if (_type != LuaType::NIL && _type != expectedType_) { | ||
80 | raise_luaL_error(L_, "INTERNAL ERROR: Unexpected value."); | ||
81 | } | ||
78 | STACK_CHECK(L_, 1); | 82 | STACK_CHECK(L_, 1); |
79 | } | 83 | } |
80 | 84 | ||
@@ -160,7 +164,7 @@ DeepFactory* DeepFactory::LookupFactory(lua_State* const L_, StackIndex const in | |||
160 | } | 164 | } |
161 | 165 | ||
162 | // replace metatable with the factory pointer, if it is actually a deep userdata | 166 | // replace metatable with the factory pointer, if it is actually a deep userdata |
163 | LookupDeep(L_); // L_: deep ... factory|nil | 167 | LookupDeep(L_, LuaType::LIGHTUSERDATA); // L_: deep ... factory|nil |
164 | 168 | ||
165 | DeepFactory* const _ret{ luaG_tolightuserdata<DeepFactory>(L_, kIdxTop) }; // nullptr if not a userdata | 169 | DeepFactory* const _ret{ luaG_tolightuserdata<DeepFactory>(L_, kIdxTop) }; // nullptr if not a userdata |
166 | lua_pop(L_, 1); | 170 | lua_pop(L_, 1); |
@@ -206,7 +210,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, | |||
206 | // Get/create metatable for 'factory' (in this state) | 210 | // Get/create metatable for 'factory' (in this state) |
207 | DeepFactory& _factory = prelude_->factory; | 211 | DeepFactory& _factory = prelude_->factory; |
208 | lua_pushlightuserdata(L_, std::bit_cast<void*>(&_factory)); // L_: DPC proxy _factory | 212 | lua_pushlightuserdata(L_, std::bit_cast<void*>(&_factory)); // L_: DPC proxy _factory |
209 | LookupDeep(L_); // L_: DPC proxy metatable|nil | 213 | LookupDeep(L_, LuaType::TABLE); // L_: DPC proxy metatable|nil |
210 | 214 | ||
211 | if (lua_isnil(L_, -1)) { // No metatable yet. | 215 | if (lua_isnil(L_, -1)) { // No metatable yet. |
212 | lua_pop(L_, 1); // L_: DPC proxy | 216 | lua_pop(L_, 1); // L_: DPC proxy |
@@ -345,7 +349,7 @@ void DeepFactory::storeDeepLookup(lua_State* const L_) const | |||
345 | // the deep metatable is at the top of the stack // L_: mt | 349 | // the deep metatable is at the top of the stack // L_: mt |
346 | STACK_GROW(L_, 3); | 350 | STACK_GROW(L_, 3); |
347 | STACK_CHECK_START_REL(L_, 0); // L_: mt | 351 | STACK_CHECK_START_REL(L_, 0); // L_: mt |
348 | std::ignore = kDeepLookupRegKey.getSubTable(L_, NArr{ 0 }, NRec{ 0 }); // L_: mt {} | 352 | std::ignore = kDeepFactoryMetatableBijectionRegKey.getSubTable(L_, NArr{ 0 }, NRec{ 0 }); // L_: mt {} |
349 | lua_pushvalue(L_, -2); // L_: mt {} mt | 353 | lua_pushvalue(L_, -2); // L_: mt {} mt |
350 | lua_pushlightuserdata(L_, std::bit_cast<void*>(this)); // L_: mt {} mt factory | 354 | lua_pushlightuserdata(L_, std::bit_cast<void*>(this)); // L_: mt {} mt factory |
351 | lua_rawset(L_, -3); // L_: mt {} | 355 | lua_rawset(L_, -3); // L_: mt {} |