diff options
author | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-08 18:42:39 +0200 |
---|---|---|
committer | Benoit Germain <benoit.germain@ubisoft.com> | 2024-10-08 18:42:39 +0200 |
commit | 16b5070c0cd56e10c5074eb9903dbc3ae4e15a61 (patch) | |
tree | f6d5cdb74b505e13aa3261f7ab6192da0133b7b9 /src/lanes.cpp | |
parent | e939e5e6a894a042d3301e47faa05264445f27f6 (diff) | |
download | lanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.tar.gz lanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.tar.bz2 lanes-16b5070c0cd56e10c5074eb9903dbc3ae4e15a61.zip |
Sprinkling StackIndex all over the place
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r-- | src/lanes.cpp | 68 |
1 files changed, 34 insertions, 34 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index c820568..3aef572 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -169,12 +169,12 @@ LUAG_FUNC(sleep) | |||
169 | lua_pushcfunction(L_, LG_linda_receive); // L_: duration|nil receive() | 169 | lua_pushcfunction(L_, LG_linda_receive); // L_: duration|nil receive() |
170 | STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments | 170 | STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments |
171 | _U->timerLinda->push(L_); // L_: duration|nil receive() timerLinda | 171 | _U->timerLinda->push(L_); // L_: duration|nil receive() timerLinda |
172 | if (luaG_tostring(L_, 1) == "indefinitely") { | 172 | if (luaG_tostring(L_, StackIndex{ 1 }) == "indefinitely") { |
173 | lua_pushnil(L_); // L_: duration? receive() timerLinda nil | 173 | lua_pushnil(L_); // L_: duration? receive() timerLinda nil |
174 | } else if (lua_isnoneornil(L_, 1)) { | 174 | } else if (lua_isnoneornil(L_, 1)) { |
175 | lua_pushnumber(L_, 0); // L_: duration? receive() timerLinda 0 | 175 | lua_pushnumber(L_, 0); // L_: duration? receive() timerLinda 0 |
176 | } else if (!lua_isnumber(L_, 1)) { | 176 | } else if (!lua_isnumber(L_, 1)) { |
177 | raise_luaL_argerror(L_, 1, "invalid duration"); | 177 | raise_luaL_argerror(L_, StackIndex{ 1 }, "invalid duration"); |
178 | } | 178 | } |
179 | else { | 179 | else { |
180 | lua_pushnumber(L_, lua_tonumber(L_, 1)); // L_: duration? receive() timerLinda duration | 180 | lua_pushnumber(L_, lua_tonumber(L_, 1)); // L_: duration? receive() timerLinda duration |
@@ -193,7 +193,7 @@ LUAG_FUNC(sleep) | |||
193 | // upvalue[1]: _G.require | 193 | // upvalue[1]: _G.require |
194 | LUAG_FUNC(require) | 194 | LUAG_FUNC(require) |
195 | { | 195 | { |
196 | std::string_view const _name{ luaG_tostring(L_, 1) }; // L_: "name" ... | 196 | std::string_view const _name{ luaG_tostring(L_, StackIndex{ 1 }) }; // L_: "name" ... |
197 | int const _nargs{ lua_gettop(L_) }; | 197 | int const _nargs{ lua_gettop(L_) }; |
198 | DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) }); | 198 | DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) }); |
199 | STACK_CHECK_START_REL(L_, 0); | 199 | STACK_CHECK_START_REL(L_, 0); |
@@ -202,7 +202,7 @@ LUAG_FUNC(require) | |||
202 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require | 202 | lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require |
203 | lua_insert(L_, 1); // L_: require "name" ... | 203 | lua_insert(L_, 1); // L_: require "name" ... |
204 | lua_call(L_, _nargs, 1); // L_: module | 204 | lua_call(L_, _nargs, 1); // L_: module |
205 | tools::PopulateFuncLookupTable(L_, -1, _name); | 205 | tools::PopulateFuncLookupTable(L_, kIdxTop, _name); |
206 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' END" << std::endl); | 206 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' END" << std::endl); |
207 | STACK_CHECK(L_, 0); | 207 | STACK_CHECK(L_, 0); |
208 | return 1; | 208 | return 1; |
@@ -215,8 +215,8 @@ LUAG_FUNC(require) | |||
215 | // lanes.register( "modname", module) | 215 | // lanes.register( "modname", module) |
216 | LUAG_FUNC(register) | 216 | LUAG_FUNC(register) |
217 | { | 217 | { |
218 | std::string_view const _name{ luaG_checkstring(L_, 1) }; | 218 | std::string_view const _name{ luaG_checkstring(L_, StackIndex{ 1 }) }; |
219 | LuaType const _mod_type{ luaG_type(L_, 2) }; | 219 | LuaType const _mod_type{ luaG_type(L_, StackIndex{ 2 }) }; |
220 | // ignore extra arguments, just in case | 220 | // ignore extra arguments, just in case |
221 | lua_settop(L_, 2); | 221 | lua_settop(L_, 2); |
222 | luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type"); | 222 | luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type"); |
@@ -224,7 +224,7 @@ LUAG_FUNC(register) | |||
224 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table | 224 | STACK_CHECK_START_REL(L_, 0); // "name" mod_table |
225 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' BEGIN" << std::endl); | 225 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' BEGIN" << std::endl); |
226 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); | 226 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); |
227 | tools::PopulateFuncLookupTable(L_, -1, _name); | 227 | tools::PopulateFuncLookupTable(L_, kIdxTop, _name); |
228 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' END" << std::endl); | 228 | DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' END" << std::endl); |
229 | STACK_CHECK(L_, 0); | 229 | STACK_CHECK(L_, 0); |
230 | return 0; | 230 | return 0; |
@@ -249,17 +249,17 @@ LUAG_FUNC(register) | |||
249 | // | 249 | // |
250 | LUAG_FUNC(lane_new) | 250 | LUAG_FUNC(lane_new) |
251 | { | 251 | { |
252 | static constexpr int kFuncIdx{ 1 }; | 252 | static constexpr StackIndex kFuncIdx{ 1 }; |
253 | static constexpr int kLibsIdx{ 2 }; | 253 | static constexpr StackIndex kLibsIdx{ 2 }; |
254 | static constexpr int kPrioIdx{ 3 }; | 254 | static constexpr StackIndex kPrioIdx{ 3 }; |
255 | static constexpr int kGlobIdx{ 4 }; | 255 | static constexpr StackIndex kGlobIdx{ 4 }; |
256 | static constexpr int kPackIdx{ 5 }; | 256 | static constexpr StackIndex kPackIdx{ 5 }; |
257 | static constexpr int kRequIdx{ 6 }; | 257 | static constexpr StackIndex kRequIdx{ 6 }; |
258 | static constexpr int kGcCbIdx{ 7 }; | 258 | static constexpr StackIndex kGcCbIdx{ 7 }; |
259 | static constexpr int kNameIdx{ 8 }; | 259 | static constexpr StackIndex kNameIdx{ 8 }; |
260 | static constexpr int kErTlIdx{ 9 }; | 260 | static constexpr StackIndex kErTlIdx{ 9 }; |
261 | static constexpr int kAsCoro{ 10 }; | 261 | static constexpr StackIndex kAsCoro{ 10 }; |
262 | static constexpr int kFixedArgsIdx{ 10 }; | 262 | static constexpr StackIndex kFixedArgsIdx{ 10 }; |
263 | 263 | ||
264 | int const _nargs{ lua_gettop(L_) - kFixedArgsIdx }; | 264 | int const _nargs{ lua_gettop(L_) - kFixedArgsIdx }; |
265 | LUA_ASSERT(L_, _nargs >= 0); | 265 | LUA_ASSERT(L_, _nargs >= 0); |
@@ -342,7 +342,7 @@ LUAG_FUNC(lane_new) | |||
342 | lua_newtable(L); // L: ... lane {uv} | 342 | lua_newtable(L); // L: ... lane {uv} |
343 | 343 | ||
344 | // Store the gc_cb callback in the uservalue | 344 | // Store the gc_cb callback in the uservalue |
345 | int const _gc_cb_idx{ lua_isnoneornil(L, kGcCbIdx) ? 0 : kGcCbIdx }; | 345 | StackIndex const _gc_cb_idx{ lua_isnoneornil(L, kGcCbIdx) ? 0 : kGcCbIdx }; |
346 | if (_gc_cb_idx > 0) { | 346 | if (_gc_cb_idx > 0) { |
347 | kLaneGC.pushKey(L); // L: ... lane {uv} k | 347 | kLaneGC.pushKey(L); // L: ... lane {uv} k |
348 | lua_pushvalue(L, _gc_cb_idx); // L: ... lane {uv} k gc_cb | 348 | lua_pushvalue(L, _gc_cb_idx); // L: ... lane {uv} k gc_cb |
@@ -350,11 +350,11 @@ LUAG_FUNC(lane_new) | |||
350 | } | 350 | } |
351 | STACK_CHECK(L, 2); | 351 | STACK_CHECK(L, 2); |
352 | // store the uservalue in the Lane full userdata | 352 | // store the uservalue in the Lane full userdata |
353 | lua_setiuservalue(L, -2, 1); // L: ... lane | 353 | lua_setiuservalue(L, StackIndex{ -2 }, 1); // L: ... lane |
354 | 354 | ||
355 | lua_State* const _L2{ lane->L }; | 355 | lua_State* const _L2{ lane->L }; |
356 | STACK_CHECK_START_REL(_L2, 0); | 356 | STACK_CHECK_START_REL(_L2, 0); |
357 | int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; | 357 | StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; |
358 | std::string_view const _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; | 358 | std::string_view const _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; |
359 | if (!_debugName.empty()) | 359 | if (!_debugName.empty()) |
360 | { | 360 | { |
@@ -366,7 +366,7 @@ LUAG_FUNC(lane_new) | |||
366 | lua_getinfo(L, ">S", &_ar); // L: ... lane | 366 | lua_getinfo(L, ">S", &_ar); // L: ... lane |
367 | luaG_pushstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "<name>" | 367 | luaG_pushstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "<name>" |
368 | } | 368 | } |
369 | lane->changeDebugName(-1); | 369 | lane->changeDebugName(kIdxTop); |
370 | lua_pop(_L2, 1); // L: ... lane L2: | 370 | lua_pop(_L2, 1); // L: ... lane L2: |
371 | } | 371 | } |
372 | STACK_CHECK(_L2, 0); | 372 | STACK_CHECK(_L2, 0); |
@@ -412,7 +412,7 @@ LUAG_FUNC(lane_new) | |||
412 | STACK_CHECK_START_REL(L_, 0); | 412 | STACK_CHECK_START_REL(L_, 0); |
413 | 413 | ||
414 | // package | 414 | // package |
415 | int const _package_idx{ lua_isnoneornil(L_, kPackIdx) ? 0 : kPackIdx }; | 415 | StackIndex const _package_idx{ lua_isnoneornil(L_, kPackIdx) ? 0 : kPackIdx }; |
416 | if (_package_idx != 0) { | 416 | if (_package_idx != 0) { |
417 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: update 'package'" << std::endl); | 417 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: update 'package'" << std::endl); |
418 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack | 418 | // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack |
@@ -424,7 +424,7 @@ LUAG_FUNC(lane_new) | |||
424 | STACK_CHECK(_L2, 0); | 424 | STACK_CHECK(_L2, 0); |
425 | 425 | ||
426 | // modules to require in the target lane *before* the function is transfered! | 426 | // modules to require in the target lane *before* the function is transfered! |
427 | int const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx }; | 427 | StackIndex const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx }; |
428 | if (_required_idx != 0) { | 428 | if (_required_idx != 0) { |
429 | int _nbRequired{ 1 }; | 429 | int _nbRequired{ 1 }; |
430 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl); | 430 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl); |
@@ -436,11 +436,11 @@ LUAG_FUNC(lane_new) | |||
436 | 436 | ||
437 | lua_pushnil(L_); // L_: [fixed] args... nil L2: | 437 | lua_pushnil(L_); // L_: [fixed] args... nil L2: |
438 | while (lua_next(L_, _required_idx) != 0) { // L_: [fixed] args... n "modname" L2: | 438 | while (lua_next(L_, _required_idx) != 0) { // L_: [fixed] args... n "modname" L2: |
439 | if (luaG_type(L_, -1) != LuaType::STRING || luaG_type(L_, -2) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) { | 439 | if (luaG_type(L_, kIdxTop) != LuaType::STRING || luaG_type(L_, StackIndex{ -2 }) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) { |
440 | raise_luaL_error(L_, "required module list should be a list of strings"); | 440 | raise_luaL_error(L_, "required module list should be a list of strings"); |
441 | } else { | 441 | } else { |
442 | // require the module in the target state, and populate the lookup table there too | 442 | // require the module in the target state, and populate the lookup table there too |
443 | std::string_view const _name{ luaG_tostring(L_, -1) }; | 443 | std::string_view const _name{ luaG_tostring(L_, kIdxTop) }; |
444 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl); | 444 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl); |
445 | 445 | ||
446 | // require the module in the target lane | 446 | // require the module in the target lane |
@@ -459,7 +459,7 @@ LUAG_FUNC(lane_new) | |||
459 | } | 459 | } |
460 | // here the module was successfully required // L_: [fixed] args... n "modname" L2: ret | 460 | // here the module was successfully required // L_: [fixed] args... n "modname" L2: ret |
461 | // after requiring the module, register the functions it exported in our name<->function database | 461 | // after requiring the module, register the functions it exported in our name<->function database |
462 | tools::PopulateFuncLookupTable(_L2, -1, _name); | 462 | tools::PopulateFuncLookupTable(_L2, kIdxTop, _name); |
463 | lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: | 463 | lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: |
464 | } | 464 | } |
465 | } | 465 | } |
@@ -473,7 +473,7 @@ LUAG_FUNC(lane_new) | |||
473 | // Appending the specified globals to the global environment | 473 | // Appending the specified globals to the global environment |
474 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... | 474 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... |
475 | // | 475 | // |
476 | int const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? 0 : kGlobIdx }; | 476 | StackIndex const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? 0 : kGlobIdx }; |
477 | if (_globals_idx != 0) { | 477 | if (_globals_idx != 0) { |
478 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer globals" << std::endl); | 478 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer globals" << std::endl); |
479 | if (!lua_istable(L_, _globals_idx)) { | 479 | if (!lua_istable(L_, _globals_idx)) { |
@@ -603,7 +603,7 @@ LUAG_FUNC(wakeup_conv) | |||
603 | 603 | ||
604 | STACK_CHECK_START_REL(L_, 0); | 604 | STACK_CHECK_START_REL(L_, 0); |
605 | auto _readInteger = [L = L_](std::string_view const& name_) { | 605 | auto _readInteger = [L = L_](std::string_view const& name_) { |
606 | std::ignore = luaG_getfield(L, 1, name_); | 606 | std::ignore = luaG_getfield(L, StackIndex{ 1 }, name_); |
607 | lua_Integer const val{ lua_tointeger(L, -1) }; | 607 | lua_Integer const val{ lua_tointeger(L, -1) }; |
608 | lua_pop(L, 1); | 608 | lua_pop(L, 1); |
609 | return static_cast<int>(val); | 609 | return static_cast<int>(val); |
@@ -619,7 +619,7 @@ LUAG_FUNC(wakeup_conv) | |||
619 | // If Lua table has '.isdst' we trust that. If it does not, we'll let | 619 | // If Lua table has '.isdst' we trust that. If it does not, we'll let |
620 | // 'mktime' decide on whether the time is within DST or not (value -1). | 620 | // 'mktime' decide on whether the time is within DST or not (value -1). |
621 | // | 621 | // |
622 | int const _isdst{ (luaG_getfield(L_, 1, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 }; | 622 | int const _isdst{ (luaG_getfield(L_, StackIndex{ 1 }, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 }; |
623 | lua_pop(L_, 1); | 623 | lua_pop(L_, 1); |
624 | STACK_CHECK(L_, 0); | 624 | STACK_CHECK(L_, 0); |
625 | 625 | ||
@@ -682,8 +682,8 @@ LUAG_FUNC(configure) | |||
682 | 682 | ||
683 | Universe* _U{ Universe::Get(L_) }; | 683 | Universe* _U{ Universe::Get(L_) }; |
684 | bool const _from_master_state{ _U == nullptr }; | 684 | bool const _from_master_state{ _U == nullptr }; |
685 | std::string_view const _name{ luaG_checkstring(L_, lua_upvalueindex(1)) }; | 685 | std::string_view const _name{ luaG_checkstring(L_, StackIndex{ lua_upvalueindex(1) }) }; |
686 | LUA_ASSERT(L_, luaG_type(L_, 1) == LuaType::TABLE); | 686 | LUA_ASSERT(L_, luaG_type(L_, StackIndex{ 1 }) == LuaType::TABLE); |
687 | 687 | ||
688 | STACK_GROW(L_, 4); | 688 | STACK_GROW(L_, 4); |
689 | STACK_CHECK_START_ABS(L_, 1); // L_: settings | 689 | STACK_CHECK_START_ABS(L_, 1); // L_: settings |
@@ -759,7 +759,7 @@ LUAG_FUNC(configure) | |||
759 | // register all native functions found in that module in the transferable functions database | 759 | // register all native functions found in that module in the transferable functions database |
760 | // we process it before _G because we don't want to find the module when scanning _G (this would generate longer names) | 760 | // we process it before _G because we don't want to find the module when scanning _G (this would generate longer names) |
761 | // for example in package.loaded["lanes.core"].* | 761 | // for example in package.loaded["lanes.core"].* |
762 | tools::PopulateFuncLookupTable(L_, -1, _name); | 762 | tools::PopulateFuncLookupTable(L_, kIdxTop, _name); |
763 | STACK_CHECK(L_, 2); | 763 | STACK_CHECK(L_, 2); |
764 | 764 | ||
765 | // record all existing C/JIT-fast functions | 765 | // record all existing C/JIT-fast functions |
@@ -769,7 +769,7 @@ LUAG_FUNC(configure) | |||
769 | // because we will do it after on_state_create() is called, | 769 | // because we will do it after on_state_create() is called, |
770 | // and we don't want to skip _G because of caching in case globals are created then | 770 | // and we don't want to skip _G because of caching in case globals are created then |
771 | luaG_pushglobaltable(L_); // L_: settings M _G | 771 | luaG_pushglobaltable(L_); // L_: settings M _G |
772 | tools::PopulateFuncLookupTable(L_, -1, {}); | 772 | tools::PopulateFuncLookupTable(L_, kIdxTop, {}); |
773 | lua_pop(L_, 1); // L_: settings M | 773 | lua_pop(L_, 1); // L_: settings M |
774 | } | 774 | } |
775 | lua_pop(L_, 1); // L_: settings | 775 | lua_pop(L_, 1); // L_: settings |