aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/cancel.cpp2
-rw-r--r--src/deep.cpp2
-rw-r--r--src/keeper.cpp12
-rw-r--r--src/lanes.cpp36
-rw-r--r--src/linda.cpp2
-rw-r--r--src/macros_and_utils.h8
-rw-r--r--src/state.cpp34
-rw-r--r--src/tools.cpp38
-rw-r--r--src/tools.h23
9 files changed, 93 insertions, 64 deletions
diff --git a/src/cancel.cpp b/src/cancel.cpp
index 3a01ac2..b3e52b6 100644
--- a/src/cancel.cpp
+++ b/src/cancel.cpp
@@ -213,7 +213,7 @@ CancelOp which_cancel_op(char const* op_string_)
213 lua_remove(L, idx_); // argument is processed, remove it 213 lua_remove(L, idx_); // argument is processed, remove it
214 if (op == CancelOp::Invalid) 214 if (op == CancelOp::Invalid)
215 { 215 {
216 std::ignore = luaL_error(L, "invalid hook option %s", str); // doesn't return 216 luaL_error(L, "invalid hook option %s", str); // doesn't return
217 } 217 }
218 return op; 218 return op;
219 } 219 }
diff --git a/src/deep.cpp b/src/deep.cpp
index 418724e..d0b8123 100644
--- a/src/deep.cpp
+++ b/src/deep.cpp
@@ -488,7 +488,7 @@ bool copydeep(Universe* U, Dest L2, int L2_cache_i, Source L, int i, LookupMode
488 { 488 {
489 // raise the error in the proper state (not the keeper) 489 // raise the error in the proper state (not the keeper)
490 lua_State* const errL{ (mode_ == LookupMode::FromKeeper) ? L2 : L }; 490 lua_State* const errL{ (mode_ == LookupMode::FromKeeper) ? L2 : L };
491 std::ignore = luaL_error(errL, errmsg); // doesn't return 491 luaL_error(errL, errmsg); // doesn't return
492 } 492 }
493 return true; 493 return true;
494} \ No newline at end of file 494} \ No newline at end of file
diff --git a/src/keeper.cpp b/src/keeper.cpp
index 37fcba5..f56c50c 100644
--- a/src/keeper.cpp
+++ b/src/keeper.cpp
@@ -656,7 +656,7 @@ void init_keepers(Universe* U, lua_State* L)
656 lua_pop(L, 1); // 656 lua_pop(L, 1); //
657 if (nb_keepers < 1) 657 if (nb_keepers < 1)
658 { 658 {
659 std::ignore = luaL_error(L, "Bad number of keepers (%d)", nb_keepers); // doesn't return 659 luaL_error(L, "Bad number of keepers (%d)", nb_keepers); // doesn't return
660 } 660 }
661 STACK_CHECK(L, 0); 661 STACK_CHECK(L, 0);
662 662
@@ -671,7 +671,7 @@ void init_keepers(Universe* U, lua_State* L)
671 U->keepers = static_cast<Keepers*>(U->internal_allocator.alloc(bytes)); 671 U->keepers = static_cast<Keepers*>(U->internal_allocator.alloc(bytes));
672 if (U->keepers == nullptr) 672 if (U->keepers == nullptr)
673 { 673 {
674 std::ignore = luaL_error(L, "init_keepers() failed while creating keeper array; out of memory"); // doesn't return 674 luaL_error(L, "init_keepers() failed while creating keeper array; out of memory"); // doesn't return
675 } 675 }
676 U->keepers->Keepers::Keepers(); 676 U->keepers->Keepers::Keepers();
677 U->keepers->gc_threshold = keepers_gc_threshold; 677 U->keepers->gc_threshold = keepers_gc_threshold;
@@ -688,7 +688,7 @@ void init_keepers(Universe* U, lua_State* L)
688 lua_State* const K{ create_state(U, L) }; 688 lua_State* const K{ create_state(U, L) };
689 if (K == nullptr) 689 if (K == nullptr)
690 { 690 {
691 std::ignore = luaL_error(L, "init_keepers() failed while creating keeper states; out of memory"); // doesn't return 691 luaL_error(L, "init_keepers() failed while creating keeper states; out of memory"); // doesn't return
692 } 692 }
693 693
694 U->keepers->keeper_array[i].L = K; 694 U->keepers->keeper_array[i].L = K;
@@ -717,7 +717,7 @@ void init_keepers(Universe* U, lua_State* L)
717 if (!lua_isnil(L, -1)) 717 if (!lua_isnil(L, -1))
718 { 718 {
719 // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately 719 // when copying with mode LookupMode::ToKeeper, error message is pushed at the top of the stack, not raised immediately
720 if (luaG_inter_copy_package(U, Source{ L }, Dest{ K }, -1, LookupMode::ToKeeper)) 720 if (luaG_inter_copy_package(U, Source{ L }, Dest{ K }, -1, LookupMode::ToKeeper) != InterCopyResult::Success)
721 { 721 {
722 // if something went wrong, the error message is at the top of the stack 722 // if something went wrong, the error message is at the top of the stack
723 lua_remove(L, -2); // error_msg 723 lua_remove(L, -2); // error_msg
@@ -840,7 +840,7 @@ int keeper_call(Universe* U, lua_State* K, keeper_api_t func_, lua_State* L, voi
840 840
841 lua_pushlightuserdata(K, linda); 841 lua_pushlightuserdata(K, linda);
842 842
843 if ((args == 0) || luaG_inter_copy(U, Source{ L }, Dest{ K }, args, LookupMode::ToKeeper) == 0) // L->K 843 if ((args == 0) || luaG_inter_copy(U, Source{ L }, Dest{ K }, args, LookupMode::ToKeeper) == InterCopyResult::Success) // L->K
844 { 844 {
845 lua_call(K, 1 + args, LUA_MULTRET); 845 lua_call(K, 1 + args, LUA_MULTRET);
846 retvals = lua_gettop(K) - Ktos; 846 retvals = lua_gettop(K) - Ktos;
@@ -848,7 +848,7 @@ int keeper_call(Universe* U, lua_State* K, keeper_api_t func_, lua_State* L, voi
848 // this may interrupt a lane, causing the destruction of the underlying OS thread 848 // this may interrupt a lane, causing the destruction of the underlying OS thread
849 // after this, another lane making use of this keeper can get an error code from the mutex-locking function 849 // after this, another lane making use of this keeper can get an error code from the mutex-locking function
850 // when attempting to grab the mutex again (WINVER <= 0x400 does this, but locks just fine, I don't know about pthread) 850 // when attempting to grab the mutex again (WINVER <= 0x400 does this, but locks just fine, I don't know about pthread)
851 if ((retvals > 0) && luaG_inter_move(U, Source{ K }, Dest{ L }, retvals, LookupMode::FromKeeper) != 0) // K->L 851 if ((retvals > 0) && luaG_inter_move(U, Source{ K }, Dest{ L }, retvals, LookupMode::FromKeeper) != InterCopyResult::Success) // K->L
852 { 852 {
853 retvals = -1; 853 retvals = -1;
854 } 854 }
diff --git a/src/lanes.cpp b/src/lanes.cpp
index f549359..462de0f 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -540,7 +540,7 @@ static void selfdestruct_add(Lane* lane_)
540 if (lane != SELFDESTRUCT_END) 540 if (lane != SELFDESTRUCT_END)
541 { 541 {
542 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it) 542 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it)
543 std::ignore = luaL_error(L, "Zombie thread %s refuses to die!", lane->debug_name); // doesn't return 543 luaL_error(L, "Zombie thread %s refuses to die!", lane->debug_name); // doesn't return
544 } 544 }
545 } 545 }
546 546
@@ -1134,7 +1134,8 @@ LUAG_FUNC(lane_new)
1134 { 1134 {
1135 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END)); 1135 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END));
1136 // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack 1136 // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack
1137 std::ignore = luaG_inter_copy_package(U, Source{ L }, Dest{ L2 }, package_idx, LookupMode::LaneBody); 1137 [[maybe_unused]] InterCopyResult const ret{ luaG_inter_copy_package(U, Source{ L }, Dest{ L2 }, package_idx, LookupMode::LaneBody) };
1138 ASSERT_L(ret == InterCopyResult::Success); // either all went well, or we should not even get here
1138 } 1139 }
1139 1140
1140 // modules to require in the target lane *before* the function is transfered! 1141 // modules to require in the target lane *before* the function is transfered!
@@ -1146,7 +1147,7 @@ LUAG_FUNC(lane_new)
1146 // should not happen, was checked in lanes.lua before calling lane_new() 1147 // should not happen, was checked in lanes.lua before calling lane_new()
1147 if (lua_type(L, required_idx) != LUA_TTABLE) 1148 if (lua_type(L, required_idx) != LUA_TTABLE)
1148 { 1149 {
1149 return luaL_error(L, "expected required module list as a table, got %s", luaL_typename(L, required_idx)); 1150 luaL_error(L, "expected required module list as a table, got %s", luaL_typename(L, required_idx)); // doesn't return
1150 } 1151 }
1151 1152
1152 lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil 1153 lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil
@@ -1154,7 +1155,7 @@ LUAG_FUNC(lane_new)
1154 { 1155 {
1155 if (lua_type(L, -1) != LUA_TSTRING || lua_type(L, -2) != LUA_TNUMBER || lua_tonumber(L, -2) != nbRequired) 1156 if (lua_type(L, -1) != LUA_TSTRING || lua_type(L, -2) != LUA_TNUMBER || lua_tonumber(L, -2) != nbRequired)
1156 { 1157 {
1157 return luaL_error(L, "required module list should be a list of strings"); 1158 luaL_error(L, "required module list should be a list of strings"); // doesn't return
1158 } 1159 }
1159 else 1160 else
1160 { 1161 {
@@ -1168,7 +1169,7 @@ LUAG_FUNC(lane_new)
1168 if (lua_isnil( L2, -1)) 1169 if (lua_isnil( L2, -1))
1169 { 1170 {
1170 lua_pop( L2, 1); // 1171 lua_pop( L2, 1); //
1171 return luaL_error(L, "cannot pre-require modules without loading 'package' library first"); 1172 luaL_error(L, "cannot pre-require modules without loading 'package' library first"); // doesn't return
1172 } 1173 }
1173 else 1174 else
1174 { 1175 {
@@ -1203,7 +1204,7 @@ LUAG_FUNC(lane_new)
1203 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer globals\n" INDENT_END)); 1204 DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: transfer globals\n" INDENT_END));
1204 if (!lua_istable(L, globals_idx)) 1205 if (!lua_istable(L, globals_idx))
1205 { 1206 {
1206 return luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); 1207 luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); // doesn't return
1207 } 1208 }
1208 1209
1209 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 1210 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
@@ -1230,11 +1231,11 @@ LUAG_FUNC(lane_new)
1230 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); 1231 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END));
1231 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 1232 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
1232 lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func 1233 lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func
1233 int const res{ luaG_inter_move(U, Source{ L }, Dest{ L2 }, 1, LookupMode::LaneBody) }; // func libs priority globals package required gc_cb [... args ...] // func 1234 InterCopyResult const res{ luaG_inter_move(U, Source{ L }, Dest{ L2 }, 1, LookupMode::LaneBody) }; // func libs priority globals package required gc_cb [... args ...] // func
1234 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 1235 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
1235 if (res != 0) 1236 if (res != InterCopyResult::Success)
1236 { 1237 {
1237 return luaL_error(L, "tried to copy unsupported types"); 1238 luaL_error(L, "tried to copy unsupported types"); // doesn't return
1238 } 1239 }
1239 } 1240 }
1240 else if (lua_type(L, 1) == LUA_TSTRING) 1241 else if (lua_type(L, 1) == LUA_TSTRING)
@@ -1243,7 +1244,7 @@ LUAG_FUNC(lane_new)
1243 // compile the string 1244 // compile the string
1244 if (luaL_loadstring(L2, lua_tostring(L, 1)) != 0) // func 1245 if (luaL_loadstring(L2, lua_tostring(L, 1)) != 0) // func
1245 { 1246 {
1246 return luaL_error(L, "error when parsing lane function code"); 1247 luaL_error(L, "error when parsing lane function code"); // doesn't return
1247 } 1248 }
1248 } 1249 }
1249 STACK_CHECK(L, 0); 1250 STACK_CHECK(L, 0);
@@ -1253,14 +1254,13 @@ LUAG_FUNC(lane_new)
1253 // revive arguments 1254 // revive arguments
1254 if (nargs > 0) 1255 if (nargs > 0)
1255 { 1256 {
1256 int res;
1257 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END)); 1257 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END));
1258 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 1258 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
1259 res = luaG_inter_move(U, Source{ L }, Dest{ L2 }, nargs, LookupMode::LaneBody); // func libs priority globals package required gc_cb // func [... args ...] 1259 InterCopyResult const res{ luaG_inter_move(U, Source{ L }, Dest{ L2 }, nargs, LookupMode::LaneBody) }; // func libs priority globals package required gc_cb // func [... args ...]
1260 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 1260 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
1261 if (res != 0) 1261 if (res != InterCopyResult::Success)
1262 { 1262 {
1263 return luaL_error(L, "tried to copy unsupported types"); 1263 luaL_error(L, "tried to copy unsupported types"); // doesn't return
1264 } 1264 }
1265 } 1265 }
1266 STACK_CHECK(L, -nargs); 1266 STACK_CHECK(L, -nargs);
@@ -1420,9 +1420,9 @@ LUAG_FUNC(thread_join)
1420 case Lane::Done: 1420 case Lane::Done:
1421 { 1421 {
1422 int const n{ lua_gettop(L2) }; // whole L2 stack 1422 int const n{ lua_gettop(L2) }; // whole L2 stack
1423 if ((n > 0) && (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != 0)) 1423 if ((n > 0) && (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != InterCopyResult::Success))
1424 { 1424 {
1425 return luaL_error(L, "tried to copy unsupported types"); 1425 luaL_error(L, "tried to copy unsupported types"); // doesn't return
1426 } 1426 }
1427 ret = n; 1427 ret = n;
1428 } 1428 }
@@ -1434,9 +1434,9 @@ LUAG_FUNC(thread_join)
1434 STACK_GROW(L, 3); 1434 STACK_GROW(L, 3);
1435 lua_pushnil(L); 1435 lua_pushnil(L);
1436 // even when ERROR_FULL_STACK, if the error is not LUA_ERRRUN, the handler wasn't called, and we only have 1 error message on the stack ... 1436 // even when ERROR_FULL_STACK, if the error is not LUA_ERRRUN, the handler wasn't called, and we only have 1 error message on the stack ...
1437 if (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != 0) // nil "err" [trace] 1437 if (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != InterCopyResult::Success) // nil "err" [trace]
1438 { 1438 {
1439 return luaL_error(L, "tried to copy unsupported types: %s", lua_tostring(L, -n)); 1439 luaL_error(L, "tried to copy unsupported types: %s", lua_tostring(L, -n)); // doesn't return
1440 } 1440 }
1441 ret = 1 + n; 1441 ret = 1 + n;
1442 } 1442 }
diff --git a/src/linda.cpp b/src/linda.cpp
index 50964ad..e749f52 100644
--- a/src/linda.cpp
+++ b/src/linda.cpp
@@ -162,7 +162,7 @@ static void check_key_types(lua_State* L, int start_, int end_)
162 { 162 {
163 continue; 163 continue;
164 } 164 }
165 std::ignore = luaL_error(L, "argument #%d: invalid key type (not a boolean, string, number or light userdata)", i); 165 luaL_error(L, "argument #%d: invalid key type (not a boolean, string, number or light userdata)", i); // doesn't return
166 } 166 }
167} 167}
168 168
diff --git a/src/macros_and_utils.h b/src/macros_and_utils.h
index 31ae8bd..99e49f9 100644
--- a/src/macros_and_utils.h
+++ b/src/macros_and_utils.h
@@ -77,7 +77,7 @@ class StackChecker
77 if ((offset_ < 0) || (m_oldtop < 0)) 77 if ((offset_ < 0) || (m_oldtop < 0))
78 { 78 {
79 assert(false); 79 assert(false);
80 std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); 80 luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), offset_, file_, line_); // doesn't return
81 } 81 }
82 } 82 }
83 83
@@ -88,7 +88,7 @@ class StackChecker
88 if (lua_gettop(m_L) != pos_) 88 if (lua_gettop(m_L) != pos_)
89 { 89 {
90 assert(false); 90 assert(false);
91 std::ignore = luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); 91 luaL_error(m_L, "STACK INIT ASSERT failed (%d not %d): %s:%d", lua_gettop(m_L), pos_, file_, line_); // doesn't return
92 } 92 }
93 } 93 }
94 94
@@ -108,7 +108,7 @@ class StackChecker
108 if (actual != expected_) 108 if (actual != expected_)
109 { 109 {
110 assert(false); 110 assert(false);
111 std::ignore = luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); 111 luaL_error(m_L, "STACK ASSERT failed (%d not %d): %s:%d", actual, expected_, file_, line_); // doesn't return
112 } 112 }
113 } 113 }
114 } 114 }
@@ -128,7 +128,7 @@ inline void STACK_GROW(lua_State* L, int n_)
128{ 128{
129 if (!lua_checkstack(L, n_)) 129 if (!lua_checkstack(L, n_))
130 { 130 {
131 std::ignore = luaL_error(L, "Cannot grow stack!"); 131 luaL_error(L, "Cannot grow stack!"); // doesn't return
132 } 132 }
133} 133}
134 134
diff --git a/src/state.cpp b/src/state.cpp
index 496e21e..4a5f995 100644
--- a/src/state.cpp
+++ b/src/state.cpp
@@ -8,7 +8,7 @@
8=============================================================================== 8===============================================================================
9 9
10Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com> 10Copyright (C) 2002-10 Asko Kauppi <akauppi@gmail.com>
112011-21 benoit Germain <bnt.germain@gmail.com> 112011-24 benoit Germain <bnt.germain@gmail.com>
12 12
13Permission is hereby granted, free of charge, to any person obtaining a copy 13Permission is hereby granted, free of charge, to any person obtaining a copy
14of this software and associated documentation files (the "Software"), to deal 14of this software and associated documentation files (the "Software"), to deal
@@ -79,6 +79,8 @@ THE SOFTWARE.
79 return lua_gettop(L); // result(s) 79 return lua_gettop(L); // result(s)
80} 80}
81 81
82// #################################################################################################
83
82/* 84/*
83* Serialize calls to 'require', if it exists 85* Serialize calls to 'require', if it exists
84*/ 86*/
@@ -117,6 +119,7 @@ void serialize_require(DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L)
117 return 1; 119 return 1;
118} 120}
119 121
122// #################################################################################################
120 123
121static luaL_Reg const libs[] = 124static luaL_Reg const libs[] =
122{ 125{
@@ -154,7 +157,9 @@ static luaL_Reg const libs[] =
154 { nullptr, nullptr } 157 { nullptr, nullptr }
155}; 158};
156 159
157static void open1lib( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, char const* name_, size_t len_) 160// #################################################################################################
161
162static void open1lib(DEBUGSPEW_PARAM_COMMA(Universe* U) lua_State* L, char const* name_, size_t len_)
158{ 163{
159 int i; 164 int i;
160 for( i = 0; libs[i].name; ++ i) 165 for( i = 0; libs[i].name; ++ i)
@@ -183,6 +188,7 @@ static void open1lib( DEBUGSPEW_PARAM_COMMA( Universe* U) lua_State* L, char con
183 } 188 }
184} 189}
185 190
191// #################################################################################################
186 192
187// just like lua_xmove, args are (from, to) 193// just like lua_xmove, args are (from, to)
188static void copy_one_time_settings(Universe* U, Source L, Dest L2) 194static void copy_one_time_settings(Universe* U, Source L, Dest L2)
@@ -194,19 +200,21 @@ static void copy_one_time_settings(Universe* U, Source L, Dest L2)
194 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END)); 200 DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "copy_one_time_settings()\n" INDENT_END));
195 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 201 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
196 202
197 CONFIG_REGKEY.pushValue(L); // config 203 CONFIG_REGKEY.pushValue(L); // config
198 // copy settings from from source to destination registry 204 // copy settings from from source to destination registry
199 if( luaG_inter_move( U, L, L2, 1, LookupMode::LaneBody) < 0) // // config 205 if (luaG_inter_move(U, L, L2, 1, LookupMode::LaneBody) != InterCopyResult::Success) // // config
200 { 206 {
201 (void) luaL_error( L, "failed to copy settings when loading lanes.core"); 207 luaL_error( L, "failed to copy settings when loading lanes.core"); // doesn't return
202 } 208 }
203 // set L2:_R[CONFIG_REGKEY] = settings 209 // set L2:_R[CONFIG_REGKEY] = settings
204 CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config 210 CONFIG_REGKEY.setValue(L2, [](lua_State* L) { lua_insert(L, -2); }); // config
205 STACK_CHECK( L2, 0); 211 STACK_CHECK(L2, 0);
206 STACK_CHECK( L, 0); 212 STACK_CHECK(L, 0);
207 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 213 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
208} 214}
209 215
216// #################################################################################################
217
210void initialize_on_state_create( Universe* U, lua_State* L) 218void initialize_on_state_create( Universe* U, lua_State* L)
211{ 219{
212 STACK_CHECK_START_REL(L, 1); // settings 220 STACK_CHECK_START_REL(L, 1); // settings
@@ -238,7 +246,9 @@ void initialize_on_state_create( Universe* U, lua_State* L)
238 STACK_CHECK(L, 1); 246 STACK_CHECK(L, 1);
239} 247}
240 248
241lua_State* create_state( Universe* U, lua_State* from_) 249// #################################################################################################
250
251lua_State* create_state(Universe* U, lua_State* from_)
242{ 252{
243 lua_State* L; 253 lua_State* L;
244#if LUAJIT_FLAVOR() == 64 254#if LUAJIT_FLAVOR() == 64
@@ -264,11 +274,13 @@ lua_State* create_state( Universe* U, lua_State* from_)
264 274
265 if (L == nullptr) 275 if (L == nullptr)
266 { 276 {
267 std::ignore = luaL_error( from_, "luaG_newstate() failed while creating state; out of memory"); 277 luaL_error(from_, "luaG_newstate() failed while creating state; out of memory"); // doesn't return
268 } 278 }
269 return L; 279 return L;
270} 280}
271 281
282// #################################################################################################
283
272void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMode mode_) 284void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMode mode_)
273{ 285{
274 if (U->on_state_create_func != nullptr) 286 if (U->on_state_create_func != nullptr)
@@ -304,6 +316,8 @@ void call_on_state_create(Universe* U, lua_State* L, lua_State* from_, LookupMod
304 } 316 }
305} 317}
306 318
319// #################################################################################################
320
307/* 321/*
308* Like 'luaL_openlibs()' but allows the set of libraries be selected 322* Like 'luaL_openlibs()' but allows the set of libraries be selected
309* 323*
diff --git a/src/tools.cpp b/src/tools.cpp
index 9207df6..aa95f04 100644
--- a/src/tools.cpp
+++ b/src/tools.cpp
@@ -1415,7 +1415,7 @@ static void copy_cached_func(Universe* U, Dest L2, int L2_cache_i, Source L, int
1415 lua_pop(L2, 1); // _R[REG_MTID] 1415 lua_pop(L2, 1); // _R[REG_MTID]
1416 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_gettop(L), VT::METATABLE, mode_, upName_)) // _R[REG_MTID] mt? 1416 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_gettop(L), VT::METATABLE, mode_, upName_)) // _R[REG_MTID] mt?
1417 { 1417 {
1418 std::ignore = luaL_error(L, "Error copying a metatable"); // doesn't return 1418 luaL_error(L, "Error copying a metatable"); // doesn't return
1419 } 1419 }
1420 1420
1421 STACK_CHECK(L2, 2); // _R[REG_MTID] mt 1421 STACK_CHECK(L2, 2); // _R[REG_MTID] mt
@@ -1600,7 +1600,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1600 { 1600 {
1601 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_absindex(L, -1), VT::NORMAL, mode_, upName_)) // ... u uv 1601 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_absindex(L, -1), VT::NORMAL, mode_, upName_)) // ... u uv
1602 { 1602 {
1603 std::ignore = luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1)); // doesn't return 1603 luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1)); // doesn't return
1604 } 1604 }
1605 lua_pop( L, 1); // ... mt __lanesclone [uv]* 1605 lua_pop( L, 1); // ... mt __lanesclone [uv]*
1606 // this pops the value from the stack 1606 // this pops the value from the stack
@@ -1670,7 +1670,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1670 } 1670 }
1671 else // raise an error 1671 else // raise an error
1672 { 1672 {
1673 std::ignore = luaL_error(L, "can't copy non-deep full userdata across lanes"); // doesn't return 1673 luaL_error(L, "can't copy non-deep full userdata across lanes"); // doesn't return
1674 } 1674 }
1675 1675
1676 STACK_CHECK(L2, 1); 1676 STACK_CHECK(L2, 1);
@@ -1739,7 +1739,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1739 { 1739 {
1740 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_absindex(L, -1), vt_, mode_, upName_)) // ... mt u uv 1740 if (!inter_copy_one(U, L2, L2_cache_i, L, lua_absindex(L, -1), vt_, mode_, upName_)) // ... mt u uv
1741 { 1741 {
1742 std::ignore = luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1)); // doesn't return 1742 luaL_error(L, "Cannot copy upvalue type '%s'", luaL_typename(L, -1)); // doesn't return
1743 } 1743 }
1744 lua_pop(L, 1); // ... u [uv]* 1744 lua_pop(L, 1); // ... u [uv]*
1745 // this pops the value from the stack 1745 // this pops the value from the stack
@@ -1973,7 +1973,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1973* 1973*
1974* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'. 1974* Note: Parameters are in this order ('L' = from first) to be same as 'lua_xmove'.
1975*/ 1975*/
1976[[nodiscard]] int luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_) 1976[[nodiscard]] InterCopyResult luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_)
1977{ 1977{
1978 int const top_L{ lua_gettop(L) }; // ... {}n 1978 int const top_L{ lua_gettop(L) }; // ... {}n
1979 int const top_L2{ lua_gettop(L2) }; // ... 1979 int const top_L2{ lua_gettop(L2) }; // ...
@@ -1988,7 +1988,7 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
1988 // requesting to copy more than is available? 1988 // requesting to copy more than is available?
1989 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "nothing to copy()\n" INDENT_END)); 1989 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)); 1990 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
1991 return -1; 1991 return InterCopyResult::NotEnoughValues;
1992 } 1992 }
1993 1993
1994 STACK_CHECK_START_REL(L2, 0); 1994 STACK_CHECK_START_REL(L2, 0);
@@ -2025,25 +2025,31 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
2025 // Remove the cache table. Persistent caching would cause i.e. multiple 2025 // Remove the cache table. Persistent caching would cause i.e. multiple
2026 // messages passed in the same table to use the same table also in receiving end. 2026 // messages passed in the same table to use the same table also in receiving end.
2027 lua_remove(L2, top_L2 + 1); 2027 lua_remove(L2, top_L2 + 1);
2028 return 0; 2028 return InterCopyResult::Success;
2029 } 2029 }
2030 2030
2031 // error -> pop everything from the target state stack 2031 // error -> pop everything from the target state stack
2032 lua_settop(L2, top_L2); 2032 lua_settop(L2, top_L2);
2033 STACK_CHECK(L2, 0); 2033 STACK_CHECK(L2, 0);
2034 return -2; 2034 return InterCopyResult::Error;
2035} 2035}
2036 2036
2037// ################################################################################################# 2037// #################################################################################################
2038 2038
2039[[nodiscard]] int luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_) 2039[[nodiscard]] InterCopyResult luaG_inter_move(Universe* U, Source L, Dest L2, int n_, LookupMode mode_)
2040{ 2040{
2041 int const ret{ luaG_inter_copy(U, L, L2, n, mode_) }; 2041 InterCopyResult const ret{ luaG_inter_copy(U, L, L2, n_, mode_) };
2042 lua_pop( L, n); 2042 lua_pop( L, n_);
2043 return ret; 2043 return ret;
2044} 2044}
2045 2045
2046[[nodiscard]] int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_) 2046// #################################################################################################
2047
2048// transfers stuff from L->_G["package"] to L2->_G["package"]
2049// returns InterCopyResult::Success if everything is fine
2050// returns InterCopyResult::Error if pushed an error message in L
2051// else raise an error in L
2052[[nodiscard]] InterCopyResult luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_)
2047{ 2053{
2048 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END)); 2054 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "luaG_inter_copy_package()\n" INDENT_END));
2049 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); 2055 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed));
@@ -2056,7 +2062,11 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
2056 lua_pushfstring(L, "expected package as table, got %s", luaL_typename(L, package_idx_)); 2062 lua_pushfstring(L, "expected package as table, got %s", luaL_typename(L, package_idx_));
2057 STACK_CHECK(L, 1); 2063 STACK_CHECK(L, 1);
2058 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later 2064 // raise the error when copying from lane to lane, else just leave it on the stack to be raised later
2059 return (mode_ == LookupMode::LaneBody) ? lua_error(L) : 1; 2065 if (mode_ == LookupMode::LaneBody)
2066 {
2067 lua_error(L); // doesn't return
2068 }
2069 return InterCopyResult::Error;
2060 } 2070 }
2061 lua_getglobal(L2, "package"); 2071 lua_getglobal(L2, "package");
2062 if (!lua_isnil(L2, -1)) // package library not loaded: do nothing 2072 if (!lua_isnil(L2, -1)) // package library not loaded: do nothing
@@ -2095,5 +2105,5 @@ static constexpr UniqueKey CLONABLES_CACHE_KEY{ 0xD04EE018B3DEE8F5ull };
2095 STACK_CHECK(L2, 0); 2105 STACK_CHECK(L2, 0);
2096 STACK_CHECK(L, 0); 2106 STACK_CHECK(L, 0);
2097 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); 2107 DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed));
2098 return 0; 2108 return InterCopyResult::Success;
2099} 2109}
diff --git a/src/tools.h b/src/tools.h
index f0de7ec..dce7378 100644
--- a/src/tools.h
+++ b/src/tools.h
@@ -1,8 +1,6 @@
1#pragma once 1#pragma once
2 2
3#include "threading.h"
4#include "deep.h" 3#include "deep.h"
5
6#include "macros_and_utils.h" 4#include "macros_and_utils.h"
7 5
8// forwards 6// forwards
@@ -11,13 +9,13 @@ class Universe;
11// ################################################################################################ 9// ################################################################################################
12 10
13#ifdef _DEBUG 11#ifdef _DEBUG
14void luaG_dump( lua_State* L); 12void luaG_dump(lua_State* L);
15#endif // _DEBUG 13#endif // _DEBUG
16 14
17// ################################################################################################ 15// ################################################################################################
18 16
19void push_registry_subtable_mode( lua_State* L, UniqueKey key_, const char* mode_); 17void push_registry_subtable_mode(lua_State* L, UniqueKey key_, const char* mode_);
20void push_registry_subtable( lua_State* L, UniqueKey key_); 18void push_registry_subtable(lua_State* L, UniqueKey key_);
21 19
22enum class VT 20enum class VT
23{ 21{
@@ -25,14 +23,21 @@ enum class VT
25 KEY, 23 KEY,
26 METATABLE 24 METATABLE
27}; 25};
26
27enum class InterCopyResult
28{
29 Success,
30 NotEnoughValues,
31 Error
32};
33
28[[nodiscard]] bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_); 34[[nodiscard]] bool inter_copy_one(Universe* U, Dest L2, int L2_cache_i, Source L, int i, VT vt_, LookupMode mode_, char const* upName_);
29 35
30// ################################################################################################ 36// ################################################################################################
31 37
32[[nodiscard]] int luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_); 38[[nodiscard]] InterCopyResult luaG_inter_copy_package(Universe* U, Source L, Dest L2, int package_idx_, LookupMode mode_);
33 39[[nodiscard]] InterCopyResult luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_);
34[[nodiscard]] int luaG_inter_copy(Universe* U, Source L, Dest L2, int n, LookupMode mode_); 40[[nodiscard]] InterCopyResult luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_);
35[[nodiscard]] int luaG_inter_move(Universe* U, Source L, Dest L2, int n, LookupMode mode_);
36 41
37[[nodiscard]] int luaG_nameof(lua_State* L); 42[[nodiscard]] int luaG_nameof(lua_State* L);
38 43