MCPcopy Create free account
hub / github.com/buke/quickjs-go / ToInt32Array

Method ToInt32Array

value.go:937–957  ·  view source on GitHub ↗

ToInt32Array converts the value to int32 slice if it's an Int32Array.

()

Source from the content-addressed store, hash-verified

935
936// ToInt32Array converts the value to int32 slice if it's an Int32Array.
937func (v *Value) ToInt32Array() ([]int32, error) {
938 if !v.IsInt32Array() {
939 return nil, errors.New("value is not an Int32Array")
940 }
941
942 buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
943 defer buffer.Free()
944
945 totalSize := uint(byteOffset + byteLength)
946 bytes, err := buffer.ToByteArray(totalSize)
947 if err != nil {
948 return nil, err
949 }
950
951 data := bytes[byteOffset : byteOffset+byteLength]
952 result := make([]int32, len(data)/4)
953 for i := 0; i < len(result); i++ {
954 result[i] = int32(binary.LittleEndian.Uint32(data[i*4:]))
955 }
956 return result, nil
957}
958
959// ToUint32Array converts the value to uint32 slice if it's a Uint32Array.
960func (v *Value) ToUint32Array() ([]uint32, error) {

Callers 4

TestContextTypedArraysFunction · 0.80
unmarshalSliceMethod · 0.80
TestValueTypedArraysFunction · 0.80

Calls 6

IsInt32ArrayMethod · 0.95
getTypedArrayInfoMethod · 0.95
NewMethod · 0.80
ToByteArrayMethod · 0.80
FreeMethod · 0.45
Uint32Method · 0.45

Tested by 3

TestContextTypedArraysFunction · 0.64
TestValueTypedArraysFunction · 0.64