(s interface{})
| 63 | } |
| 64 | |
| 65 | func SliceToTypedArray(s interface{}) (val js.Value, free func()) { |
| 66 | free = func() {} |
| 67 | switch s := s.(type) { |
| 68 | case []int8: |
| 69 | a := js.Global().Get("Uint8Array").New(len(s)) |
| 70 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 71 | runtime.KeepAlive(s) |
| 72 | buf := a.Get("buffer") |
| 73 | return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength")), free |
| 74 | case []int16: |
| 75 | a := js.Global().Get("Uint8Array").New(len(s) * 2) |
| 76 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 77 | runtime.KeepAlive(s) |
| 78 | buf := a.Get("buffer") |
| 79 | return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), free |
| 80 | case []int32: |
| 81 | a := js.Global().Get("Uint8Array").New(len(s) * 4) |
| 82 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 83 | runtime.KeepAlive(s) |
| 84 | buf := a.Get("buffer") |
| 85 | return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), free |
| 86 | case []uint8: |
| 87 | a := js.Global().Get("Uint8Array").New(len(s)) |
| 88 | js.CopyBytesToJS(a, s) |
| 89 | runtime.KeepAlive(s) |
| 90 | return a, free |
| 91 | case []uint16: |
| 92 | a := js.Global().Get("Uint8Array").New(len(s) * 2) |
| 93 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 94 | runtime.KeepAlive(s) |
| 95 | buf := a.Get("buffer") |
| 96 | return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2), free |
| 97 | case []uint32: |
| 98 | a := js.Global().Get("Uint8Array").New(len(s) * 4) |
| 99 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 100 | runtime.KeepAlive(s) |
| 101 | buf := a.Get("buffer") |
| 102 | return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), free |
| 103 | case []float32: |
| 104 | a := js.Global().Get("Uint8Array").New(len(s) * 4) |
| 105 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 106 | runtime.KeepAlive(s) |
| 107 | buf := a.Get("buffer") |
| 108 | return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4), free |
| 109 | case []float64: |
| 110 | a := js.Global().Get("Uint8Array").New(len(s) * 8) |
| 111 | js.CopyBytesToJS(a, sliceToByteSlice(s)) |
| 112 | runtime.KeepAlive(s) |
| 113 | buf := a.Get("buffer") |
| 114 | return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8), free |
| 115 | default: |
| 116 | panic("jsutil: unexpected value at SliceToTypedArray()") |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | func Equal(a, b js.Value) bool { |
| 121 | return a.Equal(b) |
no test coverage detected