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 `CopyBytesToGo` does not work #39129

Closed
rajkumarGosavi opened this issue May 18, 2020 · 4 comments
Closed

Wasm `CopyBytesToGo` does not work #39129

rajkumarGosavi opened this issue May 18, 2020 · 4 comments

Comments

@rajkumarGosavi
Copy link

@rajkumarGosavi rajkumarGosavi commented May 18, 2020

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

$ go version
go version go1.13.7 linux/amd64

Does this issue reproduce with the latest release?

Do not know

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

go env Output
$ go env
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/home/username/.cache/go-build"
GOENV="/home/username/.config/go/env"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOINSECURE=""
GONOPROXY=""
GONOSUMDB=""
GOOS="linux"
GOPATH="/home/username/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/linux_amd64"
GCCGO="gccgo"
AR="ar"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build641848429=/tmp/go-build -gno-record-gcc-switches"

What did you do?

I had a file which I fetched using the JS Fetch API. I converted the contents of the file to Uint8Array.
And passed the Uint8Array as a parameter to the globally registered golang function in my wasm file.
Now as per the https://www.godoc.org/syscall/js#CopyBytesToGo documentation spcified here. If one gives a Uint8Array to CopyBytesToGo function then one must get the []byte

fmt.Println("params", params[0].Type())
buff := make([]byte, 0)
n := js.CopyBytesToGo(buff, params[0])
fmt.Println("count", n)

What did you expect to see?

I expected to see my target buffer or []byte to contain the bytes from the Uint8Array passed from JS. And the count to be non zero

What did you see instead?

I get the count to be 0 and the type to be object

@agnivade
Copy link
Contributor

@agnivade agnivade commented May 20, 2020

You need to create buff the same size as params[0]. If you create a zero length buffer, zero bytes will be copied.

Unlike other projects, we do not use the issue tracker for questions such as these. It is only used for bugs and feature proposals. I will close this issue, but please feel free to ask it in any of these forums below:

Thanks

@agnivade agnivade closed this May 20, 2020
@rajkumarGosavi
Copy link
Author

@rajkumarGosavi rajkumarGosavi commented May 20, 2020

@agnivade thank you for replying. I have successfully resolved it in my project. My point being one can specify this example or note in the docs. It will be very helpful for others who are getting started

@agnivade
Copy link
Contributor

@agnivade agnivade commented May 20, 2020

I have tried adding an example here, but it was rejected at the end because it turned out to be too complicated to be an example. If you have a better way to add an example, please feel free to send a CL.

@dustinbowers
Copy link

@dustinbowers dustinbowers commented Oct 25, 2020

@agnivade Thanks! This was exactly what I needed to pull a JS Uint8Array into a Go byte slice

For anyone in the future: the trick is to initialize the byte-slice with the JS-land length before doing the copying, like this:

array := args[0]
buf := make([]byte, array.Get("length").Int())
n := js.CopyBytesToGo(buf, array)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.