From 0d3b030eb92657dc276a6188f61e5cfdd7e265cb Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 27 May 2024 16:30:26 +0200 Subject: Optional Decoda support (disabled by default) --- src/intercopycontext.cpp | 17 +++++++++-------- src/lane.cpp | 20 ++++++++++---------- src/lane.h | 11 +++++++---- src/lanes.cpp | 3 +++ src/lanesconf.h | 1 + src/uniquekey.h | 6 +++--- src/universe.cpp | 6 +++++- 7 files changed, 38 insertions(+), 26 deletions(-) (limited to 'src') diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 6c72b1c..893305e 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -29,6 +29,7 @@ THE SOFTWARE. #include "debugspew.h" #include "deep.h" #include "keeper.h" +#include "lane.h" #include "linda.h" #include "universe.h" @@ -106,11 +107,11 @@ THE SOFTWARE. if (_fqn.empty() && !lua_istable(L1, L1_i)) { // raise an error if we try to send an unknown function (but not for tables) _fqn = std::string_view{}; // just in case // try to discover the name of the function we want to send - lua_getglobal(L1, "decoda_name"); // L1: ... v ... decoda_name + kLaneNameRegKey.pushValue(L1); // L1: ... v ... lane_name char const* _from{ lua_tostring(L1, -1) }; - lua_pushcfunction(L1, luaG_nameof); // L1: ... v ... decoda_name luaG_nameof - lua_pushvalue(L1, L1_i); // L1: ... v ... decoda_name luaG_nameof t - lua_call(L1, 1, 2); // L1: ... v ... decoda_name "type" "name"|nil + lua_pushcfunction(L1, luaG_nameof); // L1: ... v ... lane_name luaG_nameof + lua_pushvalue(L1, L1_i); // L1: ... v ... lane_name luaG_nameof t + lua_call(L1, 1, 2); // L1: ... v ... lane_name "type" "name"|nil char const* _typewhat{ (lua_type(L1, -2) == LUA_TSTRING) ? lua_tostring(L1, -2) : luaL_typename(L1, -2) }; // second return value can be nil if the table was not found // probable reason: the function was removed from the source Lua state before Lanes was required. @@ -325,10 +326,10 @@ void InterCopyContext::lookup_native_func() const // nil means we don't know how to transfer stuff: user should do something // anything other than function or table should not happen! if (!lua_isfunction(L2, -1) && !lua_istable(L2, -1)) { - lua_getglobal(L1, "decoda_name"); // L1: ... f ... decoda_name + kLaneNameRegKey.pushValue(L1); // L1: ... f ... lane_name char const* const _from{ lua_tostring(L1, -1) }; lua_pop(L1, 1); // L1: ... f ... - lua_getglobal(L2, "decoda_name"); // L1: ... f ... L2: {} f decoda_name + kLaneNameRegKey.pushValue(L2); // L1: ... f ... L2: {} f lane_name char const* const _to{ lua_tostring(L2, -1) }; lua_pop(L2, 1); // L2: {} f // when mode_ == LookupMode::FromKeeper, L is a keeper state and L2 is not, therefore L2 is the state where we want to raise the error @@ -449,10 +450,10 @@ void InterCopyContext::copy_cached_func() const STACK_CHECK(L2, 0); return false; } else if (!lua_istable(L2, -1)) { // this can happen if someone decides to replace same already registered item (for a example a standard lib function) with a table - lua_getglobal(L1, "decoda_name"); // L1: ... t ... decoda_name + kLaneNameRegKey.pushValue(L1); // L1: ... t ... lane_name char const* _from{ lua_tostring(L1, -1) }; lua_pop(L1, 1); // L1: ... t ... - lua_getglobal(L2, "decoda_name"); // L1: ... t ... L2: {} t decoda_name + kLaneNameRegKey.pushValue(L2); // L1: ... t ... L2: {} t lane_name char const* _to{ lua_tostring(L2, -1) }; lua_pop(L2, 1); // L1: ... t ... L2: {} t raise_luaL_error( diff --git a/src/lane.cpp b/src/lane.cpp index f220bce..ada1846 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -802,20 +802,20 @@ Lane::~Lane() // ################################################################################################# -void Lane::changeDebugName(int nameIdx_) +void Lane::changeDebugName(int const nameIdx_) { - // xxh64 of string "debugName" generated at https://www.pelock.com/products/hash-calculator - static constexpr RegistryUniqueKey kRegKey{ 0xA194E2645C57F6DDull }; - nameIdx_ = lua_absindex(L, nameIdx_); - luaL_checktype(L, nameIdx_, LUA_TSTRING); // L: ... "name" ... + int const _nameIdx{ lua_absindex(L, nameIdx_) }; + luaL_checktype(L, _nameIdx, LUA_TSTRING); // L: ... "name" ... STACK_CHECK_START_REL(L, 0); // 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... - kRegKey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); }); // L: ... "name" ... + kLaneNameRegKey.setValue(L, [idx = _nameIdx](lua_State* L_) { lua_pushvalue(L_, idx); }); // L: ... "name" ... // keep a direct pointer on the string - debugName = lua_tostringview(L, nameIdx_); - // to see VM name in Decoda debugger Virtual Machine window - lua_pushvalue(L, nameIdx_); // L: ... "name" ... "name" - lua_setglobal(L, "decoda_name"); // L: ... "name" ... + debugName = lua_tostringview(L, _nameIdx); + if constexpr (HAVE_DECODA_NAME()) { + // to see VM name in Decoda debugger Virtual Machine window + lua_pushvalue(L, _nameIdx); // L: ... "name" ... "name" + lua_setglobal(L, "decoda_name"); // L: ... "name" ... + } // and finally set the OS thread name THREAD_SETNAME(debugName.data()); STACK_CHECK(L, 0); diff --git a/src/lane.h b/src/lane.h index 1fdd955..4dcb99e 100644 --- a/src/lane.h +++ b/src/lane.h @@ -13,6 +13,9 @@ // ################################################################################################# +// xxh64 of string "kExtendedStackTraceRegKey" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kExtendedStackTraceRegKey{ 0x38147AD48FB426E2ull }; // used as registry key + /* * registry[FINALIZER_REG_KEY] is either nil (no finalizers) or a table * of functions that Lanes will call after the executing 'pcall' has ended. @@ -24,15 +27,15 @@ // xxh64 of string "kFinalizerRegKey" generated at https://www.pelock.com/products/hash-calculator static constexpr RegistryUniqueKey kFinalizerRegKey{ 0xFE936BFAA718FEEAull }; -// xxh64 of string "kExtendedStackTraceRegKey" generated at https://www.pelock.com/products/hash-calculator -static constexpr RegistryUniqueKey kExtendedStackTraceRegKey{ 0x38147AD48FB426E2ull }; // used as registry key - // xxh64 of string "kLaneGC" generated at https://www.pelock.com/products/hash-calculator static constexpr UniqueKey kLaneGC{ 0x5D6122141727F960ull }; // xxh64 of string "kLanePointerRegKey" generated at https://www.pelock.com/products/hash-calculator static constexpr RegistryUniqueKey kLanePointerRegKey{ 0x2D8CF03FE9F0A51Aull }; // used as registry key + // xxh64 of string "debugName" generated at https://www.pelock.com/products/hash-calculator +static constexpr RegistryUniqueKey kLaneNameRegKey{ 0xA194E2645C57F6DDull }; + // ################################################################################################# // The chain is ended by '(Lane*)(-1)', not nullptr: 'selfdestructFirst -> ... -> ... -> (-1)' @@ -124,7 +127,7 @@ class Lane Lane(Universe* U_, lua_State* L_, ErrorTraceLevel errorTraceLevel_); ~Lane(); - void changeDebugName(int nameIdx_); + void changeDebugName(int const nameIdx_); void close() { lua_State* _L{ L }; L = nullptr; lua_close(_L); } [[nodiscard]] std::string_view errorTraceLevelString() const; [[nodiscard]] int pushErrorHandler() const; diff --git a/src/lanes.cpp b/src/lanes.cpp index 20636b2..0ea0900 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -676,6 +676,9 @@ LUAG_FUNC(configure) // increment refcount so that this linda remains alive as long as the universe exists. _U->timerLinda->refcount.fetch_add(1, std::memory_order_relaxed); lua_pop(L_, 1); // L_: settings + // 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... + kLaneNameRegKey.setValue(L_, [](lua_State* L_) { std::ignore = lua_pushstringview(L_, "main"); }); + } STACK_CHECK(L_, 1); diff --git a/src/lanesconf.h b/src/lanesconf.h index 3836848..2df7a71 100644 --- a/src/lanesconf.h +++ b/src/lanesconf.h @@ -42,3 +42,4 @@ #endif // (defined PLATFORM_WIN32) || (defined PLATFORM_POCKETPC) #define USE_DEBUG_SPEW() 0 +#define HAVE_DECODA_NAME() 0 diff --git a/src/uniquekey.h b/src/uniquekey.h index 9981bb8..cfc8ab0 100644 --- a/src/uniquekey.h +++ b/src/uniquekey.h @@ -62,9 +62,9 @@ class RegistryUniqueKey void setValue(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) - pushKey(L_); // ... key - operation_(L_); // ... key value - lua_rawset(L_, LUA_REGISTRYINDEX); // ... + pushKey(L_); // ... key + operation_(L_); // ... key value + lua_rawset(L_, LUA_REGISTRYINDEX); // ... } // --------------------------------------------------------------------------------------------- template diff --git a/src/universe.cpp b/src/universe.cpp index 9e1ac5f..5fe53d5 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -286,7 +286,11 @@ void Universe::initializeKeepers(lua_State* L_) // to see VM name in Decoda debugger lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" - lua_setglobal(_K, "decoda_name"); // L_: settings K: + if constexpr (HAVE_DECODA_NAME()) { + lua_pushvalue(_K, -1); // K: "Keeper #n" Keeper #n" + lua_setglobal(_K, "decoda_name"); // L_: settings K: "Keeper #n" + } + kLaneNameRegKey.setValue(_K, [](lua_State* L_) { lua_insert(L_, -2); }); // K: // create the fifos table in the keeper state Keepers::CreateFifosTable(_K); STACK_CHECK(_K, 0); -- cgit v1.2.3-55-g6feb