aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-27 11:44:58 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-27 11:44:58 +0200
commit82ec0e4de089074aae26ab72040523fa7d21557d (patch)
tree16b29239c5bb1d194c4d3f7edbd0b5e3974aba39 /src
parenta30ac3790edea8329c199c9c42ff4150cf20c8ba (diff)
downloadlanes-82ec0e4de089074aae26ab72040523fa7d21557d.tar.gz
lanes-82ec0e4de089074aae26ab72040523fa7d21557d.tar.bz2
lanes-82ec0e4de089074aae26ab72040523fa7d21557d.zip
namespace tweaks
Diffstat (limited to 'src')
-rw-r--r--src/lane.cpp84
-rw-r--r--src/lanes.cpp30
-rw-r--r--src/lanesconf.h3
-rw-r--r--src/linda.cpp38
-rw-r--r--src/state.cpp46
-rw-r--r--src/tools.cpp4
-rw-r--r--src/universe.cpp4
7 files changed, 111 insertions, 98 deletions
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)
354// ################################################################################################# 354// #################################################################################################
355 355
356#if USE_DEBUG_SPEW() 356#if USE_DEBUG_SPEW()
357// can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-( 357namespace {
358// LUA_ERRERR doesn't have the same value 358 // can't use direct LUA_x errcode indexing because the sequence is not the same between Lua 5.1 and 5.2 :-(
359struct errcode_name 359 // LUA_ERRERR doesn't have the same value
360{ 360 struct errcode_name
361 LuaError code; 361 {
362 char const* name; 362 LuaError code;
363}; 363 std::string_view const name;
364 364 };
365static struct errcode_name s_errcodes[] = { 365
366 { LuaError::OK, "LUA_OK" }, 366 namespace local {
367 { LuaError::YIELD, "LUA_YIELD" }, 367
368 { LuaError::ERRRUN, "LUA_ERRRUN" }, 368 static struct errcode_name sErrCodes[] = {
369 { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" }, 369 { LuaError::OK, "LUA_OK" },
370 { LuaError::ERRMEM, "LUA_ERRMEM" }, 370 { LuaError::YIELD, "LUA_YIELD" },
371 { LuaError::ERRGCMM, "LUA_ERRGCMM" }, 371 { LuaError::ERRRUN, "LUA_ERRRUN" },
372 { LuaError::ERRERR, "LUA_ERRERR" }, 372 { LuaError::ERRSYNTAX, "LUA_ERRSYNTAX" },
373}; 373 { LuaError::ERRMEM, "LUA_ERRMEM" },
374static char const* get_errcode_name(LuaError _code) 374 { LuaError::ERRGCMM, "LUA_ERRGCMM" },
375{ 375 { LuaError::ERRERR, "LUA_ERRERR" },
376 for (errcode_name const& _entry : s_errcodes) { 376 };
377 if (_entry.code == _code) { 377 } // namespace local
378 return _entry.name; 378
379 static std::string_view GetErrcodeName(LuaError _code) noexcept
380 {
381 for (errcode_name const& _entry : local::sErrCodes) {
382 if (_entry.code == _code) {
383 return _entry.name;
384 }
379 } 385 }
386 return "<nullptr>";
380 } 387 }
381 return "<nullptr>"; 388} // namespace
382}
383#endif // USE_DEBUG_SPEW() 389#endif // USE_DEBUG_SPEW()
384 390
385// ################################################################################################# 391// #################################################################################################
@@ -545,7 +551,7 @@ static void push_stack_trace(lua_State* L_, Lane::ErrorTraceLevel errorTraceLeve
545 LUA_ASSERT(L_, lua_isfunction(L_, -1)); 551 LUA_ASSERT(L_, lua_isfunction(L_, -1));
546 if (lua_rc_ != LuaError::OK) { // we have an error message and an optional stack trace at the bottom of the stack 552 if (lua_rc_ != LuaError::OK) { // we have an error message and an optional stack trace at the bottom of the stack
547 LUA_ASSERT(L_, _finalizers_index == 2 || _finalizers_index == 3); 553 LUA_ASSERT(L_, _finalizers_index == 2 || _finalizers_index == 3);
548 // char const* err_msg = lua_tostring(L_, 1); 554 //std::string_view const _err_msg{ lua_tostringview(L_, 1) };
549 lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg 555 lua_pushvalue(L_, 1); // L_: ... finalizers lane_error finalizer err_msg
550 // 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 556 // 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
551 if (_finalizers_index == 3) { 557 if (_finalizers_index == 3) {
@@ -677,11 +683,11 @@ static void lane_main(Lane* lane_)
677 // in case of error and if it exists, fetch stack trace from registry and push it 683 // in case of error and if it exists, fetch stack trace from registry and push it
678 push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace] 684 push_stack_trace(_L, lane_->errorTraceLevel, _rc, 1); // L: retvals|error [trace]
679 685
680 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); 686 DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " body: " << GetErrcodeName(_rc) << " (" << (kCancelError.equals(_L, 1) ? "cancelled" : lua_typename(_L, lua_type(_L, 1))) << ")" << std::endl);
681 // Call finalizers, if the script has set them up. 687 // Call finalizers, if the script has set them up.
682 // 688 //
683 LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) }; 689 LuaError const _rc2{ run_finalizers(_L, lane_->errorTraceLevel, _rc) };
684 DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << get_errcode_name(_rc2) << std::endl); 690 DEBUGSPEW_CODE(DebugSpew(_U) << "Lane " << _L << " finalizer: " << GetErrcodeName(_rc2) << std::endl);
685 if (_rc2 != LuaError::OK) { // Error within a finalizer! 691 if (_rc2 != LuaError::OK) { // Error within a finalizer!
686 // the finalizer generated an error, and left its own error message [and stack trace] on the stack 692 // the finalizer generated an error, and left its own error message [and stack trace] on the stack
687 _rc = _rc2; // we're overruling the earlier script error or normal return 693 _rc = _rc2; // we're overruling the earlier script error or normal return
@@ -840,23 +846,25 @@ void Lane::changeDebugName(int nameIdx_)
840 846
841// ################################################################################################# 847// #################################################################################################
842 848
843namespace global { 849namespace {
844 static struct luaL_Reg const sLaneFunctions[] = { 850 namespace local {
845 { "__gc", lane_gc }, 851 static struct luaL_Reg const sLaneFunctions[] = {
846 { "__index", LG_thread_index }, 852 { "__gc", lane_gc },
847 { "cancel", LG_thread_cancel }, 853 { "__index", LG_thread_index },
848 { "get_debug_threadname", LG_get_debug_threadname }, 854 { "cancel", LG_thread_cancel },
849 { "join", LG_thread_join }, 855 { "get_debug_threadname", LG_get_debug_threadname },
850 { nullptr, nullptr } 856 { "join", LG_thread_join },
851 }; 857 { nullptr, nullptr }
852} // namespace global 858 };
859 } // namespace local
860} // namespace
853 861
854 // contains keys: { __gc, __index, cached_error, cached_tostring, cancel, join, get_debug_threadname } 862 // contains keys: { __gc, __index, cached_error, cached_tostring, cancel, join, get_debug_threadname }
855void Lane::PushMetatable(lua_State* L_) 863void Lane::PushMetatable(lua_State* L_)
856{ 864{
857 STACK_CHECK_START_REL(L_, 0); 865 STACK_CHECK_START_REL(L_, 0);
858 if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt 866 if (luaL_newmetatable(L_, kLaneMetatableName)) { // L_: mt
859 luaG_registerlibfuncs(L_, global::sLaneFunctions); 867 luaG_registerlibfuncs(L_, local::sLaneFunctions);
860 // cache error() and tostring() 868 // cache error() and tostring()
861 kCachedError.pushKey(L_); // L_: mt kCachedError 869 kCachedError.pushKey(L_); // L_: mt kCachedError
862 lua_getglobal(L_, "error"); // L_: mt kCachedError error() 870 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)
588 588
589extern LUAG_FUNC(linda); 589extern LUAG_FUNC(linda);
590 590
591namespace global { 591namespace {
592 static struct luaL_Reg const sLanesFunctions[] = { 592 namespace local {
593 { "linda", LG_linda }, 593 static struct luaL_Reg const sLanesFunctions[] = {
594 { "now_secs", LG_now_secs }, 594 { "linda", LG_linda },
595 { "wakeup_conv", LG_wakeup_conv }, 595 { "now_secs", LG_now_secs },
596 { "set_thread_priority", LG_set_thread_priority }, 596 { "wakeup_conv", LG_wakeup_conv },
597 { "set_thread_affinity", LG_set_thread_affinity }, 597 { "set_thread_priority", LG_set_thread_priority },
598 { "nameof", luaG_nameof }, 598 { "set_thread_affinity", LG_set_thread_affinity },
599 { "register", LG_register }, 599 { "nameof", luaG_nameof },
600 { "set_singlethreaded", LG_set_singlethreaded }, 600 { "register", LG_register },
601 { nullptr, nullptr } 601 { "set_singlethreaded", LG_set_singlethreaded },
602 }; 602 { nullptr, nullptr }
603} // namespace global 603 };
604 } // namespace local
605} // namespace
604 606
605// ################################################################################################# 607// #################################################################################################
606 608
@@ -686,7 +688,7 @@ LUAG_FUNC(configure)
686 lua_pushnil(L_); // L_: settings M nil 688 lua_pushnil(L_); // L_: settings M nil
687 lua_setfield(L_, -2, "configure"); // L_: settings M 689 lua_setfield(L_, -2, "configure"); // L_: settings M
688 // add functions to the module's table 690 // add functions to the module's table
689 luaG_registerlibfuncs(L_, global::sLanesFunctions); 691 luaG_registerlibfuncs(L_, local::sLanesFunctions);
690 692
691 // register core.threads() only if settings say it should be available 693 // register core.threads() only if settings say it should be available
692 if (_U->tracker.isActive()) { 694 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 @@
16// style is camel case. scope of variable is optionally specified with a single lowercase letter. 16// style is camel case. scope of variable is optionally specified with a single lowercase letter.
17// constants: prefix k, followed by an uppercase letter 17// constants: prefix k, followed by an uppercase letter
18// program-level global variable: in 'global' namespace, prefix g, followed by an uppercase letter 18// program-level global variable: in 'global' namespace, prefix g, followed by an uppercase letter
19// file-level static variable: in 'global' namespace, prefix s, followed by an uppercase letter 19// file-level types: in anonymous namespace
20// file-level static variable: in anonymous::'local' namespace, prefix s, followed by an uppercase letter
20// file-level function (static or not): no prefix, start with an uppercase letter 21// file-level function (static or not): no prefix, start with an uppercase letter
21// class/struct/enum type: no prefix, start with an uppercase letter 22// class/struct/enum type: no prefix, start with an uppercase letter
22// static class member/method: no prefix, start with an uppercase letter 23// 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)
750 750
751// ################################################################################################# 751// #################################################################################################
752 752
753namespace global { 753namespace {
754 static luaL_Reg const sLindaMT[] = { 754 namespace local {
755 { "__concat", LG_linda_concat }, 755 static luaL_Reg const sLindaMT[] = {
756 { "__tostring", LG_linda_tostring }, 756 { "__concat", LG_linda_concat },
757 { "__towatch", LG_linda_towatch }, // Decoda __towatch support 757 { "__tostring", LG_linda_tostring },
758 { "cancel", LG_linda_cancel }, 758 { "__towatch", LG_linda_towatch }, // Decoda __towatch support
759 { "count", LG_linda_count }, 759 { "cancel", LG_linda_cancel },
760 { "deep", LG_linda_deep }, 760 { "count", LG_linda_count },
761 { "dump", LG_linda_dump }, 761 { "deep", LG_linda_deep },
762 { "get", LG_linda_get }, 762 { "dump", LG_linda_dump },
763 { "limit", LG_linda_limit }, 763 { "get", LG_linda_get },
764 { "receive", LG_linda_receive }, 764 { "limit", LG_linda_limit },
765 { "send", LG_linda_send }, 765 { "receive", LG_linda_receive },
766 { "set", LG_linda_set }, 766 { "send", LG_linda_send },
767 { nullptr, nullptr } 767 { "set", LG_linda_set },
768 }; 768 { nullptr, nullptr }
769} // namespace global 769 };
770 } // namespace local
771} // namespace
770// it's somewhat awkward to instanciate the LindaFactory here instead of lindafactory.cpp, 772// it's somewhat awkward to instanciate the LindaFactory here instead of lindafactory.cpp,
771// but that's necessary to provide s_LindaMT without exposing it outside linda.cpp. 773// but that's necessary to provide s_LindaMT without exposing it outside linda.cpp.
772/*static*/ LindaFactory LindaFactory::Instance{ global::sLindaMT }; 774/*static*/ LindaFactory LindaFactory::Instance{ local::sLindaMT };
773 775
774// ################################################################################################# 776// #################################################################################################
775// ################################################################################################# 777// #################################################################################################
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_)
119} 119}
120 120
121// ################################################################################################# 121// #################################################################################################
122 122namespace {
123namespace global 123 namespace local {
124{ 124 static luaL_Reg const sLibs[] = {
125 static luaL_Reg const sLibs[] = { 125 { "base", nullptr }, // ignore "base" (already acquired it)
126 { "base", nullptr }, // ignore "base" (already acquired it)
127#if LUA_VERSION_NUM >= 502 126#if LUA_VERSION_NUM >= 502
128#ifdef luaopen_bit32 127#ifdef luaopen_bit32
129 { LUA_BITLIBNAME, luaopen_bit32 }, 128 { LUA_BITLIBNAME, luaopen_bit32 },
130#endif 129#endif
131 { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base! 130 { LUA_COLIBNAME, luaopen_coroutine }, // Lua 5.2: coroutine is no longer a part of base!
132#else // LUA_VERSION_NUM 131#else // LUA_VERSION_NUM
133 { LUA_COLIBNAME, nullptr }, // Lua 5.1: part of base package 132 { LUA_COLIBNAME, nullptr }, // Lua 5.1: part of base package
134#endif // LUA_VERSION_NUM 133#endif // LUA_VERSION_NUM
135 { LUA_DBLIBNAME, luaopen_debug }, 134 { LUA_DBLIBNAME, luaopen_debug },
136#ifndef PLATFORM_XBOX // no os/io libs on xbox 135#ifndef PLATFORM_XBOX // no os/io libs on xbox
137 { LUA_IOLIBNAME, luaopen_io }, 136 { LUA_IOLIBNAME, luaopen_io },
138 { LUA_OSLIBNAME, luaopen_os }, 137 { LUA_OSLIBNAME, luaopen_os },
139#endif // PLATFORM_XBOX 138#endif // PLATFORM_XBOX
140 { LUA_LOADLIBNAME, luaopen_package }, 139 { LUA_LOADLIBNAME, luaopen_package },
141 { LUA_MATHLIBNAME, luaopen_math }, 140 { LUA_MATHLIBNAME, luaopen_math },
142 { LUA_STRLIBNAME, luaopen_string }, 141 { LUA_STRLIBNAME, luaopen_string },
143 { LUA_TABLIBNAME, luaopen_table }, 142 { LUA_TABLIBNAME, luaopen_table },
144#if LUA_VERSION_NUM >= 503 143#if LUA_VERSION_NUM >= 503
145 { LUA_UTF8LIBNAME, luaopen_utf8 }, 144 { LUA_UTF8LIBNAME, luaopen_utf8 },
146#endif 145#endif
147#if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs 146#if LUAJIT_FLAVOR() != 0 // building against LuaJIT headers, add some LuaJIT-specific libs
148 { LUA_BITLIBNAME, luaopen_bit }, 147 { LUA_BITLIBNAME, luaopen_bit },
149 { LUA_FFILIBNAME, luaopen_ffi }, 148 { LUA_FFILIBNAME, luaopen_ffi },
150 { LUA_JITLIBNAME, luaopen_jit }, 149 { LUA_JITLIBNAME, luaopen_jit },
151#endif // LUAJIT_FLAVOR() 150#endif // LUAJIT_FLAVOR()
152 151
153 { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function) 152 { kLanesCoreLibName, require_lanes_core } // So that we can open it like any base library (possible since we have access to the init function)
154 }; 153 };
155 154
156} // namespace global 155 } // namespace local
156} // namespace
157 157
158// ################################################################################################# 158// #################################################################################################
159 159
160static void open1lib(lua_State* L_, std::string_view const& name_) 160static void open1lib(lua_State* L_, std::string_view const& name_)
161{ 161{
162 for (luaL_Reg const& _entry : global::sLibs) { 162 for (luaL_Reg const& _entry : local::sLibs) {
163 if (name_ == _entry.name) { 163 if (name_ == _entry.name) {
164 lua_CFunction const _libfunc{ _entry.func }; 164 lua_CFunction const _libfunc{ _entry.func };
165 if (!_libfunc) { 165 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_,
234 lua_pushnil(L_); // L_: ... {i_} {bfc} nil 234 lua_pushnil(L_); // L_: ... {i_} {bfc} nil
235 while (lua_next(L_, i_) != 0) { // L_: ... {i_} {bfc} k v 235 while (lua_next(L_, i_) != 0) { // L_: ... {i_} {bfc} k v
236 // just for debug, not actually needed 236 // just for debug, not actually needed
237 // char const* key = (lua_type(L, -2) == LUA_TSTRING) ? lua_tostring(L, -2) : "not a string"; 237 // std::string_view const _key{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : "not a string" };
238 // subtable: process it recursively 238 // subtable: process it recursively
239 if (lua_istable(L_, -1)) { // L_: ... {i_} {bfc} k {} 239 if (lua_istable(L_, -1)) { // L_: ... {i_} {bfc} k {}
240 // increment visit count to make sure we will actually scan it at this recursive level 240 // 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
380 // scan table contents 380 // scan table contents
381 lua_pushnil(L_); // L_: o "r" {c} {fqn} ... {?} nil 381 lua_pushnil(L_); // L_: o "r" {c} {fqn} ... {?} nil
382 while (lua_next(L_, -2)) { // L_: o "r" {c} {fqn} ... {?} k v 382 while (lua_next(L_, -2)) { // L_: o "r" {c} {fqn} ... {?} k v
383 // char const *const strKey = (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostring(L_, -2) : nullptr; // only for debugging 383 // std::string_view const _strKey{ (lua_type(L_, -2) == LUA_TSTRING) ? lua_tostringview(L_, -2) : "" }; // only for debugging
384 // lua_Number const numKey = (lua_type(L_, -2) == LUA_TNUMBER) ? lua_tonumber(L_, -2) : -6666; // only for debugging 384 // lua_Number const numKey = (lua_type(L_, -2) == LUA_TNUMBER) ? lua_tonumber(L_, -2) : -6666; // only for debugging
385 STACK_CHECK(L_, 2); 385 STACK_CHECK(L_, 2);
386 // append key name to fqn stack 386 // 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_)
155 provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator 155 provideAllocator = lua_tocfunction(L_, -1); // L_: settings allocator
156 if (provideAllocator != nullptr) { 156 if (provideAllocator != nullptr) {
157 // make sure the function doesn't have upvalues 157 // make sure the function doesn't have upvalues
158 char const* upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval? 158 char const* _upname = lua_getupvalue(L_, -1, 1); // L_: settings allocator upval?
159 if (upname != nullptr) { // should be "" for C functions with upvalues if any 159 if (_upname != nullptr) { // should be "" for C functions with upvalues if any
160 raise_luaL_error(L_, "config.allocator() shouldn't have upvalues"); 160 raise_luaL_error(L_, "config.allocator() shouldn't have upvalues");
161 } 161 }
162 // remove this C function from the config table so that it doesn't cause problems 162 // remove this C function from the config table so that it doesn't cause problems