From c1f36574d689433421db3478e969d3251339e52e Mon Sep 17 00:00:00 2001 From: shiroyk Date: Thu, 22 Aug 2024 09:03:38 +0800 Subject: [PATCH] BigInt64Array/BigUint64Array now export []int64/[]uint64 --- builtin_typedarrays.go | 3 ++- typedarrays.go | 21 ++++++++------------- typedarrays_test.go | 9 ++++----- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/builtin_typedarrays.go b/builtin_typedarrays.go index 1da178c7..a05a507a 100644 --- a/builtin_typedarrays.go +++ b/builtin_typedarrays.go @@ -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() diff --git a/typedarrays.go b/typedarrays.go index cc589e15..f305b1c0 100644 --- a/typedarrays.go +++ b/typedarrays.go @@ -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 @@ -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 { @@ -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() diff --git a/typedarrays_test.go b/typedarrays_test.go index d361d841..92653d6a 100644 --- a/typedarrays_test.go +++ b/typedarrays_test.go @@ -2,7 +2,6 @@ package goja import ( "bytes" - "math/big" "testing" ) @@ -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 { @@ -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 {