Files
wazero/examples/stdio_test.go
Adrian Cole 76c0bfc33f Combines Store+Engine into Runtime
This simplifies state management and the amount of terminology end-users
need to learn by using one concept `Runtime` instead of two: `Engine`
and `Store`. This bridges the concepts to the specification by still
having `wazero.Runtime` implement `wasm.Store`.

The net result is that we can know for sure which "engine" is used when
decoding. This allows us a lot of flexibility especially pre-compilation
when JIT is possible.

This also changes the default to JIT based on compiler flags so that
downstream projects like wapc-go don't have to do this individually
(including tracking of which OS+Arch have JIT).

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-03-02 12:29:13 +08:00

37 lines
1000 B
Go

package examples
import (
"bytes"
_ "embed"
"strings"
"testing"
"github.com/stretchr/testify/require"
"github.com/tetratelabs/wazero"
)
// stdioWasm was compiled from TinyGo testdata/stdio.go
//go:embed testdata/stdio.wasm
var stdioWasm []byte
func Test_stdio(t *testing.T) {
r := wazero.NewRuntime()
stdinBuf := bytes.NewBuffer([]byte("WASI\n"))
stdoutBuf := bytes.NewBuffer(nil)
stderrBuf := bytes.NewBuffer(nil)
// Configure WASI host functions with the IO buffers
wasiConfig := &wazero.WASIConfig{Stdin: stdinBuf, Stdout: stdoutBuf, Stderr: stderrBuf}
_, err := r.NewHostModule(wazero.WASISnapshotPreview1WithConfig(wasiConfig))
require.NoError(t, err)
// StartWASICommand runs the "_start" function which is what TinyGo compiles "main" to
_, err = wazero.StartWASICommandFromSource(r, stdioWasm)
require.NoError(t, err)
require.Equal(t, "Hello, WASI!", strings.TrimSpace(stdoutBuf.String()))
require.Equal(t, "Error Message", strings.TrimSpace(stderrBuf.String()))
}