Navigation Menu

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

wasm: No support for converting JS arrays into Go equivalent (inverse of TypedArrayOf()) #31335

Closed
gabbifish opened this issue Apr 8, 2019 · 2 comments
Labels
arch-wasm WebAssembly issues FeatureRequest FrozenDueToAge NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone

Comments

@gabbifish
Copy link

gabbifish commented Apr 8, 2019

What version of Go are you using (go version)?

$ go version 1.12

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOOS="darwin"

What did you do?

This isn't a bug, but rather a WASM syscall/js feature suggestion. While syscall/js offers extremely helpful functionality for converting Go slices into JS arrays (specifically, func TypedArrayOf()), support for the inverse doesn't exist. This means that, to read a JS array passed into a Go function as a js.Value, one has the following options:

  1. Read elements from the JS array element by element, converting each value into the appropriate type before inserting it into a Go slice. (This is currently what is done in the example linked below). This works, but is clumsy
  2. Use the unsafe package to directly access the ref field in js.Value and use the js.Value's length to directly access the bytes starting at ref. Not ideal--requires using reflect to access ref, which is a private field.

As a result, passing JS arrays into Go functions in WASM requires extra lift and could benefit from an efficient JS array-to-Go slice conversion.

Check out the example code in
https://github.com/gabbifish/go-wasm-example/blob/master/main.go#L41-L53

What did you expect to see?

Support for converting JS arrays into Go arrays, given that the inverse logic already exists.

What did you see instead?

^ A hope that this functionality can be offered in Go WASM support in the future :) If there is interest, I'd be happy to work on a PR implementing functionality along these lines.

@agnivade
Copy link
Contributor

agnivade commented Apr 8, 2019

Yet another approach is to pass the length of the JS array to Go, and create a slice, then pass the slice header pointer to JS and set the wasm linear memory with the JS array at that offset. Takes 2 hops, but does not require reflect.

This is coming up often. I think we should have a standard method in syscall/js before the community comes up with different methods.

@bradfitz @neelance

@agnivade agnivade added arch-wasm WebAssembly issues NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one. labels Apr 8, 2019
@dmitshur dmitshur added this to the Unplanned milestone Apr 22, 2019
@agnivade
Copy link
Contributor

agnivade commented Jun 2, 2019

This is done with c468ad0. 🎉

@agnivade agnivade closed this as completed Jun 2, 2019
@golang golang locked and limited conversation to collaborators Jun 1, 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 NeedsInvestigation Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Projects
None yet
Development

No branches or pull requests

4 participants