diff options
Diffstat (limited to 'src/deep.cpp')
-rw-r--r-- | src/deep.cpp | 38 |
1 files changed, 17 insertions, 21 deletions
diff --git a/src/deep.cpp b/src/deep.cpp index f091d4d..e62ca6d 100644 --- a/src/deep.cpp +++ b/src/deep.cpp | |||
@@ -108,12 +108,12 @@ static void get_deep_lookup( lua_State* L) | |||
108 | * Return the registered ID function for 'index' (deep userdata proxy), | 108 | * Return the registered ID function for 'index' (deep userdata proxy), |
109 | * or nullptr if 'index' is not a deep userdata proxy. | 109 | * or nullptr if 'index' is not a deep userdata proxy. |
110 | */ | 110 | */ |
111 | static inline luaG_IdFunction* get_idfunc( lua_State* L, int index, LookupMode mode_) | 111 | static inline luaG_IdFunction get_idfunc( lua_State* L, int index, LookupMode mode_) |
112 | { | 112 | { |
113 | // when looking inside a keeper, we are 100% sure the object is a deep userdata | 113 | // when looking inside a keeper, we are 100% sure the object is a deep userdata |
114 | if( mode_ == eLM_FromKeeper) | 114 | if( mode_ == eLM_FromKeeper) |
115 | { | 115 | { |
116 | DeepPrelude** const proxy{ lua_touserdata<DeepPrelude*>(L, index) }; | 116 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L, index) }; |
117 | // we can (and must) cast and fetch the internally stored idfunc | 117 | // we can (and must) cast and fetch the internally stored idfunc |
118 | return (*proxy)->idfunc; | 118 | return (*proxy)->idfunc; |
119 | } | 119 | } |
@@ -133,7 +133,7 @@ static inline luaG_IdFunction* get_idfunc( lua_State* L, int index, LookupMode m | |||
133 | // replace metatable with the idfunc pointer, if it is actually a deep userdata | 133 | // replace metatable with the idfunc pointer, if it is actually a deep userdata |
134 | get_deep_lookup( L); // deep ... idfunc|nil | 134 | get_deep_lookup( L); // deep ... idfunc|nil |
135 | 135 | ||
136 | luaG_IdFunction* const ret{ lua_touserdata<luaG_IdFunction>(L, -1) }; // nullptr if not a userdata | 136 | luaG_IdFunction const ret{ *lua_tolightuserdata<luaG_IdFunction>(L, -1) }; // nullptr if not a userdata |
137 | lua_pop( L, 1); | 137 | lua_pop( L, 1); |
138 | STACK_CHECK( L, 0); | 138 | STACK_CHECK( L, 0); |
139 | return ret; | 139 | return ret; |
@@ -161,7 +161,7 @@ void free_deep_prelude( lua_State* L, DeepPrelude* prelude_) | |||
161 | */ | 161 | */ |
162 | static int deep_userdata_gc( lua_State* L) | 162 | static int deep_userdata_gc( lua_State* L) |
163 | { | 163 | { |
164 | DeepPrelude** const proxy{ lua_touserdata<DeepPrelude*>(L, 1) }; | 164 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L, 1) }; |
165 | DeepPrelude* p = *proxy; | 165 | DeepPrelude* p = *proxy; |
166 | 166 | ||
167 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded | 167 | // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded |
@@ -203,8 +203,6 @@ static int deep_userdata_gc( lua_State* L) | |||
203 | */ | 203 | */ |
204 | char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, LookupMode mode_) | 204 | char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, LookupMode mode_) |
205 | { | 205 | { |
206 | DeepPrelude** proxy; | ||
207 | |||
208 | // Check if a proxy already exists | 206 | // Check if a proxy already exists |
209 | push_registry_subtable_mode( L, DEEP_PROXY_CACHE_KEY, "v"); // DPC | 207 | push_registry_subtable_mode( L, DEEP_PROXY_CACHE_KEY, "v"); // DPC |
210 | lua_pushlightuserdata( L, prelude); // DPC deep | 208 | lua_pushlightuserdata( L, prelude); // DPC deep |
@@ -223,7 +221,7 @@ char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, Lookup | |||
223 | STACK_CHECK_START_REL(L, 0); | 221 | STACK_CHECK_START_REL(L, 0); |
224 | 222 | ||
225 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) | 223 | // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) |
226 | proxy = (DeepPrelude**) lua_newuserdatauv( L, sizeof(DeepPrelude*), nuv_); // DPC proxy | 224 | DeepPrelude** proxy = (DeepPrelude**) lua_newuserdatauv(L, sizeof(DeepPrelude*), nuv_); // DPC proxy |
227 | ASSERT_L( proxy); | 225 | ASSERT_L( proxy); |
228 | *proxy = prelude; | 226 | *proxy = prelude; |
229 | prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data | 227 | prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data |
@@ -333,9 +331,9 @@ char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, Lookup | |||
333 | } | 331 | } |
334 | } | 332 | } |
335 | } | 333 | } |
336 | STACK_CHECK( L, 2); // DPC proxy metatable | 334 | STACK_CHECK(L, 2); // DPC proxy metatable |
337 | ASSERT_L( lua_isuserdata( L, -2)); | 335 | ASSERT_L(lua_type(L, -2) == LUA_TUSERDATA); |
338 | ASSERT_L( lua_istable( L, -1)); | 336 | ASSERT_L(lua_istable( L, -1)); |
339 | lua_setmetatable( L, -2); // DPC proxy | 337 | lua_setmetatable( L, -2); // DPC proxy |
340 | 338 | ||
341 | // If we're here, we obviously had to create a new proxy, so cache it. | 339 | // If we're here, we obviously had to create a new proxy, so cache it. |
@@ -343,8 +341,8 @@ char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, Lookup | |||
343 | lua_pushvalue( L, -2); // DPC proxy deep proxy | 341 | lua_pushvalue( L, -2); // DPC proxy deep proxy |
344 | lua_rawset( L, -4); // DPC proxy | 342 | lua_rawset( L, -4); // DPC proxy |
345 | lua_remove( L, -2); // proxy | 343 | lua_remove( L, -2); // proxy |
346 | ASSERT_L( lua_isuserdata( L, -1)); | 344 | ASSERT_L(lua_type(L, -1) == LUA_TUSERDATA); |
347 | STACK_CHECK( L, 0); | 345 | STACK_CHECK(L, 0); |
348 | return nullptr; | 346 | return nullptr; |
349 | } | 347 | } |
350 | 348 | ||
@@ -370,7 +368,7 @@ char const* push_deep_proxy(lua_State* L, DeepPrelude* prelude, int nuv_, Lookup | |||
370 | * | 368 | * |
371 | * Returns: 'proxy' userdata for accessing the deep data via 'luaG_todeep()' | 369 | * Returns: 'proxy' userdata for accessing the deep data via 'luaG_todeep()' |
372 | */ | 370 | */ |
373 | int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction* idfunc, int nuv_) | 371 | int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc, int nuv_) |
374 | { | 372 | { |
375 | STACK_GROW( L, 1); | 373 | STACK_GROW( L, 1); |
376 | STACK_CHECK_START_REL(L, 0); | 374 | STACK_CHECK_START_REL(L, 0); |
@@ -416,7 +414,7 @@ int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction* idfunc, int nuv_) | |||
416 | * Reference count is not changed, and access to the deep userdata is not | 414 | * Reference count is not changed, and access to the deep userdata is not |
417 | * serialized. It is the module's responsibility to prevent conflicting usage. | 415 | * serialized. It is the module's responsibility to prevent conflicting usage. |
418 | */ | 416 | */ |
419 | void* luaG_todeep( lua_State* L, luaG_IdFunction* idfunc, int index) | 417 | DeepPrelude* luaG_todeep(lua_State* L, luaG_IdFunction idfunc, int index) |
420 | { | 418 | { |
421 | STACK_CHECK_START_REL(L, 0); | 419 | STACK_CHECK_START_REL(L, 0); |
422 | // ensure it is actually a deep userdata | 420 | // ensure it is actually a deep userdata |
@@ -426,7 +424,7 @@ void* luaG_todeep( lua_State* L, luaG_IdFunction* idfunc, int index) | |||
426 | } | 424 | } |
427 | STACK_CHECK(L, 0); | 425 | STACK_CHECK(L, 0); |
428 | 426 | ||
429 | DeepPrelude** const proxy{ lua_touserdata<DeepPrelude*>(L, index) }; | 427 | DeepPrelude** const proxy{ lua_tofulluserdata<DeepPrelude*>(L, index) }; |
430 | 428 | ||
431 | return *proxy; | 429 | return *proxy; |
432 | } | 430 | } |
@@ -441,10 +439,7 @@ void* luaG_todeep( lua_State* L, luaG_IdFunction* idfunc, int index) | |||
441 | */ | 439 | */ |
442 | bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_) | 440 | bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, LookupMode mode_, char const* upName_) |
443 | { | 441 | { |
444 | char const* errmsg; | 442 | luaG_IdFunction const idfunc { get_idfunc(L, i, mode_) }; |
445 | luaG_IdFunction* idfunc = get_idfunc( L, i, mode_); | ||
446 | int nuv = 0; | ||
447 | |||
448 | if (idfunc == nullptr) | 443 | if (idfunc == nullptr) |
449 | { | 444 | { |
450 | return false; // not a deep userdata | 445 | return false; // not a deep userdata |
@@ -454,7 +449,8 @@ bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, L | |||
454 | STACK_CHECK_START_REL(L2, 0); | 449 | STACK_CHECK_START_REL(L2, 0); |
455 | 450 | ||
456 | // extract all uservalues of the source | 451 | // extract all uservalues of the source |
457 | while( lua_getiuservalue( L, i, nuv + 1) != LUA_TNONE) // ... u [uv]* nil | 452 | int nuv = 0; |
453 | while (lua_getiuservalue(L, i, nuv + 1) != LUA_TNONE) // ... u [uv]* nil | ||
458 | { | 454 | { |
459 | ++ nuv; | 455 | ++ nuv; |
460 | } | 456 | } |
@@ -462,7 +458,7 @@ bool copydeep(Universe* U, lua_State* L2, int L2_cache_i, lua_State* L, int i, L | |||
462 | lua_pop( L, 1); // ... u [uv]* | 458 | lua_pop( L, 1); // ... u [uv]* |
463 | STACK_CHECK( L, nuv); | 459 | STACK_CHECK( L, nuv); |
464 | 460 | ||
465 | errmsg = push_deep_proxy(L2, *lua_touserdata<DeepPrelude*>(L, i), nuv, mode_); // u | 461 | char const* errmsg{ push_deep_proxy(L2, *lua_tofulluserdata<DeepPrelude*>(L, i), nuv, mode_) }; // u |
466 | 462 | ||
467 | // transfer all uservalues of the source in the destination | 463 | // transfer all uservalues of the source in the destination |
468 | { | 464 | { |