aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-03-21 15:41:54 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2024-03-21 15:41:54 +0100
commitf0170ce8f1a90337637d387b87280f121d0578fe (patch)
tree32065aa3470b54e6058e5f47e4931171b7ec28f2 /src
parent0b13436b835ea96ecdf930a380e9e5c8add8cb45 (diff)
downloadlanes-f0170ce8f1a90337637d387b87280f121d0578fe.tar.gz
lanes-f0170ce8f1a90337637d387b87280f121d0578fe.tar.bz2
lanes-f0170ce8f1a90337637d387b87280f121d0578fe.zip
C++ migration: REGISTRY_SET and REGISTRY_GET are gone, welcome templates and lambdas
Diffstat (limited to 'src')
-rw-r--r--src/deep.cpp2
-rw-r--r--src/keeper.cpp9
-rw-r--r--src/lanes.cpp35
-rw-r--r--src/lanes_private.h2
-rw-r--r--src/macros_and_utils.h14
-rw-r--r--src/state.cpp10
-rw-r--r--src/tools.cpp58
-rw-r--r--src/uniquekey.h14
-rw-r--r--src/universe.cpp6
9 files changed, 75 insertions, 75 deletions
diff --git a/src/deep.cpp b/src/deep.cpp
index c9e3655..1aab6ed 100644
--- a/src/deep.cpp
+++ b/src/deep.cpp
@@ -94,7 +94,7 @@ static void get_deep_lookup( lua_State* L)
94{ 94{
95 STACK_GROW( L, 1); 95 STACK_GROW( L, 1);
96 STACK_CHECK( L, 1); // a 96 STACK_CHECK( L, 1); // a
97 REGISTRY_GET( L, DEEP_LOOKUP_KEY); // a {} 97 DEEP_LOOKUP_KEY.query_registry(L); // a {}
98 if( !lua_isnil( L, -1)) 98 if( !lua_isnil( L, -1))
99 { 99 {
100 lua_insert( L, -2); // {} a 100 lua_insert( L, -2); // {} a
diff --git a/src/keeper.cpp b/src/keeper.cpp
index 6fb22a9..c886718 100644
--- a/src/keeper.cpp
+++ b/src/keeper.cpp
@@ -172,7 +172,7 @@ static void push_table( lua_State* L, int idx_)
172 STACK_GROW( L, 4); 172 STACK_GROW( L, 4);
173 STACK_CHECK( L, 0); 173 STACK_CHECK( L, 0);
174 idx_ = lua_absindex( L, idx_); 174 idx_ = lua_absindex( L, idx_);
175 REGISTRY_GET( L, FIFOS_KEY); // ud fifos 175 FIFOS_KEY.query_registry(L); // ud fifos
176 lua_pushvalue( L, idx_); // ud fifos ud 176 lua_pushvalue( L, idx_); // ud fifos ud
177 lua_rawget( L, -2); // ud fifos fifos[ud] 177 lua_rawget( L, -2); // ud fifos fifos[ud]
178 STACK_MID( L, 2); 178 STACK_MID( L, 2);
@@ -196,7 +196,7 @@ int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t
196 if( KL == nullptr) return 0; 196 if( KL == nullptr) return 0;
197 STACK_GROW( KL, 4); 197 STACK_GROW( KL, 4);
198 STACK_CHECK( KL, 0); 198 STACK_CHECK( KL, 0);
199 REGISTRY_GET( KL, FIFOS_KEY); // fifos 199 FIFOS_KEY.query_registry(KL); // fifos
200 lua_pushlightuserdata( KL, ptr_); // fifos ud 200 lua_pushlightuserdata( KL, ptr_); // fifos ud
201 lua_rawget( KL, -2); // fifos storage 201 lua_rawget( KL, -2); // fifos storage
202 lua_remove( KL, -2); // storage 202 lua_remove( KL, -2); // storage
@@ -243,7 +243,7 @@ int keepercall_clear( lua_State* L)
243{ 243{
244 STACK_GROW( L, 3); 244 STACK_GROW( L, 3);
245 STACK_CHECK( L, 0); 245 STACK_CHECK( L, 0);
246 REGISTRY_GET( L, FIFOS_KEY); // ud fifos 246 FIFOS_KEY.query_registry(L); // ud fifos
247 lua_pushvalue( L, 1); // ud fifos ud 247 lua_pushvalue( L, 1); // ud fifos ud
248 lua_pushnil( L); // ud fifos ud nil 248 lua_pushnil( L); // ud fifos ud nil
249 lua_rawset( L, -3); // ud fifos 249 lua_rawset( L, -3); // ud fifos
@@ -712,9 +712,8 @@ void init_keepers( Universe* U, lua_State* L)
712 // to see VM name in Decoda debugger 712 // to see VM name in Decoda debugger
713 lua_pushfstring( K, "Keeper #%d", i + 1); // "Keeper #n" 713 lua_pushfstring( K, "Keeper #%d", i + 1); // "Keeper #n"
714 lua_setglobal( K, "decoda_name"); // 714 lua_setglobal( K, "decoda_name"); //
715
716 // create the fifos table in the keeper state 715 // create the fifos table in the keeper state
717 REGISTRY_SET( K, FIFOS_KEY, lua_newtable( K)); 716 FIFOS_KEY.set_registry(K, [](lua_State* L) { lua_newtable(L); } );
718 STACK_END( K, 0); 717 STACK_END( K, 0);
719 } 718 }
720 STACK_END( L, 0); 719 STACK_END( L, 0);
diff --git a/src/lanes.cpp b/src/lanes.cpp
index acfa0dc..1589240 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -157,7 +157,7 @@ static bool push_registry_table( lua_State* L, UniqueKey key, bool create)
157 STACK_GROW( L, 3); 157 STACK_GROW( L, 3);
158 STACK_CHECK( L, 0); 158 STACK_CHECK( L, 0);
159 159
160 REGISTRY_GET( L, key); // ? 160 key.query_registry(L); // ?
161 if( lua_isnil( L, -1)) // nil? 161 if( lua_isnil( L, -1)) // nil?
162 { 162 {
163 lua_pop( L, 1); // 163 lua_pop( L, 1); //
@@ -168,7 +168,7 @@ static bool push_registry_table( lua_State* L, UniqueKey key, bool create)
168 } 168 }
169 169
170 lua_newtable( L); // t 170 lua_newtable( L); // t
171 REGISTRY_SET( L, key, lua_pushvalue( L, -2)); 171 key.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); });
172 } 172 }
173 STACK_END( L, 1); 173 STACK_END( L, 1);
174 return true; // table pushed 174 return true; // table pushed
@@ -669,7 +669,7 @@ LUAG_FUNC( set_error_reporting)
669 return luaL_error( L, "unsupported error reporting model"); 669 return luaL_error( L, "unsupported error reporting model");
670 } 670 }
671done: 671done:
672 REGISTRY_SET( L, EXTENDED_STACKTRACE_REGKEY, lua_pushboolean( L, equal)); 672 EXTENDED_STACKTRACE_REGKEY.set_registry(L, [equal](lua_State* L) { lua_pushboolean(L, equal); });
673 return 0; 673 return 0;
674} 674}
675 675
@@ -679,7 +679,7 @@ static int lane_error( lua_State* L)
679 int n; 679 int n;
680 680
681 // error message (any type) 681 // error message (any type)
682 STACK_CHECK_ABS( L, 1); // some_error 682 STACK_CHECK_ABS( L, 1); // some_error
683 683
684 // Don't do stack survey for cancelled lanes. 684 // Don't do stack survey for cancelled lanes.
685 // 685 //
@@ -689,11 +689,11 @@ static int lane_error( lua_State* L)
689 } 689 }
690 690
691 STACK_GROW( L, 3); 691 STACK_GROW( L, 3);
692 REGISTRY_GET( L, EXTENDED_STACKTRACE_REGKEY); // some_error basic|extended 692 EXTENDED_STACKTRACE_REGKEY.query_registry(L); // some_error basic|extended
693 bool const extended{ lua_toboolean(L, -1) ? true : false}; 693 bool const extended{ lua_toboolean(L, -1) ? true : false};
694 lua_pop( L, 1); // some_error 694 lua_pop( L, 1); // some_error
695 695
696 // Place stack trace at 'registry[lane_error]' for the 'lua_pcall()' 696 // Place stack trace at 'registry[STACKTRACE_REGKEY]' for the 'lua_pcall()'
697 // caller to fetch. This bypasses the Lua 5.1 limitation of only one 697 // caller to fetch. This bypasses the Lua 5.1 limitation of only one
698 // return value from error handler to 'lua_pcall()' caller. 698 // return value from error handler to 'lua_pcall()' caller.
699 699
@@ -703,7 +703,7 @@ static int lane_error( lua_State* L)
703 // 703 //
704 // table of { "sourcefile.lua:<line>", ... } 704 // table of { "sourcefile.lua:<line>", ... }
705 // 705 //
706 lua_newtable( L); // some_error {} 706 lua_newtable( L); // some_error {}
707 707
708 // Best to start from level 1, but in some cases it might be a C function 708 // Best to start from level 1, but in some cases it might be a C function
709 // and we don't get '.currentline' for that. It's okay - just keep level 709 // and we don't get '.currentline' for that. It's okay - just keep level
@@ -739,10 +739,11 @@ static int lane_error( lua_State* L)
739 { 739 {
740 lua_pushfstring( L, "%s:?", ar.short_src); // some_error {} "blah" 740 lua_pushfstring( L, "%s:?", ar.short_src); // some_error {} "blah"
741 } 741 }
742 lua_rawseti( L, -2, (lua_Integer) n); // some_error {} 742 lua_rawseti( L, -2, (lua_Integer) n); // some_error {}
743 } 743 }
744 744
745 REGISTRY_SET( L, STACKTRACE_REGKEY, lua_insert( L, -2)); // some_error 745 // store the stack trace table in the registry
746 STACKTRACE_REGKEY.set_registry(L, [](lua_State* L) { lua_insert(L, -2); }); // some_error
746 747
747 STACK_END( L, 1); 748 STACK_END( L, 1);
748 return 1; // the untouched error value 749 return 1; // the untouched error value
@@ -764,7 +765,7 @@ static void push_stack_trace( lua_State* L, int rc_, int stk_base_)
764 // fetch the call stack table from the registry where the handler stored it 765 // fetch the call stack table from the registry where the handler stored it
765 STACK_GROW( L, 1); 766 STACK_GROW( L, 1);
766 // yields nil if no stack was generated (in case of cancellation for example) 767 // yields nil if no stack was generated (in case of cancellation for example)
767 REGISTRY_GET( L, STACKTRACE_REGKEY); // err trace|nil 768 STACKTRACE_REGKEY.query_registry(L); // err trace|nil
768 STACK_END( L, 1); 769 STACK_END( L, 1);
769 770
770 // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed 771 // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed
@@ -794,7 +795,7 @@ LUAG_FUNC( set_debug_threadname)
794 lua_settop( L, 1); 795 lua_settop( L, 1);
795 STACK_CHECK_ABS( L, 1); 796 STACK_CHECK_ABS( L, 1);
796 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... 797 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global...
797 REGISTRY_SET( L, hidden_regkey, lua_pushvalue( L, -2)); 798 hidden_regkey.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); });
798 STACK_MID( L, 1); 799 STACK_MID( L, 1);
799 s->debug_name = lua_tostring( L, -1); 800 s->debug_name = lua_tostring( L, -1);
800 // keep a direct pointer on the string 801 // keep a direct pointer on the string
@@ -1276,7 +1277,7 @@ LUAG_FUNC( lane_new)
1276 lua_setiuservalue( L, -2, 1); // func libs priority globals package required gc_cb lane 1277 lua_setiuservalue( L, -2, 1); // func libs priority globals package required gc_cb lane
1277 1278
1278 // Store 's' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). 1279 // Store 's' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive).
1279 REGISTRY_SET( L2, CANCEL_TEST_KEY, lua_pushlightuserdata( L2, s)); // func [... args ...] 1280 CANCEL_TEST_KEY.set_registry(L2, [s](lua_State* L) { lua_pushlightuserdata(L, s); }); // func [... args ...]
1280 1281
1281 STACK_END( L, 1); 1282 STACK_END( L, 1);
1282 STACK_END( L2, 1 + nargs); 1283 STACK_END( L2, 1 + nargs);
@@ -1993,7 +1994,7 @@ LUAG_FUNC( configure)
1993 1994
1994 STACK_MID( L, 2); // reference stack contains only the function argument 'settings' 1995 STACK_MID( L, 2); // reference stack contains only the function argument 'settings'
1995 // we'll need this every time we transfer some C function from/to this state 1996 // we'll need this every time we transfer some C function from/to this state
1996 REGISTRY_SET( L, LOOKUP_REGKEY, lua_newtable( L)); 1997 LOOKUP_REGKEY.set_registry(L, [](lua_State* L) { lua_newtable(L); }); // settings M
1997 STACK_MID( L, 2); 1998 STACK_MID( L, 2);
1998 1999
1999 // register all native functions found in that module in the transferable functions database 2000 // register all native functions found in that module in the transferable functions database
@@ -2015,8 +2016,8 @@ LUAG_FUNC( configure)
2015 } 2016 }
2016 lua_pop( L, 1); // settings 2017 lua_pop( L, 1); // settings
2017 2018
2018 // set _R[CONFIG_REGKEY] = settings 2019 // set _R[CONFIG_REGKEY] = settings
2019 REGISTRY_SET( L, CONFIG_REGKEY, lua_pushvalue( L, -2)); // -2 because CONFIG_REGKEY is pushed before the value itself 2020 CONFIG_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); });
2020 STACK_END( L, 1); 2021 STACK_END( L, 1);
2021 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); 2022 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L));
2022 DEBUGSPEW_CODE( -- U->debugspew_indent_depth); 2023 DEBUGSPEW_CODE( -- U->debugspew_indent_depth);
@@ -2103,7 +2104,7 @@ LANES_API int luaopen_lanes_core( lua_State* L)
2103 lua_pushvalue( L, 1); // M "lanes.core" 2104 lua_pushvalue( L, 1); // M "lanes.core"
2104 lua_pushvalue( L, -2); // M "lanes.core" M 2105 lua_pushvalue( L, -2); // M "lanes.core" M
2105 lua_pushcclosure( L, LG_configure, 2); // M LG_configure() 2106 lua_pushcclosure( L, LG_configure, 2); // M LG_configure()
2106 REGISTRY_GET( L, CONFIG_REGKEY); // M LG_configure() settings 2107 CONFIG_REGKEY.query_registry(L); // M LG_configure() settings
2107 if( !lua_isnil( L, -1)) // this is not the first require "lanes.core": call configure() immediately 2108 if( !lua_isnil( L, -1)) // this is not the first require "lanes.core": call configure() immediately
2108 { 2109 {
2109 lua_pushvalue( L, -1); // M LG_configure() settings settings 2110 lua_pushvalue( L, -1); // M LG_configure() settings settings
diff --git a/src/lanes_private.h b/src/lanes_private.h
index 75607b9..2fbbae9 100644
--- a/src/lanes_private.h
+++ b/src/lanes_private.h
@@ -83,7 +83,7 @@ static inline Lane* get_lane_from_registry( lua_State* L)
83 Lane* s; 83 Lane* s;
84 STACK_GROW( L, 1); 84 STACK_GROW( L, 1);
85 STACK_CHECK( L, 0); 85 STACK_CHECK( L, 0);
86 REGISTRY_GET( L, CANCEL_TEST_KEY); 86 CANCEL_TEST_KEY.query_registry(L);
87 s = (Lane*) lua_touserdata( L, -1); // lightuserdata (true 's_lane' pointer) / nil 87 s = (Lane*) lua_touserdata( L, -1); // lightuserdata (true 's_lane' pointer) / nil
88 lua_pop( L, 1); 88 lua_pop( L, 1);
89 STACK_END( L, 0); 89 STACK_END( L, 0);
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index ae93e97..67213bc 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -80,18 +80,4 @@ extern char const* debugspew_indent;
80 80
81#define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0) 81#define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0)
82 82
83// non-string keyed registry access
84#define REGISTRY_SET( L, key_, value_) \
85{ \
86 key_.push(L); \
87 value_; \
88 lua_rawset( L, LUA_REGISTRYINDEX); \
89}
90
91#define REGISTRY_GET( L, key_) \
92{ \
93 key_.push(L); \
94 lua_rawget( L, LUA_REGISTRYINDEX); \
95}
96
97#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L) 83#define LUAG_FUNC( func_name) int LG_##func_name( lua_State* L)
diff --git a/src/state.cpp b/src/state.cpp
index 688ac2a..f53d180 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -203,14 +203,14 @@ static void copy_one_time_settings( Universe* U, lua_State* L, lua_State* L2)
203 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); 203 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END));
204 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 204 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth);
205 205
206 REGISTRY_GET( L, CONFIG_REGKEY); // config 206 CONFIG_REGKEY.query_registry(L); // config
207 // copy settings from from source to destination registry 207 // copy settings from from source to destination registry
208 if( luaG_inter_move( U, L, L2, 1, eLM_LaneBody) < 0) // // config 208 if( luaG_inter_move( U, L, L2, 1, eLM_LaneBody) < 0) // // config
209 { 209 {
210 (void) luaL_error( L, "failed to copy settings when loading lanes.core"); 210 (void) luaL_error( L, "failed to copy settings when loading lanes.core");
211 } 211 }
212 // set L2:_R[CONFIG_REGKEY] = settings 212 // set L2:_R[CONFIG_REGKEY] = settings
213 REGISTRY_SET( L2, CONFIG_REGKEY, lua_insert( L2, -2)); // 213 CONFIG_REGKEY.set_registry(L2, [](lua_State* L) { lua_insert(L, -2); }); // config
214 STACK_END( L2, 0); 214 STACK_END( L2, 0);
215 STACK_END( L, 0); 215 STACK_END( L, 0);
216 DEBUGSPEW_CODE( -- U->debugspew_indent_depth); 216 DEBUGSPEW_CODE( -- U->debugspew_indent_depth);
@@ -297,7 +297,7 @@ void call_on_state_create( Universe* U, lua_State* L, lua_State* from_, LookupMo
297 // this doesn't count as an error though 297 // this doesn't count as an error though
298 return; 298 return;
299 } 299 }
300 REGISTRY_GET( L, CONFIG_REGKEY); // {} 300 CONFIG_REGKEY.query_registry(L); // {}
301 STACK_MID( L, 1); 301 STACK_MID( L, 1);
302 lua_getfield( L, -1, "on_state_create"); // {} on_state_create() 302 lua_getfield( L, -1, "on_state_create"); // {} on_state_create()
303 lua_remove( L, -2); // on_state_create() 303 lua_remove( L, -2); // on_state_create()
@@ -338,7 +338,7 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_)
338 STACK_MID( L, 0); 338 STACK_MID( L, 0);
339 339
340 // we'll need this every time we transfer some C function from/to this state 340 // we'll need this every time we transfer some C function from/to this state
341 REGISTRY_SET( L, LOOKUP_REGKEY, lua_newtable( L)); 341 LOOKUP_REGKEY.set_registry(L, [](lua_State* L) { lua_newtable(L); });
342 STACK_MID( L, 0); 342 STACK_MID( L, 0);
343 343
344 // neither libs (not even 'base') nor special init func: we are done 344 // neither libs (not even 'base') nor special init func: we are done
diff --git a/src/tools.cpp b/src/tools.cpp
index 88e7e2b..5196e7e 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -61,32 +61,32 @@ DEBUGSPEW_CODE( char const* debugspew_indent = "----+----!----+----!----+----!--
61 */ 61 */
62void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_) 62void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_)
63{ 63{
64 STACK_GROW( L, 3); 64 STACK_GROW(L, 3);
65 STACK_CHECK( L, 0); 65 STACK_CHECK(L, 0);
66 66
67 REGISTRY_GET( L, key_); // {}|nil 67 key_.query_registry(L); // {}|nil
68 STACK_MID( L, 1); 68 STACK_MID(L, 1);
69
70 if( lua_isnil( L, -1))
71 {
72 lua_pop( L, 1); //
73 lua_newtable( L); // {}
74 // _R[key_] = {}
75 REGISTRY_SET( L, key_, lua_pushvalue( L, -2)); // {}
76 STACK_MID( L, 1);
77 69
78 // Set its metatable if requested 70 if (lua_isnil(L, -1))
79 if( mode_)
80 { 71 {
81 lua_newtable( L); // {} mt 72 lua_pop(L, 1); //
82 lua_pushliteral( L, "__mode"); // {} mt "__mode" 73 lua_newtable(L); // {}
83 lua_pushstring( L, mode_); // {} mt "__mode" mode 74 // _R[key_] = {}
84 lua_rawset( L, -3); // {} mt 75 key_.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // {}
85 lua_setmetatable( L, -2); // {} 76 STACK_MID(L, 1);
77
78 // Set its metatable if requested
79 if (mode_)
80 {
81 lua_newtable(L); // {} mt
82 lua_pushliteral(L, "__mode"); // {} mt "__mode"
83 lua_pushstring(L, mode_); // {} mt "__mode" mode
84 lua_rawset(L, -3); // {} mt
85 lua_setmetatable(L, -2); // {}
86 }
86 } 87 }
87 } 88 STACK_END(L, 1);
88 STACK_END( L, 1); 89 ASSERT_L(lua_istable(L, -1));
89 ASSERT_L( lua_istable( L, -1));
90} 90}
91 91
92// ################################################################################################ 92// ################################################################################################
@@ -573,7 +573,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_)
573 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); 573 DEBUGSPEW_CODE( ++ U->debugspew_indent_depth);
574 STACK_GROW( L, 3); 574 STACK_GROW( L, 3);
575 STACK_CHECK( L, 0); 575 STACK_CHECK( L, 0);
576 REGISTRY_GET( L, LOOKUP_REGKEY); // {} 576 LOOKUP_REGKEY.query_registry(L); // {}
577 STACK_MID( L, 1); 577 STACK_MID( L, 1);
578 ASSERT_L( lua_istable( L, -1)); 578 ASSERT_L( lua_istable( L, -1));
579 if( lua_type( L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function 579 if( lua_type( L, in_base) == LUA_TFUNCTION) // for example when a module is a simple function
@@ -603,12 +603,12 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_)
603 STACK_MID( L, 2); 603 STACK_MID( L, 2);
604 } 604 }
605 // retrieve the cache, create it if we haven't done it yet 605 // retrieve the cache, create it if we haven't done it yet
606 REGISTRY_GET( L, LOOKUPCACHE_REGKEY); // {} {fqn} {cache}? 606 LOOKUPCACHE_REGKEY.query_registry(L); // {} {fqn} {cache}?
607 if( lua_isnil( L, -1)) 607 if( lua_isnil( L, -1))
608 { 608 {
609 lua_pop( L, 1); // {} {fqn} 609 lua_pop( L, 1); // {} {fqn}
610 lua_newtable( L); // {} {fqn} {cache} 610 lua_newtable( L); // {} {fqn} {cache}
611 REGISTRY_SET( L, LOOKUPCACHE_REGKEY, lua_pushvalue( L, -2)); 611 LOOKUPCACHE_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); });
612 STACK_MID( L, 3); 612 STACK_MID( L, 3);
613 } 613 }
614 // process everything we find in that table, filling in lookup data for all functions and tables we see there 614 // process everything we find in that table, filling in lookup data for all functions and tables we see there
@@ -719,7 +719,7 @@ static char const* find_lookup_name(lua_State* L, int i, LookupMode mode_, char
719 else 719 else
720 { 720 {
721 // fetch the name from the source state's lookup table 721 // fetch the name from the source state's lookup table
722 REGISTRY_GET( L, LOOKUP_REGKEY); // ... v ... {} 722 LOOKUP_REGKEY.query_registry(L); // ... v ... {}
723 STACK_MID( L, 1); 723 STACK_MID( L, 1);
724 ASSERT_L( lua_istable( L, -1)); 724 ASSERT_L( lua_istable( L, -1));
725 lua_pushvalue( L, i); // ... v ... {} v 725 lua_pushvalue( L, i); // ... v ... {} v
@@ -792,7 +792,7 @@ static bool lookup_table(lua_State* L2, lua_State* L, int i, LookupMode mode_, c
792 792
793 case eLM_LaneBody: 793 case eLM_LaneBody:
794 case eLM_FromKeeper: 794 case eLM_FromKeeper:
795 REGISTRY_GET( L2, LOOKUP_REGKEY); // {} 795 LOOKUP_REGKEY.query_registry(L2); // {}
796 STACK_MID( L2, 1); 796 STACK_MID( L2, 1);
797 ASSERT_L( lua_istable( L2, -1)); 797 ASSERT_L( lua_istable( L2, -1));
798 lua_pushlstring( L2, fqn, len); // {} "f.q.n" 798 lua_pushlstring( L2, fqn, len); // {} "f.q.n"
@@ -1093,7 +1093,7 @@ static void lookup_native_func(lua_State* L2, lua_State* L, int i, LookupMode mo
1093 1093
1094 case eLM_LaneBody: 1094 case eLM_LaneBody:
1095 case eLM_FromKeeper: 1095 case eLM_FromKeeper:
1096 REGISTRY_GET( L2, LOOKUP_REGKEY); // {} 1096 LOOKUP_REGKEY.query_registry(L2); // {}
1097 STACK_MID( L2, 1); 1097 STACK_MID( L2, 1);
1098 ASSERT_L( lua_istable( L2, -1)); 1098 ASSERT_L( lua_istable( L2, -1));
1099 lua_pushlstring( L2, fqn, len); // {} "f.q.n" 1099 lua_pushlstring( L2, fqn, len); // {} "f.q.n"
diff --git a/src/uniquekey.h b/src/uniquekey.h
index fb98628..777d640 100644
--- a/src/uniquekey.h
+++ b/src/uniquekey.h
@@ -1,6 +1,7 @@
1#pragma once 1#pragma once
2 2
3#include "compat.h" 3#include "compat.h"
4#include "macros_and_utils.h"
4 5
5class UniqueKey 6class UniqueKey
6{ 7{
@@ -38,4 +39,17 @@ class UniqueKey
38 // unfortunately, converting a scalar to a pointer must go through a C cast 39 // unfortunately, converting a scalar to a pointer must go through a C cast
39 return lua_touserdata(L, i) == (void*) m_storage; 40 return lua_touserdata(L, i) == (void*) m_storage;
40 } 41 }
42 void query_registry(lua_State* const L) const
43 {
44 push(L);
45 lua_rawget(L, LUA_REGISTRYINDEX);
46 }
47 template <typename OP>
48 void set_registry(lua_State* L, OP operation_) const
49 {
50 // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever)
51 push(L); // ... key
52 operation_(L); // ... key value
53 lua_rawset(L, LUA_REGISTRYINDEX); // ...
54 }
41}; 55};
diff --git a/src/universe.cpp b/src/universe.cpp
index d04a2f8..5d0d3b6 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -46,7 +46,7 @@ Universe* universe_create( lua_State* L)
46 Universe* U = (Universe*) lua_newuserdatauv( L, sizeof(Universe), 0); // universe 46 Universe* U = (Universe*) lua_newuserdatauv( L, sizeof(Universe), 0); // universe
47 memset( U, 0, sizeof( Universe)); 47 memset( U, 0, sizeof( Universe));
48 STACK_CHECK( L, 1); 48 STACK_CHECK( L, 1);
49 REGISTRY_SET( L, UNIVERSE_REGKEY, lua_pushvalue(L, -2)); // universe 49 UNIVERSE_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // universe
50 STACK_END( L, 1); 50 STACK_END( L, 1);
51 return U; 51 return U;
52} 52}
@@ -56,7 +56,7 @@ Universe* universe_create( lua_State* L)
56void universe_store( lua_State* L, Universe* U) 56void universe_store( lua_State* L, Universe* U)
57{ 57{
58 STACK_CHECK( L, 0); 58 STACK_CHECK( L, 0);
59 REGISTRY_SET( L, UNIVERSE_REGKEY, (nullptr != U) ? lua_pushlightuserdata( L, U) : lua_pushnil( L)); 59 UNIVERSE_REGKEY.set_registry(L, [U](lua_State* L) { U ? lua_pushlightuserdata( L, U) : lua_pushnil( L); });
60 STACK_END( L, 0); 60 STACK_END( L, 0);
61} 61}
62 62
@@ -67,7 +67,7 @@ Universe* universe_get( lua_State* L)
67 Universe* universe; 67 Universe* universe;
68 STACK_GROW( L, 2); 68 STACK_GROW( L, 2);
69 STACK_CHECK( L, 0); 69 STACK_CHECK( L, 0);
70 REGISTRY_GET( L, UNIVERSE_REGKEY); 70 UNIVERSE_REGKEY.query_registry(L);
71 universe = (Universe*) lua_touserdata( L, -1); // nullptr if nil 71 universe = (Universe*) lua_touserdata( L, -1); // nullptr if nil
72 lua_pop( L, 1); 72 lua_pop( L, 1);
73 STACK_END( L, 0); 73 STACK_END( L, 0);