tinygo icon indicating copy to clipboard operation
tinygo copied to clipboard

how to pass js array to wasm ?

Open 826327700 opened this issue 3 years ago • 4 comments

who can give me a exmaple ! thanks so much!

826327700 avatar Jun 29 '22 09:06 826327700

I believe you must pass it as a string. So on the javascript side, you would do something like myWasmFunction(JSON.stringify(myArray))

And on the tinygo side, you would have to unmarshal it with something like tinyjson (because encoding/json doesn't work in tinygo)

evilnoxx avatar Jul 11 '22 15:07 evilnoxx

I believe you must pass it as a string. So on the javascript side, you would do something like myWasmFunction(JSON.stringify(myArray))

Can you provide a full example about how to pass a string to wasm module? I thought this should be accomplished by

  • manually allocating memory for the string inside wasm
  • copying the string to the allocated memory
  • passing to myWasmFunction address of the string start and string length. If we can do all the same with TinyGo in a different, simpler way, please, tell us how to do it.

vlkv avatar Jul 18 '22 21:07 vlkv

Good news. Thanks to @trivigy, there's an example for this.

Follow the directions here, except make slices instead of make main https://github.com/tinygo-org/tinygo/blob/release/src/examples/wasm/README.md

I verified this just now. https://github.com/tinygo-org/tinygo/blob/release/src/examples/wasm/slices/wasm.go

codefromthecrypt avatar Sep 07 '22 06:09 codefromthecrypt