diff options
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r-- | src/lanes.cpp | 61 |
1 files changed, 30 insertions, 31 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index 8b4410a..17d4f67 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -946,13 +946,12 @@ LUAG_FUNC(require) | |||
946 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 946 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); |
947 | STACK_CHECK_START_REL(L, 0); | 947 | STACK_CHECK_START_REL(L, 0); |
948 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); | 948 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END, name)); |
949 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 949 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
950 | lua_pushvalue(L, lua_upvalueindex(1)); // "name" require | 950 | lua_pushvalue(L, lua_upvalueindex(1)); // "name" require |
951 | lua_insert(L, 1); // require "name" | 951 | lua_insert(L, 1); // require "name" |
952 | lua_call(L, nargs, 1); // module | 952 | lua_call(L, nargs, 1); // module |
953 | populate_func_lookup_table(L, -1, name); | 953 | populate_func_lookup_table(L, -1, name); |
954 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END, name)); | 954 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END, name)); |
955 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
956 | STACK_CHECK(L, 0); | 955 | STACK_CHECK(L, 0); |
957 | return 1; | 956 | return 1; |
958 | } | 957 | } |
@@ -972,10 +971,9 @@ LUAG_FUNC(register) | |||
972 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); | 971 | DEBUGSPEW_CODE(Universe* U = universe_get(L)); |
973 | STACK_CHECK_START_REL(L, 0); // "name" mod_table | 972 | STACK_CHECK_START_REL(L, 0); // "name" mod_table |
974 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); | 973 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END, name)); |
975 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 974 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
976 | populate_func_lookup_table(L, -1, name); | 975 | populate_func_lookup_table(L, -1, name); |
977 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END, name)); | 976 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END, name)); |
978 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
979 | STACK_CHECK(L, 0); | 977 | STACK_CHECK(L, 0); |
980 | return 0; | 978 | return 0; |
981 | } | 979 | } |
@@ -1022,7 +1020,6 @@ LUAG_FUNC(lane_new) | |||
1022 | 1020 | ||
1023 | /* --- Create and prepare the sub state --- */ | 1021 | /* --- Create and prepare the sub state --- */ |
1024 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); | 1022 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END)); |
1025 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | ||
1026 | 1023 | ||
1027 | // populate with selected libraries at the same time | 1024 | // populate with selected libraries at the same time |
1028 | lua_State* const L2{ luaG_newstate(U, Source{ L }, libs_str) }; // L // L2 | 1025 | lua_State* const L2{ luaG_newstate(U, Source{ L }, libs_str) }; // L // L2 |
@@ -1041,7 +1038,8 @@ LUAG_FUNC(lane_new) | |||
1041 | lua_State* const m_L; | 1038 | lua_State* const m_L; |
1042 | Lane* m_lane{ nullptr }; | 1039 | Lane* m_lane{ nullptr }; |
1043 | int const m_gc_cb_idx; | 1040 | int const m_gc_cb_idx; |
1044 | DEBUGSPEW_CODE(Universe* const U); // for DEBUGSPEW only (hence the absence of m_ prefix) | 1041 | DEBUGSPEW_CODE(Universe* const U); |
1042 | DEBUGSPEW_CODE(DebugSpewIndentScope m_scope); | ||
1045 | 1043 | ||
1046 | public: | 1044 | public: |
1047 | 1045 | ||
@@ -1050,7 +1048,9 @@ LUAG_FUNC(lane_new) | |||
1050 | , m_lane{ lane_ } | 1048 | , m_lane{ lane_ } |
1051 | , m_gc_cb_idx{ gc_cb_idx_ } | 1049 | , m_gc_cb_idx{ gc_cb_idx_ } |
1052 | DEBUGSPEW_COMMA_PARAM(U{ U_ }) | 1050 | DEBUGSPEW_COMMA_PARAM(U{ U_ }) |
1053 | {} | 1051 | DEBUGSPEW_COMMA_PARAM(m_scope{ U_ }) |
1052 | { | ||
1053 | } | ||
1054 | 1054 | ||
1055 | ~OnExit() | 1055 | ~OnExit() |
1056 | { | 1056 | { |
@@ -1132,7 +1132,8 @@ LUAG_FUNC(lane_new) | |||
1132 | { | 1132 | { |
1133 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END)); | 1133 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END)); |
1134 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack | 1134 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack |
1135 | [[maybe_unused]] InterCopyResult const ret{ luaG_inter_copy_package(U, Source{ L }, Dest{ L2 }, package_idx, LookupMode::LaneBody) }; | 1135 | InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, SourceIndex{ package_idx }, {}, {}, {} }; |
1136 | [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() }; | ||
1136 | ASSERT_L(ret == InterCopyResult::Success); // either all went well, or we should not even get here | 1137 | ASSERT_L(ret == InterCopyResult::Success); // either all went well, or we should not even get here |
1137 | } | 1138 | } |
1138 | 1139 | ||
@@ -1141,7 +1142,7 @@ LUAG_FUNC(lane_new) | |||
1141 | { | 1142 | { |
1142 | int nbRequired = 1; | 1143 | int nbRequired = 1; |
1143 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END)); | 1144 | DEBUGSPEW_CODE( fprintf( stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END)); |
1144 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 1145 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1145 | // should not happen, was checked in lanes.lua before calling lane_new() | 1146 | // should not happen, was checked in lanes.lua before calling lane_new() |
1146 | if (lua_type(L, required_idx) != LUA_TTABLE) | 1147 | if (lua_type(L, required_idx) != LUA_TTABLE) |
1147 | { | 1148 | { |
@@ -1175,10 +1176,8 @@ LUAG_FUNC(lane_new) | |||
1175 | if (lua_pcall( L2, 1, 1, 0) != LUA_OK) // ret/errcode | 1176 | if (lua_pcall( L2, 1, 1, 0) != LUA_OK) // ret/errcode |
1176 | { | 1177 | { |
1177 | // propagate error to main state if any | 1178 | // propagate error to main state if any |
1178 | std::ignore = luaG_inter_move(U | 1179 | InterCopyContext c{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }; |
1179 | , Source{ L2 }, Dest{ L } | 1180 | std::ignore = c.inter_move(1); // func libs priority globals package required gc_cb [... args ...] n "modname" error |
1180 | , 1, LookupMode::LaneBody | ||
1181 | ); // func libs priority globals package required gc_cb [... args ...] n "modname" error | ||
1182 | raise_lua_error(L); | 1181 | raise_lua_error(L); |
1183 | } | 1182 | } |
1184 | // after requiring the module, register the functions it exported in our name<->function database | 1183 | // after requiring the module, register the functions it exported in our name<->function database |
@@ -1189,7 +1188,6 @@ LUAG_FUNC(lane_new) | |||
1189 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] n | 1188 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] n |
1190 | ++ nbRequired; | 1189 | ++ nbRequired; |
1191 | } // func libs priority globals package required gc_cb [... args ...] | 1190 | } // func libs priority globals package required gc_cb [... args ...] |
1192 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
1193 | } | 1191 | } |
1194 | STACK_CHECK(L, 0); | 1192 | STACK_CHECK(L, 0); |
1195 | STACK_CHECK(L2, 0); // | 1193 | STACK_CHECK(L2, 0); // |
@@ -1205,20 +1203,19 @@ LUAG_FUNC(lane_new) | |||
1205 | luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); // doesn't return | 1203 | luaL_error(L, "Expected table, got %s", luaL_typename(L, globals_idx)); // doesn't return |
1206 | } | 1204 | } |
1207 | 1205 | ||
1208 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 1206 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1209 | lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil | 1207 | lua_pushnil(L); // func libs priority globals package required gc_cb [... args ...] nil |
1210 | // Lua 5.2 wants us to push the globals table on the stack | 1208 | // Lua 5.2 wants us to push the globals table on the stack |
1211 | lua_pushglobaltable(L2); // _G | 1209 | InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; |
1210 | lua_pushglobaltable(L2); // _G | ||
1212 | while( lua_next(L, globals_idx)) // func libs priority globals package required gc_cb [... args ...] k v | 1211 | while( lua_next(L, globals_idx)) // func libs priority globals package required gc_cb [... args ...] k v |
1213 | { | 1212 | { |
1214 | std::ignore = luaG_inter_copy(U, Source{ L }, Dest{ L2 }, 2, LookupMode::LaneBody); // _G k v | 1213 | std::ignore = c.inter_copy(2); // _G k v |
1215 | // assign it in L2's globals table | 1214 | // assign it in L2's globals table |
1216 | lua_rawset(L2, -3); // _G | 1215 | lua_rawset(L2, -3); // _G |
1217 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] k | 1216 | lua_pop(L, 1); // func libs priority globals package required gc_cb [... args ...] k |
1218 | } // func libs priority globals package required gc_cb [... args ...] | 1217 | } // func libs priority globals package required gc_cb [... args ...] |
1219 | lua_pop( L2, 1); // | 1218 | lua_pop( L2, 1); // |
1220 | |||
1221 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
1222 | } | 1219 | } |
1223 | STACK_CHECK(L, 0); | 1220 | STACK_CHECK(L, 0); |
1224 | STACK_CHECK(L2, 0); | 1221 | STACK_CHECK(L2, 0); |
@@ -1228,10 +1225,10 @@ LUAG_FUNC(lane_new) | |||
1228 | if (func_type == LuaType::FUNCTION) | 1225 | if (func_type == LuaType::FUNCTION) |
1229 | { | 1226 | { |
1230 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END)); | 1227 | 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)); | 1228 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1232 | lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func | 1229 | lua_pushvalue(L, 1); // func libs priority globals package required gc_cb [... args ...] func |
1233 | InterCopyResult const res{ luaG_inter_move(U, Source{ L }, Dest{ L2 }, 1, LookupMode::LaneBody) }; // func libs priority globals package required gc_cb [... args ...] // func | 1230 | InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; |
1234 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | 1231 | InterCopyResult const res{ c.inter_move(1) }; // func libs priority globals package required gc_cb [... args ...] // func |
1235 | if (res != InterCopyResult::Success) | 1232 | if (res != InterCopyResult::Success) |
1236 | { | 1233 | { |
1237 | luaL_error(L, "tried to copy unsupported types"); // doesn't return | 1234 | luaL_error(L, "tried to copy unsupported types"); // doesn't return |
@@ -1258,9 +1255,9 @@ LUAG_FUNC(lane_new) | |||
1258 | if (nargs > 0) | 1255 | if (nargs > 0) |
1259 | { | 1256 | { |
1260 | 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)); |
1261 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 1258 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1262 | InterCopyResult const res{ luaG_inter_move(U, Source{ L }, Dest{ L2 }, nargs, LookupMode::LaneBody) }; // func libs priority globals package required gc_cb // func [... args ...] | 1259 | InterCopyContext c{ U, Dest{ L2 }, Source{ L }, {}, {}, {}, {}, {} }; |
1263 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | 1260 | InterCopyResult const res{ c.inter_move(nargs) }; // func libs priority globals package required gc_cb // func [... args ...] |
1264 | if (res != InterCopyResult::Success) | 1261 | if (res != InterCopyResult::Success) |
1265 | { | 1262 | { |
1266 | luaL_error(L, "tried to copy unsupported types"); // doesn't return | 1263 | luaL_error(L, "tried to copy unsupported types"); // doesn't return |
@@ -1278,7 +1275,6 @@ LUAG_FUNC(lane_new) | |||
1278 | onExit.success(); | 1275 | onExit.success(); |
1279 | // we should have the lane userdata on top of the stack | 1276 | // we should have the lane userdata on top of the stack |
1280 | STACK_CHECK(L, 1); | 1277 | STACK_CHECK(L, 1); |
1281 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
1282 | return 1; | 1278 | return 1; |
1283 | } | 1279 | } |
1284 | 1280 | ||
@@ -1423,7 +1419,10 @@ LUAG_FUNC(thread_join) | |||
1423 | case Lane::Done: | 1419 | case Lane::Done: |
1424 | { | 1420 | { |
1425 | int const n{ lua_gettop(L2) }; // whole L2 stack | 1421 | int const n{ lua_gettop(L2) }; // whole L2 stack |
1426 | if ((n > 0) && (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != InterCopyResult::Success)) | 1422 | if ( |
1423 | (n > 0) && | ||
1424 | (InterCopyContext{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }.inter_move(n) != InterCopyResult::Success) | ||
1425 | ) | ||
1427 | { | 1426 | { |
1428 | luaL_error(L, "tried to copy unsupported types"); // doesn't return | 1427 | luaL_error(L, "tried to copy unsupported types"); // doesn't return |
1429 | } | 1428 | } |
@@ -1437,7 +1436,8 @@ LUAG_FUNC(thread_join) | |||
1437 | STACK_GROW(L, 3); | 1436 | STACK_GROW(L, 3); |
1438 | lua_pushnil(L); | 1437 | lua_pushnil(L); |
1439 | // 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 ... | 1438 | // 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 ... |
1440 | if (luaG_inter_move(U, Source{ L2 }, Dest{ L }, n, LookupMode::LaneBody) != InterCopyResult::Success) // nil "err" [trace] | 1439 | InterCopyContext c{ U, Dest{ L }, Source{ L2 }, {}, {}, {}, {}, {} }; |
1440 | if (c.inter_move(n) != InterCopyResult::Success) // nil "err" [trace] | ||
1441 | { | 1441 | { |
1442 | luaL_error(L, "tried to copy unsupported types: %s", lua_tostring(L, -n)); // doesn't return | 1442 | luaL_error(L, "tried to copy unsupported types: %s", lua_tostring(L, -n)); // doesn't return |
1443 | } | 1443 | } |
@@ -1772,12 +1772,12 @@ LUAG_FUNC(configure) | |||
1772 | STACK_CHECK_START_ABS(L, 1); // settings | 1772 | STACK_CHECK_START_ABS(L, 1); // settings |
1773 | 1773 | ||
1774 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); | 1774 | DEBUGSPEW_CODE(fprintf( stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END, L)); |
1775 | DEBUGSPEW_CODE(if (U) U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 1775 | DEBUGSPEW_CODE(DebugSpewIndentScope scope{ U }); |
1776 | 1776 | ||
1777 | if (U == nullptr) | 1777 | if (U == nullptr) |
1778 | { | 1778 | { |
1779 | U = universe_create(L); // settings universe | 1779 | U = universe_create(L); // settings universe |
1780 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_add(1, std::memory_order_relaxed)); | 1780 | DEBUGSPEW_CODE(DebugSpewIndentScope scope2{ U }); |
1781 | lua_newtable( L); // settings universe mt | 1781 | lua_newtable( L); // settings universe mt |
1782 | lua_getfield(L, 1, "shutdown_timeout"); // settings universe mt shutdown_timeout | 1782 | lua_getfield(L, 1, "shutdown_timeout"); // settings universe mt shutdown_timeout |
1783 | lua_getfield(L, 1, "shutdown_mode"); // settings universe mt shutdown_timeout shutdown_mode | 1783 | lua_getfield(L, 1, "shutdown_mode"); // settings universe mt shutdown_timeout shutdown_mode |
@@ -1920,7 +1920,6 @@ LUAG_FUNC(configure) | |||
1920 | CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); | 1920 | CONFIG_REGKEY.setValue(L, [](lua_State* L) { lua_pushvalue(L, -2); }); |
1921 | STACK_CHECK(L, 1); | 1921 | STACK_CHECK(L, 1); |
1922 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); | 1922 | DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END, L)); |
1923 | DEBUGSPEW_CODE(U->debugspew_indent_depth.fetch_sub(1, std::memory_order_relaxed)); | ||
1924 | // Return the settings table | 1923 | // Return the settings table |
1925 | return 1; | 1924 | return 1; |
1926 | } | 1925 | } |