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

Method ToFloat32Array

value.go:983–1004  ·  view source on GitHub ↗

ToFloat32Array converts the value to float32 slice if it's a Float32Array.

()

Source from the content-addressed store, hash-verified

981
982// ToFloat32Array converts the value to float32 slice if it's a Float32Array.
983func (v *Value) ToFloat32Array() ([]float32, error) {
984 if !v.IsFloat32Array() {
985 return nil, errors.New("value is not a Float32Array")
986 }
987
988 buffer, byteOffset, byteLength, _ := v.getTypedArrayInfo()
989 defer buffer.Free()
990
991 totalSize := uint(byteOffset + byteLength)
992 bytes, err := buffer.ToByteArray(totalSize)
993 if err != nil {
994 return nil, err
995 }
996
997 data := bytes[byteOffset : byteOffset+byteLength]
998 result := make([]float32, len(data)/4)
999 for i := 0; i < len(result); i++ {
1000 bits := binary.LittleEndian.Uint32(data[i*4:])
1001 result[i] = math.Float32frombits(bits)
1002 }
1003 return result, nil
1004}
1005
1006// ToFloat64Array converts the value to float64 slice if it's a Float64Array.
1007func (v *Value) ToFloat64Array() ([]float64, error) {

Callers 3

TestContextTypedArraysFunction · 0.80
unmarshalSliceMethod · 0.80
TestValueTypedArraysFunction · 0.80

Calls 6

IsFloat32ArrayMethod · 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