Skip to content

Commit 39a20c2

Browse files
committed
Adjust library for new go lang 1.15+
* where TypedArrayOf was removed & replaced with other techniques * the conversion was inspired & copied from issue golang/go#32402
1 parent 3cc81fb commit 39a20c2

File tree

3 files changed

+168
-51
lines changed

3 files changed

+168
-51
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
module github.com/nuberu/webgl
2+
3+
go 1.15

js.go

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
package webgl
2+
3+
import (
4+
"fmt"
5+
"reflect"
6+
"runtime"
7+
"syscall/js"
8+
"unsafe"
9+
)
10+
11+
func sliceToByteSlice(s interface{}) []byte {
12+
switch s := s.(type) {
13+
case []int8:
14+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
15+
return *(*[]byte)(unsafe.Pointer(h))
16+
case []int16:
17+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
18+
h.Len *= 2
19+
h.Cap *= 2
20+
return *(*[]byte)(unsafe.Pointer(h))
21+
case []int32:
22+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
23+
h.Len *= 4
24+
h.Cap *= 4
25+
return *(*[]byte)(unsafe.Pointer(h))
26+
case []int64:
27+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
28+
h.Len *= 8
29+
h.Cap *= 8
30+
return *(*[]byte)(unsafe.Pointer(h))
31+
case []uint8:
32+
return s
33+
case []uint16:
34+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
35+
h.Len *= 2
36+
h.Cap *= 2
37+
return *(*[]byte)(unsafe.Pointer(h))
38+
case []uint32:
39+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
40+
h.Len *= 4
41+
h.Cap *= 4
42+
return *(*[]byte)(unsafe.Pointer(h))
43+
case []uint64:
44+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
45+
h.Len *= 8
46+
h.Cap *= 8
47+
return *(*[]byte)(unsafe.Pointer(h))
48+
case []float32:
49+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
50+
h.Len *= 4
51+
h.Cap *= 4
52+
return *(*[]byte)(unsafe.Pointer(h))
53+
case []float64:
54+
h := (*reflect.SliceHeader)(unsafe.Pointer(&s))
55+
h.Len *= 8
56+
h.Cap *= 8
57+
return *(*[]byte)(unsafe.Pointer(h))
58+
default:
59+
panic(fmt.Sprintf("jsutil: unexpected value at sliceToBytesSlice: %T", s))
60+
}
61+
}
62+
func TypedArrayOf(s interface{}) js.Value {
63+
switch s := s.(type) {
64+
case []int8:
65+
a := js.Global().Get("Uint8Array").New(len(s))
66+
js.CopyBytesToJS(a, sliceToByteSlice(s))
67+
runtime.KeepAlive(s)
68+
buf := a.Get("buffer")
69+
return js.Global().Get("Int8Array").New(buf, a.Get("byteOffset"), a.Get("byteLength"))
70+
case []int16:
71+
a := js.Global().Get("Uint8Array").New(len(s) * 2)
72+
js.CopyBytesToJS(a, sliceToByteSlice(s))
73+
runtime.KeepAlive(s)
74+
buf := a.Get("buffer")
75+
return js.Global().Get("Int16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2)
76+
case []int32:
77+
a := js.Global().Get("Uint8Array").New(len(s) * 4)
78+
js.CopyBytesToJS(a, sliceToByteSlice(s))
79+
runtime.KeepAlive(s)
80+
buf := a.Get("buffer")
81+
return js.Global().Get("Int32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
82+
case []uint8:
83+
a := js.Global().Get("Uint8Array").New(len(s))
84+
js.CopyBytesToJS(a, s)
85+
runtime.KeepAlive(s)
86+
return a
87+
case []uint16:
88+
a := js.Global().Get("Uint8Array").New(len(s) * 2)
89+
js.CopyBytesToJS(a, sliceToByteSlice(s))
90+
runtime.KeepAlive(s)
91+
buf := a.Get("buffer")
92+
return js.Global().Get("Uint16Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/2)
93+
case []uint32:
94+
a := js.Global().Get("Uint8Array").New(len(s) * 4)
95+
js.CopyBytesToJS(a, sliceToByteSlice(s))
96+
runtime.KeepAlive(s)
97+
buf := a.Get("buffer")
98+
return js.Global().Get("Uint32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
99+
case []float32:
100+
a := js.Global().Get("Uint8Array").New(len(s) * 4)
101+
js.CopyBytesToJS(a, sliceToByteSlice(s))
102+
runtime.KeepAlive(s)
103+
buf := a.Get("buffer")
104+
return js.Global().Get("Float32Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/4)
105+
case []float64:
106+
a := js.Global().Get("Uint8Array").New(len(s) * 8)
107+
js.CopyBytesToJS(a, sliceToByteSlice(s))
108+
runtime.KeepAlive(s)
109+
buf := a.Get("buffer")
110+
return js.Global().Get("Float64Array").New(buf, a.Get("byteOffset"), a.Get("byteLength").Int()/8)
111+
default:
112+
panic(fmt.Sprintf("jsutil: unexpected value at SliceToTypedArray: %T", s))
113+
}
114+
}

0 commit comments

Comments
 (0)