Skip to content

Commit

Permalink
BigInt64Array/BigUint64Array now export []int64/[]uint64
Browse files Browse the repository at this point in the history
  • Loading branch information
shiroyk committed Aug 22, 2024
1 parent 56089c0 commit c1f3657
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
3 changes: 2 additions & 1 deletion builtin_typedarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -1226,7 +1226,8 @@ func (r *Runtime) typedArrayProto_with(call FunctionCall) Value {
}

var numericValue Value
if ta.typedArray.exportType() == typeBigInt64Array {
if ta.typedArray.exportType() == typeBigInt64Array ||
ta.typedArray.exportType() == typeBigUint64Array {
numericValue = toBigInt(call.Argument(1))
} else {
numericValue = call.Argument(1).ToNumber()
Expand Down
21 changes: 8 additions & 13 deletions typedarrays.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,14 +693,10 @@ func (a *bigInt64Array) export(offset int, length int) interface{} {
sliceHeader.Data = (*reflect.SliceHeader)(unsafe.Pointer(a)).Data + uintptr(offset)*8
sliceHeader.Len = length
sliceHeader.Cap = length
ret := make([]*big.Int, 0, length)
for _, v := range res {
ret = append(ret, big.NewInt(v))
}
return ret
return res
}

var typeBigInt64Array = reflect.TypeOf(([]*big.Int)(nil))
var typeBigInt64Array = reflect.TypeOf(([]int64)(nil))

func (a *bigInt64Array) exportType() reflect.Type {
return typeBigInt64Array
Expand Down Expand Up @@ -758,15 +754,13 @@ func (a *bigUint64Array) export(offset int, length int) interface{} {
sliceHeader.Data = (*reflect.SliceHeader)(unsafe.Pointer(a)).Data + uintptr(offset)*8
sliceHeader.Len = length
sliceHeader.Cap = length
ret := make([]*big.Int, 0, length)
for _, v := range res {
ret = append(ret, new(big.Int).SetUint64(v))
}
return ret
return res
}

var typeBigUint64Array = reflect.TypeOf(([]uint64)(nil))

func (a *bigUint64Array) exportType() reflect.Type {
return typeBigInt64Array
return typeBigUint64Array
}

func (a *typedArrayObject) _getIdx(idx int) Value {
Expand Down Expand Up @@ -837,7 +831,8 @@ func (a *typedArrayObject) isValidIntegerIndex(idx int) bool {
}

func (a *typedArrayObject) _putIdx(idx int, v Value) {
if a.typedArray.exportType() == typeBigInt64Array {
if a.typedArray.exportType() == typeBigInt64Array ||
a.typedArray.exportType() == typeBigUint64Array {
v = toBigInt(v)
} else {
v = v.ToNumber()
Expand Down
9 changes: 4 additions & 5 deletions typedarrays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goja

import (
"bytes"
"math/big"
"testing"
)

Expand Down Expand Up @@ -519,8 +518,8 @@ func TestTypedArrayExport(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if a, ok := v.Export().([]*big.Int); ok {
if len(a) != 2 || a[0].Cmp(big.NewInt(1)) != 0 || a[1].Cmp(big.NewInt(2)) != 0 {
if a, ok := v.Export().([]int64); ok {
if len(a) != 2 || a[0] != 1 || a[1] != 2 {
t.Fatal(a)
}
} else {
Expand All @@ -533,8 +532,8 @@ func TestTypedArrayExport(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if a, ok := v.Export().([]*big.Int); ok {
if len(a) != 2 || a[0].Cmp(big.NewInt(1)) != 0 || a[1].Cmp(big.NewInt(2)) != 0 {
if a, ok := v.Export().([]uint64); ok {
if len(a) != 2 || a[0] != 1 || a[1] != 2 {
t.Fatal(a)
}
} else {
Expand Down

0 comments on commit c1f3657

Please sign in to comment.