Skip to content

Commit

Permalink
Fix Map hash bug
Browse files Browse the repository at this point in the history
- `map_hash_key` must generate the same key for JS_INT and JS_FLOAT64
   with the same value
- add test cases in tests/test_builtin.js
  • Loading branch information
chqrlie committed Feb 22, 2024
1 parent b70e764 commit 27928ce
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
4 changes: 2 additions & 2 deletions quickjs.c
Original file line number Diff line number Diff line change
Expand Up @@ -46963,7 +46963,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key)
h = (uintptr_t)JS_VALUE_GET_PTR(key) * 3163;
break;
case JS_TAG_INT:
d = JS_VALUE_GET_INT(key) * 3163;
d = JS_VALUE_GET_INT(key);
goto hash_float64;
case JS_TAG_FLOAT64:
d = JS_VALUE_GET_FLOAT64(key);
Expand All @@ -46973,7 +46973,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key)
hash_float64:
u.d = d;
h = (u.u32[0] ^ u.u32[1]) * 3163;
break;
return h ^= JS_TAG_FLOAT64;
default:
h = 0; /* XXX: bignum support */
break;
Expand Down
14 changes: 14 additions & 0 deletions tests/test_builtin.js
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,20 @@ function test_map()
{
var a, i, n, tab, o, v;
n = 1000;

a = new Map();
for (var i = 0; i < n; i++) {
a.set(i, i);
}
a.set(-2147483648, 1);
assert(a.get(-2147483648), 1);
assert(a.get(-2147483647 - 1), 1);
assert(a.get(-2147483647.5 - 0.5), 1);

a.set(1n, 1n);
assert(a.get(1n), 1n);
assert(a.get(2n**1000n - (2n**1000n - 1n)), 1n);

a = new Map();
tab = [];
for(i = 0; i < n; i++) {
Expand Down

0 comments on commit 27928ce

Please sign in to comment.