diff options
Diffstat (limited to 'src/deep.cpp')
-rw-r--r-- | src/deep.cpp | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/src/deep.cpp b/src/deep.cpp index 5a62000..10f589e 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -66,19 +66,19 @@ static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull | |||
66 | * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. | 66 | * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. |
67 | * Pops the both values off the stack. | 67 | * Pops the both values off the stack. |
68 | */ | 68 | */ |
69 | static void set_deep_lookup(lua_State* L) | 69 | static void set_deep_lookup(lua_State* L_) |
70 | { | 70 | { |
71 | STACK_GROW( L, 3); | 71 | STACK_GROW( L_, 3); |
72 | STACK_CHECK_START_REL(L, 2); // a b | 72 | STACK_CHECK_START_REL(L_, 2); // a b |
73 | push_registry_subtable( L, kDeepLookupRegKey); // a b {} | 73 | push_registry_subtable( L_, kDeepLookupRegKey); // a b {} |
74 | STACK_CHECK( L, 3); | 74 | STACK_CHECK( L_, 3); |
75 | lua_insert( L, -3); // {} a b | 75 | lua_insert( L_, -3); // {} a b |
76 | lua_pushvalue( L, -1); // {} a b b | 76 | lua_pushvalue( L_, -1); // {} a b b |
77 | lua_pushvalue( L,-3); // {} a b b a | 77 | lua_pushvalue( L_,-3); // {} a b b a |
78 | lua_rawset( L, -5); // {} a b | 78 | lua_rawset( L_, -5); // {} a b |
79 | lua_rawset( L, -3); // {} | 79 | lua_rawset( L_, -3); // {} |
80 | lua_pop( L, 1); // | 80 | lua_pop( L_, 1); // |
81 | STACK_CHECK( L, 0); | 81 | STACK_CHECK( L_, 0); |
82 | } | 82 | } |
83 | 83 | ||
84 | // ################################################################################################# | 84 | // ################################################################################################# |
@@ -87,18 +87,18 @@ static void set_deep_lookup(lua_State* L) | |||
87 | * Pops the key (metatable or factory) off the stack, and replaces with the | 87 | * Pops the key (metatable or factory) off the stack, and replaces with the |
88 | * deep lookup value (factory/metatable/nil). | 88 | * deep lookup value (factory/metatable/nil). |
89 | */ | 89 | */ |
90 | static void get_deep_lookup(lua_State* L) | 90 | static void get_deep_lookup(lua_State* L_) |
91 | { | 91 | { |
92 | STACK_GROW( L, 1); | 92 | STACK_GROW( L_, 1); |
93 | STACK_CHECK_START_REL(L, 1); // a | 93 | STACK_CHECK_START_REL(L_, 1); // a |
94 | kDeepLookupRegKey.pushValue(L); // a {} | 94 | kDeepLookupRegKey.pushValue(L_); // a {} |
95 | if (!lua_isnil( L, -1)) | 95 | if (!lua_isnil( L_, -1)) |
96 | { | 96 | { |
97 | lua_insert( L, -2); // {} a | 97 | lua_insert( L_, -2); // {} a |
98 | lua_rawget( L, -2); // {} b | 98 | lua_rawget( L_, -2); // {} b |
99 | } | 99 | } |
100 | lua_remove( L, -2); // a|b | 100 | lua_remove( L_, -2); // a|b |
101 | STACK_CHECK( L, 1); | 101 | STACK_CHECK( L_, 1); |
102 | } | 102 | } |
103 | 103 | ||
104 | // ################################################################################################# | 104 | // ################################################################################################# |
@@ -107,12 +107,12 @@ static void get_deep_lookup(lua_State* L) | |||
107 | * Return the registered factory for 'index' (deep userdata proxy), | 107 | * Return the registered factory for 'index' (deep userdata proxy), |
108 | * or nullptr if 'index' is not a deep userdata proxy. | 108 | * or nullptr if 'index' is not a deep userdata proxy. |
109 | */ | 109 | */ |
110 | [[nodiscard]] static inline DeepFactory* get_factory(lua_State* L, int index, LookupMode mode_) | 110 | [[nodiscard]] static inline DeepFactory* get_factory(lua_State* L_, int index, LookupMode mode_) |
111 | { | 111 | { |
112 | // when looking inside a keeper, we are 100% sure the object is a deep userdata | 112 | // when looking inside a keeper, we are 100% sure the object is a deep userdata |
113 | if (mode_ == LookupMode::FromKeeper) | 113 | if (mode_ == LookupMode::FromKeeper) |
114 | { | 114 | { |
115 | DeepPrelude* const proxy{ *lua_tofulluserdata<DeepPrelude*>(L, index) }; | 115 | DeepPrelude* const proxy{ *lua_tofulluserdata<DeepPrelude*>(L_, index) }; |
116 | // we can (and must) cast and fetch the internally stored factory | 116 | // we can (and must) cast and fetch the internally stored factory |
117 | return &proxy->m_factory; | 117 | return &proxy->m_factory; |
118 | } | 118 | } |
@@ -121,31 +121,31 @@ static void get_deep_lookup(lua_State* L) | |||
121 | // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database | 121 | // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database |
122 | // it is the only way to ensure that the userdata is indeed a deep userdata! | 122 | // it is the only way to ensure that the userdata is indeed a deep userdata! |
123 | // of course, we could just trust the caller, but we won't | 123 | // of course, we could just trust the caller, but we won't |
124 | STACK_GROW( L, 1); | 124 | STACK_GROW( L_, 1); |
125 | STACK_CHECK_START_REL(L, 0); | 125 | STACK_CHECK_START_REL(L_, 0); |
126 | 126 | ||
127 | if (!lua_getmetatable( L, index)) // deep ... metatable? | 127 | if (!lua_getmetatable( L_, index)) // deep ... metatable? |
128 | { | 128 | { |
129 | return nullptr; // no metatable: can't be a deep userdata object! | 129 | return nullptr; // no metatable: can't be a deep userdata object! |
130 | } | 130 | } |
131 | 131 | ||
132 | // replace metatable with the factory pointer, if it is actually a deep userdata | 132 | // replace metatable with the factory pointer, if it is actually a deep userdata |
133 | get_deep_lookup( L); // deep ... factory|nil | 133 | get_deep_lookup( L_); // deep ... factory|nil |
134 | 134 | ||
135 | DeepFactory* const ret{ lua_tolightuserdata<DeepFactory>(L, -1) }; // nullptr if not a userdata | 135 | DeepFactory* const ret{ lua_tolightuserdata<DeepFactory>(L_, -1) }; // nullptr if not a userdata |
136 | lua_pop( L, 1); | 136 | lua_pop( L_, 1); |
137 | STACK_CHECK( L, 0); | 137 | STACK_CHECK( L_, 0); |
138 | return ret; | 138 | return ret; |
139 | } | 139 | } |
140 | } | 140 | } |
141 | 141 | ||
142 | // ################################################################################################# | 142 | // ################################################################################################# |
143 | 143 | ||
144 | void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | 144 | void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) |
145 | { | 145 | { |
146 | STACK_CHECK_START_REL(L, 0); | 146 | STACK_CHECK_START_REL(L_, 0); |
147 | o_->m_factory.deleteDeepObjectInternal(L, o_); | 147 | o_->m_factory.deleteDeepObjectInternal(L_, o_); |
148 | STACK_CHECK(L, 0); | 148 | STACK_CHECK(L_, 0); |
149 | } | 149 | } |
150 | 150 | ||
151 | // ################################################################################################# | 151 | // ################################################################################################# |
@@ -156,9 +156,9 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
156 | * End of life for a proxy object; reduce the deep reference count and clean it up if reaches 0. | 156 | * End of life for a proxy object; reduce the deep reference count and clean it up if reaches 0. |
157 | * | 157 | * |
158 | */ | 158 | */ |
159 | [[nodiscard]] static int deep_userdata_gc(lua_State* L) | 159 | [[nodiscard]] static int deep_userdata_gc(lua_State* L_) |
160 | { | 160 | { |
161 | DeepPrelude* const* const proxy{ lua_tofulluserdata<DeepPrelude*>(L, 1) }; | 161 | DeepPrelude* const* const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, 1) }; |
162 | DeepPrelude* const p{ *proxy }; | 162 | DeepPrelude* const p{ *proxy }; |
163 | 163 | ||
164 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded | 164 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded |
@@ -168,14 +168,14 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
168 | if (isLastRef) | 168 | if (isLastRef) |
169 | { | 169 | { |
170 | // retrieve wrapped __gc | 170 | // retrieve wrapped __gc |
171 | lua_pushvalue( L, lua_upvalueindex( 1)); // self __gc? | 171 | lua_pushvalue( L_, lua_upvalueindex( 1)); // self __gc? |
172 | if (!lua_isnil( L, -1)) | 172 | if (!lua_isnil( L_, -1)) |
173 | { | 173 | { |
174 | lua_insert( L, -2); // __gc self | 174 | lua_insert( L_, -2); // __gc self |
175 | lua_call( L, 1, 0); // | 175 | lua_call( L_, 1, 0); // |
176 | } | 176 | } |
177 | // we don't really know what remains on the stack at that point (depending on us finding a __gc or not), but we don't care | 177 | // we don't really know what remains on the stack at that point (depending on us finding a __gc or not), but we don't care |
178 | DeepFactory::DeleteDeepObject(L, p); | 178 | DeepFactory::DeleteDeepObject(L_, p); |
179 | } | 179 | } |
180 | return 0; | 180 | return 0; |
181 | } | 181 | } |
@@ -388,17 +388,17 @@ int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const | |||
388 | * Reference count is not changed, and access to the deep userdata is not | 388 | * Reference count is not changed, and access to the deep userdata is not |
389 | * serialized. It is the module's responsibility to prevent conflicting usage. | 389 | * serialized. It is the module's responsibility to prevent conflicting usage. |
390 | */ | 390 | */ |
391 | DeepPrelude* DeepFactory::toDeep(lua_State* L, int index) const | 391 | DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index) const |
392 | { | 392 | { |
393 | STACK_CHECK_START_REL(L, 0); | 393 | STACK_CHECK_START_REL(L_, 0); |
394 | // ensure it is actually a deep userdata we created | 394 | // ensure it is actually a deep userdata we created |
395 | if (get_factory(L, index, LookupMode::LaneBody) != this) | 395 | if (get_factory(L_, index, LookupMode::LaneBody) != this) |
396 | { | 396 | { |
397 | return nullptr; // no metatable, or wrong kind | 397 | return nullptr; // no metatable, or wrong kind |
398 | } | 398 | } |
399 | STACK_CHECK(L, 0); | 399 | STACK_CHECK(L_, 0); |
400 | 400 | ||
401 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L, index) }; | 401 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L_, index) }; |
402 | return *proxy; | 402 | return *proxy; |
403 | } | 403 | } |
404 | 404 | ||