22
33package wgpu
44
5- import (
6- "log/slog"
7- "syscall/js"
8- "unsafe"
9- )
10-
11- // BytesToJS converts the given bytes to a js Uint8ClampedArray
12- // by using the global wasm memory bytes. This avoids the
13- // copying present in [js.CopyBytesToJS].
14- func BytesToJS (b []byte ) js.Value {
15- ptr := uintptr (unsafe .Pointer (& b [0 ]))
16- // We directly pass the offset and length to the constructor to avoid calling subarray or slice,
17- // thereby improving performance and safety (this fixes a detached array buffer crash).
18- return js .Global ().Get ("Uint8ClampedArray" ).New (js .Global ().Get ("wasm" ).Get ("instance" ).Get ("exports" ).Get ("mem" ).Get ("buffer" ), ptr , len (b ))
19- }
20-
215// mapSlice can be used to transform one slice into another by providing a
226// function to do the mapping.
237func mapSlice [S , T any ](slice []S , fn func (S ) T ) []T {
@@ -31,37 +15,6 @@ func mapSlice[S, T any](slice []S, fn func(S) T) []T {
3115 return result
3216}
3317
34- // AwaitJS is a helper function equivalent to await in JS.
35- // It is copied from https://go-review.googlesource.com/c/go/+/150917/
36- func AwaitJS (promise js.Value ) (result js.Value , ok bool ) {
37- if promise .Type () != js .TypeObject || promise .Get ("then" ).Type () != js .TypeFunction {
38- return promise , true
39- }
40-
41- done := make (chan struct {})
42-
43- onResolve := js .FuncOf (func (this js.Value , args []js.Value ) any {
44- result = args [0 ]
45- ok = true
46- close (done )
47- return nil
48- })
49- defer onResolve .Release ()
50-
51- onReject := js .FuncOf (func (this js.Value , args []js.Value ) any {
52- result = args [0 ]
53- ok = false
54- slog .Error ("wgpu.AwaitJS: promise rejected" , "reason" , result )
55- close (done )
56- return nil
57- })
58- defer onReject .Release ()
59-
60- promise .Call ("then" , onResolve , onReject )
61- <- done
62- return
63- }
64-
6518// no-ops
6619func SetLogLevel (level LogLevel ) {}
6720func GetVersion () Version { return 0 }
0 commit comments