The componentized successor to wasi_snapshot_preview1 is not compatible with the prior imports or even error numbers. Before releasing wazero 1.0 we need to change this package to reflect that WASI 2 is effectively a different API. Fixes #263 Signed-off-by: Adrian Cole <adrian@tetrate.io>
72 lines
2.0 KiB
Go
72 lines
2.0 KiB
Go
package vs
|
|
|
|
import (
|
|
_ "embed"
|
|
"testing"
|
|
|
|
"github.com/tetratelabs/wazero"
|
|
"github.com/tetratelabs/wazero/internal/testing/require"
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
"github.com/tetratelabs/wazero/internal/wasm/binary"
|
|
"github.com/tetratelabs/wazero/internal/wasm/text"
|
|
"github.com/tetratelabs/wazero/wasi_snapshot_preview1"
|
|
)
|
|
|
|
func TestExampleUpToDate(t *testing.T) {
|
|
t.Run("binary.DecodeModule", func(t *testing.T) {
|
|
m, err := binary.DecodeModule(exampleBinary, wasm.Features20220419, wasm.MemorySizer)
|
|
require.NoError(t, err)
|
|
require.Equal(t, example, m)
|
|
})
|
|
|
|
t.Run("text.DecodeModule", func(t *testing.T) {
|
|
m, err := text.DecodeModule(exampleText, wasm.Features20220419, wasm.MemorySizer)
|
|
require.NoError(t, err)
|
|
require.Equal(t, example, m)
|
|
})
|
|
|
|
t.Run("Executable", func(t *testing.T) {
|
|
r := wazero.NewRuntimeWithConfig(wazero.NewRuntimeConfig().WithWasmCore2())
|
|
|
|
// Add WASI to satisfy import tests
|
|
wm, err := wasi_snapshot_preview1.Instantiate(testCtx, r)
|
|
require.NoError(t, err)
|
|
defer wm.Close(testCtx)
|
|
|
|
// Decode and instantiate the module
|
|
module, err := r.InstantiateModuleFromCode(testCtx, exampleBinary)
|
|
require.NoError(t, err)
|
|
defer module.Close(testCtx)
|
|
|
|
// Call the swap function as a smoke test
|
|
results, err := module.ExportedFunction("swap").Call(testCtx, 1, 2)
|
|
require.NoError(t, err)
|
|
require.Equal(t, []uint64{2, 1}, results)
|
|
})
|
|
}
|
|
|
|
func BenchmarkCodec(b *testing.B) {
|
|
b.Run("binary.DecodeModule", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := binary.DecodeModule(exampleBinary, wasm.Features20220419, wasm.MemorySizer); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
b.Run("binary.EncodeModule", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
_ = binary.EncodeModule(example)
|
|
}
|
|
})
|
|
b.Run("text.DecodeModule", func(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
if _, err := text.DecodeModule(exampleText, wasm.Features20220419, wasm.MemorySizer); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|