aboutsummaryrefslogtreecommitdiff
path: root/src/lanes.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/lanes.cpp')
-rw-r--r--src/lanes.cpp44
1 files changed, 22 insertions, 22 deletions
diff --git a/src/lanes.cpp b/src/lanes.cpp
index c91256e..a5c9c8b 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -166,17 +166,17 @@ LUAG_FUNC(set_thread_affinity)
166// upvalue[1]: _G.require 166// upvalue[1]: _G.require
167LUAG_FUNC(require) 167LUAG_FUNC(require)
168{ 168{
169 char const* _name{ lua_tostring(L_, 1) }; // L_: "name" ... 169 std::string_view const _name{ lua_tostringview(L_, 1) }; // L_: "name" ...
170 int const _nargs{ lua_gettop(L_) }; 170 int const _nargs{ lua_gettop(L_) };
171 DEBUGSPEW_CODE(Universe * _U{ universe_get(L_) }); 171 DEBUGSPEW_CODE(Universe * _U{ universe_get(L_) });
172 STACK_CHECK_START_REL(L_, 0); 172 STACK_CHECK_START_REL(L_, 0);
173 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s BEGIN\n" INDENT_END(_U), _name)); 173 DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' BEGIN" << std::endl);
174 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 174 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
175 lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require 175 lua_pushvalue(L_, lua_upvalueindex(1)); // L_: "name" ... require
176 lua_insert(L_, 1); // L_: require "name" ... 176 lua_insert(L_, 1); // L_: require "name" ...
177 lua_call(L_, _nargs, 1); // L_: module 177 lua_call(L_, _nargs, 1); // L_: module
178 populate_func_lookup_table(L_, -1, _name); 178 populate_func_lookup_table(L_, -1, _name);
179 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.require %s END\n" INDENT_END(_U), _name)); 179 DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.require '" << _name << "' END" << std::endl);
180 STACK_CHECK(L_, 0); 180 STACK_CHECK(L_, 0);
181 return 1; 181 return 1;
182} 182}
@@ -188,17 +188,17 @@ LUAG_FUNC(require)
188// lanes.register( "modname", module) 188// lanes.register( "modname", module)
189LUAG_FUNC(register) 189LUAG_FUNC(register)
190{ 190{
191 char const* _name{ luaL_checkstring(L_, 1) }; 191 std::string_view const _name{ luaL_checkstringview(L_, 1) };
192 LuaType const _mod_type{ lua_type_as_enum(L_, 2) }; 192 LuaType const _mod_type{ lua_type_as_enum(L_, 2) };
193 // ignore extra parameters, just in case 193 // ignore extra parameters, just in case
194 lua_settop(L_, 2); 194 lua_settop(L_, 2);
195 luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type"); 195 luaL_argcheck(L_, (_mod_type == LuaType::TABLE) || (_mod_type == LuaType::FUNCTION), 2, "unexpected module type");
196 DEBUGSPEW_CODE(Universe* _U = universe_get(L_)); 196 DEBUGSPEW_CODE(Universe* _U = universe_get(L_));
197 STACK_CHECK_START_REL(L_, 0); // "name" mod_table 197 STACK_CHECK_START_REL(L_, 0); // "name" mod_table
198 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s BEGIN\n" INDENT_END(_U), _name)); 198 DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' BEGIN" << std::endl);
199 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 199 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
200 populate_func_lookup_table(L_, -1, _name); 200 populate_func_lookup_table(L_, -1, _name);
201 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lanes.register %s END\n" INDENT_END(_U), _name)); 201 DEBUGSPEW_CODE(DebugSpew(_U) << "lanes.register '" << _name << "' END" << std::endl);
202 STACK_CHECK(L_, 0); 202 STACK_CHECK(L_, 0);
203 return 0; 203 return 0;
204} 204}
@@ -236,7 +236,7 @@ LUAG_FUNC(lane_new)
236 LUA_ASSERT(L_, _nargs >= 0); 236 LUA_ASSERT(L_, _nargs >= 0);
237 237
238 Universe* const _U{ universe_get(L_) }; 238 Universe* const _U{ universe_get(L_) };
239 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: setup\n" INDENT_END(_U))); 239 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: setup" << std::endl);
240 240
241 char const* const _libs_str{ lua_tostring(L_, kLibsIdx) }; 241 char const* const _libs_str{ lua_tostring(L_, kLibsIdx) };
242 lua_State* const _L2{ luaG_newstate(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2: 242 lua_State* const _L2{ luaG_newstate(_U, SourceState{ L_ }, _libs_str) }; // L_: [fixed] ... L2:
@@ -288,7 +288,7 @@ LUAG_FUNC(lane_new)
288 private: 288 private:
289 void prepareUserData() 289 void prepareUserData()
290 { 290 {
291 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: preparing lane userdata\n" INDENT_END(lane->U))); 291 DEBUGSPEW_CODE(DebugSpew(lane->U) << "lane_new: preparing lane userdata" << std::endl);
292 STACK_CHECK_START_REL(L, 0); 292 STACK_CHECK_START_REL(L, 0);
293 // a Lane full userdata needs a single uservalue 293 // a Lane full userdata needs a single uservalue
294 Lane** const _ud{ lua_newuserdatauv<Lane*>(L, 1) }; // L: ... lane 294 Lane** const _ud{ lua_newuserdatauv<Lane*>(L, 1) }; // L: ... lane
@@ -344,7 +344,7 @@ LUAG_FUNC(lane_new)
344 } 344 }
345 } onExit{ L_, _lane}; 345 } onExit{ L_, _lane};
346 // launch the thread early, it will sync with a std::latch to parallelize OS thread warmup and L2 preparation 346 // launch the thread early, it will sync with a std::latch to parallelize OS thread warmup and L2 preparation
347 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: launching thread\n" INDENT_END(_U))); 347 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: launching thread" << std::endl);
348 // public Lanes API accepts a generic range -3/+3 348 // public Lanes API accepts a generic range -3/+3
349 // that will be remapped into the platform-specific scheduler priority scheme 349 // that will be remapped into the platform-specific scheduler priority scheme
350 // On some platforms, -3 is equivalent to -2 and +3 to +2 350 // On some platforms, -3 is equivalent to -2 and +3 to +2
@@ -371,7 +371,7 @@ LUAG_FUNC(lane_new)
371 // package 371 // package
372 int const _package_idx{ lua_isnoneornil(L_, kPackIdx) ? 0 : kPackIdx }; 372 int const _package_idx{ lua_isnoneornil(L_, kPackIdx) ? 0 : kPackIdx };
373 if (_package_idx != 0) { 373 if (_package_idx != 0) {
374 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: update 'package'\n" INDENT_END(_U))); 374 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: update 'package'" << std::endl);
375 // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack 375 // when copying with mode LookupMode::LaneBody, should raise an error in case of problem, not leave it one the stack
376 InterCopyContext c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, SourceIndex{ _package_idx }, {}, {}, {} }; 376 InterCopyContext c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, SourceIndex{ _package_idx }, {}, {}, {} };
377 [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() }; 377 [[maybe_unused]] InterCopyResult const ret{ c.inter_copy_package() };
@@ -382,7 +382,7 @@ LUAG_FUNC(lane_new)
382 int const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx }; 382 int const _required_idx{ lua_isnoneornil(L_, kRequIdx) ? 0 : kRequIdx };
383 if (_required_idx != 0) { 383 if (_required_idx != 0) {
384 int _nbRequired{ 1 }; 384 int _nbRequired{ 1 };
385 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require 'required' list\n" INDENT_END(_U))); 385 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: process 'required' list" << std::endl);
386 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 386 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
387 // should not happen, was checked in lanes.lua before calling lane_new() 387 // should not happen, was checked in lanes.lua before calling lane_new()
388 if (lua_type(L_, _required_idx) != LUA_TTABLE) { 388 if (lua_type(L_, _required_idx) != LUA_TTABLE) {
@@ -396,7 +396,7 @@ LUAG_FUNC(lane_new)
396 } else { 396 } else {
397 // require the module in the target state, and populate the lookup table there too 397 // require the module in the target state, and populate the lookup table there too
398 std::string_view const _name{ lua_tostringview(L_, -1) }; 398 std::string_view const _name{ lua_tostringview(L_, -1) };
399 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: require '%s'\n" INDENT_END(_U), _name.data())); 399 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: require '" << _name << "'" << std::endl);
400 400
401 // require the module in the target lane 401 // require the module in the target lane
402 lua_getglobal(_L2, "require"); // L_: [fixed] args... n "modname" L2: require()? 402 lua_getglobal(_L2, "require"); // L_: [fixed] args... n "modname" L2: require()?
@@ -404,7 +404,7 @@ LUAG_FUNC(lane_new)
404 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: 404 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2:
405 raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first"); 405 raise_luaL_error(L_, "cannot pre-require modules without loading 'package' library first");
406 } else { 406 } else {
407 lua_pushlstring(_L2, _name.data(), _name.size()); // L_: [fixed] args... n "modname" L2: require() name 407 std::ignore = lua_pushstringview(_L2, _name); // L_: [fixed] args... n "modname" L2: require() name
408 LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) }; // L_: [fixed] args... n "modname" L2: ret/errcode 408 LuaError const _rc{ lua_pcall(_L2, 1, 1, 0) }; // L_: [fixed] args... n "modname" L2: ret/errcode
409 if (_rc != LuaError::OK) { 409 if (_rc != LuaError::OK) {
410 // propagate error to main state if any 410 // propagate error to main state if any
@@ -414,7 +414,7 @@ LUAG_FUNC(lane_new)
414 } 414 }
415 // here the module was successfully required // L_: [fixed] args... n "modname" L2: ret 415 // here the module was successfully required // L_: [fixed] args... n "modname" L2: ret
416 // after requiring the module, register the functions it exported in our name<->function database 416 // after requiring the module, register the functions it exported in our name<->function database
417 populate_func_lookup_table(_L2, -1, _name.data()); 417 populate_func_lookup_table(_L2, -1, _name);
418 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2: 418 lua_pop(_L2, 1); // L_: [fixed] args... n "modname" L2:
419 } 419 }
420 } 420 }
@@ -430,7 +430,7 @@ LUAG_FUNC(lane_new)
430 // 430 //
431 int const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? 0 : kGlobIdx }; 431 int const _globals_idx{ lua_isnoneornil(L_, kGlobIdx) ? 0 : kGlobIdx };
432 if (_globals_idx != 0) { 432 if (_globals_idx != 0) {
433 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer globals\n" INDENT_END(_U))); 433 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer globals" << std::endl);
434 if (!lua_istable(L_, _globals_idx)) { 434 if (!lua_istable(L_, _globals_idx)) {
435 raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, _globals_idx)); 435 raise_luaL_error(L_, "Expected table, got %s", luaL_typename(L_, _globals_idx));
436 } 436 }
@@ -455,7 +455,7 @@ LUAG_FUNC(lane_new)
455 [[maybe_unused]] int const errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh? 455 [[maybe_unused]] int const errorHandlerCount{ _lane->pushErrorHandler() }; // L_: [fixed] args... L2: eh?
456 LuaType const _func_type{ lua_type_as_enum(L_, kFuncIdx) }; 456 LuaType const _func_type{ lua_type_as_enum(L_, kFuncIdx) };
457 if (_func_type == LuaType::FUNCTION) { 457 if (_func_type == LuaType::FUNCTION) {
458 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane body\n" INDENT_END(_U))); 458 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane body" << std::endl);
459 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 459 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
460 lua_pushvalue(L_, kFuncIdx); // L_: [fixed] args... func L2: eh? 460 lua_pushvalue(L_, kFuncIdx); // L_: [fixed] args... func L2: eh?
461 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; 461 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
@@ -464,7 +464,7 @@ LUAG_FUNC(lane_new)
464 raise_luaL_error(L_, "tried to copy unsupported types"); 464 raise_luaL_error(L_, "tried to copy unsupported types");
465 } 465 }
466 } else if (_func_type == LuaType::STRING) { 466 } else if (_func_type == LuaType::STRING) {
467 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: compile lane body\n" INDENT_END(_U))); 467 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: compile lane body" << std::endl);
468 // compile the string 468 // compile the string
469 if (luaL_loadstring(_L2, lua_tostring(L_, kFuncIdx)) != 0) { // L_: [fixed] args... L2: eh? func 469 if (luaL_loadstring(_L2, lua_tostring(L_, kFuncIdx)) != 0) { // L_: [fixed] args... L2: eh? func
470 raise_luaL_error(L_, "error when parsing lane function code"); 470 raise_luaL_error(L_, "error when parsing lane function code");
@@ -478,7 +478,7 @@ LUAG_FUNC(lane_new)
478 478
479 // revive arguments 479 // revive arguments
480 if (_nargs > 0) { 480 if (_nargs > 0) {
481 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "lane_new: transfer lane arguments\n" INDENT_END(_U))); 481 DEBUGSPEW_CODE(DebugSpew(_U) << "lane_new: transfer lane arguments" << std::endl);
482 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 482 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
483 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} }; 483 InterCopyContext _c{ _U, DestState{ _L2 }, SourceState{ L_ }, {}, {}, {}, {}, {} };
484 InterCopyResult const res{ _c.inter_move(_nargs) }; // L_: [fixed] L2: eh? func args... 484 InterCopyResult const res{ _c.inter_move(_nargs) }; // L_: [fixed] L2: eh? func args...
@@ -627,13 +627,13 @@ LUAG_FUNC(configure)
627 627
628 Universe* _U{ universe_get(L_) }; 628 Universe* _U{ universe_get(L_) };
629 bool const _from_master_state{ _U == nullptr }; 629 bool const _from_master_state{ _U == nullptr };
630 char const* const _name{ luaL_checkstring(L_, lua_upvalueindex(1)) }; 630 std::string_view const _name{ luaL_checkstringview(L_, lua_upvalueindex(1)) };
631 LUA_ASSERT(L_, lua_type(L_, 1) == LUA_TTABLE); 631 LUA_ASSERT(L_, lua_type(L_, 1) == LUA_TTABLE);
632 632
633 STACK_GROW(L_, 4); 633 STACK_GROW(L_, 4);
634 STACK_CHECK_START_ABS(L_, 1); // L_: settings 634 STACK_CHECK_START_ABS(L_, 1); // L_: settings
635 635
636 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() BEGIN\n" INDENT_END(_U), L_)); 636 DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": lanes.configure() BEGIN" << std::endl);
637 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U }); 637 DEBUGSPEW_CODE(DebugSpewIndentScope _scope{ _U });
638 638
639 if (_U == nullptr) { 639 if (_U == nullptr) {
@@ -757,7 +757,7 @@ LUAG_FUNC(configure)
757 // because we will do it after on_state_create() is called, 757 // because we will do it after on_state_create() is called,
758 // and we don't want to skip _G because of caching in case globals are created then 758 // and we don't want to skip _G because of caching in case globals are created then
759 lua_pushglobaltable(L_); // L_: settings M _G 759 lua_pushglobaltable(L_); // L_: settings M _G
760 populate_func_lookup_table(L_, -1, nullptr); 760 populate_func_lookup_table(L_, -1, {});
761 lua_pop(L_, 1); // L_: settings M 761 lua_pop(L_, 1); // L_: settings M
762 } 762 }
763 lua_pop(L_, 1); // L_: settings 763 lua_pop(L_, 1); // L_: settings
@@ -765,7 +765,7 @@ LUAG_FUNC(configure)
765 // set _R[kConfigRegKey] = settings 765 // set _R[kConfigRegKey] = settings
766 kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); }); 766 kConfigRegKey.setValue(L_, [](lua_State* L_) { lua_pushvalue(L_, -2); });
767 STACK_CHECK(L_, 1); 767 STACK_CHECK(L_, 1);
768 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "%p: lanes.configure() END\n" INDENT_END(_U), L_)); 768 DEBUGSPEW_CODE(DebugSpew(_U) << L_ << ": lanes.configure() END" << std::endl);
769 // Return the settings table 769 // Return the settings table
770 return 1; 770 return 1;
771} 771}