refactor: wasm.ValueType as type definition + widening (uint64) (#2495)

This commit is contained in:
Edoardo Vacchi
2026-05-11 21:01:15 +02:00
committed by GitHub
parent 8755611b73
commit a3374cf27a
59 changed files with 426 additions and 392 deletions
+2 -2
View File
@@ -243,13 +243,13 @@ type hostFunctionBuilder struct {
// WithGoFunction implements HostFunctionBuilder.WithGoFunction // WithGoFunction implements HostFunctionBuilder.WithGoFunction
func (h *hostFunctionBuilder) WithGoFunction(fn api.GoFunction, params, results []api.ValueType) HostFunctionBuilder { func (h *hostFunctionBuilder) WithGoFunction(fn api.GoFunction, params, results []api.ValueType) HostFunctionBuilder {
h.fn = &wasm.HostFunc{ParamTypes: params, ResultTypes: results, Code: wasm.Code{GoFunc: fn}} h.fn = &wasm.HostFunc{ParamTypes: wasm.FromApiValueType(params), ResultTypes: wasm.FromApiValueType(results), Code: wasm.Code{GoFunc: fn}}
return h return h
} }
// WithGoModuleFunction implements HostFunctionBuilder.WithGoModuleFunction // WithGoModuleFunction implements HostFunctionBuilder.WithGoModuleFunction
func (h *hostFunctionBuilder) WithGoModuleFunction(fn api.GoModuleFunction, params, results []api.ValueType) HostFunctionBuilder { func (h *hostFunctionBuilder) WithGoModuleFunction(fn api.GoModuleFunction, params, results []api.ValueType) HostFunctionBuilder {
h.fn = &wasm.HostFunc{ParamTypes: params, ResultTypes: results, Code: wasm.Code{GoFunc: fn}} h.fn = &wasm.HostFunc{ParamTypes: wasm.FromApiValueType(params), ResultTypes: wasm.FromApiValueType(results), Code: wasm.Code{GoFunc: fn}}
return h return h
} }
+18 -18
View File
@@ -11,7 +11,7 @@ import (
// TestNewHostModuleBuilder_Compile only covers a few scenarios to avoid duplicating tests in internal/wasm/host_test.go // TestNewHostModuleBuilder_Compile only covers a few scenarios to avoid duplicating tests in internal/wasm/host_test.go
func TestNewHostModuleBuilder_Compile(t *testing.T) { func TestNewHostModuleBuilder_Compile(t *testing.T) {
i32, i64 := api.ValueTypeI32, api.ValueTypeI64 i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
uint32_uint32 := func(context.Context, uint32) uint32 { uint32_uint32 := func(context.Context, uint32) uint32 {
return 0 return 0
@@ -54,7 +54,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)}, CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -80,7 +80,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)}, CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -107,7 +107,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)}, CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -133,7 +133,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32)}, CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32)},
@@ -159,8 +159,8 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0, 1}, FunctionSection: []wasm.Index{0, 1},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32), wasm.MustParseGoReflectFuncCode(uint32_uint32)}, CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32), wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -183,12 +183,12 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
input: func(r Runtime) HostModuleBuilder { input: func(r Runtime) HostModuleBuilder {
return r.NewHostModuleBuilder("host"). return r.NewHostModuleBuilder("host").
NewFunctionBuilder(). NewFunctionBuilder().
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}). WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1") Export("1")
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{ CodeSection: []wasm.Code{
@@ -210,13 +210,13 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
name: "WithGoFunction WithName WithParameterNames", name: "WithGoFunction WithName WithParameterNames",
input: func(r Runtime) HostModuleBuilder { input: func(r Runtime) HostModuleBuilder {
return r.NewHostModuleBuilder("host").NewFunctionBuilder(). return r.NewHostModuleBuilder("host").NewFunctionBuilder().
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}). WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
WithName("get").WithParameterNames("x"). WithName("get").WithParameterNames("x").
Export("1") Export("1")
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{ CodeSection: []wasm.Code{
@@ -240,15 +240,15 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
input: func(r Runtime) HostModuleBuilder { input: func(r Runtime) HostModuleBuilder {
return r.NewHostModuleBuilder("host"). return r.NewHostModuleBuilder("host").
NewFunctionBuilder(). NewFunctionBuilder().
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}). WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1"). Export("1").
NewFunctionBuilder(). NewFunctionBuilder().
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}). WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1") Export("1")
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{ CodeSection: []wasm.Code{
@@ -272,16 +272,16 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
// Intentionally not in lexicographic order // Intentionally not in lexicographic order
return r.NewHostModuleBuilder("host"). return r.NewHostModuleBuilder("host").
NewFunctionBuilder(). NewFunctionBuilder().
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}). WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("2"). Export("2").
NewFunctionBuilder(). NewFunctionBuilder().
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}). WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1") Export("1")
}, },
expected: &wasm.Module{ expected: &wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}}, {Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
}, },
FunctionSection: []wasm.Index{0, 1}, FunctionSection: []wasm.Index{0, 1},
CodeSection: []wasm.Code{ CodeSection: []wasm.Code{
+4 -4
View File
@@ -177,10 +177,10 @@ func (f *mockFunctionDefinition) DebugName() string {
return f.debugName return f.debugName
} }
func (f *mockFunctionDefinition) ParamTypes() []wasm.ValueType { func (f *mockFunctionDefinition) ParamTypes() []api.ValueType {
return []wasm.ValueType{} return []api.ValueType{}
} }
func (f *mockFunctionDefinition) ResultTypes() []wasm.ValueType { func (f *mockFunctionDefinition) ResultTypes() []api.ValueType {
return []wasm.ValueType{} return []api.ValueType{}
} }
+23 -23
View File
@@ -23,8 +23,8 @@ var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary")
func Test_loggingListener(t *testing.T) { func Test_loggingListener(t *testing.T) {
wasiFuncName := wasi.RandomGetName wasiFuncName := wasi.RandomGetName
wasiFuncType := wasm.FunctionType{ wasiFuncType := wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32}, Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32},
} }
wasiParamNames := []string{"buf", "buf_len"} wasiParamNames := []string{"buf", "buf_len"}
wasiParams := []uint64{0, 8} wasiParams := []uint64{0, 8}
@@ -84,7 +84,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "i32", name: "i32",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
params: []uint64{math.MaxUint32}, params: []uint64{math.MaxUint32},
expected: `--> test.fn(-1) expected: `--> test.fn(-1)
<-- <--
@@ -92,7 +92,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "i32 named", name: "i32 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
params: []uint64{math.MaxUint32}, params: []uint64{math.MaxUint32},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=-1) expected: `--> test.fn(x=-1)
@@ -101,7 +101,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "i64", name: "i64",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
params: []uint64{math.MaxUint64}, params: []uint64{math.MaxUint64},
expected: `--> test.fn(-1) expected: `--> test.fn(-1)
<-- <--
@@ -109,7 +109,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "i64 named", name: "i64 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
params: []uint64{math.MaxUint64}, params: []uint64{math.MaxUint64},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=-1) expected: `--> test.fn(x=-1)
@@ -118,7 +118,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "f32", name: "f32",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF32}},
params: []uint64{api.EncodeF32(math.MaxFloat32)}, params: []uint64{api.EncodeF32(math.MaxFloat32)},
expected: `--> test.fn(3.4028235e+38) expected: `--> test.fn(3.4028235e+38)
<-- <--
@@ -126,7 +126,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "f32 named", name: "f32 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF32}},
params: []uint64{api.EncodeF32(math.MaxFloat32)}, params: []uint64{api.EncodeF32(math.MaxFloat32)},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=3.4028235e+38) expected: `--> test.fn(x=3.4028235e+38)
@@ -135,7 +135,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "f64", name: "f64",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF64}},
params: []uint64{api.EncodeF64(math.MaxFloat64)}, params: []uint64{api.EncodeF64(math.MaxFloat64)},
expected: `--> test.fn(1.7976931348623157e+308) expected: `--> test.fn(1.7976931348623157e+308)
<-- <--
@@ -143,7 +143,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "f64 named", name: "f64 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF64}},
params: []uint64{api.EncodeF64(math.MaxFloat64)}, params: []uint64{api.EncodeF64(math.MaxFloat64)},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=1.7976931348623157e+308) expected: `--> test.fn(x=1.7976931348623157e+308)
@@ -152,7 +152,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "externref", name: "externref",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
params: []uint64{0}, params: []uint64{0},
expected: `--> test.fn(0000000000000000) expected: `--> test.fn(0000000000000000)
<-- <--
@@ -160,7 +160,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "externref named", name: "externref named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}}, functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
params: []uint64{0}, params: []uint64{0},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=0000000000000000) expected: `--> test.fn(x=0000000000000000)
@@ -169,7 +169,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "v128", name: "v128",
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}}, functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
params: []uint64{0, 1}, params: []uint64{0, 1},
expected: `--> test.fn(00000000000000000000000000000001) expected: `--> test.fn(00000000000000000000000000000001)
<-- <--
@@ -177,7 +177,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "v128 named", name: "v128 named",
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}}, functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
params: []uint64{0, 1}, params: []uint64{0, 1},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=00000000000000000000000000000001) expected: `--> test.fn(x=00000000000000000000000000000001)
@@ -186,7 +186,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "funcref", name: "funcref",
functype: wasm.FunctionType{Params: []api.ValueType{0x70}}, functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
params: []uint64{0}, params: []uint64{0},
expected: `--> test.fn(0000000000000000) expected: `--> test.fn(0000000000000000)
<-- <--
@@ -194,7 +194,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "funcref named", name: "funcref named",
functype: wasm.FunctionType{Params: []api.ValueType{0x70}}, functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
params: []uint64{0}, params: []uint64{0},
paramNames: []string{"x"}, paramNames: []string{"x"},
expected: `--> test.fn(x=0000000000000000) expected: `--> test.fn(x=0000000000000000)
@@ -203,7 +203,7 @@ func Test_loggingListener(t *testing.T) {
}, },
{ {
name: "no params, one result", name: "no params, one result",
functype: wasm.FunctionType{Results: []api.ValueType{api.ValueTypeI32}}, functype: wasm.FunctionType{Results: []wasm.ValueType{wasm.ValueTypeI32}},
results: []uint64{math.MaxUint32}, results: []uint64{math.MaxUint32},
expected: `--> test.fn() expected: `--> test.fn()
<-- -1 <-- -1
@@ -212,8 +212,8 @@ func Test_loggingListener(t *testing.T) {
{ {
name: "one param, one result", name: "one param, one result",
functype: wasm.FunctionType{ functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32}, Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeF32}, Results: []wasm.ValueType{wasm.ValueTypeF32},
}, },
params: []uint64{math.MaxUint32}, params: []uint64{math.MaxUint32},
results: []uint64{api.EncodeF32(math.MaxFloat32)}, results: []uint64{api.EncodeF32(math.MaxFloat32)},
@@ -224,8 +224,8 @@ func Test_loggingListener(t *testing.T) {
{ {
name: "two params, two results", name: "two params, two results",
functype: wasm.FunctionType{ functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64}, Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
}, },
params: []uint64{math.MaxUint32, math.MaxUint64}, params: []uint64{math.MaxUint32, math.MaxUint64},
results: []uint64{api.EncodeF32(math.MaxFloat32), api.EncodeF64(math.MaxFloat64)}, results: []uint64{api.EncodeF32(math.MaxFloat32), api.EncodeF64(math.MaxFloat64)},
@@ -236,8 +236,8 @@ func Test_loggingListener(t *testing.T) {
{ {
name: "two params, two results named", name: "two params, two results named",
functype: wasm.FunctionType{ functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64}, Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64}, Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
}, },
params: []uint64{math.MaxUint32, math.MaxUint64}, params: []uint64{math.MaxUint32, math.MaxUint64},
paramNames: []string{"x", "y"}, paramNames: []string{"x", "y"},
+1 -1
View File
@@ -22,7 +22,7 @@ func LookupFunction(
expectedParamTypes, expectedResultTypes []api.ValueType, expectedParamTypes, expectedResultTypes []api.ValueType,
) api.Function { ) api.Function {
m := module.(*wasm.ModuleInstance) m := module.(*wasm.ModuleInstance)
typ := &wasm.FunctionType{Params: expectedParamTypes, Results: expectedResultTypes} typ := &wasm.FunctionType{Params: wasm.FromApiValueType(expectedParamTypes), Results: wasm.FromApiValueType(expectedResultTypes)}
typ.CacheNumInUint64() typ.CacheNumInUint64()
typeID := m.GetFunctionTypeID(typ) typeID := m.GetFunctionTypeID(typ)
if int(tableIndex) >= len(m.Tables) { if int(tableIndex) >= len(m.Tables) {
+8 -7
View File
@@ -14,6 +14,7 @@ import (
func TestLookupFunction(t *testing.T) { func TestLookupFunction(t *testing.T) {
const i32 = wasm.ValueTypeI32 const i32 = wasm.ValueTypeI32
const i32a = api.ValueTypeI32
bytes := binaryencoding.EncodeModule(&wasm.Module{ bytes := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
{Results: []wasm.ValueType{i32}}, {Results: []wasm.ValueType{i32}},
@@ -51,14 +52,14 @@ func TestLookupFunction(t *testing.T) {
require.NotNil(t, m) require.NotNil(t, m)
t.Run("v_i32", func(t *testing.T) { t.Run("v_i32", func(t *testing.T) {
f := table.LookupFunction(m, 0, 0, nil, []api.ValueType{i32}) f := table.LookupFunction(m, 0, 0, nil, []api.ValueType{i32a})
var result [1]uint64 var result [1]uint64
err = f.CallWithStack(context.Background(), result[:]) err = f.CallWithStack(context.Background(), result[:])
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, uint64(1), result[0]) require.Equal(t, uint64(1), result[0])
}) })
t.Run("i32i32_i32i32", func(t *testing.T) { t.Run("i32i32_i32i32", func(t *testing.T) {
f := table.LookupFunction(m, 0, 1, []api.ValueType{i32, i32}, []api.ValueType{i32, i32}) f := table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, []api.ValueType{i32a, i32a})
stack := [2]uint64{100, 200} stack := [2]uint64{100, 200}
err = f.CallWithStack(context.Background(), stack[:]) err = f.CallWithStack(context.Background(), stack[:])
require.NoError(t, err) require.NoError(t, err)
@@ -68,11 +69,11 @@ func TestLookupFunction(t *testing.T) {
t.Run("panics", func(t *testing.T) { t.Run("panics", func(t *testing.T) {
err := require.CapturePanic(func() { err := require.CapturePanic(func() {
table.LookupFunction(m, 0, 2000, nil, []api.ValueType{i32}) table.LookupFunction(m, 0, 2000, nil, []api.ValueType{i32a})
}) })
require.Equal(t, "invalid table access", err.Error()) require.Equal(t, "invalid table access", err.Error())
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(m, 1000, 0, nil, []api.ValueType{i32}) table.LookupFunction(m, 1000, 0, nil, []api.ValueType{i32a})
}) })
require.Equal(t, "table index out of range", err.Error()) require.Equal(t, "table index out of range", err.Error())
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
@@ -80,15 +81,15 @@ func TestLookupFunction(t *testing.T) {
}) })
require.Equal(t, "indirect call type mismatch", err.Error()) require.Equal(t, "indirect call type mismatch", err.Error())
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(m, 0, 0, []api.ValueType{i32}, nil) table.LookupFunction(m, 0, 0, []api.ValueType{i32a}, nil)
}) })
require.Equal(t, "indirect call type mismatch", err.Error()) require.Equal(t, "indirect call type mismatch", err.Error())
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(m, 0, 1, []api.ValueType{i32, i32}, nil) table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, nil)
}) })
require.Equal(t, "indirect call type mismatch", err.Error()) require.Equal(t, "indirect call type mismatch", err.Error())
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(m, 0, 1, []api.ValueType{i32, i32}, []api.ValueType{i32, api.ValueTypeF32}) table.LookupFunction(m, 0, 1, []api.ValueType{i32a, i32a}, []api.ValueType{i32a, api.ValueTypeF32})
}) })
require.Equal(t, "indirect call type mismatch", err.Error()) require.Equal(t, "indirect call type mismatch", err.Error())
}) })
+3 -3
View File
@@ -146,7 +146,7 @@ func (e *functionExporter) ExportFunctions(builder wazero.HostModuleBuilder) {
var abortMessageEnabled = &wasm.HostFunc{ var abortMessageEnabled = &wasm.HostFunc{
ExportName: AbortName, ExportName: AbortName,
Name: "~lib/builtins/abort", Name: "~lib/builtins/abort",
ParamTypes: []api.ValueType{i32, i32, i32, i32}, ParamTypes: []wasm.ValueType{i32, i32, i32, i32},
ParamNames: []string{"message", "fileName", "lineNumber", "columnNumber"}, ParamNames: []string{"message", "fileName", "lineNumber", "columnNumber"},
Code: wasm.Code{GoFunc: api.GoModuleFunc(abortWithMessage)}, Code: wasm.Code{GoFunc: api.GoModuleFunc(abortWithMessage)},
} }
@@ -195,7 +195,7 @@ var traceDisabled = traceStdout.WithGoModuleFunc(func(context.Context, api.Modul
var traceStdout = &wasm.HostFunc{ var traceStdout = &wasm.HostFunc{
ExportName: TraceName, ExportName: TraceName,
Name: "~lib/builtins/trace", Name: "~lib/builtins/trace",
ParamTypes: []api.ValueType{i32, i32, f64, f64, f64, f64, f64}, ParamTypes: []wasm.ValueType{i32, i32, f64, f64, f64, f64, f64},
ParamNames: []string{"message", "nArgs", "arg0", "arg1", "arg2", "arg3", "arg4"}, ParamNames: []string{"message", "nArgs", "arg0", "arg1", "arg2", "arg3", "arg4"},
Code: wasm.Code{ Code: wasm.Code{
GoFunc: api.GoModuleFunc(func(_ context.Context, mod api.Module, stack []uint64) { GoFunc: api.GoModuleFunc(func(_ context.Context, mod api.Module, stack []uint64) {
@@ -281,7 +281,7 @@ func formatFloat(f float64) string {
var seed = &wasm.HostFunc{ var seed = &wasm.HostFunc{
ExportName: SeedName, ExportName: SeedName,
Name: "~lib/builtins/seed", Name: "~lib/builtins/seed",
ResultTypes: []api.ValueType{f64}, ResultTypes: []wasm.ValueType{f64},
ResultNames: []string{"rand"}, ResultNames: []string{"rand"},
Code: wasm.Code{ Code: wasm.Code{
GoFunc: api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) { GoFunc: api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) {
+25 -26
View File
@@ -7,7 +7,6 @@ import (
"testing" "testing"
"github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental" "github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging" "github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
@@ -137,44 +136,44 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_v", ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
}, },
{ {
ExportName: "invoke_i", ExportName: "invoke_i",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i32}, ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i32}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
}, },
{ {
ExportName: "invoke_p", ExportName: "invoke_p",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i32}, ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i32}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
}, },
{ {
ExportName: "invoke_j", ExportName: "invoke_j",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i64}, ResultTypes: []wasm.ValueType{i64},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i64}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i64}}}},
}, },
{ {
ExportName: "invoke_f", ExportName: "invoke_f",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
ResultTypes: []api.ValueType{f32}, ResultTypes: []wasm.ValueType{f32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{f32}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{f32}}}},
}, },
{ {
ExportName: "invoke_d", ExportName: "invoke_d",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
ResultTypes: []api.ValueType{f64}, ResultTypes: []wasm.ValueType{f64},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{f64}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{f64}}}},
}, },
}, },
}, },
@@ -205,7 +204,7 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_v", ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
}, },
@@ -231,7 +230,7 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_v", ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"}, ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
}, },
@@ -255,9 +254,9 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_vi", ExportName: "invoke_vi",
ParamTypes: []api.ValueType{i32, i32}, ParamTypes: []wasm.ValueType{i32, i32},
ParamNames: []string{"index", "a1"}, ParamNames: []string{"index", "a1"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Params: []api.ValueType{i32}}}}, Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Params: []wasm.ValueType{i32}}}},
}, },
}, },
}, },
@@ -281,12 +280,12 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_iiiii", ExportName: "invoke_iiiii",
ParamTypes: []api.ValueType{i32, i32, i32, i32, i32}, ParamTypes: []wasm.ValueType{i32, i32, i32, i32, i32},
ParamNames: []string{"index", "a1", "a2", "a3", "a4"}, ParamNames: []string{"index", "a1", "a2", "a3", "a4"},
ResultTypes: []wasm.ValueType{i32}, ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{ Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{
Params: []api.ValueType{i32, i32, i32, i32}, Params: []wasm.ValueType{i32, i32, i32, i32},
Results: []api.ValueType{i32}, Results: []wasm.ValueType{i32},
}}}, }}},
}, },
}, },
@@ -310,10 +309,10 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{ expected: []*wasm.HostFunc{
{ {
ExportName: "invoke_viiiddiiiiii", ExportName: "invoke_viiiddiiiiii",
ParamTypes: []api.ValueType{i32, i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32}, ParamTypes: []wasm.ValueType{i32, i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32},
ParamNames: []string{"index", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11"}, ParamNames: []string{"index", "a1", "a2", "a3", "a4", "a5", "a6", "a7", "a8", "a9", "a10", "a11"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{ Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{
Params: []api.ValueType{i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32}, Params: []wasm.ValueType{i32, i32, i32, f64, f64, i32, i32, i32, i32, i32, i32},
}}}, }}},
}, },
}, },
+2 -2
View File
@@ -41,7 +41,7 @@ import (
// See argsSizesGet // See argsSizesGet
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#args_get // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#args_get
// See https://en.wikipedia.org/wiki/Null-terminated_string // See https://en.wikipedia.org/wiki/Null-terminated_string
var argsGet = newHostFunc(wasip1.ArgsGetName, argsGetFn, []api.ValueType{i32, i32}, "argv", "argv_buf") var argsGet = newHostFunc(wasip1.ArgsGetName, argsGetFn, []wasm.ValueType{i32, i32}, "argv", "argv_buf")
func argsGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func argsGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
@@ -78,7 +78,7 @@ func argsGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
// See argsGet // See argsGet
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#args_sizes_get // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#args_sizes_get
// See https://en.wikipedia.org/wiki/Null-terminated_string // See https://en.wikipedia.org/wiki/Null-terminated_string
var argsSizesGet = newHostFunc(wasip1.ArgsSizesGetName, argsSizesGetFn, []api.ValueType{i32, i32}, "result.argc", "result.argv_len") var argsSizesGet = newHostFunc(wasip1.ArgsSizesGetName, argsSizesGetFn, []wasm.ValueType{i32, i32}, "result.argc", "result.argv_len")
func argsSizesGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func argsSizesGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
+2 -2
View File
@@ -37,7 +37,7 @@ import (
// Note: This is similar to `clock_getres` in POSIX. // Note: This is similar to `clock_getres` in POSIX.
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-clock_res_getid-clockid---errno-timestamp // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-clock_res_getid-clockid---errno-timestamp
// See https://linux.die.net/man/3/clock_getres // See https://linux.die.net/man/3/clock_getres
var clockResGet = newHostFunc(wasip1.ClockResGetName, clockResGetFn, []api.ValueType{i32, i32}, "id", "result.resolution") var clockResGet = newHostFunc(wasip1.ClockResGetName, clockResGetFn, []wasm.ValueType{i32, i32}, "id", "result.resolution")
func clockResGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func clockResGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
@@ -90,7 +90,7 @@ func clockResGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno
// Note: This is similar to `clock_gettime` in POSIX. // Note: This is similar to `clock_gettime` in POSIX.
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-clock_time_getid-clockid-precision-timestamp---errno-timestamp // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-clock_time_getid-clockid-precision-timestamp---errno-timestamp
// See https://linux.die.net/man/3/clock_gettime // See https://linux.die.net/man/3/clock_gettime
var clockTimeGet = newHostFunc(wasip1.ClockTimeGetName, clockTimeGetFn, []api.ValueType{i32, i64, i32}, "id", "precision", "result.timestamp") var clockTimeGet = newHostFunc(wasip1.ClockTimeGetName, clockTimeGetFn, []wasm.ValueType{i32, i64, i32}, "id", "precision", "result.timestamp")
func clockTimeGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func clockTimeGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
+2 -2
View File
@@ -41,7 +41,7 @@ import (
// See environSizesGet // See environSizesGet
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_get // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_get
// See https://en.wikipedia.org/wiki/Null-terminated_string // See https://en.wikipedia.org/wiki/Null-terminated_string
var environGet = newHostFunc(wasip1.EnvironGetName, environGetFn, []api.ValueType{i32, i32}, "environ", "environ_buf") var environGet = newHostFunc(wasip1.EnvironGetName, environGetFn, []wasm.ValueType{i32, i32}, "environ", "environ_buf")
func environGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func environGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
@@ -81,7 +81,7 @@ func environGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno
// See environGet // See environGet
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_sizes_get // https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_sizes_get
// and https://en.wikipedia.org/wiki/Null-terminated_string // and https://en.wikipedia.org/wiki/Null-terminated_string
var environSizesGet = newHostFunc(wasip1.EnvironSizesGetName, environSizesGetFn, []api.ValueType{i32, i32}, "result.environc", "result.environv_len") var environSizesGet = newHostFunc(wasip1.EnvironSizesGetName, environSizesGetFn, []wasm.ValueType{i32, i32}, "result.environc", "result.environv_len")
func environSizesGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func environSizesGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
+15 -15
View File
@@ -115,7 +115,7 @@ func fdAllocateFn(_ context.Context, mod api.Module, params []uint64) experiment
// Note: This is similar to `close` in POSIX. // Note: This is similar to `close` in POSIX.
// See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_close // See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_close
// and https://linux.die.net/man/3/close // and https://linux.die.net/man/3/close
var fdClose = newHostFunc(wasip1.FdCloseName, fdCloseFn, []api.ValueType{i32}, "fd") var fdClose = newHostFunc(wasip1.FdCloseName, fdCloseFn, []wasm.ValueType{i32}, "fd")
func fdCloseFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { func fdCloseFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fsc := mod.(*wasm.ModuleInstance).Sys.FS() fsc := mod.(*wasm.ModuleInstance).Sys.FS()
@@ -128,7 +128,7 @@ func fdCloseFn(_ context.Context, mod api.Module, params []uint64) experimentals
// the data of a file to disk. // the data of a file to disk.
// //
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_datasyncfd-fd---errno // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_datasyncfd-fd---errno
var fdDatasync = newHostFunc(wasip1.FdDatasyncName, fdDatasyncFn, []api.ValueType{i32}, "fd") var fdDatasync = newHostFunc(wasip1.FdDatasyncName, fdDatasyncFn, []wasm.ValueType{i32}, "fd")
func fdDatasyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { func fdDatasyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fsc := mod.(*wasm.ModuleInstance).Sys.FS() fsc := mod.(*wasm.ModuleInstance).Sys.FS()
@@ -179,7 +179,7 @@ func fdDatasyncFn(_ context.Context, mod api.Module, params []uint64) experiment
// well as additional fields. // well as additional fields.
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fdstat // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fdstat
// and https://linux.die.net/man/3/fsync // and https://linux.die.net/man/3/fsync
var fdFdstatGet = newHostFunc(wasip1.FdFdstatGetName, fdFdstatGetFn, []api.ValueType{i32, i32}, "fd", "result.stat") var fdFdstatGet = newHostFunc(wasip1.FdFdstatGetName, fdFdstatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.stat")
// fdFdstatGetFn cannot currently use proxyResultParams because fdstat is larger // fdFdstatGetFn cannot currently use proxyResultParams because fdstat is larger
// than api.ValueTypeI64 (i64 == 8 bytes, but fdstat is 24). // than api.ValueTypeI64 (i64 == 8 bytes, but fdstat is 24).
@@ -386,7 +386,7 @@ var fdFdstatSetRights = stubFunction(
// Note: This is similar to `fstat` in POSIX. // Note: This is similar to `fstat` in POSIX.
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_filestat_getfd-fd---errno-filestat // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_filestat_getfd-fd---errno-filestat
// and https://linux.die.net/man/3/fstat // and https://linux.die.net/man/3/fstat
var fdFilestatGet = newHostFunc(wasip1.FdFilestatGetName, fdFilestatGetFn, []api.ValueType{i32, i32}, "fd", "result.filestat") var fdFilestatGet = newHostFunc(wasip1.FdFilestatGetName, fdFilestatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.filestat")
// fdFilestatGetFn cannot currently use proxyResultParams because filestat is // fdFilestatGetFn cannot currently use proxyResultParams because filestat is
// larger than api.ValueTypeI64 (i64 == 8 bytes, but filestat is 64). // larger than api.ValueTypeI64 (i64 == 8 bytes, but filestat is 64).
@@ -564,7 +564,7 @@ func toTimes(walltime func() int64, atim, mtim int64, fstFlags uint16) (int64, i
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_preadfd-fd-iovs-iovec_array-offset-filesize---errno-size // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_preadfd-fd-iovs-iovec_array-offset-filesize---errno-size
var fdPread = newHostFunc( var fdPread = newHostFunc(
wasip1.FdPreadName, fdPreadFn, wasip1.FdPreadName, fdPreadFn,
[]api.ValueType{i32, i32, i32, i64, i32}, []wasm.ValueType{i32, i32, i32, i64, i32},
"fd", "iovs", "iovs_len", "offset", "result.nread", "fd", "iovs", "iovs_len", "offset", "result.nread",
) )
@@ -603,7 +603,7 @@ func fdPreadFn(_ context.Context, mod api.Module, params []uint64) experimentals
// //
// See fdPrestatDirName and // See fdPrestatDirName and
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#prestat // https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#prestat
var fdPrestatGet = newHostFunc(wasip1.FdPrestatGetName, fdPrestatGetFn, []api.ValueType{i32, i32}, "fd", "result.prestat") var fdPrestatGet = newHostFunc(wasip1.FdPrestatGetName, fdPrestatGetFn, []wasm.ValueType{i32, i32}, "fd", "result.prestat")
func fdPrestatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { func fdPrestatGetFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fsc := mod.(*wasm.ModuleInstance).Sys.FS() fsc := mod.(*wasm.ModuleInstance).Sys.FS()
@@ -655,7 +655,7 @@ func fdPrestatGetFn(_ context.Context, mod api.Module, params []uint64) experime
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_prestat_dir_name // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_prestat_dir_name
var fdPrestatDirName = newHostFunc( var fdPrestatDirName = newHostFunc(
wasip1.FdPrestatDirNameName, fdPrestatDirNameFn, wasip1.FdPrestatDirNameName, fdPrestatDirNameFn,
[]api.ValueType{i32, i32, i32}, []wasm.ValueType{i32, i32, i32},
"fd", "result.path", "result.path_len", "fd", "result.path", "result.path_len",
) )
@@ -687,7 +687,7 @@ func fdPrestatDirNameFn(_ context.Context, mod api.Module, params []uint64) expe
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_pwritefd-fd-iovs-ciovec_array-offset-filesize---errno-size // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_pwritefd-fd-iovs-ciovec_array-offset-filesize---errno-size
var fdPwrite = newHostFunc( var fdPwrite = newHostFunc(
wasip1.FdPwriteName, fdPwriteFn, wasip1.FdPwriteName, fdPwriteFn,
[]api.ValueType{i32, i32, i32, i64, i32}, []wasm.ValueType{i32, i32, i32, i64, i32},
"fd", "iovs", "iovs_len", "offset", "result.nwritten", "fd", "iovs", "iovs_len", "offset", "result.nwritten",
) )
@@ -746,7 +746,7 @@ func fdPwriteFn(_ context.Context, mod api.Module, params []uint64) experimental
// and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_read // and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_read
var fdRead = newHostFunc( var fdRead = newHostFunc(
wasip1.FdReadName, fdReadFn, wasip1.FdReadName, fdReadFn,
[]api.ValueType{i32, i32, i32, i32}, []wasm.ValueType{i32, i32, i32, i32},
"fd", "iovs", "iovs_len", "result.nread", "fd", "iovs", "iovs_len", "result.nread",
) )
@@ -1113,7 +1113,7 @@ func fdRenumberFn(_ context.Context, mod api.Module, params []uint64) experiment
// and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_seek // and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_seek
var fdSeek = newHostFunc( var fdSeek = newHostFunc(
wasip1.FdSeekName, fdSeekFn, wasip1.FdSeekName, fdSeekFn,
[]api.ValueType{i32, i64, i32, i32}, []wasm.ValueType{i32, i64, i32, i32},
"fd", "offset", "whence", "result.newoffset", "fd", "offset", "whence", "result.newoffset",
) )
@@ -1140,7 +1140,7 @@ func fdSeekFn(_ context.Context, mod api.Module, params []uint64) experimentalsy
// and metadata of a file to disk. // and metadata of a file to disk.
// //
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_syncfd-fd---errno // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_syncfd-fd---errno
var fdSync = newHostFunc(wasip1.FdSyncName, fdSyncFn, []api.ValueType{i32}, "fd") var fdSync = newHostFunc(wasip1.FdSyncName, fdSyncFn, []wasm.ValueType{i32}, "fd")
func fdSyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno { func fdSyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fsc := mod.(*wasm.ModuleInstance).Sys.FS() fsc := mod.(*wasm.ModuleInstance).Sys.FS()
@@ -1158,7 +1158,7 @@ func fdSyncFn(_ context.Context, mod api.Module, params []uint64) experimentalsy
// offset of a file descriptor. // offset of a file descriptor.
// //
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_tellfd-fd---errno-filesize // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-fd_tellfd-fd---errno-filesize
var fdTell = newHostFunc(wasip1.FdTellName, fdTellFn, []api.ValueType{i32, i32}, "fd", "result.offset") var fdTell = newHostFunc(wasip1.FdTellName, fdTellFn, []wasm.ValueType{i32, i32}, "fd", "result.offset")
func fdTellFn(ctx context.Context, mod api.Module, params []uint64) experimentalsys.Errno { func fdTellFn(ctx context.Context, mod api.Module, params []uint64) experimentalsys.Errno {
fd := params[0] fd := params[0]
@@ -1230,7 +1230,7 @@ func fdTellFn(ctx context.Context, mod api.Module, params []uint64) experimental
// and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_write // and https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fd_write
var fdWrite = newHostFunc( var fdWrite = newHostFunc(
wasip1.FdWriteName, fdWriteFn, wasip1.FdWriteName, fdWriteFn,
[]api.ValueType{i32, i32, i32, i32}, []wasm.ValueType{i32, i32, i32, i32},
"fd", "iovs", "iovs_len", "result.nwritten", "fd", "iovs", "iovs_len", "result.nwritten",
) )
@@ -1390,7 +1390,7 @@ func pathCreateDirectoryFn(_ context.Context, mod api.Module, params []uint64) e
// and https://linux.die.net/man/2/fstatat // and https://linux.die.net/man/2/fstatat
var pathFilestatGet = newHostFunc( var pathFilestatGet = newHostFunc(
wasip1.PathFilestatGetName, pathFilestatGetFn, wasip1.PathFilestatGetName, pathFilestatGetFn,
[]api.ValueType{i32, i32, i32, i32, i32}, []wasm.ValueType{i32, i32, i32, i32, i32},
"fd", "flags", "path", "path_len", "result.filestat", "fd", "flags", "path", "path_len", "result.filestat",
) )
@@ -1571,7 +1571,7 @@ func pathLinkFn(_ context.Context, mod api.Module, params []uint64) experimental
// See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#path_open // See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#path_open
var pathOpen = newHostFunc( var pathOpen = newHostFunc(
wasip1.PathOpenName, pathOpenFn, wasip1.PathOpenName, pathOpenFn,
[]api.ValueType{i32, i32, i32, i32, i32, i64, i64, i32, i32}, []wasm.ValueType{i32, i32, i32, i32, i32, i64, i64, i32, i32},
"fd", "dirflags", "path", "path_len", "oflags", "fs_rights_base", "fs_rights_inheriting", "fdflags", "result.opened_fd", "fd", "dirflags", "path", "path_len", "oflags", "fs_rights_base", "fs_rights_inheriting", "fdflags", "result.opened_fd",
) )
+1 -1
View File
@@ -38,7 +38,7 @@ import (
// See https://linux.die.net/man/3/poll // See https://linux.die.net/man/3/poll
var pollOneoff = newHostFunc( var pollOneoff = newHostFunc(
wasip1.PollOneoffName, pollOneoffFn, wasip1.PollOneoffName, pollOneoffFn,
[]api.ValueType{i32, i32, i32, i32}, []wasm.ValueType{i32, i32, i32, i32},
"in", "out", "nsubscriptions", "result.nevents", "in", "out", "nsubscriptions", "result.nevents",
) )
+2 -2
View File
@@ -21,7 +21,7 @@ import (
var procExit = &wasm.HostFunc{ var procExit = &wasm.HostFunc{
ExportName: wasip1.ProcExitName, ExportName: wasip1.ProcExitName,
Name: wasip1.ProcExitName, Name: wasip1.ProcExitName,
ParamTypes: []api.ValueType{i32}, ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"rval"}, ParamNames: []string{"rval"},
Code: wasm.Code{GoFunc: api.GoModuleFunc(procExitFn)}, Code: wasm.Code{GoFunc: api.GoModuleFunc(procExitFn)},
} }
@@ -41,4 +41,4 @@ func procExitFn(ctx context.Context, mod api.Module, params []uint64) {
// procRaise is stubbed and will never be supported, as it was removed. // procRaise is stubbed and will never be supported, as it was removed.
// //
// See https://github.com/WebAssembly/WASI/pull/136 // See https://github.com/WebAssembly/WASI/pull/136
var procRaise = stubFunction(wasip1.ProcRaiseName, []api.ValueType{i32}, "sig") var procRaise = stubFunction(wasip1.ProcRaiseName, []wasm.ValueType{i32}, "sig")
+1 -1
View File
@@ -34,7 +34,7 @@ import (
// buf --^ // buf --^
// //
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-bufLen-size---errno // See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#-random_getbuf-pointeru8-bufLen-size---errno
var randomGet = newHostFunc(wasip1.RandomGetName, randomGetFn, []api.ValueType{i32, i32}, "buf", "buf_len") var randomGet = newHostFunc(wasip1.RandomGetName, randomGetFn, []wasm.ValueType{i32, i32}, "buf", "buf_len")
func randomGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno { func randomGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
sysCtx := mod.(*wasm.ModuleInstance).Sys sysCtx := mod.(*wasm.ModuleInstance).Sys
+3 -3
View File
@@ -269,7 +269,7 @@ func writeOffsetsAndNullTerminatedValues(mem api.Memory, values [][]byte, offset
func newHostFunc( func newHostFunc(
name string, name string,
goFunc wasiFunc, goFunc wasiFunc,
paramTypes []api.ValueType, paramTypes []wasm.ValueType,
paramNames ...string, paramNames ...string,
) *wasm.HostFunc { ) *wasm.HostFunc {
return &wasm.HostFunc{ return &wasm.HostFunc{
@@ -277,7 +277,7 @@ func newHostFunc(
Name: name, Name: name,
ParamTypes: paramTypes, ParamTypes: paramTypes,
ParamNames: paramNames, ParamNames: paramNames,
ResultTypes: []api.ValueType{i32}, ResultTypes: []wasm.ValueType{i32},
ResultNames: []string{"errno"}, ResultNames: []string{"errno"},
Code: wasm.Code{GoFunc: goFunc}, Code: wasm.Code{GoFunc: goFunc},
} }
@@ -305,7 +305,7 @@ func stubFunction(name string, paramTypes []wasm.ValueType, paramNames ...string
Name: name, Name: name,
ParamTypes: paramTypes, ParamTypes: paramTypes,
ParamNames: paramNames, ParamNames: paramNames,
ResultTypes: []api.ValueType{i32}, ResultTypes: []wasm.ValueType{i32},
ResultNames: []string{"errno"}, ResultNames: []string{"errno"},
Code: wasm.Code{ Code: wasm.Code{
GoFunc: api.GoModuleFunc(func(_ context.Context, _ api.Module, stack []uint64) { stack[0] = uint64(wasip1.ErrnoNosys) }), GoFunc: api.GoModuleFunc(func(_ context.Context, _ api.Module, stack []uint64) { stack[0] = uint64(wasip1.ErrnoNosys) }),
+4 -4
View File
@@ -63,9 +63,9 @@ const InvokePrefix = "invoke_"
func NewInvokeFunc(importName string, params, results []api.ValueType) *wasm.HostFunc { func NewInvokeFunc(importName string, params, results []api.ValueType) *wasm.HostFunc {
// The type we invoke is the same type as the import except without the // The type we invoke is the same type as the import except without the
// index parameter. // index parameter.
fn := &InvokeFunc{&wasm.FunctionType{Results: results}} fn := &InvokeFunc{&wasm.FunctionType{Results: wasm.FromApiValueType(results)}}
if len(params) > 1 { if len(params) > 1 {
fn.FunctionType.Params = params[1:] fn.FunctionType.Params = wasm.FromApiValueType(params[1:])
} }
// Now, make friendly parameter names. // Now, make friendly parameter names.
@@ -76,9 +76,9 @@ func NewInvokeFunc(importName string, params, results []api.ValueType) *wasm.Hos
} }
return &wasm.HostFunc{ return &wasm.HostFunc{
ExportName: importName, ExportName: importName,
ParamTypes: params, ParamTypes: wasm.FromApiValueType(params),
ParamNames: paramNames, ParamNames: paramNames,
ResultTypes: results, ResultTypes: wasm.FromApiValueType(results),
Code: wasm.Code{GoFunc: fn}, Code: wasm.Code{GoFunc: fn},
} }
} }
+7 -7
View File
@@ -740,7 +740,7 @@ func TestCompile_Refs(t *testing.T) {
{ {
name: "ref.null (externref)", name: "ref.null (externref)",
body: []byte{ body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeExternref, wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeDrop, wasm.OpcodeDrop,
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
@@ -753,7 +753,7 @@ func TestCompile_Refs(t *testing.T) {
{ {
name: "ref.null (funcref)", name: "ref.null (funcref)",
body: []byte{ body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeFuncref, wasm.OpcodeRefNull, wasm.ValueTypeFuncref.Kind(),
wasm.OpcodeDrop, wasm.OpcodeDrop,
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
@@ -781,7 +781,7 @@ func TestCompile_Refs(t *testing.T) {
{ {
name: "ref.is_null (externref)", name: "ref.is_null (externref)",
body: []byte{ body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeExternref, wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeRefIsNull, wasm.OpcodeRefIsNull,
wasm.OpcodeDrop, wasm.OpcodeDrop,
wasm.OpcodeEnd, wasm.OpcodeEnd,
@@ -838,7 +838,7 @@ func TestCompile_TableGetOrSet(t *testing.T) {
name: "table.set (externref)", name: "table.set (externref)",
body: []byte{ body: []byte{
wasm.OpcodeI32Const, 10, wasm.OpcodeI32Const, 10,
wasm.OpcodeRefNull, wasm.ValueTypeExternref, wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeTableSet, 0, wasm.OpcodeTableSet, 0,
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
@@ -894,7 +894,7 @@ func TestCompile_TableGrowFillSize(t *testing.T) {
{ {
name: "table.grow", name: "table.grow",
body: []byte{ body: []byte{
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(),
wasm.OpcodeI32Const, 1, wasm.OpcodeI32Const, 1,
wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableGrow, 1, wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableGrow, 1,
wasm.OpcodeEnd, wasm.OpcodeEnd,
@@ -911,7 +911,7 @@ func TestCompile_TableGrowFillSize(t *testing.T) {
name: "table.fill", name: "table.fill",
body: []byte{ body: []byte{
wasm.OpcodeI32Const, 10, wasm.OpcodeI32Const, 10,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(),
wasm.OpcodeI32Const, 1, wasm.OpcodeI32Const, 1,
wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableFill, 1, wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableFill, 1,
wasm.OpcodeEnd, wasm.OpcodeEnd,
@@ -2817,7 +2817,7 @@ func TestCompile_select_vectors(t *testing.T) {
wasm.OpcodeVecPrefix, wasm.OpcodeVecPrefix,
wasm.OpcodeVecV128Const, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, wasm.OpcodeVecV128Const, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
wasm.OpcodeI32Const, 0, wasm.OpcodeI32Const, 0,
wasm.OpcodeTypedSelect, 0x1, wasm.ValueTypeV128, wasm.OpcodeTypedSelect, 0x1, wasm.ValueTypeV128.Kind(),
wasm.OpcodeDrop, wasm.OpcodeDrop,
wasm.OpcodeEnd, wasm.OpcodeEnd,
}}}, }}},
@@ -345,7 +345,7 @@ var (
wasm.OpcodeLocalGet, 2, // x wasm.OpcodeLocalGet, 2, // x
wasm.OpcodeLocalGet, 3, // y wasm.OpcodeLocalGet, 3, // y
wasm.OpcodeLocalGet, 1, // cond wasm.OpcodeLocalGet, 1, // cond
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeI64, wasm.OpcodeTypedSelect, 1, wasm.ValueTypeI64.Kind(),
// f32 select. // f32 select.
wasm.OpcodeLocalGet, 4, // x wasm.OpcodeLocalGet, 4, // x
@@ -354,7 +354,7 @@ var (
wasm.OpcodeLocalGet, 6, wasm.OpcodeLocalGet, 6,
wasm.OpcodeLocalGet, 7, wasm.OpcodeLocalGet, 7,
wasm.OpcodeF64Gt, wasm.OpcodeF64Gt,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF32, wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF32.Kind(),
// f64 select. // f64 select.
wasm.OpcodeLocalGet, 6, // x wasm.OpcodeLocalGet, 6, // x
@@ -363,7 +363,7 @@ var (
wasm.OpcodeLocalGet, 4, wasm.OpcodeLocalGet, 4,
wasm.OpcodeLocalGet, 5, wasm.OpcodeLocalGet, 5,
wasm.OpcodeF32Ne, wasm.OpcodeF32Ne,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF64, wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF64.Kind(),
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, nil), }, nil),
@@ -155,8 +155,8 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance {
// Build the importing module. // Build the importing module.
importingModuleBin := binaryencoding.EncodeModule(&wasm.Module{ importingModuleBin := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{ TypeSection: []wasm.FunctionType{{
Params: []wasm.ValueType{i32}, Params: []wasm.ValueType{wasm.ValueType(i32)},
Results: []wasm.ValueType{f32}, Results: []wasm.ValueType{wasm.ValueType(f32)},
}}, }},
ImportSection: []wasm.Import{ ImportSection: []wasm.Import{
// Placeholders for imports from hostModule. // Placeholders for imports from hostModule.
+17 -17
View File
@@ -952,31 +952,31 @@ func testLookupFunction(t *testing.T, r wazero.Runtime) {
t.Run("null reference", func(t *testing.T) { t.Run("null reference", func(t *testing.T) {
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(inst, 0, 3, nil, []wasm.ValueType{i32}) table.LookupFunction(inst, 0, 3, nil, wasm.ToApiValueType([]wasm.ValueType{i32}))
}) })
require.Equal(t, wasmruntime.ErrRuntimeInvalidTableAccess, err) require.Equal(t, wasmruntime.ErrRuntimeInvalidTableAccess, err)
}) })
t.Run("out of range", func(t *testing.T) { t.Run("out of range", func(t *testing.T) {
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(inst, 0, 1000, nil, []wasm.ValueType{i32}) table.LookupFunction(inst, 0, 1000, nil, wasm.ToApiValueType([]wasm.ValueType{i32}))
}) })
require.Equal(t, wasmruntime.ErrRuntimeInvalidTableAccess, err) require.Equal(t, wasmruntime.ErrRuntimeInvalidTableAccess, err)
}) })
t.Run("type mismatch", func(t *testing.T) { t.Run("type mismatch", func(t *testing.T) {
err = require.CapturePanic(func() { err = require.CapturePanic(func() {
table.LookupFunction(inst, 0, 0, []wasm.ValueType{i32}, nil) table.LookupFunction(inst, 0, 0, wasm.ToApiValueType([]wasm.ValueType{i32}), nil)
}) })
require.Equal(t, wasmruntime.ErrRuntimeIndirectCallTypeMismatch, err) require.Equal(t, wasmruntime.ErrRuntimeIndirectCallTypeMismatch, err)
}) })
t.Run("ok", func(t *testing.T) { t.Run("ok", func(t *testing.T) {
f2 := table.LookupFunction(inst, 0, 0, nil, []wasm.ValueType{i32}) f2 := table.LookupFunction(inst, 0, 0, nil, wasm.ToApiValueType([]wasm.ValueType{i32}))
res, err := f2.Call(testCtx) res, err := f2.Call(testCtx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, uint64(3), res[0]) require.Equal(t, uint64(3), res[0])
f0 := table.LookupFunction(inst, 0, 1, nil, []wasm.ValueType{i32}) f0 := table.LookupFunction(inst, 0, 1, nil, wasm.ToApiValueType([]wasm.ValueType{i32}))
res, err = f0.Call(testCtx) res, err = f0.Call(testCtx)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, uint64(1), res[0]) require.Equal(t, uint64(1), res[0])
@@ -1103,7 +1103,7 @@ func testModuleMemory(t *testing.T, r wazero.Runtime) {
one := uint32(1) one := uint32(1)
bin := binaryencoding.EncodeModule(&wasm.Module{ bin := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}, ParamNumInUint64: 1}, {}}, TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}, ParamNumInUint64: 1}, {}},
FunctionSection: []wasm.Index{0, 1}, FunctionSection: []wasm.Index{0, 1},
MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 20}, MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 20},
DataSection: []wasm.DataSegment{ DataSection: []wasm.DataSegment{
@@ -1445,23 +1445,23 @@ func testBeforeListenerStackIterator(t *testing.T, r wazero.Runtime) {
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
// f1 type // f1 type
{ {
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}, Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
Results: []api.ValueType{}, Results: []wasm.ValueType{},
}, },
// f2 type // f2 type
{ {
Params: []api.ValueType{}, Params: []wasm.ValueType{},
Results: []api.ValueType{api.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32},
}, },
// f3 type // f3 type
{ {
Params: []api.ValueType{api.ValueTypeI32}, Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32},
}, },
// f4 type // f4 type
{ {
Params: []api.ValueType{api.ValueTypeI32}, Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32},
}, },
}, },
ImportFunctionCount: 1, ImportFunctionCount: 1,
@@ -1551,11 +1551,11 @@ func testListenerStackIteratorOffset(t *testing.T, r wazero.Runtime) {
encoded := binaryencoding.EncodeModule(&wasm.Module{ encoded := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{ TypeSection: []wasm.FunctionType{
// f1 type // f1 type
{Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}}, {Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32}},
// f2 type // f2 type
{Results: []api.ValueType{api.ValueTypeI32}}, {Results: []wasm.ValueType{wasm.ValueTypeI32}},
// f3 type // f3 type
{Params: []api.ValueType{api.ValueTypeI32}, Results: []api.ValueType{api.ValueTypeI32}}, {Params: []wasm.ValueType{wasm.ValueTypeI32}, Results: []wasm.ValueType{wasm.ValueTypeI32}},
}, },
FunctionSection: []wasm.Index{0, 1, 2}, FunctionSection: []wasm.Index{0, 1, 2},
NameSection: &wasm.NameSection{ NameSection: &wasm.NameSection{
@@ -141,9 +141,9 @@ func encodeModule(m *wasm.Module) []byte {
for _, ft := range m.TypeSection { for _, ft := range m.TypeSection {
s = append(s, 0x60) // func type s = append(s, 0x60) // func type
s = appendUleb128(s, uint32(len(ft.Params))) s = appendUleb128(s, uint32(len(ft.Params)))
s = append(s, ft.Params...) s = append(s, wasm.ToApiValueType(ft.Params)...)
s = appendUleb128(s, uint32(len(ft.Results))) s = appendUleb128(s, uint32(len(ft.Results)))
s = append(s, ft.Results...) s = append(s, wasm.ToApiValueType(ft.Results)...)
} }
return s return s
}) })
@@ -373,7 +373,7 @@ func Test888(t *testing.T) {
ValType: wasm.ValueTypeFuncref, ValType: wasm.ValueTypeFuncref,
Mutable: false, Mutable: false,
}, },
Init: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.ValueTypeFuncref}), Init: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.ValueTypeFuncref.Kind()}),
}, },
}, },
ExportSection: []wasm.Export{ ExportSection: []wasm.Export{
@@ -447,7 +447,7 @@ func RunCase(t *testing.T, testDataFS embed.FS, f string, ctx context.Context, c
skipIndices[i] = true skipIndices[i] = true
} }
} }
matched, valuesMsg := valuesEq(results, exps, fn.Definition().ResultTypes(), laneTypes, skipIndices) matched, valuesMsg := valuesEq(results, exps, wasm.FromApiValueType(fn.Definition().ResultTypes()), laneTypes, skipIndices)
require.True(t, matched, msg+"\n"+valuesMsg) require.True(t, matched, msg+"\n"+valuesMsg)
case "get": case "get":
_, exps := c.getAssertReturnArgsExps() _, exps := c.getAssertReturnArgsExps()
+8 -8
View File
@@ -46,9 +46,9 @@ func TestEncodeCode(t *testing.T) {
Body: addLocalZeroLocalOne, Body: addLocalZeroLocalOne,
}, },
expected: append([]byte{ expected: append([]byte{
0x09, // 9 bytes to encode locals and the body 0x09, // 9 bytes to encode locals and the body
0x01, // 1 local block 0x01, // 1 local block
0x02, wasm.ValueTypeI32, // local block 1 0x02, wasm.ValueTypeI32.Kind(), // local block 1
}, },
addLocalZeroLocalOne..., // Body addLocalZeroLocalOne..., // Body
), ),
@@ -60,11 +60,11 @@ func TestEncodeCode(t *testing.T) {
Body: addLocalZeroLocalTwo, Body: addLocalZeroLocalTwo,
}, },
expected: append([]byte{ expected: append([]byte{
0x0d, // 13 bytes to encode locals and the body 0x0d, // 13 bytes to encode locals and the body
0x03, // 3 local blocks 0x03, // 3 local blocks
0x01, wasm.ValueTypeI32, // local block 1 0x01, wasm.ValueTypeI32.Kind(), // local block 1
0x01, wasm.ValueTypeI64, // local block 2 0x01, wasm.ValueTypeI64.Kind(), // local block 2
0x01, wasm.ValueTypeI32, // local block 3 0x01, wasm.ValueTypeI32.Kind(), // local block 3
}, },
addLocalZeroLocalTwo..., // Body addLocalZeroLocalTwo..., // Body
), ),
+10 -10
View File
@@ -44,8 +44,8 @@ func TestModule_Encode(t *testing.T) {
wasm.SectionIDType, 0x12, // 18 bytes in this section wasm.SectionIDType, 0x12, // 18 bytes in this section
0x03, // 3 types 0x03, // 3 types
0x60, 0x00, 0x00, // func=0x60 no param no result 0x60, 0x00, 0x00, // func=0x60 no param no result
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result 0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 2 params and 1 result
0x60, 0x04, i32, i32, i32, i32, 0x01, i32, // func=0x60 4 params and 1 result 0x60, 0x04, i32.Kind(), i32.Kind(), i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 4 params and 1 result
), ),
}, },
{ {
@@ -69,9 +69,9 @@ func TestModule_Encode(t *testing.T) {
}, },
expected: append(append(Magic, version...), expected: append(append(Magic, version...),
wasm.SectionIDType, 0x0d, // 13 bytes in this section wasm.SectionIDType, 0x0d, // 13 bytes in this section
0x02, // 2 types 0x02, // 2 types
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result 0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 2 params and 1 result
0x60, 0x02, f32, f32, 0x01, f32, // func=0x60 2 params and 1 result 0x60, 0x02, f32.Kind(), f32.Kind(), 0x01, f32.Kind(), // func=0x60 2 params and 1 result
wasm.SectionIDImport, 0x17, // 23 bytes in this section wasm.SectionIDImport, 0x17, // 23 bytes in this section
0x02, // 2 imports 0x02, // 2 imports
0x04, 'M', 'a', 't', 'h', 0x03, 'M', 'u', 'l', wasm.ExternTypeFunc, 0x04, 'M', 'a', 't', 'h', 0x03, 'M', 'u', 'l', wasm.ExternTypeFunc,
@@ -111,8 +111,8 @@ func TestModule_Encode(t *testing.T) {
}, },
expected: append(append(Magic, version...), expected: append(append(Magic, version...),
wasm.SectionIDTable, 0x04, // 4 bytes in this section wasm.SectionIDTable, 0x04, // 4 bytes in this section
0x01, // 1 table 0x01, // 1 table
wasm.RefTypeFuncref, 0x0, 0x03, // func, only min: 3 wasm.RefTypeFuncref.Kind(), 0x0, 0x03, // func, only min: 3
wasm.SectionIDMemory, 0x04, // 4 bytes in this section wasm.SectionIDMemory, 0x04, // 4 bytes in this section
0x01, // 1 memory 0x01, // 1 memory
0x01, 0x01, 0x01, // min and max = 1 0x01, 0x01, 0x01, // min and max = 1
@@ -143,8 +143,8 @@ func TestModule_Encode(t *testing.T) {
}, },
expected: append(append(Magic, version...), expected: append(append(Magic, version...),
wasm.SectionIDType, 0x07, // 7 bytes in this section wasm.SectionIDType, 0x07, // 7 bytes in this section
0x01, // 1 type 0x01, // 1 type
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result 0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 2 params and 1 result
wasm.SectionIDFunction, 0x02, // 2 bytes in this section wasm.SectionIDFunction, 0x02, // 2 bytes in this section
0x01, // 1 function 0x01, // 1 function
0x00, // func[0] type index 0 0x00, // func[0] type index 0
@@ -187,7 +187,7 @@ func TestModule_Encode(t *testing.T) {
}, },
expected: append(append(Magic, version...), expected: append(append(Magic, version...),
wasm.SectionIDGlobal, 0x06, // 6 bytes in this section wasm.SectionIDGlobal, 0x06, // 6 bytes in this section
0x01, wasm.ValueTypeI32, 0x01, // 1 global i32 mutable 0x01, wasm.ValueTypeI32.Kind(), 0x01, // 1 global i32 mutable
wasm.OpcodeI32Const, 0x00, wasm.OpcodeEnd, // arbitrary init to zero wasm.OpcodeI32Const, 0x00, wasm.OpcodeEnd, // arbitrary init to zero
wasm.SectionIDExport, 0x06, // 6 bytes in this section wasm.SectionIDExport, 0x06, // 6 bytes in this section
0x01, // 1 export 0x01, // 1 export
+1 -1
View File
@@ -12,7 +12,7 @@ func encodeGlobal(g wasm.Global) (data []byte) {
if g.Type.Mutable { if g.Type.Mutable {
mutable = 1 mutable = 1
} }
data = []byte{g.Type.ValType, mutable} data = []byte{g.Type.ValType.Kind(), mutable}
data = append(data, encodeConstantExpression(g.Init)...) data = append(data, encodeConstantExpression(g.Init)...)
return return
} }
@@ -20,7 +20,7 @@ func TestEncodeGlobal(t *testing.T) {
Init: wasm.NewConstantExpressionFromI32(1), Init: wasm.NewConstantExpressionFromI32(1),
}, },
expected: []byte{ expected: []byte{
wasm.ValueTypeI32, 0x00, // 0 == const wasm.ValueTypeI32.Kind(), 0x00, // 0 == const
wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
}, },
}, },
@@ -31,7 +31,7 @@ func TestEncodeGlobal(t *testing.T) {
Init: wasm.NewConstantExpressionFromI32(1), Init: wasm.NewConstantExpressionFromI32(1),
}, },
expected: []byte{ expected: []byte{
wasm.ValueTypeI32, 0x01, // 1 == var wasm.ValueTypeI32.Kind(), 0x01, // 1 == var
wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
}, },
}, },
+2 -2
View File
@@ -18,7 +18,7 @@ func EncodeImport(i *wasm.Import) []byte {
case wasm.ExternTypeFunc: case wasm.ExternTypeFunc:
data = append(data, leb128.EncodeUint32(i.DescFunc)...) data = append(data, leb128.EncodeUint32(i.DescFunc)...)
case wasm.ExternTypeTable: case wasm.ExternTypeTable:
data = append(data, wasm.RefTypeFuncref) data = append(data, wasm.RefTypeFuncref.Kind())
data = append(data, EncodeLimitsType(i.DescTable.Min, i.DescTable.Max, false)...) data = append(data, EncodeLimitsType(i.DescTable.Min, i.DescTable.Max, false)...)
case wasm.ExternTypeMemory: case wasm.ExternTypeMemory:
maxPtr := &i.DescMem.Max maxPtr := &i.DescMem.Max
@@ -32,7 +32,7 @@ func EncodeImport(i *wasm.Import) []byte {
if g.Mutable { if g.Mutable {
mutable = 1 mutable = 1
} }
data = append(data, g.ValType, mutable) data = append(data, g.ValType.Kind(), mutable)
case wasm.ExternTypeTag: case wasm.ExternTypeTag:
data = append(data, 0x00) // attribute byte data = append(data, 0x00) // attribute byte
data = append(data, leb128.EncodeUint32(i.DescTag)...) data = append(data, leb128.EncodeUint32(i.DescTag)...)
@@ -84,7 +84,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h', 0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i', 0x02, 'p', 'i',
wasm.ExternTypeGlobal, wasm.ExternTypeGlobal,
wasm.ValueTypeF64, 0x00, // 0 == const wasm.ValueTypeF64.Kind(), 0x00, // 0 == const
}, },
}, },
{ {
@@ -99,7 +99,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h', 0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i', 0x02, 'p', 'i',
wasm.ExternTypeGlobal, wasm.ExternTypeGlobal,
wasm.ValueTypeF64, 0x01, // 1 == var wasm.ValueTypeF64.Kind(), 0x01, // 1 == var
}, },
}, },
{ {
@@ -114,7 +114,7 @@ func TestEncodeImport(t *testing.T) {
0x02, 'm', 'y', 0x02, 'm', 'y',
0x05, 't', 'a', 'b', 'l', 'e', 0x05, 't', 'a', 'b', 'l', 'e',
wasm.ExternTypeTable, wasm.ExternTypeTable,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
0x1, 0x1, 0x2, // Limit with max. 0x1, 0x1, 0x2, // Limit with max.
}, },
}, },
+1 -1
View File
@@ -8,5 +8,5 @@ import (
// //
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
func EncodeTable(i *wasm.Table) []byte { func EncodeTable(i *wasm.Table) []byte {
return append([]byte{i.Type}, EncodeLimitsType(i.Min, i.Max, false)...) return append([]byte{i.Type.Kind()}, EncodeLimitsType(i.Min, i.Max, false)...)
} }
+12 -11
View File
@@ -9,13 +9,13 @@ var noValType = []byte{0}
// encodedValTypes is a cache of size prefixed binary encoding of known val types. // encodedValTypes is a cache of size prefixed binary encoding of known val types.
var encodedValTypes = map[wasm.ValueType][]byte{ var encodedValTypes = map[wasm.ValueType][]byte{
wasm.ValueTypeI32: {1, wasm.ValueTypeI32}, wasm.ValueTypeI32: {1, wasm.ValueTypeI32.Kind()},
wasm.ValueTypeI64: {1, wasm.ValueTypeI64}, wasm.ValueTypeI64: {1, wasm.ValueTypeI64.Kind()},
wasm.ValueTypeF32: {1, wasm.ValueTypeF32}, wasm.ValueTypeF32: {1, wasm.ValueTypeF32.Kind()},
wasm.ValueTypeF64: {1, wasm.ValueTypeF64}, wasm.ValueTypeF64: {1, wasm.ValueTypeF64.Kind()},
wasm.ValueTypeExternref: {1, wasm.ValueTypeExternref}, wasm.ValueTypeExternref: {1, wasm.ValueTypeExternref.Kind()},
wasm.ValueTypeFuncref: {1, wasm.ValueTypeFuncref}, wasm.ValueTypeFuncref: {1, wasm.ValueTypeFuncref.Kind()},
wasm.ValueTypeV128: {1, wasm.ValueTypeV128}, wasm.ValueTypeV128: {1, wasm.ValueTypeV128.Kind()},
} }
// EncodeValTypes fast paths binary encoding of common value type lengths // EncodeValTypes fast paths binary encoding of common value type lengths
@@ -29,13 +29,14 @@ func EncodeValTypes(vt []wasm.ValueType) []byte {
return encoded return encoded
} }
case 2: // ex $wasi.environ_sizes_get case 2: // ex $wasi.environ_sizes_get
return []byte{2, vt[0], vt[1]} return []byte{2, vt[0].Kind(), vt[1].Kind()}
case 4: // ex $wasi.fd_write case 4: // ex $wasi.fd_write
return []byte{4, vt[0], vt[1], vt[2], vt[3]} return []byte{4, vt[0].Kind(), vt[1].Kind(), vt[2].Kind(), vt[3].Kind()}
case 9: // ex $wasi.fd_write case 9: // ex $wasi.fd_write
return []byte{9, vt[0], vt[1], vt[2], vt[3], vt[4], vt[5], vt[6], vt[7], vt[8]} return []byte{9, vt[0].Kind(), vt[1].Kind(), vt[2].Kind(), vt[3].Kind(), vt[4].Kind(), vt[5].Kind(), vt[6].Kind(), vt[7].Kind(), vt[8].Kind()}
} }
// Slow path others until someone complains with a valid signature // Slow path others until someone complains with a valid signature
count := leb128.EncodeUint32(uint32(len(vt))) count := leb128.EncodeUint32(uint32(len(vt)))
return append(count, vt...) avt := wasm.ToApiValueType(vt)
return append(count, avt...)
} }
+15 -15
View File
@@ -27,77 +27,77 @@ func TestEncodeValTypes(t *testing.T) {
{ {
name: "funcref", name: "funcref",
input: []wasm.ValueType{fref}, input: []wasm.ValueType{fref},
expected: []byte{1, fref}, expected: []byte{1, fref.Kind()},
}, },
{ {
name: "externref", name: "externref",
input: []wasm.ValueType{ext}, input: []wasm.ValueType{ext},
expected: []byte{1, ext}, expected: []byte{1, ext.Kind()},
}, },
{ {
name: "i32", name: "i32",
input: []wasm.ValueType{i32}, input: []wasm.ValueType{i32},
expected: []byte{1, i32}, expected: []byte{1, i32.Kind()},
}, },
{ {
name: "i64", name: "i64",
input: []wasm.ValueType{i64}, input: []wasm.ValueType{i64},
expected: []byte{1, i64}, expected: []byte{1, i64.Kind()},
}, },
{ {
name: "f32", name: "f32",
input: []wasm.ValueType{f32}, input: []wasm.ValueType{f32},
expected: []byte{1, f32}, expected: []byte{1, f32.Kind()},
}, },
{ {
name: "f64", name: "f64",
input: []wasm.ValueType{f64}, input: []wasm.ValueType{f64},
expected: []byte{1, f64}, expected: []byte{1, f64.Kind()},
}, },
{ {
name: "i32i64", name: "i32i64",
input: []wasm.ValueType{i32, i64}, input: []wasm.ValueType{i32, i64},
expected: []byte{2, i32, i64}, expected: []byte{2, i32.Kind(), i64.Kind()},
}, },
{ {
name: "i32i64f32", name: "i32i64f32",
input: []wasm.ValueType{i32, i64, f32}, input: []wasm.ValueType{i32, i64, f32},
expected: []byte{3, i32, i64, f32}, expected: []byte{3, i32.Kind(), i64.Kind(), f32.Kind()},
}, },
{ {
name: "i32i64f32f64", name: "i32i64f32f64",
input: []wasm.ValueType{i32, i64, f32, f64}, input: []wasm.ValueType{i32, i64, f32, f64},
expected: []byte{4, i32, i64, f32, f64}, expected: []byte{4, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind()},
}, },
{ {
name: "i32i64f32f64i32", name: "i32i64f32f64i32",
input: []wasm.ValueType{i32, i64, f32, f64, i32}, input: []wasm.ValueType{i32, i64, f32, f64, i32},
expected: []byte{5, i32, i64, f32, f64, i32}, expected: []byte{5, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64", name: "i32i64f32f64i32i64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64},
expected: []byte{6, i32, i64, f32, f64, i32, i64}, expected: []byte{6, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32", name: "i32i64f32f64i32i64f32",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32},
expected: []byte{7, i32, i64, f32, f64, i32, i64, f32}, expected: []byte{7, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64", name: "i32i64f32f64i32i64f32f64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64},
expected: []byte{8, i32, i64, f32, f64, i32, i64, f32, f64}, expected: []byte{8, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64i32", name: "i32i64f32f64i32i64f32f64i32",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32},
expected: []byte{9, i32, i64, f32, f64, i32, i64, f32, f64, i32}, expected: []byte{9, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64i32i64", name: "i32i64f32f64i32i64f32f64i32i64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32, i64},
expected: []byte{10, i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, expected: []byte{10, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind()},
}, },
} }
+4 -4
View File
@@ -214,9 +214,9 @@ func ensureDummyImports(r wazero.Runtime, origin *wasm.Module, requireNoError fu
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
}) })
case wasm.ValueTypeExternref: case wasm.ValueTypeExternref:
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeExternref}) body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind()})
case wasm.ValueTypeFuncref: case wasm.ValueTypeFuncref:
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref}) body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind()})
} }
} }
body.WriteByte(wasm.OpcodeEnd) body.WriteByte(wasm.OpcodeEnd)
@@ -243,10 +243,10 @@ func ensureDummyImports(r wazero.Runtime, origin *wasm.Module, requireNoError fu
data = []byte{wasm.OpcodeVecV128Const, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} data = []byte{wasm.OpcodeVecV128Const, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
case wasm.ValueTypeExternref: case wasm.ValueTypeExternref:
opcode = wasm.OpcodeRefNull opcode = wasm.OpcodeRefNull
data = []byte{wasm.RefTypeExternref} data = []byte{wasm.RefTypeExternref.Kind()}
case wasm.ValueTypeFuncref: case wasm.ValueTypeFuncref:
opcode = wasm.OpcodeRefNull opcode = wasm.OpcodeRefNull
data = []byte{wasm.RefTypeFuncref} data = []byte{wasm.RefTypeFuncref.Kind()}
} }
m.GlobalSection = append(m.GlobalSection, wasm.Global{ m.GlobalSection = append(m.GlobalSection, wasm.Global{
Type: imp.DescGlobal, Init: wasm.NewConstantExpressionFromOpcode(opcode, data), Type: imp.DescGlobal, Init: wasm.NewConstantExpressionFromOpcode(opcode, data),
+1 -1
View File
@@ -49,7 +49,7 @@ func NewModuleBinary(moduleName string, proxyTarget wazero.CompiledModule) []byt
var cnt wasm.Index var cnt wasm.Index
for _, def := range funcDefs { for _, def := range funcDefs {
proxyModule.TypeSection = append(proxyModule.TypeSection, wasm.FunctionType{ proxyModule.TypeSection = append(proxyModule.TypeSection, wasm.FunctionType{
Params: def.ParamTypes(), Results: def.ResultTypes(), Params: wasm.FromApiValueType(def.ParamTypes()), Results: wasm.FromApiValueType(def.ResultTypes()),
}) })
// Imports the function. // Imports the function.
+2 -2
View File
@@ -45,7 +45,7 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64, ret *wasm.Code) (err e
} }
bytesRead += n + 1 bytesRead += n + 1
switch vt := b; vt { switch vt := b; wasm.ValueType(vt) {
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64, case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64,
wasm.ValueTypeFuncref, wasm.ValueTypeExternref, wasm.ValueTypeV128, wasm.ValueTypeFuncref, wasm.ValueTypeExternref, wasm.ValueTypeV128,
wasm.ValueTypeExnref: wasm.ValueTypeExnref:
@@ -80,7 +80,7 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64, ret *wasm.Code) (err e
} }
for j := uint32(0); j < num; j++ { for j := uint32(0); j < num; j++ {
localTypes = append(localTypes, b) localTypes = append(localTypes, wasm.ValueType(b))
} }
} }
+2 -1
View File
@@ -55,7 +55,8 @@ func decodeConstantExpression(r *bytes.Reader, enabledFeatures api.CoreFeatures,
if err := enabledFeatures.RequireEnabled(api.CoreFeatureBulkMemoryOperations); err != nil { if err := enabledFeatures.RequireEnabled(api.CoreFeatureBulkMemoryOperations); err != nil {
return fmt.Errorf("ref.null is not supported as %w", err) return fmt.Errorf("ref.null is not supported as %w", err)
} }
reftype, err := r.ReadByte() b, err := r.ReadByte()
reftype := wasm.ValueType(b)
if err != nil { if err != nil {
return fmt.Errorf("read reference type for ref.null: %w", err) return fmt.Errorf("read reference type for ref.null: %w", err)
} else if reftype != wasm.RefTypeFuncref && reftype != wasm.RefTypeExternref { } else if reftype != wasm.RefTypeFuncref && reftype != wasm.RefTypeExternref {
+5 -5
View File
@@ -35,18 +35,18 @@ func TestDecodeConstantExpression(t *testing.T) {
{ {
in: []byte{ in: []byte{
wasm.OpcodeRefNull, wasm.OpcodeRefNull,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
{ {
in: []byte{ in: []byte{
wasm.OpcodeRefNull, wasm.OpcodeRefNull,
wasm.RefTypeExternref, wasm.RefTypeExternref.Kind(),
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref}), exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref.Kind()}),
}, },
{ {
in: []byte{ in: []byte{
@@ -124,7 +124,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
{ {
in: []byte{ in: []byte{
wasm.OpcodeRefNull, wasm.OpcodeRefNull,
wasm.RefTypeExternref, wasm.RefTypeExternref.Kind(),
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
expectedErr: "ref.null is not supported as feature \"bulk-memory-operations\" is disabled", expectedErr: "ref.null is not supported as feature \"bulk-memory-operations\" is disabled",
+4 -3
View File
@@ -60,9 +60,10 @@ func decodeElementConstExprVector(r *bytes.Reader, elemType wasm.RefType, enable
} }
func decodeElementRefType(r *bytes.Reader) (ret wasm.RefType, err error) { func decodeElementRefType(r *bytes.Reader) (ret wasm.RefType, err error) {
ret, err = r.ReadByte() b, e := r.ReadByte()
if err != nil { ret = wasm.ValueType(b)
err = fmt.Errorf("read element ref type: %w", err) if e != nil {
err = fmt.Errorf("read element ref type: %w", e)
return return
} }
if ret != wasm.RefTypeFuncref && ret != wasm.RefTypeExternref { if ret != wasm.RefTypeFuncref && ret != wasm.RefTypeExternref {
+36 -36
View File
@@ -75,11 +75,11 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{ {
in: []byte{ in: []byte{
2, // Two indexes. 2, // Two indexes.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, 100, wasm.OpcodeEnd, wasm.OpcodeRefFunc, 100, wasm.OpcodeEnd,
}, },
exp: []wasm.ConstantExpression{ exp: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, []byte{100}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, []byte{100}),
}, },
refType: wasm.RefTypeFuncref, refType: wasm.RefTypeFuncref,
@@ -88,18 +88,18 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{ {
in: []byte{ in: []byte{
4, // Four indexes. 4, // Four indexes.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd, wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
exp: []wasm.ConstantExpression{ exp: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeGlobalGet, leb128.EncodeUint32(1)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeGlobalGet, leb128.EncodeUint32(1)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
refType: wasm.RefTypeFuncref, refType: wasm.RefTypeFuncref,
features: api.CoreFeatureBulkMemoryOperations, features: api.CoreFeatureBulkMemoryOperations,
@@ -107,14 +107,14 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{ {
in: []byte{ in: []byte{
3, // Three indexes. 3, // Three indexes.
wasm.OpcodeRefNull, wasm.RefTypeExternref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd, wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeExternref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind(), wasm.OpcodeEnd,
}, },
exp: []wasm.ConstantExpression{ exp: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeGlobalGet, leb128.EncodeUint32(1)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeGlobalGet, leb128.EncodeUint32(1)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref.Kind()}),
}, },
refType: wasm.RefTypeExternref, refType: wasm.RefTypeExternref,
features: api.CoreFeatureBulkMemoryOperations, features: api.CoreFeatureBulkMemoryOperations,
@@ -145,7 +145,7 @@ func Test_decodeElementConstExprVector_errors(t *testing.T) {
}, },
{ {
name: "feature", name: "feature",
in: []byte{1, wasm.OpcodeRefNull, wasm.RefTypeExternref, wasm.OpcodeEnd}, in: []byte{1, wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind(), wasm.OpcodeEnd},
expErr: "ref.null is not supported as feature \"bulk-memory-operations\" is disabled", expErr: "ref.null is not supported as feature \"bulk-memory-operations\" is disabled",
}, },
{ {
@@ -335,18 +335,18 @@ func TestDecodeElementSegment(t *testing.T) {
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
// Init const expr vector. // Init const expr vector.
3, // number of const expr. 3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
exp: wasm.ElementSegment{ exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}), OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
Init: []wasm.ConstantExpression{ Init: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
Mode: wasm.ElementModeActive, Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref, Type: wasm.RefTypeFuncref,
@@ -357,20 +357,20 @@ func TestDecodeElementSegment(t *testing.T) {
name: "passive const expr vector - funcref", name: "passive const expr vector - funcref",
in: []byte{ in: []byte{
5, // Prefix. 5, // Prefix.
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
// Init const expr vector. // Init const expr vector.
3, // number of const expr. 3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
exp: wasm.ElementSegment{ exp: wasm.ElementSegment{
Init: []wasm.ConstantExpression{ Init: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
Mode: wasm.ElementModePassive, Mode: wasm.ElementModePassive,
Type: wasm.RefTypeFuncref, Type: wasm.RefTypeFuncref,
@@ -393,21 +393,21 @@ func TestDecodeElementSegment(t *testing.T) {
0, 0,
// Offset expr. // Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
// Init const expr vector. // Init const expr vector.
3, // number of const expr. 3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
exp: wasm.ElementSegment{ exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}), OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
Init: []wasm.ConstantExpression{ Init: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
Mode: wasm.ElementModeActive, Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref, Type: wasm.RefTypeFuncref,
@@ -421,21 +421,21 @@ func TestDecodeElementSegment(t *testing.T) {
10, 10,
// Offset expr. // Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
// Init const expr vector. // Init const expr vector.
3, // number of const expr. 3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
exp: wasm.ElementSegment{ exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}), OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
Init: []wasm.ConstantExpression{ Init: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
}, },
Mode: wasm.ElementModeActive, Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref, Type: wasm.RefTypeFuncref,
@@ -450,14 +450,14 @@ func TestDecodeElementSegment(t *testing.T) {
10, 10,
// Offset expr. // Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd, wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
// Init const expr vector. // Init const expr vector.
3, // number of const expr. 3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x80, 0x80, 0x4f, // 165675008 in varint encoding. 0x80, 0x80, 0x80, 0x4f, // 165675008 in varint encoding.
wasm.OpcodeEnd, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
}, },
expErr: `table index must be zero but was 10: feature "reference-types" is disabled`, expErr: `table index must be zero but was 10: feature "reference-types" is disabled`,
features: api.CoreFeatureBulkMemoryOperations, features: api.CoreFeatureBulkMemoryOperations,
@@ -466,17 +466,17 @@ func TestDecodeElementSegment(t *testing.T) {
name: "declarative const expr vector", name: "declarative const expr vector",
in: []byte{ in: []byte{
7, // Prefix. 7, // Prefix.
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
// Init const expr vector. // Init const expr vector.
2, // number of const expr. 2, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd, wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, wasm.OpcodeRefFunc,
0x80, 0x7f, 0x80, 0x7f,
wasm.OpcodeEnd, wasm.OpcodeEnd,
}, },
exp: wasm.ElementSegment{ exp: wasm.ElementSegment{
Init: []wasm.ConstantExpression{ Init: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)), wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, leb128.EncodeUint32(16256)),
}, },
Mode: wasm.ElementModeDeclarative, Mode: wasm.ElementModeDeclarative,
+11 -11
View File
@@ -26,52 +26,52 @@ func TestFunctionType(t *testing.T) {
{ {
name: "one param no result", name: "one param no result",
input: wasm.FunctionType{Params: []wasm.ValueType{i32}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32}},
expected: []byte{0x60, 1, i32, 0}, expected: []byte{0x60, 1, i32.Kind(), 0},
}, },
{ {
name: "no param one result", name: "no param one result",
input: wasm.FunctionType{Results: []wasm.ValueType{i32}}, input: wasm.FunctionType{Results: []wasm.ValueType{i32}},
expected: []byte{0x60, 0, 1, i32}, expected: []byte{0x60, 0, 1, i32.Kind()},
}, },
{ {
name: "one param one result", name: "one param one result",
input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}}, input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
expected: []byte{0x60, 1, i64, 1, i32}, expected: []byte{0x60, 1, i64.Kind(), 1, i32.Kind()},
}, },
{ {
name: "two params no result", name: "two params no result",
input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}},
expected: []byte{0x60, 2, i32, i64, 0}, expected: []byte{0x60, 2, i32.Kind(), i64.Kind(), 0},
}, },
{ {
name: "two param one result", name: "two param one result",
input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32}},
expected: []byte{0x60, 2, i32, i64, 1, i32}, expected: []byte{0x60, 2, i32.Kind(), i64.Kind(), 1, i32.Kind()},
}, },
{ {
name: "no param two results", name: "no param two results",
input: wasm.FunctionType{Results: []wasm.ValueType{i32, i64}}, input: wasm.FunctionType{Results: []wasm.ValueType{i32, i64}},
expected: []byte{0x60, 0, 2, i32, i64}, expected: []byte{0x60, 0, 2, i32.Kind(), i64.Kind()},
}, },
{ {
name: "one param two results", name: "one param two results",
input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32, i64}}, input: wasm.FunctionType{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32, i64}},
expected: []byte{0x60, 1, i64, 2, i32, i64}, expected: []byte{0x60, 1, i64.Kind(), 2, i32.Kind(), i64.Kind()},
}, },
{ {
name: "two param two results", name: "two param two results",
input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32, i64}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32, i64}, Results: []wasm.ValueType{i32, i64}},
expected: []byte{0x60, 2, i32, i64, 2, i32, i64}, expected: []byte{0x60, 2, i32.Kind(), i64.Kind(), 2, i32.Kind(), i64.Kind()},
}, },
{ {
name: "two param two results with funcrefs", name: "two param two results with funcrefs",
input: wasm.FunctionType{Params: []wasm.ValueType{i32, funcRef}, Results: []wasm.ValueType{funcRef, i64}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32, funcRef}, Results: []wasm.ValueType{funcRef, i64}},
expected: []byte{0x60, 2, i32, funcRef, 2, funcRef, i64}, expected: []byte{0x60, 2, i32.Kind(), funcRef.Kind(), 2, funcRef.Kind(), i64.Kind()},
}, },
{ {
name: "two param two results with externrefs", name: "two param two results with externrefs",
input: wasm.FunctionType{Params: []wasm.ValueType{i32, externRef}, Results: []wasm.ValueType{externRef, i64}}, input: wasm.FunctionType{Params: []wasm.ValueType{i32, externRef}, Results: []wasm.ValueType{externRef, i64}},
expected: []byte{0x60, 2, i32, externRef, 2, externRef, i64}, expected: []byte{0x60, 2, i32.Kind(), externRef.Kind(), 2, externRef.Kind(), i64.Kind()},
}, },
} }
@@ -95,7 +95,7 @@ func TestFunctionType(t *testing.T) {
} }
func TestDecodeFunctionType_Errors(t *testing.T) { func TestDecodeFunctionType_Errors(t *testing.T) {
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64 i32, i64 := wasm.ValueTypeI32.Kind(), wasm.ValueTypeI64.Kind()
tests := []struct { tests := []struct {
name string name string
input []byte input []byte
+3 -3
View File
@@ -85,7 +85,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h', 0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i', 0x02, 'p', 'i',
wasm.ExternTypeGlobal, wasm.ExternTypeGlobal,
wasm.ValueTypeF64, 0x00, // 0 == const wasm.ValueTypeF64.Kind(), 0x00, // 0 == const
}, },
}, },
{ {
@@ -100,7 +100,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h', 0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i', 0x02, 'p', 'i',
wasm.ExternTypeGlobal, wasm.ExternTypeGlobal,
wasm.ValueTypeF64, 0x01, // 1 == var wasm.ValueTypeF64.Kind(), 0x01, // 1 == var
}, },
}, },
{ {
@@ -115,7 +115,7 @@ func TestEncodeImport(t *testing.T) {
0x02, 'm', 'y', 0x02, 'm', 'y',
0x05, 't', 'a', 'b', 'l', 'e', 0x05, 't', 'a', 'b', 'l', 'e',
wasm.ExternTypeTable, wasm.ExternTypeTable,
wasm.RefTypeFuncref, wasm.RefTypeFuncref.Kind(),
0x1, 0x1, 0x2, // Limit with max. 0x1, 0x1, 0x2, // Limit with max.
}, },
}, },
+9 -9
View File
@@ -20,18 +20,18 @@ func TestTableSection(t *testing.T) {
{ {
name: "min and min with max", name: "min and min with max",
input: []byte{ input: []byte{
0x01, // 1 table 0x01, // 1 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3) wasm.RefTypeFuncref.Kind(), 0x01, 2, 3, // (table 2 3)
}, },
expected: []wasm.Table{{Min: 2, Max: &three, Type: wasm.RefTypeFuncref}}, expected: []wasm.Table{{Min: 2, Max: &three, Type: wasm.RefTypeFuncref}},
}, },
{ {
name: "min and min with max - three tables", name: "min and min with max - three tables",
input: []byte{ input: []byte{
0x03, // 3 table 0x03, // 3 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3) wasm.RefTypeFuncref.Kind(), 0x01, 2, 3, // (table 2 3)
wasm.RefTypeExternref, 0x01, 2, 3, // (table 2 3) wasm.RefTypeExternref.Kind(), 0x01, 2, 3, // (table 2 3)
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3) wasm.RefTypeFuncref.Kind(), 0x01, 2, 3, // (table 2 3)
}, },
expected: []wasm.Table{ expected: []wasm.Table{
{Min: 2, Max: &three, Type: wasm.RefTypeFuncref}, {Min: 2, Max: &three, Type: wasm.RefTypeFuncref},
@@ -62,9 +62,9 @@ func TestTableSection_Errors(t *testing.T) {
{ {
name: "min and min with max", name: "min and min with max",
input: []byte{ input: []byte{
0x02, // 2 tables 0x02, // 2 tables
wasm.RefTypeFuncref, 0x00, 0x01, // (table 1) wasm.RefTypeFuncref.Kind(), 0x00, 0x01, // (table 1)
wasm.RefTypeFuncref, 0x01, 0x02, 0x03, // (table 2 3) wasm.RefTypeFuncref.Kind(), 0x01, 0x02, 0x03, // (table 2 3)
}, },
expectedErr: "at most one table allowed in module as feature \"reference-types\" is disabled", expectedErr: "at most one table allowed in module as feature \"reference-types\" is disabled",
features: api.CoreFeaturesV1, features: api.CoreFeaturesV1,
+2 -1
View File
@@ -12,7 +12,8 @@ import (
// //
// See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table // See https://www.w3.org/TR/2019/REC-wasm-core-1-20191205/#binary-table
func decodeTable(r *bytes.Reader, enabledFeatures api.CoreFeatures, ret *wasm.Table) (err error) { func decodeTable(r *bytes.Reader, enabledFeatures api.CoreFeatures, ret *wasm.Table) (err error) {
ret.Type, err = r.ReadByte() b, err := r.ReadByte()
ret.Type = wasm.ValueType(b)
if err != nil { if err != nil {
return fmt.Errorf("read leading byte: %v", err) return fmt.Errorf("read leading byte: %v", err)
} }
+9 -9
View File
@@ -24,32 +24,32 @@ func TestTableType(t *testing.T) {
{ {
name: "min 0 - funcref", name: "min 0 - funcref",
input: wasm.Table{Type: wasm.RefTypeFuncref}, input: wasm.Table{Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x0, 0}, expected: []byte{wasm.RefTypeFuncref.Kind(), 0x0, 0},
}, },
{ {
name: "min 0 - externref", name: "min 0 - externref",
input: wasm.Table{Type: wasm.RefTypeExternref}, input: wasm.Table{Type: wasm.RefTypeExternref},
expected: []byte{wasm.RefTypeExternref, 0x0, 0}, expected: []byte{wasm.RefTypeExternref.Kind(), 0x0, 0},
}, },
{ {
name: "min 0, max 0", name: "min 0, max 0",
input: wasm.Table{Max: &zero, Type: wasm.RefTypeFuncref}, input: wasm.Table{Max: &zero, Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x1, 0, 0}, expected: []byte{wasm.RefTypeFuncref.Kind(), 0x1, 0, 0},
}, },
{ {
name: "min largest", name: "min largest",
input: wasm.Table{Min: max, Type: wasm.RefTypeFuncref}, input: wasm.Table{Min: max, Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x0, 0x80, 0x80, 0x80, 0x40}, expected: []byte{wasm.RefTypeFuncref.Kind(), 0x0, 0x80, 0x80, 0x80, 0x40},
}, },
{ {
name: "min 0, max largest", name: "min 0, max largest",
input: wasm.Table{Max: &max, Type: wasm.RefTypeFuncref}, input: wasm.Table{Max: &max, Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x1, 0, 0x80, 0x80, 0x80, 0x40}, expected: []byte{wasm.RefTypeFuncref.Kind(), 0x1, 0, 0x80, 0x80, 0x80, 0x40},
}, },
{ {
name: "min largest max largest", name: "min largest max largest",
input: wasm.Table{Min: max, Max: &max, Type: wasm.RefTypeFuncref}, input: wasm.Table{Min: max, Max: &max, Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x1, 0x80, 0x80, 0x80, 0x40, 0x80, 0x80, 0x80, 0x40}, expected: []byte{wasm.RefTypeFuncref.Kind(), 0x1, 0x80, 0x80, 0x80, 0x40, 0x80, 0x80, 0x80, 0x40},
}, },
} }
@@ -84,19 +84,19 @@ func TestDecodeTableType_Errors(t *testing.T) {
}, },
{ {
name: "max < min", name: "max < min",
input: []byte{wasm.RefTypeFuncref, 0x1, 0x80, 0x80, 0x4, 0}, input: []byte{wasm.RefTypeFuncref.Kind(), 0x1, 0x80, 0x80, 0x4, 0},
expectedErr: "table size minimum must not be greater than maximum", expectedErr: "table size minimum must not be greater than maximum",
features: api.CoreFeatureReferenceTypes, features: api.CoreFeatureReferenceTypes,
}, },
{ {
name: "min > limit", name: "min > limit",
input: []byte{wasm.RefTypeFuncref, 0x0, 0xff, 0xff, 0xff, 0xff, 0xf}, input: []byte{wasm.RefTypeFuncref.Kind(), 0x0, 0xff, 0xff, 0xff, 0xff, 0xf},
expectedErr: "table min must be at most 134217728", expectedErr: "table min must be at most 134217728",
features: api.CoreFeatureReferenceTypes, features: api.CoreFeatureReferenceTypes,
}, },
{ {
name: "shared", name: "shared",
input: []byte{wasm.RefTypeFuncref, 0x2, 0}, input: []byte{wasm.RefTypeFuncref.Kind(), 0x2, 0},
expectedErr: "tables cannot be marked as shared", expectedErr: "tables cannot be marked as shared",
// Shared tables are an error even if threads are enabled. // Shared tables are an error even if threads are enabled.
features: experimental.CoreFeaturesThreads, features: experimental.CoreFeaturesThreads,
+4 -4
View File
@@ -23,10 +23,10 @@ func decodeValueTypes(r *bytes.Reader, num uint32) ([]wasm.ValueType, error) {
return nil, err return nil, err
} }
switch b { switch b {
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64, case wasm.ValueTypeI32.Kind(), wasm.ValueTypeF32.Kind(), wasm.ValueTypeI64.Kind(), wasm.ValueTypeF64.Kind(),
wasm.ValueTypeExternref, wasm.ValueTypeFuncref, wasm.ValueTypeV128, wasm.ValueTypeExternref.Kind(), wasm.ValueTypeFuncref.Kind(), wasm.ValueTypeV128.Kind(),
wasm.ValueTypeExnref: wasm.ValueTypeExnref.Kind():
ret = append(ret, b) ret = append(ret, wasm.ValueType(b))
case wasm.RefPrefixNullable, wasm.RefPrefixNonNullable: case wasm.RefPrefixNullable, wasm.RefPrefixNonNullable:
ht, _, err := leb128.DecodeInt33AsInt64(r) ht, _, err := leb128.DecodeInt33AsInt64(r)
if err != nil { if err != nil {
+15 -15
View File
@@ -29,77 +29,77 @@ func TestEncodeValTypes(t *testing.T) {
{ {
name: "funcref", name: "funcref",
input: []wasm.ValueType{fref}, input: []wasm.ValueType{fref},
expected: []byte{1, fref}, expected: []byte{1, fref.Kind()},
}, },
{ {
name: "externref", name: "externref",
input: []wasm.ValueType{ext}, input: []wasm.ValueType{ext},
expected: []byte{1, ext}, expected: []byte{1, ext.Kind()},
}, },
{ {
name: "i32", name: "i32",
input: []wasm.ValueType{i32}, input: []wasm.ValueType{i32},
expected: []byte{1, i32}, expected: []byte{1, i32.Kind()},
}, },
{ {
name: "i64", name: "i64",
input: []wasm.ValueType{i64}, input: []wasm.ValueType{i64},
expected: []byte{1, i64}, expected: []byte{1, i64.Kind()},
}, },
{ {
name: "f32", name: "f32",
input: []wasm.ValueType{f32}, input: []wasm.ValueType{f32},
expected: []byte{1, f32}, expected: []byte{1, f32.Kind()},
}, },
{ {
name: "f64", name: "f64",
input: []wasm.ValueType{f64}, input: []wasm.ValueType{f64},
expected: []byte{1, f64}, expected: []byte{1, f64.Kind()},
}, },
{ {
name: "i32i64", name: "i32i64",
input: []wasm.ValueType{i32, i64}, input: []wasm.ValueType{i32, i64},
expected: []byte{2, i32, i64}, expected: []byte{2, i32.Kind(), i64.Kind()},
}, },
{ {
name: "i32i64f32", name: "i32i64f32",
input: []wasm.ValueType{i32, i64, f32}, input: []wasm.ValueType{i32, i64, f32},
expected: []byte{3, i32, i64, f32}, expected: []byte{3, i32.Kind(), i64.Kind(), f32.Kind()},
}, },
{ {
name: "i32i64f32f64", name: "i32i64f32f64",
input: []wasm.ValueType{i32, i64, f32, f64}, input: []wasm.ValueType{i32, i64, f32, f64},
expected: []byte{4, i32, i64, f32, f64}, expected: []byte{4, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind()},
}, },
{ {
name: "i32i64f32f64i32", name: "i32i64f32f64i32",
input: []wasm.ValueType{i32, i64, f32, f64, i32}, input: []wasm.ValueType{i32, i64, f32, f64, i32},
expected: []byte{5, i32, i64, f32, f64, i32}, expected: []byte{5, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64", name: "i32i64f32f64i32i64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64},
expected: []byte{6, i32, i64, f32, f64, i32, i64}, expected: []byte{6, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32", name: "i32i64f32f64i32i64f32",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32},
expected: []byte{7, i32, i64, f32, f64, i32, i64, f32}, expected: []byte{7, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64", name: "i32i64f32f64i32i64f32f64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64},
expected: []byte{8, i32, i64, f32, f64, i32, i64, f32, f64}, expected: []byte{8, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64i32", name: "i32i64f32f64i32i64f32f64i32",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32},
expected: []byte{9, i32, i64, f32, f64, i32, i64, f32, f64, i32}, expected: []byte{9, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind()},
}, },
{ {
name: "i32i64f32f64i32i64f32f64i32i64", name: "i32i64f32f64i32i64f32f64i32i64",
input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, input: []wasm.ValueType{i32, i64, f32, f64, i32, i64, f32, f64, i32, i64},
expected: []byte{10, i32, i64, f32, f64, i32, i64, f32, f64, i32, i64}, expected: []byte{10, i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind(), f32.Kind(), f64.Kind(), i32.Kind(), i64.Kind()},
}, },
} }
+7 -7
View File
@@ -99,7 +99,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
} else { } else {
instName = InstructionName(op) instName = InstructionName(op)
} }
fmt.Printf("handling %s, stack=%s, blocks: %v\n", instName, valueTypeStack.stack, controlBlockStack) fmt.Printf("handling %s, stack=%v, blocks: %v\n", instName, valueTypeStack.stack, controlBlockStack)
} }
if len(controlBlockStack.stack) == 0 { if len(controlBlockStack.stack) == 0 {
@@ -997,7 +997,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
switch op { switch op {
case OpcodeRefNull: case OpcodeRefNull:
pc++ pc++
switch reftype := body[pc]; reftype { switch reftype := body[pc]; ValueType(reftype) {
case ValueTypeExternref: case ValueTypeExternref:
valueTypeStack.push(ValueTypeExternref) valueTypeStack.push(ValueTypeExternref)
case ValueTypeFuncref: case ValueTypeFuncref:
@@ -1041,7 +1041,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
refType := tables[tableIndex].Type refType := tables[tableIndex].Type
if op == OpcodeTableGet { if op == OpcodeTableGet {
if err := valueTypeStack.popAndVerifyType(api.ValueTypeI32); err != nil { if err := valueTypeStack.popAndVerifyType(ValueTypeI32); err != nil {
return fmt.Errorf("cannot pop the operand for table.get: %v", err) return fmt.Errorf("cannot pop the operand for table.get: %v", err)
} }
valueTypeStack.push(refType) valueTypeStack.push(refType)
@@ -1049,7 +1049,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
if err := valueTypeStack.popAndVerifyType(refType); err != nil { if err := valueTypeStack.popAndVerifyType(refType); err != nil {
return fmt.Errorf("cannot pop the operand for table.set: %v", err) return fmt.Errorf("cannot pop the operand for table.set: %v", err)
} }
if err := valueTypeStack.popAndVerifyType(api.ValueTypeI32); err != nil { if err := valueTypeStack.popAndVerifyType(ValueTypeI32); err != nil {
return fmt.Errorf("cannot pop the operand for table.set: %v", err) return fmt.Errorf("cannot pop the operand for table.set: %v", err)
} }
} }
@@ -2028,7 +2028,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
if ifMissingElse { if ifMissingElse {
// If this is the end of block without else, the number of block's results and params must be same. // If this is the end of block without else, the number of block's results and params must be same.
// Otherwise, the value stack would result in the inconsistent state at runtime. // Otherwise, the value stack would result in the inconsistent state at runtime.
if !bytes.Equal(bl.blockType.Results, bl.blockType.Params) { if !slices.Equal(bl.blockType.Results, bl.blockType.Params) {
return typeCountError(false, OpcodeElseName, bl.blockType.Params, bl.blockType.Results) return typeCountError(false, OpcodeElseName, bl.blockType.Params, bl.blockType.Results)
} }
// -1 skips else, to handle if block without else properly. // -1 skips else, to handle if block without else properly.
@@ -2091,9 +2091,9 @@ func (m *Module) validateFunctionWithMaxStackValues(
return fmt.Errorf("too many type immediates for %s", InstructionName(op)) return fmt.Errorf("too many type immediates for %s", InstructionName(op))
} }
pc++ pc++
tp := body[pc] tp := ValueType(body[pc])
if tp != ValueTypeI32 && tp != ValueTypeI64 && tp != ValueTypeF32 && tp != ValueTypeF64 && if tp != ValueTypeI32 && tp != ValueTypeI64 && tp != ValueTypeF32 && tp != ValueTypeF64 &&
tp != api.ValueTypeExternref && tp != ValueTypeFuncref && tp != ValueTypeV128 { tp != ValueTypeExternref && tp != ValueTypeFuncref && tp != ValueTypeV128 {
return fmt.Errorf("invalid type %s for %s", ValueTypeName(tp), OpcodeTypedSelectName) return fmt.Errorf("invalid type %s for %s", ValueTypeName(tp), OpcodeTypedSelectName)
} }
} else if isReferenceValueType(v1) || isReferenceValueType(v2) { } else if isReferenceValueType(v1) || isReferenceValueType(v2) {
+18 -18
View File
@@ -2273,7 +2273,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.null (funcref)", name: "ref.null (funcref)",
flag: api.CoreFeatureReferenceTypes, flag: api.CoreFeatureReferenceTypes,
body: []byte{ body: []byte{
OpcodeRefNull, ValueTypeFuncref, OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeDrop, OpcodeEnd, OpcodeDrop, OpcodeEnd,
}, },
}, },
@@ -2281,7 +2281,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.null (externref)", name: "ref.null (externref)",
flag: api.CoreFeatureReferenceTypes, flag: api.CoreFeatureReferenceTypes,
body: []byte{ body: []byte{
OpcodeRefNull, ValueTypeExternref, OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeDrop, OpcodeEnd, OpcodeDrop, OpcodeEnd,
}, },
}, },
@@ -2289,7 +2289,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.null - disabled", name: "ref.null - disabled",
flag: api.CoreFeaturesV1, flag: api.CoreFeaturesV1,
body: []byte{ body: []byte{
OpcodeRefNull, ValueTypeFuncref, OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeDrop, OpcodeEnd, OpcodeDrop, OpcodeEnd,
}, },
expectedErr: "ref.null invalid as feature \"reference-types\" is disabled", expectedErr: "ref.null invalid as feature \"reference-types\" is disabled",
@@ -2298,7 +2298,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.is_null", name: "ref.is_null",
flag: api.CoreFeatureReferenceTypes, flag: api.CoreFeatureReferenceTypes,
body: []byte{ body: []byte{
OpcodeRefNull, ValueTypeFuncref, OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeRefIsNull, OpcodeRefIsNull,
OpcodeDrop, OpcodeEnd, OpcodeDrop, OpcodeEnd,
}, },
@@ -2373,7 +2373,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{ {
name: "table.grow (funcref)", name: "table.grow (funcref)",
body: []byte{ body: []byte{
OpcodeRefNull, RefTypeFuncref, OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow, OpcodeMiscPrefix, OpcodeMiscTableGrow,
0, // Table Index. 0, // Table Index.
@@ -2385,7 +2385,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{ {
name: "table.grow (funcref) - type mismatch", name: "table.grow (funcref) - type mismatch",
body: []byte{ body: []byte{
OpcodeRefNull, RefTypeFuncref, OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow, OpcodeMiscPrefix, OpcodeMiscTableGrow,
1, // Table of externref type -> mismatch. 1, // Table of externref type -> mismatch.
@@ -2397,7 +2397,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{ {
name: "table.grow (externref)", name: "table.grow (externref)",
body: []byte{ body: []byte{
OpcodeRefNull, RefTypeExternref, OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow, OpcodeMiscPrefix, OpcodeMiscTableGrow,
1, // Table Index. 1, // Table Index.
@@ -2409,7 +2409,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{ {
name: "table.grow (externref) type mismatch", name: "table.grow (externref) type mismatch",
body: []byte{ body: []byte{
OpcodeRefNull, RefTypeExternref, OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow, OpcodeMiscPrefix, OpcodeMiscTableGrow,
0, // Table of funcref type -> mismatch. 0, // Table of funcref type -> mismatch.
@@ -2421,7 +2421,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{ {
name: "table.grow - table not found", name: "table.grow - table not found",
body: []byte{ body: []byte{
OpcodeRefNull, RefTypeFuncref, OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow, OpcodeMiscPrefix, OpcodeMiscTableGrow,
10, // Table Index. 10, // Table Index.
@@ -2454,7 +2454,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (funcref)", name: "table.fill (funcref)",
body: []byte{ body: []byte{
OpcodeI32Const, 1, // offset OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeFuncref, OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill, OpcodeMiscPrefix, OpcodeMiscTableFill,
0, // Table Index. 0, // Table Index.
@@ -2466,7 +2466,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (funcref) - type mismatch", name: "table.fill (funcref) - type mismatch",
body: []byte{ body: []byte{
OpcodeI32Const, 1, // offset OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeFuncref, OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill, OpcodeMiscPrefix, OpcodeMiscTableFill,
1, // Table of externref type -> mismatch. 1, // Table of externref type -> mismatch.
@@ -2479,7 +2479,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (externref)", name: "table.fill (externref)",
body: []byte{ body: []byte{
OpcodeI32Const, 1, // offset OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeExternref, OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill, OpcodeMiscPrefix, OpcodeMiscTableFill,
1, // Table Index. 1, // Table Index.
@@ -2491,7 +2491,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (externref) - type mismatch", name: "table.fill (externref) - type mismatch",
body: []byte{ body: []byte{
OpcodeI32Const, 1, // offset OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeExternref, OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill, OpcodeMiscPrefix, OpcodeMiscTableFill,
0, // Table of funcref type -> mismatch. 0, // Table of funcref type -> mismatch.
@@ -2576,7 +2576,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set (funcref)", name: "table.set (funcref)",
body: []byte{ body: []byte{
OpcodeI32Const, 0, OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeFuncref, OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeTableSet, 0, OpcodeTableSet, 0,
OpcodeEnd, OpcodeEnd,
}, },
@@ -2586,7 +2586,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set type mismatch (src=funcref, dst=externref)", name: "table.set type mismatch (src=funcref, dst=externref)",
body: []byte{ body: []byte{
OpcodeI32Const, 0, OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeFuncref, OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeTableSet, 1, OpcodeTableSet, 1,
OpcodeEnd, OpcodeEnd,
}, },
@@ -2597,7 +2597,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set (externref)", name: "table.set (externref)",
body: []byte{ body: []byte{
OpcodeI32Const, 0, OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeExternref, OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeTableSet, 1, OpcodeTableSet, 1,
OpcodeEnd, OpcodeEnd,
}, },
@@ -2607,7 +2607,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set type mismatch (src=externref, dst=funcref)", name: "table.set type mismatch (src=externref, dst=funcref)",
body: []byte{ body: []byte{
OpcodeI32Const, 0, OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeExternref, OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeTableSet, 0, OpcodeTableSet, 0,
OpcodeEnd, OpcodeEnd,
}, },
@@ -2655,7 +2655,7 @@ func TestModule_funcValidation_Select_error(t *testing.T) {
name: "typed_select (disabled)", name: "typed_select (disabled)",
body: []byte{ body: []byte{
OpcodeI32Const, 0, OpcodeI32Const, 0, OpcodeI32Const, 0, OpcodeI32Const, 0, OpcodeI32Const, 0, OpcodeI32Const, 0,
OpcodeTypedSelect, 1, ValueTypeI32, // immediate vector's size must be one OpcodeTypedSelect, 1, ValueTypeI32.Kind(), // immediate vector's size must be one
OpcodeDrop, OpcodeDrop,
OpcodeEnd, OpcodeEnd,
}, },
+26 -4
View File
@@ -168,8 +168,8 @@ func (f *FunctionDefinition) GoFunction() interface{} {
} }
// ParamTypes implements api.FunctionDefinition ParamTypes. // ParamTypes implements api.FunctionDefinition ParamTypes.
func (f *FunctionDefinition) ParamTypes() []ValueType { func (f *FunctionDefinition) ParamTypes() []api.ValueType {
return f.Functype.Params return ToApiValueType(f.Functype.Params)
} }
// ParamNames implements the same method as documented on api.FunctionDefinition. // ParamNames implements the same method as documented on api.FunctionDefinition.
@@ -178,11 +178,33 @@ func (f *FunctionDefinition) ParamNames() []string {
} }
// ResultTypes implements api.FunctionDefinition ResultTypes. // ResultTypes implements api.FunctionDefinition ResultTypes.
func (f *FunctionDefinition) ResultTypes() []ValueType { func (f *FunctionDefinition) ResultTypes() []api.ValueType {
return f.Functype.Results return ToApiValueType(f.Functype.Results)
} }
// ResultNames implements the same method as documented on api.FunctionDefinition. // ResultNames implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) ResultNames() []string { func (f *FunctionDefinition) ResultNames() []string {
return f.resultNames return f.resultNames
} }
func ToApiValueType(values []ValueType) []api.ValueType {
if values == nil {
return nil
}
apiValues := make([]api.ValueType, len(values))
for i, v := range values {
apiValues[i] = api.ValueType(v)
}
return apiValues
}
func FromApiValueType(apiValues []api.ValueType) []ValueType {
if apiValues == nil {
return nil
}
values := make([]ValueType, len(apiValues))
for i, v := range apiValues {
values[i] = ValueType(v)
}
return values
}
+2 -2
View File
@@ -13,7 +13,7 @@ type constantGlobal struct {
// Type implements api.Global. // Type implements api.Global.
func (g constantGlobal) Type() api.ValueType { func (g constantGlobal) Type() api.ValueType {
return g.g.Type.ValType return api.ValueType(g.g.Type.ValType)
} }
// Get implements api.Global. // Get implements api.Global.
@@ -35,7 +35,7 @@ type mutableGlobal struct {
// Type implements api.Global. // Type implements api.Global.
func (g mutableGlobal) Type() api.ValueType { func (g mutableGlobal) Type() api.ValueType {
return g.g.Type.ValType return api.ValueType(g.g.Type.ValType)
} }
// Get implements api.Global. // Get implements api.Global.
+12 -12
View File
@@ -27,7 +27,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32}, Type: GlobalType{ValType: ValueTypeI32},
Val: 1, Val: 1,
}}, }},
expectedType: ValueTypeI32, expectedType: ValueTypeI32.Kind(),
expectedVal: 1, expectedVal: 1,
expectedString: "global(1)", expectedString: "global(1)",
}, },
@@ -37,7 +37,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32}, Type: GlobalType{ValType: ValueTypeI32},
Val: math.MaxInt32, Val: math.MaxInt32,
}}, }},
expectedType: ValueTypeI32, expectedType: ValueTypeI32.Kind(),
expectedVal: math.MaxInt32, expectedVal: math.MaxInt32,
expectedString: "global(2147483647)", expectedString: "global(2147483647)",
}, },
@@ -47,7 +47,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64}, Type: GlobalType{ValType: ValueTypeI64},
Val: 1, Val: 1,
}}, }},
expectedType: ValueTypeI64, expectedType: ValueTypeI64.Kind(),
expectedVal: 1, expectedVal: 1,
expectedString: "global(1)", expectedString: "global(1)",
}, },
@@ -57,7 +57,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64}, Type: GlobalType{ValType: ValueTypeI64},
Val: math.MaxInt64, Val: math.MaxInt64,
}}, }},
expectedType: ValueTypeI64, expectedType: ValueTypeI64.Kind(),
expectedVal: math.MaxInt64, expectedVal: math.MaxInt64,
expectedString: "global(9223372036854775807)", expectedString: "global(9223372036854775807)",
}, },
@@ -67,7 +67,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32}, Type: GlobalType{ValType: ValueTypeF32},
Val: api.EncodeF32(1.0), Val: api.EncodeF32(1.0),
}}, }},
expectedType: ValueTypeF32, expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(1.0), expectedVal: api.EncodeF32(1.0),
expectedString: "global(1.000000)", expectedString: "global(1.000000)",
}, },
@@ -77,7 +77,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32}, Type: GlobalType{ValType: ValueTypeF32},
Val: api.EncodeF32(math.MaxFloat32), Val: api.EncodeF32(math.MaxFloat32),
}}, }},
expectedType: ValueTypeF32, expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(math.MaxFloat32), expectedVal: api.EncodeF32(math.MaxFloat32),
expectedString: "global(340282346638528859811704183484516925440.000000)", expectedString: "global(340282346638528859811704183484516925440.000000)",
}, },
@@ -87,7 +87,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64}, Type: GlobalType{ValType: ValueTypeF64},
Val: api.EncodeF64(1.0), Val: api.EncodeF64(1.0),
}}, }},
expectedType: ValueTypeF64, expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(1.0), expectedVal: api.EncodeF64(1.0),
expectedString: "global(1.000000)", expectedString: "global(1.000000)",
}, },
@@ -97,7 +97,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64}, Type: GlobalType{ValType: ValueTypeF64},
Val: api.EncodeF64(math.MaxFloat64), Val: api.EncodeF64(math.MaxFloat64),
}}, }},
expectedType: ValueTypeF64, expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(math.MaxFloat64), expectedVal: api.EncodeF64(math.MaxFloat64),
expectedString: "global(179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)", expectedString: "global(179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)",
}, },
@@ -107,7 +107,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32, Mutable: true}, Type: GlobalType{ValType: ValueTypeI32, Mutable: true},
Val: 1, Val: 1,
}}, }},
expectedType: ValueTypeI32, expectedType: ValueTypeI32.Kind(),
expectedVal: 1, expectedVal: 1,
expectedString: "global(1)", expectedString: "global(1)",
expectedMutable: true, expectedMutable: true,
@@ -118,7 +118,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64, Mutable: true}, Type: GlobalType{ValType: ValueTypeI64, Mutable: true},
Val: 1, Val: 1,
}}, }},
expectedType: ValueTypeI64, expectedType: ValueTypeI64.Kind(),
expectedVal: 1, expectedVal: 1,
expectedString: "global(1)", expectedString: "global(1)",
expectedMutable: true, expectedMutable: true,
@@ -129,7 +129,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32, Mutable: true}, Type: GlobalType{ValType: ValueTypeF32, Mutable: true},
Val: api.EncodeF32(1.0), Val: api.EncodeF32(1.0),
}}, }},
expectedType: ValueTypeF32, expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(1.0), expectedVal: api.EncodeF32(1.0),
expectedString: "global(1.000000)", expectedString: "global(1.000000)",
expectedMutable: true, expectedMutable: true,
@@ -140,7 +140,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64, Mutable: true}, Type: GlobalType{ValType: ValueTypeF64, Mutable: true},
Val: api.EncodeF64(1.0), Val: api.EncodeF64(1.0),
}}, }},
expectedType: ValueTypeF64, expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(1.0), expectedVal: api.EncodeF64(1.0),
expectedString: "global(1.000000)", expectedString: "global(1.000000)",
expectedMutable: true, expectedMutable: true,
+3 -3
View File
@@ -1,12 +1,12 @@
package wasm package wasm
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"slices"
"github.com/tetratelabs/wazero/api" "github.com/tetratelabs/wazero/api"
) )
@@ -47,7 +47,7 @@ func (f *reflectGoModuleFunction) EqualTo(that interface{}) bool {
return false return false
} else { } else {
// TODO compare reflect pointers // TODO compare reflect pointers
return bytes.Equal(f.params, f2.params) && bytes.Equal(f.results, f2.results) return slices.Equal(f.params, f2.params) && slices.Equal(f.results, f2.results)
} }
} }
@@ -67,7 +67,7 @@ func (f *reflectGoFunction) EqualTo(that interface{}) bool {
} else { } else {
// TODO compare reflect pointers // TODO compare reflect pointers
return f.pk == f2.pk && return f.pk == f2.pk &&
bytes.Equal(f.params, f2.params) && bytes.Equal(f.results, f2.results) slices.Equal(f.params, f2.params) && slices.Equal(f.results, f2.results)
} }
} }
+23 -15
View File
@@ -6,6 +6,7 @@ import (
"encoding/binary" "encoding/binary"
"errors" "errors"
"fmt" "fmt"
"slices"
"sort" "sort"
"strings" "strings"
"sync" "sync"
@@ -776,7 +777,7 @@ func (f *FunctionType) CacheNumInUint64() {
// EqualsSignature returns true if the function type has the same parameters and results. // EqualsSignature returns true if the function type has the same parameters and results.
func (f *FunctionType) EqualsSignature(params []ValueType, results []ValueType) bool { func (f *FunctionType) EqualsSignature(params []ValueType, results []ValueType) bool {
return bytes.Equal(f.Params, params) && bytes.Equal(f.Results, results) return slices.Equal(f.Params, params) && slices.Equal(f.Results, results)
} }
// EqualsType returns true if the function types are structurally equal AND // EqualsType returns true if the function types are structurally equal AND
@@ -1125,21 +1126,24 @@ func SectionIDName(sectionID SectionID) string {
return "unknown" return "unknown"
} }
// ValueType is an alias of api.ValueType defined to simplify imports. // ValueType represents a WebAssembly value type as a uint64.
type ValueType = api.ValueType //
// Layout:
//
// bits 0-7: kind byte (backward-compatible with api.ValueType)
// bits 8-15: flags (nullability, concrete ref)
// bits 32-63: type index (for concrete refs like (ref $3))
type ValueType uint64
const ( const (
ValueTypeI32 = api.ValueTypeI32 ValueTypeI32 ValueType = 0x7f
ValueTypeI64 = api.ValueTypeI64 ValueTypeI64 ValueType = 0x7e
ValueTypeF32 = api.ValueTypeF32 ValueTypeF32 ValueType = 0x7d
ValueTypeF64 = api.ValueTypeF64 ValueTypeF64 ValueType = 0x7c
// TODO: ValueTypeV128 is not exposed in the api pkg yet. ValueTypeV128 ValueType = 0x7b
ValueTypeV128 ValueType = 0x7b
// TODO: ValueTypeFuncref is not exposed in the api pkg yet.
ValueTypeFuncref ValueType = 0x70 ValueTypeFuncref ValueType = 0x70
ValueTypeExternref = api.ValueTypeExternref ValueTypeExternref ValueType = 0x6f
// ValueTypeExnref is the exception reference type used in exception handling. ValueTypeExnref ValueType = 0x69
ValueTypeExnref ValueType = 0x69
) )
const ( const (
@@ -1167,7 +1171,11 @@ func ValueTypeName(t ValueType) string {
} else if t == ValueTypeExnref { } else if t == ValueTypeExnref {
return "exnref" return "exnref"
} }
return api.ValueTypeName(t) return api.ValueTypeName(api.ValueType(t))
}
func (v ValueType) Kind() byte {
return byte(v)
} }
func isReferenceValueType(vt ValueType) bool { func isReferenceValueType(vt ValueType) bool {
@@ -1207,7 +1215,7 @@ const (
) )
// ExternTypeName is an alias of api.ExternTypeName defined to simplify imports. // ExternTypeName is an alias of api.ExternTypeName defined to simplify imports.
func ExternTypeName(t ValueType) string { func ExternTypeName(t ExternType) string {
if t == ExternTypeTag { if t == ExternTypeTag {
return ExternTypeTagName return ExternTypeTagName
} }
+12 -12
View File
@@ -295,12 +295,12 @@ func TestValidateConstExpression(t *testing.T) {
require.EqualError(t, err, "ref.func index out of range [5] with length 1") require.EqualError(t, err, "ref.func index out of range [5] with length 1")
}) })
t.Run("ref.null", func(t *testing.T) { t.Run("ref.null", func(t *testing.T) {
expr := NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref}) expr := NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref.Kind()})
err := validateConstExpression(nil, 0, err := validateConstExpression(nil, 0,
&expr, &expr,
ValueTypeFuncref) ValueTypeFuncref)
require.NoError(t, err) require.NoError(t, err)
expr = NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}) expr = NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()})
err = validateConstExpression(nil, 0, err = validateConstExpression(nil, 0,
&expr, &expr,
ValueTypeExternref) ValueTypeExternref)
@@ -799,11 +799,11 @@ func TestModule_buildGlobals(t *testing.T) {
}, },
{ {
Type: GlobalType{Mutable: false, ValType: ValueTypeExternref}, Type: GlobalType{Mutable: false, ValType: ValueTypeExternref},
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
}, },
{ {
Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref}, Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref},
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref}), Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref.Kind()}),
}, },
{ {
Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref}, Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref},
@@ -948,7 +948,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeActive, Mode: ElementModeActive,
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
}, },
}, },
@@ -956,7 +956,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeDeclarative, Mode: ElementModeDeclarative,
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
}, },
}, },
@@ -965,8 +965,8 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
}, },
}, },
}, },
@@ -993,7 +993,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeActive, Mode: ElementModeActive,
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
}, },
}, },
@@ -1001,7 +1001,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeDeclarative, Mode: ElementModeDeclarative,
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
}, },
}, },
@@ -1010,8 +1010,8 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Init: []ConstantExpression{ Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}), NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
}, },
}, },
}, },
+6 -6
View File
@@ -602,11 +602,11 @@ func TestGlobalInstance_initialize(t *testing.T) {
}{ }{
{ {
name: "ref.null (externref)", name: "ref.null (externref)",
expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref}), expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
}, },
{ {
name: "ref.null (funcref)", name: "ref.null (funcref)",
expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref}), expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref.Kind()}),
}, },
} }
@@ -614,7 +614,7 @@ func TestGlobalInstance_initialize(t *testing.T) {
tc := tt tc := tt
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
g := GlobalInstance{} g := GlobalInstance{}
g.Type.ValType = tc.expr.Data[0] g.Type.ValType = ValueType(tc.expr.Data[0])
g.initialize(nil, &tc.expr, nil) g.initialize(nil, &tc.expr, nil)
require.Equal(t, uint64(0), g.Val) require.Equal(t, uint64(0), g.Val)
}) })
@@ -713,7 +713,7 @@ func Test_resolveImports(t *testing.T) {
Source: &Module{ Source: &Module{
FunctionSection: []Index{0, 0, 1, 0, 0}, FunctionSection: []Index{0, 0, 1, 0, 0},
TypeSection: []FunctionType{ TypeSection: []FunctionType{
{Params: []ValueType{ExternTypeFunc}}, {Params: []ValueType{ValueType(ExternTypeFunc)}},
{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}}, {Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}},
}, },
}, },
@@ -722,7 +722,7 @@ func Test_resolveImports(t *testing.T) {
module := &Module{ module := &Module{
TypeSection: []FunctionType{ TypeSection: []FunctionType{
{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}}, {Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}},
{Params: []ValueType{ExternTypeFunc}}, {Params: []ValueType{ValueType(ExternTypeFunc)}},
}, },
ImportFunctionCount: 2, ImportFunctionCount: 2,
ImportPerModule: map[string][]*Import{ ImportPerModule: map[string][]*Import{
@@ -1020,7 +1020,7 @@ func TestModuleInstance_applyElements(t *testing.T) {
m.applyElements([]ElementSegment{ m.applyElements([]ElementSegment{
{Mode: ElementModeActive, OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{ {Mode: ElementModeActive, OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(0)), NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(0)),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(2)), NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(2)),
}}, }},
}) })
+1 -1
View File
@@ -16,7 +16,7 @@ type Table struct {
} }
// RefType is either RefTypeFuncref or RefTypeExternref as of WebAssembly core 2.0. // RefType is either RefTypeFuncref or RefTypeExternref as of WebAssembly core 2.0.
type RefType = byte type RefType = ValueType
const ( const (
// RefTypeFuncref represents a reference to a function. // RefTypeFuncref represents a reference to a function.
+4 -4
View File
@@ -743,9 +743,9 @@ func TestModule_buildTables(t *testing.T) {
TableSection: []Table{{Min: 10, Type: RefTypeExternref}}, TableSection: []Table{{Min: 10, Type: RefTypeExternref}},
ElementSection: []ElementSegment{ ElementSection: []ElementSegment{
{OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{ {OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}), NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
}}, // three null refs. }}, // three null refs.
}, },
}, },
@@ -886,7 +886,7 @@ func TestModule_buildTables(t *testing.T) {
ElementSection: []ElementSegment{ ElementSection: []ElementSegment{
{ {
OffsetExpr: NewConstantExpressionFromOpcode(OpcodeGlobalGet, []byte{0x0}), OffsetExpr: NewConstantExpressionFromOpcode(OpcodeGlobalGet, []byte{0x0}),
Init: []ConstantExpression{NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}), NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(2))}, Init: []ConstantExpression{NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}), NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(2))},
TableIndex: 1, TableIndex: 1,
}, },
{ {
+2 -2
View File
@@ -67,7 +67,7 @@ func TestRuntime_CompileModule(t *testing.T) {
{ {
name: "FunctionSection, but not exported", name: "FunctionSection, but not exported",
wasm: &wasm.Module{ wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}},
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
}, },
@@ -79,7 +79,7 @@ func TestRuntime_CompileModule(t *testing.T) {
{ {
name: "FunctionSection exported", name: "FunctionSection exported",
wasm: &wasm.Module{ wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}}, TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}},
FunctionSection: []wasm.Index{0}, FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}}, CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
ExportSection: []wasm.Export{{ ExportSection: []wasm.Export{{