aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-06-06 17:22:45 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-06-06 17:22:45 +0200
commit91a34bd09900967e8b9cccdbd6d94a9f8cc8506c (patch)
tree26ca2e83a6841361ac2d104c9d06bf78edd166e5
parentd9a149a230f9b320113020789beb78a883da3b40 (diff)
downloadlanes-91a34bd09900967e8b9cccdbd6d94a9f8cc8506c.tar.gz
lanes-91a34bd09900967e8b9cccdbd6d94a9f8cc8506c.tar.bz2
lanes-91a34bd09900967e8b9cccdbd6d94a9f8cc8506c.zip
lanes.linda("auto")
-rw-r--r--docs/index.html5
-rw-r--r--src/linda.cpp3
-rw-r--r--src/lindafactory.cpp16
-rw-r--r--tests/basic.lua7
4 files changed, 26 insertions, 5 deletions
diff --git a/docs/index.html b/docs/index.html
index 1b24422..e17c7db 100644
--- a/docs/index.html
+++ b/docs/index.html
@@ -1217,6 +1217,11 @@
1217</pre></td></tr></table> 1217</pre></td></tr></table>
1218 1218
1219<p> 1219<p>
1220 Converting the Linda to a string will yield the provided name prefixed by <tt>"Linda: "</tt>.<br/>
1221 If <tt>opt_name</tt> is omitted, it will evaluate to an hexadecimal number uniquely representing that linda.<br/>
1222 If <tt>opt_name</tt> is <tt>"auto"</tt>, Lanes will try to construct a name from the source location that called <tt>lanes.linda()</tt>. If that fails, the linda name will be <tt>"&lt;unresolved&gt;"</tt>.
1223</p>
1224<p>
1220 Timeouts are given in seconds (&gt= 0, millisecond accuracy) or <tt>nil</tt>. Timeout can be omitted only if the first key is not a number (then it's equivalent to an infinite duration). 1225 Timeouts are given in seconds (&gt= 0, millisecond accuracy) or <tt>nil</tt>. Timeout can be omitted only if the first key is not a number (then it's equivalent to an infinite duration).
1221</p> 1226</p>
1222 1227
diff --git a/src/linda.cpp b/src/linda.cpp
index 33d9f0b..8b6df8e 100644
--- a/src/linda.cpp
+++ b/src/linda.cpp
@@ -88,7 +88,8 @@ template <bool OPT>
88 if (!_lindaName.empty()) { 88 if (!_lindaName.empty()) {
89 std::ignore = luaG_pushstringview(L_, _lindaName); 89 std::ignore = luaG_pushstringview(L_, _lindaName);
90 } else { 90 } else {
91 lua_pushfstring(L_, "%p", _linda); 91 // obfuscate the pointer so that we can't read the value with our eyes out of a script
92 std::ignore = luaG_pushstringview(L_, "%p", std::bit_cast<uintptr_t>(_linda) ^ kConfigRegKey.storage);
92 } 93 }
93 lua_concat(L_, 2); 94 lua_concat(L_, 2);
94 return 1; 95 return 1;
diff --git a/src/lindafactory.cpp b/src/lindafactory.cpp
index d99d546..3f4b9b0 100644
--- a/src/lindafactory.cpp
+++ b/src/lindafactory.cpp
@@ -101,7 +101,7 @@ std::string_view LindaFactory::moduleName() const
101 101
102// ################################################################################################# 102// #################################################################################################
103 103
104DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* L_) const 104DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* const L_) const
105{ 105{
106 std::string_view _linda_name{}; 106 std::string_view _linda_name{};
107 LindaGroup _linda_group{ 0 }; 107 LindaGroup _linda_group{ 0 };
@@ -124,6 +124,20 @@ DeepPrelude* LindaFactory::newDeepObjectInternal(lua_State* L_) const
124 break; 124 break;
125 } 125 }
126 126
127 // store in the linda the location of the script that created it
128 if (_linda_name == "auto") {
129 lua_Debug _ar;
130 if (lua_getstack(L_, 1, &_ar) == 1) { // 1 because we want the name of the function that called lanes.linda (where we currently are)
131 lua_getinfo(L_, "Sln", &_ar);
132 _linda_name = luaG_pushstringview(L_, "%s:%d", _ar.short_src, _ar.currentline);
133 } else {
134 _linda_name = luaG_pushstringview(L_, "<unresolved>");
135 }
136 // since the name is not empty, it is at slot 1, and we can replace "auto" with the result, just in case
137 LUA_ASSERT(L_, luaG_tostringview(L_, 1) == "auto");
138 lua_replace(L_, 1);
139 }
140
127 // The deep data is allocated separately of Lua stack; we might no longer be around when last reference to it is being released. 141 // The deep data is allocated separately of Lua stack; we might no longer be around when last reference to it is being released.
128 // One can use any memory allocation scheme. Just don't use L's allocF because we don't know which state will get the honor of GCing the linda 142 // One can use any memory allocation scheme. Just don't use L's allocF because we don't know which state will get the honor of GCing the linda
129 Universe* const _U{ Universe::Get(L_) }; 143 Universe* const _U{ Universe::Get(L_) };
diff --git a/tests/basic.lua b/tests/basic.lua
index 8522895..59b5940 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -15,7 +15,7 @@ local require_assert_result_1, require_assert_result_2 = require "assert" --
15print("require_assert_result:", require_assert_result_1, require_assert_result_2) 15print("require_assert_result:", require_assert_result_1, require_assert_result_2)
16 16
17local lanes_gen= assert(lanes.gen) 17local lanes_gen= assert(lanes.gen)
18local lanes_linda= assert(lanes.linda) 18local lanes_linda = assert(lanes.linda)
19 19
20local tostring= assert(tostring) 20local tostring= assert(tostring)
21 21
@@ -160,7 +160,7 @@ PRINT(" "..st)
160assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'") 160assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'")
161 161
162-- cancellation of lanes waiting on a linda 162-- cancellation of lanes waiting on a linda
163local limited = lanes.linda("limited") 163local limited = lanes_linda("limited")
164assert.fails(function() limited:limit("key", -1) end) 164assert.fails(function() limited:limit("key", -1) end)
165assert.failsnot(function() limited:limit("key", 1) end) 165assert.failsnot(function() limited:limit("key", 1) end)
166-- [[################################################ 166-- [[################################################
@@ -422,6 +422,7 @@ local function chunk2(linda)
422 -- 422 --
423 local info= debug.getinfo(1) -- 1 = us 423 local info= debug.getinfo(1) -- 1 = us
424 -- 424 --
425 PRINT("linda named-> '" ..tostring(linda).."'")
425 PRINT "debug.getinfo->" 426 PRINT "debug.getinfo->"
426 for k,v in pairs(info) do PRINT(k,v) end 427 for k,v in pairs(info) do PRINT(k,v) end
427 428
@@ -448,7 +449,7 @@ local function chunk2(linda)
448 linda:send("up", function() return ":)" end, "ok2") 449 linda:send("up", function() return ":)" end, "ok2")
449end 450end
450 451
451local linda= lanes.linda("linda") 452local linda = lanes_linda("auto")
452local t2= lanes_gen("debug,string,io", {gc_cb = gc_cb}, chunk2)(linda) -- prepare & launch 453local t2= lanes_gen("debug,string,io", {gc_cb = gc_cb}, chunk2)(linda) -- prepare & launch
453linda:send("down", function(linda) linda:send("up", "ready!") end, 454linda:send("down", function(linda) linda:send("up", "ready!") end,
454 "ok") 455 "ok")