Files
wazero/examples/allocation/tinygo
Crypt Keeper 8c2f0928bc Panics caller on exit error (#673)
This changes the AssemblyScript abort handler and WASI proc_exit
implementation to panic the caller which eventually invoked close.

This ensures no code executes afterwards, For example, LLVM inserts
unreachable instructions after calls to exit.

See https://github.com/emscripten-core/emscripten/issues/12322
See #601

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-07-06 16:30:31 +08:00
..

TinyGo allocation example

This example shows how to pass strings in and out of a Wasm function defined in TinyGo, built with tinygo build -o greet.wasm -scheduler=none -target=wasi greet.go

Ex.

$ go run greet.go wazero
wasm >> Hello, wazero!
go >> Hello, wazero!

Under the covers, greet.go does a few things of interest:

  • Uses unsafe.Pointer to change a Go pointer to a numeric type.
  • Uses reflect.StringHeader to build back a string from a pointer, len pair.
  • Relies on TinyGo not eagerly freeing pointers returned.

Go does not export allocation functions, but when TinyGo generates WebAssembly, it exports "malloc" and "free", which we use for that purpose. These are not documented, so not necessarily a best practice. See the following issues for updates:

Note: While folks here are familiar with TinyGo, wazero isn't a TinyGo project. We hope this gets you started. For next steps, consider reading the TinyGo Using WebAssembly Guide or joining the #TinyGo channel on the Gophers Slack.