aboutsummaryrefslogtreecommitdiff
path: root/src/linda.cpp
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2025-07-04 13:50:53 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2025-07-04 13:50:53 +0200
commit042055968ab0c48faec607889814e38c50c09efa (patch)
tree4ec067a03ffcb720d8bf38968d5f84ccec8230d0 /src/linda.cpp
parent963afcbb3d9dac47b84552ed11e53a0641ad924d (diff)
downloadlanes-042055968ab0c48faec607889814e38c50c09efa.tar.gz
lanes-042055968ab0c48faec607889814e38c50c09efa.tar.bz2
lanes-042055968ab0c48faec607889814e38c50c09efa.zip
Changed lua wrapper prefixes from luaG_ to luaW_ (w as in wrapper!)
Diffstat (limited to 'src/linda.cpp')
-rw-r--r--src/linda.cpp60
1 files changed, 30 insertions, 30 deletions
diff --git a/src/linda.cpp b/src/linda.cpp
index 5fb8279..75d1748 100644
--- a/src/linda.cpp
+++ b/src/linda.cpp
@@ -47,7 +47,7 @@ namespace {
47 { 47 {
48 STACK_CHECK_START_REL(L_, 0); 48 STACK_CHECK_START_REL(L_, 0);
49 for (StackIndex const _i : std::ranges::iota_view{ start_, StackIndex{ end_ + 1 } }) { 49 for (StackIndex const _i : std::ranges::iota_view{ start_, StackIndex{ end_ + 1 } }) {
50 LuaType const _t{ luaG_type(L_, _i) }; 50 LuaType const _t{ luaW_type(L_, _i) };
51 switch (_t) { 51 switch (_t) {
52 case LuaType::BOOLEAN: 52 case LuaType::BOOLEAN:
53 case LuaType::NUMBER: 53 case LuaType::NUMBER:
@@ -109,13 +109,13 @@ namespace {
109 { 109 {
110 Linda* const _linda{ ToLinda<OPT>(L_, idx_) }; 110 Linda* const _linda{ ToLinda<OPT>(L_, idx_) };
111 if (_linda != nullptr) { 111 if (_linda != nullptr) {
112 luaG_pushstring(L_, "Linda: "); 112 luaW_pushstring(L_, "Linda: ");
113 std::string_view const _lindaName{ _linda->getName() }; 113 std::string_view const _lindaName{ _linda->getName() };
114 if (!_lindaName.empty()) { 114 if (!_lindaName.empty()) {
115 luaG_pushstring(L_, _lindaName); 115 luaW_pushstring(L_, _lindaName);
116 } else { 116 } else {
117 // obfuscate the pointer so that we can't read the value with our eyes out of a script 117 // obfuscate the pointer so that we can't read the value with our eyes out of a script
118 luaG_pushstring(L_, "%p", _linda->obfuscated()); 118 luaW_pushstring(L_, "%p", _linda->obfuscated());
119 } 119 }
120 lua_concat(L_, 2); 120 lua_concat(L_, 2);
121 return 1; 121 return 1;
@@ -132,7 +132,7 @@ namespace {
132 StackIndex _key_i{ 2 }; // index of first slot, if timeout not there 132 StackIndex _key_i{ 2 }; // index of first slot, if timeout not there
133 133
134 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() }; 134 std::chrono::time_point<std::chrono::steady_clock> _until{ std::chrono::time_point<std::chrono::steady_clock>::max() };
135 if (luaG_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion 135 if (luaW_type(L_, StackIndex{ 2 }) == LuaType::NUMBER) { // we don't want to use lua_isnumber() because of autocoercion
136 lua_Duration const _duration{ lua_tonumber(L_, 2) }; 136 lua_Duration const _duration{ lua_tonumber(L_, 2) };
137 if (_duration.count() >= 0.0) { 137 if (_duration.count() >= 0.0) {
138 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration); 138 _until = std::chrono::steady_clock::now() + std::chrono::duration_cast<std::chrono::steady_clock::duration>(_duration);
@@ -279,7 +279,7 @@ namespace {
279 if (_nbPushed == 0) { 279 if (_nbPushed == 0) {
280 // not enough data in the linda slot to fulfill the request, return nil, "timeout" 280 // not enough data in the linda slot to fulfill the request, return nil, "timeout"
281 lua_pushnil(L_); 281 lua_pushnil(L_);
282 luaG_pushstring(L_, "timeout"); 282 luaW_pushstring(L_, "timeout");
283 return 2; 283 return 2;
284 } 284 }
285 return _nbPushed; 285 return _nbPushed;
@@ -350,16 +350,16 @@ Linda* Linda::CreateTimerLinda(lua_State* const L_)
350 // Initialize 'timerLinda'; a common Linda object shared by all states 350 // Initialize 'timerLinda'; a common Linda object shared by all states
351 lua_pushcfunction(L_, LG_linda); // L_: lanes.linda 351 lua_pushcfunction(L_, LG_linda); // L_: lanes.linda
352 lua_createtable(L_, 0, 3); // L_: lanes.linda {} 352 lua_createtable(L_, 0, 3); // L_: lanes.linda {}
353 luaG_pushstring(L_, "lanes-timer"); // L_: lanes.linda {} "lanes-timer" 353 luaW_pushstring(L_, "lanes-timer"); // L_: lanes.linda {} "lanes-timer"
354 luaG_setfield(L_, StackIndex{ -2 }, std::string_view{ "name" }); // L_: lanes.linda { .name="lanes-timer" } 354 luaW_setfield(L_, StackIndex{ -2 }, std::string_view{ "name" }); // L_: lanes.linda { .name="lanes-timer" }
355 lua_pushinteger(L_, 0); // L_: lanes.linda { .name="lanes-timer" } 0 355 lua_pushinteger(L_, 0); // L_: lanes.linda { .name="lanes-timer" } 0
356 luaG_setfield(L_, StackIndex{ -2 }, std::string_view{ "group" }); // L_: lanes.linda { .name="lanes-timer" .group = 0 } 356 luaW_setfield(L_, StackIndex{ -2 }, std::string_view{ "group" }); // L_: lanes.linda { .name="lanes-timer" .group = 0 }
357 // note that wake_period is not set (will default to the value in the universe) 357 // note that wake_period is not set (will default to the value in the universe)
358 lua_call(L_, 1, 1); // L_: linda 358 lua_call(L_, 1, 1); // L_: linda
359 STACK_CHECK(L_, 1); 359 STACK_CHECK(L_, 1);
360 360
361 // Proxy userdata contents is only a 'DeepPrelude*' pointer 361 // Proxy userdata contents is only a 'DeepPrelude*' pointer
362 auto const _timerLinda{ *luaG_tofulluserdata<Linda*>(L_, kIdxTop) }; 362 auto const _timerLinda{ *luaW_tofulluserdata<Linda*>(L_, kIdxTop) };
363 // increment refcount so that this linda remains alive as long as the universe exists. 363 // increment refcount so that this linda remains alive as long as the universe exists.
364 _timerLinda->refcount.fetch_add(1, std::memory_order_relaxed); 364 _timerLinda->refcount.fetch_add(1, std::memory_order_relaxed);
365 lua_pop(L_, 1); // L_: 365 lua_pop(L_, 1); // L_:
@@ -452,7 +452,7 @@ int Linda::ProtectedCall(lua_State* const L_, lua_CFunction const f_)
452 452
453void Linda::pushCancelString(lua_State* L_) const 453void Linda::pushCancelString(lua_State* L_) const
454{ 454{
455 luaG_pushstring(L_, cancelStatus == Status::Cancelled ? "cancelled" : "active"); 455 luaW_pushstring(L_, cancelStatus == Status::Cancelled ? "cancelled" : "active");
456} 456}
457 457
458// ################################################################################################# 458// #################################################################################################
@@ -504,7 +504,7 @@ void Linda::setName(std::string_view const& name_)
504LUAG_FUNC(linda_cancel) 504LUAG_FUNC(linda_cancel)
505{ 505{
506 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) }; 506 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) };
507 std::string_view const _who{ luaG_optstring(L_, StackIndex{ 2 }, "both") }; 507 std::string_view const _who{ luaW_optstring(L_, StackIndex{ 2 }, "both") };
508 // make sure we got 2 arguments: the linda and the cancellation mode 508 // make sure we got 2 arguments: the linda and the cancellation mode
509 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments"); 509 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments");
510 510
@@ -592,13 +592,13 @@ static int linda_index_string(lua_State* L_)
592 Linda* const _linda{ ToLinda<false>(L_, kIdxSelf) }; 592 Linda* const _linda{ ToLinda<false>(L_, kIdxSelf) };
593 LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: linda "key" 593 LUA_ASSERT(L_, lua_gettop(L_) == 2); // L_: linda "key"
594 594
595 std::string_view const _keystr{ luaG_tostring(L_, kIdxKey) }; 595 std::string_view const _keystr{ luaW_tostring(L_, kIdxKey) };
596 lua_settop(L_, 2); // keep only our original arguments on the stack 596 lua_settop(L_, 2); // keep only our original arguments on the stack
597 597
598 // look in metatable first 598 // look in metatable first
599 lua_getmetatable(L_, kIdxSelf); // L_: linda "key" mt 599 lua_getmetatable(L_, kIdxSelf); // L_: linda "key" mt
600 lua_replace(L_, -3); // L_: mt "key" 600 lua_replace(L_, -3); // L_: mt "key"
601 if (luaG_rawget(L_, StackIndex{ -2 }) != LuaType::NIL) { // found something? // L_: mt value 601 if (luaW_rawget(L_, StackIndex{ -2 }) != LuaType::NIL) { // found something? // L_: mt value
602 return 1; // done 602 return 1; // done
603 } 603 }
604 604
@@ -618,12 +618,12 @@ static LUAG_FUNC(linda_index)
618 static constexpr StackIndex kIdxKey{ 2 }; 618 static constexpr StackIndex kIdxKey{ 2 };
619 LUA_ASSERT(L_, lua_gettop(L_) == 2); 619 LUA_ASSERT(L_, lua_gettop(L_) == 2);
620 620
621 switch (luaG_type(L_, kIdxKey)) { 621 switch (luaW_type(L_, kIdxKey)) {
622 case LuaType::STRING: 622 case LuaType::STRING:
623 return linda_index_string(L_); // stack modification is undefined, returned value is at the top 623 return linda_index_string(L_); // stack modification is undefined, returned value is at the top
624 624
625 default: // unknown key 625 default: // unknown key
626 raise_luaL_error(L_, "Unsupported linda indexing key type %s", luaG_typename(L_, kIdxKey).data()); 626 raise_luaL_error(L_, "Unsupported linda indexing key type %s", luaW_typename(L_, kIdxKey).data());
627 } 627 }
628} 628}
629 629
@@ -766,7 +766,7 @@ LUAG_FUNC(linda_limit)
766 int const _nargs{ lua_gettop(L_) }; 766 int const _nargs{ lua_gettop(L_) };
767 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments"); 767 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments");
768 // make sure we got a numeric limit, or "unlimited", (or nothing) 768 // make sure we got a numeric limit, or "unlimited", (or nothing)
769 bool const _unlimited{ luaG_tostring(L_, StackIndex{ 3 }) == "unlimited" }; 769 bool const _unlimited{ luaW_tostring(L_, StackIndex{ 3 }) == "unlimited" };
770 LindaLimit const _val{ _unlimited ? std::numeric_limits<LindaLimit::type>::max() : static_cast<LindaLimit::type>(luaL_optinteger(L_, 3, 0)) }; 770 LindaLimit const _val{ _unlimited ? std::numeric_limits<LindaLimit::type>::max() : static_cast<LindaLimit::type>(luaL_optinteger(L_, 3, 0)) };
771 if (_val < 0) { 771 if (_val < 0) {
772 raise_luaL_argerror(L_, StackIndex{ 3 }, "limit must be >= 0"); 772 raise_luaL_argerror(L_, StackIndex{ 3 }, "limit must be >= 0");
@@ -777,23 +777,23 @@ LUAG_FUNC(linda_limit)
777 KeeperCallResult _pushed; 777 KeeperCallResult _pushed;
778 if (_linda->cancelStatus == Linda::Active) { 778 if (_linda->cancelStatus == Linda::Active) {
779 if (_unlimited) { 779 if (_unlimited) {
780 LUA_ASSERT(L_, lua_gettop(L_) == 3 && luaG_tostring(L_, StackIndex{ 3 }) == "unlimited"); 780 LUA_ASSERT(L_, lua_gettop(L_) == 3 && luaW_tostring(L_, StackIndex{ 3 }) == "unlimited");
781 // inside the Keeper, unlimited is signified with a -1 limit (can't use nil because of nil kNilSentinel conversions!) 781 // inside the Keeper, unlimited is signified with a -1 limit (can't use nil because of nil kNilSentinel conversions!)
782 lua_pop(L_, 1); // L_: linda slot 782 lua_pop(L_, 1); // L_: linda slot
783 lua_pushinteger(L_, -1); // L_: linda slot nil 783 lua_pushinteger(L_, -1); // L_: linda slot nil
784 } 784 }
785 Keeper* const _keeper{ _linda->whichKeeper() }; 785 Keeper* const _keeper{ _linda->whichKeeper() };
786 _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, StackIndex{ 2 }); 786 _pushed = keeper_call(_keeper->K, KEEPER_API(limit), L_, _linda, StackIndex{ 2 });
787 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 2) && luaG_type(L_, kIdxTop) == LuaType::STRING); 787 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 2) && luaW_type(L_, kIdxTop) == LuaType::STRING);
788 if (_nargs == 3) { // 3 args: setting the limit 788 if (_nargs == 3) { // 3 args: setting the limit
789 // changing the limit: no error, boolean value saying if we should wake blocked writer threads 789 // changing the limit: no error, boolean value saying if we should wake blocked writer threads
790 LUA_ASSERT(L_, luaG_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN); // L_: bool string 790 LUA_ASSERT(L_, luaW_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN); // L_: bool string
791 if (lua_toboolean(L_, -2)) { 791 if (lua_toboolean(L_, -2)) {
792 _linda->readHappened.notify_all(); // To be done from within the 'K' locking area 792 _linda->readHappened.notify_all(); // To be done from within the 'K' locking area
793 } 793 }
794 } else { // 2 args: reading the limit 794 } else { // 2 args: reading the limit
795 // reading the limit: a number >=0 or "unlimited" 795 // reading the limit: a number >=0 or "unlimited"
796 LUA_ASSERT(L_, luaG_type(L_, StackIndex{ -2 }) == LuaType::NUMBER || luaG_tostring(L_, StackIndex{ -2 }) == "unlimited"); 796 LUA_ASSERT(L_, luaW_type(L_, StackIndex{ -2 }) == LuaType::NUMBER || luaW_tostring(L_, StackIndex{ -2 }) == "unlimited");
797 } 797 }
798 } else { // linda is cancelled 798 } else { // linda is cancelled
799 // do nothing and return nil,lanes.cancel_error 799 // do nothing and return nil,lanes.cancel_error
@@ -849,7 +849,7 @@ LUAG_FUNC(linda_restrict)
849 int const _nargs{ lua_gettop(L_) }; 849 int const _nargs{ lua_gettop(L_) };
850 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments"); 850 luaL_argcheck(L_, _nargs == 2 || _nargs == 3, 2, "wrong number of arguments");
851 // make sure we got a known restrict mode, (or nothing) 851 // make sure we got a known restrict mode, (or nothing)
852 std::string_view const _mode{ luaG_tostring(L_, StackIndex{ 3 }) }; 852 std::string_view const _mode{ luaW_tostring(L_, StackIndex{ 3 }) };
853 if (!_mode.empty() && (_mode != "none" && _mode != "set/get" && _mode != "send/receive")) { 853 if (!_mode.empty() && (_mode != "none" && _mode != "set/get" && _mode != "send/receive")) {
854 raise_luaL_argerror(L_, StackIndex{ 3 }, "unknown restrict mode"); 854 raise_luaL_argerror(L_, StackIndex{ 3 }, "unknown restrict mode");
855 } 855 }
@@ -861,7 +861,7 @@ LUAG_FUNC(linda_restrict)
861 Keeper* const _keeper{ _linda->whichKeeper() }; 861 Keeper* const _keeper{ _linda->whichKeeper() };
862 _pushed = keeper_call(_keeper->K, KEEPER_API(restrict), L_, _linda, StackIndex{ 2 }); 862 _pushed = keeper_call(_keeper->K, KEEPER_API(restrict), L_, _linda, StackIndex{ 2 });
863 // we should get a single return value: the string describing the previous restrict mode 863 // we should get a single return value: the string describing the previous restrict mode
864 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 1) && luaG_type(L_, kIdxTop) == LuaType::STRING); 864 LUA_ASSERT(L_, _pushed.has_value() && (_pushed.value() == 1) && luaW_type(L_, kIdxTop) == LuaType::STRING);
865 } else { // linda is cancelled 865 } else { // linda is cancelled
866 // do nothing and return nil,lanes.cancel_error 866 // do nothing and return nil,lanes.cancel_error
867 lua_pushnil(L_); 867 lua_pushnil(L_);
@@ -981,7 +981,7 @@ LUAG_FUNC(linda_send)
981 } else { 981 } else {
982 // not enough room in the Linda slot to fulfill the request, return nil, "timeout" 982 // not enough room in the Linda slot to fulfill the request, return nil, "timeout"
983 lua_pushnil(L_); 983 lua_pushnil(L_);
984 luaG_pushstring(L_, "timeout"); 984 luaW_pushstring(L_, "timeout");
985 return 2; 985 return 2;
986 } 986 }
987 } 987 }
@@ -1016,7 +1016,7 @@ LUAG_FUNC(linda_set)
1016 if (kRestrictedChannel.equals(L_, kIdxTop)) { 1016 if (kRestrictedChannel.equals(L_, kIdxTop)) {
1017 raise_luaL_error(L_, "Key is restricted"); 1017 raise_luaL_error(L_, "Key is restricted");
1018 } 1018 }
1019 LUA_ASSERT(L_, _pushed.value() == 2 && luaG_type(L_, kIdxTop) == LuaType::STRING && luaG_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN); 1019 LUA_ASSERT(L_, _pushed.value() == 2 && luaW_type(L_, kIdxTop) == LuaType::STRING && luaW_type(L_, StackIndex{ -2 }) == LuaType::BOOLEAN);
1020 1020
1021 if (_has_data) { 1021 if (_has_data) {
1022 // we put some data in the slot, tell readers that they should wake 1022 // we put some data in the slot, tell readers that they should wake
@@ -1078,7 +1078,7 @@ LUAG_FUNC(linda_towatch)
1078LUAG_FUNC(linda_wake) 1078LUAG_FUNC(linda_wake)
1079{ 1079{
1080 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) }; 1080 Linda* const _linda{ ToLinda<false>(L_, StackIndex{ 1 }) };
1081 std::string_view const _who{ luaG_optstring(L_, StackIndex{ 2 }, "both") }; 1081 std::string_view const _who{ luaW_optstring(L_, StackIndex{ 2 }, "both") };
1082 // make sure we got 2 arguments: the linda and the wake targets 1082 // make sure we got 2 arguments: the linda and the wake targets
1083 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments"); 1083 luaL_argcheck(L_, lua_gettop(L_) <= 2, 2, "wrong number of arguments");
1084 1084
@@ -1154,8 +1154,8 @@ LUAG_FUNC(linda)
1154 if (lua_isnil(L_, kIdxTop)) { 1154 if (lua_isnil(L_, kIdxTop)) {
1155 lua_pop(L_, 1); 1155 lua_pop(L_, 1);
1156 lua_pushnumber(L_, _U->lindaWakePeriod.count()); 1156 lua_pushnumber(L_, _U->lindaWakePeriod.count());
1157 } else if (luaG_type(L_, kIdxTop) == LuaType::STRING) { 1157 } else if (luaW_type(L_, kIdxTop) == LuaType::STRING) {
1158 if (luaG_tostring(L_, kIdxTop) != "never") { 1158 if (luaW_tostring(L_, kIdxTop) != "never") {
1159 luaL_argerror(L_, 1, "invalid wake_period"); 1159 luaL_argerror(L_, 1, "invalid wake_period");
1160 } else { 1160 } else {
1161 lua_pop(L_, 1); 1161 lua_pop(L_, 1);
@@ -1177,7 +1177,7 @@ LUAG_FUNC(linda)
1177 1177
1178#if LUA_VERSION_NUM >= 504 // to-be-closed support starts with Lua 5.4 1178#if LUA_VERSION_NUM >= 504 // to-be-closed support starts with Lua 5.4
1179 lua_getfield(L_, 1, "close_handler"); // L_: {} wake_period group close_handler 1179 lua_getfield(L_, 1, "close_handler"); // L_: {} wake_period group close_handler
1180 LuaType const _handlerType{ luaG_type(L_, kIdxTop) }; 1180 LuaType const _handlerType{ luaW_type(L_, kIdxTop) };
1181 if (_handlerType == LuaType::NIL) { 1181 if (_handlerType == LuaType::NIL) {
1182 lua_pop(L_, 1); // L_: {} wake_period group 1182 lua_pop(L_, 1); // L_: {} wake_period group
1183 } else if (_handlerType == LuaType::USERDATA || _handlerType == LuaType::TABLE) { 1183 } else if (_handlerType == LuaType::USERDATA || _handlerType == LuaType::TABLE) {
@@ -1188,7 +1188,7 @@ LUAG_FUNC(linda)
1188 } 1188 }
1189#endif // LUA_VERSION_NUM >= 504 1189#endif // LUA_VERSION_NUM >= 504
1190 1190
1191 auto const _nameType{ luaG_getfield(L_, StackIndex{ 1 }, "name") }; // L_: {} wake_period group [close_handler] name 1191 auto const _nameType{ luaW_getfield(L_, StackIndex{ 1 }, "name") }; // L_: {} wake_period group [close_handler] name
1192 luaL_argcheck(L_, _nameType == LuaType::NIL || _nameType == LuaType::STRING, 1, "name is not a string"); 1192 luaL_argcheck(L_, _nameType == LuaType::NIL || _nameType == LuaType::STRING, 1, "name is not a string");
1193 lua_replace(L_, 1); // L_: name wake_period group [close_handler] 1193 lua_replace(L_, 1); // L_: name wake_period group [close_handler]
1194 } 1194 }