From f0170ce8f1a90337637d387b87280f121d0578fe Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Thu, 21 Mar 2024 15:41:54 +0100 Subject: C++ migration: REGISTRY_SET and REGISTRY_GET are gone, welcome templates and lambdas --- src/deep.cpp | 2 +- src/keeper.cpp | 9 ++++---- src/lanes.cpp | 35 +++++++++++++++--------------- src/lanes_private.h | 2 +- src/macros_and_utils.h | 14 ------------ src/state.cpp | 10 ++++----- src/tools.cpp | 58 +++++++++++++++++++++++++------------------------- src/uniquekey.h | 14 ++++++++++++ src/universe.cpp | 6 +++--- 9 files changed, 75 insertions(+), 75 deletions(-) (limited to 'src') 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) { STACK_GROW( L, 1); STACK_CHECK( L, 1); // a - REGISTRY_GET( L, DEEP_LOOKUP_KEY); // a {} + DEEP_LOOKUP_KEY.query_registry(L); // a {} if( !lua_isnil( L, -1)) { 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_) STACK_GROW( L, 4); STACK_CHECK( L, 0); idx_ = lua_absindex( L, idx_); - REGISTRY_GET( L, FIFOS_KEY); // ud fifos + FIFOS_KEY.query_registry(L); // ud fifos lua_pushvalue( L, idx_); // ud fifos ud lua_rawget( L, -2); // ud fifos fifos[ud] STACK_MID( L, 2); @@ -196,7 +196,7 @@ int keeper_push_linda_storage( Universe* U, lua_State* L, void* ptr_, ptrdiff_t if( KL == nullptr) return 0; STACK_GROW( KL, 4); STACK_CHECK( KL, 0); - REGISTRY_GET( KL, FIFOS_KEY); // fifos + FIFOS_KEY.query_registry(KL); // fifos lua_pushlightuserdata( KL, ptr_); // fifos ud lua_rawget( KL, -2); // fifos storage lua_remove( KL, -2); // storage @@ -243,7 +243,7 @@ int keepercall_clear( lua_State* L) { STACK_GROW( L, 3); STACK_CHECK( L, 0); - REGISTRY_GET( L, FIFOS_KEY); // ud fifos + FIFOS_KEY.query_registry(L); // ud fifos lua_pushvalue( L, 1); // ud fifos ud lua_pushnil( L); // ud fifos ud nil lua_rawset( L, -3); // ud fifos @@ -712,9 +712,8 @@ void init_keepers( Universe* U, lua_State* L) // to see VM name in Decoda debugger lua_pushfstring( K, "Keeper #%d", i + 1); // "Keeper #n" lua_setglobal( K, "decoda_name"); // - // create the fifos table in the keeper state - REGISTRY_SET( K, FIFOS_KEY, lua_newtable( K)); + FIFOS_KEY.set_registry(K, [](lua_State* L) { lua_newtable(L); } ); STACK_END( K, 0); } 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) STACK_GROW( L, 3); STACK_CHECK( L, 0); - REGISTRY_GET( L, key); // ? + key.query_registry(L); // ? if( lua_isnil( L, -1)) // nil? { lua_pop( L, 1); // @@ -168,7 +168,7 @@ static bool push_registry_table( lua_State* L, UniqueKey key, bool create) } lua_newtable( L); // t - REGISTRY_SET( L, key, lua_pushvalue( L, -2)); + key.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); } STACK_END( L, 1); return true; // table pushed @@ -669,7 +669,7 @@ LUAG_FUNC( set_error_reporting) return luaL_error( L, "unsupported error reporting model"); } done: - REGISTRY_SET( L, EXTENDED_STACKTRACE_REGKEY, lua_pushboolean( L, equal)); + EXTENDED_STACKTRACE_REGKEY.set_registry(L, [equal](lua_State* L) { lua_pushboolean(L, equal); }); return 0; } @@ -679,7 +679,7 @@ static int lane_error( lua_State* L) int n; // error message (any type) - STACK_CHECK_ABS( L, 1); // some_error + STACK_CHECK_ABS( L, 1); // some_error // Don't do stack survey for cancelled lanes. // @@ -689,11 +689,11 @@ static int lane_error( lua_State* L) } STACK_GROW( L, 3); - REGISTRY_GET( L, EXTENDED_STACKTRACE_REGKEY); // some_error basic|extended + EXTENDED_STACKTRACE_REGKEY.query_registry(L); // some_error basic|extended bool const extended{ lua_toboolean(L, -1) ? true : false}; - lua_pop( L, 1); // some_error + lua_pop( L, 1); // some_error - // Place stack trace at 'registry[lane_error]' for the 'lua_pcall()' + // Place stack trace at 'registry[STACKTRACE_REGKEY]' for the 'lua_pcall()' // caller to fetch. This bypasses the Lua 5.1 limitation of only one // return value from error handler to 'lua_pcall()' caller. @@ -703,7 +703,7 @@ static int lane_error( lua_State* L) // // table of { "sourcefile.lua:", ... } // - lua_newtable( L); // some_error {} + lua_newtable( L); // some_error {} // Best to start from level 1, but in some cases it might be a C function // 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) { lua_pushfstring( L, "%s:?", ar.short_src); // some_error {} "blah" } - lua_rawseti( L, -2, (lua_Integer) n); // some_error {} + lua_rawseti( L, -2, (lua_Integer) n); // some_error {} } - REGISTRY_SET( L, STACKTRACE_REGKEY, lua_insert( L, -2)); // some_error + // store the stack trace table in the registry + STACKTRACE_REGKEY.set_registry(L, [](lua_State* L) { lua_insert(L, -2); }); // some_error STACK_END( L, 1); return 1; // the untouched error value @@ -764,7 +765,7 @@ static void push_stack_trace( lua_State* L, int rc_, int stk_base_) // fetch the call stack table from the registry where the handler stored it STACK_GROW( L, 1); // yields nil if no stack was generated (in case of cancellation for example) - REGISTRY_GET( L, STACKTRACE_REGKEY); // err trace|nil + STACKTRACE_REGKEY.query_registry(L); // err trace|nil STACK_END( L, 1); // For cancellation the error message is CANCEL_ERROR, and a stack trace isn't placed @@ -794,7 +795,7 @@ LUAG_FUNC( set_debug_threadname) lua_settop( L, 1); STACK_CHECK_ABS( L, 1); // 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... - REGISTRY_SET( L, hidden_regkey, lua_pushvalue( L, -2)); + hidden_regkey.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_MID( L, 1); s->debug_name = lua_tostring( L, -1); // keep a direct pointer on the string @@ -1276,7 +1277,7 @@ LUAG_FUNC( lane_new) lua_setiuservalue( L, -2, 1); // func libs priority globals package required gc_cb lane // Store 's' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). - REGISTRY_SET( L2, CANCEL_TEST_KEY, lua_pushlightuserdata( L2, s)); // func [... args ...] + CANCEL_TEST_KEY.set_registry(L2, [s](lua_State* L) { lua_pushlightuserdata(L, s); }); // func [... args ...] STACK_END( L, 1); STACK_END( L2, 1 + nargs); @@ -1993,7 +1994,7 @@ LUAG_FUNC( configure) STACK_MID( L, 2); // reference stack contains only the function argument 'settings' // we'll need this every time we transfer some C function from/to this state - REGISTRY_SET( L, LOOKUP_REGKEY, lua_newtable( L)); + LOOKUP_REGKEY.set_registry(L, [](lua_State* L) { lua_newtable(L); }); // settings M STACK_MID( L, 2); // register all native functions found in that module in the transferable functions database @@ -2015,8 +2016,8 @@ LUAG_FUNC( configure) } lua_pop( L, 1); // settings - // set _R[CONFIG_REGKEY] = settings - REGISTRY_SET( L, CONFIG_REGKEY, lua_pushvalue( L, -2)); // -2 because CONFIG_REGKEY is pushed before the value itself + // set _R[CONFIG_REGKEY] = settings + CONFIG_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_END( L, 1); DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); DEBUGSPEW_CODE( -- U->debugspew_indent_depth); @@ -2103,7 +2104,7 @@ LANES_API int luaopen_lanes_core( lua_State* L) lua_pushvalue( L, 1); // M "lanes.core" lua_pushvalue( L, -2); // M "lanes.core" M lua_pushcclosure( L, LG_configure, 2); // M LG_configure() - REGISTRY_GET( L, CONFIG_REGKEY); // M LG_configure() settings + CONFIG_REGKEY.query_registry(L); // M LG_configure() settings if( !lua_isnil( L, -1)) // this is not the first require "lanes.core": call configure() immediately { 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) Lane* s; STACK_GROW( L, 1); STACK_CHECK( L, 0); - REGISTRY_GET( L, CANCEL_TEST_KEY); + CANCEL_TEST_KEY.query_registry(L); s = (Lane*) lua_touserdata( L, -1); // lightuserdata (true 's_lane' pointer) / nil lua_pop( L, 1); 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; #define STACK_GROW( L, n) do { if (!lua_checkstack(L,(int)(n))) luaL_error( L, "Cannot grow stack!" ); } while( 0) -// non-string keyed registry access -#define REGISTRY_SET( L, key_, value_) \ -{ \ - key_.push(L); \ - value_; \ - lua_rawset( L, LUA_REGISTRYINDEX); \ -} - -#define REGISTRY_GET( L, key_) \ -{ \ - key_.push(L); \ - lua_rawget( L, LUA_REGISTRYINDEX); \ -} - #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) DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); - REGISTRY_GET( L, CONFIG_REGKEY); // config - // copy settings from from source to destination registry + CONFIG_REGKEY.query_registry(L); // config + // copy settings from from source to destination registry if( luaG_inter_move( U, L, L2, 1, eLM_LaneBody) < 0) // // config { (void) luaL_error( L, "failed to copy settings when loading lanes.core"); } // set L2:_R[CONFIG_REGKEY] = settings - REGISTRY_SET( L2, CONFIG_REGKEY, lua_insert( L2, -2)); // + CONFIG_REGKEY.set_registry(L2, [](lua_State* L) { lua_insert(L, -2); }); // config STACK_END( L2, 0); STACK_END( L, 0); DEBUGSPEW_CODE( -- U->debugspew_indent_depth); @@ -297,7 +297,7 @@ void call_on_state_create( Universe* U, lua_State* L, lua_State* from_, LookupMo // this doesn't count as an error though return; } - REGISTRY_GET( L, CONFIG_REGKEY); // {} + CONFIG_REGKEY.query_registry(L); // {} STACK_MID( L, 1); lua_getfield( L, -1, "on_state_create"); // {} on_state_create() lua_remove( L, -2); // on_state_create() @@ -338,7 +338,7 @@ lua_State* luaG_newstate( Universe* U, lua_State* from_, char const* libs_) STACK_MID( L, 0); // we'll need this every time we transfer some C function from/to this state - REGISTRY_SET( L, LOOKUP_REGKEY, lua_newtable( L)); + LOOKUP_REGKEY.set_registry(L, [](lua_State* L) { lua_newtable(L); }); STACK_MID( L, 0); // 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 = "----+----!----+----!----+----!-- */ void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_) { - STACK_GROW( L, 3); - STACK_CHECK( L, 0); - - REGISTRY_GET( L, key_); // {}|nil - STACK_MID( L, 1); - - if( lua_isnil( L, -1)) - { - lua_pop( L, 1); // - lua_newtable( L); // {} - // _R[key_] = {} - REGISTRY_SET( L, key_, lua_pushvalue( L, -2)); // {} - STACK_MID( L, 1); + STACK_GROW(L, 3); + STACK_CHECK(L, 0); + + key_.query_registry(L); // {}|nil + STACK_MID(L, 1); - // Set its metatable if requested - if( mode_) + if (lua_isnil(L, -1)) { - lua_newtable( L); // {} mt - lua_pushliteral( L, "__mode"); // {} mt "__mode" - lua_pushstring( L, mode_); // {} mt "__mode" mode - lua_rawset( L, -3); // {} mt - lua_setmetatable( L, -2); // {} + lua_pop(L, 1); // + lua_newtable(L); // {} + // _R[key_] = {} + key_.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // {} + STACK_MID(L, 1); + + // Set its metatable if requested + if (mode_) + { + lua_newtable(L); // {} mt + lua_pushliteral(L, "__mode"); // {} mt "__mode" + lua_pushstring(L, mode_); // {} mt "__mode" mode + lua_rawset(L, -3); // {} mt + lua_setmetatable(L, -2); // {} + } } - } - STACK_END( L, 1); - ASSERT_L( lua_istable( L, -1)); + STACK_END(L, 1); + ASSERT_L(lua_istable(L, -1)); } // ################################################################################################ @@ -573,7 +573,7 @@ void populate_func_lookup_table( lua_State* L, int _i, char const* name_) DEBUGSPEW_CODE( ++ U->debugspew_indent_depth); STACK_GROW( L, 3); STACK_CHECK( L, 0); - REGISTRY_GET( L, LOOKUP_REGKEY); // {} + LOOKUP_REGKEY.query_registry(L); // {} STACK_MID( L, 1); ASSERT_L( lua_istable( L, -1)); 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_) STACK_MID( L, 2); } // retrieve the cache, create it if we haven't done it yet - REGISTRY_GET( L, LOOKUPCACHE_REGKEY); // {} {fqn} {cache}? + LOOKUPCACHE_REGKEY.query_registry(L); // {} {fqn} {cache}? if( lua_isnil( L, -1)) { lua_pop( L, 1); // {} {fqn} lua_newtable( L); // {} {fqn} {cache} - REGISTRY_SET( L, LOOKUPCACHE_REGKEY, lua_pushvalue( L, -2)); + LOOKUPCACHE_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); STACK_MID( L, 3); } // 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 else { // fetch the name from the source state's lookup table - REGISTRY_GET( L, LOOKUP_REGKEY); // ... v ... {} + LOOKUP_REGKEY.query_registry(L); // ... v ... {} STACK_MID( L, 1); ASSERT_L( lua_istable( L, -1)); 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 case eLM_LaneBody: case eLM_FromKeeper: - REGISTRY_GET( L2, LOOKUP_REGKEY); // {} + LOOKUP_REGKEY.query_registry(L2); // {} STACK_MID( L2, 1); ASSERT_L( lua_istable( L2, -1)); 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 case eLM_LaneBody: case eLM_FromKeeper: - REGISTRY_GET( L2, LOOKUP_REGKEY); // {} + LOOKUP_REGKEY.query_registry(L2); // {} STACK_MID( L2, 1); ASSERT_L( lua_istable( L2, -1)); 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 @@ #pragma once #include "compat.h" +#include "macros_and_utils.h" class UniqueKey { @@ -38,4 +39,17 @@ class UniqueKey // unfortunately, converting a scalar to a pointer must go through a C cast return lua_touserdata(L, i) == (void*) m_storage; } + void query_registry(lua_State* const L) const + { + push(L); + lua_rawget(L, LUA_REGISTRYINDEX); + } + template + void set_registry(lua_State* L, OP operation_) const + { + // Note we can't check stack consistency because operation is not always a push (could be insert, replace, whatever) + push(L); // ... key + operation_(L); // ... key value + lua_rawset(L, LUA_REGISTRYINDEX); // ... + } }; 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) Universe* U = (Universe*) lua_newuserdatauv( L, sizeof(Universe), 0); // universe memset( U, 0, sizeof( Universe)); STACK_CHECK( L, 1); - REGISTRY_SET( L, UNIVERSE_REGKEY, lua_pushvalue(L, -2)); // universe + UNIVERSE_REGKEY.set_registry(L, [](lua_State* L) { lua_pushvalue(L, -2); }); // universe STACK_END( L, 1); return U; } @@ -56,7 +56,7 @@ Universe* universe_create( lua_State* L) void universe_store( lua_State* L, Universe* U) { STACK_CHECK( L, 0); - REGISTRY_SET( L, UNIVERSE_REGKEY, (nullptr != U) ? lua_pushlightuserdata( L, U) : lua_pushnil( L)); + UNIVERSE_REGKEY.set_registry(L, [U](lua_State* L) { U ? lua_pushlightuserdata( L, U) : lua_pushnil( L); }); STACK_END( L, 0); } @@ -67,7 +67,7 @@ Universe* universe_get( lua_State* L) Universe* universe; STACK_GROW( L, 2); STACK_CHECK( L, 0); - REGISTRY_GET( L, UNIVERSE_REGKEY); + UNIVERSE_REGKEY.query_registry(L); universe = (Universe*) lua_touserdata( L, -1); // nullptr if nil lua_pop( L, 1); STACK_END( L, 0); -- cgit v1.2.3-55-g6feb