Improve BigInt hashing (#38)

Fixes: https://github.com/quickjs-ng/quickjs/issues/35
This commit is contained in:
Ben Noordhuis 2023-11-10 21:01:09 +01:00 committed by GitHub
parent 8d62210e7d
commit 7b64da2325
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 1 deletions

View file

@ -43301,6 +43301,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key)
uint32_t h; uint32_t h;
double d; double d;
JSFloat64Union u; JSFloat64Union u;
bf_t *a;
switch(tag) { switch(tag) {
case JS_TAG_BOOL: case JS_TAG_BOOL:
@ -43316,6 +43317,10 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key)
case JS_TAG_INT: case JS_TAG_INT:
d = JS_VALUE_GET_INT(key) * 3163; d = JS_VALUE_GET_INT(key) * 3163;
goto hash_float64; goto hash_float64;
case JS_TAG_BIG_INT:
a = JS_GetBigInt(key);
h = hash_string8((void *)a->tab, a->len * sizeof(*a->tab), 0);
break;
case JS_TAG_FLOAT64: case JS_TAG_FLOAT64:
d = JS_VALUE_GET_FLOAT64(key); d = JS_VALUE_GET_FLOAT64(key);
/* normalize the NaN */ /* normalize the NaN */
@ -43326,7 +43331,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValueConst key)
h = (u.u32[0] ^ u.u32[1]) * 3163; h = (u.u32[0] ^ u.u32[1]) * 3163;
break; break;
default: default:
h = 0; /* XXX: bigint support */ h = 0;
break; break;
} }
h ^= tag; h ^= tag;

View file

@ -110,5 +110,30 @@ function test_bigint2()
assertThrows(SyntaxError, () => { BigInt(" 123 r") } ); assertThrows(SyntaxError, () => { BigInt(" 123 r") } );
} }
function test_bigint_map()
{
var m = new Map();
assert(m.size, 0);
for (let i = 0n; i < 1337n; i++) {
const r = m.set(i, i.toString());
assert(r, m);
}
assert(m.size, 1337);
for (let i = 0n; i < 1337n; i++) {
const r = m.get(i);
assert(r, i.toString());
}
assert(m.get(1337n), undefined);
assert(m.size, 1337);
for (let i = 0n; i < 1337n; i++)
assert(m.delete(i));
assert(!m.delete(1337n));
assert(m.size, 0);
}
test_bigint1(); test_bigint1();
test_bigint2(); test_bigint2();
test_bigint_map();