Go library to define type-safe host functions in wazero and other WebAssembly runtimes.
Features:
- 🛡 Type safe
- 🐎 Fast
- 🔨 Works with any WebAssmebly runtime, like wazero or wasman
- 🧠 Handles for you memory operations
- 👉 Manages external references
- 🧼 Simple and clean API
- 🐜 Can be compiled with TinyGo
- 😎 No reflect, no unsafe, only generics and dedication.
go get github.com/orsinium-labs/wypes
Define a function using provided types:
func addI32(a wypes.Int32, b wypes.Int32) wypes.Int32 {
return a + b
}
Define a mapping of module and function names to function definitions:
modules := wypes.Modules{
"env": {
"add_i32": wypes.H2(addI32),
},
}
Link the modules to the runtime. We provide a convenience method to do this for wazero:
err := modules.DefineWazero(r, nil)
That's it! Now the wasm module can call the env.add_i32
function.
The library provides lots of useful types that you can use in your functions. Make sure to check the docs. A few highlights:
- Context provides access to the context.Context passed into the guest function call in wazero.
- Store provides access to all the state: memory, stack, references.
- Duration and Time to pass time.Duration and time.Time (as UNIX timestamp).
- HostRef can hold a reference to the Refs store of host objects.
- Void is used as the return type for functions that return no value.
See documentation for more.