aboutsummaryrefslogtreecommitdiff
path: root/src/intercopycontext.cpp
diff options
context:
space:
mode:
authorBenoit Germain <benoit.germain@ubisoft.com>2025-10-27 08:43:22 +0100
committerBenoit Germain <benoit.germain@ubisoft.com>2025-10-27 08:43:22 +0100
commit5e5bcf37450d07f7f2812255bbd1df35d8e6ce75 (patch)
tree14cac4a19d4e35b60f0a56c948f45b158881daa5 /src/intercopycontext.cpp
parent3b4848ca1c4ea40d0e052cdc81bb9f66ce882a8a (diff)
downloadlanes-5e5bcf37450d07f7f2812255bbd1df35d8e6ce75.tar.gz
lanes-5e5bcf37450d07f7f2812255bbd1df35d8e6ce75.tar.bz2
lanes-5e5bcf37450d07f7f2812255bbd1df35d8e6ce75.zip
verbose_errors improvement
* Use std::format instead of sprintf for verbose errors when decoding table keys * Add a unit test for the different table key types
Diffstat (limited to 'src/intercopycontext.cpp')
-rw-r--r--src/intercopycontext.cpp23
1 files changed, 11 insertions, 12 deletions
diff --git a/src/intercopycontext.cpp b/src/intercopycontext.cpp
index 61ce08f..0ccd619 100644
--- a/src/intercopycontext.cpp
+++ b/src/intercopycontext.cpp
@@ -474,29 +474,28 @@ void InterCopyContext::interCopyKeyValuePair() const
474 // for debug purposes, let's try to build a useful name 474 // for debug purposes, let's try to build a useful name
475 if (luaW_type(L1, _key_i) == LuaType::STRING) { 475 if (luaW_type(L1, _key_i) == LuaType::STRING) {
476 std::string_view const _key{ luaW_tostring(L1, _key_i) }; 476 std::string_view const _key{ luaW_tostring(L1, _key_i) };
477 size_t const _bufLen{ name.size() + _key.size() + 2 }; // +2 for separator dot and terminating 0 477 _valPath = static_cast<char*>(alloca(name.size() + _key.size() + 2)); // +2 for separator dot and terminating 0
478 _valPath = static_cast<char*>(alloca(_bufLen)); 478 *std::format_to(_valPath, "{}.{}", name, _key) = 0;
479 sprintf(_valPath, "%s." STRINGVIEW_FMT, name.data(), (int) _key.size(), _key.data());
480 } 479 }
481#if defined LUA_LNUM || LUA_VERSION_NUM >= 503 480#if defined LUA_LNUM || LUA_VERSION_NUM >= 503
482 else if (lua_isinteger(L1, _key_i)) { 481 else if (lua_isinteger(L1, _key_i)) {
483 lua_Integer const key{ lua_tointeger(L1, _key_i) }; 482 lua_Integer const key{ lua_tointeger(L1, _key_i) };
484 _valPath = (char*) alloca(name.size() + 32 + 3); // +3 for [] and terminating 0 483 _valPath = static_cast<char*>(alloca(name.size() + 32 + 3)); // +3 for [] and terminating 0
485 sprintf(_valPath, "%s[" LUA_INTEGER_FMT "]", name.data(), key); 484 *std::format_to(_valPath, "{}[{}]", name, key) = 0;
486 } 485 }
487#endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503 486#endif // defined LUA_LNUM || LUA_VERSION_NUM >= 503
488 else if (luaW_type(L1, _key_i) == LuaType::NUMBER) { 487 else if (luaW_type(L1, _key_i) == LuaType::NUMBER) {
489 lua_Number const key{ lua_tonumber(L1, _key_i) }; 488 lua_Number const key{ lua_tonumber(L1, _key_i) };
490 _valPath = (char*) alloca(name.size() + 32 + 3); // +3 for [] and terminating 0 489 _valPath = static_cast<char*>(alloca(name.size() + 32 + 3)); // +3 for [] and terminating 0
491 sprintf(_valPath, "%s[" LUA_NUMBER_FMT "]", name.data(), key); 490 *std::format_to(_valPath, "{}[{}]", name, key) = 0;
492 } else if (luaW_type(L1, _key_i) == LuaType::LIGHTUSERDATA) { 491 } else if (luaW_type(L1, _key_i) == LuaType::LIGHTUSERDATA) {
493 void* const _key{ lua_touserdata(L1, _key_i) }; 492 void* const _key{ lua_touserdata(L1, _key_i) };
494 _valPath = (char*) alloca(name.size() + 16 + 5); // +5 for [U:] and terminating 0 493 _valPath = static_cast<char*>(alloca(name.size() + 16 + 5)); // +5 for [U:] and terminating 0
495 sprintf(_valPath, "%s[U:%p]", name.data(), _key); 494 *std::format_to(_valPath, "{}[U:{}]", name, _key) = 0;
496 } else if (luaW_type(L1, _key_i) == LuaType::BOOLEAN) { 495 } else if (luaW_type(L1, _key_i) == LuaType::BOOLEAN) {
497 int const _key{ lua_toboolean(L1, _key_i) }; 496 int const _key{ lua_toboolean(L1, _key_i) };
498 _valPath = (char*) alloca(name.size() + 8); // +8 for [], 'false' and terminating 0 497 _valPath = static_cast<char*>(alloca(name.size() + 8)); // +8 for [], 'false' and terminating 0
499 sprintf(_valPath, "%s[%s]", name.data(), _key ? "true" : "false"); 498 *std::format_to(_valPath, "{}[{}]", name, _key ? "true" : "false") = 0;
500 } 499 }
501 } 500 }
502 501
@@ -1430,7 +1429,7 @@ InterCopyResult InterCopyContext::interCopy(int const n_) const
1430 for (StackIndex _i{ (L1_i != 0) ? L1_i.value() : (_top_L1 - n_ + 1) }, _j{ 1 }; _j <= n_; ++_i, ++_j) { 1429 for (StackIndex _i{ (L1_i != 0) ? L1_i.value() : (_top_L1 - n_ + 1) }, _j{ 1 }; _j <= n_; ++_i, ++_j) {
1431 char _tmpBuf[16]; 1430 char _tmpBuf[16];
1432 if (U->verboseErrors) { 1431 if (U->verboseErrors) {
1433 sprintf(_tmpBuf, "arg_%d", _j.value()); 1432 *std::format_to(_tmpBuf, "arg#{}", _j.value()) = 0;
1434 _c.name = _tmpBuf; 1433 _c.name = _tmpBuf;
1435 } 1434 }
1436 _c.L1_i = SourceIndex{ _i.value() }; 1435 _c.L1_i = SourceIndex{ _i.value() };