github.com/megatontech/mynoteforgo@v0.0.0-20200507084910-5d0c6ea6e890/源码/syscall/js/typedarray.go (about) 1 // Copyright 2018 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // +build js,wasm 6 7 package js 8 9 import ( 10 "sync" 11 "unsafe" 12 ) 13 14 var ( 15 int8Array = Global().Get("Int8Array") 16 int16Array = Global().Get("Int16Array") 17 int32Array = Global().Get("Int32Array") 18 uint8Array = Global().Get("Uint8Array") 19 uint16Array = Global().Get("Uint16Array") 20 uint32Array = Global().Get("Uint32Array") 21 float32Array = Global().Get("Float32Array") 22 float64Array = Global().Get("Float64Array") 23 ) 24 25 var _ Wrapper = TypedArray{} // TypedArray must implement Wrapper 26 27 // TypedArray represents a JavaScript typed array. 28 type TypedArray struct { 29 Value 30 } 31 32 // Release frees up resources allocated for the typed array. 33 // The typed array and its buffer must not be accessed after calling Release. 34 func (a TypedArray) Release() { 35 openTypedArraysMutex.Lock() 36 delete(openTypedArrays, a) 37 openTypedArraysMutex.Unlock() 38 } 39 40 var ( 41 openTypedArraysMutex sync.Mutex 42 openTypedArrays = make(map[TypedArray]interface{}) 43 ) 44 45 // TypedArrayOf returns a JavaScript typed array backed by the slice's underlying array. 46 // 47 // The supported types are []int8, []int16, []int32, []uint8, []uint16, []uint32, []float32 and []float64. 48 // Passing an unsupported value causes a panic. 49 // 50 // TypedArray.Release must be called to free up resources when the typed array will not be used any more. 51 func TypedArrayOf(slice interface{}) TypedArray { 52 a := TypedArray{typedArrayOf(slice)} 53 openTypedArraysMutex.Lock() 54 openTypedArrays[a] = slice 55 openTypedArraysMutex.Unlock() 56 return a 57 } 58 59 func typedArrayOf(slice interface{}) Value { 60 switch slice := slice.(type) { 61 case []int8: 62 if len(slice) == 0 { 63 return int8Array.New(memory.Get("buffer"), 0, 0) 64 } 65 return int8Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 66 case []int16: 67 if len(slice) == 0 { 68 return int16Array.New(memory.Get("buffer"), 0, 0) 69 } 70 return int16Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 71 case []int32: 72 if len(slice) == 0 { 73 return int32Array.New(memory.Get("buffer"), 0, 0) 74 } 75 return int32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 76 case []uint8: 77 if len(slice) == 0 { 78 return uint8Array.New(memory.Get("buffer"), 0, 0) 79 } 80 return uint8Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 81 case []uint16: 82 if len(slice) == 0 { 83 return uint16Array.New(memory.Get("buffer"), 0, 0) 84 } 85 return uint16Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 86 case []uint32: 87 if len(slice) == 0 { 88 return uint32Array.New(memory.Get("buffer"), 0, 0) 89 } 90 return uint32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 91 case []float32: 92 if len(slice) == 0 { 93 return float32Array.New(memory.Get("buffer"), 0, 0) 94 } 95 return float32Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 96 case []float64: 97 if len(slice) == 0 { 98 return float64Array.New(memory.Get("buffer"), 0, 0) 99 } 100 return float64Array.New(memory.Get("buffer"), unsafe.Pointer(&slice[0]), len(slice)) 101 default: 102 panic("TypedArrayOf: not a supported slice") 103 } 104 }