Files
wazero/examples/allocation/zig
Crypt Keeper 5bd521eb3c Moves host function imports into their own directory (#784)
Our root directory is getting crowded and it is also difficult to
organize the "host imports" concept due to this.

This moves common and language-specific imports into their own
directory. This will break go import signatures on the next release, but
is more sustainable overall.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-08-31 10:22:15 +08:00
..
2022-08-11 16:31:06 +08:00
2022-08-18 14:47:49 +09:00
2022-08-11 16:31:06 +08:00

Zig allocation example

This example shows how to pass strings in and out of a Wasm function defined in Zig, built with zig build.

Ex.

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

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

  • Uses @ptrToInt to change a Zig pointer to a numeric type
  • Uses [*]u8 as an argument to take a pointer and slices it to build back a string

The Zig code exports "malloc" and "free", which we use for that purpose.

Note: This example uses @panic() rather than unreachable to handle errors since unreachable emits a call to panic only in Debug and ReleaseSafe mode. In ReleaseFast and ReleaseSmall mode, it would lead into undefined behavior.