diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2026-03-02 18:18:57 +0100 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2026-03-02 18:18:57 +0100 |
| commit | c0ba5fb14cbd6ecab7dbefe0000f37ad2485a7d3 (patch) | |
| tree | e5c4e9d8e1a10d5204921a32206bf05aa8a63e82 /src | |
| parent | 4a8f8d2cd6a9726767b2a113e6885a4c2ac663a0 (diff) | |
| download | lanes-c0ba5fb14cbd6ecab7dbefe0000f37ad2485a7d3.tar.gz lanes-c0ba5fb14cbd6ecab7dbefe0000f37ad2485a7d3.tar.bz2 lanes-c0ba5fb14cbd6ecab7dbefe0000f37ad2485a7d3.zip | |
lane_new internal code cleanup step 4: TransferGlobals
Diffstat (limited to 'src')
| -rw-r--r-- | src/lanes.cpp | 49 |
1 files changed, 28 insertions, 21 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index edcc556..9889dda 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
| @@ -342,6 +342,33 @@ namespace { | |||
| 342 | ++_nbRequired; | 342 | ++_nbRequired; |
| 343 | } // L_: [fixed] args... | 343 | } // L_: [fixed] args... |
| 344 | } | 344 | } |
| 345 | |||
| 346 | // Transfer each key/value pair from globalsIdx_ into the lane's global environment (_G). | ||
| 347 | // Must be called after stdlibs are loaded and modules required, so that references to | ||
| 348 | // native functions those exposed can be resolved. No-op when globalsIdx_ is nil/none. | ||
| 349 | static void TransferGlobals(Universe* const U_, lua_State* const L_, lua_State* const L2_, StackIndex const globalsIdx_) | ||
| 350 | { | ||
| 351 | if (lua_isnoneornil(L_, globalsIdx_)) { | ||
| 352 | return; | ||
| 353 | } | ||
| 354 | DEBUGSPEW_CODE(DebugSpew(U_) << "lane_new: transfer globals" << std::endl); | ||
| 355 | if (!lua_istable(L_, globalsIdx_)) { | ||
| 356 | raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, globalsIdx_)); | ||
| 357 | } | ||
| 358 | |||
| 359 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); | ||
| 360 | lua_pushnil(L_); // L_: [fixed] args... nil L2: | ||
| 361 | // Lua 5.2 wants us to push the globals table on the stack | ||
| 362 | InterCopyContext _c{ U_, DestState{ L2_ }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | ||
| 363 | luaW_pushglobaltable(L2_); // L_: [fixed] args... nil L2: _G | ||
| 364 | while (lua_next(L_, globalsIdx_)) { // L_: [fixed] args... k v L2: _G | ||
| 365 | std::ignore = _c.interCopy(2); // L_: [fixed] args... k v L2: _G k v | ||
| 366 | // assign it in L2's globals table | ||
| 367 | lua_rawset(L2_, -3); // L_: [fixed] args... k v L2: _G | ||
| 368 | lua_pop(L_, 1); // L_: [fixed] args... k | ||
| 369 | } // L_: [fixed] args... | ||
| 370 | lua_pop(L2_, 1); // L_: [fixed] args... L2: | ||
| 371 | } | ||
| 345 | } // namespace local | 372 | } // namespace local |
| 346 | } // namespace | 373 | } // namespace |
| 347 | 374 | ||
| @@ -533,27 +560,7 @@ LUAG_FUNC(lane_new) | |||
| 533 | 560 | ||
| 534 | // Appending the specified globals to the global environment | 561 | // Appending the specified globals to the global environment |
| 535 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... | 562 | // *after* stdlibs have been loaded and modules required, in case we transfer references to native functions they exposed... |
| 536 | // | 563 | local::TransferGlobals(_U, L_, _L2, kGlobIdx); |
| 537 | StackIndex const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? kIdxNone : kGlobIdx }; | ||
| 538 | if (_globals_idx != 0) { | ||
| 539 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer globals" << std::endl); | ||
| 540 | if (!lua_istable(L_, _globals_idx)) { | ||
| 541 | raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, _globals_idx)); | ||
| 542 | } | ||
| 543 | |||
| 544 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); | ||
| 545 | lua_pushnil(L_); // L_: [fixed] args... nil L2: | ||
| 546 | // Lua 5.2 wants us to push the globals table on the stack | ||
| 547 | InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | ||
| 548 | luaW_pushglobaltable(_L2); // L_: [fixed] args... nil L2: _G | ||
| 549 | while (lua_next(L_, _globals_idx)) { // L_: [fixed] args... k v L2: _G | ||
| 550 | std::ignore = _c.interCopy(2); // L_: [fixed] args... k v L2: _G k v | ||
| 551 | // assign it in L2's globals table | ||
| 552 | lua_rawset(_L2, -3); // L_: [fixed] args... k v L2: _G | ||
| 553 | lua_pop(L_, 1); // L_: [fixed] args... k | ||
| 554 | } // L_: [fixed] args... | ||
| 555 | lua_pop(_L2, 1); // L_: [fixed] args... L2: | ||
| 556 | } | ||
| 557 | STACK_CHECK(L_, 0); | 564 | STACK_CHECK(L_, 0); |
| 558 | STACK_CHECK(_L2, 0); | 565 | STACK_CHECK(_L2, 0); |
| 559 | 566 | ||
