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>
131 lines
3.8 KiB
Go
131 lines
3.8 KiB
Go
package binary
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/tetratelabs/wazero/internal/testing/require"
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
func TestFunctionType(t *testing.T) {
|
|
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.FunctionType
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "empty",
|
|
input: &wasm.FunctionType{},
|
|
expected: []byte{0x60, 0, 0},
|
|
},
|
|
{
|
|
name: "one param no result",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i32}},
|
|
expected: []byte{0x60, 1, i32, 0},
|
|
},
|
|
{
|
|
name: "no param one result",
|
|
input: &wasm.FunctionType{Results: []wasm.ValueType{i32}},
|
|
expected: []byte{0x60, 0, 1, i32},
|
|
},
|
|
{
|
|
name: "one param one result",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
|
|
expected: []byte{0x60, 1, i64, 1, i32},
|
|
},
|
|
{
|
|
name: "two params no result",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}},
|
|
expected: []byte{0x60, 2, i32, i64, 0},
|
|
},
|
|
{
|
|
name: "two param one result",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32}},
|
|
expected: []byte{0x60, 2, i32, i64, 1, i32},
|
|
},
|
|
{
|
|
name: "no param two results",
|
|
input: &wasm.FunctionType{Results: []wasm.ValueType{i32, i64}},
|
|
expected: []byte{0x60, 0, 2, i32, i64},
|
|
},
|
|
{
|
|
name: "one param two results",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32, i64}},
|
|
expected: []byte{0x60, 1, i64, 2, i32, i64},
|
|
},
|
|
{
|
|
name: "two param two results",
|
|
input: &wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32, i64}},
|
|
expected: []byte{0x60, 2, i32, i64, 2, i32, i64},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
b := encodeFunctionType(tc.input)
|
|
t.Run(fmt.Sprintf("encode - %s", tc.name), func(t *testing.T) {
|
|
require.Equal(t, tc.expected, b)
|
|
})
|
|
|
|
t.Run(fmt.Sprintf("decode - %s", tc.name), func(t *testing.T) {
|
|
binary, err := decodeFunctionType(wasm.Features20220419, bytes.NewReader(b))
|
|
require.NoError(t, err)
|
|
require.Equal(t, binary, tc.input)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDecodeFunctionType_Errors(t *testing.T) {
|
|
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
|
|
tests := []struct {
|
|
name string
|
|
input []byte
|
|
enabledFeatures wasm.Features
|
|
expectedErr string
|
|
}{
|
|
{
|
|
name: "undefined param no result",
|
|
input: []byte{0x60, 1, 0x6f, 0},
|
|
expectedErr: "could not read parameter types: invalid value type: 111",
|
|
},
|
|
{
|
|
name: "no param undefined result",
|
|
input: []byte{0x60, 0, 1, 0x6f},
|
|
expectedErr: "could not read result types: invalid value type: 111",
|
|
},
|
|
{
|
|
name: "undefined param undefined result",
|
|
input: []byte{0x60, 1, 0x6f, 1, 0x6f},
|
|
expectedErr: "could not read parameter types: invalid value type: 111",
|
|
},
|
|
{
|
|
name: "no param two results - multi-value not enabled",
|
|
input: []byte{0x60, 0, 2, i32, i64},
|
|
expectedErr: "multiple result types invalid as feature \"multi-value\" is disabled",
|
|
},
|
|
{
|
|
name: "one param two results - multi-value not enabled",
|
|
input: []byte{0x60, 1, i64, 2, i32, i64},
|
|
expectedErr: "multiple result types invalid as feature \"multi-value\" is disabled",
|
|
},
|
|
{
|
|
name: "two param two results - multi-value not enabled",
|
|
input: []byte{0x60, 2, i32, i64, 2, i32, i64},
|
|
expectedErr: "multiple result types invalid as feature \"multi-value\" is disabled",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
_, err := decodeFunctionType(wasm.Features20191205, bytes.NewReader(tc.input))
|
|
require.EqualError(t, err, tc.expectedErr)
|
|
})
|
|
}
|
|
}
|