From 5c8d8f4987570df02c64f00b4263206cc783c804 Mon Sep 17 00:00:00 2001 From: sekiguchi-nagisa Date: Sat, 13 Apr 2024 18:42:48 +0900 Subject: [PATCH] optimize KeyNotFound error message generation in map method --- src/builtin.h | 16 ++++++++++------ src/constant.h | 2 +- test/exec/cases/base/map3.ds | 18 ++++++++++++++++++ 3 files changed, 29 insertions(+), 7 deletions(-) create mode 100644 test/exec/cases/base/map3.ds diff --git a/src/builtin.h b/src/builtin.h index e7b46383c..6400348ec 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -1853,10 +1853,12 @@ ARSH_METHOD array_next(RuntimeContext &ctx) { ARSH_METHOD map_get(RuntimeContext &ctx) { SUPPRESS_WARNING(map_get); auto &obj = typeAs(LOCAL(0)); - auto retIndex = obj.lookup(LOCAL(1)); + auto &key = LOCAL(1); + auto retIndex = obj.lookup(key); if (retIndex == -1) { - std::string msg("not found key: "); - msg += LOCAL(1).toString(); + std::string msg = "not found key: "; + appendAsPrintable(key.hasStrRef() ? key.asStrRef() : key.toString(), SYS_LIMIT_ERROR_MSG_MAX, + msg); raiseError(ctx, TYPE::KeyNotFoundError, std::move(msg)); RET_ERROR; } @@ -1932,10 +1934,12 @@ ARSH_METHOD map_swap(RuntimeContext &ctx) { auto &obj = typeAs(LOCAL(0)); CHECK_ITER_INVALIDATION(obj); Value value = LOCAL(2); - auto retIndex = obj.lookup(LOCAL(1)); + auto &key = LOCAL(1); + auto retIndex = obj.lookup(key); if (retIndex == -1) { - std::string msg("not found key: "); - msg += LOCAL(1).toString(); + std::string msg = "not found key: "; + appendAsPrintable(key.hasStrRef() ? key.asStrRef() : key.toString(), SYS_LIMIT_ERROR_MSG_MAX, + msg); raiseError(ctx, TYPE::KeyNotFoundError, std::move(msg)); RET_ERROR; } diff --git a/src/constant.h b/src/constant.h index 2e514373c..2e1be1bf4 100644 --- a/src/constant.h +++ b/src/constant.h @@ -489,7 +489,7 @@ constexpr size_t SYS_LIMIT_FUNC_DEPTH = 32; constexpr size_t SYS_LIMIT_UPVAR_NUM = UINT8_MAX; constexpr size_t SYS_LIMIT_JOB_DESC_LEN = 96; constexpr size_t SYS_LIMIT_XTRACE_LINE_LEN = 128; -constexpr size_t SYS_LIMIT_ERROR_MSG_MAX = UINT16_MAX; // for interna error message +constexpr size_t SYS_LIMIT_ERROR_MSG_MAX = UINT16_MAX; // for internal error message constexpr size_t SYS_LIMIT_STRING_MAX = INT32_MAX; constexpr size_t SYS_LIMIT_ARRAY_MAX = INT32_MAX; constexpr size_t SYS_LIMIT_KEY_BINDING_MAX = UINT8_MAX; diff --git a/test/exec/cases/base/map3.ds b/test/exec/cases/base/map3.ds new file mode 100644 index 000000000..afaa66538 --- /dev/null +++ b/test/exec/cases/base/map3.ds @@ -0,0 +1,18 @@ +# too large key + +printf -v var "%*s" 2147483640 @ +let v = $reply.remove('var')! + +var ex = 34 as Any +try { + assert $reply[$v] == 'hey' +} catch e { $ex = $e; } +assert ($ex as KeyNotFoundError).message().startsWith('not found key: ') +assert ($ex as KeyNotFoundError).message().size() < $v.size() + + +$ex = 3425 +try { + $reply.swap($v, 'hey') +} catch e { $ex = $e; } +assert ($ex as KeyNotFoundError).message().startsWith('not found key: ') \ No newline at end of file