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

Method ToUint32Array

value.go:960–980  ·  view source on GitHub ↗

ToUint32Array converts the value to uint32 slice if it's a Uint32Array.

()

Source from the content-addressed store, hash-verified

958
959// ToUint32Array converts the value to uint32 slice if it's a Uint32Array.
960func (v *Value) ToUint32Array() ([]uint32, error) {
961 if !v.IsUint32Array() {
962 return nil, errors.New("value is not a Uint32Array")
963 }
964
965 buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
966 defer buffer.Free()
967
968 totalSize := uint(byteOffset + byteLength)
969 bytes, err := buffer.ToByteArray(totalSize)
970 if err != nil {
971 return nil, err
972 }
973
974 data := bytes[byteOffset : byteOffset+byteLength]
975 result := make([]uint32, len(data)/4)
976 for i := 0; i < len(result); i++ {
977 result[i] = binary.LittleEndian.Uint32(data[i*4:])
978 }
979 return result, nil
980}
981
982// ToFloat32Array converts the value to float32 slice if it's a Float32Array.
983func (v *Value) ToFloat32Array() ([]float32, error) {

Callers 3

TestContextTypedArraysFunction · 0.80
unmarshalSliceMethod · 0.80
TestValueTypedArraysFunction · 0.80

Calls 6

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

Tested by 2

TestContextTypedArraysFunction · 0.64
TestValueTypedArraysFunction · 0.64