Skip to content

Commit

Permalink
move CopyBytesToJS to util_js.go
Browse files Browse the repository at this point in the history
  • Loading branch information
kkoreilly committed Sep 6, 2024
1 parent 781a77c commit e35089e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
18 changes: 0 additions & 18 deletions wgpu/adapt_js.go

This file was deleted.

15 changes: 14 additions & 1 deletion wgpu/util_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,20 @@

package wgpu

import "syscall/js"
import (
"syscall/js"
"unsafe"
)

// BytesToJS converts the given bytes to a js Uint8ClampedArray
// by using the global wasm memory bytes. This avoids the
// copying present in [js.CopyBytesToJS].
func BytesToJS(b []byte) js.Value {
ptr := uintptr(unsafe.Pointer(&b[0]))
memoryBytes := js.Global().Get("Uint8ClampedArray").New(js.Global().Get("wasm").Get("instance").Get("exports").Get("mem").Get("buffer"))
// using subarray instead of slice gives a 5x performance improvement due to no copying
return memoryBytes.Call("subarray", ptr, ptr+uintptr(len(b)))
}

// mapSlice can be used to transform one slice into another by providing a
// function to do the mapping.
Expand Down

0 comments on commit e35089e

Please sign in to comment.