Files
wazero/wasi/usage_test.go
Crypt Keeper 106f96b066 Adds import-go example (#466)
This shows how to define, export and import functions written in Go.

Fixes #464

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
2022-04-15 09:31:52 +08:00

43 lines
1.1 KiB
Go

package wasi
import (
"bytes"
_ "embed"
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/internal/testing/require"
)
// wasiArg was compiled from testdata/wasi_arg.wat
//go:embed testdata/wasi_arg.wasm
var wasiArg []byte
func TestInstantiateModuleWithConfig(t *testing.T) {
r := wazero.NewRuntime()
stdout := bytes.NewBuffer(nil)
// Configure WASI to write stdout to a buffer, so that we can verify it later.
sys := wazero.NewModuleConfig().WithStdout(stdout)
wm, err := InstantiateSnapshotPreview1(r)
require.NoError(t, err)
defer wm.Close()
compiled, err := r.CompileModule(wasiArg)
require.NoError(t, err)
defer compiled.Close()
// Re-use the same module many times.
for _, tc := range []string{"a", "b", "c"} {
mod, err := r.InstantiateModuleWithConfig(compiled, sys.WithArgs(tc).WithName(tc))
require.NoError(t, err)
// Ensure the scoped configuration applied. As the args are null-terminated, we append zero (NUL).
require.Equal(t, append([]byte(tc), 0), stdout.Bytes())
stdout.Reset()
require.NoError(t, mod.Close())
}
}