aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r--src/lanes.cpp58
1 files changed, 29 insertions, 29 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp
index 0eaeb3e..d9ac4df 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -173,7 +173,7 @@ LUAG_FUNC(sleep)
173 lua_pushcfunction(L_, LG_linda_receive); // L_: duration|nil receive() 173 lua_pushcfunction(L_, LG_linda_receive); // L_: duration|nil receive()
174 STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments 174 STACK_CHECK_START_REL(L_, 0); // we pushed the function we intend to call, now prepare the arguments
175 _U->timerLinda->push(L_); // L_: duration|nil receive() timerLinda 175 _U->timerLinda->push(L_); // L_: duration|nil receive() timerLinda
176 if (luaG_tostring(L_, StackIndex{ 1 }) == "indefinitely") { 176 if (luaW_tostring(L_, StackIndex{ 1 }) == "indefinitely") {
177 lua_pushnil(L_); // L_: duration? receive() timerLinda nil 177 lua_pushnil(L_); // L_: duration? receive() timerLinda nil
178 } else if (lua_isnoneornil(L_, 1)) { 178 } else if (lua_isnoneornil(L_, 1)) {
179 lua_pushnumber(L_, 0); // L_: duration? receive() timerLinda 0 179 lua_pushnumber(L_, 0); // L_: duration? receive() timerLinda 0
@@ -183,7 +183,7 @@ LUAG_FUNC(sleep)
183 else { 183 else {
184 lua_pushnumber(L_, lua_tonumber(L_, 1)); // L_: duration? receive() timerLinda duration 184 lua_pushnumber(L_, lua_tonumber(L_, 1)); // L_: duration? receive() timerLinda duration
185 } 185 }
186 luaG_pushstring(L_, "ac100de1-a696-4619-b2f0-a26de9d58ab8"); // L_: duration? receive() timerLinda duration key 186 luaW_pushstring(L_, "ac100de1-a696-4619-b2f0-a26de9d58ab8"); // L_: duration? receive() timerLinda duration key
187 STACK_CHECK(L_, 3); // 3 arguments ready 187 STACK_CHECK(L_, 3); // 3 arguments ready
188 lua_call(L_, 3, LUA_MULTRET); // timerLinda:receive(duration,key) // L_: duration? result... 188 lua_call(L_, 3, LUA_MULTRET); // timerLinda:receive(duration,key) // L_: duration? result...
189 return lua_gettop(L_) - 1; 189 return lua_gettop(L_) - 1;
@@ -197,7 +197,7 @@ LUAG_FUNC(sleep)
197// upvalue[1]: _G.require 197// upvalue[1]: _G.require
198LUAG_FUNC(require) 198LUAG_FUNC(require)
199{ 199{
200 std::string_view const _name{ luaG_tostring(L_, StackIndex{ 1 }) }; // L_: "name" ... 200 std::string_view const _name{ luaW_tostring(L_, StackIndex{ 1 }) }; // L_: "name" ...
201 int const _nargs{ lua_gettop(L_) }; 201 int const _nargs{ lua_gettop(L_) };
202 DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) }); 202 DEBUGSPEW_CODE(Universe * _U{ Universe::Get(L_) });
203 STACK_CHECK_START_REL(L_, 0); 203 STACK_CHECK_START_REL(L_, 0);
@@ -223,8 +223,8 @@ int lanes_register(lua_State* const L_)
223 if (!_U) { 223 if (!_U) {
224 raise_luaL_error(L_, "Lanes is not ready"); 224 raise_luaL_error(L_, "Lanes is not ready");
225 } 225 }
226 std::string_view const _name{ luaG_checkstring(L_, StackIndex{ 1 }) }; 226 std::string_view const _name{ luaW_checkstring(L_, StackIndex{ 1 }) };
227 LuaType const _mod_type{ luaG_type(L_, StackIndex{ 2 }) }; 227 LuaType const _mod_type{ luaW_type(L_, StackIndex{ 2 }) };
228 // ignore extra arguments, just in case 228 // ignore extra arguments, just in case
229 lua_settop(L_, 2); 229 lua_settop(L_, 2);
230 luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type"); 230 luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type");
@@ -292,7 +292,7 @@ LUAG_FUNC(lane_new)
292 Universe* const _U{ Universe::Get(L_) }; 292 Universe* const _U{ Universe::Get(L_) };
293 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl); 293 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl);
294 294
295 std::optional<std::string_view> _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaG_tostring(L_, kLibsIdx)) }; 295 std::optional<std::string_view> _libs_str{ lua_isnil(L_, kLibsIdx) ? std::nullopt : std::make_optional(luaW_tostring(L_, kLibsIdx)) };
296 lua_State* const _S{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2: 296 lua_State* const _S{ state::NewLaneState(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2:
297 STACK_CHECK_START_REL(_S, 0); 297 STACK_CHECK_START_REL(_S, 0);
298 298
@@ -354,7 +354,7 @@ LUAG_FUNC(lane_new)
354 DEBUGSPEW_CODE(DebugSpew(lane->U) << "lane_new: preparing lane userdata" << std::endl); 354 DEBUGSPEW_CODE(DebugSpew(lane->U) << "lane_new: preparing lane userdata" << std::endl);
355 STACK_CHECK_START_REL(L, 0); 355 STACK_CHECK_START_REL(L, 0);
356 // a Lane full userdata needs a single uservalue 356 // a Lane full userdata needs a single uservalue
357 Lane** const _ud{ luaG_newuserdatauv<Lane*>(L, UserValueCount{ 1 }) }; // L: ... lane 357 Lane** const _ud{ luaW_newuserdatauv<Lane*>(L, UserValueCount{ 1 }) }; // L: ... lane
358 *_ud = lane; // don't forget to store the pointer in the userdata! 358 *_ud = lane; // don't forget to store the pointer in the userdata!
359 359
360 // Set metatable for the userdata 360 // Set metatable for the userdata
@@ -378,25 +378,25 @@ LUAG_FUNC(lane_new)
378 lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane 378 lua_setiuservalue(L, StackIndex{ -2 }, UserValueIndex{ 1 }); // L: ... lane
379 379
380 StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx }; 380 StackIndex const _name_idx{ lua_isnoneornil(L, kNameIdx) ? kIdxNone : kNameIdx };
381 std::string_view _debugName{ (_name_idx > 0) ? luaG_tostring(L, _name_idx) : std::string_view{} }; 381 std::string_view _debugName{ (_name_idx > 0) ? luaW_tostring(L, _name_idx) : std::string_view{} };
382 if (!_debugName.empty()) 382 if (!_debugName.empty())
383 { 383 {
384 if (_debugName == "auto") { 384 if (_debugName == "auto") {
385 if (luaG_type(L, kFuncIdx) == LuaType::STRING) { 385 if (luaW_type(L, kFuncIdx) == LuaType::STRING) {
386 lua_Debug _ar; 386 lua_Debug _ar;
387 if (lua_getstack(L, 2, &_ar) == 0) { // 0 is here, 1 is lanes.gen, 2 is its caller 387 if (lua_getstack(L, 2, &_ar) == 0) { // 0 is here, 1 is lanes.gen, 2 is its caller
388 lua_getstack(L, 1, &_ar); // level 2 may not exist with LuaJIT, try again with level 1 388 lua_getstack(L, 1, &_ar); // level 2 may not exist with LuaJIT, try again with level 1
389 } 389 }
390 lua_getinfo(L, "Sl", &_ar); 390 lua_getinfo(L, "Sl", &_ar);
391 luaG_pushstring(L, "%s:%d", _ar.short_src, _ar.currentline); // L: ... lane "<name>" 391 luaW_pushstring(L, "%s:%d", _ar.short_src, _ar.currentline); // L: ... lane "<name>"
392 } else { 392 } else {
393 lua_Debug _ar; 393 lua_Debug _ar;
394 lua_pushvalue(L, kFuncIdx); // L: ... lane func 394 lua_pushvalue(L, kFuncIdx); // L: ... lane func
395 lua_getinfo(L, ">S", &_ar); // L: ... lane 395 lua_getinfo(L, ">S", &_ar); // L: ... lane
396 luaG_pushstring(L, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane "<name>" 396 luaW_pushstring(L, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane "<name>"
397 } 397 }
398 lua_replace(L, _name_idx); // L: ... lane 398 lua_replace(L, _name_idx); // L: ... lane
399 _debugName = luaG_tostring(L, _name_idx); 399 _debugName = luaW_tostring(L, _name_idx);
400 } 400 }
401 lane->storeDebugName(_debugName); 401 lane->storeDebugName(_debugName);
402 } 402 }
@@ -461,17 +461,17 @@ LUAG_FUNC(lane_new)
461 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl); 461 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl);
462 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 462 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
463 // should not happen, was checked in lanes.lua before calling lane_new() 463 // should not happen, was checked in lanes.lua before calling lane_new()
464 if (luaG_type(L_, _required_idx) != LuaType::TABLE) { 464 if (luaW_type(L_, _required_idx) != LuaType::TABLE) {
465 raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, _required_idx)); 465 raise_luaL_error(L_, "expected required module list as a table, got %s", luaL_typename(L_, _required_idx));
466 } 466 }
467 467
468 lua_pushnil(L_); // L_: [fixed] args... nil L2: 468 lua_pushnil(L_); // L_: [fixed] args... nil L2:
469 while (lua_next(L_, _required_idx) != 0) { // L_: [fixed] args... n "modname" L2: 469 while (lua_next(L_, _required_idx) != 0) { // L_: [fixed] args... n "modname" L2:
470 if (luaG_type(L_, kIdxTop) != LuaType::STRING || luaG_type(L_, StackIndex{ -2 }) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) { 470 if (luaW_type(L_, kIdxTop) != LuaType::STRING || luaW_type(L_, StackIndex{ -2 }) != LuaType::NUMBER || lua_tonumber(L_, -2) != _nbRequired) {
471 raise_luaL_error(L_, "required module list should be a list of strings"); 471 raise_luaL_error(L_, "required module list should be a list of strings");
472 } else { 472 } else {
473 // require the module in the target state, and populate the lookup table there too 473 // require the module in the target state, and populate the lookup table there too
474 std::string_view const _name{ luaG_tostring(L_, kIdxTop) }; 474 std::string_view const _name{ luaW_tostring(L_, kIdxTop) };
475 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl); 475 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl);
476 476
477 // require the module in the target lane 477 // require the module in the target lane
@@ -480,7 +480,7 @@ LUAG_FUNC(lane_new)
480 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: 480 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2:
481 raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first"); 481 raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first");
482 } else { 482 } else {
483 luaG_pushstring(_L2, _name); // L_: [fixed] args... n "modname" L2: require() name 483 luaW_pushstring(_L2, _name); // L_: [fixed] args... n "modname" L2: require() name
484 LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) }; // L_: [fixed] args... n "modname" L2: ret/errcode 484 LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) }; // L_: [fixed] args... n "modname" L2: ret/errcode
485 if (_rc != LuaError::OK) { 485 if (_rc != LuaError::OK) {
486 // propagate error to main state if any 486 // propagate error to main state if any
@@ -515,7 +515,7 @@ LUAG_FUNC(lane_new)
515 lua_pushnil(L_); // L_: [fixed] args... nil L2: 515 lua_pushnil(L_); // L_: [fixed] args... nil L2:
516 // Lua 5.2 wants us to push the globals table on the stack 516 // Lua 5.2 wants us to push the globals table on the stack
517 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; 517 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
518 luaG_pushglobaltable(_L2); // L_: [fixed] args... nil L2: _G 518 luaW_pushglobaltable(_L2); // L_: [fixed] args... nil L2: _G
519 while (lua_next(L_, _globals_idx)) { // L_: [fixed] args... k v L2: _G 519 while (lua_next(L_, _globals_idx)) { // L_: [fixed] args... k v L2: _G
520 std::ignore = _c.interCopy(2); // L_: [fixed] args... k v L2: _G k v 520 std::ignore = _c.interCopy(2); // L_: [fixed] args... k v L2: _G k v
521 // assign it in L2's globals table 521 // assign it in L2's globals table
@@ -529,7 +529,7 @@ LUAG_FUNC(lane_new)
529 529
530 // Lane main function 530 // Lane main function
531 [[maybe_unused]] int const _errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? 531 [[maybe_unused]] int const _errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh?
532 LuaType const _func_type{ luaG_type(L_, kFuncIdx) }; 532 LuaType const _func_type{ luaW_type(L_, kFuncIdx) };
533 if (_func_type == LuaType::FUNCTION) { 533 if (_func_type == LuaType::FUNCTION) {
534 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl); 534 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl);
535 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 535 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
@@ -546,7 +546,7 @@ LUAG_FUNC(lane_new)
546 raise_luaL_error(L_, "error when parsing lane function code"); 546 raise_luaL_error(L_, "error when parsing lane function code");
547 } 547 }
548 } else { 548 } else {
549 raise_luaL_error(L_, "Expected function, got %s", luaG_typename(L_, _func_type).data()); 549 raise_luaL_error(L_, "Expected function, got %s", luaW_typename(L_, _func_type).data());
550 } 550 }
551 STACK_CHECK(L_, 0); 551 STACK_CHECK(L_, 0);
552 STACK_CHECK(_L2, _errorHandlerCount + 1); 552 STACK_CHECK(_L2, _errorHandlerCount + 1);
@@ -634,7 +634,7 @@ LUAG_FUNC(wakeup_conv)
634 634
635 STACK_CHECK_START_REL(L_, 0); 635 STACK_CHECK_START_REL(L_, 0);
636 auto _readInteger = [L = L_](std::string_view const& name_) { 636 auto _readInteger = [L = L_](std::string_view const& name_) {
637 std::ignore = luaG_getfield(L, StackIndex{ 1 }, name_); 637 std::ignore = luaW_getfield(L, StackIndex{ 1 }, name_);
638 lua_Integer const val{ lua_tointeger(L, -1) }; 638 lua_Integer const val{ lua_tointeger(L, -1) };
639 lua_pop(L, 1); 639 lua_pop(L, 1);
640 return static_cast<int>(val); 640 return static_cast<int>(val);
@@ -650,7 +650,7 @@ LUAG_FUNC(wakeup_conv)
650 // If Lua table has '.isdst' we trust that. If it does not, we'll let 650 // If Lua table has '.isdst' we trust that. If it does not, we'll let
651 // 'mktime' decide on whether the time is within DST or not (value -1). 651 // 'mktime' decide on whether the time is within DST or not (value -1).
652 // 652 //
653 int const _isdst{ (luaG_getfield(L_, StackIndex{ 1 }, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 }; 653 int const _isdst{ (luaW_getfield(L_, StackIndex{ 1 }, "isdst") == LuaType::BOOLEAN) ? lua_toboolean(L_, -1) : -1 };
654 lua_pop(L_, 1); 654 lua_pop(L_, 1);
655 STACK_CHECK(L_, 0); 655 STACK_CHECK(L_, 0);
656 656
@@ -715,8 +715,8 @@ LUAG_FUNC(configure)
715 715
716 Universe* _U{ Universe::Get(L_) }; 716 Universe* _U{ Universe::Get(L_) };
717 bool const _from_master_state{ _U == nullptr }; 717 bool const _from_master_state{ _U == nullptr };
718 std::string_view const _name{ luaG_checkstring(L_, StackIndex{ lua_upvalueindex(1) }) }; 718 std::string_view const _name{ luaW_checkstring(L_, StackIndex{ lua_upvalueindex(1) }) };
719 LUA_ASSERT(L_, luaG_type(L_, StackIndex{ 1 }) == LuaType::TABLE); 719 LUA_ASSERT(L_, luaW_type(L_, StackIndex{ 1 }) == LuaType::TABLE);
720 720
721 STACK_GROW(L_, 4); 721 STACK_GROW(L_, 4);
722 STACK_CHECK_START_ABS(L_, 1); // L_: settings 722 STACK_CHECK_START_ABS(L_, 1); // L_: settings
@@ -726,7 +726,7 @@ LUAG_FUNC(configure)
726 726
727 if (_U == nullptr) { 727 if (_U == nullptr) {
728 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global... 728 // store a hidden reference in the registry to make sure the string is kept around even if a lane decides to manually change the "decoda_name" global...
729 kLaneNameRegKey.setValue(L_, [](lua_State* L_) { luaG_pushstring(L_, "main"); }); 729 kLaneNameRegKey.setValue(L_, [](lua_State* L_) { luaW_pushstring(L_, "main"); });
730 730
731 // create the universe 731 // create the universe
732 _U = Universe::Create(L_); // L_: settings universe 732 _U = Universe::Create(L_); // L_: settings universe
@@ -742,7 +742,7 @@ LUAG_FUNC(configure)
742 lua_pushnil(L_); // L_: settings M nil 742 lua_pushnil(L_); // L_: settings M nil
743 lua_setfield(L_, -2, "configure"); // L_: settings M 743 lua_setfield(L_, -2, "configure"); // L_: settings M
744 // add functions to the module's table 744 // add functions to the module's table
745 luaG_registerlibfuncs(L_, local::sLanesFunctions); 745 luaW_registerlibfuncs(L_, local::sLanesFunctions);
746 746
747 // register core.threads() only if settings say it should be available 747 // register core.threads() only if settings say it should be available
748 if (_U->tracker.isActive()) { 748 if (_U->tracker.isActive()) {
@@ -767,7 +767,7 @@ LUAG_FUNC(configure)
767 lua_pushcclosure(L_, LG_require, 1); // L_: settings M lanes.require 767 lua_pushcclosure(L_, LG_require, 1); // L_: settings M lanes.require
768 lua_setfield(L_, -2, "require"); // L_: settings M 768 lua_setfield(L_, -2, "require"); // L_: settings M
769 769
770 luaG_pushstring( 770 luaW_pushstring(
771 L_, 771 L_,
772 "%d.%d.%d", 772 "%d.%d.%d",
773 LANES_VERSION_MAJOR, 773 LANES_VERSION_MAJOR,
@@ -799,7 +799,7 @@ LUAG_FUNC(configure)
799 // don't do this when called during the initialization of a new lane, 799 // don't do this when called during the initialization of a new lane,
800 // because we will do it after on_state_create() is called, 800 // because we will do it after on_state_create() is called,
801 // and we don't want to skip _G because of caching in case globals are created then 801 // and we don't want to skip _G because of caching in case globals are created then
802 luaG_pushglobaltable(L_); // L_: settings M _G 802 luaW_pushglobaltable(L_); // L_: settings M _G
803 tools::PopulateFuncLookupTable(L_, kIdxTop, {}); 803 tools::PopulateFuncLookupTable(L_, kIdxTop, {});
804 lua_pop(L_, 1); // L_: settings M 804 lua_pop(L_, 1); // L_: settings M
805 } 805 }
@@ -879,10 +879,10 @@ LANES_API int luaopen_lanes_core(lua_State* const L_)
879 879
880 // Prevent PUC-Lua/LuaJIT mismatch. Hopefully this works for MoonJIT too 880 // Prevent PUC-Lua/LuaJIT mismatch. Hopefully this works for MoonJIT too
881 if constexpr (LUAJIT_FLAVOR() == 0) { 881 if constexpr (LUAJIT_FLAVOR() == 0) {
882 if (luaG_getmodule(L_, LUA_JITLIBNAME) != LuaType::NIL) 882 if (luaW_getmodule(L_, LUA_JITLIBNAME) != LuaType::NIL)
883 raise_luaL_error(L_, "Lanes is built for PUC-Lua, don't run from LuaJIT"); 883 raise_luaL_error(L_, "Lanes is built for PUC-Lua, don't run from LuaJIT");
884 } else { 884 } else {
885 if (luaG_getmodule(L_, LUA_JITLIBNAME) == LuaType::NIL) 885 if (luaW_getmodule(L_, LUA_JITLIBNAME) == LuaType::NIL)
886 raise_luaL_error(L_, "Lanes is built for LuaJIT, don't run from PUC-Lua"); 886 raise_luaL_error(L_, "Lanes is built for LuaJIT, don't run from PUC-Lua");
887 } 887 }
888 lua_pop(L_, 1); // L_: 888 lua_pop(L_, 1); // L_: