-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathglarrays.go
168 lines (160 loc) · 5.11 KB
/
glarrays.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package noodle
import (
"fmt"
"reflect"
"runtime"
"syscall/js"
"unsafe"
)
// Shamelessly stolen from https://github.com/bobcob7/wasm-rotating-cube/blob/master/gltypes/arrays.go
// Shamelessly stolen from: https://github.com/golang/go/issues/32402
// Thanks hajimehoshi
func sliceToByteSlice(s interface{}) []byte {
switch s := s.(type) {
case []int8:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
return *(*[]byte)(unsafe.Pointer(h))
case []int16:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 2
h.Cap *= 2
return *(*[]byte)(unsafe.Pointer(h))
case []int32:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4
h.Cap *= 4
return *(*[]byte)(unsafe.Pointer(h))
case []int64:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 8
h.Cap *= 8
return *(*[]byte)(unsafe.Pointer(h))
case []uint8:
return s
case []uint16:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 2
h.Cap *= 2
return *(*[]byte)(unsafe.Pointer(h))
case []uint32:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4
h.Cap *= 4
return *(*[]byte)(unsafe.Pointer(h))
case []uint64:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 8
h.Cap *= 8
return *(*[]byte)(unsafe.Pointer(h))
case []float32:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4
h.Cap *= 4
return *(*[]byte)(unsafe.Pointer(h))
case []float64:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 8
h.Cap *= 8
return *(*[]byte)(unsafe.Pointer(h))
case []Vector2:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4 * 2
h.Cap *= 4 * 2
return *(*[]byte)(unsafe.Pointer(h))
case []Vector3:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4 * 3
h.Cap *= 4 * 3
return *(*[]byte)(unsafe.Pointer(h))
case []Vector4:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4 * 4
h.Cap *= 4 * 4
return *(*[]byte)(unsafe.Pointer(h))
case []Matrix:
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
h.Len *= 4 * 16
h.Cap *= 4 * 16
return *(*[]byte)(unsafe.Pointer(h))
default:
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
}
}
//sliceToTypedArray converts a slice of values (a buffer) into a JavaScript equivilent typed array
func sliceToTypedArray(s interface{}) js.Value {
switch s := s.(type) {
case js.Value:
return s
//Bytes
case []int8:
a := js.Global().Get("Uint8Array").New(len(s))
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength"))
//Ints
case []int16:
a := js.Global().Get("Uint8Array").New(len(s) * 2)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2)
case []int32:
a := js.Global().Get("Uint8Array").New(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
case []uint8:
a := js.Global().Get("Uint8Array").New(len(s))
js.CopyBytesToJS(a, s)
runtime.KeepAlive(s)
return a
case []uint16:
a := js.Global().Get("Uint8Array").New(len(s) * 2)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2)
case []uint32:
a := js.Global().Get("Uint8Array").New(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
//Floats
case []float32:
a := js.Global().Get("Uint8Array").New(len(s) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
case []float64:
a := js.Global().Get("Uint8Array").New(len(s) * 8)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8)
//Vectors
case []Vector2:
a := js.Global().Get("Uint8Array").New((len(s) * 2) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
case []Vector3:
a := js.Global().Get("Uint8Array").New((len(s) * 3) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
case []Vector4:
a := js.Global().Get("Uint8Array").New((len(s) * 4) * 4)
js.CopyBytesToJS(a, sliceToByteSlice(s))
runtime.KeepAlive(s)
buf := a.Get("buffer")
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
default:
panic(fmt.Sprintf("jsutil: unexpected value at SliceToTypedArray: %T", s))
}
}