Skip to content

Commit 47e07b2

Browse files
authored
Fix Map hash bug (#281)
- `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
1 parent 2d1473e commit 47e07b2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

quickjs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44377,7 +44377,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValue key)
4437744377
h = (uintptr_t)JS_VALUE_GET_PTR(key) * 3163;
4437844378
break;
4437944379
case JS_TAG_INT:
44380-
d = JS_VALUE_GET_INT(key) * 3163;
44380+
d = JS_VALUE_GET_INT(key);
4438144381
goto hash_float64;
4438244382
case JS_TAG_BIG_INT:
4438344383
a = JS_GetBigInt(key);
@@ -44391,7 +44391,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValue key)
4439144391
hash_float64:
4439244392
u.d = d;
4439344393
h = (u.u32[0] ^ u.u32[1]) * 3163;
44394-
break;
44394+
return h ^= JS_TAG_FLOAT64;
4439544395
default:
4439644396
h = 0;
4439744397
break;

tests/test_builtin.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,16 @@ function test_map()
737737
{
738738
var a, i, n, tab, o, v;
739739
n = 1000;
740+
741+
a = new Map();
742+
for (var i = 0; i < n; i++) {
743+
a.set(i, i);
744+
}
745+
a.set(-2147483648, 1);
746+
assert(a.get(-2147483648), 1);
747+
assert(a.get(-2147483647 - 1), 1);
748+
assert(a.get(-2147483647.5 - 0.5), 1);
749+
740750
a = new Map();
741751
tab = [];
742752
for(i = 0; i < n; i++) {

0 commit comments

Comments
 (0)