From 45fab6f5b12f00631005b0086931a2f25385146e Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 24 Apr 2024 15:53:04 +0200 Subject: C++ migration: using KeeperState = Unique --- src/deep.cpp | 4 ++-- src/deep.h | 4 ++-- src/keeper.cpp | 28 ++++++++++++++-------------- src/keeper.h | 8 +++++--- src/lanes.cpp | 18 +++++++++--------- src/linda.cpp | 10 +++++----- src/macros_and_utils.h | 4 ++-- src/state.cpp | 8 ++++---- src/state.h | 2 +- src/tools.cpp | 2 +- src/tools.h | 4 ++-- 11 files changed, 47 insertions(+), 45 deletions(-) (limited to 'src') diff --git a/src/deep.cpp b/src/deep.cpp index e4d090f..241cb95 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -191,7 +191,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L, DeepPrelude* o_) * used in this Lua state (metatable, registring it). Otherwise, increments the * reference count. */ -char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, LookupMode mode_) +char const* DeepFactory::PushDeepProxy(DestState L, DeepPrelude* prelude, int nuv_, LookupMode mode_) { // Check if a proxy already exists push_registry_subtable_mode(L, DEEP_PROXY_CACHE_KEY, "v"); // DPC @@ -343,7 +343,7 @@ char const* DeepFactory::PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, L * * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()' */ -int DeepFactory::pushDeepUserdata(Dest L, int nuv_) const +int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const { STACK_GROW( L, 1); STACK_CHECK_START_REL(L, 0); diff --git a/src/deep.h b/src/deep.h index 95802d1..28069fe 100644 --- a/src/deep.h +++ b/src/deep.h @@ -77,8 +77,8 @@ class DeepFactory public: // NVI: public interface - int pushDeepUserdata(Dest L, int nuv_) const; + int pushDeepUserdata(DestState L, int nuv_) const; DeepPrelude* toDeep(lua_State* L, int index) const; static void DeleteDeepObject(lua_State* L, DeepPrelude* o_); - static char const* PushDeepProxy(Dest L, DeepPrelude* prelude, int nuv_, LookupMode mode_); + static char const* PushDeepProxy(DestState L, DeepPrelude* prelude, int nuv_, LookupMode mode_); }; diff --git a/src/keeper.cpp b/src/keeper.cpp index 48f0166..c5bbb9d 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -61,10 +61,10 @@ class keeper_fifo int limit{ -1 }; // a fifo full userdata has one uservalue, the table that holds the actual fifo contents - [[nodiscard]] static void* operator new([[maybe_unused]] size_t size_, lua_State* L) noexcept { return lua_newuserdatauv(L, 1); } + [[nodiscard]] static void* operator new([[maybe_unused]] size_t size_, KeeperState L_) noexcept { return lua_newuserdatauv(L_, 1); } // always embedded somewhere else or "in-place constructed" as a full userdata // can't actually delete the operator because the compiler generates stack unwinding code that could call it in case of exception - static void operator delete([[maybe_unused]] void* p_, lua_State* L_) { LUA_ASSERT(L_, !"should never be called"); } + static void operator delete([[maybe_unused]] void* p_, KeeperState L_) { LUA_ASSERT(L_, !"should never be called"); } [[nodiscard]] static keeper_fifo* getPtr(lua_State* L, int idx_) { @@ -95,7 +95,7 @@ static constexpr int CONTENTS_TABLE{ 1 }; // in: nothing // out: { first = 1, count = 0, limit = -1} -[[nodiscard]] static keeper_fifo* fifo_new(lua_State* L) +[[nodiscard]] static keeper_fifo* fifo_new(KeeperState L) { STACK_GROW(L, 2); STACK_CHECK_START_REL(L, 0); @@ -207,10 +207,10 @@ static void push_table(lua_State* L, int idx_) // ################################################################################################# -int keeper_push_linda_storage(Universe* U, Dest L, void* ptr_, uintptr_t magic_) +int keeper_push_linda_storage(Universe* U, DestState L, void* ptr_, uintptr_t magic_) { Keeper* const K{ which_keeper(U->keepers, magic_) }; - Source const KL{ K ? K->L : nullptr }; + SourceState const KL{ K ? K->L : nullptr }; if (KL == nullptr) return 0; STACK_GROW(KL, 4); // KEEPER MAIN @@ -288,7 +288,7 @@ int keepercall_send(lua_State* L) if (lua_isnil(L, -1)) { lua_pop(L, 1); // ud key ... fifos - std::ignore = fifo_new(L); // ud key ... fifos fifo + std::ignore = fifo_new(KeeperState{ L }); // ud key ... fifos fifo lua_pushvalue(L, 2); // ud key ... fifos fifo key lua_pushvalue(L, -2); // ud key ... fifos fifo key fifo lua_rawset(L, -4); // ud key ... fifos fifo @@ -396,7 +396,7 @@ int keepercall_limit(lua_State* L) if (fifo == nullptr) { // fifos key nil lua_pop(L, 1); // fifos key - fifo = fifo_new(L); // fifos key fifo + fifo = fifo_new(KeeperState{ L }); // fifos key fifo lua_rawset(L, -3); // fifos } // remove any clutter on the stack @@ -466,7 +466,7 @@ int keepercall_set(lua_State* L) { // fifos key [val [, ...]] nil // no need to wake writers in that case, because a writer can't wait on an inexistent key lua_pop(L, 1); // fifos key [val [, ...]] - std::ignore = fifo_new(L); // fifos key [val [, ...]] fifo + std::ignore = fifo_new(KeeperState{ L }); // fifos key [val [, ...]] fifo lua_pushvalue(L, 2); // fifos key [val [, ...]] fifo key lua_pushvalue(L, -2); // fifos key [val [, ...]] fifo key fifo lua_rawset(L, 1); // fifos key [val [, ...]] fifo @@ -615,7 +615,7 @@ void close_keepers(Universe* U) for (int i = 0; i < nbKeepers; ++i) { lua_State* K = U->keepers->keeper_array[i].L; - U->keepers->keeper_array[i].L = nullptr; + U->keepers->keeper_array[i].L = KeeperState{ nullptr }; if (K != nullptr) { lua_close(K); @@ -687,7 +687,7 @@ void init_keepers(Universe* U, lua_State* L) for (int i = 0; i < nb_keepers; ++i) // settings { // note that we will leak K if we raise an error later - lua_State* const K{ create_state(U, L) }; + KeeperState const K{ create_state(U, L) }; if (K == nullptr) { luaL_error(L, "init_keepers() failed while creating keeper states; out of memory"); // doesn't return @@ -719,7 +719,7 @@ void init_keepers(Universe* U, lua_State* L) if (!lua_isnil(L, -1)) { // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately - InterCopyContext c{ U, Dest{ K }, Source{ L }, {}, SourceIndex{ lua_absindex(L, -1) }, {}, LookupMode::ToKeeper, {} }; + InterCopyContext c{ U, DestState{ K }, SourceState{ L }, {}, SourceIndex{ lua_absindex(L, -1) }, {}, LookupMode::ToKeeper, {} }; if (c.inter_copy_package() != InterCopyResult::Success) { // if something went wrong, the error message is at the top of the stack @@ -832,7 +832,7 @@ void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode * * Returns: number of return values (pushed to 'L'), unset in case of error */ -KeeperCallResult keeper_call(Universe* U, lua_State* K, keeper_api_t func_, lua_State* L, void* linda, int starting_index) +KeeperCallResult keeper_call(Universe* U, KeeperState K, keeper_api_t func_, lua_State* L, void* linda, int starting_index) { KeeperCallResult result; int const args{ starting_index ? (lua_gettop(L) - starting_index + 1) : 0 }; @@ -845,7 +845,7 @@ KeeperCallResult keeper_call(Universe* U, lua_State* K, keeper_api_t func_, lua_ lua_pushlightuserdata(K, linda); // func_ linda if ( (args == 0) || - (InterCopyContext{ U, Dest{ K }, Source{ L }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) + (InterCopyContext{ U, DestState{ K }, SourceState{ L }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) ) { // func_ linda args... lua_call(K, 1 + args, LUA_MULTRET); // result... @@ -856,7 +856,7 @@ KeeperCallResult keeper_call(Universe* U, lua_State* K, keeper_api_t func_, lua_ // when attempting to grab the mutex again (WINVER <= 0x400 does this, but locks just fine, I don't know about pthread) if ( (retvals == 0) || - (InterCopyContext{ U, Dest{ L }, Source{ K }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) + (InterCopyContext{ U, DestState{ L }, SourceState{ K }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) ) // K->L { result.emplace(retvals); diff --git a/src/keeper.h b/src/keeper.h index 30de3a6..ef86f94 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -18,10 +18,12 @@ extern "C" { enum class LookupMode; class Universe; +using KeeperState = Unique; + struct Keeper { std::mutex m_mutex; - lua_State* L{ nullptr }; + KeeperState L{ nullptr }; // int count; }; @@ -43,7 +45,7 @@ void close_keepers(Universe* U); [[nodiscard]] Keeper* keeper_acquire(Keepers* keepers_, uintptr_t magic_); void keeper_release(Keeper* K_); void keeper_toggle_nil_sentinels(lua_State* L, int val_i_, LookupMode const mode_); -[[nodiscard]] int keeper_push_linda_storage(Universe* U, Dest L, void* ptr_, uintptr_t magic_); +[[nodiscard]] int keeper_push_linda_storage(Universe* U, DestState L, void* ptr_, uintptr_t magic_); using keeper_api_t = lua_CFunction; #define KEEPER_API(_op) keepercall_##_op @@ -59,4 +61,4 @@ using keeper_api_t = lua_CFunction; [[nodiscard]] int keepercall_count(lua_State* L); using KeeperCallResult = Unique>; -[[nodiscard]] KeeperCallResult keeper_call(Universe* U, lua_State* K, keeper_api_t _func, lua_State* L, void* linda, int starting_index); +[[nodiscard]] KeeperCallResult keeper_call(Universe* U, KeeperState K, keeper_api_t _func, lua_State* L, void* linda, int starting_index); diff --git a/src/lanes.cpp b/src/lanes.cpp index 880c150..d549449 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -1001,7 +1001,7 @@ LUAG_FUNC(lane_new) DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); // populate with selected libraries at the same time - lua_State* const L2{ luaG_newstate(U, Source{ L }, libs_str) }; // L // L2 + lua_State* const L2{ luaG_newstate(U, SourceState{ L }, libs_str) }; // L // L2 // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) Lane* const lane{ new (U) Lane{ U, L2 } }; @@ -1111,7 +1111,7 @@ LUAG_FUNC(lane_new) { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END)); // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack - InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, SourceIndex{ package_idx }, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L2 }, SourceState{ L }, {}, SourceIndex{ package_idx }, {}, {}, {} }; [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() }; LUA_ASSERT(L, ret == InterCopyResult::Success); // either all went well, or we should not even get here } @@ -1155,7 +1155,7 @@ LUAG_FUNC(lane_new) if (lua_pcall( L2, 1, 1, 0) != LUA_OK) // ret/errcode { // propagate error to main state if any - InterCopyContext c{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L }, SourceState{ L2 }, {}, {}, {}, {}, {} }; std::ignore = c.inter_move(1); // func libs priority globals package required gc_cb [... args ...] n "modname" error raise_lua_error(L); } @@ -1185,7 +1185,7 @@ LUAG_FUNC(lane_new) DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil // Lua 5.2 wants us to push the globals table on the stack - InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L2 }, SourceState{ L }, {}, {}, {}, {}, {} }; lua_pushglobaltable(L2); // _G while( lua_next(L, globals_idx)) // func libs priority globals package required gc_cb [... args ...] k v { @@ -1206,7 +1206,7 @@ LUAG_FUNC(lane_new) DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func - InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L2 }, SourceState{ L }, {}, {}, {}, {}, {} }; InterCopyResult const res{ c.inter_move(1) }; // func libs priority globals package required gc_cb [... args ...] // func if (res != InterCopyResult::Success) { @@ -1235,7 +1235,7 @@ LUAG_FUNC(lane_new) { DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); - InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L2 }, SourceState{ L }, {}, {}, {}, {}, {} }; InterCopyResult const res{ c.inter_move(nargs) }; // func libs priority globals package required gc_cb // func [... args ...] if (res != InterCopyResult::Success) { @@ -1402,7 +1402,7 @@ LUAG_FUNC(thread_join) int const n{ lua_gettop(L2) }; // whole L2 stack if ( (n > 0) && - (InterCopyContext{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }.inter_move(n) != InterCopyResult::Success) + (InterCopyContext{ U, DestState{ L }, SourceState{ L2 }, {}, {}, {}, {}, {} }.inter_move(n) != InterCopyResult::Success) ) { luaL_error(L, "tried to copy unsupported types"); // doesn't return @@ -1417,7 +1417,7 @@ LUAG_FUNC(thread_join) STACK_GROW(L, 3); lua_pushnil(L); // even when ERROR_FULL_STACK, if the error is not LUA_ERRRUN, the handler wasn't called, and we only have 1 error message on the stack ... - InterCopyContext c{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }; + InterCopyContext c{ U, DestState{ L }, SourceState{ L2 }, {}, {}, {}, {}, {} }; if (c.inter_move(n) != InterCopyResult::Success) // nil "err" [trace] { luaL_error(L, "tried to copy unsupported types: %s", lua_tostring(L, -n)); // doesn't return @@ -1815,7 +1815,7 @@ LUAG_FUNC(configure) STACK_CHECK(L, 2); { - char const* errmsg{ DeepFactory::PushDeepProxy(Dest{ L }, U->timer_deep, 0, LookupMode::LaneBody) }; // settings M timer_deep + char const* errmsg{ DeepFactory::PushDeepProxy(DestState{ L }, U->timer_deep, 0, LookupMode::LaneBody) }; // settings M timer_deep if (errmsg != nullptr) { return luaL_error(L, errmsg); diff --git a/src/linda.cpp b/src/linda.cpp index 4635d75..a19b126 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -307,7 +307,7 @@ LUAG_FUNC(linda_send) { Lane* const lane{ LANE_POINTER_REGKEY.readLightUserDataValue(L) }; Keeper* const K{ which_keeper(linda->U->keepers, linda->hashSeed()) }; - lua_State* const KL{ K ? K->L : nullptr }; + KeeperState const KL{ K ? K->L : nullptr }; if (KL == nullptr) return 0; @@ -473,7 +473,7 @@ LUAG_FUNC(linda_receive) Lane* const lane{ LANE_POINTER_REGKEY.readLightUserDataValue(L) }; Keeper* const K{ which_keeper(linda->U->keepers, linda->hashSeed()) }; - lua_State* const KL{ K ? K->L : nullptr }; + KeeperState const KL{ K ? K->L : nullptr }; if (KL == nullptr) return 0; @@ -863,7 +863,7 @@ LUAG_FUNC(linda_dump) auto dump = [](lua_State* L) { Linda* const linda{ ToLinda(L, 1) }; - return keeper_push_linda_storage(linda->U, Dest{ L }, linda, linda->hashSeed()); + return keeper_push_linda_storage(linda->U, DestState{ L }, linda, linda->hashSeed()); }; return Linda::ProtectedCall(L, dump); } @@ -877,7 +877,7 @@ LUAG_FUNC(linda_dump) LUAG_FUNC(linda_towatch) { Linda* const linda{ ToLinda(L, 1) }; - int pushed{ keeper_push_linda_storage(linda->U, Dest{ L }, linda, linda->hashSeed()) }; + int pushed{ keeper_push_linda_storage(linda->U, DestState{ L }, linda, linda->hashSeed()) }; if (pushed == 0) { // if the linda is empty, don't return nil @@ -1028,5 +1028,5 @@ LUAG_FUNC(linda) luaL_checktype(L, 1, LUA_TSTRING); luaL_checktype(L, 2, LUA_TNUMBER); } - return g_LindaFactory.pushDeepUserdata(Dest{ L }, 0); + return g_LindaFactory.pushDeepUserdata(DestState{ L }, 0); } diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index edda76e..068ff26 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -206,8 +206,8 @@ class Unique>> : public T // ################################################################################################# -using Source = Unique; -using Dest = Unique; +using SourceState = Unique; +using DestState = Unique; // ################################################################################################# diff --git a/src/state.cpp b/src/state.cpp index 9ad237d..2a69a46 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -190,7 +190,7 @@ static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const // ################################################################################################# // just like lua_xmove, args are (from, to) -static void copy_one_time_settings(Universe* U, Source L1, Dest L2) +static void copy_one_time_settings(Universe* U, SourceState L1, DestState L2) { DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); @@ -331,9 +331,9 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod * *NOT* called for keeper states! * */ -lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_) +lua_State* luaG_newstate(Universe* U, SourceState from_, char const* libs_) { - Dest const L{ create_state(U, from_) }; + DestState const L{ create_state(U, from_) }; STACK_GROW(L, 2); STACK_CHECK_START_ABS(L, 0); @@ -361,7 +361,7 @@ lua_State* luaG_newstate(Universe* U, Source from_, char const* libs_) copy_one_time_settings( U, from_, L); // 'lua.c' stops GC during initialization so perhaps its a good idea. :) - lua_gc( L, LUA_GCSTOP, 0); + lua_gc(L, LUA_GCSTOP, 0); // Anything causes 'base' to be taken in diff --git a/src/state.h b/src/state.h index 059e163..2d65f16 100644 --- a/src/state.h +++ b/src/state.h @@ -11,7 +11,7 @@ void serialize_require(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L); // ################################################################################################# [[nodiscard]] lua_State* create_state(Universe* U, lua_State* from_); -[[nodiscard]] lua_State* luaG_newstate(Universe* U, Source _from, char const* libs); +[[nodiscard]] lua_State* luaG_newstate(Universe* U, SourceState _from, char const* libs); // ################################################################################################# diff --git a/src/tools.cpp b/src/tools.cpp index e2ce8b8..a25fb2b 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -751,7 +751,7 @@ static constexpr RegistryUniqueKey REG_MTID{ 0x2E68F9B4751584DCull }; * Returns true if the table was cached (no need to fill it!); false if * it's a virgin. */ -[[nodiscard]] static bool push_cached_table(Dest L2, CacheIndex L2_cache_i, Source L1, SourceIndex i) +[[nodiscard]] static bool push_cached_table(DestState L2, CacheIndex L2_cache_i, SourceState L1, SourceIndex i) { void const* p{ lua_topointer(L1, i) }; diff --git a/src/tools.h b/src/tools.h index 673d6f0..755a0c6 100644 --- a/src/tools.h +++ b/src/tools.h @@ -34,8 +34,8 @@ class InterCopyContext public: Universe* const U; - Dest const L2; - Source const L1; + DestState const L2; + SourceState const L1; CacheIndex const L2_cache_i; SourceIndex L1_i; // that one can change when we reuse the context VT vt; // that one can change when we reuse the context -- cgit v1.2.3-55-g6feb