Files
wazero/examples/fibonacci_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

41 lines
1.0 KiB
Go

package examples
import (
"context"
_ "embed"
"testing"
"github.com/stretchr/testify/require"
"github.com/tetratelabs/wazero"
)
// fibWasm was compiled from TinyGo testdata/fibonacci.go
//go:embed testdata/fibonacci.wasm
var fibWasm []byte // TODO: implement this in text format as it is less distracting setup
func Test_fibonacci(t *testing.T) {
r := wazero.NewRuntime()
// Note: fibonacci.go doesn't directly use WASI, but TinyGo needs to be initialized as a WASI Command.
_, err := r.NewHostModule(wazero.WASISnapshotPreview1())
require.NoError(t, err)
module, err := wazero.StartWASICommandFromSource(r, fibWasm)
require.NoError(t, err)
fibonacci := module.Function("fibonacci")
for _, c := range []struct {
input, expected uint64 // i32_i32 sig, but wasm.Function params and results are uint64
}{
{input: 20, expected: 6765},
{input: 10, expected: 55},
{input: 5, expected: 5},
} {
results, err := fibonacci.Call(context.Background(), c.input)
require.NoError(t, err)
require.Equal(t, c.expected, results[0])
}
}