Skip to content

Commit

Permalink
optimize KeyNotFound error message generation in map method
Browse files Browse the repository at this point in the history
  • Loading branch information
sekiguchi-nagisa committed Apr 13, 2024
1 parent feb559b commit 5c8d8f4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -1853,10 +1853,12 @@ ARSH_METHOD array_next(RuntimeContext &ctx) {
ARSH_METHOD map_get(RuntimeContext &ctx) {
SUPPRESS_WARNING(map_get);
auto &obj = typeAs<OrderedMapObject>(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;
}
Expand Down Expand Up @@ -1932,10 +1934,12 @@ ARSH_METHOD map_swap(RuntimeContext &ctx) {
auto &obj = typeAs<OrderedMapObject>(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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/constant.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
18 changes: 18 additions & 0 deletions test/exec/cases/base/map3.ds
Original file line number Diff line number Diff line change
@@ -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: ')

0 comments on commit 5c8d8f4

Please sign in to comment.