diff options
Diffstat (limited to 'src/deep.cpp')
-rw-r--r-- | src/deep.cpp | 56 |
1 files changed, 28 insertions, 28 deletions
diff --git a/src/deep.cpp b/src/deep.cpp index 3eb38dc..3326f98 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -81,7 +81,7 @@ static void set_deep_lookup(lua_State* L) | |||
81 | STACK_CHECK( L, 0); | 81 | STACK_CHECK( L, 0); |
82 | } | 82 | } |
83 | 83 | ||
84 | // ################################################################################################ | 84 | // ################################################################################################# |
85 | 85 | ||
86 | /* | 86 | /* |
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 |
@@ -92,7 +92,7 @@ static void get_deep_lookup(lua_State* L) | |||
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 | DEEP_LOOKUP_KEY.pushValue(L); // a {} | 94 | DEEP_LOOKUP_KEY.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 |
@@ -101,7 +101,7 @@ static void get_deep_lookup(lua_State* L) | |||
101 | STACK_CHECK( L, 1); | 101 | STACK_CHECK( L, 1); |
102 | } | 102 | } |
103 | 103 | ||
104 | // ################################################################################################ | 104 | // ################################################################################################# |
105 | 105 | ||
106 | /* | 106 | /* |
107 | * Return the registered factory for 'index' (deep userdata proxy), | 107 | * Return the registered factory for 'index' (deep userdata proxy), |
@@ -124,7 +124,7 @@ static void get_deep_lookup(lua_State* L) | |||
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 | } |
@@ -139,7 +139,7 @@ static void get_deep_lookup(lua_State* L) | |||
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 | { |
@@ -148,7 +148,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
148 | STACK_CHECK(L, 0); | 148 | STACK_CHECK(L, 0); |
149 | } | 149 | } |
150 | 150 | ||
151 | // ################################################################################################ | 151 | // ################################################################################################# |
152 | 152 | ||
153 | /* | 153 | /* |
154 | * void= mt.__gc( proxy_ud ) | 154 | * void= mt.__gc( proxy_ud ) |
@@ -169,7 +169,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
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); // |
@@ -180,7 +180,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
180 | return 0; | 180 | return 0; |
181 | } | 181 | } |
182 | 182 | ||
183 | // ################################################################################################ | 183 | // ################################################################################################# |
184 | 184 | ||
185 | /* | 185 | /* |
186 | * Push a proxy userdata on the stack. | 186 | * Push a proxy userdata on the stack. |
@@ -194,25 +194,25 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) | |||
194 | char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, LookupMode mode_) | 194 | char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, LookupMode mode_) |
195 | { | 195 | { |
196 | // Check if a proxy already exists | 196 | // Check if a proxy already exists |
197 | push_registry_subtable_mode( L, DEEP_PROXY_CACHE_KEY, "v"); // DPC | 197 | push_registry_subtable_mode(L, DEEP_PROXY_CACHE_KEY, "v"); // DPC |
198 | lua_pushlightuserdata( L, prelude); // DPC deep | 198 | lua_pushlightuserdata(L, prelude); // DPC deep |
199 | lua_rawget( L, -2); // DPC proxy | 199 | lua_rawget(L, -2); // DPC proxy |
200 | if ( !lua_isnil( L, -1)) | 200 | if (!lua_isnil(L, -1)) |
201 | { | 201 | { |
202 | lua_remove( L, -2); // proxy | 202 | lua_remove(L, -2); // proxy |
203 | return nullptr; | 203 | return nullptr; |
204 | } | 204 | } |
205 | else | 205 | else |
206 | { | 206 | { |
207 | lua_pop( L, 1); // DPC | 207 | lua_pop(L, 1); // DPC |
208 | } | 208 | } |
209 | 209 | ||
210 | STACK_GROW( L, 7); | 210 | STACK_GROW(L, 7); |
211 | STACK_CHECK_START_REL(L, 0); | 211 | STACK_CHECK_START_REL(L, 0); |
212 | 212 | ||
213 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) | 213 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) |
214 | DeepPrelude** proxy = (DeepPrelude**) lua_newuserdatauv(L, sizeof(DeepPrelude*), nuv_); // DPC proxy | 214 | DeepPrelude** const proxy{ lua_newuserdatauv<DeepPrelude*>(L, nuv_) }; // DPC proxy |
215 | ASSERT_L( proxy); | 215 | ASSERT_L(proxy); |
216 | *proxy = prelude; | 216 | *proxy = prelude; |
217 | prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data | 217 | prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data |
218 | 218 | ||
@@ -221,7 +221,7 @@ char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, L | |||
221 | lua_pushlightuserdata( L, std::bit_cast<void*>(&factory)); // DPC proxy factory | 221 | lua_pushlightuserdata( L, std::bit_cast<void*>(&factory)); // DPC proxy factory |
222 | get_deep_lookup( L); // DPC proxy metatable? | 222 | get_deep_lookup( L); // DPC proxy metatable? |
223 | 223 | ||
224 | if( lua_isnil( L, -1)) // // No metatable yet. | 224 | if (lua_isnil( L, -1)) // // No metatable yet. |
225 | { | 225 | { |
226 | lua_pop(L, 1); // DPC proxy | 226 | lua_pop(L, 1); // DPC proxy |
227 | int const oldtop{ lua_gettop(L) }; | 227 | int const oldtop{ lua_gettop(L) }; |
@@ -245,7 +245,7 @@ char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, L | |||
245 | lua_newtable( L); // DPC proxy metatable | 245 | lua_newtable( L); // DPC proxy metatable |
246 | lua_pushnil( L); // DPC proxy metatable nil | 246 | lua_pushnil( L); // DPC proxy metatable nil |
247 | } | 247 | } |
248 | if( lua_isnil( L, -1)) | 248 | if (lua_isnil( L, -1)) |
249 | { | 249 | { |
250 | // Add our own '__gc' method | 250 | // Add our own '__gc' method |
251 | lua_pop( L, 1); // DPC proxy metatable | 251 | lua_pop( L, 1); // DPC proxy metatable |
@@ -269,22 +269,22 @@ char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, L | |||
269 | // L.registry._LOADED exists without having registered the 'package' library. | 269 | // L.registry._LOADED exists without having registered the 'package' library. |
270 | lua_getglobal( L, "require"); // DPC proxy metatable require() | 270 | lua_getglobal( L, "require"); // DPC proxy metatable require() |
271 | // check that the module is already loaded (or being loaded, we are happy either way) | 271 | // check that the module is already loaded (or being loaded, we are happy either way) |
272 | if( lua_isfunction( L, -1)) | 272 | if (lua_isfunction( L, -1)) |
273 | { | 273 | { |
274 | lua_pushstring( L, modname); // DPC proxy metatable require() "module" | 274 | lua_pushstring( L, modname); // DPC proxy metatable require() "module" |
275 | lua_getfield( L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED | 275 | lua_getfield( L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED |
276 | if( lua_istable( L, -1)) | 276 | if (lua_istable( L, -1)) |
277 | { | 277 | { |
278 | lua_pushvalue( L, -2); // DPC proxy metatable require() "module" _R._LOADED "module" | 278 | lua_pushvalue( L, -2); // DPC proxy metatable require() "module" _R._LOADED "module" |
279 | lua_rawget( L, -2); // DPC proxy metatable require() "module" _R._LOADED module | 279 | lua_rawget( L, -2); // DPC proxy metatable require() "module" _R._LOADED module |
280 | int const alreadyloaded = lua_toboolean( L, -1); | 280 | int const alreadyloaded = lua_toboolean( L, -1); |
281 | if( !alreadyloaded) // not loaded | 281 | if (!alreadyloaded) // not loaded |
282 | { | 282 | { |
283 | int require_result; | 283 | int require_result; |
284 | lua_pop( L, 2); // DPC proxy metatable require() "module" | 284 | lua_pop( L, 2); // DPC proxy metatable require() "module" |
285 | // require "modname" | 285 | // require "modname" |
286 | require_result = lua_pcall( L, 1, 0, 0); // DPC proxy metatable error? | 286 | require_result = lua_pcall( L, 1, 0, 0); // DPC proxy metatable error? |
287 | if( require_result != LUA_OK) | 287 | if (require_result != LUA_OK) |
288 | { | 288 | { |
289 | // failed, return the error message | 289 | // failed, return the error message |
290 | lua_pushfstring( L, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); | 290 | lua_pushfstring( L, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); |
@@ -326,7 +326,7 @@ char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, L | |||
326 | return nullptr; | 326 | return nullptr; |
327 | } | 327 | } |
328 | 328 | ||
329 | // ################################################################################################ | 329 | // ################################################################################################# |
330 | 330 | ||
331 | /* | 331 | /* |
332 | * Create a deep userdata | 332 | * Create a deep userdata |
@@ -354,7 +354,7 @@ int DeepFactory::pushDeepUserdata(Dest L, int nuv_) const | |||
354 | return luaL_error( L, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); | 354 | return luaL_error( L, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); |
355 | } | 355 | } |
356 | 356 | ||
357 | if( prelude->m_magic != DEEP_VERSION) | 357 | if (prelude->m_magic != DEEP_VERSION) |
358 | { | 358 | { |
359 | // just in case, don't leak the newly allocated deep userdata object | 359 | // just in case, don't leak the newly allocated deep userdata object |
360 | deleteDeepObjectInternal(L, prelude); | 360 | deleteDeepObjectInternal(L, prelude); |
@@ -364,7 +364,7 @@ int DeepFactory::pushDeepUserdata(Dest L, int nuv_) const | |||
364 | ASSERT_L(prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 | 364 | ASSERT_L(prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 |
365 | ASSERT_L(&prelude->m_factory == this); | 365 | ASSERT_L(&prelude->m_factory == this); |
366 | 366 | ||
367 | if( lua_gettop( L) - oldtop != 0) | 367 | if (lua_gettop( L) - oldtop != 0) |
368 | { | 368 | { |
369 | // just in case, don't leak the newly allocated deep userdata object | 369 | // just in case, don't leak the newly allocated deep userdata object |
370 | deleteDeepObjectInternal(L, prelude); | 370 | deleteDeepObjectInternal(L, prelude); |
@@ -380,7 +380,7 @@ int DeepFactory::pushDeepUserdata(Dest L, int nuv_) const | |||
380 | return 1; | 380 | return 1; |
381 | } | 381 | } |
382 | 382 | ||
383 | // ################################################################################################ | 383 | // ################################################################################################# |
384 | 384 | ||
385 | /* | 385 | /* |
386 | * Access deep userdata through a proxy. | 386 | * Access deep userdata through a proxy. |
@@ -402,7 +402,7 @@ DeepPrelude* DeepFactory::toDeep(lua_State* L, int index) const | |||
402 | return *proxy; | 402 | return *proxy; |
403 | } | 403 | } |
404 | 404 | ||
405 | // ################################################################################################ | 405 | // ################################################################################################# |
406 | 406 | ||
407 | /* | 407 | /* |
408 | * Copy deep userdata between two separate Lua states (from L to L2) | 408 | * Copy deep userdata between two separate Lua states (from L to L2) |