aboutsummaryrefslogtreecommitdiff
path: root/src/keeper.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/keeper.c')
-rw-r--r--src/keeper.c34
1 files changed, 18 insertions, 16 deletions
diff --git a/src/keeper.c b/src/keeper.c
index 0471cb7..ae3e2a8 100644
--- a/src/keeper.c
+++ b/src/keeper.c
@@ -166,10 +166,9 @@ static DECLARE_CONST_UNIQUE_KEY( FIFOS_KEY, 0xdce50bbc351cd465);
166static void push_table( lua_State* L, int idx_) 166static void push_table( lua_State* L, int idx_)
167{ 167{
168 STACK_GROW( L, 4); 168 STACK_GROW( L, 4);
169 STACK_CHECK( L); 169 STACK_CHECK( L, 0);
170 idx_ = lua_absindex( L, idx_); 170 idx_ = lua_absindex( L, idx_);
171 push_unique_key( L, FIFOS_KEY); // ud fifos_key 171 REGISTRY_GET( L, FIFOS_KEY); // ud fifos
172 lua_rawget( L, LUA_REGISTRYINDEX); // ud fifos
173 lua_pushvalue( L, idx_); // ud fifos ud 172 lua_pushvalue( L, idx_); // ud fifos ud
174 lua_rawget( L, -2); // ud fifos fifos[ud] 173 lua_rawget( L, -2); // ud fifos fifos[ud]
175 STACK_MID( L, 2); 174 STACK_MID( L, 2);
@@ -192,9 +191,8 @@ int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t
192 lua_State* const KL = K ? K->L : NULL; 191 lua_State* const KL = K ? K->L : NULL;
193 if( KL == NULL) return 0; 192 if( KL == NULL) return 0;
194 STACK_GROW( KL, 4); 193 STACK_GROW( KL, 4);
195 STACK_CHECK( KL); 194 STACK_CHECK( KL, 0);
196 push_unique_key( KL, FIFOS_KEY); // fifos_key 195 REGISTRY_GET( KL, FIFOS_KEY); // fifos
197 lua_rawget( KL, LUA_REGISTRYINDEX); // fifos
198 lua_pushlightuserdata( KL, ptr_); // fifos ud 196 lua_pushlightuserdata( KL, ptr_); // fifos ud
199 lua_rawget( KL, -2); // fifos storage 197 lua_rawget( KL, -2); // fifos storage
200 lua_remove( KL, -2); // storage 198 lua_remove( KL, -2); // storage
@@ -207,7 +205,7 @@ int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t
207 // move data from keeper to destination state KEEPER MAIN 205 // move data from keeper to destination state KEEPER MAIN
208 lua_pushnil( KL); // storage nil 206 lua_pushnil( KL); // storage nil
209 STACK_GROW( L, 5); 207 STACK_GROW( L, 5);
210 STACK_CHECK( L); 208 STACK_CHECK( L, 0);
211 lua_newtable( L); // out 209 lua_newtable( L); // out
212 while( lua_next( KL, -2)) // storage key fifo 210 while( lua_next( KL, -2)) // storage key fifo
213 { 211 {
@@ -240,12 +238,13 @@ int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t
240int keepercall_clear( lua_State* L) 238int keepercall_clear( lua_State* L)
241{ 239{
242 STACK_GROW( L, 3); 240 STACK_GROW( L, 3);
243 push_unique_key( L, FIFOS_KEY); // ud fifos_key 241 STACK_CHECK( L, 0);
244 lua_rawget( L, LUA_REGISTRYINDEX); // ud fifos 242 REGISTRY_GET( L, FIFOS_KEY); // ud fifos
245 lua_pushvalue( L, 1); // ud fifos ud 243 lua_pushvalue( L, 1); // ud fifos ud
246 lua_pushnil( L); // ud fifos ud nil 244 lua_pushnil( L); // ud fifos ud nil
247 lua_rawset( L, -3); // ud fifos 245 lua_rawset( L, -3); // ud fifos
248 lua_pop( L, 1); // ud 246 lua_pop( L, 1); // ud
247 STACK_END( L, 0);
249 return 0; 248 return 0;
250} 249}
251 250
@@ -634,11 +633,14 @@ void init_keepers( Universe* U, lua_State* L)
634 void* allocUD; 633 void* allocUD;
635 lua_Alloc allocF = lua_getallocf( L, &allocUD); 634 lua_Alloc allocF = lua_getallocf( L, &allocUD);
636 635
637 STACK_CHECK( L); // L K 636 STACK_CHECK( L, 0); // L K
638 lua_getfield( L, 1, "nb_keepers"); // nb_keepers 637 lua_getfield( L, 1, "nb_keepers"); // nb_keepers
639 nb_keepers = (int) lua_tointeger( L, -1); 638 nb_keepers = (int) lua_tointeger( L, -1);
640 lua_pop( L, 1); // 639 lua_pop( L, 1); //
641 assert( nb_keepers >= 1); 640 if( nb_keepers < 1)
641 {
642 (void) luaL_error( L, "Bad number of keepers (%d)", nb_keepers);
643 }
642 644
643 // Keepers contains an array of 1 s_Keeper, adjust for the actual number of keeper states 645 // Keepers contains an array of 1 s_Keeper, adjust for the actual number of keeper states
644 { 646 {
@@ -654,6 +656,7 @@ void init_keepers( Universe* U, lua_State* L)
654 } 656 }
655 for( i = 0; i < nb_keepers; ++ i) // keepersUD 657 for( i = 0; i < nb_keepers; ++ i) // keepersUD
656 { 658 {
659 // note that we will leak K if we raise an error later
657 lua_State* K = PROPAGATE_ALLOCF_ALLOC(); 660 lua_State* K = PROPAGATE_ALLOCF_ALLOC();
658 if( K == NULL) 661 if( K == NULL)
659 { 662 {
@@ -667,10 +670,12 @@ void init_keepers( Universe* U, lua_State* L)
667 // therefore, we need a recursive mutex. 670 // therefore, we need a recursive mutex.
668 MUTEX_RECURSIVE_INIT( &U->keepers->keeper_array[i].keeper_cs); 671 MUTEX_RECURSIVE_INIT( &U->keepers->keeper_array[i].keeper_cs);
669 672
673 STACK_CHECK( K, 0);
674
670 // copy the universe pointer in the keeper itself 675 // copy the universe pointer in the keeper itself
671 universe_store( K, U); 676 universe_store( K, U);
677 STACK_MID( K, 0);
672 678
673 STACK_CHECK( K);
674 // make sure 'package' is initialized in keeper states, so that we have require() 679 // make sure 'package' is initialized in keeper states, so that we have require()
675 // this because this is needed when transferring deep userdata object 680 // this because this is needed when transferring deep userdata object
676 luaL_requiref( K, "package", luaopen_package, 1); // package 681 luaL_requiref( K, "package", luaopen_package, 1); // package
@@ -705,10 +710,7 @@ void init_keepers( Universe* U, lua_State* L)
705 lua_setglobal( K, "decoda_name"); // 710 lua_setglobal( K, "decoda_name"); //
706 711
707 // create the fifos table in the keeper state 712 // create the fifos table in the keeper state
708 push_unique_key( K, FIFOS_KEY); // fifo_key 713 REGISTRY_SET( K, FIFOS_KEY, lua_newtable( K));
709 lua_newtable( K); // fifo_key {}
710 lua_rawset( K, LUA_REGISTRYINDEX); //
711
712 STACK_END( K, 0); 714 STACK_END( K, 0);
713 } 715 }
714 STACK_END( L, 0); 716 STACK_END( L, 0);