From 82ec0e4de089074aae26ab72040523fa7d21557d Mon Sep 17 00:00:00 2001 From: Benoit Germain Date: Mon, 27 May 2024 11:44:58 +0200 Subject: namespace tweaks --- src/lane.cpp | 84 +++++++++++++++++++++++++++++++------------------------- src/lanes.cpp | 30 ++++++++++---------- src/lanesconf.h | 3 +- src/linda.cpp | 38 +++++++++++++------------ src/state.cpp | 46 +++++++++++++++---------------- src/tools.cpp | 4 +-- src/universe.cpp | 4 +-- 7 files changed, 111 insertions(+), 98 deletions(-) (limited to 'src') diff --git a/src/lane.cpp b/src/lane.cpp index 7650d6b..f220bce 100644 --- a/src/lane.cpp +++ b/src/lane.cpp @@ -354,32 +354,38 @@ static LUAG_FUNC(thread_index) // ################################################################################################# #if USE_DEBUG_SPEW() -// can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( -// LUA_ERRERR doesn't have the same value -struct errcode_name -{ - LuaError code; - char const* name; -}; - -static struct errcode_name s_errcodes[] = { - { LuaError::OK, "LUA_OK" }, - { LuaError::YIELD, "LUA_YIELD" }, - { LuaError::ERRRUN, "LUA_ERRRUN" }, - { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" }, - { LuaError::ERRMEM, "LUA_ERRMEM" }, - { LuaError::ERRGCMM, "LUA_ERRGCMM" }, - { LuaError::ERRERR, "LUA_ERRERR" }, -}; -static char const* get_errcode_name(LuaError _code) -{ - for (errcode_name const& _entry : s_errcodes) { - if (_entry.code == _code) { - return _entry.name; +namespace { + // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( + // LUA_ERRERR doesn't have the same value + struct errcode_name + { + LuaError code; + std::string_view const name; + }; + + namespace local { + + static struct errcode_name sErrCodes[] = { + { LuaError::OK, "LUA_OK" }, + { LuaError::YIELD, "LUA_YIELD" }, + { LuaError::ERRRUN, "LUA_ERRRUN" }, + { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" }, + { LuaError::ERRMEM, "LUA_ERRMEM" }, + { LuaError::ERRGCMM, "LUA_ERRGCMM" }, + { LuaError::ERRERR, "LUA_ERRERR" }, + }; + } // namespace local + + static std::string_view GetErrcodeName(LuaError _code) noexcept + { + for (errcode_name const& _entry : local::sErrCodes) { + if (_entry.code == _code) { + return _entry.name; + } } + return ""; } - return ""; -} +} // namespace #endif // USE_DEBUG_SPEW() // ################################################################################################# @@ -545,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); - // char const* err_msg = lua_tostring(L_, 1); + //std::string_view const _err_msg{ lua_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) { @@ -677,11 +683,11 @@ 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: " << get_errcode_name(_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" : lua_typename(_L, lua_type(_L, 1))) << ")" << std::endl); // Call finalizers, if the script has set them up. // LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; - DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << get_errcode_name(_rc2) << std::endl); + DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl); if (_rc2 != LuaError::OK) { // Error within a finalizer! // the finalizer generated an error, and left its own error message [and stack trace] on the stack _rc = _rc2; // we're overruling the earlier script error or normal return @@ -840,23 +846,25 @@ void Lane::changeDebugName(int nameIdx_) // ################################################################################################# -namespace global { - static struct luaL_Reg const sLaneFunctions[] = { - { "__gc", lane_gc }, - { "__index", LG_thread_index }, - { "cancel", LG_thread_cancel }, - { "get_debug_threadname", LG_get_debug_threadname }, - { "join", LG_thread_join }, - { nullptr, nullptr } - }; -} // namespace global +namespace { + namespace local { + static struct luaL_Reg const sLaneFunctions[] = { + { "__gc", lane_gc }, + { "__index", LG_thread_index }, + { "cancel", LG_thread_cancel }, + { "get_debug_threadname", LG_get_debug_threadname }, + { "join", LG_thread_join }, + { nullptr, nullptr } + }; + } // namespace local +} // namespace // contains keys: { __gc, __index, cached_error, cached_tostring, cancel, join, get_debug_threadname } void Lane::PushMetatable(lua_State* L_) { STACK_CHECK_START_REL(L_, 0); if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt - luaG_registerlibfuncs(L_, global::sLaneFunctions); + luaG_registerlibfuncs(L_, local::sLaneFunctions); // cache error() and tostring() kCachedError.pushKey(L_); // L_: mt kCachedError lua_getglobal(L_, "error"); // L_: mt kCachedError error() diff --git a/src/lanes.cpp b/src/lanes.cpp index 8033de7..6eec8c4 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp @@ -588,19 +588,21 @@ LUAG_FUNC(wakeup_conv) extern LUAG_FUNC(linda); -namespace global { - static struct luaL_Reg const sLanesFunctions[] = { - { "linda", LG_linda }, - { "now_secs", LG_now_secs }, - { "wakeup_conv", LG_wakeup_conv }, - { "set_thread_priority", LG_set_thread_priority }, - { "set_thread_affinity", LG_set_thread_affinity }, - { "nameof", luaG_nameof }, - { "register", LG_register }, - { "set_singlethreaded", LG_set_singlethreaded }, - { nullptr, nullptr } - }; -} // namespace global +namespace { + namespace local { + static struct luaL_Reg const sLanesFunctions[] = { + { "linda", LG_linda }, + { "now_secs", LG_now_secs }, + { "wakeup_conv", LG_wakeup_conv }, + { "set_thread_priority", LG_set_thread_priority }, + { "set_thread_affinity", LG_set_thread_affinity }, + { "nameof", luaG_nameof }, + { "register", LG_register }, + { "set_singlethreaded", LG_set_singlethreaded }, + { nullptr, nullptr } + }; + } // namespace local +} // namespace // ################################################################################################# @@ -686,7 +688,7 @@ LUAG_FUNC(configure) lua_pushnil(L_); // L_: settings M nil lua_setfield(L_, -2, "configure"); // L_: settings M // add functions to the module's table - luaG_registerlibfuncs(L_, global::sLanesFunctions); + luaG_registerlibfuncs(L_, local::sLanesFunctions); // register core.threads() only if settings say it should be available if (_U->tracker.isActive()) { diff --git a/src/lanesconf.h b/src/lanesconf.h index b35becc..3836848 100644 --- a/src/lanesconf.h +++ b/src/lanesconf.h @@ -16,7 +16,8 @@ // style is camel case. scope of variable is optionally specified with a single lowercase letter. // constants: prefix k, followed by an uppercase letter // program-level global variable: in 'global' namespace, prefix g, followed by an uppercase letter -// file-level static variable: in 'global' namespace, prefix s, followed by an uppercase letter +// file-level types: in anonymous namespace +// file-level static variable: in anonymous::'local' namespace, prefix s, followed by an uppercase letter // file-level function (static or not): no prefix, start with an uppercase letter // class/struct/enum type: no prefix, start with an uppercase letter // static class member/method: no prefix, start with an uppercase letter diff --git a/src/linda.cpp b/src/linda.cpp index a5db50a..87aa8fa 100644 --- a/src/linda.cpp +++ b/src/linda.cpp @@ -750,26 +750,28 @@ LUAG_FUNC(linda_towatch) // ################################################################################################# -namespace global { - static luaL_Reg const sLindaMT[] = { - { "__concat", LG_linda_concat }, - { "__tostring", LG_linda_tostring }, - { "__towatch", LG_linda_towatch }, // Decoda __towatch support - { "cancel", LG_linda_cancel }, - { "count", LG_linda_count }, - { "deep", LG_linda_deep }, - { "dump", LG_linda_dump }, - { "get", LG_linda_get }, - { "limit", LG_linda_limit }, - { "receive", LG_linda_receive }, - { "send", LG_linda_send }, - { "set", LG_linda_set }, - { nullptr, nullptr } - }; -} // namespace global +namespace { + namespace local { + static luaL_Reg const sLindaMT[] = { + { "__concat", LG_linda_concat }, + { "__tostring", LG_linda_tostring }, + { "__towatch", LG_linda_towatch }, // Decoda __towatch support + { "cancel", LG_linda_cancel }, + { "count", LG_linda_count }, + { "deep", LG_linda_deep }, + { "dump", LG_linda_dump }, + { "get", LG_linda_get }, + { "limit", LG_linda_limit }, + { "receive", LG_linda_receive }, + { "send", LG_linda_send }, + { "set", LG_linda_set }, + { nullptr, nullptr } + }; + } // namespace local +} // namespace // it's somewhat awkward to instanciate the LindaFactory here instead of lindafactory.cpp, // but that's necessary to provide s_LindaMT without exposing it outside linda.cpp. -/*static*/ LindaFactory LindaFactory::Instance{ global::sLindaMT }; +/*static*/ LindaFactory LindaFactory::Instance{ local::sLindaMT }; // ################################################################################################# // ################################################################################################# diff --git a/src/state.cpp b/src/state.cpp index 68569bd..ebe4097 100644 --- a/src/state.cpp +++ b/src/state.cpp @@ -119,47 +119,47 @@ void serialize_require(lua_State* L_) } // ################################################################################################# - -namespace global -{ - static luaL_Reg const sLibs[] = { - { "base", nullptr }, // ignore "base" (already acquired it) +namespace { + namespace local { + static luaL_Reg const sLibs[] = { + { "base", nullptr }, // ignore "base" (already acquired it) #if LUA_VERSION_NUM >= 502 #ifdef luaopen_bit32 - { LUA_BITLIBNAME, luaopen_bit32 }, + { LUA_BITLIBNAME, luaopen_bit32 }, #endif - { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base! + { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base! #else // LUA_VERSION_NUM - { LUA_COLIBNAME, nullptr }, // Lua 5.1: part of base package + { LUA_COLIBNAME, nullptr }, // Lua 5.1: part of base package #endif // LUA_VERSION_NUM - { LUA_DBLIBNAME, luaopen_debug }, + { LUA_DBLIBNAME, luaopen_debug }, #ifndef PLATFORM_XBOX // no os/io libs on xbox - { LUA_IOLIBNAME, luaopen_io }, - { LUA_OSLIBNAME, luaopen_os }, + { LUA_IOLIBNAME, luaopen_io }, + { LUA_OSLIBNAME, luaopen_os }, #endif // PLATFORM_XBOX - { LUA_LOADLIBNAME, luaopen_package }, - { LUA_MATHLIBNAME, luaopen_math }, - { LUA_STRLIBNAME, luaopen_string }, - { LUA_TABLIBNAME, luaopen_table }, + { LUA_LOADLIBNAME, luaopen_package }, + { LUA_MATHLIBNAME, luaopen_math }, + { LUA_STRLIBNAME, luaopen_string }, + { LUA_TABLIBNAME, luaopen_table }, #if LUA_VERSION_NUM >= 503 - { LUA_UTF8LIBNAME, luaopen_utf8 }, + { LUA_UTF8LIBNAME, luaopen_utf8 }, #endif #if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs - { LUA_BITLIBNAME, luaopen_bit }, - { LUA_FFILIBNAME, luaopen_ffi }, - { LUA_JITLIBNAME, luaopen_jit }, + { LUA_BITLIBNAME, luaopen_bit }, + { LUA_FFILIBNAME, luaopen_ffi }, + { LUA_JITLIBNAME, luaopen_jit }, #endif // LUAJIT_FLAVOR() - { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function) - }; + { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function) + }; -} // namespace global + } // namespace local +} // namespace // ################################################################################################# static void open1lib(lua_State* L_, std::string_view const& name_) { - for (luaL_Reg const& _entry : global::sLibs) { + for (luaL_Reg const& _entry : local::sLibs) { if (name_ == _entry.name) { lua_CFunction const _libfunc{ _entry.func }; if (!_libfunc) { diff --git a/src/tools.cpp b/src/tools.cpp index adb30b0..0b10464 100644 --- a/src/tools.cpp +++ b/src/tools.cpp @@ -234,7 +234,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 - // char const* key = (lua_type(L, -2) == LUA_TSTRING) ? lua_tostring(L, -2) : "not a string"; + // std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_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 @@ -380,7 +380,7 @@ void populate_func_lookup_table(lua_State* const L_, int const i_, std::string_v // scan table contents lua_pushnil(L_); // L_: o "r" {c} {fqn} ... {?} nil while (lua_next(L_, -2)) { // L_: o "r" {c} {fqn} ... {?} k v - // char const *const strKey = (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostring(L_, -2) : nullptr; // only for debugging + // 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 STACK_CHECK(L_, 2); // append key name to fqn stack diff --git a/src/universe.cpp b/src/universe.cpp index 3d1645f..9e1ac5f 100644 --- a/src/universe.cpp +++ b/src/universe.cpp @@ -155,8 +155,8 @@ void Universe::initializeAllocatorFunction(lua_State* L_) provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator if (provideAllocator != nullptr) { // make sure the function doesn't have upvalues - char const* upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? - if (upname != nullptr) { // should be "" for C functions with upvalues if any + char const* _upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? + if (_upname != nullptr) { // should be "" for C functions with upvalues if any raise_luaL_error(L_, "config.allocator() shouldn't have upvalues"); } // remove this C function from the config table so that it doesn't cause problems -- cgit v1.2.3-55-g6feb