From 4670b7deee3eeca20f72d91d9fb7094b51348956 Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Wed, 5 Jun 2024 16:07:08 +0200 Subject: Modernize compat --- src/cancel.cpp | 6 +- src/compat.h | 145 +++++++++++++++++++++++++++++++---------------- src/deep.cpp | 8 +-- src/intercopycontext.cpp | 42 +++++++------- src/lane.cpp | 32 +++++------ src/lanes.cpp | 34 +++++------ src/linda.cpp | 20 +++---- src/lindafactory.cpp | 6 +- src/nameof.cpp | 14 ++--- src/state.cpp | 10 ++-- src/tools.cpp | 34 +++++------ src/tracker.cpp | 2 +- src/universe.cpp | 10 ++-- 13 files changed, 206 insertions(+), 157 deletions(-) diff --git a/src/cancel.cpp b/src/cancel.cpp index 23d9d04..f53dc36 100644 --- a/src/cancel.cpp +++ b/src/cancel.cpp @@ -185,8 +185,8 @@ CancelOp which_cancel_op(std::string_view const& opString_) [[nodiscard]] static CancelOp which_cancel_op(lua_State* L_, int idx_) { - if (lua_type(L_, idx_) == LUA_TSTRING) { - std::string_view const _str{ lua_tostringview(L_, idx_) }; + if (luaG_type(L_, idx_) == LuaType::STRING) { + std::string_view const _str{ luaG_tostringview(L_, idx_) }; CancelOp _op{ which_cancel_op(_str) }; lua_remove(L_, idx_); // argument is processed, remove it if (_op == CancelOp::Invalid) { @@ -215,7 +215,7 @@ LUAG_FUNC(thread_cancel) } 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 + if (luaG_type(L_, 2) == LuaType::NUMBER) { // 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); diff --git a/src/compat.h b/src/compat.h index 43f8922..6307d86 100644 --- a/src/compat.h +++ b/src/compat.h @@ -47,11 +47,11 @@ enum class LuaType CDATA = 10 // LuaJIT CDATA }; -inline LuaType lua_type_as_enum(lua_State* L_, int idx_) +inline LuaType luaG_type(lua_State* L_, int idx_) { return static_cast(lua_type(L_, idx_)); } -inline char const* lua_typename(lua_State* L_, LuaType t_) +inline char const* luaG_typename(lua_State* L_, LuaType t_) { return lua_typename(L_, static_cast(t_)); } @@ -79,18 +79,10 @@ 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 -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 int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); @@ -102,14 +94,6 @@ int luaL_getsubtable(lua_State* L_, int idx_, const char* fname_); // wrap Lua 5.2 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 502 -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 @@ -121,7 +105,7 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u // starting with Lua 5.3, lua_getfield returns the type of the value it found #if LUA_VERSION_NUM < 503 lua_getfield(L_, idx_, k_.data()); - return lua_type_as_enum(L_, -1); + return luaG_type(L_, -1); #else // LUA_VERSION_NUM >= 503 return static_cast(lua_getfield(L_, idx_, k_.data())); #endif // LUA_VERSION_NUM >= 503 @@ -132,14 +116,6 @@ inline int lua504_dump(lua_State* L_, lua_Writer writer_, void* data_, [[maybe_u // wrap Lua 5.3 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 503 -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_)); @@ -164,14 +140,6 @@ int lua_setiuservalue(lua_State* L_, int idx_, int n_); // wrap Lua 5.4 calls under Lua 5.1 API when it is simpler that way #if LUA_VERSION_NUM == 504 -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_)); @@ -182,14 +150,6 @@ inline int luaL_optint(lua_State* L_, int n_, lua_Integer d_) // ################################################################################################# -// use this in place of lua_absindex to save a function call -inline int luaG_absindex(lua_State* L_, int idx_) -{ - return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); -} - -// ################################################################################################# - // a strong-typed wrapper over lua error codes to see them easier in a debugger enum class LuaError { @@ -211,28 +171,117 @@ inline constexpr LuaError ToLuaError(int rc_) // ################################################################################################# +// Default matches Lua 5.4 as of now +template +struct Wrap +{ + static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, int const strip_) + { + return ::lua_dump(L_, writer_, data_, strip_); + } + + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int nup_) + { + ::luaL_setfuncs(L_, funcs_, nup_); + } +}; + +// ################################################################################################# + +template +struct Wrap::type> +{ + static inline int lua_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) + { + return ::lua_dump(L_, writer_, data_, strip_); + } + + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) + { + ::luaL_setfuncs(L_, funcs_, nup_); + } +}; + +// ################################################################################################# + +template +struct Wrap::type> +{ + static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) + { + return ::lua_dump(L_, writer_, data_); + } + + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], int const nup_) + { + ::luaL_setfuncs(L_, funcs_, nup_); + } +}; + +// ################################################################################################# + +template +struct Wrap::type> +{ + static inline int lua_dump(lua_State* const L_, lua_Writer const writer_, void* const data_, [[maybe_unused]] int const strip_) + { + return ::lua_dump(L_, writer_, data_); + } + + static void luaL_setfuncs(lua_State* const L_, luaL_Reg const funcs_[], [[maybe_unused]] int const nup_) + { + ::luaL_register(L_, nullptr, funcs_); + } +}; + +// ################################################################################################# +// All the compatibility wrappers we expose start with luaG_ + +// use this in place of lua_absindex to save a function call +inline int luaG_absindex(lua_State* L_, int idx_) +{ + return (((idx_) >= 0 || (idx_) <= LUA_REGISTRYINDEX) ? (idx_) : lua_gettop(L_) + (idx_) + 1); +} + +// ------------------------------------------------------------------------------------------------- + +inline int luaG_dump(lua_State* L_, lua_Writer writer_, void* data_, int strip_) +{ + return Wrap::lua_dump(L_, writer_, data_, strip_); +} + +// ------------------------------------------------------------------------------------------------- + LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); +// ------------------------------------------------------------------------------------------------- + +inline void luaG_registerlibfuncs(lua_State* L_, luaL_Reg const funcs_[]) +{ + Wrap::luaL_setfuncs(L_, funcs_, 0); +} + // ################################################################################################# +// must keep as a macro as long as we do constant string concatenations #define STRINGVIEW_FMT "%.*s" // a replacement of lua_tolstring -[[nodiscard]] inline std::string_view lua_tostringview(lua_State* L_, int idx_) +[[nodiscard]] inline std::string_view luaG_tostringview(lua_State* L_, int idx_) { size_t _len{ 0 }; char const* _str{ lua_tolstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaL_checkstringview(lua_State* L_, int idx_) +[[nodiscard]] inline std::string_view luaG_checkstringview(lua_State* L_, int idx_) { size_t _len{ 0 }; char const* _str{ luaL_checklstring(L_, idx_, &_len) }; return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view luaL_optstringview(lua_State* L_, int idx_, std::string_view const& default_) +[[nodiscard]] inline std::string_view luaG_optstringview(lua_State* L_, int idx_, std::string_view const& default_) { if (lua_isnoneornil(L_, idx_)) { return default_; @@ -242,12 +291,12 @@ LuaType luaG_getmodule(lua_State* L_, std::string_view const& name_); return std::string_view{ _str, _len }; } -[[nodiscard]] inline std::string_view lua_pushstringview(lua_State* L_, std::string_view const& str_) +[[nodiscard]] inline std::string_view luaG_pushstringview(lua_State* L_, std::string_view const& str_) { #if LUA_VERSION_NUM == 501 // lua_pushlstring doesn't return a value in Lua 5.1 lua_pushlstring(L_, str_.data(), str_.size()); - return lua_tostringview(L_, -1); + return luaG_tostringview(L_, -1); #else return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() }; #endif // LUA_VERSION_NUM > 501 diff --git a/src/deep.cpp b/src/deep.cpp index 76397ae..97442cf 100644 --- a/src/deep.cpp +++ b/src/deep.cpp @@ -240,7 +240,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, raise_luaL_error(errL_, "lanes receiving deep userdata should register the 'package' library"); } - std::ignore = lua_pushstringview(L_, _modname); // L_: DPC proxy metatable require() "module" + std::ignore = luaG_pushstringview(L_, _modname); // L_: DPC proxy metatable require() "module" if (luaG_getfield(L_, LUA_REGISTRYINDEX, LUA_LOADED_TABLE) != LuaType::TABLE) { // L_: DPC proxy metatable require() "module" _R._LOADED // no L.registry._LOADED; can this ever happen? lua_pop(L_, 6); // L_: @@ -259,7 +259,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, LuaError const _require_result{ lua_pcall(L_, 1, 0, 0) }; // L_: DPC proxy metatable error? if (_require_result != LuaError::OK) { // failed, raise the error in the proper state - std::ignore = lua_pushstringview(errL_, lua_tostringview(L_, -1)); + std::ignore = luaG_pushstringview(errL_, luaG_tostringview(L_, -1)); raise_lua_error(errL_); } } @@ -269,7 +269,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, } } STACK_CHECK(L_, 3); // L_: DPC proxy metatable - LUA_ASSERT(L_, lua_type_as_enum(L_, -2) == LuaType::USERDATA); + LUA_ASSERT(L_, luaG_type(L_, -2) == LuaType::USERDATA); LUA_ASSERT(L_, lua_istable(L_, -1)); lua_setmetatable(L_, -2); // L_: DPC proxy @@ -278,7 +278,7 @@ void DeepFactory::PushDeepProxy(DestState const L_, DeepPrelude* const prelude_, lua_pushvalue(L_, -2); // L_: DPC proxy deep proxy lua_rawset(L_, -4); // L_: DPC proxy lua_remove(L_, -2); // L_: proxy - LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA); + LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::USERDATA); STACK_CHECK(L_, 1); } diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp index 670b955..aa802d5 100644 --- a/src/intercopycontext.cpp +++ b/src/intercopycontext.cpp @@ -100,7 +100,7 @@ THE SOFTWARE. lua_pushvalue(L1, L1_i); // L1: ... v ... {} v lua_rawget(L1, -2); // L1: ... v ... {} "f.q.n" } - std::string_view _fqn{ lua_tostringview(L1, -1) }; + std::string_view _fqn{ luaG_tostringview(L1, -1) }; DEBUGSPEW_CODE(DebugSpew(Universe::Get(L1)) << "function [C] " << _fqn << std::endl); // popping doesn't invalidate the pointer since this is an interned string gotten from the lookup database lua_pop(L1, (mode == LookupMode::FromKeeper) ? 1 : 2); // L1: ... v ... @@ -113,7 +113,7 @@ THE SOFTWARE. lua_pushcfunction(L1, LG_nameof); // L1: ... v ... lane_name LG_nameof lua_pushvalue(L1, L1_i); // L1: ... v ... lane_name LG_nameof t lua_call(L1, 1, 2); // L1: ... v ... lane_name "type" "name"|nil - char const* _typewhat{ (lua_type(L1, -2) == LUA_TSTRING) ? lua_tostring(L1, -2) : luaL_typename(L1, -2) }; + char const* _typewhat{ (luaG_type(L1, -2) == LuaType::STRING) ? lua_tostring(L1, -2) : luaL_typename(L1, -2) }; // second return value can be nil if the table was not found // probable reason: the function was removed from the source Lua state before Lanes was required. char const *_what, *_gotchaA, *_gotchaB; @@ -124,7 +124,7 @@ THE SOFTWARE. } else { _gotchaA = ""; _gotchaB = ""; - _what = (lua_type(L1, -1) == LUA_TSTRING) ? lua_tostring(L1, -1) : luaL_typename(L1, -1); + _what = (luaG_type(L1, -1) == LuaType::STRING) ? lua_tostring(L1, -1) : luaL_typename(L1, -1); } raise_luaL_error(L1, "%s%s '%s' not found in %s origin transfer database.%s", _typewhat, _gotchaA, _what, _from ? _from : "main", _gotchaB); } @@ -196,7 +196,7 @@ void InterCopyContext::copy_func() const // not sure this could ever fail but for memory shortage reasons // last parameter is Lua 5.4-specific (no stripping) luaL_Buffer B{}; - if (lua504_dump(L1, buf_writer, &B, U->stripFunctions) != 0) { + if (luaG_dump(L1, buf_writer, &B, U->stripFunctions) != 0) { raise_luaL_error(getErrL(), "internal error: function dump failed."); } @@ -224,7 +224,7 @@ void InterCopyContext::copy_func() const } { - std::string_view const _bytecode{ lua_tostringview(L1, -1) }; // L1: ... b + std::string_view const _bytecode{ luaG_tostringview(L1, -1) }; // L1: ... b LUA_ASSERT(L1, !_bytecode.empty()); STACK_GROW(L2, 2); // Note: Line numbers seem to be taken precisely from the @@ -313,7 +313,7 @@ void InterCopyContext::lookup_native_func() const case LookupMode::ToKeeper: // push a sentinel closure that holds the lookup name as upvalue - std::ignore = lua_pushstringview(L2, _fqn); // L1: ... f ... L2: "f.q.n" + std::ignore = luaG_pushstringview(L2, _fqn); // L1: ... f ... L2: "f.q.n" lua_pushcclosure(L2, func_lookup_sentinel, 1); // L1: ... f ... L2: f break; @@ -322,7 +322,7 @@ void InterCopyContext::lookup_native_func() const kLookupRegKey.pushValue(L2); // L1: ... f ... L2: {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); - std::ignore = lua_pushstringview(L2, _fqn); // L1: ... f ... L2: {} "f.q.n" + std::ignore = luaG_pushstringview(L2, _fqn); // 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! @@ -388,7 +388,7 @@ void InterCopyContext::copy_cached_func() const // push a light userdata uniquely representing the function lua_pushlightuserdata(L2, _aspointer); // L2: ... {cache} ... p - //DEBUGSPEW_CODE(DebugSpew(U) << "<< ID: " << lua_tostringview(L2, -1) << " >>" << std::endl); + //DEBUGSPEW_CODE(DebugSpew(U) << "<< ID: " << luaG_tostringview(L2, -1) << " >>" << std::endl); lua_pushvalue(L2, -1); // L2: ... {cache} ... p p lua_rawget(L2, L2_cache_i); // L2: ... {cache} ... p function|nil|true @@ -433,7 +433,7 @@ void InterCopyContext::copy_cached_func() const case LookupMode::ToKeeper: // push a sentinel closure that holds the lookup name as upvalue - std::ignore = lua_pushstringview(L2, _fqn); // L1: ... t ... L2: "f.q.n" + std::ignore = luaG_pushstringview(L2, _fqn); // L1: ... t ... L2: "f.q.n" lua_pushcclosure(L2, table_lookup_sentinel, 1); // L1: ... t ... L2: f break; @@ -442,7 +442,7 @@ void InterCopyContext::copy_cached_func() const kLookupRegKey.pushValue(L2); // L1: ... t ... L2: {} STACK_CHECK(L2, 1); LUA_ASSERT(L1, lua_istable(L2, -1)); - std::ignore = lua_pushstringview(L2, _fqn); // L2: {} "f.q.n" + std::ignore = luaG_pushstringview(L2, _fqn); // 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! @@ -462,7 +462,7 @@ void InterCopyContext::copy_cached_func() const "%s: source table '%s' found as %s in %s destination transfer database.", _from ? _from : "main", _fqn, - lua_typename(L2, lua_type_as_enum(L2, -1)), + luaG_typename(L2, luaG_type(L2, -1)), _to ? _to : "main"); } lua_remove(L2, -2); // L1: ... t ... L2: t @@ -491,8 +491,8 @@ 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) { - std::string_view const _key{ lua_tostringview(L1, _key_i) }; + if (luaG_type(L1, _key_i) == LuaType::STRING) { + std::string_view const _key{ luaG_tostringview(L1, _key_i) }; size_t const _bufLen{ strlen(name) + _key.size() + 2 }; // +2 for separator dot and terminating 0 _valPath = static_cast(alloca(_bufLen)); sprintf(_valPath, "%s." STRINGVIEW_FMT, name, (int) _key.size(), _key.data()); @@ -504,15 +504,15 @@ void InterCopyContext::inter_copy_keyvaluepair() const 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) { + else if (luaG_type(L1, _key_i) == LuaType::NUMBER) { lua_Number const key{ lua_tonumber(L1, _key_i) }; _valPath = (char*) alloca(strlen(name) + 32 + 3); // +3 for [] and terminating 0 sprintf(_valPath, "%s[" LUA_NUMBER_FMT "]", name, key); - } else if (lua_type(L1, _key_i) == LUA_TLIGHTUSERDATA) { + } else if (luaG_type(L1, _key_i) == LuaType::LIGHTUSERDATA) { void* const key{ lua_touserdata(L1, _key_i) }; _valPath = (char*) alloca(strlen(name) + 16 + 5); // +5 for [U:] and terminating 0 sprintf(_valPath, "%s[U:%p]", name, key); - } else if (lua_type(L1, _key_i) == LUA_TBOOLEAN) { + } else if (luaG_type(L1, _key_i) == LuaType::BOOLEAN) { int const key{ lua_toboolean(L1, _key_i) }; _valPath = (char*) alloca(strlen(name) + 8); // +8 for [], 'false' and terminating 0 sprintf(_valPath, "%s[%s]", name, key ? "true" : "false"); @@ -597,7 +597,7 @@ void InterCopyContext::inter_copy_keyvaluepair() const // push a light userdata uniquely representing the table lua_pushlightuserdata(L2, const_cast(_p)); // L1: ... t ... L2: ... p - //DEBUGSPEW_CODE(DebugSpew(U) << "<< ID: " << lua_tostringview(L2, -1) << " >>" << std::endl); + //DEBUGSPEW_CODE(DebugSpew(U) << "<< ID: " << luaG_tostringview(L2, -1) << " >>" << std::endl); lua_rawget(L2, L2_cache_i); // L1: ... t ... L2: ... {cached|nil} bool const _not_found_in_cache{ lua_isnil(L2, -1) }; @@ -940,9 +940,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const [[nodiscard]] bool InterCopyContext::inter_copy_string() const { - std::string_view const _s{ lua_tostringview(L1, L1_i) }; + std::string_view const _s{ luaG_tostringview(L1, L1_i) }; DEBUGSPEW_CODE(DebugSpew(nullptr) << "'" << _s << "'" << std::endl); - std::ignore = lua_pushstringview(L2, _s); + std::ignore = luaG_pushstringview(L2, _s); return true; } @@ -1094,7 +1094,7 @@ namespace { DEBUGSPEW_CODE(DebugSpew(U) << "inter_copy_one()" << std::endl); DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U }); - LuaType _val_type{ lua_type_as_enum(L1, L1_i) }; + LuaType _val_type{ luaG_type(L1, L1_i) }; DEBUGSPEW_CODE(DebugSpew(U) << local::sLuaTypeNames[static_cast(_val_type)] << " " << local::sValueTypeNames[static_cast(vt)] << ": "); // Non-POD can be skipped if its metatable contains { __lanesignore = true } @@ -1187,7 +1187,7 @@ namespace { } const _onExit{ L2 }; STACK_CHECK_START_REL(L1, 0); - if (lua_type_as_enum(L1, L1_i) != LuaType::TABLE) { + if (luaG_type(L1, L1_i) != LuaType::TABLE) { lua_pushfstring(L1, "expected package as table, got %s", luaL_typename(L1, L1_i)); STACK_CHECK(L1, 1); // raise the error when copying from lane to lane, else just leave it on the stack to be raised later diff --git a/src/lane.cpp b/src/lane.cpp index f07403f..2a2030d 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -46,7 +46,7 @@ static LUAG_FUNC(get_debug_threadname) { Lane* const _lane{ ToLane(L_, 1) }; luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); - std::ignore = lua_pushstringview(L_, _lane->debugName); + std::ignore = luaG_pushstringview(L_, _lane->debugName); return 1; } @@ -104,7 +104,7 @@ static LUAG_FUNC(thread_join) Lane* const _lane{ ToLane(L_, 1) }; 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 + if (luaG_type(L_, 2) == LuaType::NUMBER) { // 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); @@ -300,7 +300,7 @@ static int thread_index_string(lua_State* L_) Lane* const _lane{ ToLane(L_, kSelf) }; LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: lane "key" - std::string_view const _keystr{ lua_tostringview(L_, kKey) }; + std::string_view const _keystr{ luaG_tostringview(L_, kKey) }; lua_settop(L_, 2); // keep only our original arguments on the stack if (_keystr == "status") { std::ignore = _lane->pushThreadStatus(L_); // L_: lane "key" "" @@ -331,11 +331,11 @@ static LUAG_FUNC(thread_index) Lane* const _lane{ ToLane(L_, kSelf) }; LUA_ASSERT(L_, lua_gettop(L_) == 2); - switch (lua_type(L_, kKey)) { - case LUA_TNUMBER: + switch (luaG_type(L_, kKey)) { + case LuaType::NUMBER: return thread_index_number(L_); // stack modification is undefined, returned value is at the top - case LUA_TSTRING: + case LuaType::STRING: return thread_index_string(L_); // stack modification is undefined, returned value is at the top default: // unknown key @@ -345,7 +345,7 @@ static LUAG_FUNC(thread_index) lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k lua_concat(L_, 2); // L_: mt error "Unknown key: " lua_call(L_, 1, 0); // error( "Unknown key: " .. key) -> doesn't return // L_: mt - raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName.data(), lua_typename(L_, lua_type(L_, kKey))); + raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName.data(), luaG_typename(L_, luaG_type(L_, kKey))); } } @@ -498,7 +498,7 @@ static void push_stack_trace(lua_State* L_, Lane::ErrorTraceLevel errorTraceLeve // For cancellation the error message is kCancelError, and a stack trace isn't placed // For other errors, the message can be whatever was thrown, and we should have a stack trace table - LUA_ASSERT(L_, lua_type(L_, 1 + stk_base_) == (kCancelError.equals(L_, stk_base_) ? LUA_TNIL : LUA_TTABLE)); + LUA_ASSERT(L_, luaG_type(L_, 1 + stk_base_) == (kCancelError.equals(L_, stk_base_) ? LuaType::NIL : LuaType::TABLE)); // Just leaving the stack trace table on the stack is enough to get it through to the master. } else { // any kind of error can be thrown with error(), or through a lane/linda cancellation @@ -510,7 +510,7 @@ static void push_stack_trace(lua_State* L_, Lane::ErrorTraceLevel errorTraceLeve case LuaError::ERRERR: // error while running the error handler (if any, for example an out-of-memory condition) default: // the Lua core provides a string error message in those situations - LUA_ASSERT(L_, (lua_gettop(L_) == stk_base_) && (lua_type(L_, stk_base_) == LUA_TSTRING)); + LUA_ASSERT(L_, (lua_gettop(L_) == stk_base_) && (luaG_type(L_, stk_base_) == LuaType::STRING)); break; } } @@ -551,7 +551,7 @@ static void push_stack_trace(lua_State* L_, Lane::ErrorTraceLevel errorTraceLeve LUA_ASSERT(L_, lua_isfunction(L_, -1)); if (lua_rc_ != LuaError::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); - //std::string_view const _err_msg{ lua_tostringview(L_, 1) }; + //std::string_view const _err_msg{ luaG_tostringview(L_, 1) }; lua_pushvalue(L_, 1); // L_: ... 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) { @@ -683,7 +683,7 @@ static void lane_main(Lane* lane_) // in case of error and if it exists, fetch stack trace from registry and push it push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] - DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : lua_typename(_L, lua_type(_L, 1))) << ")" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : luaG_typename(_L, luaG_type(_L, 1))) << ")" << std::endl); // Call finalizers, if the script has set them up. // LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; @@ -745,7 +745,7 @@ static void lane_main(Lane* lane_) lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil if (!lua_isnil(L_, -1)) { lua_remove(L_, -2); // L_: ud gc_cb|nil - std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: ud gc_cb name + std::ignore = luaG_pushstringview(L_, _lane->debugName); // L_: ud gc_cb name _have_gc_cb = true; } else { lua_pop(L_, 2); // L_: ud @@ -810,7 +810,7 @@ void Lane::changeDebugName(int const nameIdx_) // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... kLaneNameRegKey.setValue(L, [idx = _nameIdx](lua_State* L_) { lua_pushvalue(L_, idx); }); // L: ... "name" ... // keep a direct pointer on the string - debugName = lua_tostringview(L, _nameIdx); + debugName = luaG_tostringview(L, _nameIdx); if constexpr (HAVE_DECODA_SUPPORT()) { // to see VM name in Decoda debugger Virtual Machine window lua_pushvalue(L, _nameIdx); // L: ... "name" ... "name" @@ -897,7 +897,7 @@ void Lane::PushMetatable(lua_State* L_) std::string_view const _str{ threadStatusString() }; LUA_ASSERT(L_, !_str.empty()); - return lua_pushstringview(L_, _str); + return luaG_pushstringview(L_, _str); } // ################################################################################################# @@ -907,7 +907,7 @@ void Lane::PushMetatable(lua_State* L_) std::string_view const _str{ errorTraceLevelString() }; LUA_ASSERT(L_, !_str.empty()); - return lua_pushstringview(L_, _str); + return luaG_pushstringview(L_, _str); } // ################################################################################################# @@ -922,7 +922,7 @@ void Lane::securizeDebugName(lua_State* L_) LUA_ASSERT(L_, lua_istable(L_, -1)); // we don't care about the actual key, so long as it's unique and can't collide with anything. lua_newtable(L_); // L_: lane ... {uv} {} - debugName = lua_pushstringview(L_, debugName); // L_: lane ... {uv} {} name + debugName = luaG_pushstringview(L_, debugName); // L_: lane ... {uv} {} name lua_rawset(L_, -3); // L_: lane ... {uv} lua_pop(L_, 1); // L_: lane STACK_CHECK(L_, 0); diff --git a/src/lanes.cpp b/src/lanes.cpp index 25f44d9..a36e051 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -170,7 +170,7 @@ LUAG_FUNC(sleep) lua_pushcfunction(L_, LG_linda_receive); // L_: duration|nil receive() STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments _U->timerLinda->push(L_); // L_: duration|nil receive() timerLinda - if (lua_tostringview(L_, 1) == "indefinitely") { + if (luaG_tostringview(L_, 1) == "indefinitely") { lua_pushnil(L_); // L_: duration? receive() timerLinda nil } else if (lua_isnoneornil(L_, 1)) { lua_pushnumber(L_, 0); // L_: duration? receive() timerLinda 0 @@ -180,7 +180,7 @@ LUAG_FUNC(sleep) else { lua_pushnumber(L_, lua_tonumber(L_, 1)); // L_: duration? receive() timerLinda duration } - std::ignore = lua_pushstringview(L_, "ac100de1-a696-4619-b2f0-a26de9d58ab8"); // L_: duration? receive() timerLinda duration key + std::ignore = luaG_pushstringview(L_, "ac100de1-a696-4619-b2f0-a26de9d58ab8"); // L_: duration? receive() timerLinda duration key STACK_CHECK(L_, 3); // 3 arguments ready lua_call(L_, 3, LUA_MULTRET); // timerLinda:receive(duration,key) // L_: duration? result... return lua_gettop(L_) - 1; @@ -194,7 +194,7 @@ LUAG_FUNC(sleep) // upvalue[1]: _G.require LUAG_FUNC(require) { - std::string_view const _name{ lua_tostringview(L_, 1) }; // L_: "name" ... + std::string_view const _name{ luaG_tostringview(L_, 1) }; // L_: "name" ... int const _nargs{ lua_gettop(L_) }; DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) }); STACK_CHECK_START_REL(L_, 0); @@ -216,8 +216,8 @@ LUAG_FUNC(require) // lanes.register( "modname", module) LUAG_FUNC(register) { - std::string_view const _name{ luaL_checkstringview(L_, 1) }; - LuaType const _mod_type{ lua_type_as_enum(L_, 2) }; + std::string_view const _name{ luaG_checkstringview(L_, 1) }; + LuaType const _mod_type{ luaG_type(L_, 2) }; // 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"); @@ -266,7 +266,7 @@ LUAG_FUNC(lane_new) Universe* const _U{ Universe::Get(L_) }; DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl); - std::optional _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(lua_tostringview(L_, kLibsIdx)) }; + std::optional _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaG_tostringview(L_, kLibsIdx)) }; lua_State* const _L2{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2: STACK_CHECK_START_REL(_L2, 0); @@ -345,11 +345,11 @@ LUAG_FUNC(lane_new) lua_State* _L2{ lane->L }; STACK_CHECK_START_REL(_L2, 0); int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; - std::string_view const _debugName{ (_name_idx > 0) ? lua_tostringview(L, _name_idx) : std::string_view{} }; + std::string_view const _debugName{ (_name_idx > 0) ? luaG_tostringview(L, _name_idx) : std::string_view{} }; if (!_debugName.empty()) { if (_debugName != "auto") { - std::ignore = lua_pushstringview(_L2, _debugName); // L: ... lane L2: "" + std::ignore = luaG_pushstringview(_L2, _debugName); // L: ... lane L2: "" } else { lua_Debug _ar; lua_pushvalue(L, 1); // L: ... lane func @@ -413,17 +413,17 @@ LUAG_FUNC(lane_new) DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl); DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); // should not happen, was checked in lanes.lua before calling lane_new() - if (lua_type(L_, _required_idx) != LUA_TTABLE) { + if (luaG_type(L_, _required_idx) != LuaType::TABLE) { raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, _required_idx)); } lua_pushnil(L_); // L_: [fixed] args... nil L2: while (lua_next(L_, _required_idx) != 0) { // L_: [fixed] args... n "modname" L2: - if (lua_type(L_, -1) != LUA_TSTRING || lua_type(L_, -2) != LUA_TNUMBER || lua_tonumber(L_, -2) != _nbRequired) { + if (luaG_type(L_, -1) != LuaType::STRING || luaG_type(L_, -2) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) { raise_luaL_error(L_, "required module list should be a list of strings"); } else { // require the module in the target state, and populate the lookup table there too - std::string_view const _name{ lua_tostringview(L_, -1) }; + std::string_view const _name{ luaG_tostringview(L_, -1) }; DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl); // require the module in the target lane @@ -432,7 +432,7 @@ LUAG_FUNC(lane_new) lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first"); } else { - std::ignore = lua_pushstringview(_L2, _name); // L_: [fixed] args... n "modname" L2: require() name + std::ignore = luaG_pushstringview(_L2, _name); // L_: [fixed] args... n "modname" L2: require() name LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) }; // L_: [fixed] args... n "modname" L2: ret/errcode if (_rc != LuaError::OK) { // propagate error to main state if any @@ -481,7 +481,7 @@ LUAG_FUNC(lane_new) // Lane main function [[maybe_unused]] int const errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? - LuaType const _func_type{ lua_type_as_enum(L_, kFuncIdx) }; + LuaType const _func_type{ luaG_type(L_, kFuncIdx) }; if (_func_type == LuaType::FUNCTION) { DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl); DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); @@ -498,7 +498,7 @@ LUAG_FUNC(lane_new) raise_luaL_error(L_, "error when parsing lane function code"); } } else { - raise_luaL_error(L_, "Expected function, got %s", lua_typename(L_, _func_type)); + raise_luaL_error(L_, "Expected function, got %s", luaG_typename(L_, _func_type)); } STACK_CHECK(L_, 0); STACK_CHECK(_L2, errorHandlerCount + 1); @@ -655,8 +655,8 @@ LUAG_FUNC(configure) Universe* _U{ Universe::Get(L_) }; bool const _from_master_state{ _U == nullptr }; - std::string_view const _name{ luaL_checkstringview(L_, lua_upvalueindex(1)) }; - LUA_ASSERT(L_, lua_type(L_, 1) == LUA_TTABLE); + std::string_view const _name{ luaG_checkstringview(L_, lua_upvalueindex(1)) }; + LUA_ASSERT(L_, luaG_type(L_, 1) == LuaType::TABLE); STACK_GROW(L_, 4); STACK_CHECK_START_ABS(L_, 1); // L_: settings @@ -666,7 +666,7 @@ LUAG_FUNC(configure) if (_U == nullptr) { // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... - kLaneNameRegKey.setValue(L_, [](lua_State* L_) { std::ignore = lua_pushstringview(L_, "main"); }); + kLaneNameRegKey.setValue(L_, [](lua_State* L_) { std::ignore = luaG_pushstringview(L_, "main"); }); // create the universe _U = Universe::Create(L_); // L_: settings universe diff --git a/src/linda.cpp b/src/linda.cpp index b623d29..bd3026d 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -43,7 +43,7 @@ THE SOFTWARE. static void check_key_types(lua_State* L_, int start_, int end_) { for (int _i{ start_ }; _i <= end_; ++_i) { - LuaType const t{ lua_type_as_enum(L_, _i) }; + LuaType const t{ luaG_type(L_, _i) }; switch (t) { case LuaType::BOOLEAN: case LuaType::NUMBER: @@ -79,10 +79,10 @@ template { Linda* const _linda{ ToLinda(L_, idx_) }; if (_linda != nullptr) { - std::ignore = lua_pushstringview(L_, "Linda: "); + std::ignore = luaG_pushstringview(L_, "Linda: "); std::string_view const _lindaName{ _linda->getName() }; if (!_lindaName.empty()) { - std::ignore = lua_pushstringview(L_, _lindaName); + std::ignore = luaG_pushstringview(L_, _lindaName); } else { lua_pushfstring(L_, "%p", _linda); } @@ -247,7 +247,7 @@ void Linda::setName(std::string_view const& name_) LUAG_FUNC(linda_cancel) { Linda* const _linda{ ToLinda(L_, 1) }; - std::string_view const _who{ luaL_optstringview(L_, 2, "both") }; + std::string_view const _who{ luaG_optstringview(L_, 2, "both") }; // make sure we got 2 arguments: the linda and the cancellation mode luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments"); @@ -412,7 +412,7 @@ LUAG_FUNC(linda_limit) _pushed = keeper_call(_K->L, KEEPER_API(limit), L_, _linda, 2); LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 0 || _pushed.value() == 1)); // no error, optional boolean value saying if we should wake blocked writer threads if (_pushed.value() == 1) { - LUA_ASSERT(L_, lua_type(L_, -1) == LUA_TBOOLEAN && lua_toboolean(L_, -1) == 1); + LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::BOOLEAN && lua_toboolean(L_, -1) == 1); _linda->readHappened.notify_all(); // To be done from within the 'K' locking area } } else { // linda is cancelled @@ -446,7 +446,7 @@ LUAG_FUNC(linda_receive) int _key_i{ 2 }; // index of first key, if timeout not there 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 + if (luaG_type(L_, 2) == LuaType::NUMBER) { // 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); @@ -559,7 +559,7 @@ LUAG_FUNC(linda_receive) if (_nbPushed == 0) { // not enough data in the linda slot to fulfill the request, return nil, "timeout" lua_pushnil(L_); - std::ignore = lua_pushstringview(L_, "timeout"); + std::ignore = luaG_pushstringview(L_, "timeout"); return 2; } return _nbPushed; @@ -600,7 +600,7 @@ LUAG_FUNC(linda_send) int _key_i{ 2 }; // index of first key, if timeout not there 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 + if (luaG_type(L_, 2) == LuaType::NUMBER) { // 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); @@ -742,7 +742,7 @@ LUAG_FUNC(linda_set) } if (_pushed.value() == 1) { // the key was full, but it is no longer the case, tell writers they should wake - LUA_ASSERT(L_, lua_type(L_, -1) == LUA_TBOOLEAN && lua_toboolean(L_, -1) == 1); + LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::BOOLEAN && lua_toboolean(L_, -1) == 1); _linda->readHappened.notify_all(); // To be done from within the 'K' locking area } } @@ -826,7 +826,7 @@ LUAG_FUNC(linda) int _groupIdx{}; luaL_argcheck(L_, _top <= 2, _top, "too many arguments"); if (_top == 1) { - LuaType const _t{ lua_type_as_enum(L_, 1) }; + LuaType const _t{ luaG_type(L_, 1) }; int const _nameIdx{ (_t == LuaType::STRING) ? 1 : 0 }; _groupIdx = (_t == LuaType::NUMBER) ? 1 : 0; luaL_argcheck(L_, _nameIdx || _groupIdx, 1, "wrong parameter (should be a string or a number)"); diff --git a/src/lindafactory.cpp b/src/lindafactory.cpp index 94dd3ab..d99d546 100644 --- a/src/lindafactory.cpp +++ b/src/lindafactory.cpp @@ -111,15 +111,15 @@ DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* L_) const break; case 1: // 1 parameter, either a name or a group - if (lua_type(L_, -1) == LUA_TSTRING) { - _linda_name = lua_tostringview(L_, -1); + if (luaG_type(L_, -1) == LuaType::STRING) { + _linda_name = luaG_tostringview(L_, -1); } else { _linda_group = LindaGroup{ static_cast(lua_tointeger(L_, -1)) }; } break; case 2: // 2 parameters, a name and group, in that order - _linda_name = lua_tostringview(L_, -2); + _linda_name = luaG_tostringview(L_, -2); _linda_group = LindaGroup{ static_cast(lua_tointeger(L_, -1)) }; break; } diff --git a/src/nameof.cpp b/src/nameof.cpp index 7614577..8611239 100644 --- a/src/nameof.cpp +++ b/src/nameof.cpp @@ -59,8 +59,8 @@ THE SOFTWARE. // scan table contents lua_pushnil(L_); // L_: o "r" {c} {fqn} ... {?} nil while (lua_next(L_, -2)) { // L_: o "r" {c} {fqn} ... {?} k v - // std::string_view const _strKey{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : "" }; // only for debugging - // lua_Number const numKey = (lua_type(L_, -2) == LUA_TNUMBER) ? lua_tonumber(L_, -2) : -6666; // only for debugging + // std::string_view const _strKey{ (luaG_type(L_, -2) == LuaType::STRING) ? luaG_tostringview(L_, -2) : "" }; // only for debugging + // lua_Number const numKey = (luaG_type(L_, -2) == LuaType::NUMBER) ? lua_tonumber(L_, -2) : -6666; // only for debugging STACK_CHECK(L_, 2); // append key name to fqn stack ++depth_; @@ -79,11 +79,11 @@ THE SOFTWARE. STACK_CHECK(L_, 0); break; } - switch (lua_type(L_, -1)) { // L_: o "r" {c} {fqn} ... {?} k v + switch (luaG_type(L_, -1)) { // L_: o "r" {c} {fqn} ... {?} k v default: // nil, boolean, light userdata, number and string aren't identifiable break; - case LUA_TTABLE: // L_: o "r" {c} {fqn} ... {?} k {} + case LuaType::TABLE: // L_: o "r" {c} {fqn} ... {?} k {} STACK_CHECK(L_, 2); shortest_ = DiscoverObjectNameRecur(L_, shortest_, depth_); // search in the table's metatable too @@ -102,11 +102,11 @@ THE SOFTWARE. STACK_CHECK(L_, 2); break; - case LUA_TTHREAD: // L_: o "r" {c} {fqn} ... {?} k T + case LuaType::THREAD: // L_: o "r" {c} {fqn} ... {?} k T // TODO: explore the thread's stack frame looking for our culprit? break; - case LUA_TUSERDATA: // L_: o "r" {c} {fqn} ... {?} k U + case LuaType::USERDATA: // L_: o "r" {c} {fqn} ... {?} k U STACK_CHECK(L_, 2); // search in the object's metatable (some modules are built that way) if (lua_getmetatable(L_, -1)) { // L_: o "r" {c} {fqn} ... {?} k U {mt} @@ -172,7 +172,7 @@ LUAG_FUNC(nameof) } // nil, boolean, light userdata, number and string aren't identifiable - if (lua_type(L_, 1) < LUA_TTABLE) { + if (luaG_type(L_, 1) < LuaType::TABLE) { lua_pushstring(L_, luaL_typename(L_, 1)); // L_: o "type" lua_insert(L_, -2); // L_: "type" o return 2; diff --git a/src/state.cpp b/src/state.cpp index 7585132..af071e8 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -184,9 +184,9 @@ namespace state { STACK_CHECK(L_, 1); // capture error and raise it in caller state std::string_view const _stateType{ mode_ == LookupMode::LaneBody ? "lane" : "keeper" }; - std::ignore = lua_pushstringview(L_, _stateType); // L_: on_state_create() "" + std::ignore = luaG_pushstringview(L_, _stateType); // L_: on_state_create() "" if (lua_pcall(L_, 1, 0, 0) != LUA_OK) { - raise_luaL_error(from_, "%s failed: \"%s\"", kOnStateCreate, lua_isstring(L_, -1) ? lua_tostring(L_, -1) : lua_typename(L_, lua_type(L_, -1))); + raise_luaL_error(from_, "%s failed: \"%s\"", kOnStateCreate, lua_isstring(L_, -1) ? lua_tostring(L_, -1) : luaG_typename(L_, luaG_type(L_, -1))); } STACK_CHECK(L_, 0); } @@ -365,19 +365,19 @@ namespace state { kLookupRegKey.pushValue(_L); // L: {} lua_pushnil(_L); // L: {} nil while (lua_next(_L, -2)) { // L: {} k v - std::ignore = lua_pushstringview(_L, "["); // L: {} k v "[" + std::ignore = luaG_pushstringview(_L, "["); // L: {} k v "[" lua_getglobal(_L, "tostring"); // L: {} k v "[" tostring lua_pushvalue(_L, -4); // L: {} k v "[" tostring k lua_call(_L, 1, 1); // L: {} k v "[" 'k' - std::ignore = lua_pushstringview(_L, "] = "); // L: {} k v "[" 'k' "] = " + std::ignore = luaG_pushstringview(_L, "] = "); // L: {} k v "[" 'k' "] = " lua_getglobal(_L, "tostring"); // L: {} k v "[" 'k' "] = " tostring lua_pushvalue(_L, -5); // L: {} k v "[" 'k' "] = " tostring v lua_call(_L, 1, 1); // L: {} k v "[" 'k' "] = " 'v' lua_concat(_L, 4); // L: {} k v "[k] = v" - DEBUGSPEW_CODE(DebugSpew(U_) << lua_tostringview(_L, -1) << std::endl); + DEBUGSPEW_CODE(DebugSpew(U_) << luaG_tostringview(_L, -1) << std::endl); lua_pop(_L, 2); // L: {} k } // lua_next() // L: {} lua_pop(_L, 1); // L: diff --git a/src/tools.cpp b/src/tools.cpp index e7d1775..302a0df 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -78,7 +78,7 @@ static constexpr int kWriterReturnCode{ 666 }; // the provided writer fails with code kWriterReturnCode // therefore, anytime we get kWriterReturnCode, this means that lua_dump() attempted a dump // all other cases mean this is either a C or LuaJIT-fast function - int const _dumpres{ lua504_dump(L_, dummy_writer, nullptr, 0) }; + int const _dumpres{ luaG_dump(L_, dummy_writer, nullptr, 0) }; lua_pop(L_, _mustpush); if (_dumpres == kWriterReturnCode) { return FuncSubType::Bytecode; @@ -111,7 +111,7 @@ namespace tools { // &b is popped at that point (-> replaced by the result) luaL_pushresult(&_b); // L_: ... {} ... "" STACK_CHECK(L_, 1); - return lua_tostringview(L_, -1); + return luaG_tostringview(L_, -1); } } // namespace tools @@ -141,10 +141,10 @@ static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_) // first, raise an error if the function is already known lua_pushvalue(L_, -1); // L_: ... {bfc} k o o lua_rawget(L_, _dest); // L_: ... {bfc} k o name? - std::string_view const _prevName{ lua_tostringview(L_, -1) }; // nullptr if we got nil (first encounter of this object) + std::string_view const _prevName{ luaG_tostringview(L_, -1) }; // nullptr if we got nil (first encounter of this object) // push name in fqn stack (note that concatenation will crash if name is a not string or a number) lua_pushvalue(L_, -3); // L_: ... {bfc} k o name? k - LUA_ASSERT(L_, lua_type(L_, -1) == LUA_TNUMBER || lua_type(L_, -1) == LUA_TSTRING); + LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::NUMBER || luaG_type(L_, -1) == LuaType::STRING); ++depth_; lua_rawseti(L_, _fqn, depth_); // L_: ... {bfc} k o name? // generate name @@ -157,7 +157,7 @@ static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_) // Therefore, when we encounter an object for which a name was previously registered, we need to select the a single name // 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.empty() && ((_prevName.size() < _newName.size()) || (_prevName <= _newName))) { - DEBUGSPEW_CODE(DebugSpew(_U) << lua_typename(L_, lua_type(L_, -3)) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, luaG_type(L_, -3)) << " '" << _newName << "' remains named '" << _prevName << "'" << std::endl); // the previous name is 'smaller' than the one we just generated: keep it! lua_pop(L_, 3); // L_: ... {bfc} k } else { @@ -171,7 +171,7 @@ static void update_lookup_entry(lua_State* L_, int ctxBase_, int depth_) } else { lua_remove(L_, -2); // L_: ... {bfc} k o "f.q.n" } - DEBUGSPEW_CODE(DebugSpew(_U) << lua_typename(L_, lua_type(L_, -2)) << " '" << _newName << "'" << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << luaG_typename(L_, luaG_type(L_, -2)) << " '" << _newName << "'" << std::endl); // prepare the stack for database feed lua_pushvalue(L_, -1); // L_: ... {bfc} k o "f.q.n" "f.q.n" lua_pushvalue(L_, -3); // L_: ... {bfc} k o "f.q.n" "f.q.n" o @@ -207,7 +207,7 @@ static void populate_func_lookup_table_recur(lua_State* L_, int dbIdx_, int i_, STACK_CHECK_START_REL(L_, 0); // L_: ... {i_} // if object is a userdata, replace it by its metatable - if (lua_type(L_, i_) == LUA_TUSERDATA) { + if (luaG_type(L_, i_) == LuaType::USERDATA) { lua_getmetatable(L_, i_); // L_: ... {i_} mt lua_replace(L_, i_); // L_: ... {i_} } @@ -236,7 +236,7 @@ static void populate_func_lookup_table_recur(lua_State* L_, int dbIdx_, int i_, lua_pushnil(L_); // L_: ... {i_} {bfc} nil while (lua_next(L_, i_) != 0) { // L_: ... {i_} {bfc} k v // just for debug, not actually needed - // std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : "not a string" }; + // std::string_view const _key{ (luaG_type(L_, -2) == LuaType::STRING) ? luaG_tostringview(L_, -2) : "not a string" }; // subtable: process it recursively if (lua_istable(L_, -1)) { // L_: ... {i_} {bfc} k {} // increment visit count to make sure we will actually scan it at this recursive level @@ -266,13 +266,13 @@ static void populate_func_lookup_table_recur(lua_State* L_, int dbIdx_, int i_, ++depth_; lua_pushnil(L_); // L_: ... {i_} {bfc} nil while (lua_next(L_, breadthFirstCache) != 0) { // L_: ... {i_} {bfc} k {} - DEBUGSPEW_CODE(std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : std::string_view{ "" } }); + DEBUGSPEW_CODE(std::string_view const _key{ (luaG_type(L_, -2) == LuaType::STRING) ? luaG_tostringview(L_, -2) : std::string_view{ "" } }); DEBUGSPEW_CODE(DebugSpew(_U) << "table '"<< _key <<"'" << std::endl); DEBUGSPEW_CODE(DebugSpewIndentScope _scope2{ _U }); // un-visit this table in case we do need to process it lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} lua_rawget(L_, _cache); // L_: ... {i_} {bfc} k {} n - LUA_ASSERT(L_, lua_type(L_, -1) == LUA_TNUMBER); + LUA_ASSERT(L_, luaG_type(L_, -1) == LuaType::NUMBER); _visit_count = lua_tointeger(L_, -1) - 1; lua_pop(L_, 1); // L_: ... {i_} {bfc} k {} lua_pushvalue(L_, -1); // L_: ... {i_} {bfc} k {} {} @@ -317,23 +317,23 @@ namespace tools { int const _dbIdx{ lua_gettop(L_) }; STACK_CHECK(L_, 1); LUA_ASSERT(L_, lua_istable(L_, -1)); - if (lua_type(L_, _in_base) == LUA_TFUNCTION) { // for example when a module is a simple function + if (luaG_type(L_, _in_base) == LuaType::FUNCTION) { // for example when a module is a simple function if (_name.empty()) { _name = "nullptr"; } lua_pushvalue(L_, _in_base); // L_: {} f - std::ignore = lua_pushstringview(L_, _name); // L_: {} f name_ + std::ignore = luaG_pushstringview(L_, _name); // L_: {} f name_ lua_rawset(L_, -3); // L_: {} - std::ignore = lua_pushstringview(L_, _name); // L_: {} name_ + std::ignore = luaG_pushstringview(L_, _name); // L_: {} name_ lua_pushvalue(L_, _in_base); // L_: {} name_ f lua_rawset(L_, -3); // L_: {} lua_pop(L_, 1); // L_: - } else if (lua_type(L_, _in_base) == LUA_TTABLE) { + } else if (luaG_type(L_, _in_base) == LuaType::TABLE) { lua_newtable(L_); // L_: {} {fqn} int _startDepth{ 0 }; if (!_name.empty()) { STACK_CHECK(L_, 2); - std::ignore = lua_pushstringview(L_, _name); // L_: {} {fqn} "name" + std::ignore = luaG_pushstringview(L_, _name); // L_: {} {fqn} "name" // generate a name, and if we already had one name, keep whichever is the shorter lua_pushvalue(L_, _in_base); // L_: {} {fqn} "name" t update_lookup_entry(L_, _dbIdx, _startDepth); // L_: {} {fqn} "name" @@ -348,7 +348,7 @@ namespace tools { lua_pop(L_, 3); // L_: } else { lua_pop(L_, 1); // L_: - raise_luaL_error(L_, "unsupported module type %s", lua_typename(L_, lua_type(L_, _in_base))); + raise_luaL_error(L_, "unsupported module type %s", luaG_typename(L_, luaG_type(L_, _in_base))); } STACK_CHECK(L_, 0); } @@ -366,7 +366,7 @@ namespace tools { +[](lua_State* L_) { int const _args{ lua_gettop(L_) }; // L_: args... - //[[maybe_unused]] std::string_view const _modname{ luaL_checkstringview(L_, 1) }; + //[[maybe_unused]] std::string_view const _modname{ luaG_checkstringview(L_, 1) }; STACK_GROW(L_, 1); diff --git a/src/tracker.cpp b/src/tracker.cpp index d42eb35..618dc37 100644 --- a/src/tracker.cpp +++ b/src/tracker.cpp @@ -92,7 +92,7 @@ void LaneTracker::tracking_add(Lane* lane_) while (_lane != TRACKING_END) { // insert a { name='', status='' } tuple, so that several lanes with the same name can't clobber each other lua_createtable(L_, 0, 2); // L_: {} {} - std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name" + std::ignore = luaG_pushstringview(L_, _lane->debugName); // L_: {} {} "name" lua_setfield(L_, -2, "name"); // L_: {} {} std::ignore = _lane->pushThreadStatus(L_); // L_: {} {} "status" lua_setfield(L_, -2, "status"); // L_: {} {} diff --git a/src/universe.cpp b/src/universe.cpp index 5a52f7e..b4d54b2 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -136,7 +136,7 @@ Universe::Universe() // Initialize 'timerLinda'; a common Linda object shared by all states lua_pushcfunction(L_, LG_linda); // L_: settings lanes.linda - std::ignore = lua_pushstringview(L_, "lanes-timer"); // L_: settings lanes.linda "lanes-timer" + std::ignore = luaG_pushstringview(L_, "lanes-timer"); // L_: settings lanes.linda "lanes-timer" lua_pushinteger(L_, 0); // L_: settings lanes.linda "lanes-timer" 0 lua_call(L_, 2, 1); // L_: settings linda STACK_CHECK(L_, 1); @@ -195,7 +195,7 @@ void Universe::initializeAllocatorFunction(lua_State* const L_) // when we transfer the config table in newly created Lua states lua_pushnil(L_); // L_: settings allocator nil lua_setfield(L_, -3, "allocator"); // L_: settings allocator - } else if (lua_type(L_, -1) == LUA_TSTRING) { // should be "protected" + } else if (luaG_type(L_, -1) == LuaType::STRING) { // should be "protected" LUA_ASSERT(L_, strcmp(lua_tostring(L_, -1), "protected") == 0); // set the original allocator to call from inside protection by the mutex protectedAllocator.initFrom(L_); @@ -211,7 +211,7 @@ void Universe::initializeAllocatorFunction(lua_State* const L_) STACK_CHECK(L_, 1); std::ignore = luaG_getfield(L_, -1, "internal_allocator"); // L_: settings "libc"|"allocator" - std::string_view const _allocator{ lua_tostringview(L_, -1) }; + std::string_view const _allocator{ luaG_tostringview(L_, -1) }; if (_allocator == "libc") { internalAllocator = AllocatorDefinition{ libc_lua_Alloc, nullptr }; } else if (provideAllocator == luaG_provide_protected_allocator) { @@ -236,7 +236,7 @@ int Universe::InitializeFinalizer(lua_State* const L_) // make sure we are only called from the Master Lua State! kUniverseFullRegKey.pushValue(L_); // L_: f U - if (lua_type_as_enum(L_, -1) != LuaType::USERDATA) { + if (luaG_type(L_, -1) != LuaType::USERDATA) { raise_luaL_error(L_, "lanes.%s called from inside a lane", kFinally); } lua_pop(L_, 1); // L_: f @@ -318,7 +318,7 @@ void Universe::terminateFreeRunningLanes(lua_State* const L_, lua_Duration const LUAG_FUNC(universe_gc) { lua_Duration const _shutdown_timeout{ lua_tonumber(L_, lua_upvalueindex(1)) }; - std::string_view const _op_string{ lua_tostringview(L_, lua_upvalueindex(2)) }; + std::string_view const _op_string{ luaG_tostringview(L_, lua_upvalueindex(2)) }; STACK_CHECK_START_ABS(L_, 1); Universe* const _U{ lua_tofulluserdata(L_, 1) }; // L_: U _U->terminateFreeRunningLanes(L_, _shutdown_timeout, which_cancel_op(_op_string)); -- cgit v1.2.3-55-g6feb