Files
wazero/examples/allocation/zig
Takeshi Yoneda 4242b5e211 Update spectest v2 to latest (#2145)
Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
2024-03-12 10:06:44 +09:00
..
2022-08-11 16:31:06 +08:00
2023-01-23 19:54:05 +09: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.

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

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.

Notes

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.