WebAssembly Core Working Draft 1 recently came out. Before that, we had a toe-hold feature bucked called FinishedFeatures. This replaces `RuntimeConfig.WithFinishedFeatures` with `RuntimeConfig.WithWasmCore2`. This also adds `WithWasmCore1` for those who want to lock into 1.0 features as opposed to relying on defaults. This also fixes some design debt where we hadn't finished migrating public types that require constructor functions (NewXxx) to interfaces. By using interfaces, we prevent people from accidentally initializing key configuration directly (via &Xxx), causing nil field refs. This also helps prevent confusion about how to use the type (ex pointer or not) as there's only one way (as an interface). See https://github.com/tetratelabs/wazero/issues/516 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"
|
|
)
|
|
|
|
func TestExampleUpToDate(t *testing.T) {
|
|
t.Run("binary.DecodeModule", func(t *testing.T) {
|
|
m, err := binary.DecodeModule(exampleBinary, wasm.Features20220419, wasm.MemoryLimitPages)
|
|
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.MemoryLimitPages)
|
|
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.InstantiateSnapshotPreview1(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.MemoryLimitPages); 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.MemoryLimitPages); err != nil {
|
|
b.Fatal(err)
|
|
}
|
|
}
|
|
})
|
|
}
|