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

Method ToUint16Array

value.go:914–934  ·  view source on GitHub ↗

ToUint16Array converts the value to uint16 slice if it's a Uint16Array.

()

Source from the content-addressed store, hash-verified

912
913// ToUint16Array converts the value to uint16 slice if it's a Uint16Array.
914func (v *Value) ToUint16Array() ([]uint16, error) {
915 if !v.IsUint16Array() {
916 return nil, errors.New("value is not a Uint16Array")
917 }
918
919 buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
920 defer buffer.Free()
921
922 totalSize := uint(byteOffset + byteLength)
923 bytes, err := buffer.ToByteArray(totalSize)
924 if err != nil {
925 return nil, err
926 }
927
928 data := bytes[byteOffset : byteOffset+byteLength]
929 result := make([]uint16, len(data)/2)
930 for i := 0; i < len(result); i++ {
931 result[i] = binary.LittleEndian.Uint16(data[i*2:])
932 }
933 return result, nil
934}
935
936// ToInt32Array converts the value to int32 slice if it's an Int32Array.
937func (v *Value) ToInt32Array() ([]int32, error) {

Callers 3

TestContextTypedArraysFunction · 0.80
unmarshalSliceMethod · 0.80
TestValueTypedArraysFunction · 0.80

Calls 5

IsUint16ArrayMethod · 0.95
getTypedArrayInfoMethod · 0.95
NewMethod · 0.80
ToByteArrayMethod · 0.80
FreeMethod · 0.45

Tested by 2

TestContextTypedArraysFunction · 0.64
TestValueTypedArraysFunction · 0.64