Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syscall/js: no method to convert TypedArrays into slices #30210

Closed
superp00t opened this issue Feb 13, 2019 · 4 comments
Closed

syscall/js: no method to convert TypedArrays into slices #30210

superp00t opened this issue Feb 13, 2019 · 4 comments
Labels
arch-wasm WebAssembly issues FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.

Comments

@superp00t
Copy link

You can convert a Go slice into a TypedArray by using js.TypedArrayOf(...), but there is apparently no way of doing the reverse operation.

You can write a function to Index() every byte value in an ArrayBuffer and put them in a byte slice, but this is cumbersome and incurs unacceptable overhead. There should be a function to directly copy the bytes into the Go heap, such as what js.Value.String() does.

@katiehockman katiehockman changed the title syscall/js: No method to convert TypedArrays into Go slices syscall/js: no method to convert TypedArrays into Go slices Feb 14, 2019
@katiehockman katiehockman changed the title syscall/js: no method to convert TypedArrays into Go slices syscall/js: no method to convert TypedArrays into slices Feb 14, 2019
@katiehockman katiehockman added the NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. label Feb 14, 2019
@katiehockman
Copy link
Contributor

/cc @neelance

@neelance
Copy link
Member

WebAssembly is only able to access typed arrays which are backed by WebAssembly's memory. This does not hold for all typed arrays, so a mapping with direct access to the typed array is not possible. The best solution is to allocate a correctly sized byte slice in Go, pass it to JS with TypedArrayOf and then use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/set to quickly copy all bytes.

@superp00t
Copy link
Author

@neelance Thanks a lot, doing that solved my problem.

@bradfitz bradfitz added FeatureRequest arch-wasm WebAssembly issues labels Feb 14, 2019
@bradfitz
Copy link
Contributor

I added metadata to this CL before I read the final comment which makes it sounds like this bug is resolved. So, closing. Correct me if that's wrong.

@golang golang locked and limited conversation to collaborators Feb 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
arch-wasm WebAssembly issues FeatureRequest FrozenDueToAge NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made.
Projects
None yet
Development

No branches or pull requests

5 participants