This converges host-defined modules with Wasm defined modules by introducing a custom section for host-defined functions. The net result are far less types and consistent initialization. * HostModule is removed for Module * HostFunction is removed for Function * ModuleContext is removed for Module Note: One impact of this is that the low-level API no longer accepts a go context (context.Context), rather a `wasm.Module` which the function is called in context of. This meant exposing `wasm.Module.WithContext` to override the default. Signed-off-by: Adrian Cole <adrian@tetrate.io>
63 lines
2.0 KiB
Go
63 lines
2.0 KiB
Go
package examples
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/tetratelabs/wazero"
|
|
"github.com/tetratelabs/wazero/wasi"
|
|
"github.com/tetratelabs/wazero/wasm"
|
|
)
|
|
|
|
func Test_WASI(t *testing.T) {
|
|
// built-in WASI function to write a random value to memory
|
|
randomGet := func(ctx wasm.Module, buf, bufLen uint32) wasi.Errno {
|
|
panic("unimplemented")
|
|
}
|
|
|
|
stdout := new(bytes.Buffer)
|
|
goFunc := func(ctx wasm.Module) {
|
|
// Write 8 random bytes to memory using WASI.
|
|
errno := randomGet(ctx, 0, 8)
|
|
require.Equal(t, wasi.ErrnoSuccess, errno)
|
|
|
|
// Read them back and print it in hex!
|
|
random, ok := ctx.Memory().ReadUint64Le(0)
|
|
require.True(t, ok)
|
|
_, _ = fmt.Fprintf(stdout, "random: %x\n", random)
|
|
}
|
|
|
|
r := wazero.NewRuntime()
|
|
|
|
// Host functions can be exported as any module name, including the empty string.
|
|
env := &wazero.HostModuleConfig{Name: "", Functions: map[string]interface{}{"random": goFunc}}
|
|
_, err := r.NewHostModuleFromConfig(env)
|
|
|
|
// Configure WASI and implement the function to use it
|
|
we, err := r.NewHostModuleFromConfig(wazero.WASISnapshotPreview1())
|
|
require.NoError(t, err)
|
|
randomGetFn := we.ExportedFunction("random_get")
|
|
|
|
// Implement the function pointer. This mainly shows how you can decouple a host function dependency.
|
|
randomGet = func(ctx wasm.Module, buf, bufLen uint32) wasi.Errno {
|
|
res, err := randomGetFn.Call(ctx, uint64(buf), uint64(bufLen))
|
|
require.NoError(t, err)
|
|
return wasi.Errno(res[0])
|
|
}
|
|
|
|
// The "random" function was imported as $random in Wasm. Since it was marked as the start
|
|
// function, it is invoked on instantiation. Ensure that worked: "random" was called!
|
|
_, err = r.NewModuleFromSource([]byte(`(module $wasi
|
|
(import "wasi_snapshot_preview1" "random_get"
|
|
(func $wasi.random_get (param $buf i32) (param $buf_len i32) (result (;errno;) i32)))
|
|
(import "" "random" (func $random))
|
|
(memory 1)
|
|
(start $random)
|
|
)`))
|
|
require.NoError(t, err)
|
|
require.Contains(t, stdout.String(), "random: ")
|
|
}
|