From 2e61dc33af885974a2a3a3f8a504061abe91bd71 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 13 May 2024 16:26:02 +0200 Subject: Progressively applying the coding rules --- src/cancel.cpp | 70 ++++----- src/compat.cpp | 12 +- src/deep.cpp | 64 ++++---- src/intercopycontext.cpp | 399 +++++++++++++++++++++++------------------------ src/keeper.cpp | 328 +++++++++++++++++++------------------- src/macros_and_utils.h | 10 +- 6 files changed, 442 insertions(+), 441 deletions(-) (limited to 'src') diff --git a/src/cancel.cpp b/src/cancel.cpp index 8356169..b297c85 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -51,9 +51,9 @@ THE SOFTWARE. */ [[nodiscard]] static inline CancelRequest cancel_test(lua_State* L_) { - Lane* const lane{ kLanePointerRegKey.readLightUserDataValue(L_) }; + Lane* const _lane{ kLanePointerRegKey.readLightUserDataValue(L_) }; // 'lane' is nullptr for the original main state (and no-one can cancel that) - return lane ? lane->cancelRequest : CancelRequest::None; + return _lane ? _lane->cancelRequest : CancelRequest::None; } // ################################################################################################# @@ -66,8 +66,8 @@ THE SOFTWARE. // LUAG_FUNC(cancel_test) { - CancelRequest test{ cancel_test(L_) }; - lua_pushboolean(L_, test != CancelRequest::None); + CancelRequest _test{ cancel_test(L_) }; + lua_pushboolean(L_, _test != CancelRequest::None); return 1; } @@ -110,9 +110,9 @@ LUAG_FUNC(cancel_test) lane_->cancelRequest = 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 (wakeLane_) { // wake the thread so that execution returns from any pending linda operation if desired - std::condition_variable* const waiting_on{ lane_->waiting_on }; - if (lane_->status == Lane::Waiting && waiting_on != nullptr) { - waiting_on->notify_all(); + std::condition_variable* const _waiting_on{ lane_->waiting_on }; + if (lane_->status == Lane::Waiting && _waiting_on != nullptr) { + _waiting_on->notify_all(); } } @@ -126,9 +126,9 @@ LUAG_FUNC(cancel_test) lane_->cancelRequest = CancelRequest::Hard; // it's now signaled to stop // lane_->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_->waiting_on; - if (lane_->status == Lane::Waiting && waiting_on != nullptr) { - waiting_on->notify_all(); + std::condition_variable* const _waiting_on{ lane_->waiting_on }; + if (lane_->status == Lane::Waiting && _waiting_on != nullptr) { + _waiting_on->notify_all(); } } @@ -163,21 +163,21 @@ CancelResult thread_cancel(Lane* lane_, CancelOp op_, int hookCount_, std::chron CancelOp which_cancel_op(char const* opString_) { - CancelOp op{ CancelOp::Invalid }; + CancelOp _op{ CancelOp::Invalid }; if (strcmp(opString_, "hard") == 0) { - op = CancelOp::Hard; + _op = CancelOp::Hard; } else if (strcmp(opString_, "soft") == 0) { - op = CancelOp::Soft; + _op = CancelOp::Soft; } else if (strcmp(opString_, "call") == 0) { - op = CancelOp::MaskCall; + _op = CancelOp::MaskCall; } else if (strcmp(opString_, "ret") == 0) { - op = CancelOp::MaskRet; + _op = CancelOp::MaskRet; } else if (strcmp(opString_, "line") == 0) { - op = CancelOp::MaskLine; + _op = CancelOp::MaskLine; } else if (strcmp(opString_, "count") == 0) { - op = CancelOp::MaskCount; + _op = CancelOp::MaskCount; } - return op; + return _op; } // ################################################################################################# @@ -185,13 +185,13 @@ CancelOp which_cancel_op(char const* opString_) [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) { if (lua_type(L_, idx_) == LUA_TSTRING) { - char const* const str{ lua_tostring(L_, idx_) }; - CancelOp op{ which_cancel_op(str) }; + 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) { - raise_luaL_error(L_, "invalid hook option %s", str); + if (_op == CancelOp::Invalid) { + raise_luaL_error(L_, "invalid hook option %s", _str); } - return op; + return _op; } return CancelOp::Hard; } @@ -201,23 +201,23 @@ CancelOp which_cancel_op(char const* opString_) // bool[,reason] = lane_h:cancel( [mode, hookcount] [, timeout] [, wake_lane]) LUAG_FUNC(thread_cancel) { - Lane* const lane{ ToLane(L_, 1) }; - CancelOp const op{ which_cancel_op(L_, 2) }; // this removes the op string from the stack + Lane* const _lane{ ToLane(L_, 1) }; + CancelOp const _op{ which_cancel_op(L_, 2) }; // this removes the op string from the stack - int hook_count{ 0 }; - if (static_cast(op) > static_cast(CancelOp::Soft)) { // hook is requested - hook_count = static_cast(luaL_checkinteger(L_, 2)); + int _hook_count{ 0 }; + if (static_cast(_op) > static_cast(CancelOp::Soft)) { // hook is requested + _hook_count = static_cast(luaL_checkinteger(L_, 2)); lua_remove(L_, 2); // argument is processed, remove it - if (hook_count < 1) { + if (_hook_count < 1) { raise_luaL_error(L_, "hook count cannot be < 1"); } } - std::chrono::time_point until{ std::chrono::time_point::max() }; + std::chrono::time_point _until{ std::chrono::time_point::max() }; if (lua_type(L_, 2) == LUA_TNUMBER) { // we don't want to use lua_isnumber() because of autocoercion lua_Duration const duration{ lua_tonumber(L_, 2) }; if (duration.count() >= 0.0) { - until = std::chrono::steady_clock::now() + std::chrono::duration_cast(duration); + _until = std::chrono::steady_clock::now() + std::chrono::duration_cast(duration); } else { raise_luaL_argerror(L_, 2, "duration cannot be < 0"); } @@ -227,16 +227,16 @@ LUAG_FUNC(thread_cancel) } // we wake by default in "hard" mode (remember that hook is hard too), but this can be turned off if desired - bool wake_lane{ op != CancelOp::Soft }; + bool _wake_lane{ _op != CancelOp::Soft }; if (lua_gettop(L_) >= 2) { if (!lua_isboolean(L_, 2)) { raise_luaL_error(L_, "wake_lindas parameter is not a boolean"); } - wake_lane = lua_toboolean(L_, 2); + _wake_lane = lua_toboolean(L_, 2); lua_remove(L_, 2); // argument is processed, remove it } STACK_CHECK_START_REL(L_, 0); - switch (thread_cancel(lane, op, hook_count, until, wake_lane)) { + switch (thread_cancel(_lane, _op, _hook_count, _until, _wake_lane)) { default: // should never happen unless we added a case and forgot to handle it LUA_ASSERT(L_, false); break; @@ -248,7 +248,7 @@ LUAG_FUNC(thread_cancel) case CancelResult::Cancelled: lua_pushboolean(L_, 1); // true - lane->pushThreadStatus(L_); // true status + _lane->pushThreadStatus(L_); // true status break; } STACK_CHECK(L_, 2); diff --git a/src/compat.cpp b/src/compat.cpp index 4e8025e..b45cce2 100644 --- a/src/compat.cpp +++ b/src/compat.cpp @@ -14,15 +14,15 @@ LuaType luaG_getmodule(lua_State* L_, char const* name_) { STACK_CHECK_START_REL(L_, 0); - LuaType type{ static_cast(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil - if (type != LuaType::TABLE) { // L_: _R._LOADED|nil + LuaType _type{ static_cast(lua503_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE)) };// L_: _R._LOADED|nil + if (_type != LuaType::TABLE) { // L_: _R._LOADED|nil STACK_CHECK(L_, 1); - return type; + return _type; } - type = static_cast(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil - lua_remove(L_, -2); // L_: {module}|nil + _type = static_cast(lua503_getfield(L_, -1, name_)); // L_: _R._LOADED {module}|nil + lua_remove(L_, -2); // L_: {module}|nil STACK_CHECK(L_, 1); - return type; + return _type; } // ################################################################################################# diff --git a/src/deep.cpp b/src/deep.cpp index e0c2a39..9474666 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -59,6 +59,8 @@ static constexpr RegistryUniqueKey kDeepLookupRegKey{ 0xC6788345703C6059ull }; */ static constexpr RegistryUniqueKey kDeepProxyCacheRegKey{ 0xEBCD49AE1A3DD35Eull }; +// ################################################################################################# + /* * Sets up [-1]<->[-2] two-way lookups, and ensures the lookup table exists. * Pops the both values off the stack. @@ -106,9 +108,9 @@ static void LookupDeep(lua_State* L_) { // 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->factory; + return &_proxy->factory; } else { // essentially we are making sure that the metatable of the object we want to copy is stored in our metatable/factory database // it is the only way to ensure that the userdata is indeed a deep userdata! @@ -123,10 +125,10 @@ static void LookupDeep(lua_State* L_) // replace metatable with the factory pointer, if it is actually a deep userdata LookupDeep(L_); // L_: deep ... factory|nil - DeepFactory* const ret{ lua_tolightuserdata(L_, -1) }; // nullptr if not a userdata + DeepFactory* const _ret{ lua_tolightuserdata(L_, -1) }; // nullptr if not a userdata lua_pop(L_, 1); STACK_CHECK(L_, 0); - return ret; + return _ret; } } @@ -149,12 +151,12 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) */ [[nodiscard]] static int deep_userdata_gc(lua_State* L_) { - DeepPrelude* const* const proxy{ lua_tofulluserdata(L_, 1) }; - DeepPrelude* const p{ *proxy }; + DeepPrelude* const* const _proxy{ lua_tofulluserdata(L_, 1) }; + DeepPrelude* const _p{ *_proxy }; // can work without a universe if creating a deep userdata from some external C module when Lanes isn't loaded // in that case, we are not multithreaded and locking isn't necessary anyway - bool const isLastRef{ p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; + bool const isLastRef{ _p->refcount.fetch_sub(1, std::memory_order_relaxed) == 1 }; if (isLastRef) { // retrieve wrapped __gc @@ -166,7 +168,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_) // need an empty stack in case we are GC_ing from a Keeper, so that empty stack checks aren't triggered lua_pop(L_, 2); // L_: } - DeepFactory::DeleteDeepObject(L_, p); + DeepFactory::DeleteDeepObject(L_, _p); } return 0; } @@ -219,9 +221,9 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int 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_) }; // L_: DPC proxy - LUA_ASSERT(L_, proxy); - *proxy = prelude_; + DeepPrelude** const _proxy{ lua_newuserdatauv(L_, nuv_) }; // L_: DPC proxy + LUA_ASSERT(L_, _proxy); + *_proxy = prelude_; prelude_->refcount.fetch_add(1, std::memory_order_relaxed); // one more proxy pointing to this deep data // Get/create metatable for 'factory' (in this state) @@ -231,13 +233,13 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int if (lua_isnil(L_, -1)) { // No metatable yet. lua_pop(L_, 1); // L_: DPC proxy - int const oldtop{ lua_gettop(L_) }; + int const _oldtop{ lua_gettop(L_) }; // 1 - make one and register it if (mode_ != LookupMode::ToKeeper) { factory.createMetatable(L_); // L_: DPC proxy metatable - if (lua_gettop(L_) - oldtop != 1 || !lua_istable(L_, -1)) { + 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); // L_: DPC proxy X + lua_settop(L_, _oldtop); // L_: DPC proxy X lua_pop(L_, 3); // L_: return "Bad DeepFactory::createMetatable overload: unexpected pushed value"; } @@ -262,12 +264,12 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int factory.storeDeepLookup(L_); // 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 + 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() // check that the module is already loaded (or being loaded, we are happy either way) if (lua_isfunction(L_, -1)) { - lua_pushstring(L_, modname); // L_: DPC proxy metatable require() "module" + lua_pushstring(L_, _modname); // L_: DPC proxy metatable require() "module" lua_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE); // L_: DPC proxy metatable require() "module" _R._LOADED if (lua_istable(L_, -1)) { lua_pushvalue(L_, -2); // L_: DPC proxy metatable require() "module" _R._LOADED "module" @@ -280,7 +282,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int require_result = lua_pcall(L_, 1, 0, 0); // L_: 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_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", _modname); lua_insert(L_, -2); // L_: DPC proxy metatable prefix error lua_concat(L_, 2); // L_: DPC proxy metatable error return lua_tostring(L_, -1); @@ -334,30 +336,30 @@ 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_) }; - if (prelude == nullptr) { + 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)"); } - if (prelude->magic != kDeepVersion) { + if (_prelude->magic != kDeepVersion) { // just in case, don't leak the newly allocated deep userdata object - deleteDeepObjectInternal(L_, prelude); + 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->refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 - LUA_ASSERT(L_, &prelude->factory == this); + LUA_ASSERT(L_, _prelude->refcount.load(std::memory_order_relaxed) == 0); // 'DeepFactory::PushDeepProxy' will lift it to 1 + LUA_ASSERT(L_, &_prelude->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); + 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 - if (errmsg != nullptr) { - raise_luaL_error(L_, errmsg); + char const* const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy + if (_err != nullptr) { + raise_luaL_error(L_, _err); } STACK_CHECK(L_, 1); return 1; @@ -380,6 +382,6 @@ DeepPrelude* DeepFactory::toDeep(lua_State* L_, int index_) const } STACK_CHECK(L_, 0); - DeepPrelude** const proxy{ lua_tofulluserdata(L_, index_) }; - return *proxy; + DeepPrelude** const _proxy{ lua_tofulluserdata(L_, index_) }; + return *_proxy; } diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 07fcd77..10d620e 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -37,11 +37,11 @@ THE SOFTWARE. // luckily, this also works with earlier Lua versions [[nodiscard]] static int buf_writer(lua_State* L_, void const* b_, size_t size_, void* ud_) { - luaL_Buffer* const B{ static_cast(ud_) }; - if (!B->L) { - luaL_buffinit(L_, B); + luaL_Buffer* const _B{ static_cast(ud_) }; + if (!_B->L) { + luaL_buffinit(L_, _B); } - luaL_addlstring(B, static_cast(b_), size_); + luaL_addlstring(_B, static_cast(b_), size_); return 0; } @@ -78,12 +78,12 @@ THE SOFTWARE. STACK_CHECK_START_REL(L_, 0); STACK_GROW(L_, 3); // up to 3 slots are necessary on error if (mode_ == LookupMode::FromKeeper) { - lua_CFunction f = lua_tocfunction(L_, i_); // should *always* be one of the function sentinels - if (f == func_lookup_sentinel || f == table_lookup_sentinel || f == userdata_clone_sentinel) { + lua_CFunction const _f{ lua_tocfunction(L_, i_) }; // should *always* be one of the function sentinels + if (_f == func_lookup_sentinel || _f == table_lookup_sentinel || _f == userdata_clone_sentinel) { lua_getupvalue(L_, i_, 1); // L_: ... v ... "f.q.n" } else { // if this is not a sentinel, this is some user-created table we wanted to lookup - LUA_ASSERT(L_, nullptr == f && lua_istable(L_, i_)); + LUA_ASSERT(L_, nullptr == _f && lua_istable(L_, i_)); // push anything that will convert to nullptr string lua_pushnil(L_); // L_: ... v ... nil } @@ -95,13 +95,13 @@ THE SOFTWARE. lua_pushvalue(L_, i_); // L_: ... v ... {} v lua_rawget(L_, -2); // L_: ... v ... {} "f.q.n" } - char const* fqn{ lua_tolstring(L_, -1, len_) }; + char const* _fqn{ lua_tolstring(L_, -1, len_) }; DEBUGSPEW_CODE(Universe* const U = universe_get(L_)); - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), fqn)); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "function [C] %s \n" INDENT_END(U), _fqn)); // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database lua_pop(L_, (mode_ == LookupMode::FromKeeper) ? 1 : 2); // L_: ... v ... STACK_CHECK(L_, 0); - if (nullptr == fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) + if (nullptr == _fqn && !lua_istable(L_, i_)) { // raise an error if we try to send an unknown function (but not for tables) *len_ = 0; // just in case // try to discover the name of the function we want to send lua_getglobal(L_, "decoda_name"); // L_: ... v ... decoda_name @@ -125,7 +125,7 @@ THE SOFTWARE. raise_luaL_error(L_, "%s%s '%s' not found in %s origin transfer database.%s", typewhat, gotchaA, what, from ? from : "main", gotchaB); } STACK_CHECK(L_, 0); - return fqn; + return _fqn; } // ################################################################################################# @@ -147,26 +147,26 @@ static constexpr RegistryUniqueKey kMtIdRegKey{ 0xA8895DCF4EC3FE3Cull }; lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} lua_rawget(L_, -2); // L_: ... _R[kMtIdRegKey] mtk? - lua_Integer id{ lua_tointeger(L_, -1) }; // 0 for nil + lua_Integer _id{ lua_tointeger(L_, -1) }; // 0 for nil lua_pop(L_, 1); // L_: ... _R[kMtIdRegKey] STACK_CHECK(L_, 1); - if (id == 0) { - id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); + if (_id == 0) { + _id = U_->nextMetatableId.fetch_add(1, std::memory_order_relaxed); // Create two-way references: id_uint <-> table lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] {mt} - lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] {mt} id + lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] {mt} id lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] - lua_pushinteger(L_, id); // L_: ... _R[kMtIdRegKey] id + lua_pushinteger(L_, _id); // L_: ... _R[kMtIdRegKey] id lua_pushvalue(L_, idx_); // L_: ... _R[kMtIdRegKey] id {mt} lua_rawset(L_, -3); // L_: ... _R[kMtIdRegKey] } lua_pop(L_, 1); // L_: ... STACK_CHECK(L_, 0); - return id; + return _id; } // ################################################################################################# @@ -181,8 +181,8 @@ void InterCopyContext::copy_func() const // 'lua_dump()' needs the function at top of stack // if already on top of the stack, no need to push again - bool const needToPush{ L1_i != lua_gettop(L1) }; - if (needToPush) { + bool const _needToPush{ L1_i != lua_gettop(L1) }; + if (_needToPush) { lua_pushvalue(L1, L1_i); // L1: ... f } @@ -191,8 +191,7 @@ void InterCopyContext::copy_func() const // to the writer" (and we only return 0) // not sure this could ever fail but for memory shortage reasons // last parameter is Lua 5.4-specific (no stripping) - luaL_Buffer B; - B.L = nullptr; + luaL_Buffer B{}; if (lua504_dump(L1, buf_writer, &B, 0) != 0) { raise_luaL_error(getErrL(), "internal error: function dump failed."); } @@ -201,7 +200,7 @@ void InterCopyContext::copy_func() const luaL_pushresult(&B); // L1: ... f b // if not pushed, no need to pop - if (needToPush) { + if (_needToPush) { lua_remove(L1, -2); // L1: ... b } @@ -214,18 +213,18 @@ void InterCopyContext::copy_func() const // stack and start the what string with the character '>'." // { - lua_Debug ar; + lua_Debug _ar; lua_pushvalue(L1, L1_i); // L1: ... b f // fills 'fname' 'namewhat' and 'linedefined', pops function - lua_getinfo(L1, ">nS", &ar); // L1: ... b - fname = ar.namewhat; - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), ar.short_src, ar.linedefined)); // just gives nullptr + lua_getinfo(L1, ">nS", &_ar); // L1: ... b + fname = _ar.namewhat; + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "FNAME: %s @ %d" INDENT_END(U), _ar.short_src, _ar.linedefined)); // just gives nullptr } #endif // LOG_FUNC_INFO { - size_t sz; - char const* s = lua_tolstring(L1, -1, &sz); // L1: ... b - LUA_ASSERT(L1, s && sz); + size_t _sz; + char const* _s{ lua_tolstring(L1, -1, &_sz) }; // L1: ... b + LUA_ASSERT(L1, _s && _sz); STACK_GROW(L2, 2); // Note: Line numbers seem to be taken precisely from the // original function. 'fname' is not used since the chunk @@ -233,7 +232,7 @@ void InterCopyContext::copy_func() const // // TBD: Can we get the function's original name through, as well? // - if (luaL_loadbuffer(L2, s, sz, fname) != 0) { // L2: ... {cache} ... p function + if (luaL_loadbuffer(L2, _s, _sz, fname) != 0) { // L2: ... {cache} ... p function // chunk is precompiled so only LUA_ERRMEM can happen // "Otherwise, it pushes an error message" // @@ -259,15 +258,15 @@ void InterCopyContext::copy_func() const */ int n{ 0 }; { - InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; + InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, {} }; #if LUA_VERSION_NUM >= 502 // Starting with Lua 5.2, each Lua function gets its environment as one of its upvalues (named LUA_ENV, aka "_ENV" by default) // Generally this is LUA_RIDX_GLOBALS, which we don't want to copy from the source to the destination state... // -> if we encounter an upvalue equal to the global table in the source, bind it to the destination's global table lua_pushglobaltable(L1); // L1: ... _G #endif // LUA_VERSION_NUM - for (n = 0; (c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, c.name)); + for (n = 0; (_c.name = lua_getupvalue(L1, L1_i, 1 + n)) != nullptr; ++n) { // L1: ... _G up[n] + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "UPNAME[%d]: %s -> " INDENT_END(U), n, _c.name)); #if LUA_VERSION_NUM >= 502 if (lua_rawequal(L1, -1, -2)) { // is the upvalue equal to the global table? DEBUGSPEW_CODE(fprintf(stderr, "pushing destination global scope\n")); @@ -276,8 +275,8 @@ void InterCopyContext::copy_func() const #endif // LUA_VERSION_NUM { DEBUGSPEW_CODE(fprintf(stderr, "copying value\n")); - c.L1_i = SourceIndex{ lua_gettop(L1) }; - if (!c.inter_copy_one()) { // L2: ... {cache} ... function + _c.L1_i = SourceIndex{ lua_gettop(L1) }; + if (!_c.inter_copy_one()) { // L2: ... {cache} ... function raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } } @@ -292,13 +291,13 @@ void InterCopyContext::copy_func() const STACK_CHECK(L1, 0); // Set upvalues (originally set to 'nil' by 'lua_load') - for (int const func_index{ lua_gettop(L2) - n }; n > 0; --n) { - char const* rc{ lua_setupvalue(L2, func_index, n) }; // L2: ... {cache} ... function + for (int const _func_index{ lua_gettop(L2) - n }; n > 0; --n) { + char const* _rc{ lua_setupvalue(L2, _func_index, n) }; // L2: ... {cache} ... function // // "assigns the value at the top of the stack to the upvalue and returns its name. // It also pops the value from the stack." - LUA_ASSERT(L1, rc); // not having enough slots? + LUA_ASSERT(L1, _rc); // not having enough slots? } // once all upvalues have been set we are left // with the function at the top of the stack // L2: ... {cache} ... function @@ -312,8 +311,8 @@ void InterCopyContext::copy_func() const void InterCopyContext::lookup_native_func() const { // get the name of the function we want to send - size_t len; - char const* const fqn{ find_lookup_name(L1, L1_i, mode, name, &len) }; + size_t _len; + char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; // push the equivalent function in the destination's stack, retrieved from the lookup table STACK_CHECK_START_REL(L2, 0); STACK_GROW(L2, 3); // up to 3 slots are necessary on error @@ -324,7 +323,7 @@ void InterCopyContext::lookup_native_func() const case LookupMode::ToKeeper: // push a sentinel closure that holds the lookup name as upvalue - lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: "f.q.n" + lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: "f.q.n" lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f break; @@ -333,25 +332,25 @@ void InterCopyContext::lookup_native_func() const kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); - lua_pushlstring(L2, fqn, len); // L1: ... f ... L2: {} "f.q.n" + lua_pushlstring(L2, _fqn, _len); // L1: ... f ... L2: {} "f.q.n" lua_rawget(L2, -2); // L1: ... f ... L2: {} f // 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 - char const* const from{ lua_tostring(L1, -1) }; + 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 - char const* const to{ lua_tostring(L2, -1) }; + 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 raise_luaL_error( getErrL(), "%s%s: function '%s' not found in %s destination transfer database.", lua_isnil(L2, -1) ? "" : "INTERNAL ERROR IN ", - from ? from : "main", - fqn, - to ? to : "main"); + _from ? _from : "main", + _fqn, + _to ? _to : "main"); return; } lua_remove(L2, -2); // L2: f @@ -381,10 +380,10 @@ void InterCopyContext::lookup_native_func() const // Always pushes a function to 'L2'. void InterCopyContext::copy_cached_func() const { - FuncSubType const funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; - if (funcSubType == FuncSubType::Bytecode) { - void* const aspointer = const_cast(lua_topointer(L1, L1_i)); - // TBD: Merge this and same code for tables + FuncSubType const _funcSubType{ luaG_getfuncsubtype(L1, L1_i) }; + if (_funcSubType == FuncSubType::Bytecode) { + void* const _aspointer{ const_cast(lua_topointer(L1, L1_i)) }; + // TODO: Merge this and same code for tables LUA_ASSERT(L1, L2_cache_i != 0); STACK_GROW(L2, 2); @@ -397,7 +396,7 @@ void InterCopyContext::copy_cached_func() const // is only for the duration of a copy (both states are locked). // push a light userdata uniquely representing the function - lua_pushlightuserdata(L2, aspointer); // L2: ... {cache} ... p + lua_pushlightuserdata(L2, _aspointer); // L2: ... {cache} ... p // fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); @@ -430,9 +429,9 @@ void InterCopyContext::copy_cached_func() const [[nodiscard]] bool InterCopyContext::lookup_table() const { // get the name of the table we want to send - size_t len; - char const* fqn = find_lookup_name(L1, L1_i, mode, name, &len); - if (nullptr == fqn) { // name not found, it is some user-created table + size_t _len; + char const* const _fqn{ find_lookup_name(L1, L1_i, mode, name, &_len) }; + if (nullptr == _fqn) { // name not found, it is some user-created table return false; } // push the equivalent table in the destination's stack, retrieved from the lookup table @@ -445,7 +444,7 @@ void InterCopyContext::copy_cached_func() const case LookupMode::ToKeeper: // push a sentinel closure that holds the lookup name as upvalue - lua_pushlstring(L2, fqn, len); // L1: ... t ... L2: "f.q.n" + lua_pushlstring(L2, _fqn, _len); // L1: ... t ... L2: "f.q.n" lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f break; @@ -454,7 +453,7 @@ void InterCopyContext::copy_cached_func() const kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); - lua_pushlstring(L2, fqn, len); // L2: {} "f.q.n" + lua_pushlstring(L2, _fqn, _len); // L2: {} "f.q.n" lua_rawget(L2, -2); // L2: {} t // we accept destination lookup failures in the case of transfering the Lanes body function (this will result in the source table being cloned instead) // but not when we extract something out of a keeper, as there is nothing to clone! @@ -473,7 +472,7 @@ void InterCopyContext::copy_cached_func() const getErrL(), "%s: source table '%s' found as %s in %s destination transfer database.", from ? from : "main", - fqn, + _fqn, lua_typename(L2, lua_type_as_enum(L2, -1)), to ? to : "main"); } @@ -488,12 +487,12 @@ void InterCopyContext::copy_cached_func() const void InterCopyContext::inter_copy_keyvaluepair() const { - SourceIndex const val_i{ lua_gettop(L1) }; - SourceIndex const key_i{ val_i - 1 }; + SourceIndex const _val_i{ lua_gettop(L1) }; + SourceIndex const _key_i{ _val_i - 1 }; // For the key, only basic key types are copied over. others ignored - InterCopyContext c{ U, L2, L1, L2_cache_i, key_i, VT::KEY, mode, name }; - if (!c.inter_copy_one()) { + InterCopyContext _c{ U, L2, L1, L2_cache_i, _key_i, VT::KEY, mode, name }; + if (!_c.inter_copy_one()) { return; // we could raise an error instead of ignoring the table entry, like so: // raise_luaL_error(L1, "Unable to copy %s key '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", name, luaL_typename(L1, key_i)); @@ -503,44 +502,44 @@ void InterCopyContext::inter_copy_keyvaluepair() const char* valPath{ nullptr }; if (U->verboseErrors) { // for debug purposes, let's try to build a useful name - if (lua_type(L1, key_i) == LUA_TSTRING) { - char const* key{ lua_tostring(L1, key_i) }; - size_t const keyRawLen = lua_rawlen(L1, key_i); + if (lua_type(L1, _key_i) == LUA_TSTRING) { + char const* key{ lua_tostring(L1, _key_i) }; + size_t const keyRawLen = lua_rawlen(L1, _key_i); size_t const bufLen = strlen(name) + keyRawLen + 2; valPath = (char*) alloca(bufLen); sprintf(valPath, "%s.%*s", name, (int) keyRawLen, key); key = nullptr; } #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 - else if (lua_isinteger(L1, key_i)) { - lua_Integer const key{ lua_tointeger(L1, key_i) }; + else if (lua_isinteger(L1, _key_i)) { + lua_Integer const key{ lua_tointeger(L1, _key_i) }; valPath = (char*) alloca(strlen(name) + 32 + 3); sprintf(valPath, "%s[" LUA_INTEGER_FMT "]", name, key); } #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 - else if (lua_type(L1, key_i) == LUA_TNUMBER) { - lua_Number const key{ lua_tonumber(L1, key_i) }; + else if (lua_type(L1, _key_i) == LUA_TNUMBER) { + lua_Number const key{ lua_tonumber(L1, _key_i) }; valPath = (char*) alloca(strlen(name) + 32 + 3); sprintf(valPath, "%s[" LUA_NUMBER_FMT "]", name, key); - } else if (lua_type(L1, key_i) == LUA_TLIGHTUSERDATA) { - void* const key{ lua_touserdata(L1, key_i) }; + } else if (lua_type(L1, _key_i) == LUA_TLIGHTUSERDATA) { + void* const key{ lua_touserdata(L1, _key_i) }; valPath = (char*) alloca(strlen(name) + 16 + 5); sprintf(valPath, "%s[U:%p]", name, key); - } else if (lua_type(L1, key_i) == LUA_TBOOLEAN) { - int const key{ lua_toboolean(L1, key_i) }; + } else if (lua_type(L1, _key_i) == LUA_TBOOLEAN) { + int const key{ lua_toboolean(L1, _key_i) }; valPath = (char*) alloca(strlen(name) + 8); sprintf(valPath, "%s[%s]", name, key ? "true" : "false"); } } - c.L1_i = SourceIndex{ val_i }; + _c.L1_i = SourceIndex{ _val_i }; // Contents of metatables are copied with cache checking. important to detect loops. - c.vt = VT::NORMAL; - c.name = valPath ? valPath : name; - if (c.inter_copy_one()) { + _c.vt = VT::NORMAL; + _c.name = valPath ? valPath : name; + if (_c.inter_copy_one()) { LUA_ASSERT(L1, lua_istable(L2, -3)); lua_rawset(L2, -3); // add to table (pops key & val) } else { - raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, val_i)); + raise_luaL_error(getErrL(), "Unable to copy %s entry '%s' because of value is of type '%s'", (vt == VT::NORMAL) ? "table" : "metatable", valPath, luaL_typename(L1, _val_i)); } } @@ -555,13 +554,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const } STACK_CHECK(L1, 1); - lua_Integer const mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable + lua_Integer const _mt_id{ get_mt_id(U, L1, -1) }; // Unique id for the metatable STACK_CHECK_START_REL(L2, 0); STACK_GROW(L2, 4); // do we already know this metatable? std::ignore = kMtIdRegKey.getSubTable(L2, 0, 0); // L2: _R[kMtIdRegKey] - lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] id + lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] id lua_rawget(L2, -2); // L2: _R[kMtIdRegKey] mt|nil STACK_CHECK(L2, 2); @@ -574,13 +573,13 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK(L2, 2); // L2: _R[kMtIdRegKey] mt // mt_id -> metatable - lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt id + lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt id lua_pushvalue(L2, -2); // L2: _R[kMtIdRegKey] mt id mt lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt // metatable -> mt_id lua_pushvalue(L2, -1); // L2: _R[kMtIdRegKey] mt mt - lua_pushinteger(L2, mt_id); // L2: _R[kMtIdRegKey] mt mt id + lua_pushinteger(L2, _mt_id); // L2: _R[kMtIdRegKey] mt mt id lua_rawset(L2, -4); // L2: _R[kMtIdRegKey] mt STACK_CHECK(L2, 2); } @@ -600,7 +599,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const // Returns true if the table was cached (no need to fill it!); false if it's a virgin. [[nodiscard]] bool InterCopyContext::push_cached_table() const { - void const* p{ lua_topointer(L1, L1_i) }; + void const* const _p{ lua_topointer(L1, L1_i) }; LUA_ASSERT(L1, L2_cache_i != 0); STACK_GROW(L2, 3); @@ -609,37 +608,37 @@ void InterCopyContext::inter_copy_keyvaluepair() const // We don't need to use the from state ('L1') in ID since the life span // is only for the duration of a copy (both states are locked). // push a light userdata uniquely representing the table - lua_pushlightuserdata(L2, const_cast(p)); // L1: ... t ... L2: ... p + lua_pushlightuserdata(L2, const_cast(_p)); // L1: ... t ... L2: ... p // fprintf(stderr, "<< ID: %s >>\n", lua_tostring(L2, -1)); lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} - bool const not_found_in_cache{ lua_isnil(L2, -1) }; - if (not_found_in_cache) { + bool const _not_found_in_cache{ lua_isnil(L2, -1) }; + if (_not_found_in_cache) { // create a new entry in the cache lua_pop(L2, 1); // L1: ... t ... L2: ... lua_newtable(L2); // L1: ... t ... L2: ... {} - lua_pushlightuserdata(L2, const_cast(p)); // L1: ... t ... L2: ... {} p + lua_pushlightuserdata(L2, const_cast(_p)); // L1: ... t ... L2: ... {} p lua_pushvalue(L2, -2); // L1: ... t ... L2: ... {} p {} lua_rawset(L2, L2_cache_i); // L1: ... t ... L2: ... {} } STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); - return !not_found_in_cache; + return !_not_found_in_cache; } // ################################################################################################# [[nodiscard]] bool InterCopyContext::tryCopyClonable() const { - SourceIndex const L1i{ lua_absindex(L1, L1_i) }; - void* const source{ lua_touserdata(L1, L1i) }; + SourceIndex const _L1_i{ lua_absindex(L1, L1_i) }; + void* const _source{ lua_touserdata(L1, _L1_i) }; STACK_CHECK_START_REL(L1, 0); STACK_CHECK_START_REL(L2, 0); // Check if the source was already cloned during this copy - lua_pushlightuserdata(L2, source); // L2: ... source + lua_pushlightuserdata(L2, _source); // L2: ... source lua_rawget(L2, L2_cache_i); // L2: ... clone? if (!lua_isnil(L2, -1)) { STACK_CHECK(L2, 1); @@ -650,7 +649,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK(L2, 0); // no metatable? -> not clonable - if (!lua_getmetatable(L1, L1i)) { // L1: ... mt? + if (!lua_getmetatable(L1, _L1_i)) { // L1: ... mt? STACK_CHECK(L1, 0); return false; } @@ -666,18 +665,18 @@ void InterCopyContext::inter_copy_keyvaluepair() const // we need to copy over the uservalues of the userdata as well { int const mt{ lua_absindex(L1, -2) }; // L1: ... mt __lanesclone - size_t const userdata_size{ lua_rawlen(L1, L1i) }; + size_t const userdata_size{ lua_rawlen(L1, _L1_i) }; // extract all the uservalues, but don't transfer them yet - int uvi = 0; - while (lua_getiuservalue(L1, L1i, ++uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil + int _uvi{ 0 }; + while (lua_getiuservalue(L1, _L1_i, ++_uvi) != LUA_TNONE) {} // L1: ... mt __lanesclone [uv]+ nil // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]+ - --uvi; + --_uvi; // create the clone userdata with the required number of uservalue slots - void* const clone{ lua_newuserdatauv(L2, userdata_size, uvi) }; // L2: ... u + void* const _clone{ lua_newuserdatauv(L2, userdata_size, _uvi) }; // L2: ... u // copy the metatable in the target state, and give it to the clone we put there - InterCopyContext c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; - if (c.inter_copy_one()) { // L2: ... u mt|sentinel + InterCopyContext _c{ U, L2, L1, L2_cache_i, SourceIndex{ mt }, VT::NORMAL, mode, name }; + if (_c.inter_copy_one()) { // L2: ... u mt|sentinel if (LookupMode::ToKeeper == mode) { // L2: ... u sentinel LUA_ASSERT(L1, lua_tocfunction(L2, -1) == table_lookup_sentinel); // we want to create a new closure with a 'clone sentinel' function, where the upvalues are the userdata and the metatable fqn @@ -694,7 +693,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const raise_luaL_error(getErrL(), "Error copying a metatable"); } // first, add the entry in the cache (at this point it is either the actual userdata or the keeper sentinel - lua_pushlightuserdata(L2, source); // L2: ... u source + lua_pushlightuserdata(L2, _source); // L2: ... u source lua_pushvalue(L2, -2); // L2: ... u source u lua_rawset(L2, L2_cache_i); // L2: ... u // make sure we have the userdata now @@ -702,15 +701,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const lua_getupvalue(L2, -1, 2); // L2: ... userdata_clone_sentinel u } // assign uservalues - while (uvi > 0) { - c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; - if (!c.inter_copy_one()) { // L2: ... u uv + while (_uvi > 0) { + _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; + if (!_c.inter_copy_one()) { // L2: ... u uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } lua_pop(L1, 1); // L1: ... mt __lanesclone [uv]* // this pops the value from the stack - lua_setiuservalue(L2, -2, uvi); // L2: ... u - --uvi; + lua_setiuservalue(L2, -2, _uvi); // L2: ... u + --_uvi; } // when we are done, all uservalues are popped from the source stack, and we want only the single transferred value in the destination if (LookupMode::ToKeeper == mode) { // L2: ... userdata_clone_sentinel u @@ -719,8 +718,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK(L2, 1); STACK_CHECK(L1, 2); // call cloning function in source state to perform the actual memory cloning - lua_pushlightuserdata(L1, clone); // L1: ... mt __lanesclone clone - lua_pushlightuserdata(L1, source); // L1: ... mt __lanesclone clone source + lua_pushlightuserdata(L1, _clone); // L1: ... mt __lanesclone clone + lua_pushlightuserdata(L1, _source); // L1: ... mt __lanesclone clone source lua_pushinteger(L1, static_cast(userdata_size)); // L1: ... mt __lanesclone clone source size lua_call(L1, 3, 0); // L1: ... mt STACK_CHECK(L1, 1); @@ -738,8 +737,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const // Returns false if not a deep userdata, else true (unless an error occured) [[nodiscard]] bool InterCopyContext::tryCopyDeep() const { - DeepFactory* const factory{ LookupFactory(L1, L1_i, mode) }; - if (factory == nullptr) { + DeepFactory* const _factory{ LookupFactory(L1, L1_i, mode) }; + if (_factory == nullptr) { return false; // not a deep userdata } @@ -747,33 +746,33 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK_START_REL(L2, 0); // extract all uservalues of the source. unfortunately, the only way to know their count is to iterate until we fail - int nuv = 0; - while (lua_getiuservalue(L1, L1_i, nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil - ++nuv; + int _nuv = 0; + while (lua_getiuservalue(L1, L1_i, _nuv + 1) != LUA_TNONE) { // L1: ... u [uv]* nil + ++_nuv; } // last call returned TNONE and pushed nil, that we don't need lua_pop(L1, 1); // L1: ... u [uv]* - STACK_CHECK(L1, nuv); + STACK_CHECK(L1, _nuv); DeepPrelude* const u{ *lua_tofulluserdata(L1, L1_i) }; - char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, nuv, mode) }; // L1: ... u [uv]* L2: u + char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u if (errmsg != nullptr) { raise_luaL_error(getErrL(), errmsg); } // transfer all uservalues of the source in the destination { - InterCopyContext c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; - int const clone_i{ lua_gettop(L2) }; - while (nuv) { - c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; - if (!c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv + InterCopyContext _c{ U, L2, L1, L2_cache_i, {}, VT::NORMAL, mode, name }; + int const _clone_i{ lua_gettop(L2) }; + while (_nuv) { + _c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; + if (!_c.inter_copy_one()) { // L1: ... u [uv]* L2: u uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } lua_pop(L1, 1); // L1: ... u [uv]* // this pops the value from the stack - lua_setiuservalue(L2, clone_i, nuv); // L2: u - --nuv; + lua_setiuservalue(L2, _clone_i, _nuv); // L2: u + --_nuv; } } @@ -787,9 +786,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const [[nodiscard]] bool InterCopyContext::inter_copy_boolean() const { - int const v{ lua_toboolean(L1, L1_i) }; - DEBUGSPEW_CODE(fprintf(stderr, "%s\n", v ? "true" : "false")); - lua_pushboolean(L2, v); + int const _v{ lua_toboolean(L1, L1_i) }; + DEBUGSPEW_CODE(fprintf(stderr, "%s\n", _v ? "true" : "false")); + lua_pushboolean(L2, _v); return true; } @@ -810,8 +809,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const // let's see if we already restored this userdata lua_getupvalue(L1, L1_i, 2); // L1: ... u - void* source = lua_touserdata(L1, -1); - lua_pushlightuserdata(L2, source); // L2: ... source + void* _source{ lua_touserdata(L1, -1) }; + lua_pushlightuserdata(L2, _source); // L2: ... source lua_rawget(L2, L2_cache_i); // L2: ... u? if (!lua_isnil(L2, -1)) { lua_pop(L1, 1); // L1: ... @@ -829,22 +828,22 @@ void InterCopyContext::inter_copy_keyvaluepair() const } // 'L1_i' slot was the proxy closure, but from now on we operate onthe actual userdata we extracted from it SourceIndex const source_i{ lua_gettop(L1) }; - source = lua_touserdata(L1, -1); - void* clone{ nullptr }; + _source = lua_touserdata(L1, -1); + void* _clone{ nullptr }; // get the number of bytes to allocate for the clone size_t const userdata_size{ lua_rawlen(L1, -1) }; { // extract uservalues (don't transfer them yet) - int uvi = 0; - while (lua_getiuservalue(L1, source_i, ++uvi) != LUA_TNONE) {} // L1: ... u uv + int _uvi = 0; + while (lua_getiuservalue(L1, source_i, ++_uvi) != LUA_TNONE) {} // L1: ... u uv // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now lua_pop(L1, 1); // L1: ... u [uv]* - --uvi; - STACK_CHECK(L1, uvi + 1); + --_uvi; + STACK_CHECK(L1, _uvi + 1); // create the clone userdata with the required number of uservalue slots - clone = lua_newuserdatauv(L2, userdata_size, uvi); // L2: ... mt u + _clone = lua_newuserdatauv(L2, userdata_size, _uvi); // L2: ... mt u // add it in the cache - lua_pushlightuserdata(L2, source); // L2: ... mt u source + lua_pushlightuserdata(L2, _source); // L2: ... mt u source lua_pushvalue(L2, -2); // L2: ... mt u source u lua_rawset(L2, L2_cache_i); // L2: ... mt u // set metatable @@ -852,15 +851,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const lua_setmetatable(L2, -2); // L2: ... mt u // transfer and assign uservalues InterCopyContext c{ *this }; - while (uvi > 0) { + while (_uvi > 0) { c.L1_i = SourceIndex{ lua_absindex(L1, -1) }; if (!c.inter_copy_one()) { // L2: ... mt u uv raise_luaL_error(getErrL(), "Cannot copy upvalue type '%s'", luaL_typename(L1, -1)); } lua_pop(L1, 1); // L1: ... u [uv]* // this pops the value from the stack - lua_setiuservalue(L2, -2, uvi); // L2: ... mt u - --uvi; + lua_setiuservalue(L2, -2, _uvi); // L2: ... mt u + --_uvi; } // when we are done, all uservalues are popped from the stack, we can pop the source as well lua_pop(L1, 1); // L1: ... @@ -868,12 +867,12 @@ void InterCopyContext::inter_copy_keyvaluepair() const STACK_CHECK(L2, 2); // L2: ... mt u } // perform the custom cloning part - lua_insert(L2, -2); // L2: ... u mt + lua_insert(L2, -2); // L2: ... u mt // __lanesclone should always exist because we wouldn't be restoring data from a userdata_clone_sentinel closure to begin with lua_getfield(L2, -1, "__lanesclone"); // L2: ... u mt __lanesclone lua_remove(L2, -2); // L2: ... u __lanesclone - lua_pushlightuserdata(L2, clone); // L2: ... u __lanesclone clone - lua_pushlightuserdata(L2, source); // L2: ... u __lanesclone clone source + lua_pushlightuserdata(L2, _clone); // L2: ... u __lanesclone clone + lua_pushlightuserdata(L2, _source); // L2: ... u __lanesclone clone source lua_pushinteger(L2, userdata_size); // L2: ... u __lanesclone clone source size // clone:__lanesclone(dest, source, size) lua_call(L2, 3, 0); // L2: ... u @@ -891,9 +890,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const [[nodiscard]] bool InterCopyContext::inter_copy_lightuserdata() const { - void* const p{ lua_touserdata(L1, L1_i) }; - DEBUGSPEW_CODE(fprintf(stderr, "%p\n", p)); - lua_pushlightuserdata(L2, p); + void* const _p{ lua_touserdata(L1, L1_i) }; + DEBUGSPEW_CODE(fprintf(stderr, "%p\n", _p)); + lua_pushlightuserdata(L2, _p); return true; } @@ -915,15 +914,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const // LNUM patch support (keeping integer accuracy) #if defined LUA_LNUM || LUA_VERSION_NUM >= 503 if (lua_isinteger(L1, L1_i)) { - lua_Integer const v{ lua_tointeger(L1, L1_i) }; - DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", v)); - lua_pushinteger(L2, v); + lua_Integer const _v{ lua_tointeger(L1, L1_i) }; + DEBUGSPEW_CODE(fprintf(stderr, LUA_INTEGER_FMT "\n", _v)); + lua_pushinteger(L2, _v); } else #endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 { - lua_Number const v{ lua_tonumber(L1, L1_i) }; - DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", v)); - lua_pushnumber(L2, v); + lua_Number const _v{ lua_tonumber(L1, L1_i) }; + DEBUGSPEW_CODE(fprintf(stderr, LUA_NUMBER_FMT "\n", _v)); + lua_pushnumber(L2, _v); } return true; } @@ -932,10 +931,10 @@ void InterCopyContext::inter_copy_keyvaluepair() const [[nodiscard]] bool InterCopyContext::inter_copy_string() const { - size_t len; - char const* const s{ lua_tolstring(L1, L1_i, &len) }; - DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", s)); - lua_pushlstring(L2, s, len); + size_t _len; + char const* const _s{ lua_tolstring(L1, L1_i, &_len) }; + DEBUGSPEW_CODE(fprintf(stderr, "'%s'\n", _s)); + lua_pushlstring(L2, _s, _len); return true; } @@ -1029,8 +1028,8 @@ void InterCopyContext::inter_copy_keyvaluepair() const // Not a deep or clonable full userdata if (U->demoteFullUserdata) { // attempt demotion to light userdata - void* const lud{ lua_touserdata(L1, L1_i) }; - lua_pushlightuserdata(L2, lud); + void* const _lud{ lua_touserdata(L1, L1_i) }; + lua_pushlightuserdata(L2, _lud); } else { // raise an error raise_luaL_error(getErrL(), "can't copy non-deep full userdata across lanes"); } @@ -1083,16 +1082,16 @@ static char const* vt_names[] = { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "inter_copy_one()\n" INDENT_END(U))); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); - LuaType val_type{ lua_type_as_enum(L1, L1_i) }; - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast(val_type)], vt_names[static_cast(vt)])); + LuaType _val_type{ lua_type_as_enum(L1, L1_i) }; + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%s %s: " INDENT_END(U), lua_type_names[static_cast(_val_type)], vt_names[static_cast(vt)])); // Non-POD can be skipped if its metatable contains { __lanesignore = true } - if (((1 << static_cast(val_type)) & kPODmask) == 0) { + if (((1 << static_cast(_val_type)) & kPODmask) == 0) { if (lua_getmetatable(L1, L1_i)) { // L1: ... mt lua_getfield(L1, -1, "__lanesignore"); // L1: ... mt ignore? if (lua_isboolean(L1, -1) && lua_toboolean(L1, -1)) { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "__lanesignore -> LUA_TNIL\n" INDENT_END(U))); - val_type = LuaType::NIL; + _val_type = LuaType::NIL; } lua_pop(L1, 2); // L1: ... } @@ -1100,47 +1099,47 @@ static char const* vt_names[] = { STACK_CHECK(L1, 0); // Lets push nil to L2 if the object should be ignored - bool ret{ true }; - switch (val_type) { + bool _ret{ true }; + switch (_val_type) { // Basic types allowed both as values, and as table keys case LuaType::BOOLEAN: - ret = inter_copy_boolean(); + _ret = inter_copy_boolean(); break; case LuaType::NUMBER: - ret = inter_copy_number(); + _ret = inter_copy_number(); break; case LuaType::STRING: - ret = inter_copy_string(); + _ret = inter_copy_string(); break; case LuaType::LIGHTUSERDATA: - ret = inter_copy_lightuserdata(); + _ret = inter_copy_lightuserdata(); break; // The following types are not allowed as table keys case LuaType::USERDATA: - ret = inter_copy_userdata(); + _ret = inter_copy_userdata(); break; case LuaType::NIL: - ret = inter_copy_nil(); + _ret = inter_copy_nil(); break; case LuaType::FUNCTION: - ret = inter_copy_function(); + _ret = inter_copy_function(); break; case LuaType::TABLE: - ret = inter_copy_table(); + _ret = inter_copy_table(); break; // The following types cannot be copied case LuaType::CDATA: [[fallthrough]]; case LuaType::THREAD: - ret = false; + _ret = false; break; } - STACK_CHECK(L2, ret ? 1 : 0); + STACK_CHECK(L2, _ret ? 1 : 0); STACK_CHECK(L1, 0); - return ret; + return _ret; } // ################################################################################################# @@ -1189,30 +1188,30 @@ static char const* vt_names[] = { return InterCopyResult::Success; } - InterCopyResult result{ InterCopyResult::Success }; + InterCopyResult _result{ InterCopyResult::Success }; // package.loaders is renamed package.searchers in Lua 5.2 // but don't copy it anyway, as the function names change depending on the slot index! // users should provide an on_state_create function to setup custom loaders instead // don't copy package.preload in keeper states (they don't know how to translate functions) - char const* entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; - for (char const* const entry : entries) { - if (!entry) { + char const* _entries[] = { "path", "cpath", (mode == LookupMode::LaneBody) ? "preload" : nullptr /*, (LUA_VERSION_NUM == 501) ? "loaders" : "searchers"*/, nullptr }; + for (char const* const _entry : _entries) { + if (!_entry) { continue; } - DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), entry)); - lua_getfield(L1, L1_i, entry); + DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "package.%s\n" INDENT_END(U), _entry)); + lua_getfield(L1, L1_i, _entry); if (lua_isnil(L1, -1)) { lua_pop(L1, 1); } else { { DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); - result = inter_move(1); // moves the entry to L2 + _result = inter_move(1); // moves the entry to L2 STACK_CHECK(L1, 0); } - if (result == InterCopyResult::Success) { - lua_setfield(L2, -2, entry); // set package[entry] + if (_result == InterCopyResult::Success) { + lua_setfield(L2, -2, _entry); // set package[entry] } else { - lua_pushfstring(L1, "failed to copy package entry %s", entry); + lua_pushfstring(L1, "failed to copy package entry %s", _entry); // raise the error when copying from lane to lane, else just leave it on the stack to be raised later if (mode == LookupMode::LaneBody) { raise_lua_error(getErrL()); @@ -1223,7 +1222,7 @@ static char const* vt_names[] = { } } STACK_CHECK(L1, 0); - return result; + return _result; } // ################################################################################################# @@ -1237,8 +1236,8 @@ static char const* vt_names[] = { DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy()\n" INDENT_END(U))); DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); - int const top_L1{ lua_gettop(L1) }; - if (n_ > top_L1) { + int const _top_L1{ lua_gettop(L1) }; + if (n_ > _top_L1) { // requesting to copy more than is available? DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END(U))); return InterCopyResult::NotEnoughValues; @@ -1252,36 +1251,36 @@ static char const* vt_names[] = { * function entries, avoiding the same entries to be passed on as multiple * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! */ - int const top_L2{ lua_gettop(L2) }; // L2: ... + int const _top_L2{ lua_gettop(L2) }; // L2: ... lua_newtable(L2); // L2: ... cache - char tmpBuf[16]; - char const* const pBuf{ U->verboseErrors ? tmpBuf : "?" }; - InterCopyContext c{ U, L2, L1, CacheIndex{ top_L2 + 1 }, {}, VT::NORMAL, mode, pBuf }; - bool copyok{ true }; + char _tmpBuf[16]; + char const* const _pBuf{ U->verboseErrors ? _tmpBuf : "?" }; + InterCopyContext _c{ U, L2, L1, CacheIndex{ _top_L2 + 1 }, {}, VT::NORMAL, mode, _pBuf }; + bool _copyok{ true }; STACK_CHECK_START_REL(L1, 0); - for (int i{ top_L1 - n_ + 1 }, j{ 1 }; i <= top_L1; ++i, ++j) { + for (int i{ _top_L1 - n_ + 1 }, j{ 1 }; i <= _top_L1; ++i, ++j) { if (U->verboseErrors) { - sprintf(tmpBuf, "arg_%d", j); + sprintf(_tmpBuf, "arg_%d", j); } - c.L1_i = SourceIndex{ i }; - copyok = c.inter_copy_one(); // L2: ... cache {}n - if (!copyok) { + _c.L1_i = SourceIndex{ i }; + _copyok = _c.inter_copy_one(); // L2: ... cache {}n + if (!_copyok) { break; } } STACK_CHECK(L1, 0); - if (copyok) { + if (_copyok) { STACK_CHECK(L2, n_ + 1); // Remove the cache table. Persistent caching would cause i.e. multiple // messages passed in the same table to use the same table also in receiving end. - lua_remove(L2, top_L2 + 1); + lua_remove(L2, _top_L2 + 1); // L2: ... {}n return InterCopyResult::Success; } // error -> pop everything from the target state stack - lua_settop(L2, top_L2); + lua_settop(L2, _top_L2); STACK_CHECK(L2, 0); return InterCopyResult::Error; } @@ -1290,7 +1289,7 @@ static char const* vt_names[] = { [[nodiscard]] InterCopyResult InterCopyContext::inter_move(int n_) const { - InterCopyResult const ret{ inter_copy(n_) }; + InterCopyResult const _ret{ inter_copy(n_) }; lua_pop(L1, n_); - return ret; + return _ret; } diff --git a/src/keeper.cpp b/src/keeper.cpp index 7367d0c..39d2e85 100644 --- a/src/keeper.cpp +++ b/src/keeper.cpp @@ -76,15 +76,15 @@ static constexpr int kContentsTableIndex{ 1 }; // replaces the fifo ud by its uservalue on the stack [[nodiscard]] static keeper_fifo* prepare_fifo_access(lua_State* L_, int idx_) { - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, idx_) }; - if (fifo != nullptr) { + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, idx_) }; + if (_fifo != nullptr) { idx_ = lua_absindex(L_, idx_); STACK_GROW(L_, 1); // we can replace the fifo userdata in the stack without fear of it being GCed, there are other references around lua_getiuservalue(L_, idx_, kContentsTableIndex); lua_replace(L_, idx_); } - return fifo; + return _fifo; } // ################################################################################################# @@ -95,12 +95,12 @@ static constexpr int kContentsTableIndex{ 1 }; { STACK_GROW(L_, 2); STACK_CHECK_START_REL(L_, 0); - keeper_fifo* const fifo{ new (L_) keeper_fifo{} }; + keeper_fifo* const _fifo{ new (L_) keeper_fifo{} }; STACK_CHECK(L_, 1); lua_newtable(L_); lua_setiuservalue(L_, -2, kContentsTableIndex); STACK_CHECK(L_, 1); - return fifo; + return _fifo; } // ################################################################################################# @@ -109,12 +109,12 @@ static constexpr int kContentsTableIndex{ 1 }; // out: nothing, removes all pushed values from the stack static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) { - int const idx{ lua_gettop(L_) - count_ }; - int const start{ fifo_->first + fifo_->count - 1 }; + int const _idx{ lua_gettop(L_) - count_ }; + int const _start{ fifo_->first + fifo_->count - 1 }; // pop all additional arguments, storing them in the fifo - for (int i = count_; i >= 1; --i) { + for (int _i = count_; _i >= 1; --_i) { // store in the fifo the value at the top of the stack at the specified index, popping it from the stack - lua_rawseti(L_, idx, start + i); + lua_rawseti(L_, _idx, _start + _i); } fifo_->count += count_; } @@ -129,8 +129,8 @@ static void fifo_push(lua_State* L_, keeper_fifo* fifo_, int count_) static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) { STACK_GROW(L_, count_); - for (int i = 0; i < count_; ++i) { - lua_rawgeti(L_, 1, (fifo_->first + i)); + for (int _i = 0; _i < count_; ++_i) { + lua_rawgeti(L_, 1, (fifo_->first + _i)); } } @@ -141,32 +141,32 @@ static void fifo_peek(lua_State* L_, keeper_fifo* fifo_, int count_) static void fifo_pop(lua_State* L_, keeper_fifo* fifo_, int count_) { LUA_ASSERT(L_, lua_istable(L_, -1)); - int const fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl + int const _fifo_idx{ lua_gettop(L_) }; // L_: ... fifotbl // each iteration pushes a value on the stack! STACK_GROW(L_, count_ + 2); // skip first item, we will push it last for (int i = 1; i < count_; ++i) { int const at{ fifo_->first + i }; // push item on the stack - lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl val + lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl val // remove item from the fifo lua_pushnil(L_); // L_: ... fifotbl val nil - lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl val + lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl val } // now process first item { int const at{ fifo_->first }; - lua_rawgeti(L_, fifo_idx, at); // L_: ... fifotbl vals val + lua_rawgeti(L_, _fifo_idx, at); // L_: ... fifotbl vals val lua_pushnil(L_); // L_: ... fifotbl vals val nil - lua_rawseti(L_, fifo_idx, at); // L_: ... fifotbl vals val - lua_replace(L_, fifo_idx); // L_: ... vals + lua_rawseti(L_, _fifo_idx, at); // L_: ... fifotbl vals val + lua_replace(L_, _fifo_idx); // L_: ... vals } // avoid ever-growing indexes by resetting each time we detect the fifo is empty { - int const new_count{ fifo_->count - count_ }; - fifo_->first = (new_count == 0) ? 1 : (fifo_->first + count_); - fifo_->count = new_count; + int const _new_count{ fifo_->count - count_ }; + fifo_->first = (_new_count == 0) ? 1 : (fifo_->first + count_); + fifo_->count = _new_count; } } @@ -202,34 +202,34 @@ 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_) { - Keeper* const K{ linda_.whichKeeper() }; - SourceState const KL{ K ? K->L : nullptr }; - if (KL == nullptr) + Keeper* const _K{ linda_.whichKeeper() }; + SourceState const _KL{ _K ? _K->L : nullptr }; + if (_KL == nullptr) return 0; - STACK_GROW(KL, 4); - STACK_CHECK_START_REL(KL, 0); - kFifosRegKey.pushValue(KL); // KL: fifos L_: - lua_pushlightuserdata(KL, &linda_); // KL: fifos ud L_: - lua_rawget(KL, -2); // KL: fifos storage L_: - lua_remove(KL, -2); // KL: storage L_: - if (!lua_istable(KL, -1)) { - lua_pop(KL, 1); // KL: L_: - STACK_CHECK(KL, 0); + STACK_GROW(_KL, 4); + STACK_CHECK_START_REL(_KL, 0); + kFifosRegKey.pushValue(_KL); // KL: fifos L_: + lua_pushlightuserdata(_KL, &linda_); // KL: fifos ud L_: + lua_rawget(_KL, -2); // KL: fifos storage L_: + lua_remove(_KL, -2); // KL: storage L_: + if (!lua_istable(_KL, -1)) { + lua_pop(_KL, 1); // KL: L_: + STACK_CHECK(_KL, 0); return 0; } // move data from keeper to destination state STACK_GROW(L_, 5); STACK_CHECK_START_REL(L_, 0); lua_newtable(L_); // KL: storage L_: out - InterCopyContext c{ linda_.U, L_, KL, {}, {}, {}, LookupMode::FromKeeper, {} }; - lua_pushnil(KL); // KL: storage nil L_: out - while (lua_next(KL, -2)) { // KL: storage key fifo L_: out - keeper_fifo* fifo = prepare_fifo_access(KL, -1); // KL: storage key fifotbl L_: out - lua_pushvalue(KL, -2); // KL: storage key fifotbl key L_: out - std::ignore = c.inter_move(1); // KL: storage key fifotbl L_: out key + InterCopyContext _c{ linda_.U, L_, _KL, {}, {}, {}, LookupMode::FromKeeper, {} }; + lua_pushnil(_KL); // KL: storage nil L_: out + while (lua_next(_KL, -2)) { // KL: storage key fifo L_: out + keeper_fifo* fifo = prepare_fifo_access(_KL, -1); // KL: storage key fifotbl L_: out + lua_pushvalue(_KL, -2); // KL: storage key fifotbl key L_: out + std::ignore = _c.inter_move(1); // KL: storage key fifotbl L_: out key STACK_CHECK(L_, 2); lua_newtable(L_); // KL: storage key L_: out key keyout - std::ignore = c.inter_move(1); // KL: storage key L_: out key keyout fifotbl + std::ignore = _c.inter_move(1); // KL: storage key L_: out key keyout fifotbl lua_pushinteger(L_, fifo->first); // KL: storage key L_: out key keyout fifotbl first STACK_CHECK(L_, 5); lua_setfield(L_, -3, "first"); // KL: storage key L_: out key keyout fifotbl @@ -244,8 +244,8 @@ int keeper_push_linda_storage(Linda& linda_, DestState L_) STACK_CHECK(L_, 1); } // KL_: storage L_: out STACK_CHECK(L_, 1); - lua_pop(KL, 1); // KL: L_: out - STACK_CHECK(KL, 0); + lua_pop(_KL, 1); // KL: L_: out + STACK_CHECK(_KL, 0); return 1; } @@ -271,7 +271,7 @@ int keepercall_clear(lua_State* L_) // out: true|false int keepercall_send(lua_State* L_) { - int const n{ lua_gettop(L_) - 2 }; + int const _n{ lua_gettop(L_) - 2 }; push_table(L_, 1); // L_: ud key ... fifos // get the fifo associated to this key in this linda, create it if it doesn't exist lua_pushvalue(L_, 2); // L_: ud key ... fifos key @@ -284,14 +284,14 @@ int keepercall_send(lua_State* L_) lua_rawset(L_, -4); // L_: ud key ... fifos fifo } lua_remove(L_, -2); // L_: ud key ... fifo - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo->limit >= 0 && fifo->count + n > fifo->limit) { + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo->limit >= 0 && _fifo->count + _n > _fifo->limit) { lua_settop(L_, 0); // L_: lua_pushboolean(L_, 0); // L_:false } else { - fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl + _fifo = prepare_fifo_access(L_, -1); // L_: ud fifotbl lua_replace(L_, 2); // L_: ud fifotbl ... - fifo_push(L_, fifo, n); // L_: ud fifotbl + fifo_push(L_, _fifo, _n); // L_: ud fifotbl lua_settop(L_, 0); // L_: lua_pushboolean(L_, 1); // L_: true } @@ -304,19 +304,19 @@ int keepercall_send(lua_State* L_) // out: (key, val) or nothing int keepercall_receive(lua_State* L_) { - int const top{ lua_gettop(L_) }; + int const _top{ lua_gettop(L_) }; push_table(L_, 1); // L_: ud keys fifos lua_replace(L_, 1); // L_: fifos keys - for (int i = 2; i <= top; ++i) { - lua_pushvalue(L_, i); // L_: fifos keys key[i] + for (int _i = 2; _i <= _top; ++_i) { + lua_pushvalue(L_, _i); // L_: fifos keys key[i] lua_rawget(L_, 1); // L_: fifos keys fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl - if (fifo != nullptr && fifo->count > 0) { - fifo_pop(L_, fifo, 1); // L_: fifos keys val + keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos keys fifotbl + if (_fifo != nullptr && _fifo->count > 0) { + fifo_pop(L_, _fifo, 1); // L_: fifos keys val if (!lua_isnil(L_, -1)) { lua_replace(L_, 1); // L_: val keys - lua_settop(L_, i); // L_: val keys key[i] - if (i != 2) { + lua_settop(L_, _i); // L_: val keys key[i] + if (_i != 2) { lua_replace(L_, 2); // L_: val key keys lua_settop(L_, 2); // L_: val key } @@ -324,7 +324,7 @@ int keepercall_receive(lua_State* L_) return 2; } } - lua_settop(L_, top); // L_: data keys + lua_settop(L_, _top); // L_: data keys } // nothing to receive return 0; @@ -335,9 +335,9 @@ int keepercall_receive(lua_State* L_) // in: linda_ud key mincount [maxcount] int keepercall_receive_batched(lua_State* L_) { - int const min_count{ static_cast(lua_tointeger(L_, 3)) }; - if (min_count > 0) { - int const max_count{ static_cast(luaL_optinteger(L_, 4, min_count)) }; + int const _min_count{ static_cast(lua_tointeger(L_, 3)) }; + if (_min_count > 0) { + int const _max_count{ static_cast(luaL_optinteger(L_, 4, _min_count)) }; lua_settop(L_, 2); // L_: ud key lua_insert(L_, 1); // L_: key ud push_table(L_, 2); // L_: key ud fifos @@ -345,9 +345,9 @@ int keepercall_receive_batched(lua_State* L_) lua_pushvalue(L_, 1); // L_: key fifos key lua_rawget(L_, 2); // L_: key fifos fifo lua_remove(L_, 2); // L_: key fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl - if (fifo != nullptr && fifo->count >= min_count) { - fifo_pop(L_, fifo, std::min(max_count, fifo->count)); // L_: key ... + keeper_fifo* const _fifo{ prepare_fifo_access(L_, 2) }; // L_: key fifotbl + if (_fifo != nullptr && _fifo->count >= _min_count) { + fifo_pop(L_, _fifo, std::min(_max_count, _fifo->count)); // L_: key ... } else { lua_settop(L_, 0); // L_: } @@ -363,16 +363,16 @@ int keepercall_receive_batched(lua_State* L_) // out: true or nil int keepercall_limit(lua_State* L_) { - int const limit{ static_cast(lua_tointeger(L_, 3)) }; + int const _limit{ static_cast(lua_tointeger(L_, 3)) }; push_table(L_, 1); // L_: ud key n fifos lua_replace(L_, 1); // L_: fifos key n lua_pop(L_, 1); // L_: fifos key lua_pushvalue(L_, -1); // L_: fifos key key lua_rawget(L_, -3); // L_: fifos key fifo|nil - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo == nullptr) { // L_: fifos key nil + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo == nullptr) { // L_: fifos key nil lua_pop(L_, 1); // L_: fifos key - fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo + _fifo = fifo_new(KeeperState{ L_ }); // L_: fifos key fifo lua_rawset(L_, -3); // L_: fifos } // remove any clutter on the stack @@ -380,13 +380,13 @@ int keepercall_limit(lua_State* L_) // return true if we decide that blocked threads waiting to write on that key should be awakened // this is the case if we detect the key was full but it is no longer the case if ( - ((fifo->limit >= 0) && (fifo->count >= fifo->limit)) // the key was full if limited and count exceeded the previous limit - && ((limit < 0) || (fifo->count < limit)) // the key is not full if unlimited or count is lower than the new limit + ((_fifo->limit >= 0) && (_fifo->count >= _fifo->limit)) // the key was full if limited and count exceeded the previous limit + && ((_limit < 0) || (_fifo->count < _limit)) // the key is not full if unlimited or count is lower than the new limit ) { lua_pushboolean(L_, 1); // L_: true } // set the new limit - fifo->limit = limit; + _fifo->limit = _limit; // return 0 or 1 value return lua_gettop(L_); } @@ -397,7 +397,7 @@ int keepercall_limit(lua_State* L_) // out: true if the linda was full but it's no longer the case, else nothing int keepercall_set(lua_State* L_) { - bool should_wake_writers{ false }; + bool _should_wake_writers{ false }; STACK_GROW(L_, 6); // retrieve fifos associated with the linda @@ -409,28 +409,28 @@ int keepercall_set(lua_State* L_) lua_pushvalue(L_, -1); // L_: fifos key key lua_rawget(L_, 1); // L_: fifos key fifo|nil // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo - if (fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo != nullptr) { // might be nullptr if we set a nonexistent key to nil // L_: fifos key fifo + if (_fifo->limit < 0) { // fifo limit value is the default (unlimited): we can totally remove it lua_pop(L_, 1); // L_: fifos key lua_pushnil(L_); // L_: fifos key nil lua_rawset(L_, -3); // L_: fifos } else { // we create room if the fifo was full but it is no longer the case - should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit); + _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit); lua_remove(L_, -2); // L_: fifos fifo lua_newtable(L_); // L_: fifos fifo {} lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos fifo - fifo->first = 1; - fifo->count = 0; + _fifo->first = 1; + _fifo->count = 0; } } } else { // set/replace contents stored at the specified key? - int const count{ lua_gettop(L_) - 2 }; // number of items we want to store + int const _count{ lua_gettop(L_) - 2 }; // number of items we want to store lua_pushvalue(L_, 2); // L_: fifos key [val [, ...]] key lua_rawget(L_, 1); // L_: fifos key [val [, ...]] fifo|nil - keeper_fifo* fifo{ keeper_fifo::getPtr(L_, -1) }; - if (fifo == nullptr) { // can be nullptr if we store a value at a new key // fifos key [val [, ...]] nil + keeper_fifo* _fifo{ keeper_fifo::getPtr(L_, -1) }; + if (_fifo == nullptr) { // can be nullptr if we store a value at a new key // 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); // L_: fifos key [val [, ...]] std::ignore = fifo_new(KeeperState{ L_ }); // L_: fifos key [val [, ...]] fifo @@ -440,19 +440,19 @@ int keepercall_set(lua_State* L_) } else { // L_: fifos key [val [, ...]] fifo // the fifo exists, we just want to update its contents // we create room if the fifo was full but it is no longer the case - should_wake_writers = (fifo->limit > 0) && (fifo->count >= fifo->limit) && (count < fifo->limit); + _should_wake_writers = (_fifo->limit > 0) && (_fifo->count >= _fifo->limit) && (_count < _fifo->limit); // empty the fifo for the specified key: replace uservalue with a virgin table, reset counters, but leave limit unchanged! lua_newtable(L_); // L_: fifos key [val [, ...]] fifo {} lua_setiuservalue(L_, -2, kContentsTableIndex); // L_: fifos key [val [, ...]] fifo - fifo->first = 1; - fifo->count = 0; + _fifo->first = 1; + _fifo->count = 0; } - fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl + _fifo = prepare_fifo_access(L_, -1); // L_: fifos key [val [, ...]] fifotbl // move the fifo below the values we want to store lua_insert(L_, 3); // L_: fifos key fifotbl [val [, ...]] - fifo_push(L_, fifo, count); // L_: fifos key fifotbl + fifo_push(L_, _fifo, _count); // L_: fifos key fifotbl } - return should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; + return _should_wake_writers ? (lua_pushboolean(L_, 1), 1) : 0; } // ################################################################################################# @@ -461,21 +461,21 @@ int keepercall_set(lua_State* L_) // out: at most values int keepercall_get(lua_State* L_) { - int count{ 1 }; + int _count{ 1 }; if (lua_gettop(L_) == 3) { // L_: ud key count - count = static_cast(lua_tointeger(L_, 3)); + _count = static_cast(lua_tointeger(L_, 3)); lua_pop(L_, 1); // L_: ud key } push_table(L_, 1); // L_: ud key fifos lua_replace(L_, 1); // L_: fifos key lua_rawget(L_, 1); // L_: fifos fifo - keeper_fifo* const fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl - if (fifo != nullptr && fifo->count > 0) { + keeper_fifo* const _fifo{ prepare_fifo_access(L_, -1) }; // L_: fifos fifotbl + if (_fifo != nullptr && _fifo->count > 0) { lua_remove(L_, 1); // L_: fifotbl - count = std::min(count, fifo->count); + _count = std::min(_count, _fifo->count); // read value off the fifo - fifo_peek(L_, fifo, count); // L_: fifotbl ... - return count; + fifo_peek(L_, _fifo, _count); // L_: fifotbl ... + return _count; } // no fifo was ever registered for this key, or it is empty return 0; @@ -494,10 +494,10 @@ int keepercall_count(lua_State* L_) lua_replace(L_, 1); // L_: out fifos lua_pushnil(L_); // L_: out fifos nil while (lua_next(L_, 2)) { // L_: out fifos key fifo - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; lua_pop(L_, 1); // L_: out fifos key lua_pushvalue(L_, -1); // L_: out fifos key key - lua_pushinteger(L_, fifo->count); // L_: out fifos key key count + lua_pushinteger(L_, _fifo->count); // L_: out fifos key key count lua_rawset(L_, -5); // L_: out fifos key } lua_pop(L_, 1); // L_: out @@ -510,8 +510,8 @@ int keepercall_count(lua_State* L_) if (lua_isnil(L_, -1)) { // L_: the key is unknown // L_: fifos nil lua_remove(L_, -2); // L_: nil } else { // the key is known // L_: fifos fifo - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; - lua_pushinteger(L_, fifo->count); // L_: fifos fifo count + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; + lua_pushinteger(L_, _fifo->count); // L_: fifos fifo count lua_replace(L_, -3); // L_: count fifo lua_pop(L_, 1); // L_: count } @@ -526,10 +526,10 @@ int keepercall_count(lua_State* L_) while (lua_gettop(L_) > 2) { lua_pushvalue(L_, -1); // L_: out fifos keys... key lua_rawget(L_, 2); // L_: out fifos keys... fifo|nil - keeper_fifo* const fifo{ keeper_fifo::getPtr(L_, -1) }; + keeper_fifo* const _fifo{ keeper_fifo::getPtr(L_, -1) }; lua_pop(L_, 1); // L_: out fifos keys... - if (fifo != nullptr) { // L_: the key is known - lua_pushinteger(L_, fifo->count); // L_: out fifos keys... count + if (_fifo != nullptr) { // L_: the key is known + lua_pushinteger(L_, _fifo->count); // L_: out fifos keys... count lua_rawset(L_, 1); // L_: out fifos keys... } else { // the key is unknown lua_pop(L_, 1); // L_: out fifos keys... @@ -557,27 +557,27 @@ int keepercall_count(lua_State* L_) void close_keepers(Universe* U_) { if (U_->keepers != nullptr) { - int nbKeepers{ U_->keepers->nb_keepers }; + int _nbKeepers{ U_->keepers->nb_keepers }; // NOTE: imagine some keeper state N+1 currently holds a linda that uses another keeper N, and a _gc that will make use of it // when keeper N+1 is closed, object is GCed, linda operation is called, which attempts to acquire keeper N, whose Lua state no longer exists // in that case, the linda operation should do nothing. which means that these operations must check for keeper acquisition success // which is early-outed with a U->keepers->nbKeepers null-check U_->keepers->nb_keepers = 0; - for (int i = 0; i < nbKeepers; ++i) { - lua_State* const K{ U_->keepers->keeper_array[i].L }; - U_->keepers->keeper_array[i].L = KeeperState{ nullptr }; + for (int _i = 0; _i < _nbKeepers; ++_i) { + lua_State* const K{ U_->keepers->keeper_array[_i].L }; + U_->keepers->keeper_array[_i].L = KeeperState{ nullptr }; if (K != nullptr) { lua_close(K); } else { // detected partial init: destroy only the mutexes that got initialized properly - nbKeepers = i; + _nbKeepers = _i; } } - for (int i = 0; i < nbKeepers; ++i) { - U_->keepers->keeper_array[i].~Keeper(); + for (int _i = 0; _i < _nbKeepers; ++_i) { + U_->keepers->keeper_array[_i].~Keeper(); } // free the keeper bookkeeping structure - U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (nbKeepers - 1) * sizeof(Keeper)); + U_->internalAllocator.free(U_->keepers, sizeof(Keepers) + (_nbKeepers - 1) * sizeof(Keeper)); U_->keepers = nullptr; } } @@ -600,10 +600,10 @@ void init_keepers(Universe* U_, lua_State* L_) LUA_ASSERT(L_, lua_gettop(L_) == 1 && lua_istable(L_, 1)); STACK_CHECK_START_REL(L_, 0); // L_: settings lua_getfield(L_, 1, "nb_keepers"); // L_: settings nb_keepers - int const nb_keepers{ static_cast(lua_tointeger(L_, -1)) }; + int const _nb_keepers{ static_cast(lua_tointeger(L_, -1)) }; lua_pop(L_, 1); // L_: settings - if (nb_keepers < 1) { - raise_luaL_error(L_, "Bad number of keepers (%d)", nb_keepers); + if (_nb_keepers < 1) { + raise_luaL_error(L_, "Bad number of keepers (%d)", _nb_keepers); } STACK_CHECK(L_, 0); @@ -614,51 +614,51 @@ void init_keepers(Universe* U_, lua_State* L_) // Keepers contains an array of 1 Keeper, adjust for the actual number of keeper states { - size_t const bytes = sizeof(Keepers) + (nb_keepers - 1) * sizeof(Keeper); + size_t const bytes = sizeof(Keepers) + (_nb_keepers - 1) * sizeof(Keeper); U_->keepers = static_cast(U_->internalAllocator.alloc(bytes)); if (U_->keepers == nullptr) { raise_luaL_error(L_, "init_keepers() failed while creating keeper array; out of memory"); } U_->keepers->Keepers::Keepers(); U_->keepers->gc_threshold = keepers_gc_threshold; - U_->keepers->nb_keepers = nb_keepers; + U_->keepers->nb_keepers = _nb_keepers; - for (int i = 0; i < nb_keepers; ++i) { - U_->keepers->keeper_array[i].Keeper::Keeper(); + for (int _i = 0; _i < _nb_keepers; ++_i) { + U_->keepers->keeper_array[_i].Keeper::Keeper(); } } - for (int i = 0; i < nb_keepers; ++i) { + for (int _i = 0; _i < _nb_keepers; ++_i) { // note that we will leak K if we raise an error later - KeeperState const K{ create_state(U_, L_) }; // L_: settings K: - if (K == nullptr) { + KeeperState const _K{ create_state(U_, L_) }; // L_: settings K: + if (_K == nullptr) { raise_luaL_error(L_, "init_keepers() failed while creating keeper states; out of memory"); } - U_->keepers->keeper_array[i].L = K; + U_->keepers->keeper_array[_i].L = _K; if (U_->keepers->gc_threshold >= 0) { - lua_gc(K, LUA_GCSTOP, 0); + lua_gc(_K, LUA_GCSTOP, 0); } - STACK_CHECK_START_ABS(K, 0); + STACK_CHECK_START_ABS(_K, 0); // copy the universe pointer in the keeper itself - universe_store(K, U_); - STACK_CHECK(K, 0); + universe_store(_K, U_); + STACK_CHECK(_K, 0); // make sure 'package' is initialized in keeper states, so that we have require() // this because this is needed when transferring deep userdata object - luaL_requiref(K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package - lua_pop(K, 1); // L_: settings K: - STACK_CHECK(K, 0); - serialize_require(DEBUGSPEW_PARAM_COMMA(U_) K); - STACK_CHECK(K, 0); + luaL_requiref(_K, LUA_LOADLIBNAME, luaopen_package, 1); // L_: settings K: package + lua_pop(_K, 1); // L_: settings K: + STACK_CHECK(_K, 0); + serialize_require(DEBUGSPEW_PARAM_COMMA(U_) _K); + STACK_CHECK(_K, 0); // copy package.path and package.cpath from the source state if (luaG_getmodule(L_, LUA_LOADLIBNAME) != LuaType::NIL) { // L_: settings package K: // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately - InterCopyContext c{ U_, DestState{ K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; - if (c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: + InterCopyContext _c{ U_, DestState{ _K }, SourceState{ L_ }, {}, SourceIndex{ lua_absindex(L_, -1) }, {}, LookupMode::ToKeeper, {} }; + if (_c.inter_copy_package() != InterCopyResult::Success) { // L_: settings ... error_msg K: // if something went wrong, the error message is at the top of the stack lua_remove(L_, -2); // L_: settings error_msg raise_lua_error(L_); @@ -666,19 +666,19 @@ void init_keepers(Universe* U_, lua_State* L_) } lua_pop(L_, 1); // L_: settings K: STACK_CHECK(L_, 0); - STACK_CHECK(K, 0); + STACK_CHECK(_K, 0); // attempt to call on_state_create(), if we have one and it is a C function // (only support a C function because we can't transfer executable Lua code in keepers) // will raise an error in L_ in case of problem - callOnStateCreate(U_, K, L_, LookupMode::ToKeeper); + callOnStateCreate(U_, _K, L_, LookupMode::ToKeeper); // 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: + lua_pushfstring(_K, "Keeper #%d", _i + 1); // L_: settings K: "Keeper #n" + lua_setglobal(_K, "decoda_name"); // L_: settings K: // create the fifos table in the keeper state - kFifosRegKey.setValue(K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: - STACK_CHECK(K, 0); + kFifosRegKey.setValue(_K, [](lua_State* L_) { lua_newtable(L_); }); // L_: settings K: + STACK_CHECK(_K, 0); } STACK_CHECK(L_, 0); } @@ -687,12 +687,12 @@ void init_keepers(Universe* U_, lua_State* L_) Keeper* Linda::acquireKeeper() const { - int const nbKeepers{ U->keepers->nb_keepers }; + int const _nbKeepers{ U->keepers->nb_keepers }; // can be 0 if this happens during main state shutdown (lanes is being GC'ed -> no keepers) - if (nbKeepers) { - Keeper* const K{ &U->keepers->keeper_array[keeperIndex] }; - K->mutex.lock(); - return K; + if (_nbKeepers) { + Keeper* const _K{ &U->keepers->keeper_array[keeperIndex] }; + _K->mutex.lock(); + return _K; } return nullptr; } @@ -711,17 +711,17 @@ void Linda::releaseKeeper(Keeper* K_) const void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mode_) { - int const n{ lua_gettop(L_) }; - for (int i = start_; i <= n; ++i) { + int const _n{ lua_gettop(L_) }; + for (int _i = start_; _i <= _n; ++_i) { if (mode_ == LookupMode::ToKeeper) { - if (lua_isnil(L_, i)) { + if (lua_isnil(L_, _i)) { kNilSentinel.pushKey(L_); - lua_replace(L_, i); + lua_replace(L_, _i); } } else { - if (kNilSentinel.equals(L_, i)) { + if (kNilSentinel.equals(L_, _i)) { lua_pushnil(L_); - lua_replace(L_, i); + lua_replace(L_, _i); } } } @@ -740,21 +740,21 @@ void keeper_toggle_nil_sentinels(lua_State* L_, int start_, LookupMode const mod */ 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 }; // L: ... args... K_: - int const top_K{ lua_gettop(K_) }; + KeeperCallResult _result{}; + int const _args{ starting_index_ ? (lua_gettop(L_) - starting_index_ + 1) : 0 }; // L: ... args... K_: + int const _top_K{ lua_gettop(K_) }; // if we didn't do anything wrong, the keeper stack should be clean - LUA_ASSERT(L_, top_K == 0); + LUA_ASSERT(L_, _top_K == 0); STACK_GROW(K_, 2); PUSH_KEEPER_FUNC(K_, func_); // L: ... args... K_: func_ lua_pushlightuserdata(K_, linda_); // L: ... args... K_: func_ linda if ( - (args == 0) || - (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(args) == InterCopyResult::Success) + (_args == 0) || + (InterCopyContext{ U_, DestState{ K_ }, SourceState{ L_ }, {}, {}, {}, LookupMode::ToKeeper, {} }.inter_copy(_args) == InterCopyResult::Success) ) { // L: ... args... K_: func_ linda args... - lua_call(K_, 1 + args, LUA_MULTRET); // L: ... args... K_: result... - int const retvals{ lua_gettop(K_) - top_K }; + lua_call(K_, 1 + _args, LUA_MULTRET); // L: ... args... K_: result... + int const retvals{ lua_gettop(K_) - _top_K }; // note that this can raise a lua error while the keeper state (and its mutex) is acquired // this may interrupt a lane, causing the destruction of the underlying OS thread // after this, another lane making use of this keeper can get an error code from the mutex-locking function @@ -763,29 +763,29 @@ KeeperCallResult keeper_call(Universe* U_, KeeperState K_, keeper_api_t func_, l (retvals == 0) || (InterCopyContext{ U_, DestState{ L_ }, SourceState{ K_ }, {}, {}, {}, LookupMode::FromKeeper, {} }.inter_move(retvals) == InterCopyResult::Success) ) { // L: ... args... result... K_: result... - result.emplace(retvals); + _result.emplace(retvals); } } // whatever happens, restore the stack to where it was at the origin - lua_settop(K_, top_K); // L: ... args... result... K_: + lua_settop(K_, _top_K); // L: ... args... result... K_: // don't do this for this particular function, as it is only called during Linda destruction, and we don't want to raise an error, ever if (func_ != KEEPER_API(clear)) [[unlikely]] { // since keeper state GC is stopped, let's run a step once in a while if required - int const gc_threshold{ U_->keepers->gc_threshold }; - if (gc_threshold == 0) [[unlikely]] { + int const _gc_threshold{ U_->keepers->gc_threshold }; + if (_gc_threshold == 0) [[unlikely]] { lua_gc(K_, LUA_GCSTEP, 0); - } else if (gc_threshold > 0) [[likely]] { - int const gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; - if (gc_usage >= gc_threshold) { + } else if (_gc_threshold > 0) [[likely]] { + int const _gc_usage{ lua_gc(K_, LUA_GCCOUNT, 0) }; + if (_gc_usage >= _gc_threshold) { lua_gc(K_, LUA_GCCOLLECT, 0); - int const gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; - if (gc_usage_after > gc_threshold) [[unlikely]] { - raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", gc_usage_after); + int const _gc_usage_after{ lua_gc(K_, LUA_GCCOUNT, 0) }; + if (_gc_usage_after > _gc_threshold) [[unlikely]] { + raise_luaL_error(L_, "Keeper GC threshold is too low, need at least %d", _gc_usage_after); } } } } - return result; + return _result; } diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h index b8f04b6..a8738a8 100644 --- a/src/macros_and_utils.h +++ b/src/macros_and_utils.h @@ -159,26 +159,26 @@ class StackChecker }; #define STACK_CHECK_START_REL(L, offset_) \ - StackChecker stackChecker_##L \ + StackChecker _stackChecker_##L \ { \ L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ } #define STACK_CHECK_START_ABS(L, offset_) \ - StackChecker stackChecker_##L \ + StackChecker _stackChecker_##L \ { \ L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ } #define STACK_CHECK_RESET_REL(L, offset_) \ - stackChecker_##L = StackChecker \ + _stackChecker_##L = StackChecker \ { \ L, StackChecker::Relative{ offset_ }, __FILE__, __LINE__ \ } #define STACK_CHECK_RESET_ABS(L, offset_) \ - stackChecker_##L = StackChecker \ + _stackChecker_##L = StackChecker \ { \ L, StackChecker::Absolute{ offset_ }, __FILE__, __LINE__ \ } -#define STACK_CHECK(L, offset_) stackChecker_##L.check(offset_, __FILE__, __LINE__) +#define STACK_CHECK(L, offset_) _stackChecker_##L.check(offset_, __FILE__, __LINE__) #endif // NDEBUG -- cgit v1.2.3-55-g6feb