diff options
Diffstat (limited to 'src/deep.c')
-rw-r--r-- | src/deep.c | 39 |
1 files changed, 18 insertions, 21 deletions
@@ -33,6 +33,7 @@ THE SOFTWARE. | |||
33 | */ | 33 | */ |
34 | 34 | ||
35 | #include <stdio.h> | 35 | #include <stdio.h> |
36 | #include <assert.h> | ||
36 | #include <string.h> | 37 | #include <string.h> |
37 | #include <ctype.h> | 38 | #include <ctype.h> |
38 | #include <stdlib.h> | 39 | #include <stdlib.h> |
@@ -63,20 +64,18 @@ THE SOFTWARE. | |||
63 | static void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_) | 64 | static void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_) |
64 | { | 65 | { |
65 | STACK_GROW( L, 3); | 66 | STACK_GROW( L, 3); |
66 | STACK_CHECK( L); | 67 | STACK_CHECK( L, 0); |
67 | 68 | ||
68 | push_unique_key( L, key_); // key | 69 | REGISTRY_GET( L, key_); // {}|nil |
69 | lua_rawget( L, LUA_REGISTRYINDEX); // {}|nil | 70 | STACK_MID( L, 1); |
70 | 71 | ||
71 | if( lua_isnil( L, -1)) | 72 | if( lua_isnil( L, -1)) |
72 | { | 73 | { |
73 | lua_pop( L, 1); // | 74 | lua_pop( L, 1); // |
74 | lua_newtable( L); // {} | 75 | lua_newtable( L); // {} |
75 | push_unique_key( L, key_); // {} key | ||
76 | lua_pushvalue( L, -2); // {} key {} | ||
77 | |||
78 | // _R[key_] = {} | 76 | // _R[key_] = {} |
79 | lua_rawset( L, LUA_REGISTRYINDEX); // {} | 77 | REGISTRY_SET( L, key_, lua_pushvalue( L, -2)); // {} |
78 | STACK_MID( L, 1); | ||
80 | 79 | ||
81 | // Set its metatable if requested | 80 | // Set its metatable if requested |
82 | if( mode_) | 81 | if( mode_) |
@@ -128,16 +127,16 @@ static DECLARE_CONST_UNIQUE_KEY( DEEP_PROXY_CACHE_KEY, 0x05773d6fc26be106); | |||
128 | static void set_deep_lookup( lua_State* L) | 127 | static void set_deep_lookup( lua_State* L) |
129 | { | 128 | { |
130 | STACK_GROW( L, 3); | 129 | STACK_GROW( L, 3); |
131 | STACK_CHECK( L); // a b | 130 | STACK_CHECK( L, 2); // a b |
132 | push_registry_subtable( L, DEEP_LOOKUP_KEY); // a b {} | 131 | push_registry_subtable( L, DEEP_LOOKUP_KEY); // a b {} |
133 | STACK_MID( L, 1); | 132 | STACK_MID( L, 3); |
134 | lua_insert( L, -3); // {} a b | 133 | lua_insert( L, -3); // {} a b |
135 | lua_pushvalue( L, -1); // {} a b b | 134 | lua_pushvalue( L, -1); // {} a b b |
136 | lua_pushvalue( L,-3); // {} a b b a | 135 | lua_pushvalue( L,-3); // {} a b b a |
137 | lua_rawset( L, -5); // {} a b | 136 | lua_rawset( L, -5); // {} a b |
138 | lua_rawset( L, -3); // {} | 137 | lua_rawset( L, -3); // {} |
139 | lua_pop( L, 1); // | 138 | lua_pop( L, 1); // |
140 | STACK_END( L, -2); | 139 | STACK_END( L, 0); |
141 | } | 140 | } |
142 | 141 | ||
143 | /* | 142 | /* |
@@ -147,17 +146,15 @@ static void set_deep_lookup( lua_State* L) | |||
147 | static void get_deep_lookup( lua_State* L) | 146 | static void get_deep_lookup( lua_State* L) |
148 | { | 147 | { |
149 | STACK_GROW( L, 1); | 148 | STACK_GROW( L, 1); |
150 | STACK_CHECK( L); // a | 149 | STACK_CHECK( L, 1); // a |
151 | push_unique_key( L, DEEP_LOOKUP_KEY); // a DLK | 150 | REGISTRY_GET( L, DEEP_LOOKUP_KEY); // a {} |
152 | lua_rawget( L, LUA_REGISTRYINDEX); // a {} | ||
153 | |||
154 | if( !lua_isnil( L, -1)) | 151 | if( !lua_isnil( L, -1)) |
155 | { | 152 | { |
156 | lua_insert( L, -2); // {} a | 153 | lua_insert( L, -2); // {} a |
157 | lua_rawget( L, -2); // {} b | 154 | lua_rawget( L, -2); // {} b |
158 | } | 155 | } |
159 | lua_remove( L, -2); // a|b | 156 | lua_remove( L, -2); // a|b |
160 | STACK_END( L, 0); | 157 | STACK_END( L, 1); |
161 | } | 158 | } |
162 | 159 | ||
163 | /* | 160 | /* |
@@ -180,7 +177,7 @@ static inline luaG_IdFunction get_idfunc( lua_State* L, int index, LookupMode mo | |||
180 | // of course, we could just trust the caller, but we won't | 177 | // of course, we could just trust the caller, but we won't |
181 | luaG_IdFunction ret; | 178 | luaG_IdFunction ret; |
182 | STACK_GROW( L, 1); | 179 | STACK_GROW( L, 1); |
183 | STACK_CHECK( L); | 180 | STACK_CHECK( L, 0); |
184 | 181 | ||
185 | if( !lua_getmetatable( L, index)) // deep ... metatable? | 182 | if( !lua_getmetatable( L, index)) // deep ... metatable? |
186 | { | 183 | { |
@@ -284,7 +281,7 @@ char const* push_deep_proxy( Universe* U, lua_State* L, DeepPrelude* prelude, Lo | |||
284 | if( U) MUTEX_UNLOCK( &U->deep_lock); | 281 | if( U) MUTEX_UNLOCK( &U->deep_lock); |
285 | 282 | ||
286 | STACK_GROW( L, 7); | 283 | STACK_GROW( L, 7); |
287 | STACK_CHECK( L); | 284 | STACK_CHECK( L, 0); |
288 | 285 | ||
289 | proxy = lua_newuserdata( L, sizeof(DeepPrelude*)); // DPC proxy | 286 | proxy = lua_newuserdata( L, sizeof(DeepPrelude*)); // DPC proxy |
290 | ASSERT_L( proxy); | 287 | ASSERT_L( proxy); |
@@ -318,7 +315,7 @@ char const* push_deep_proxy( Universe* U, lua_State* L, DeepPrelude* prelude, Lo | |||
318 | lua_newtable( L); // DPC proxy metatable | 315 | lua_newtable( L); // DPC proxy metatable |
319 | lua_pushnil( L); // DPC proxy metatable nil | 316 | lua_pushnil( L); // DPC proxy metatable nil |
320 | } | 317 | } |
321 | if (lua_isnil(L, -1)) | 318 | if( lua_isnil( L, -1)) |
322 | { | 319 | { |
323 | // Add our own '__gc' method | 320 | // Add our own '__gc' method |
324 | lua_pop( L, 1); // DPC proxy metatable | 321 | lua_pop( L, 1); // DPC proxy metatable |
@@ -438,9 +435,9 @@ int luaG_newdeepuserdata( lua_State* L, luaG_IdFunction idfunc) | |||
438 | char const* errmsg; | 435 | char const* errmsg; |
439 | 436 | ||
440 | STACK_GROW( L, 1); | 437 | STACK_GROW( L, 1); |
441 | STACK_CHECK( L); | 438 | STACK_CHECK( L, 0); |
442 | { | 439 | { |
443 | int oldtop = lua_gettop( L); | 440 | int const oldtop = lua_gettop( L); |
444 | DeepPrelude* prelude = idfunc( L, eDO_new); | 441 | DeepPrelude* prelude = idfunc( L, eDO_new); |
445 | if( prelude == NULL) | 442 | if( prelude == NULL) |
446 | { | 443 | { |
@@ -484,7 +481,7 @@ void* luaG_todeep( lua_State* L, luaG_IdFunction idfunc, int index) | |||
484 | { | 481 | { |
485 | DeepPrelude** proxy; | 482 | DeepPrelude** proxy; |
486 | 483 | ||
487 | STACK_CHECK( L); | 484 | STACK_CHECK( L, 0); |
488 | // ensure it is actually a deep userdata | 485 | // ensure it is actually a deep userdata |
489 | if( get_idfunc( L, index, eLM_LaneBody) != idfunc) | 486 | if( get_idfunc( L, index, eLM_LaneBody) != idfunc) |
490 | { | 487 | { |