aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2024-05-20 17:11:20 +0200
committerBenoit Germain <benoit.germain@ubisoft.com>2024-05-20 17:11:20 +0200
commiteb090da027012292e8856dc01856cde8880d3467 (patch)
tree7cd0480536ad52e76d9922a05f926ed4d7eb133f /src
parentac8caa415b36c875578c2ad0490965b80be2f37e (diff)
downloadlanes-eb090da027012292e8856dc01856cde8880d3467.tar.gz
lanes-eb090da027012292e8856dc01856cde8880d3467.tar.bz2
lanes-eb090da027012292e8856dc01856cde8880d3467.zip
Lane::debugName is a std::string_view
Diffstat (limited to 'src')
-rw-r--r--src/compat.h13
-rw-r--r--src/deep.cpp18
-rw-r--r--src/deep.h2
-rw-r--r--src/intercopycontext.cpp8
-rw-r--r--src/lane.cpp18
-rw-r--r--src/lane.h2
-rw-r--r--src/lanes.cpp20
-rw-r--r--src/tracker.cpp2
-rw-r--r--src/universe.cpp2
9 files changed, 47 insertions, 38 deletions
diff --git a/src/compat.h b/src/compat.h
index 3fb4e2d..b5afe17 100644
--- a/src/compat.h
+++ b/src/compat.h
@@ -254,9 +254,20 @@ LuaType luaG_getmodule(lua_State* L_, char const* name_);
254// ################################################################################################# 254// #################################################################################################
255 255
256// a replacement of lua_tolstring 256// a replacement of lua_tolstring
257inline std::string_view lua_tostringview(lua_State* L_, int idx_) 257[[nodiscard]] inline std::string_view lua_tostringview(lua_State* L_, int idx_)
258{ 258{
259 size_t _len{ 0 }; 259 size_t _len{ 0 };
260 char const* _str{ lua_tolstring(L_, idx_, &_len) }; 260 char const* _str{ lua_tolstring(L_, idx_, &_len) };
261 return std::string_view{ _str, _len }; 261 return std::string_view{ _str, _len };
262} 262}
263
264[[nodiscard]] inline std::string_view lua_pushstringview(lua_State* L_, std::string_view const& str_)
265{
266#if LUA_VERSION_NUM == 501
267 // lua_pushlstring doesn't return a value in Lua 5.1
268 lua_pushlstring(L_, str_.data(), str_.size());
269 return lua_tostringview(L_, -1);
270#else
271 return std::string_view{ lua_pushlstring(L_, str_.data(), str_.size()), str_.size() };
272#endif // LUA_VERSION_NUM > 501
273} \ No newline at end of file
diff --git a/src/deep.cpp b/src/deep.cpp
index 1685ebb..51c9250 100644
--- a/src/deep.cpp
+++ b/src/deep.cpp
@@ -184,7 +184,7 @@ void DeepFactory::DeleteDeepObject(lua_State* L_, DeepPrelude* o_)
184 * used in this Lua state (metatable, registring it). Otherwise, increments the 184 * used in this Lua state (metatable, registring it). Otherwise, increments the
185 * reference count. 185 * reference count.
186 */ 186 */
187char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_) 187std::string_view DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int nuv_, LookupMode mode_)
188{ 188{
189 // Check if a proxy already exists 189 // Check if a proxy already exists
190 kDeepProxyCacheRegKey.getSubTableMode(L_, "v"); // L_: DPC 190 kDeepProxyCacheRegKey.getSubTableMode(L_, "v"); // L_: DPC
@@ -192,7 +192,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int
192 lua_rawget(L_, -2); // L_: DPC proxy 192 lua_rawget(L_, -2); // L_: DPC proxy
193 if (!lua_isnil(L_, -1)) { 193 if (!lua_isnil(L_, -1)) {
194 lua_remove(L_, -2); // L_: proxy 194 lua_remove(L_, -2); // L_: proxy
195 return nullptr; 195 return std::string_view{};
196 } else { 196 } else {
197 lua_pop(L_, 1); // L_: DPC 197 lua_pop(L_, 1); // L_: DPC
198 } 198 }
@@ -264,18 +264,18 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int
264 lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", _modname.data()); 264 lua_pushfstring(L_, "error while requiring '%s' identified by DeepFactory::moduleName: ", _modname.data());
265 lua_insert(L_, -2); // L_: DPC proxy metatable prefix error 265 lua_insert(L_, -2); // L_: DPC proxy metatable prefix error
266 lua_concat(L_, 2); // L_: DPC proxy metatable error 266 lua_concat(L_, 2); // L_: DPC proxy metatable error
267 return lua_tostring(L_, -1); 267 return lua_tostringview(L_, -1);
268 } 268 }
269 } else { // already loaded, we are happy 269 } else { // already loaded, we are happy
270 lua_pop(L_, 4); // L_: DPC proxy metatable 270 lua_pop(L_, 4); // L_: DPC proxy metatable
271 } 271 }
272 } else { // no L.registry._LOADED; can this ever happen? 272 } else { // no L.registry._LOADED; can this ever happen?
273 lua_pop(L_, 6); // L_: 273 lua_pop(L_, 6); // L_:
274 return "unexpected error while requiring a module identified by DeepFactory::moduleName"; 274 return std::string_view{ "unexpected error while requiring a module identified by DeepFactory::moduleName" };
275 } 275 }
276 } else { // a module name, but no require() function :-( 276 } else { // a module name, but no require() function :-(
277 lua_pop(L_, 4); // L_: 277 lua_pop(L_, 4); // L_:
278 return "lanes receiving deep userdata should register the 'package' library"; 278 return std::string_view{ "lanes receiving deep userdata should register the 'package' library" };
279 } 279 }
280 } 280 }
281 } 281 }
@@ -291,7 +291,7 @@ char const* DeepFactory::PushDeepProxy(DestState L_, DeepPrelude* prelude_, int
291 lua_remove(L_, -2); // L_: proxy 291 lua_remove(L_, -2); // L_: proxy
292 LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA); 292 LUA_ASSERT(L_, lua_type_as_enum(L_, -1) == LuaType::USERDATA);
293 STACK_CHECK(L_, 0); 293 STACK_CHECK(L_, 0);
294 return nullptr; 294 return std::string_view{};
295} 295}
296 296
297// ################################################################################################# 297// #################################################################################################
@@ -336,9 +336,9 @@ int DeepFactory::pushDeepUserdata(DestState L_, int nuv_) const
336 raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack"); 336 raise_luaL_error(L_, "Bad DeepFactory::newDeepObjectInternal overload: should not push anything on the stack");
337 } 337 }
338 338
339 char const* const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy 339 std::string_view const _err{ DeepFactory::PushDeepProxy(L_, _prelude, nuv_, LookupMode::LaneBody) }; // proxy
340 if (_err != nullptr) { 340 if (!_err.empty()) {
341 raise_luaL_error(L_, _err); 341 raise_luaL_error(L_, _err.data());
342 } 342 }
343 STACK_CHECK(L_, 1); 343 STACK_CHECK(L_, 1);
344 return 1; 344 return 1;
diff --git a/src/deep.h b/src/deep.h
index 96461d6..dc32251 100644
--- a/src/deep.h
+++ b/src/deep.h
@@ -75,7 +75,7 @@ class DeepFactory
75 [[nodiscard]] int pushDeepUserdata(DestState L_, int nuv_) const; 75 [[nodiscard]] int pushDeepUserdata(DestState L_, int nuv_) const;
76 [[nodiscard]] DeepPrelude* toDeep(lua_State* L_, int index_) const; 76 [[nodiscard]] DeepPrelude* toDeep(lua_State* L_, int index_) const;
77 static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_); 77 static void DeleteDeepObject(lua_State* L_, DeepPrelude* o_);
78 [[nodiscard]] static char const* PushDeepProxy(DestState L_, DeepPrelude* o_, int nuv_, LookupMode mode_); 78 [[nodiscard]] static std::string_view PushDeepProxy(DestState L_, DeepPrelude* o_, int nuv_, LookupMode mode_);
79}; 79};
80 80
81// ################################################################################################# 81// #################################################################################################
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp
index 1487afd..08709e5 100644
--- a/src/intercopycontext.cpp
+++ b/src/intercopycontext.cpp
@@ -751,9 +751,9 @@ void InterCopyContext::inter_copy_keyvaluepair() const
751 STACK_CHECK(L1, _nuv); 751 STACK_CHECK(L1, _nuv);
752 752
753 DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) }; 753 DeepPrelude* const u{ *lua_tofulluserdata<DeepPrelude*>(L1, L1_i) };
754 char const* errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u 754 std::string_view const errmsg{ DeepFactory::PushDeepProxy(L2, u, _nuv, mode) }; // L1: ... u [uv]* L2: u
755 if (errmsg != nullptr) { 755 if (!errmsg.empty()) {
756 raise_luaL_error(getErrL(), errmsg); 756 raise_luaL_error(getErrL(), errmsg.data());
757 } 757 }
758 758
759 // transfer all uservalues of the source in the destination 759 // transfer all uservalues of the source in the destination
@@ -1142,7 +1142,7 @@ static char const* vt_names[] = {
1142// transfers stuff from L1->_G["package"] to L2->_G["package"] 1142// transfers stuff from L1->_G["package"] to L2->_G["package"]
1143// returns InterCopyResult::Success if everything is fine 1143// returns InterCopyResult::Success if everything is fine
1144// returns InterCopyResult::Error if pushed an error message in L1 1144// returns InterCopyResult::Error if pushed an error message in L1
1145// else raise an error in L1 1145// else raise an error in whichever state is not a keeper
1146[[nodiscard]] InterCopyResult InterCopyContext::inter_copy_package() const 1146[[nodiscard]] InterCopyResult InterCopyContext::inter_copy_package() const
1147{ 1147{
1148 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy_package()\n" INDENT_END(U))); 1148 DEBUGSPEW_CODE(fprintf(stderr, INDENT_BEGIN "InterCopyContext::inter_copy_package()\n" INDENT_END(U)));
diff --git a/src/lane.cpp b/src/lane.cpp
index 3017806..e838b7d 100644
--- a/src/lane.cpp
+++ b/src/lane.cpp
@@ -46,7 +46,7 @@ static LUAG_FUNC(get_debug_threadname)
46{ 46{
47 Lane* const _lane{ ToLane(L_, 1) }; 47 Lane* const _lane{ ToLane(L_, 1) };
48 luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments"); 48 luaL_argcheck(L_, lua_gettop(L_) == 1, 2, "too many arguments");
49 lua_pushstring(L_, _lane->debugName); 49 std::ignore = lua_pushstringview(L_, _lane->debugName);
50 return 1; 50 return 1;
51} 51}
52 52
@@ -278,7 +278,7 @@ static int thread_index_number(lua_State* L_)
278 lua_replace(L_, -3); // L_: lane n error() "error" 278 lua_replace(L_, -3); // L_: lane n error() "error"
279 lua_pushinteger(L_, 3); // L_: lane n error() "error" 3 279 lua_pushinteger(L_, 3); // L_: lane n error() "error" 3
280 lua_call(L_, 2, 0); // error(tostring(errstring), 3) -> doesn't return // L_: lane n 280 lua_call(L_, 2, 0); // error(tostring(errstring), 3) -> doesn't return // L_: lane n
281 raise_luaL_error(L_, "%s: should not get here!", _lane->debugName); 281 raise_luaL_error(L_, "%s: should not get here!", _lane->debugName.data());
282 } else { 282 } else {
283 lua_pop(L_, 1); // L_: lane n {uv} 283 lua_pop(L_, 1); // L_: lane n {uv}
284 } 284 }
@@ -345,7 +345,7 @@ static LUAG_FUNC(thread_index)
345 lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k 345 lua_pushvalue(L_, kKey); // L_: mt error "Unknown key: " k
346 lua_concat(L_, 2); // L_: mt error "Unknown key: <k>" 346 lua_concat(L_, 2); // L_: mt error "Unknown key: <k>"
347 lua_call(L_, 1, 0); // error( "Unknown key: " .. key) -> doesn't return // L_: mt 347 lua_call(L_, 1, 0); // error( "Unknown key: " .. key) -> doesn't return // L_: mt
348 raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName, lua_typename(L_, lua_type(L_, kKey))); 348 raise_luaL_error(L_, "%s[%s]: should not get here!", _lane->debugName.data(), lua_typename(L_, lua_type(L_, kKey)));
349 } 349 }
350} 350}
351 351
@@ -739,7 +739,7 @@ static void lane_main(Lane* lane_)
739 lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil 739 lua_rawget(L_, -2); // L_: ud uservalue gc_cb|nil
740 if (!lua_isnil(L_, -1)) { 740 if (!lua_isnil(L_, -1)) {
741 lua_remove(L_, -2); // L_: ud gc_cb|nil 741 lua_remove(L_, -2); // L_: ud gc_cb|nil
742 lua_pushstring(L_, _lane->debugName); // L_: ud gc_cb name 742 std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: ud gc_cb name
743 _have_gc_cb = true; 743 _have_gc_cb = true;
744 } else { 744 } else {
745 lua_pop(L_, 2); // L_: ud 745 lua_pop(L_, 2); // L_: ud
@@ -759,7 +759,7 @@ static void lane_main(Lane* lane_)
759 // no longer accessing the Lua VM: we can close right now 759 // no longer accessing the Lua VM: we can close right now
760 _lane->close(); 760 _lane->close();
761 // just in case, but _lane will be freed soon so... 761 // just in case, but _lane will be freed soon so...
762 _lane->debugName = "<gc>"; 762 _lane->debugName = std::string_view{ "<gc>" };
763 } 763 }
764 764
765 // Clean up after a (finished) thread 765 // Clean up after a (finished) thread
@@ -812,12 +812,12 @@ void Lane::changeDebugName(int nameIdx_)
812 // 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... 812 // 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...
813 kRegKey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); }); // L: ... "name" ... 813 kRegKey.setValue(L, [nameIdx = nameIdx_](lua_State* L_) { lua_pushvalue(L_, nameIdx); }); // L: ... "name" ...
814 // keep a direct pointer on the string 814 // keep a direct pointer on the string
815 debugName = lua_tostring(L, nameIdx_); 815 debugName = lua_tostringview(L, nameIdx_);
816 // to see VM name in Decoda debugger Virtual Machine window 816 // to see VM name in Decoda debugger Virtual Machine window
817 lua_pushvalue(L, nameIdx_); // L: ... "name" ... "name" 817 lua_pushvalue(L, nameIdx_); // L: ... "name" ... "name"
818 lua_setglobal(L, "decoda_name"); // L: ... "name" ... 818 lua_setglobal(L, "decoda_name"); // L: ... "name" ...
819 // and finally set the OS thread name 819 // and finally set the OS thread name
820 THREAD_SETNAME(debugName); 820 THREAD_SETNAME(debugName.data());
821 STACK_CHECK(L, 0); 821 STACK_CHECK(L, 0);
822} 822}
823 823
@@ -920,9 +920,7 @@ void Lane::securizeDebugName(lua_State* L_)
920 LUA_ASSERT(L_, lua_istable(L_, -1)); 920 LUA_ASSERT(L_, lua_istable(L_, -1));
921 // we don't care about the actual key, so long as it's unique and can't collide with anything. 921 // we don't care about the actual key, so long as it's unique and can't collide with anything.
922 lua_newtable(L_); // L_: lane ... {uv} {} 922 lua_newtable(L_); // L_: lane ... {uv} {}
923 // Lua 5.1 can't do 'lane_->debugName = lua_pushstring(L_, lane_->debugName);' 923 debugName = lua_pushstringview(L_, debugName); // L_: lane ... {uv} {} name
924 lua_pushstring(L_, debugName); // L_: lane ... {uv} {} name
925 debugName = lua_tostring(L_, -1);
926 lua_rawset(L_, -3); // L_: lane ... {uv} 924 lua_rawset(L_, -3); // L_: lane ... {uv}
927 lua_pop(L_, 1); // L_: lane 925 lua_pop(L_, 1); // L_: lane
928 STACK_CHECK(L_, 0); 926 STACK_CHECK(L_, 0);
diff --git a/src/lane.h b/src/lane.h
index 03f795b..10045c8 100644
--- a/src/lane.h
+++ b/src/lane.h
@@ -81,7 +81,7 @@ class Lane
81 // M: sub-thread OS thread 81 // M: sub-thread OS thread
82 // S: not used 82 // S: not used
83 83
84 char const* debugName{ "<unnamed>" }; 84 std::string_view debugName{ "<unnamed>" };
85 85
86 Universe* const U; 86 Universe* const U;
87 lua_State* L; 87 lua_State* L;
diff --git a/src/lanes.cpp b/src/lanes.cpp
index 74e2507..22391d5 100644
--- a/src/lanes.cpp
+++ b/src/lanes.cpp
@@ -317,16 +317,16 @@ LUAG_FUNC(lane_new)
317 lua_State* _L2{ lane->L }; 317 lua_State* _L2{ lane->L };
318 STACK_CHECK_START_REL(_L2, 0); 318 STACK_CHECK_START_REL(_L2, 0);
319 int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx }; 319 int const _name_idx{ lua_isnoneornil(L, kNameIdx) ? 0 : kNameIdx };
320 char const* const debugName{ (_name_idx > 0) ? lua_tostring(L, _name_idx) : nullptr }; 320 std::string_view const _debugName{ (_name_idx > 0) ? lua_tostringview(L, _name_idx) : std::string_view{} };
321 if (debugName) 321 if (!_debugName.empty())
322 { 322 {
323 if (strcmp(debugName, "auto") != 0) { 323 if (_debugName != "auto") {
324 lua_pushstring(_L2, debugName); // L: ... lane L2: "<name>" 324 std::ignore = lua_pushstringview(_L2, _debugName); // L: ... lane L2: "<name>"
325 } else { 325 } else {
326 lua_Debug ar; 326 lua_Debug _ar;
327 lua_pushvalue(L, 1); // L: ... lane func 327 lua_pushvalue(L, 1); // L: ... lane func
328 lua_getinfo(L, ">S", &ar); // L: ... lane 328 lua_getinfo(L, ">S", &_ar); // L: ... lane
329 lua_pushfstring(_L2, "%s:%d", ar.short_src, ar.linedefined); // L: ... lane L2: "<name>" 329 lua_pushfstring(_L2, "%s:%d", _ar.short_src, _ar.linedefined); // L: ... lane L2: "<name>"
330 } 330 }
331 lane->changeDebugName(-1); 331 lane->changeDebugName(-1);
332 lua_pop(_L2, 1); // L: ... lane L2: 332 lua_pop(_L2, 1); // L: ... lane L2:
@@ -701,11 +701,11 @@ LUAG_FUNC(configure)
701 STACK_CHECK(L_, 2); 701 STACK_CHECK(L_, 2);
702 702
703 { 703 {
704 char const* _errmsg{ 704 std::string_view const _errmsg{
705 DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, 0, LookupMode::LaneBody) 705 DeepFactory::PushDeepProxy(DestState{ L_ }, _U->timerLinda, 0, LookupMode::LaneBody)
706 }; // L_: settings M timerLinda 706 }; // L_: settings M timerLinda
707 if (_errmsg != nullptr) { 707 if (!_errmsg.empty()) {
708 raise_luaL_error(L_, _errmsg); 708 raise_luaL_error(L_, _errmsg.data());
709 } 709 }
710 lua_setfield(L_, -2, "timer_gateway"); // L_: settings M 710 lua_setfield(L_, -2, "timer_gateway"); // L_: settings M
711 } 711 }
diff --git a/src/tracker.cpp b/src/tracker.cpp
index 76b814d..3040154 100644
--- a/src/tracker.cpp
+++ b/src/tracker.cpp
@@ -94,7 +94,7 @@ void LaneTracker::tracking_add(Lane* lane_)
94 while (_lane != TRACKING_END) { 94 while (_lane != TRACKING_END) {
95 // insert a { name='<name>', status='<status>' } tuple, so that several lanes with the same name can't clobber each other 95 // insert a { name='<name>', status='<status>' } tuple, so that several lanes with the same name can't clobber each other
96 lua_createtable(L_, 0, 2); // L_: {} {} 96 lua_createtable(L_, 0, 2); // L_: {} {}
97 lua_pushstring(L_, _lane->debugName); // L_: {} {} "name" 97 std::ignore = lua_pushstringview(L_, _lane->debugName); // L_: {} {} "name"
98 lua_setfield(L_, -2, "name"); // L_: {} {} 98 lua_setfield(L_, -2, "name"); // L_: {} {}
99 _lane->pushThreadStatus(L_); // L_: {} {} "status" 99 _lane->pushThreadStatus(L_); // L_: {} {} "status"
100 lua_setfield(L_, -2, "status"); // L_: {} {} 100 lua_setfield(L_, -2, "status"); // L_: {} {}
diff --git a/src/universe.cpp b/src/universe.cpp
index 52aa368..4f21306 100644
--- a/src/universe.cpp
+++ b/src/universe.cpp
@@ -357,7 +357,7 @@ void Universe::terminateFreeRunningLanes(lua_State* L_, lua_Duration shutdownTim
357 Lane* _lane{ selfdestructFirst }; 357 Lane* _lane{ selfdestructFirst };
358 if (_lane != SELFDESTRUCT_END) { 358 if (_lane != SELFDESTRUCT_END) {
359 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it) 359 // this causes a leak because we don't call U's destructor (which could be bad if the still running lanes are accessing it)
360 raise_luaL_error(L_, "Zombie thread %s refuses to die!", _lane->debugName); 360 raise_luaL_error(L_, "Zombie thread %s refuses to die!", _lane->debugName.data());
361 } 361 }
362 } 362 }
363} 363}