diff options
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r-- | src/lanes.cpp | 28 |
1 files changed, 22 insertions, 6 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp index 473e150..c11dd6a 100644 --- a/src/lanes.cpp +++ b/src/lanes.cpp | |||
@@ -268,11 +268,16 @@ LUAG_FUNC(lane_new) | |||
268 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl); | 268 | DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl); |
269 | 269 | ||
270 | std::optional<std::string_view> _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaG_tostring(L_, kLibsIdx)) }; | 270 | std::optional<std::string_view> _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaG_tostring(L_, kLibsIdx)) }; |
271 | lua_State* const _L2{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2: | 271 | lua_State* const _S{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2: |
272 | STACK_CHECK_START_REL(_L2, 0); | 272 | STACK_CHECK_START_REL(_S, 0); |
273 | 273 | ||
274 | // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) | 274 | // 'lane' is allocated from heap, not Lua, since its life span may surpass the handle's (if free running thread) |
275 | Lane* const _lane{ new (_U) Lane{ _U, _L2, static_cast<Lane::ErrorTraceLevel>(lua_tointeger(L_, kErTlIdx)) } }; | 275 | Lane::ErrorTraceLevel const _errorTraceLevel{ static_cast<Lane::ErrorTraceLevel>(lua_tointeger(L_, kErTlIdx)) }; |
276 | bool const _asCoroutine{ lua_toboolean(L_, kAsCoro) ? true : false }; | ||
277 | Lane* const _lane{ new (_U) Lane{ _U, _S, _errorTraceLevel, _asCoroutine } }; | ||
278 | STACK_CHECK(_S, _asCoroutine ? 1 : 0); // the Lane's thread is on the Lane's state stack | ||
279 | lua_State* const _L2{ _lane->L }; | ||
280 | STACK_CHECK_START_REL(_L2, 0); | ||
276 | if (_lane == nullptr) { | 281 | if (_lane == nullptr) { |
277 | raise_luaL_error(L_, "could not create lane: out of memory"); | 282 | raise_luaL_error(L_, "could not create lane: out of memory"); |
278 | } | 283 | } |
@@ -347,7 +352,7 @@ LUAG_FUNC(lane_new) | |||
347 | 352 | ||
348 | lua_setiuservalue(L, -2, 1); // L: ... lane | 353 | lua_setiuservalue(L, -2, 1); // L: ... lane |
349 | 354 | ||
350 | lua_State* _L2{ lane->L }; | 355 | lua_State* const _L2{ lane->L }; |
351 | STACK_CHECK_START_REL(_L2, 0); | 356 | STACK_CHECK_START_REL(_L2, 0); |
352 | int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; | 357 | int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; |
353 | 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{} }; |
@@ -357,7 +362,7 @@ LUAG_FUNC(lane_new) | |||
357 | luaG_pushstring(_L2, _debugName); // L: ... lane L2: "<name>" | 362 | luaG_pushstring(_L2, _debugName); // L: ... lane L2: "<name>" |
358 | } else { | 363 | } else { |
359 | lua_Debug _ar; | 364 | lua_Debug _ar; |
360 | lua_pushvalue(L, 1); // L: ... lane func | 365 | lua_pushvalue(L, kFuncIdx); // L: ... lane func |
361 | lua_getinfo(L, ">S", &_ar); // L: ... lane | 366 | lua_getinfo(L, ">S", &_ar); // L: ... lane |
362 | 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>" |
363 | } | 368 | } |
@@ -415,6 +420,8 @@ LUAG_FUNC(lane_new) | |||
415 | [[maybe_unused]] InterCopyResult const _ret{ _c.interCopyPackage() }; | 420 | [[maybe_unused]] InterCopyResult const _ret{ _c.interCopyPackage() }; |
416 | LUA_ASSERT(L_, _ret == InterCopyResult::Success); // either all went well, or we should not even get here | 421 | LUA_ASSERT(L_, _ret == InterCopyResult::Success); // either all went well, or we should not even get here |
417 | } | 422 | } |
423 | STACK_CHECK(L_, 0); | ||
424 | STACK_CHECK(_L2, 0); | ||
418 | 425 | ||
419 | // 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! |
420 | int const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx }; | 427 | int const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx }; |
@@ -526,6 +533,7 @@ LUAG_FUNC(lane_new) | |||
526 | } | 533 | } |
527 | STACK_CHECK(L_, -_nargs); | 534 | STACK_CHECK(L_, -_nargs); |
528 | LUA_ASSERT(L_, lua_gettop(L_) == kFixedArgsIdx); | 535 | LUA_ASSERT(L_, lua_gettop(L_) == kFixedArgsIdx); |
536 | STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs); | ||
529 | 537 | ||
530 | // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). | 538 | // Store 'lane' in the lane's registry, for 'cancel_test()' (we do cancel tests at pending send/receive). |
531 | kLanePointerRegKey.setValue( | 539 | kLanePointerRegKey.setValue( |
@@ -533,6 +541,14 @@ LUAG_FUNC(lane_new) | |||
533 | ); | 541 | ); |
534 | STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs); | 542 | STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs); |
535 | 543 | ||
544 | // if in coroutine mode, the Lane's master state stack should contain the thread | ||
545 | if (_asCoroutine) { | ||
546 | LUA_ASSERT(L_, _S != _L2); | ||
547 | STACK_CHECK(_S, 1); | ||
548 | } | ||
549 | // and the thread's stack has whatever is needed to run | ||
550 | STACK_CHECK(_L2, _errorHandlerCount + 1 + _nargs); | ||
551 | |||
536 | STACK_CHECK_RESET_REL(L_, 0); | 552 | STACK_CHECK_RESET_REL(L_, 0); |
537 | // all went well, the lane's thread can start working | 553 | // all went well, the lane's thread can start working |
538 | _onExit.success(); // L_: [fixed] lane L2: <living its own life> | 554 | _onExit.success(); // L_: [fixed] lane L2: <living its own life> |
@@ -541,7 +557,7 @@ LUAG_FUNC(lane_new) | |||
541 | return 1; | 557 | return 1; |
542 | } | 558 | } |
543 | 559 | ||
544 | // ################################################################################################ | 560 | // ################################################################################################# |
545 | 561 | ||
546 | // threads() -> {}|nil | 562 | // threads() -> {}|nil |
547 | // Return a list of all known lanes | 563 | // Return a list of all known lanes |