Skip to content

Commit

Permalink
Fixed BigInt hash
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Aug 18, 2024
1 parent 6017f8f commit 76da319
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 additions & 2 deletions builtin_bigint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 3 additions & 0 deletions map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goja
import (
"hash/maphash"
"math"
"math/big"
"strconv"
"testing"
)
Expand All @@ -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)
Expand Down

0 comments on commit 76da319

Please sign in to comment.