diff options
| author | Benoit Germain <benoit.germain@ubisoft.com> | 2026-03-02 18:24:40 +0100 |
|---|---|---|
| committer | Benoit Germain <benoit.germain@ubisoft.com> | 2026-03-02 18:24:40 +0100 |
| commit | 4eb317406d5e9d0337510a3d965f0ed7f313d7f2 (patch) | |
| tree | 42d2502b4179f5a50c71ce6bcc07966a3d849a46 /src/lanes.cpp | |
| parent | c0ba5fb14cbd6ecab7dbefe0000f37ad2485a7d3 (diff) | |
| download | lanes-4eb317406d5e9d0337510a3d965f0ed7f313d7f2.tar.gz lanes-4eb317406d5e9d0337510a3d965f0ed7f313d7f2.tar.bz2 lanes-4eb317406d5e9d0337510a3d965f0ed7f313d7f2.zip | |
lane_new internal code cleanup step 5: TransferLaneBody
Diffstat (limited to 'src/lanes.cpp')
| -rw-r--r-- | src/lanes.cpp | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index 9889dda..b0e0bcf 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
| @@ -369,6 +369,34 @@ namespace { | |||
| 369 | } // L_: [fixed] args... | 369 | } // L_: [fixed] args... |
| 370 | lua_pop(L2_, 1); // L_: [fixed] args... L2: | 370 | lua_pop(L2_, 1); // L_: [fixed] args... L2: |
| 371 | } | 371 | } |
| 372 | |||
| 373 | // Push the optional error handler onto L2, then transfer the lane body (a Lua function or | ||
| 374 | // a string to be compiled) from L_ into L2_. Returns the error handler count (0 or 1) so | ||
| 375 | // the caller can assert the correct L2 stack depth after arguments are transferred. | ||
| 376 | [[nodiscard]] static int TransferLaneBody(Universe* const U_, lua_State* const L_, lua_State* const L2_, StackIndex const funcIdx_, Lane* const lane_) | ||
| 377 | { | ||
| 378 | int const _errorHandlerCount{ lane_->pushErrorHandler() }; // L_: [fixed] args... L2: eh? | ||
| 379 | LuaType const _func_type{ luaW_type(L_, funcIdx_) }; | ||
| 380 | if (_func_type == LuaType::FUNCTION) { | ||
| 381 | DEBUGSPEW_CODE(DebugSpew(U_) << "lane_new: transfer lane body" << std::endl); | ||
| 382 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ U_ }); | ||
| 383 | lua_pushvalue(L_, funcIdx_); // L_: [fixed] args... func L2: eh? | ||
| 384 | InterCopyContext _c{ U_, DestState{ L2_ }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | ||
| 385 | InterCopyResult const _res{ _c.interMove(1) }; // L_: [fixed] args... L2: eh? func | ||
| 386 | if (_res != InterCopyResult::Success) { | ||
| 387 | raise_luaL_error(L_, "tried to copy unsupported types"); | ||
| 388 | } | ||
| 389 | } else if (_func_type == LuaType::STRING) { | ||
| 390 | DEBUGSPEW_CODE(DebugSpew(U_) << "lane_new: compile lane body" << std::endl); | ||
| 391 | // compile the string | ||
| 392 | if (luaL_loadstring(L2_, lua_tostring(L_, funcIdx_)) != 0) { // L_: [fixed] args... L2: eh? func | ||
| 393 | raise_luaL_error(L_, "error when parsing lane function code"); | ||
| 394 | } | ||
| 395 | } else { | ||
| 396 | raise_luaL_error(L_, "Expected function, got %s", luaW_typename(L_, _func_type).data()); | ||
| 397 | } | ||
| 398 | return _errorHandlerCount; | ||
| 399 | } | ||
| 372 | } // namespace local | 400 | } // namespace local |
| 373 | } // namespace | 401 | } // namespace |
| 374 | 402 | ||
| @@ -565,26 +593,7 @@ LUAG_FUNC(lane_new) | |||
| 565 | STACK_CHECK(_L2, 0); | 593 | STACK_CHECK(_L2, 0); |
| 566 | 594 | ||
| 567 | // Lane main function | 595 | // Lane main function |
| 568 | [[maybe_unused]] int const _errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? | 596 | int const _errorHandlerCount{ local::TransferLaneBody(_U, L_, _L2, kFuncIdx, _lane) }; |
| 569 | LuaType const _func_type{ luaW_type(L_, kFuncIdx) }; | ||
| 570 | if (_func_type == LuaType::FUNCTION) { | ||
| 571 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl); | ||
| 572 | DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); | ||
| 573 | lua_pushvalue(L_, kFuncIdx); // L_: [fixed] args... func L2: eh? | ||
| 574 | InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; | ||
| 575 | InterCopyResult const _res{ _c.interMove(1) }; // L_: [fixed] args... L2: eh? func | ||
| 576 | if (_res != InterCopyResult::Success) { | ||
| 577 | raise_luaL_error(L_, "tried to copy unsupported types"); | ||
| 578 | } | ||
| 579 | } else if (_func_type == LuaType::STRING) { | ||
| 580 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: compile lane body" << std::endl); | ||
| 581 | // compile the string | ||
| 582 | if (luaL_loadstring(_L2, lua_tostring(L_, kFuncIdx)) != 0) { // L_: [fixed] args... L2: eh? func | ||
| 583 | raise_luaL_error(L_, "error when parsing lane function code"); | ||
| 584 | } | ||
| 585 | } else { | ||
| 586 | raise_luaL_error(L_, "Expected function, got %s", luaW_typename(L_, _func_type).data()); | ||
| 587 | } | ||
| 588 | STACK_CHECK(L_, 0); | 597 | STACK_CHECK(L_, 0); |
| 589 | STACK_CHECK(_L2, _errorHandlerCount + 1); | 598 | STACK_CHECK(_L2, _errorHandlerCount + 1); |
| 590 | LUA_ASSERT(L_, lua_isfunction(_L2, _errorHandlerCount + 1)); | 599 | LUA_ASSERT(L_, lua_isfunction(_L2, _errorHandlerCount + 1)); |
