aboutsummaryrefslogtreecommitdiff
path: root/src/tools.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/tools.cpp')
-rw-r--r--src/tools.cpp260
1 files changed, 132 insertions, 128 deletions
diff --git a/src/tools.cpp b/src/tools.cpp
index 07f9ae6..4083a57 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -144,7 +144,7 @@ void luaG_dump( lua_State* L)
144// ################################################################################################ 144// ################################################################################################
145 145
146// same as PUC-Lua l_alloc 146// same as PUC-Lua l_alloc
147extern "C" static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_) 147extern "C" [[nodiscard]] static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused]] void* ptr_, [[maybe_unused]] size_t osize_, size_t nsize_)
148{ 148{
149 if (nsize_ == 0) 149 if (nsize_ == 0)
150 { 150 {
@@ -159,7 +159,7 @@ extern "C" static void* libc_lua_Alloc([[maybe_unused]] void* ud, [[maybe_unused
159 159
160// ################################################################################################# 160// #################################################################################################
161 161
162static int luaG_provide_protected_allocator(lua_State* L) 162[[nodiscard]] static int luaG_provide_protected_allocator(lua_State* L)
163{ 163{
164 Universe* const U{ universe_get(L) }; 164 Universe* const U{ universe_get(L) };
165 // push a new full userdata on the stack, giving access to the universe's protected allocator 165 // push a new full userdata on the stack, giving access to the universe's protected allocator
@@ -234,7 +234,7 @@ void initialize_allocator_function(Universe* U, lua_State* L)
234 234
235// ################################################################################################ 235// ################################################################################################
236 236
237static int dummy_writer( lua_State* L, void const* p, size_t sz, void* ud) 237[[nodiscard]] static int dummy_writer(lua_State* L, void const* p, size_t sz, void* ud)
238{ 238{
239 (void)L; (void)p; (void)sz; (void) ud; // unused 239 (void)L; (void)p; (void)sz; (void) ud; // unused
240 return 666; 240 return 666;
@@ -291,7 +291,7 @@ FuncSubType luaG_getfuncsubtype( lua_State *L, int _i)
291 291
292// ################################################################################################# 292// #################################################################################################
293 293
294static lua_CFunction luaG_tocfunction(lua_State* L, int _i, FuncSubType* _out) 294[[nodiscard]] static lua_CFunction luaG_tocfunction(lua_State* L, int _i, FuncSubType* _out)
295{ 295{
296 lua_CFunction p = lua_tocfunction( L, _i); 296 lua_CFunction p = lua_tocfunction( L, _i);
297 *_out = luaG_getfuncsubtype( L, _i); 297 *_out = luaG_getfuncsubtype( L, _i);
@@ -304,7 +304,7 @@ static constexpr UniqueKey LOOKUPCACHE_REGKEY{ 0x837a68dfc6fcb716ull };
304// ################################################################################################# 304// #################################################################################################
305 305
306// inspired from tconcat() in ltablib.c 306// inspired from tconcat() in ltablib.c
307static char const* luaG_pushFQN( lua_State* L, int t, int last, size_t* length) 307[[nodiscard]] static char const* luaG_pushFQN(lua_State* L, int t, int last, size_t* length)
308{ 308{
309 int i = 1; 309 int i = 1;
310 luaL_Buffer b; 310 luaL_Buffer b;
@@ -338,7 +338,7 @@ static char const* luaG_pushFQN( lua_State* L, int t, int last, size_t* length)
338 * if we already had an entry of type [o] = ..., replace the name if the new one is shorter 338 * if we already had an entry of type [o] = ..., replace the name if the new one is shorter
339 * pops the processed object from the stack 339 * pops the processed object from the stack
340 */ 340 */
341static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, int _ctx_base, int _depth) 341static void update_lookup_entry(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, int _ctx_base, int _depth)
342{ 342{
343 // slot 1 in the stack contains the table that receives everything we found 343 // slot 1 in the stack contains the table that receives everything we found
344 int const dest = _ctx_base; 344 int const dest = _ctx_base;
@@ -362,7 +362,7 @@ static void update_lookup_entry( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State*
362 ++ _depth; 362 ++ _depth;
363 lua_rawseti( L, fqn, _depth); // ... {bfc} k o name? 363 lua_rawseti( L, fqn, _depth); // ... {bfc} k o name?
364 // generate name 364 // generate name
365 DEBUGSPEW_CODE( newName =) luaG_pushFQN( L, fqn, _depth, &newNameLength); // ... {bfc} k o name? "f.q.n" 365 DEBUGSPEW_OR_NOT(newName, std::ignore) = luaG_pushFQN(L, fqn, _depth, &newNameLength);// ... {bfc} k o name? "f.q.n"
366 // Lua 5.2 introduced a hash randomizer seed which causes table iteration to yield a different key order 366 // Lua 5.2 introduced a hash randomizer seed which causes table iteration to yield a different key order
367 // on different VMs even when the tables are populated the exact same way. 367 // on different VMs even when the tables are populated the exact same way.
368 // When Lua is built with compatibility options (such as LUA_COMPAT_ALL), 368 // When Lua is built with compatibility options (such as LUA_COMPAT_ALL),
@@ -613,7 +613,7 @@ static constexpr UniqueKey REG_MTID{ 0x2e68f9b4751584dcull };
613/* 613/*
614* Get a unique ID for metatable at [i]. 614* Get a unique ID for metatable at [i].
615*/ 615*/
616static lua_Integer get_mt_id( Universe* U, lua_State* L, int i) 616[[nodiscard]] static lua_Integer get_mt_id(Universe* U, lua_State* L, int i)
617{ 617{
618 lua_Integer id; 618 lua_Integer id;
619 619
@@ -654,25 +654,25 @@ static lua_Integer get_mt_id( Universe* U, lua_State* L, int i)
654// ################################################################################################# 654// #################################################################################################
655 655
656// function sentinel used to transfer native functions from/to keeper states 656// function sentinel used to transfer native functions from/to keeper states
657static int func_lookup_sentinel( lua_State* L) 657[[nodiscard]] static int func_lookup_sentinel(lua_State* L)
658{ 658{
659 return luaL_error( L, "function lookup sentinel for %s, should never be called", lua_tostring( L, lua_upvalueindex( 1))); 659 return luaL_error(L, "function lookup sentinel for %s, should never be called", lua_tostring(L, lua_upvalueindex(1)));
660} 660}
661 661
662// ################################################################################################# 662// #################################################################################################
663 663
664// function sentinel used to transfer native table from/to keeper states 664// function sentinel used to transfer native table from/to keeper states
665static int table_lookup_sentinel( lua_State* L) 665[[nodiscard]] static int table_lookup_sentinel(lua_State* L)
666{ 666{
667 return luaL_error( L, "table lookup sentinel for %s, should never be called", lua_tostring( L, lua_upvalueindex( 1))); 667 return luaL_error(L, "table lookup sentinel for %s, should never be called", lua_tostring(L, lua_upvalueindex(1)));
668} 668}
669 669
670// ################################################################################################# 670// #################################################################################################
671 671
672// function sentinel used to transfer cloned full userdata from/to keeper states 672// function sentinel used to transfer cloned full userdata from/to keeper states
673static int userdata_clone_sentinel( lua_State* L) 673[[nodiscard]] static int userdata_clone_sentinel(lua_State* L)
674{ 674{
675 return luaL_error( L, "userdata clone sentinel for %s, should never be called", lua_tostring( L, lua_upvalueindex( 1))); 675 return luaL_error(L, "userdata clone sentinel for %s, should never be called", lua_tostring(L, lua_upvalueindex(1)));
676} 676}
677 677
678// ################################################################################################# 678// #################################################################################################
@@ -680,7 +680,7 @@ static int userdata_clone_sentinel( lua_State* L)
680/* 680/*
681 * retrieve the name of a function/table in the lookup database 681 * retrieve the name of a function/table in the lookup database
682 */ 682 */
683static char const* find_lookup_name(lua_State* L, int i, LookupMode mode_, char const* upName_, size_t* len_) 683[[nodiscard]] static char const* find_lookup_name(lua_State* L, int i, LookupMode mode_, char const* upName_, size_t* len_)
684{ 684{
685 DEBUGSPEW_CODE( Universe* const U = universe_get( L)); 685 DEBUGSPEW_CODE( Universe* const U = universe_get( L));
686 char const* fqn; 686 char const* fqn;
@@ -753,7 +753,7 @@ static char const* find_lookup_name(lua_State* L, int i, LookupMode mode_, char
753/* 753/*
754 * Push a looked-up table, or nothing if we found nothing 754 * Push a looked-up table, or nothing if we found nothing
755 */ 755 */
756static bool lookup_table(Dest L2, Source L, int i, LookupMode mode_, char const* upName_) 756[[nodiscard]] static bool lookup_table(Dest L2, Source L, int i, LookupMode mode_, char const* upName_)
757{ 757{
758 // get the name of the table we want to send 758 // get the name of the table we want to send
759 size_t len; 759 size_t len;
@@ -830,13 +830,12 @@ static bool lookup_table(Dest L2, Source L, int i, LookupMode mode_, char const*
830 * Returns true if the table was cached (no need to fill it!); false if 830 * Returns true if the table was cached (no need to fill it!); false if
831 * it's a virgin. 831 * it's a virgin.
832 */ 832 */
833static bool push_cached_table(lua_State* L2, int L2_cache_i, lua_State* L, int i) 833[[nodiscard]] static bool push_cached_table(Dest L2, int L2_cache_i, Source L, int i)
834{ 834{
835 bool not_found_in_cache; // L2
836 void const* p{ lua_topointer(L, i) }; 835 void const* p{ lua_topointer(L, i) };
837 836
838 ASSERT_L( L2_cache_i != 0); 837 ASSERT_L( L2_cache_i != 0);
839 STACK_GROW( L2, 3); 838 STACK_GROW( L2, 3); // L2
840 STACK_CHECK_START_REL(L2, 0); 839 STACK_CHECK_START_REL(L2, 0);
841 840
842 // We don't need to use the from state ('L') in ID since the life span 841 // We don't need to use the from state ('L') in ID since the life span
@@ -847,7 +846,7 @@ static bool push_cached_table(lua_State* L2, int L2_cache_i, lua_State* L, int i
847 //fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1)); 846 //fprintf( stderr, "<< ID: %s >>\n", lua_tostring( L2, -1));
848 847
849 lua_rawget( L2, L2_cache_i); // ... {cached|nil} 848 lua_rawget( L2, L2_cache_i); // ... {cached|nil}
850 not_found_in_cache = lua_isnil( L2, -1); 849 bool const not_found_in_cache{ lua_isnil(L2, -1) };
851 if( not_found_in_cache) 850 if( not_found_in_cache)
852 { 851 {
853 lua_pop( L2, 1); // ... 852 lua_pop( L2, 1); // ...
@@ -866,83 +865,83 @@ static bool push_cached_table(lua_State* L2, int L2_cache_i, lua_State* L, int i
866/* 865/*
867 * Return some name helping to identify an object 866 * Return some name helping to identify an object
868 */ 867 */
869static int discover_object_name_recur( lua_State* L, int shortest_, int depth_) 868[[nodiscard]] static int discover_object_name_recur(lua_State* L, int shortest_, int depth_)
870{ 869{
871 int const what = 1; // o "r" {c} {fqn} ... {?} 870 int const what = 1; // o "r" {c} {fqn} ... {?}
872 int const result = 2; 871 int const result = 2;
873 int const cache = 3; 872 int const cache = 3;
874 int const fqn = 4; 873 int const fqn = 4;
875 // no need to scan this table if the name we will discover is longer than one we already know 874 // no need to scan this table if the name we will discover is longer than one we already know
876 if( shortest_ <= depth_ + 1) 875 if (shortest_ <= depth_ + 1)
877 { 876 {
878 return shortest_; 877 return shortest_;
879 } 878 }
880 STACK_GROW( L, 3); 879 STACK_GROW(L, 3);
881 STACK_CHECK_START_REL(L, 0); 880 STACK_CHECK_START_REL(L, 0);
882 // stack top contains the table to search in 881 // stack top contains the table to search in
883 lua_pushvalue( L, -1); // o "r" {c} {fqn} ... {?} {?} 882 lua_pushvalue(L, -1); // o "r" {c} {fqn} ... {?} {?}
884 lua_rawget( L, cache); // o "r" {c} {fqn} ... {?} nil/1 883 lua_rawget(L, cache); // o "r" {c} {fqn} ... {?} nil/1
885 // if table is already visited, we are done 884 // if table is already visited, we are done
886 if( !lua_isnil( L, -1)) 885 if( !lua_isnil(L, -1))
887 { 886 {
888 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} 887 lua_pop(L, 1); // o "r" {c} {fqn} ... {?}
889 return shortest_; 888 return shortest_;
890 } 889 }
891 // examined table is not in the cache, add it now 890 // examined table is not in the cache, add it now
892 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} 891 lua_pop(L, 1); // o "r" {c} {fqn} ... {?}
893 lua_pushvalue( L, -1); // o "r" {c} {fqn} ... {?} {?} 892 lua_pushvalue(L, -1); // o "r" {c} {fqn} ... {?} {?}
894 lua_pushinteger( L, 1); // o "r" {c} {fqn} ... {?} {?} 1 893 lua_pushinteger(L, 1); // o "r" {c} {fqn} ... {?} {?} 1
895 lua_rawset( L, cache); // o "r" {c} {fqn} ... {?} 894 lua_rawset(L, cache); // o "r" {c} {fqn} ... {?}
896 // scan table contents 895 // scan table contents
897 lua_pushnil( L); // o "r" {c} {fqn} ... {?} nil 896 lua_pushnil(L); // o "r" {c} {fqn} ... {?} nil
898 while( lua_next( L, -2)) // o "r" {c} {fqn} ... {?} k v 897 while (lua_next(L, -2)) // o "r" {c} {fqn} ... {?} k v
899 { 898 {
900 //char const *const strKey = (lua_type( L, -2) == LUA_TSTRING) ? lua_tostring( L, -2) : nullptr; // only for debugging 899 //char const *const strKey = (lua_type(L, -2) == LUA_TSTRING) ? lua_tostring(L, -2) : nullptr; // only for debugging
901 //lua_Number const numKey = (lua_type( L, -2) == LUA_TNUMBER) ? lua_tonumber( L, -2) : -6666; // only for debugging 900 //lua_Number const numKey = (lua_type(L, -2) == LUA_TNUMBER) ? lua_tonumber(L, -2) : -6666; // only for debugging
902 STACK_CHECK( L, 2); 901 STACK_CHECK(L, 2);
903 // append key name to fqn stack 902 // append key name to fqn stack
904 ++ depth_; 903 ++ depth_;
905 lua_pushvalue( L, -2); // o "r" {c} {fqn} ... {?} k v k 904 lua_pushvalue(L, -2); // o "r" {c} {fqn} ... {?} k v k
906 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v 905 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v
907 if( lua_rawequal( L, -1, what)) // is it what we are looking for? 906 if (lua_rawequal(L, -1, what)) // is it what we are looking for?
908 { 907 {
909 STACK_CHECK( L, 2); 908 STACK_CHECK(L, 2);
910 // update shortest name 909 // update shortest name
911 if( depth_ < shortest_) 910 if( depth_ < shortest_)
912 { 911 {
913 shortest_ = depth_; 912 shortest_ = depth_;
914 luaG_pushFQN( L, fqn, depth_, nullptr); // o "r" {c} {fqn} ... {?} k v "fqn" 913 std::ignore = luaG_pushFQN(L, fqn, depth_, nullptr); // o "r" {c} {fqn} ... {?} k v "fqn"
915 lua_replace( L, result); // o "r" {c} {fqn} ... {?} k v 914 lua_replace(L, result); // o "r" {c} {fqn} ... {?} k v
916 } 915 }
917 // no need to search further at this level 916 // no need to search further at this level
918 lua_pop( L, 2); // o "r" {c} {fqn} ... {?} 917 lua_pop(L, 2); // o "r" {c} {fqn} ... {?}
919 STACK_CHECK( L, 0); 918 STACK_CHECK(L, 0);
920 break; 919 break;
921 } 920 }
922 switch( lua_type( L, -1)) // o "r" {c} {fqn} ... {?} k v 921 switch (lua_type(L, -1)) // o "r" {c} {fqn} ... {?} k v
923 { 922 {
924 default: // nil, boolean, light userdata, number and string aren't identifiable 923 default: // nil, boolean, light userdata, number and string aren't identifiable
925 break; 924 break;
926 925
927 case LUA_TTABLE: // o "r" {c} {fqn} ... {?} k {} 926 case LUA_TTABLE: // o "r" {c} {fqn} ... {?} k {}
928 STACK_CHECK( L, 2); 927 STACK_CHECK(L, 2);
929 shortest_ = discover_object_name_recur( L, shortest_, depth_); 928 shortest_ = discover_object_name_recur(L, shortest_, depth_);
930 // search in the table's metatable too 929 // search in the table's metatable too
931 if( lua_getmetatable( L, -1)) // o "r" {c} {fqn} ... {?} k {} {mt} 930 if (lua_getmetatable(L, -1)) // o "r" {c} {fqn} ... {?} k {} {mt}
932 { 931 {
933 if( lua_istable( L, -1)) 932 if( lua_istable(L, -1))
934 { 933 {
935 ++ depth_; 934 ++ depth_;
936 lua_pushliteral( L, "__metatable"); // o "r" {c} {fqn} ... {?} k {} {mt} "__metatable" 935 lua_pushliteral(L, "__metatable"); // o "r" {c} {fqn} ... {?} k {} {mt} "__metatable"
937 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k {} {mt} 936 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k {} {mt}
938 shortest_ = discover_object_name_recur( L, shortest_, depth_); 937 shortest_ = discover_object_name_recur(L, shortest_, depth_);
939 lua_pushnil( L); // o "r" {c} {fqn} ... {?} k {} {mt} nil 938 lua_pushnil(L); // o "r" {c} {fqn} ... {?} k {} {mt} nil
940 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k {} {mt} 939 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k {} {mt}
941 -- depth_; 940 -- depth_;
942 } 941 }
943 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} k {} 942 lua_pop(L, 1); // o "r" {c} {fqn} ... {?} k {}
944 } 943 }
945 STACK_CHECK( L, 2); 944 STACK_CHECK(L, 2);
946 break; 945 break;
947 946
948 case LUA_TTHREAD: // o "r" {c} {fqn} ... {?} k T 947 case LUA_TTHREAD: // o "r" {c} {fqn} ... {?} k T
@@ -950,61 +949,61 @@ static int discover_object_name_recur( lua_State* L, int shortest_, int depth_)
950 break; 949 break;
951 950
952 case LUA_TUSERDATA: // o "r" {c} {fqn} ... {?} k U 951 case LUA_TUSERDATA: // o "r" {c} {fqn} ... {?} k U
953 STACK_CHECK( L, 2); 952 STACK_CHECK(L, 2);
954 // search in the object's metatable (some modules are built that way) 953 // search in the object's metatable (some modules are built that way)
955 if( lua_getmetatable( L, -1)) // o "r" {c} {fqn} ... {?} k U {mt} 954 if (lua_getmetatable(L, -1)) // o "r" {c} {fqn} ... {?} k U {mt}
956 { 955 {
957 if( lua_istable( L, -1)) 956 if (lua_istable(L, -1))
958 { 957 {
959 ++ depth_; 958 ++ depth_;
960 lua_pushliteral( L, "__metatable"); // o "r" {c} {fqn} ... {?} k U {mt} "__metatable" 959 lua_pushliteral(L, "__metatable"); // o "r" {c} {fqn} ... {?} k U {mt} "__metatable"
961 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k U {mt} 960 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k U {mt}
962 shortest_ = discover_object_name_recur( L, shortest_, depth_); 961 shortest_ = discover_object_name_recur(L, shortest_, depth_);
963 lua_pushnil( L); // o "r" {c} {fqn} ... {?} k U {mt} nil 962 lua_pushnil(L); // o "r" {c} {fqn} ... {?} k U {mt} nil
964 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k U {mt} 963 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k U {mt}
965 -- depth_; 964 -- depth_;
966 } 965 }
967 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} k U 966 lua_pop(L, 1); // o "r" {c} {fqn} ... {?} k U
968 } 967 }
969 STACK_CHECK( L, 2); 968 STACK_CHECK(L, 2);
970 // search in the object's uservalues 969 // search in the object's uservalues
971 { 970 {
972 int uvi = 1; 971 int uvi = 1;
973 while( lua_getiuservalue( L, -1, uvi) != LUA_TNONE) // o "r" {c} {fqn} ... {?} k U {u} 972 while (lua_getiuservalue(L, -1, uvi) != LUA_TNONE) // o "r" {c} {fqn} ... {?} k U {u}
974 { 973 {
975 if( lua_istable( L, -1)) // if it is a table, look inside 974 if( lua_istable(L, -1)) // if it is a table, look inside
976 { 975 {
977 ++ depth_; 976 ++ depth_;
978 lua_pushliteral( L, "uservalue"); // o "r" {c} {fqn} ... {?} k v {u} "uservalue" 977 lua_pushliteral(L, "uservalue"); // o "r" {c} {fqn} ... {?} k v {u} "uservalue"
979 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v {u} 978 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v {u}
980 shortest_ = discover_object_name_recur( L, shortest_, depth_); 979 shortest_ = discover_object_name_recur(L, shortest_, depth_);
981 lua_pushnil( L); // o "r" {c} {fqn} ... {?} k v {u} nil 980 lua_pushnil(L); // o "r" {c} {fqn} ... {?} k v {u} nil
982 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v {u} 981 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k v {u}
983 -- depth_; 982 -- depth_;
984 } 983 }
985 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} k U 984 lua_pop(L, 1); // o "r" {c} {fqn} ... {?} k U
986 ++ uvi; 985 ++ uvi;
987 } 986 }
988 // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now 987 // when lua_getiuservalue() returned LUA_TNONE, it pushed a nil. pop it now
989 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} k U 988 lua_pop(L, 1); // o "r" {c} {fqn} ... {?} k U
990 } 989 }
991 STACK_CHECK( L, 2); 990 STACK_CHECK(L, 2);
992 break; 991 break;
993 } 992 }
994 // make ready for next iteration 993 // make ready for next iteration
995 lua_pop( L, 1); // o "r" {c} {fqn} ... {?} k 994 lua_pop(L, 1); // o "r" {c} {fqn} ... {?} k
996 // remove name from fqn stack 995 // remove name from fqn stack
997 lua_pushnil( L); // o "r" {c} {fqn} ... {?} k nil 996 lua_pushnil(L); // o "r" {c} {fqn} ... {?} k nil
998 lua_rawseti( L, fqn, depth_); // o "r" {c} {fqn} ... {?} k 997 lua_rawseti(L, fqn, depth_); // o "r" {c} {fqn} ... {?} k
999 STACK_CHECK( L, 1); 998 STACK_CHECK(L, 1);
1000 -- depth_; 999 -- depth_;
1001 } // o "r" {c} {fqn} ... {?} 1000 } // o "r" {c} {fqn} ... {?}
1002 STACK_CHECK( L, 0); 1001 STACK_CHECK(L, 0);
1003 // remove the visited table from the cache, in case a shorter path to the searched object exists 1002 // remove the visited table from the cache, in case a shorter path to the searched object exists
1004 lua_pushvalue( L, -1); // o "r" {c} {fqn} ... {?} {?} 1003 lua_pushvalue(L, -1); // o "r" {c} {fqn} ... {?} {?}
1005 lua_pushnil( L); // o "r" {c} {fqn} ... {?} {?} nil 1004 lua_pushnil(L); // o "r" {c} {fqn} ... {?} {?} nil
1006 lua_rawset( L, cache); // o "r" {c} {fqn} ... {?} 1005 lua_rawset(L, cache); // o "r" {c} {fqn} ... {?}
1007 STACK_CHECK( L, 0); 1006 STACK_CHECK(L, 0);
1008 return shortest_; 1007 return shortest_;
1009} 1008}
1010 1009
@@ -1169,7 +1168,7 @@ static char const* vt_names[] =
1169// we have to do it that way because we can't unbalance the stack between buffer operations 1168// we have to do it that way because we can't unbalance the stack between buffer operations
1170// namely, this means we can't push a function on top of the stack *after* we initialize the buffer! 1169// namely, this means we can't push a function on top of the stack *after* we initialize the buffer!
1171// luckily, this also works with earlier Lua versions 1170// luckily, this also works with earlier Lua versions
1172static int buf_writer( lua_State* L, void const* b, size_t size, void* ud) 1171[[nodiscard]] static int buf_writer(lua_State* L, void const* b, size_t size, void* ud)
1173{ 1172{
1174 luaL_Buffer* B = (luaL_Buffer*) ud; 1173 luaL_Buffer* B = (luaL_Buffer*) ud;
1175 if( !B->L) 1174 if( !B->L)
@@ -1339,7 +1338,7 @@ static void copy_func(Universe* U, Dest L2, int L2_cache_i, Source L, int i, Loo
1339static void copy_cached_func(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_) 1338static void copy_cached_func(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_)
1340{ 1339{
1341 FuncSubType funcSubType; 1340 FuncSubType funcSubType;
1342 /*lua_CFunction cfunc =*/ luaG_tocfunction( L, i, &funcSubType); // nullptr for LuaJIT-fast && bytecode functions 1341 std::ignore = luaG_tocfunction(L, i, &funcSubType); // nullptr for LuaJIT-fast && bytecode functions
1343 if( funcSubType == FST_Bytecode) 1342 if( funcSubType == FST_Bytecode)
1344 { 1343 {
1345 void* const aspointer = (void*)lua_topointer( L, i); 1344 void* const aspointer = (void*)lua_topointer( L, i);
@@ -1391,7 +1390,7 @@ static void copy_cached_func(Universe* U, Dest L2, int L2_cache_i, Source L, int
1391 1390
1392// ################################################################################################# 1391// #################################################################################################
1393 1392
1394static bool push_cached_metatable(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_) 1393[[nodiscard]] static bool push_cached_metatable(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode mode_, char const* upName_)
1395{ 1394{
1396 STACK_CHECK_START_REL(L, 0); 1395 STACK_CHECK_START_REL(L, 0);
1397 if( lua_getmetatable( L, i)) // ... mt 1396 if( lua_getmetatable( L, i)) // ... mt
@@ -1442,7 +1441,7 @@ static bool push_cached_metatable(Universe* U, Dest L2, int L2_cache_i, Source L
1442 1441
1443// ################################################################################################# 1442// #################################################################################################
1444 1443
1445static void inter_copy_keyvaluepair(Universe* U, Dest L2, int L2_cache_i, Source L, VT vt_, LookupMode mode_, char const* upName_) 1444[[nodiscard]] static void inter_copy_keyvaluepair(Universe* U, Dest L2, int L2_cache_i, Source L, VT vt_, LookupMode mode_, char const* upName_)
1446{ 1445{
1447 int val_i = lua_gettop(L); 1446 int val_i = lua_gettop(L);
1448 int key_i = val_i - 1; 1447 int key_i = val_i - 1;
@@ -1514,7 +1513,7 @@ static void inter_copy_keyvaluepair(Universe* U, Dest L2, int L2_cache_i, Source
1514*/ 1513*/
1515static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull }; 1514static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1516 1515
1517static bool copyclone(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, LookupMode mode_, char const* upName_) 1516[[nodiscard]] static bool copyclone(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, LookupMode mode_, char const* upName_)
1518{ 1517{
1519 void* const source = lua_touserdata( L, source_i_); 1518 void* const source = lua_touserdata( L, source_i_);
1520 source_i_ = lua_absindex( L, source_i_); 1519 source_i_ = lua_absindex( L, source_i_);
@@ -1600,7 +1599,11 @@ static bool copyclone(Universe* U, Dest L2, int L2_cache_i, Source L, int source
1600 // assign uservalues 1599 // assign uservalues
1601 while( uvi > 0) 1600 while( uvi > 0)
1602 { 1601 {
1603 inter_copy_one(U, L2, L2_cache_i, L, lua_absindex( L, -1), VT::NORMAL, mode_, upName_); // ... u uv 1602 std::ignore = inter_copy_one(U
1603 , L2, L2_cache_i
1604 , L, lua_absindex(L, -1)
1605 , VT::NORMAL, mode_, upName_
1606 ); // ... u uv
1604 lua_pop( L, 1); // ... mt __lanesclone [uv]* 1607 lua_pop( L, 1); // ... mt __lanesclone [uv]*
1605 // this pops the value from the stack 1608 // this pops the value from the stack
1606 lua_setiuservalue( L2, -2, uvi); // ... u 1609 lua_setiuservalue( L2, -2, uvi); // ... u
@@ -1629,7 +1632,7 @@ static bool copyclone(Universe* U, Dest L2, int L2_cache_i, Source L, int source
1629 1632
1630// ################################################################################################# 1633// #################################################################################################
1631 1634
1632static bool inter_copy_userdata(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_) 1635[[nodiscard]] static bool inter_copy_userdata(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1633{ 1636{
1634 STACK_CHECK_START_REL(L, 0); 1637 STACK_CHECK_START_REL(L, 0);
1635 STACK_CHECK_START_REL(L2, 0); 1638 STACK_CHECK_START_REL(L2, 0);
@@ -1679,7 +1682,7 @@ static bool inter_copy_userdata(Universe* U, Dest L2, int L2_cache_i, Source L,
1679 1682
1680// ################################################################################################# 1683// #################################################################################################
1681 1684
1682static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, VT vt_, LookupMode mode_, char const* upName_) 1685[[nodiscard]] static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L, int source_i_, VT vt_, LookupMode mode_, char const* upName_)
1683{ 1686{
1684 if (vt_ == VT::KEY) 1687 if (vt_ == VT::KEY)
1685 { 1688 {
@@ -1693,13 +1696,10 @@ static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L,
1693 if( lua_tocfunction( L, source_i_) == userdata_clone_sentinel) // we are actually copying a clonable full userdata from a keeper 1696 if( lua_tocfunction( L, source_i_) == userdata_clone_sentinel) // we are actually copying a clonable full userdata from a keeper
1694 { 1697 {
1695 // clone the full userdata again 1698 // clone the full userdata again
1696 size_t userdata_size = 0;
1697 void* source;
1698 void* clone;
1699 1699
1700 // let's see if we already restored this userdata 1700 // let's see if we already restored this userdata
1701 lua_getupvalue( L, source_i_, 2); // ... u 1701 lua_getupvalue( L, source_i_, 2); // ... u
1702 source = lua_touserdata( L, -1); 1702 void* source = lua_touserdata( L, -1);
1703 lua_pushlightuserdata( L2, source); // ... source 1703 lua_pushlightuserdata( L2, source); // ... source
1704 lua_rawget( L2, L2_cache_i); // ... u? 1704 lua_rawget( L2, L2_cache_i); // ... u?
1705 if( !lua_isnil( L2, -1)) 1705 if( !lua_isnil( L2, -1))
@@ -1712,12 +1712,13 @@ static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L,
1712 lua_pop( L2, 1); // ... 1712 lua_pop( L2, 1); // ...
1713 1713
1714 // this function has 2 upvalues: the fqn of its metatable, and the userdata itself 1714 // this function has 2 upvalues: the fqn of its metatable, and the userdata itself
1715 lookup_table( L2, L, source_i_, mode_, upName_); // ... mt 1715 std::ignore = lookup_table( L2, L, source_i_, mode_, upName_); // ... mt
1716 // originally 'source_i_' slot was the proxy closure, but from now on it indexes the actual userdata we extracted from it 1716 // originally 'source_i_' slot was the proxy closure, but from now on it indexes the actual userdata we extracted from it
1717 source_i_ = lua_gettop( L); 1717 source_i_ = lua_gettop( L);
1718 source = lua_touserdata( L, -1); 1718 source = lua_touserdata( L, -1);
1719 void* clone{ nullptr };
1719 // get the number of bytes to allocate for the clone 1720 // get the number of bytes to allocate for the clone
1720 userdata_size = (size_t) lua_rawlen( L, -1); 1721 size_t const userdata_size { lua_rawlen(L, -1) };
1721 { 1722 {
1722 // extract uservalues (don't transfer them yet) 1723 // extract uservalues (don't transfer them yet)
1723 int uvi = 0; 1724 int uvi = 0;
@@ -1738,7 +1739,11 @@ static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L,
1738 // transfer and assign uservalues 1739 // transfer and assign uservalues
1739 while( uvi > 0) 1740 while( uvi > 0)
1740 { 1741 {
1741 inter_copy_one(U, L2, L2_cache_i, L, lua_absindex(L, -1), vt_, mode_, upName_); // ... mt u uv 1742 std::ignore = inter_copy_one(U
1743 , L2, L2_cache_i
1744 , L, lua_absindex(L, -1)
1745 , vt_, mode_, upName_
1746 ); // ... mt u uv
1742 lua_pop( L, 1); // ... u [uv]* 1747 lua_pop( L, 1); // ... u [uv]*
1743 // this pops the value from the stack 1748 // this pops the value from the stack
1744 lua_setiuservalue( L2, -2, uvi); // ... mt u 1749 lua_setiuservalue( L2, -2, uvi); // ... mt u
@@ -1774,7 +1779,7 @@ static bool inter_copy_function(Universe* U, Dest L2, int L2_cache_i, Source L,
1774 1779
1775// ################################################################################################# 1780// #################################################################################################
1776 1781
1777static bool inter_copy_table(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_) 1782[[nodiscard]] static bool inter_copy_table(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1778{ 1783{
1779 if (vt_ == VT::KEY) 1784 if (vt_ == VT::KEY)
1780 { 1785 {
@@ -1846,7 +1851,7 @@ static bool inter_copy_table(Universe* U, Dest L2, int L2_cache_i, Source L, int
1846* 1851*
1847* Returns true if value was pushed, false if its type is non-supported. 1852* Returns true if value was pushed, false if its type is non-supported.
1848*/ 1853*/
1849bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_) 1854[[nodiscard]] bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_)
1850{ 1855{
1851 bool ret{ true }; 1856 bool ret{ true };
1852 int val_type = lua_type( L, i); 1857 int val_type = lua_type( L, i);
@@ -1971,78 +1976,77 @@ bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt
1971* 1976*
1972* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'. 1977* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'.
1973*/ 1978*/
1974int luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_) 1979[[nodiscard]] int luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_)
1975{ 1980{
1976 int top_L = lua_gettop(L); // ... {}n 1981 int const top_L{ lua_gettop(L) }; // ... {}n
1977 int top_L2 = lua_gettop(L2); // ... 1982 int const top_L2{ lua_gettop(L2) }; // ...
1978 int i, j;
1979 char tmpBuf[16]; 1983 char tmpBuf[16];
1980 char const* pBuf = U->verboseErrors ? tmpBuf : "?"; 1984 char const* pBuf{ U->verboseErrors ? tmpBuf : "?" };
1981 bool copyok{ true };
1982 1985
1983 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "luaG_inter_copy()\n" INDENT_END)); 1986 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy()\n" INDENT_END));
1984 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 1987 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
1985 1988
1986 if( n > top_L) 1989 if (n > top_L)
1987 { 1990 {
1988 // requesting to copy more than is available? 1991 // requesting to copy more than is available?
1989 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); 1992 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END));
1990 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 1993 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
1991 return -1; 1994 return -1;
1992 } 1995 }
1993 1996
1994 STACK_CHECK_START_REL(L2, 0); 1997 STACK_CHECK_START_REL(L2, 0);
1995 STACK_GROW( L2, n + 1); 1998 STACK_GROW(L2, n + 1);
1996 1999
1997 /* 2000 /*
1998 * Make a cache table for the duration of this copy. Collects tables and 2001 * Make a cache table for the duration of this copy. Collects tables and
1999 * function entries, avoiding the same entries to be passed on as multiple 2002 * function entries, avoiding the same entries to be passed on as multiple
2000 * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner! 2003 * copies. ESSENTIAL i.e. for handling upvalue tables in the right manner!
2001 */ 2004 */
2002 lua_newtable( L2); // ... cache 2005 lua_newtable(L2); // ... cache
2003 2006
2004 STACK_CHECK_START_REL(L, 0); 2007 STACK_CHECK_START_REL(L, 0);
2005 for( i = top_L - n + 1, j = 1; i <= top_L; ++ i, ++ j) 2008 bool copyok{ true };
2009 for (int i = top_L - n + 1, j = 1; i <= top_L; ++i, ++j)
2006 { 2010 {
2007 if( U->verboseErrors) 2011 if (U->verboseErrors)
2008 { 2012 {
2009 sprintf( tmpBuf, "arg_%d", j); 2013 sprintf(tmpBuf, "arg_%d", j);
2010 } 2014 }
2011 copyok = inter_copy_one(U, L2, top_L2 + 1, L, i, VT::NORMAL, mode_, pBuf); // ... cache {}n 2015 copyok = inter_copy_one(U, L2, top_L2 + 1, L, i, VT::NORMAL, mode_, pBuf); // ... cache {}n
2012 if( !copyok) 2016 if (!copyok)
2013 { 2017 {
2014 break; 2018 break;
2015 } 2019 }
2016 } 2020 }
2017 STACK_CHECK( L, 0); 2021 STACK_CHECK(L, 0);
2018 2022
2019 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 2023 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
2020 2024
2021 if( copyok) 2025 if (copyok)
2022 { 2026 {
2023 STACK_CHECK( L2, n + 1); 2027 STACK_CHECK(L2, n + 1);
2024 // Remove the cache table. Persistent caching would cause i.e. multiple 2028 // Remove the cache table. Persistent caching would cause i.e. multiple
2025 // messages passed in the same table to use the same table also in receiving end. 2029 // messages passed in the same table to use the same table also in receiving end.
2026 lua_remove( L2, top_L2 + 1); 2030 lua_remove(L2, top_L2 + 1);
2027 return 0; 2031 return 0;
2028 } 2032 }
2029 2033
2030 // error -> pop everything from the target state stack 2034 // error -> pop everything from the target state stack
2031 lua_settop( L2, top_L2); 2035 lua_settop(L2, top_L2);
2032 STACK_CHECK( L2, 0); 2036 STACK_CHECK(L2, 0);
2033 return -2; 2037 return -2;
2034} 2038}
2035 2039
2036// ################################################################################################# 2040// #################################################################################################
2037 2041
2038int luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_) 2042[[nodiscard]] int luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_)
2039{ 2043{
2040 int ret = luaG_inter_copy( U, L, L2, n, mode_); 2044 int const ret{ luaG_inter_copy(U, L, L2, n, mode_) };
2041 lua_pop( L, (int) n); 2045 lua_pop( L, n);
2042 return ret; 2046 return ret;
2043} 2047}
2044 2048
2045int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_) 2049[[nodiscard]] int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_)
2046{ 2050{
2047 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); 2051 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END));
2048 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 2052 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
@@ -2080,7 +2084,7 @@ int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, Lo
2080 else 2084 else
2081 { 2085 {
2082 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 2086 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
2083 luaG_inter_move(U, L, L2, 1, mode_); // moves the entry to L2 2087 std::ignore = luaG_inter_move(U, L, L2, 1, mode_); // moves the entry to L2
2084 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 2088 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
2085 lua_setfield(L2, -2, entry); // set package[entry] 2089 lua_setfield(L2, -2, entry); // set package[entry]
2086 } 2090 }