From d60a9fb712886880ec9630e744e1258ec3ed19b1 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 29 Apr 2024 15:55:54 +0200 Subject: Progressively applying the coding rules --- src/cancel.cpp | 86 ++++++++++-------------- src/cancel.h | 4 +- src/compat.cpp | 38 +++++------ src/compat.h | 132 +++++++++++++++++++++++++++++-------- src/deep.cpp | 176 ++++++++++++++++++++++++------------------------- src/keeper.cpp | 58 ++++++++-------- src/keeper.h | 4 +- src/lanes.cpp | 50 +++++++------- src/macros_and_utils.h | 12 ++-- src/state.cpp | 16 ++--- src/tools.cpp | 12 ++-- 11 files changed, 321 insertions(+), 267 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 82c6def..e80c0b5 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -43,14 +43,14 @@ THE SOFTWARE. // ################################################################################################# /* -* Check if the thread in question ('L') has been signalled for cancel. -* -* Called by cancellation hooks and/or pending Linda operations (because then -* the check won't affect performance). -* -* Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, -* to make execution of the lane end. -*/ + * Check if the thread in question ('L') has been signalled for cancel. + * + * Called by cancellation hooks and/or pending Linda operations (because then + * the check won't affect performance). + * + * Returns CANCEL_SOFT/HARD if any locks are to be exited, and 'raise_cancel_error()' called, + * to make execution of the lane end. + */ [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) { Lane* const lane{ kLanePointerRegKey.readLightUserDataValue(L_) }; @@ -76,11 +76,10 @@ LUAG_FUNC(cancel_test) // ################################################################################################# // ################################################################################################# -[[nodiscard]] static void cancel_hook(lua_State* L_, [[maybe_unused]] lua_Debug* ar) +[[nodiscard]] static void cancel_hook(lua_State* L_, [[maybe_unused]] lua_Debug* ar_) { DEBUGSPEW_CODE(fprintf(stderr, "cancel_hook\n")); - if (cancel_test(L_) != CancelRequest::None) - { + if (cancel_test(L_) != CancelRequest::None) { lua_sethook(L_, nullptr, 0, 0); raise_cancel_error(L_); } @@ -108,15 +107,14 @@ LUAG_FUNC(cancel_test) // ################################################################################################# -[[nodiscard]] static CancelResult thread_cancel_soft(Lane* lane_, lua_Duration duration_, bool wake_lane_) +[[nodiscard]] static CancelResult thread_cancel_soft(Lane* lane_, lua_Duration duration_, bool wakeLane_) { lane_->cancel_request = CancelRequest::Soft; // it's now signaled to stop // negative timeout: we don't want to truly abort the lane, we just want it to react to cancel_test() on its own - if (wake_lane_) // wake the thread so that execution returns from any pending linda operation if desired + if (wakeLane_) // wake the thread so that execution returns from any pending linda operation if desired { std::condition_variable* const waiting_on{ lane_->m_waiting_on }; - if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) - { + if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) { waiting_on->notify_all(); } } @@ -126,15 +124,14 @@ LUAG_FUNC(cancel_test) // ################################################################################################# -[[nodiscard]] static CancelResult thread_cancel_hard(Lane* lane_, lua_Duration duration_, bool wake_lane_) +[[nodiscard]] static CancelResult thread_cancel_hard(Lane* lane_, lua_Duration duration_, bool wakeLane_) { lane_->cancel_request = CancelRequest::Hard; // it's now signaled to stop - //lane_->m_thread.get_stop_source().request_stop(); - if (wake_lane_) // wake the thread so that execution returns from any pending linda operation if desired + // lane_->m_thread.get_stop_source().request_stop(); + if (wakeLane_) // wake the thread so that execution returns from any pending linda operation if desired { std::condition_variable* waiting_on = lane_->m_waiting_on; - if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) - { + if (lane_->m_status == Lane::Waiting && waiting_on != nullptr) { waiting_on->notify_all(); } } @@ -145,58 +142,43 @@ LUAG_FUNC(cancel_test) // ################################################################################################# -CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration duration_, bool wake_lane_) +CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, lua_Duration duration_, bool wakeLane_) { // remember that lanes are not transferable: only one thread can cancel a lane, so no multithreading issue here // We can read 'lane_->status' without locks, but not wait for it (if Posix no PTHREAD_TIMEDJOIN) - if (lane_->m_status >= Lane::Done) - { + if (lane_->m_status >= Lane::Done) { // say "ok" by default, including when lane is already done return CancelResult::Cancelled; } // signal the linda the wake up the thread so that it can react to the cancel query // let us hope we never land here with a pointer on a linda that has been destroyed... - if (op_ == CancelOp::Soft) - { - return thread_cancel_soft(lane_, duration_, wake_lane_); - } - else if (static_cast(op_) > static_cast(CancelOp::Soft)) - { - lua_sethook(lane_->L, cancel_hook, static_cast(op_), hook_count_); + if (op_ == CancelOp::Soft) { + return thread_cancel_soft(lane_, duration_, wakeLane_); + } else if (static_cast(op_) > static_cast(CancelOp::Soft)) { + lua_sethook(lane_->L, cancel_hook, static_cast(op_), hookCount_); } - return thread_cancel_hard(lane_, duration_, wake_lane_); + return thread_cancel_hard(lane_, duration_, wakeLane_); } // ################################################################################################# // ################################################################################################# -CancelOp which_cancel_op(char const* op_string_) +CancelOp which_cancel_op(char const* opString_) { CancelOp op{ CancelOp::Invalid }; - if (strcmp(op_string_, "hard") == 0) - { + if (strcmp(opString_, "hard") == 0) { op = CancelOp::Hard; - } - else if (strcmp(op_string_, "soft") == 0) - { + } else if (strcmp(opString_, "soft") == 0) { op = CancelOp::Soft; - } - else if (strcmp(op_string_, "call") == 0) - { + } else if (strcmp(opString_, "call") == 0) { op = CancelOp::MaskCall; - } - else if (strcmp(op_string_, "ret") == 0) - { + } else if (strcmp(opString_, "ret") == 0) { op = CancelOp::MaskRet; - } - else if (strcmp(op_string_, "line") == 0) - { + } else if (strcmp(opString_, "line") == 0) { op = CancelOp::MaskLine; - } - else if (strcmp(op_string_, "count") == 0) - { + } else if (strcmp(opString_, "count") == 0) { op = CancelOp::MaskCount; } return op; @@ -206,13 +188,11 @@ CancelOp which_cancel_op(char const* op_string_) [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) { - if (lua_type(L_, idx_) == LUA_TSTRING) - { + if (lua_type(L_, idx_) == LUA_TSTRING) { char const* const str{ lua_tostring(L_, idx_) }; CancelOp op{ which_cancel_op(str) }; lua_remove(L_, idx_); // argument is processed, remove it - if (op == CancelOp::Invalid) - { + if (op == CancelOp::Invalid) { raise_luaL_error(L_, "invalid hook option %s", str); } return op; diff --git a/src/cancel.h b/src/cancel.h index 59ebefe..3df5252 100644 --- a/src/cancel.h +++ b/src/cancel.h @@ -48,8 +48,8 @@ enum class CancelOp // xxh64 of string "kCancelError" generated at https://www.pelock.com/products/hash-calculator static constexpr UniqueKey kCancelError{ 0x0630345FEF912746ull, "lanes.cancel_error" }; // 'raise_cancel_error' sentinel -[[nodiscard]] CancelOp which_cancel_op(char const* op_string_); -[[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hook_count_, lua_Duration secs_, bool wake_lindas_); +[[nodiscard]] CancelOp which_cancel_op(char const* opString_); +[[nodiscard]] CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, lua_Duration secs_, bool wakeLane_); [[noreturn]] static inline void raise_cancel_error(lua_State* L_) { diff --git a/src/compat.cpp b/src/compat.cpp index 41a206a..591e19f 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -12,35 +12,35 @@ // ################################################################################################# // Copied from Lua 5.2 loadlib.c -static int luaL_getsubtable(lua_State* L_, int idx, const char* fname) +static int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_) { - lua_getfield(L_, idx, fname); + lua_getfield(L_, idx_, fname_); if (lua_istable(L_, -1)) return 1; /* table already there */ else { lua_pop(L_, 1); /* remove previous result */ - idx = lua_absindex(L_, idx); + idx_ = lua_absindex(L_, idx_); lua_newtable(L_); lua_pushvalue(L_, -1); /* copy to be left at top */ - lua_setfield(L_, idx, fname); /* assign new table to field */ + lua_setfield(L_, idx_, fname_); /* assign new table to field */ return 0; /* false, because did not find table there */ } } // ################################################################################################# -void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb) +void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_) { - lua_pushcfunction(L_, openf); - lua_pushstring(L_, modname); /* argument to open function */ + lua_pushcfunction(L_, openf_); + lua_pushstring(L_, modname_); /* argument to open function */ lua_call(L_, 1, 1); /* open module */ luaL_getsubtable(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); lua_pushvalue(L_, -2); /* make copy of module (call result) */ - lua_setfield(L_, -2, modname); /* _LOADED[modname] = module */ + lua_setfield(L_, -2, modname_); /* _LOADED[modname] = module */ lua_pop(L_, 1); /* remove _LOADED table */ - if (glb) { + if (glb_) { lua_pushvalue(L_, -1); /* copy of 'mod' */ - lua_setglobal(L_, modname); /* _G[modname] = module */ + lua_setglobal(L_, modname_); /* _G[modname] = module */ } } #endif // LUA_VERSION_NUM @@ -51,23 +51,23 @@ void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int // ################################################################################################# // ################################################################################################# -void* lua_newuserdatauv(lua_State* L_, size_t sz, int nuvalue) +void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_) { - LUA_ASSERT(L_, nuvalue <= 1); - return lua_newuserdata(L_, sz); + LUA_ASSERT(L_, nuvalue_ <= 1); + return lua_newuserdata(L_, sz_); } // ################################################################################################# // push on stack uservalue #n of full userdata at idx -int lua_getiuservalue(lua_State* L_, int idx, int n) +int lua_getiuservalue(lua_State* L_, int idx_, int n_) { // full userdata can have only 1 uservalue before 5.4 - if (n > 1) { + if (n_ > 1) { lua_pushnil(L_); return LUA_TNONE; } - lua_getuservalue(L_, idx); + lua_getuservalue(L_, idx_); #if LUA_VERSION_NUM == 501 /* default environment is not a nil (see lua_getfenv) */ @@ -88,9 +88,9 @@ int lua_getiuservalue(lua_State* L_, int idx, int n) // Pops a value from the stack and sets it as the new n-th user value associated to the full userdata at the given index. // Returns 0 if the userdata does not have that value. -int lua_setiuservalue(lua_State* L_, int idx, int n) +int lua_setiuservalue(lua_State* L_, int idx_, int n_) { - if (n > 1 + if (n_ > 1 #if LUA_VERSION_NUM == 501 || lua_type(L_, -1) != LUA_TTABLE #endif @@ -99,7 +99,7 @@ int lua_setiuservalue(lua_State* L_, int idx, int n) return 0; } - lua_setuservalue(L_, idx); + lua_setuservalue(L_, idx_); return 1; // I guess anything non-0 is ok } diff --git a/src/compat.h b/src/compat.h index a7ad1f3..4816cb7 100644 --- a/src/compat.h +++ b/src/compat.h @@ -25,76 +25,150 @@ extern "C" // code is now preferring Lua 5.4 API +// ################################################################################################# + // add some Lua 5.3-style API when building for Lua 5.1 #if LUA_VERSION_NUM == 501 #define lua501_equal lua_equal -#define lua_absindex(L, idx) (((idx) >= 0 || (idx) <= LUA_REGISTRYINDEX) ? (idx) : lua_gettop(L) + (idx) + 1) +inline int lua_absindex(lua_State* L_, int idx_) +{ + return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); +} #if LUAJIT_VERSION_NUM < 20200 // moonjit is 5.1 plus bits of 5.2 that we don't need to wrap -#define lua_pushglobaltable(L) lua_pushvalue(L, LUA_GLOBALSINDEX) +inline void lua_pushglobaltable(lua_State* L_) +{ + lua_pushvalue(L_, LUA_GLOBALSINDEX); +} #endif // LUAJIT_VERSION_NUM -#define lua_setuservalue lua_setfenv -#define lua_getuservalue lua_getfenv -#define lua_rawlen lua_objlen -#define luaG_registerlibfuncs(L_, _funcs) luaL_register(L_, nullptr, _funcs) +inline int lua_setuservalue(lua_State* L_, int idx_) +{ + return lua_setfenv(L_, idx_); +} +inline void lua_getuservalue(lua_State* L_, int idx_) +{ + lua_getfenv(L_, idx_); +} +inline size_t lua_rawlen(lua_State* L_, int idx_) +{ + return lua_objlen(L_, idx_); +} +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) +{ + luaL_register(L_, nullptr, funcs_); +} +// keep as macros to be consistent with Lua headers #define LUA_OK 0 #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.1, we don't care about the actual value -void luaL_requiref(lua_State* L_, const char* modname, lua_CFunction openf, int glb); // implementation copied from Lua 5.2 sources -#define lua504_dump(L_, writer_, data_, strip_) lua_dump(L_, writer_, data_) +void luaL_requiref(lua_State* L_, const char* modname_, lua_CFunction openf_, int glb_); // implementation copied from Lua 5.2 sources +inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) +{ + return lua_dump(L_, writer_, data_); +} #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.1 #endif // LUA_VERSION_NUM == 501 +// ################################################################################################# + // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 502 #ifndef lua501_equal // already defined when compatibility is active in luaconf.h -#define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) +inline int lua501_equal(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPEQ); +} #endif // lua501_equal #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h -#define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) +inline int lua_lessthan(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPLT); +} #endif // lua_lessthan -#define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) -#define lua504_dump(L, writer, data, strip) lua_dump(L, writer, data) +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) +{ + luaL_setfuncs(L_, funcs_, 0); +} +inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_unused]] int strip_) +{ + return lua_dump(L_, writer_, data_); +} #define LUA_LOADED_TABLE "_LOADED" // // doesn't exist in Lua 5.2 #endif // LUA_VERSION_NUM == 502 +// ################################################################################################# + // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 503 #ifndef lua501_equal // already defined when compatibility is active in luaconf.h -#define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) +inline int lua501_equal(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPEQ); +} #endif // lua501_equal #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h -#define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) +inline int lua_lessthan(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPLT); +} #endif // lua_lessthan -#define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) -#define lua504_dump lua_dump -#define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) +{ + luaL_setfuncs(L_, funcs_, 0); +} +inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) +{ + return lua_dump(L_, writer_, data_, strip_); +} +inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) +{ + return static_cast(luaL_optinteger(L_, n_, d_)); +} #endif // LUA_VERSION_NUM == 503 +// ################################################################################################# + #if LUA_VERSION_NUM < 504 -void* lua_newuserdatauv(lua_State* L, size_t sz, int nuvalue); -int lua_getiuservalue(lua_State* L, int idx, int n); -int lua_setiuservalue(lua_State* L, int idx, int n); +void* lua_newuserdatauv(lua_State* L_, size_t sz_, int nuvalue_); +int lua_getiuservalue(lua_State* L_, int idx_, int n_); +int lua_setiuservalue(lua_State* L_, int idx_, int n_); #endif // LUA_VERSION_NUM < 504 +// ################################################################################################# + // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 504 #ifndef lua501_equal // already defined when compatibility is active in luaconf.h -#define lua501_equal(L, a, b) lua_compare(L, a, b, LUA_OPEQ) +inline int lua501_equal(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPEQ); +} #endif // lua501_equal #ifndef lua_lessthan // already defined when compatibility is active in luaconf.h -#define lua_lessthan(L, a, b) lua_compare(L, a, b, LUA_OPLT) +inline int lua_lessthan(lua_State* L_, int a_, int b_) +{ + return lua_compare(L_, a_, b_, LUA_OPLT); +} #endif // lua_lessthan -#define luaG_registerlibfuncs(L, _funcs) luaL_setfuncs(L, _funcs, 0) -#define lua504_dump lua_dump -#define luaL_optint(L, n, d) ((int) luaL_optinteger(L, (n), (d))) +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) +{ + luaL_setfuncs(L_, funcs_, 0); +} +inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) +{ + return lua_dump(L_, writer_, data_, strip_); +} +inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) +{ + return static_cast(luaL_optinteger(L_, n_, d_)); +} #define LUA_ERRGCMM 666 // doesn't exist in Lua 5.4, we don't care about the actual value #endif // LUA_VERSION_NUM == 504 @@ -117,11 +191,11 @@ enum class LuaType CDATA = 10 // LuaJIT CDATA }; -inline LuaType lua_type_as_enum(lua_State* L, int idx_) +inline LuaType lua_type_as_enum(lua_State* L_, int idx_) { - return static_cast(lua_type(L, idx_)); + return static_cast(lua_type(L_, idx_)); } -inline char const* lua_typename(lua_State* L, LuaType t_) +inline char const* lua_typename(lua_State* L_, LuaType t_) { - return lua_typename(L, static_cast(t_)); + return lua_typename(L_, static_cast(t_)); } diff --git a/src/deep.cpp b/src/deep.cpp index af7e814..13932a4 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -107,12 +107,12 @@ static void get_deep_lookup(lua_State* L_) * Return the registered factory for 'index' (deep userdata proxy), * or nullptr if 'index' is not a deep userdata proxy. */ -[[nodiscard]] static inline DeepFactory* get_factory(lua_State* L_, int index, LookupMode mode_) +[[nodiscard]] static inline DeepFactory* get_factory(lua_State* L_, int index_, LookupMode mode_) { // when looking inside a keeper, we are 100% sure the object is a deep userdata if (mode_ == LookupMode::FromKeeper) { - DeepPrelude* const proxy{ *lua_tofulluserdata(L_, index) }; + DeepPrelude* const proxy{ *lua_tofulluserdata(L_, index_) }; // we can (and must) cast and fetch the internally stored factory return &proxy->m_factory; } @@ -124,7 +124,7 @@ static void get_deep_lookup(lua_State* L_) STACK_GROW( L_, 1); STACK_CHECK_START_REL(L_, 0); - if (!lua_getmetatable( L_, index)) // deep ... metatable? + if (!lua_getmetatable( L_, index_)) // deep ... metatable? { return nullptr; // no metatable: can't be a deep userdata object! } @@ -168,11 +168,11 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) if (isLastRef) { // retrieve wrapped __gc - lua_pushvalue( L_, lua_upvalueindex( 1)); // self __gc? - if (!lua_isnil( L_, -1)) + lua_pushvalue(L_, lua_upvalueindex( 1)); // self __gc? + if (!lua_isnil(L_, -1)) { - lua_insert( L_, -2); // __gc self - lua_call( L_, 1, 0); // + lua_insert(L_, -2); // __gc self + lua_call(L_, 1, 0); // } else { @@ -195,138 +195,138 @@ 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(DestState 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, kDeepProxyCacheRegKey, "v"); // DPC - lua_pushlightuserdata(L, prelude); // DPC deep - lua_rawget(L, -2); // DPC proxy - if (!lua_isnil(L, -1)) + push_registry_subtable_mode(L_, kDeepProxyCacheRegKey, "v"); // DPC + lua_pushlightuserdata(L_, prelude_); // DPC deep + lua_rawget(L_, -2); // DPC proxy + if (!lua_isnil(L_, -1)) { - lua_remove(L, -2); // proxy + lua_remove(L_, -2); // proxy return nullptr; } else { - lua_pop(L, 1); // DPC + lua_pop(L_, 1); // DPC } - STACK_GROW(L, 7); - STACK_CHECK_START_REL(L, 0); + STACK_GROW(L_, 7); + STACK_CHECK_START_REL(L_, 0); // a new full userdata, fitted with the specified number of uservalue slots (always 1 for Lua < 5.4) - DeepPrelude** const proxy{ lua_newuserdatauv(L, nuv_) }; // DPC proxy - LUA_ASSERT(L, proxy); - *proxy = prelude; - prelude->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data + DeepPrelude** const proxy{ lua_newuserdatauv(L_, nuv_) }; // DPC proxy + LUA_ASSERT(L_, proxy); + *proxy = prelude_; + prelude_->m_refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data // Get/create metatable for 'factory' (in this state) - DeepFactory& factory = prelude->m_factory; - lua_pushlightuserdata( L, std::bit_cast(&factory)); // DPC proxy factory - get_deep_lookup( L); // DPC proxy metatable? + DeepFactory& factory = prelude_->m_factory; + lua_pushlightuserdata(L_, std::bit_cast(&factory)); // DPC proxy factory + get_deep_lookup(L_); // DPC proxy metatable? - if (lua_isnil( L, -1)) // // No metatable yet. + if (lua_isnil(L_, -1)) // // No metatable yet. { - lua_pop(L, 1); // DPC proxy - int const oldtop{ lua_gettop(L) }; + lua_pop(L_, 1); // DPC proxy + int const oldtop{ lua_gettop(L_) }; // 1 - make one and register it if (mode_ != LookupMode::ToKeeper) { - factory.createMetatable(L); // DPC proxy metatable - if (lua_gettop(L) - oldtop != 1 || !lua_istable(L, -1)) + factory.createMetatable(L_); // DPC proxy metatable + if (lua_gettop(L_) - oldtop != 1 || !lua_istable(L_, -1)) { // factory didn't push exactly 1 value, or the value it pushed is not a table: ERROR! - lua_settop( L, oldtop); // DPC proxy X - lua_pop( L, 3); // + lua_settop(L_, oldtop); // DPC proxy X + lua_pop(L_, 3); // return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; } // if the metatable contains a __gc, we will call it from our own - lua_getfield( L, -1, "__gc"); // DPC proxy metatable __gc + lua_getfield(L_, -1, "__gc"); // DPC proxy metatable __gc } else { // keepers need a minimal metatable that only contains our own __gc - lua_newtable( L); // DPC proxy metatable - lua_pushnil( L); // DPC proxy metatable nil + lua_newtable(L_); // DPC proxy metatable + lua_pushnil(L_); // DPC proxy metatable nil } - if (lua_isnil( L, -1)) + if (lua_isnil(L_, -1)) { // Add our own '__gc' method - lua_pop( L, 1); // DPC proxy metatable - lua_pushcfunction( L, deep_userdata_gc); // DPC proxy metatable deep_userdata_gc + lua_pop(L_, 1); // DPC proxy metatable + lua_pushcfunction(L_, deep_userdata_gc); // DPC proxy metatable deep_userdata_gc } else { // Add our own '__gc' method wrapping the original - lua_pushcclosure( L, deep_userdata_gc, 1); // DPC proxy metatable deep_userdata_gc + lua_pushcclosure(L_, deep_userdata_gc, 1); // DPC proxy metatable deep_userdata_gc } - lua_setfield( L, -2, "__gc"); // DPC proxy metatable + lua_setfield(L_, -2, "__gc"); // DPC proxy metatable // Memorize for later rounds - lua_pushvalue( L, -1); // DPC proxy metatable metatable - lua_pushlightuserdata(L, std::bit_cast(&factory)); // DPC proxy metatable metatable factory - set_deep_lookup( L); // DPC proxy metatable + lua_pushvalue(L_, -1); // DPC proxy metatable metatable + lua_pushlightuserdata(L_, std::bit_cast(&factory)); // DPC proxy metatable metatable factory + set_deep_lookup(L_); // DPC proxy metatable // 2 - cause the target state to require the module that exported the factory if (char const* const modname{ factory.moduleName() }; modname) // we actually got a module name { // L.registry._LOADED exists without having registered the 'package' library. - lua_getglobal( L, "require"); // DPC proxy metatable require() + lua_getglobal(L_, "require"); // DPC proxy metatable require() // check that the module is already loaded (or being loaded, we are happy either way) - if (lua_isfunction( L, -1)) + if (lua_isfunction(L_, -1)) { - lua_pushstring( L, modname); // DPC proxy metatable require() "module" - lua_getfield( L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED - if (lua_istable( L, -1)) + lua_pushstring(L_, modname); // DPC proxy metatable require() "module" + lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // DPC proxy metatable require() "module" _R._LOADED + if (lua_istable(L_, -1)) { - lua_pushvalue( L, -2); // DPC proxy metatable require() "module" _R._LOADED "module" - lua_rawget( L, -2); // DPC proxy metatable require() "module" _R._LOADED module - int const alreadyloaded = lua_toboolean( L, -1); + lua_pushvalue(L_, -2); // DPC proxy metatable require() "module" _R._LOADED "module" + lua_rawget(L_, -2); // DPC proxy metatable require() "module" _R._LOADED module + int const alreadyloaded = lua_toboolean(L_, -1); if (!alreadyloaded) // not loaded { int require_result; - lua_pop( L, 2); // DPC proxy metatable require() "module" + lua_pop(L_, 2); // DPC proxy metatable require() "module" // require "modname" - require_result = lua_pcall( L, 1, 0, 0); // DPC proxy metatable error? + require_result = lua_pcall(L_, 1, 0, 0); // DPC proxy metatable error? if (require_result != LUA_OK) { // failed, return the error message - lua_pushfstring( L, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); - lua_insert( L, -2); // DPC proxy metatable prefix error - lua_concat( L, 2); // DPC proxy metatable error - return lua_tostring( L, -1); + lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", modname); + lua_insert(L_, -2); // DPC proxy metatable prefix error + lua_concat(L_, 2); // DPC proxy metatable error + return lua_tostring(L_, -1); } } else // already loaded, we are happy { - lua_pop( L, 4); // DPC proxy metatable + lua_pop(L_, 4); // DPC proxy metatable } } else // no L.registry._LOADED; can this ever happen? { - lua_pop( L, 6); // + lua_pop(L_, 6); // return "unexpected error while requiring a module identified by DeepFactory::moduleName"; } } else // a module name, but no require() function :-( { - lua_pop( L, 4); // + lua_pop(L_, 4); // return "lanes receiving deep userdata should register the 'package' library"; } } } - STACK_CHECK(L, 2); // DPC proxy metatable - LUA_ASSERT(L, lua_type_as_enum(L, -2) == LuaType::USERDATA); - LUA_ASSERT(L, lua_istable( L, -1)); - lua_setmetatable( L, -2); // DPC proxy + STACK_CHECK(L_, 2); // DPC proxy metatable + LUA_ASSERT(L_, lua_type_as_enum(L_, -2) == LuaType::USERDATA); + LUA_ASSERT(L_, lua_istable(L_, -1)); + lua_setmetatable(L_, -2); // DPC proxy // If we're here, we obviously had to create a new proxy, so cache it. - lua_pushlightuserdata( L, prelude); // DPC proxy deep - lua_pushvalue( L, -2); // DPC proxy deep proxy - lua_rawset( L, -4); // DPC proxy - lua_remove( L, -2); // proxy - LUA_ASSERT(L, lua_type_as_enum(L, -1) == LuaType::USERDATA); - STACK_CHECK(L, 0); + lua_pushlightuserdata(L_, prelude_); // DPC proxy deep + lua_pushvalue(L_, -2); // DPC proxy deep proxy + lua_rawset(L_, -4); // DPC proxy + lua_remove(L_, -2); // proxy + LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA); + STACK_CHECK(L_, 0); return nullptr; } @@ -347,40 +347,40 @@ char const* DeepFactory::PushDeepProxy(DestState L, DeepPrelude* prelude, int nu * * Returns: 'proxy' userdata for accessing the deep data via 'DeepFactory::toDeep()' */ -int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const +int DeepFactory::pushDeepUserdata(DestState L_, int nuv_) const { - STACK_GROW( L, 1); - STACK_CHECK_START_REL(L, 0); - int const oldtop{ lua_gettop(L) }; - DeepPrelude* const prelude{ newDeepObjectInternal(L) }; + STACK_GROW(L_, 1); + STACK_CHECK_START_REL(L_, 0); + int const oldtop{ lua_gettop(L_) }; + DeepPrelude* const prelude{ newDeepObjectInternal(L_) }; if (prelude == nullptr) { - raise_luaL_error(L, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); + raise_luaL_error(L_, "DeepFactory::newDeepObjectInternal failed to create deep userdata (out of memory)"); } if (prelude->m_magic != kDeepVersion) { // just in case, don't leak the newly allocated deep userdata object - deleteDeepObjectInternal(L, prelude); - raise_luaL_error(L, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); + deleteDeepObjectInternal(L_, prelude); + raise_luaL_error(L_, "Bad Deep Factory: kDeepVersion is incorrect, rebuild your implementation with the latest deep implementation"); } - LUA_ASSERT(L, prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 - LUA_ASSERT(L, &prelude->m_factory == this); + LUA_ASSERT(L_, prelude->m_refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 + LUA_ASSERT(L_, &prelude->m_factory == this); - if (lua_gettop( L) - oldtop != 0) + if (lua_gettop(L_) - oldtop != 0) { // just in case, don't leak the newly allocated deep userdata object - deleteDeepObjectInternal(L, prelude); - raise_luaL_error(L, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); + deleteDeepObjectInternal(L_, prelude); + raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); } - char const* const errmsg{ DeepFactory::PushDeepProxy(L, prelude, nuv_, LookupMode::LaneBody) }; // proxy + char const* const errmsg{ DeepFactory::PushDeepProxy(L_, prelude, nuv_, LookupMode::LaneBody) }; // proxy if (errmsg != nullptr) { - raise_luaL_error(L, errmsg); + raise_luaL_error(L_, errmsg); } - STACK_CHECK( L, 1); + STACK_CHECK(L_, 1); return 1; } @@ -392,24 +392,24 @@ int DeepFactory::pushDeepUserdata(DestState L, int nuv_) const * Reference count is not changed, and access to the deep userdata is not * serialized. It is the module's responsibility to prevent conflicting usage. */ -DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index) const +DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index_) const { STACK_CHECK_START_REL(L_, 0); // ensure it is actually a deep userdata we created - if (get_factory(L_, index, LookupMode::LaneBody) != this) + if (get_factory(L_, index_, LookupMode::LaneBody) != this) { return nullptr; // no metatable, or wrong kind } STACK_CHECK(L_, 0); - DeepPrelude** const proxy{ lua_tofulluserdata(L_, index) }; + DeepPrelude** const proxy{ lua_tofulluserdata(L_, index_) }; return *proxy; } // ################################################################################################# /* - * Copy deep userdata between two separate Lua states (from L to L2) + * Copy deep userdata between two separate Lua states (from L1 to L2) * * Returns: * the id function of the copied value, or nullptr for non-deep userdata diff --git a/src/keeper.cpp b/src/keeper.cpp index 33da736..cbdd0c9 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -209,13 +209,13 @@ static void push_table(lua_State* L_, int idx_) // ################################################################################################# // only used by linda:dump() and linda:__towatch() for debugging purposes -int keeper_push_linda_storage(Linda& linda_, DestState L) +int keeper_push_linda_storage(Linda& linda_, DestState L_) { Keeper* const K{ linda_.whichKeeper() }; SourceState const KL{ K ? K->L : nullptr }; if (KL == nullptr) return 0; - STACK_GROW(KL, 4); // KEEPER MAIN + STACK_GROW(KL, 4); // KEEPER MAIN STACK_CHECK_START_REL(KL, 0); kFifosRegKey.pushValue(KL); // fifos lua_pushlightuserdata(KL, &linda_); // fifos ud @@ -228,33 +228,33 @@ int keeper_push_linda_storage(Linda& linda_, DestState L) return 0; } // move data from keeper to destination state - STACK_GROW(L, 5); - STACK_CHECK_START_REL(L, 0); - lua_newtable(L); // out - InterCopyContext c{ linda_.U, L, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; + STACK_GROW(L_, 5); + STACK_CHECK_START_REL(L_, 0); + lua_newtable(L_); // out + InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; lua_pushnil(KL); // storage nil while (lua_next(KL, -2)) // storage key fifo { keeper_fifo* fifo = prepare_fifo_access(KL, -1); // storage key fifotbl lua_pushvalue(KL, -2); // storage key fifotbl key - std::ignore = c.inter_move(1); // storage key fifotbl // out key - STACK_CHECK(L, 2); - lua_newtable(L); // out key keyout - std::ignore = c.inter_move(1); // storage key // out key keyout fifotbl - lua_pushinteger(L, fifo->first); // out key keyout fifotbl first - STACK_CHECK(L, 5); - lua_setfield(L, -3, "first"); // out key keyout fifotbl - lua_pushinteger(L, fifo->count); // out key keyout fifobtl count - STACK_CHECK(L, 5); - lua_setfield(L, -3, "count"); // out key keyout fifotbl - lua_pushinteger(L, fifo->limit); // out key keyout fifotbl limit - STACK_CHECK(L, 5); - lua_setfield(L, -3, "limit"); // out key keyout fifotbl - lua_setfield(L, -2, "fifo"); // out key keyout - lua_rawset(L, -3); // out - STACK_CHECK(L, 1); - } - STACK_CHECK(L, 1); + std::ignore = c.inter_move(1); // storage key fifotbl // out key + STACK_CHECK(L_, 2); + lua_newtable(L_); // out key keyout + std::ignore = c.inter_move(1); // storage key // out key keyout fifotbl + lua_pushinteger(L_, fifo->first); // out key keyout fifotbl first + STACK_CHECK(L_, 5); + lua_setfield(L_, -3, "first"); // out key keyout fifotbl + lua_pushinteger(L_, fifo->count); // out key keyout fifobtl count + STACK_CHECK(L_, 5); + lua_setfield(L_, -3, "count"); // out key keyout fifotbl + lua_pushinteger(L_, fifo->limit); // out key keyout fifotbl limit + STACK_CHECK(L_, 5); + lua_setfield(L_, -3, "limit"); // out key keyout fifotbl + lua_setfield(L_, -2, "fifo"); // out key keyout + lua_rawset(L_, -3); // out + STACK_CHECK(L_, 1); + } + STACK_CHECK(L_, 1); lua_pop(KL, 1); // STACK_CHECK(KL, 0); return 1; @@ -776,10 +776,10 @@ void Linda::releaseKeeper(Keeper* K_) const // ################################################################################################# -void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mode_) +void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) { int const n{ lua_gettop(L_) }; - for (int i = val_i_; i <= n; ++i) + for (int i = start_; i <= n; ++i) { if (mode_ == LookupMode::ToKeeper) { @@ -811,17 +811,17 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mod * * Returns: number of return values (pushed to 'L'), unset in case of error */ -KeeperCallResult keeper_call(Universe* U, KeeperState 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 }; + int const args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; int const top_K{ lua_gettop(K) }; // if we didn't do anything wrong, the keeper stack should be clean LUA_ASSERT(L_, lua_gettop(K) == 0); STACK_GROW(K, 2); PUSH_KEEPER_FUNC(K, func_); // func_ - lua_pushlightuserdata(K, linda); // func_ linda + lua_pushlightuserdata(K, linda_); // func_ linda if ( (args == 0) || (InterCopyContext{ U, DestState{ K }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) diff --git a/src/keeper.h b/src/keeper.h index 3740fc5..bf1ba17 100644 --- a/src/keeper.h +++ b/src/keeper.h @@ -42,8 +42,8 @@ static constexpr UniqueKey kNilSentinel{ 0xC457D4EDDB05B5E4ull, "lanes.null" }; void init_keepers(Universe* U, lua_State* L_); void close_keepers(Universe* U); -void keeper_toggle_nil_sentinels(lua_State* L_, int val_i_, LookupMode const mode_); -[[nodiscard]] int keeper_push_linda_storage(Linda& linda_, DestState L); +void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_); +[[nodiscard]] int keeper_push_linda_storage(Linda& linda_, DestState L_); using keeper_api_t = lua_CFunction; #define KEEPER_API(_op) keepercall_##_op diff --git a/src/lanes.cpp b/src/lanes.cpp index d87d93e..16c8e47 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -208,7 +208,7 @@ static void securize_debug_threadname(lua_State* L_, Lane* lane_) STACK_GROW(L_, 3); lua_getiuservalue(L_, 1, 1); lua_newtable(L_); - // Lua 5.1 can't do 'lane_->debug_name = lua_pushstring(L, lane_->debug_name);' + // Lua 5.1 can't do 'lane_->debug_name = lua_pushstring(L_, lane_->debug_name);' lua_pushstring(L_, lane_->debug_name); lane_->debug_name = lua_tostring(L_, -1); lua_rawset(L_, -3); @@ -371,7 +371,7 @@ static void push_stack_trace(lua_State* L_, int rc_, int stk_base_) if (lua_rc_ != LUA_OK) // we have an error message and an optional stack trace at the bottom of the stack { LUA_ASSERT(L_, finalizers_index == 2 || finalizers_index == 3); - //char const* err_msg = lua_tostring(L, 1); + //char const* err_msg = lua_tostring(L_, 1); lua_pushvalue(L_, 1); // ... finalizers lane_error finalizer err_msg // note we don't always have a stack trace for example when kCancelError, or when we got an error that doesn't call our handler, such as LUA_ERRMEM if (finalizers_index == 3) @@ -819,18 +819,18 @@ static char const* get_errcode_name( int _code) } #endif // USE_DEBUG_SPEW() -static void lane_main(Lane* lane) +static void lane_main(Lane* lane_) { - lua_State* const L{ lane->L }; + lua_State* const L{ lane_->L }; // wait until the launching thread has finished preparing L - lane->m_ready.wait(); + lane_->m_ready.wait(); int rc{ LUA_ERRRUN }; - if (lane->m_status == Lane::Pending) // nothing wrong happened during preparation, we can work + if (lane_->m_status == Lane::Pending) // nothing wrong happened during preparation, we can work { // At this point, the lane function and arguments are on the stack int const nargs{ lua_gettop(L) - 1 }; DEBUGSPEW_CODE(Universe* U = universe_get(L)); - lane->m_status = Lane::Running; // Pending -> Running + lane_->m_status = Lane::Running; // Pending -> Running // Tie "set_finalizer()" to the state lua_pushcfunction(L, LG_set_finalizer); @@ -839,7 +839,7 @@ static void lane_main(Lane* lane) // Tie "set_debug_threadname()" to the state // But don't register it in the lookup database because of the Lane pointer upvalue - lua_pushlightuserdata(L, lane); + lua_pushlightuserdata(L, lane_); lua_pushcclosure(L, LG_set_debug_threadname, 1); lua_setglobal(L, "set_debug_threadname"); @@ -879,24 +879,24 @@ static void lane_main(Lane* lane) // the finalizer generated an error, and left its own error message [and stack trace] on the stack rc = rc2; // we're overruling the earlier script error or normal return } - lane->m_waiting_on = nullptr; // just in case - if (selfdestruct_remove(lane)) // check and remove (under lock!) + lane_->m_waiting_on = nullptr; // just in case + if (selfdestruct_remove(lane_)) // check and remove (under lock!) { // We're a free-running thread and no-one's there to clean us up. - lua_close(lane->L); - lane->L = nullptr; // just in case - lane->U->selfdestruct_cs.lock(); + lua_close(lane_->L); + lane_->L = nullptr; // just in case + lane_->U->selfdestruct_cs.lock(); // done with lua_close(), terminal shutdown sequence may proceed - lane->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); - lane->U->selfdestruct_cs.unlock(); + lane_->U->selfdestructing_count.fetch_sub(1, std::memory_order_release); + lane_->U->selfdestruct_cs.unlock(); // we destroy our jthread member from inside the thread body, so we have to detach so that we don't try to join, as this doesn't seem a good idea - lane->m_thread.detach(); - delete lane; - lane = nullptr; + lane_->m_thread.detach(); + delete lane_; + lane_ = nullptr; } } - if (lane) + if (lane_) { // leave results (1..top) or error message + stack trace (1..2) on the stack - master will copy them @@ -904,9 +904,9 @@ static void lane_main(Lane* lane) { // 'm_done_mutex' protects the -> Done|Error|Cancelled state change - std::lock_guard lock{ lane->m_done_mutex }; - lane->m_status = st; - lane->m_done_signal.notify_one();// wake up master (while 'lane->m_done_mutex' is on) + std::lock_guard lock{ lane_->m_done_mutex }; + lane_->m_status = st; + lane_->m_done_signal.notify_one();// wake up master (while 'lane_->m_done_mutex' is on) } } } @@ -946,7 +946,7 @@ LUAG_FUNC(register) // ignore extra parameters, just in case lua_settop(L_, 2); luaL_argcheck(L_, (mod_type == LuaType::TABLE) || (mod_type == LuaType::FUNCTION), 2, "unexpected module type"); - DEBUGSPEW_CODE(Universe* U = universe_get(L)); + DEBUGSPEW_CODE(Universe* U = universe_get(L_)); STACK_CHECK_START_REL(L_, 0); // "name" mod_table DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); @@ -1747,7 +1747,7 @@ LUAG_FUNC(configure) STACK_GROW(L_, 4); STACK_CHECK_START_ABS(L_, 1); // settings - DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); + DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L_)); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); if (U == nullptr) @@ -1898,7 +1898,7 @@ LUAG_FUNC(configure) // set _R[kConfigRegKey] = settings kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); STACK_CHECK(L_, 1); - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L_)); // Return the settings table return 1; } diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index dfde550..5d1467a 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -78,13 +78,13 @@ template #ifdef NDEBUG -#define LUA_ASSERT(L, c) ; // nothing +#define LUA_ASSERT(L_, c) ; // nothing -#define STACK_CHECK_START_REL(L, offset_) -#define STACK_CHECK_START_ABS(L, offset_) -#define STACK_CHECK_RESET_REL(L, offset_) -#define STACK_CHECK_RESET_ABS(L, offset_) -#define STACK_CHECK(L, offset_) +#define STACK_CHECK_START_REL(L_, offset_) +#define STACK_CHECK_START_ABS(L_, offset_) +#define STACK_CHECK_RESET_REL(L_, offset_) +#define STACK_CHECK_RESET_ABS(L_, offset_) +#define STACK_CHECK(L_, offset_) #else // NDEBUG diff --git a/src/state.cpp b/src/state.cpp index d6dfb89..e71865d 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -52,21 +52,21 @@ THE SOFTWARE. [[nodiscard]] static int luaG_new_require(lua_State* L_) { int rc; - int const args = lua_gettop( L_); // args - Universe* U = universe_get( L_); - //char const* modname = luaL_checkstring( L, 1); + int const args = lua_gettop(L_); // args + Universe* U = universe_get(L_); + //char const* modname = luaL_checkstring(L_, 1); - STACK_GROW( L_, 1); + STACK_GROW(L_, 1); - lua_pushvalue( L_, lua_upvalueindex( 1)); // args require - lua_insert( L_, 1); // require args + lua_pushvalue(L_, lua_upvalueindex( 1)); // args require + lua_insert(L_, 1); // require args // Using 'lua_pcall()' to catch errors; otherwise a failing 'require' would // leave us locked, blocking any future 'require' calls from other lanes. U->require_cs.lock(); // starting with Lua 5.4, require may return a second optional value, so we need LUA_MULTRET - rc = lua_pcall( L_, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) + rc = lua_pcall(L_, args, LUA_MULTRET, 0 /*errfunc*/ ); // err|result(s) U->require_cs.unlock(); // the required module (or an error message) is left on the stack as returned value by original require function @@ -76,7 +76,7 @@ THE SOFTWARE. raise_lua_error(L_); } // should be 1 for Lua <= 5.3, 1 or 2 starting with Lua 5.4 - return lua_gettop(L_); // result(s) + return lua_gettop(L_); // result(s) } // ################################################################################################# diff --git a/src/tools.cpp b/src/tools.cpp index e11cad5..7b2d153 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -305,7 +305,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L_ // based on some sorting order so that we end up with the same name in all databases whatever order the table walk yielded if (prevName != nullptr && (prevNameLength < newNameLength || lua_lessthan(L_, -2, -1))) { - DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "%s '%s' remained named '%s'\n" INDENT_END, lua_typename( L, lua_type( L, -3)), newName, prevName)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s' remained named '%s'\n" INDENT_END, lua_typename(L_, lua_type(L_, -3)), newName, prevName)); // the previous name is 'smaller' than the one we just generated: keep it! lua_pop(L_, 3); // ... {bfc} k } @@ -324,7 +324,7 @@ static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L_ { lua_remove(L_, -2); // ... {bfc} k o "f.q.n" } - DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%s '%s'\n" INDENT_END, lua_typename(L, lua_type( L, -2)), newName)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s '%s'\n" INDENT_END, lua_typename(L_, lua_type(L_, -2)), newName)); // prepare the stack for database feed lua_pushvalue(L_, -1); // ... {bfc} k o "f.q.n" "f.q.n" lua_pushvalue(L_, -3); // ... {bfc} k o "f.q.n" "f.q.n" o @@ -428,7 +428,7 @@ static void populate_func_lookup_table_recur(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_pushnil(L_); // ... {_i} {bfc} nil while (lua_next(L_, breadth_first_cache) != 0) // ... {_i} {bfc} k {} { - DEBUGSPEW_CODE(char const* key = (lua_type(L, -2) == LUA_TSTRING) ? lua_tostring(L, -2) : "not a string"); + DEBUGSPEW_CODE(char const* key = (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostring(L_, -2) : "not a string"); DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "table '%s'\n" INDENT_END, key)); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); // un-visit this table in case we do need to process it @@ -474,8 +474,8 @@ void populate_func_lookup_table(lua_State* L_, int i_, char const* name_) int const ctx_base = lua_gettop(L_) + 1; int const in_base = lua_absindex(L_, i_); int start_depth = 0; - DEBUGSPEW_CODE(Universe* U = universe_get(L)); - DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L, name_ ? name_ : "nullptr")); + DEBUGSPEW_CODE(Universe* U = universe_get(L_)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: populate_func_lookup_table('%s')\n" INDENT_END, L_, name_ ? name_ : "nullptr")); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); STACK_GROW(L_, 3); STACK_CHECK_START_REL(L_, 0); @@ -1723,7 +1723,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const return true; } - /* Check if we've already copied the same table from 'L' (during this transmission), and + /* Check if we've already copied the same table from 'L1' (during this transmission), and * reuse the old copy. This allows table upvalues shared by multiple * local functions to point to the same table, also in the target. * Also, this takes care of cyclic tables and multiple references -- cgit v1.2.3-55-g6feb