From 47e07b25aa3866e2cb2248586d0cb255de47be14 Mon Sep 17 00:00:00 2001 From: Charlie Gordon Date: Fri, 23 Feb 2024 11:57:43 +0100 Subject: [PATCH] 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 --- quickjs.c | 4 ++-- tests/test_builtin.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/quickjs.c b/quickjs.c index 573c3c5..f0372eb 100644 --- a/quickjs.c +++ b/quickjs.c @@ -44377,7 +44377,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValue 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_BIG_INT: a = JS_GetBigInt(key); @@ -44391,7 +44391,7 @@ static uint32_t map_hash_key(JSContext *ctx, JSValue key) hash_float64: u.d = d; h = (u.u32[0] ^ u.u32[1]) * 3163; - break; + return h ^= JS_TAG_FLOAT64; default: h = 0; break; diff --git a/tests/test_builtin.js b/tests/test_builtin.js index 257bfe4..93e2490 100644 --- a/tests/test_builtin.js +++ b/tests/test_builtin.js @@ -737,6 +737,16 @@ 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 = new Map(); tab = []; for(i = 0; i < n; i++) {