diff --git a/builtin_bigint.go b/builtin_bigint.go index 65a4d828..5db70844 100644 --- a/builtin_bigint.go +++ b/builtin_bigint.go @@ -109,8 +109,18 @@ func (v valueBigInt) baseObject(rt *Runtime) *Object { return rt.getBigIntPrototype() } -func (v valueBigInt) hash(*maphash.Hash) uint64 { - return v.i.Uint64() +func (v valueBigInt) hash(hash *maphash.Hash) uint64 { + var sign byte + if v.i.Sign() < 0 { + sign = 0x01 + } else { + sign = 0x00 + } + _ = hash.WriteByte(sign) + _, _ = hash.Write(v.i.Bytes()) + h := hash.Sum64() + hash.Reset() + return h } func toBigInt(value Value) valueBigInt { diff --git a/map_test.go b/map_test.go index 7d41e767..71993f42 100644 --- a/map_test.go +++ b/map_test.go @@ -3,6 +3,7 @@ package goja import ( "hash/maphash" "math" + "math/big" "strconv" "testing" ) @@ -25,6 +26,8 @@ func TestMapHash(t *testing.T) { testMapHashVal(floatToValue(1.2345), floatToValue(1.2345), true, t) testMapHashVal(SymIterator, SymToStringTag, false, t) testMapHashVal(SymIterator, SymIterator, true, t) + testMapHashVal(valueBigInt{big.NewInt(1)}, valueBigInt{big.NewInt(-1)}, false, t) + testMapHashVal(valueBigInt{big.NewInt(1)}, valueBigInt{big.NewInt(1)}, true, t) // The following tests introduce indeterministic behaviour //testMapHashVal(asciiString("Test"), asciiString("Test1"), false, t)