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
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
}
// WithGoModuleFunction implements HostFunctionBuilder.WithGoModuleFunction
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
}
+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
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 {
return 0
@@ -54,7 +54,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -80,7 +80,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -107,7 +107,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -133,7 +133,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32)},
@@ -159,8 +159,8 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0, 1},
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 {
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})).
Export("1")
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{
@@ -210,13 +210,13 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
name: "WithGoFunction WithName WithParameterNames",
input: func(r Runtime) HostModuleBuilder {
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").
Export("1")
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{
@@ -240,15 +240,15 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
input: func(r Runtime) HostModuleBuilder {
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})).
Export("1").
NewFunctionBuilder().
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}).
WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1")
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{
@@ -272,16 +272,16 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
// Intentionally not in lexicographic order
return r.NewHostModuleBuilder("host").
NewFunctionBuilder().
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}).
WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("2").
NewFunctionBuilder().
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}).
WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
Export("1")
},
expected: &wasm.Module{
TypeSection: []wasm.FunctionType{
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
},
FunctionSection: []wasm.Index{0, 1},
CodeSection: []wasm.Code{
+4 -4
View File
@@ -177,10 +177,10 @@ func (f *mockFunctionDefinition) DebugName() string {
return f.debugName
}
func (f *mockFunctionDefinition) ParamTypes() []wasm.ValueType {
return []wasm.ValueType{}
func (f *mockFunctionDefinition) ParamTypes() []api.ValueType {
return []api.ValueType{}
}
func (f *mockFunctionDefinition) ResultTypes() []wasm.ValueType {
return []wasm.ValueType{}
func (f *mockFunctionDefinition) ResultTypes() []api.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) {
wasiFuncName := wasi.RandomGetName
wasiFuncType := wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32},
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
Results: []wasm.ValueType{wasm.ValueTypeI32},
}
wasiParamNames := []string{"buf", "buf_len"}
wasiParams := []uint64{0, 8}
@@ -84,7 +84,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "i32",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
params: []uint64{math.MaxUint32},
expected: `--> test.fn(-1)
<--
@@ -92,7 +92,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "i32 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
params: []uint64{math.MaxUint32},
paramNames: []string{"x"},
expected: `--> test.fn(x=-1)
@@ -101,7 +101,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "i64",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
params: []uint64{math.MaxUint64},
expected: `--> test.fn(-1)
<--
@@ -109,7 +109,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "i64 named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
params: []uint64{math.MaxUint64},
paramNames: []string{"x"},
expected: `--> test.fn(x=-1)
@@ -118,7 +118,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "f32",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF32}},
params: []uint64{api.EncodeF32(math.MaxFloat32)},
expected: `--> test.fn(3.4028235e+38)
<--
@@ -126,7 +126,7 @@ func Test_loggingListener(t *testing.T) {
},
{
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)},
paramNames: []string{"x"},
expected: `--> test.fn(x=3.4028235e+38)
@@ -135,7 +135,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "f64",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF64}},
params: []uint64{api.EncodeF64(math.MaxFloat64)},
expected: `--> test.fn(1.7976931348623157e+308)
<--
@@ -143,7 +143,7 @@ func Test_loggingListener(t *testing.T) {
},
{
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)},
paramNames: []string{"x"},
expected: `--> test.fn(x=1.7976931348623157e+308)
@@ -152,7 +152,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "externref",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
params: []uint64{0},
expected: `--> test.fn(0000000000000000)
<--
@@ -160,7 +160,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "externref named",
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}},
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
params: []uint64{0},
paramNames: []string{"x"},
expected: `--> test.fn(x=0000000000000000)
@@ -169,7 +169,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "v128",
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}},
functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
params: []uint64{0, 1},
expected: `--> test.fn(00000000000000000000000000000001)
<--
@@ -177,7 +177,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "v128 named",
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}},
functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
params: []uint64{0, 1},
paramNames: []string{"x"},
expected: `--> test.fn(x=00000000000000000000000000000001)
@@ -186,7 +186,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "funcref",
functype: wasm.FunctionType{Params: []api.ValueType{0x70}},
functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
params: []uint64{0},
expected: `--> test.fn(0000000000000000)
<--
@@ -194,7 +194,7 @@ func Test_loggingListener(t *testing.T) {
},
{
name: "funcref named",
functype: wasm.FunctionType{Params: []api.ValueType{0x70}},
functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
params: []uint64{0},
paramNames: []string{"x"},
expected: `--> test.fn(x=0000000000000000)
@@ -203,7 +203,7 @@ func Test_loggingListener(t *testing.T) {
},
{
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},
expected: `--> test.fn()
<-- -1
@@ -212,8 +212,8 @@ func Test_loggingListener(t *testing.T) {
{
name: "one param, one result",
functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeF32},
Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []wasm.ValueType{wasm.ValueTypeF32},
},
params: []uint64{math.MaxUint32},
results: []uint64{api.EncodeF32(math.MaxFloat32)},
@@ -224,8 +224,8 @@ func Test_loggingListener(t *testing.T) {
{
name: "two params, two results",
functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64},
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64},
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
},
params: []uint64{math.MaxUint32, math.MaxUint64},
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",
functype: wasm.FunctionType{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64},
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64},
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
},
params: []uint64{math.MaxUint32, math.MaxUint64},
paramNames: []string{"x", "y"},
+1 -1
View File
@@ -22,7 +22,7 @@ func LookupFunction(
expectedParamTypes, expectedResultTypes []api.ValueType,
) api.Function {
m := module.(*wasm.ModuleInstance)
typ := &wasm.FunctionType{Params: expectedParamTypes, Results: expectedResultTypes}
typ := &wasm.FunctionType{Params: wasm.FromApiValueType(expectedParamTypes), Results: wasm.FromApiValueType(expectedResultTypes)}
typ.CacheNumInUint64()
typeID := m.GetFunctionTypeID(typ)
if int(tableIndex) >= len(m.Tables) {
+8 -7
View File
@@ -14,6 +14,7 @@ import (
func TestLookupFunction(t *testing.T) {
const i32 = wasm.ValueTypeI32
const i32a = api.ValueTypeI32
bytes := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{
{Results: []wasm.ValueType{i32}},
@@ -51,14 +52,14 @@ func TestLookupFunction(t *testing.T) {
require.NotNil(t, m)
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
err = f.CallWithStack(context.Background(), result[:])
require.NoError(t, err)
require.Equal(t, uint64(1), result[0])
})
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}
err = f.CallWithStack(context.Background(), stack[:])
require.NoError(t, err)
@@ -68,11 +69,11 @@ func TestLookupFunction(t *testing.T) {
t.Run("panics", func(t *testing.T) {
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())
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())
err = require.CapturePanic(func() {
@@ -80,15 +81,15 @@ func TestLookupFunction(t *testing.T) {
})
require.Equal(t, "indirect call type mismatch", err.Error())
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())
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())
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())
})
+3 -3
View File
@@ -146,7 +146,7 @@ func (e *functionExporter) ExportFunctions(builder wazero.HostModuleBuilder) {
var abortMessageEnabled = &wasm.HostFunc{
ExportName: AbortName,
Name: "~lib/builtins/abort",
ParamTypes: []api.ValueType{i32, i32, i32, i32},
ParamTypes: []wasm.ValueType{i32, i32, i32, i32},
ParamNames: []string{"message", "fileName", "lineNumber", "columnNumber"},
Code: wasm.Code{GoFunc: api.GoModuleFunc(abortWithMessage)},
}
@@ -195,7 +195,7 @@ var traceDisabled = traceStdout.WithGoModuleFunc(func(context.Context, api.Modul
var traceStdout = &wasm.HostFunc{
ExportName: TraceName,
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"},
Code: wasm.Code{
GoFunc: api.GoModuleFunc(func(_ context.Context, mod api.Module, stack []uint64) {
@@ -281,7 +281,7 @@ func formatFloat(f float64) string {
var seed = &wasm.HostFunc{
ExportName: SeedName,
Name: "~lib/builtins/seed",
ResultTypes: []api.ValueType{f64},
ResultTypes: []wasm.ValueType{f64},
ResultNames: []string{"rand"},
Code: wasm.Code{
GoFunc: api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) {
+25 -26
View File
@@ -7,7 +7,6 @@ import (
"testing"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
@@ -137,44 +136,44 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{
{
ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
},
{
ExportName: "invoke_i",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i32}}}},
ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
},
{
ExportName: "invoke_p",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i32}}}},
ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i32}}}},
},
{
ExportName: "invoke_j",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
ResultTypes: []api.ValueType{i64},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{i64}}}},
ResultTypes: []wasm.ValueType{i64},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{i64}}}},
},
{
ExportName: "invoke_f",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
ResultTypes: []api.ValueType{f32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{f32}}}},
ResultTypes: []wasm.ValueType{f32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []wasm.ValueType{f32}}}},
},
{
ExportName: "invoke_d",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
ResultTypes: []api.ValueType{f64},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{Results: []api.ValueType{f64}}}},
ResultTypes: []wasm.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{
{
ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
},
@@ -231,7 +230,7 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{
{
ExportName: "invoke_v",
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"index"},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{}}},
},
@@ -255,9 +254,9 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{
{
ExportName: "invoke_vi",
ParamTypes: []api.ValueType{i32, i32},
ParamTypes: []wasm.ValueType{i32, i32},
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{
{
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"},
ResultTypes: []wasm.ValueType{i32},
Code: wasm.Code{GoFunc: &internal.InvokeFunc{FunctionType: &wasm.FunctionType{
Params: []api.ValueType{i32, i32, i32, i32},
Results: []api.ValueType{i32},
Params: []wasm.ValueType{i32, i32, i32, i32},
Results: []wasm.ValueType{i32},
}}},
},
},
@@ -310,10 +309,10 @@ func TestNewFunctionExporterForModule(t *testing.T) {
expected: []*wasm.HostFunc{
{
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"},
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 https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#args_get
// 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 {
sysCtx := mod.(*wasm.ModuleInstance).Sys
@@ -78,7 +78,7 @@ func argsGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno {
// See argsGet
// 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
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 {
sysCtx := mod.(*wasm.ModuleInstance).Sys
+2 -2
View File
@@ -37,7 +37,7 @@ import (
// 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://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 {
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.
// 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
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 {
sysCtx := mod.(*wasm.ModuleInstance).Sys
+2 -2
View File
@@ -41,7 +41,7 @@ import (
// See environSizesGet
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_get
// 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 {
sysCtx := mod.(*wasm.ModuleInstance).Sys
@@ -81,7 +81,7 @@ func environGetFn(_ context.Context, mod api.Module, params []uint64) sys.Errno
// See environGet
// https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#environ_sizes_get
// 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 {
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.
// See https://github.com/WebAssembly/WASI/blob/main/phases/snapshot/docs.md#fd_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 {
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.
//
// 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 {
fsc := mod.(*wasm.ModuleInstance).Sys.FS()
@@ -179,7 +179,7 @@ func fdDatasyncFn(_ context.Context, mod api.Module, params []uint64) experiment
// well as additional fields.
// See https://github.com/WebAssembly/WASI/blob/snapshot-01/phases/snapshot/docs.md#fdstat
// 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
// 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.
// 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
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
// 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
var fdPread = newHostFunc(
wasip1.FdPreadName, fdPreadFn,
[]api.ValueType{i32, i32, i32, i64, i32},
[]wasm.ValueType{i32, i32, i32, i64, i32},
"fd", "iovs", "iovs_len", "offset", "result.nread",
)
@@ -603,7 +603,7 @@ func fdPreadFn(_ context.Context, mod api.Module, params []uint64) experimentals
//
// See fdPrestatDirName and
// 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 {
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
var fdPrestatDirName = newHostFunc(
wasip1.FdPrestatDirNameName, fdPrestatDirNameFn,
[]api.ValueType{i32, i32, i32},
[]wasm.ValueType{i32, i32, i32},
"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
var fdPwrite = newHostFunc(
wasip1.FdPwriteName, fdPwriteFn,
[]api.ValueType{i32, i32, i32, i64, i32},
[]wasm.ValueType{i32, i32, i32, i64, i32},
"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
var fdRead = newHostFunc(
wasip1.FdReadName, fdReadFn,
[]api.ValueType{i32, i32, i32, i32},
[]wasm.ValueType{i32, i32, i32, i32},
"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
var fdSeek = newHostFunc(
wasip1.FdSeekName, fdSeekFn,
[]api.ValueType{i32, i64, i32, i32},
[]wasm.ValueType{i32, i64, i32, i32},
"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.
//
// 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 {
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.
//
// 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 {
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
var fdWrite = newHostFunc(
wasip1.FdWriteName, fdWriteFn,
[]api.ValueType{i32, i32, i32, i32},
[]wasm.ValueType{i32, i32, i32, i32},
"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
var pathFilestatGet = newHostFunc(
wasip1.PathFilestatGetName, pathFilestatGetFn,
[]api.ValueType{i32, i32, i32, i32, i32},
[]wasm.ValueType{i32, i32, i32, i32, i32},
"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
var pathOpen = newHostFunc(
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",
)
+1 -1
View File
@@ -38,7 +38,7 @@ import (
// See https://linux.die.net/man/3/poll
var pollOneoff = newHostFunc(
wasip1.PollOneoffName, pollOneoffFn,
[]api.ValueType{i32, i32, i32, i32},
[]wasm.ValueType{i32, i32, i32, i32},
"in", "out", "nsubscriptions", "result.nevents",
)
+2 -2
View File
@@ -21,7 +21,7 @@ import (
var procExit = &wasm.HostFunc{
ExportName: wasip1.ProcExitName,
Name: wasip1.ProcExitName,
ParamTypes: []api.ValueType{i32},
ParamTypes: []wasm.ValueType{i32},
ParamNames: []string{"rval"},
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.
//
// 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 --^
//
// 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 {
sysCtx := mod.(*wasm.ModuleInstance).Sys
+3 -3
View File
@@ -269,7 +269,7 @@ func writeOffsetsAndNullTerminatedValues(mem api.Memory, values [][]byte, offset
func newHostFunc(
name string,
goFunc wasiFunc,
paramTypes []api.ValueType,
paramTypes []wasm.ValueType,
paramNames ...string,
) *wasm.HostFunc {
return &wasm.HostFunc{
@@ -277,7 +277,7 @@ func newHostFunc(
Name: name,
ParamTypes: paramTypes,
ParamNames: paramNames,
ResultTypes: []api.ValueType{i32},
ResultTypes: []wasm.ValueType{i32},
ResultNames: []string{"errno"},
Code: wasm.Code{GoFunc: goFunc},
}
@@ -305,7 +305,7 @@ func stubFunction(name string, paramTypes []wasm.ValueType, paramNames ...string
Name: name,
ParamTypes: paramTypes,
ParamNames: paramNames,
ResultTypes: []api.ValueType{i32},
ResultTypes: []wasm.ValueType{i32},
ResultNames: []string{"errno"},
Code: wasm.Code{
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 {
// The type we invoke is the same type as the import except without the
// index parameter.
fn := &InvokeFunc{&wasm.FunctionType{Results: results}}
fn := &InvokeFunc{&wasm.FunctionType{Results: wasm.FromApiValueType(results)}}
if len(params) > 1 {
fn.FunctionType.Params = params[1:]
fn.FunctionType.Params = wasm.FromApiValueType(params[1:])
}
// Now, make friendly parameter names.
@@ -76,9 +76,9 @@ func NewInvokeFunc(importName string, params, results []api.ValueType) *wasm.Hos
}
return &wasm.HostFunc{
ExportName: importName,
ParamTypes: params,
ParamTypes: wasm.FromApiValueType(params),
ParamNames: paramNames,
ResultTypes: results,
ResultTypes: wasm.FromApiValueType(results),
Code: wasm.Code{GoFunc: fn},
}
}
+7 -7
View File
@@ -740,7 +740,7 @@ func TestCompile_Refs(t *testing.T) {
{
name: "ref.null (externref)",
body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeExternref,
wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeDrop,
wasm.OpcodeEnd,
},
@@ -753,7 +753,7 @@ func TestCompile_Refs(t *testing.T) {
{
name: "ref.null (funcref)",
body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeFuncref,
wasm.OpcodeRefNull, wasm.ValueTypeFuncref.Kind(),
wasm.OpcodeDrop,
wasm.OpcodeEnd,
},
@@ -781,7 +781,7 @@ func TestCompile_Refs(t *testing.T) {
{
name: "ref.is_null (externref)",
body: []byte{
wasm.OpcodeRefNull, wasm.ValueTypeExternref,
wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeRefIsNull,
wasm.OpcodeDrop,
wasm.OpcodeEnd,
@@ -838,7 +838,7 @@ func TestCompile_TableGetOrSet(t *testing.T) {
name: "table.set (externref)",
body: []byte{
wasm.OpcodeI32Const, 10,
wasm.OpcodeRefNull, wasm.ValueTypeExternref,
wasm.OpcodeRefNull, wasm.ValueTypeExternref.Kind(),
wasm.OpcodeTableSet, 0,
wasm.OpcodeEnd,
},
@@ -894,7 +894,7 @@ func TestCompile_TableGrowFillSize(t *testing.T) {
{
name: "table.grow",
body: []byte{
wasm.OpcodeRefNull, wasm.RefTypeFuncref,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(),
wasm.OpcodeI32Const, 1,
wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableGrow, 1,
wasm.OpcodeEnd,
@@ -911,7 +911,7 @@ func TestCompile_TableGrowFillSize(t *testing.T) {
name: "table.fill",
body: []byte{
wasm.OpcodeI32Const, 10,
wasm.OpcodeRefNull, wasm.RefTypeFuncref,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(),
wasm.OpcodeI32Const, 1,
wasm.OpcodeMiscPrefix, wasm.OpcodeMiscTableFill, 1,
wasm.OpcodeEnd,
@@ -2817,7 +2817,7 @@ func TestCompile_select_vectors(t *testing.T) {
wasm.OpcodeVecPrefix,
wasm.OpcodeVecV128Const, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0,
wasm.OpcodeI32Const, 0,
wasm.OpcodeTypedSelect, 0x1, wasm.ValueTypeV128,
wasm.OpcodeTypedSelect, 0x1, wasm.ValueTypeV128.Kind(),
wasm.OpcodeDrop,
wasm.OpcodeEnd,
}}},
@@ -345,7 +345,7 @@ var (
wasm.OpcodeLocalGet, 2, // x
wasm.OpcodeLocalGet, 3, // y
wasm.OpcodeLocalGet, 1, // cond
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeI64,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeI64.Kind(),
// f32 select.
wasm.OpcodeLocalGet, 4, // x
@@ -354,7 +354,7 @@ var (
wasm.OpcodeLocalGet, 6,
wasm.OpcodeLocalGet, 7,
wasm.OpcodeF64Gt,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF32,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF32.Kind(),
// f64 select.
wasm.OpcodeLocalGet, 6, // x
@@ -363,7 +363,7 @@ var (
wasm.OpcodeLocalGet, 4,
wasm.OpcodeLocalGet, 5,
wasm.OpcodeF32Ne,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF64,
wasm.OpcodeTypedSelect, 1, wasm.ValueTypeF64.Kind(),
wasm.OpcodeEnd,
}, nil),
@@ -155,8 +155,8 @@ func setupHostCallBench(requireNoError func(error)) *wasm.ModuleInstance {
// Build the importing module.
importingModuleBin := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{{
Params: []wasm.ValueType{i32},
Results: []wasm.ValueType{f32},
Params: []wasm.ValueType{wasm.ValueType(i32)},
Results: []wasm.ValueType{wasm.ValueType(f32)},
}},
ImportSection: []wasm.Import{
// 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) {
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)
})
t.Run("out of range", func(t *testing.T) {
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)
})
t.Run("type mismatch", func(t *testing.T) {
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)
})
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)
require.NoError(t, err)
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)
require.NoError(t, err)
require.Equal(t, uint64(1), res[0])
@@ -1103,7 +1103,7 @@ func testModuleMemory(t *testing.T, r wazero.Runtime) {
one := uint32(1)
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},
MemorySection: &wasm.Memory{Min: 1, Cap: 1, Max: 20},
DataSection: []wasm.DataSegment{
@@ -1445,23 +1445,23 @@ func testBeforeListenerStackIterator(t *testing.T, r wazero.Runtime) {
TypeSection: []wasm.FunctionType{
// f1 type
{
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32},
Results: []api.ValueType{},
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32},
Results: []wasm.ValueType{},
},
// f2 type
{
Params: []api.ValueType{},
Results: []api.ValueType{api.ValueTypeI32},
Params: []wasm.ValueType{},
Results: []wasm.ValueType{wasm.ValueTypeI32},
},
// f3 type
{
Params: []api.ValueType{api.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32},
Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []wasm.ValueType{wasm.ValueTypeI32},
},
// f4 type
{
Params: []api.ValueType{api.ValueTypeI32},
Results: []api.ValueType{api.ValueTypeI32},
Params: []wasm.ValueType{wasm.ValueTypeI32},
Results: []wasm.ValueType{wasm.ValueTypeI32},
},
},
ImportFunctionCount: 1,
@@ -1551,11 +1551,11 @@ func testListenerStackIteratorOffset(t *testing.T, r wazero.Runtime) {
encoded := binaryencoding.EncodeModule(&wasm.Module{
TypeSection: []wasm.FunctionType{
// f1 type
{Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32, api.ValueTypeI32}},
{Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32, wasm.ValueTypeI32}},
// f2 type
{Results: []api.ValueType{api.ValueTypeI32}},
{Results: []wasm.ValueType{wasm.ValueTypeI32}},
// 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},
NameSection: &wasm.NameSection{
@@ -141,9 +141,9 @@ func encodeModule(m *wasm.Module) []byte {
for _, ft := range m.TypeSection {
s = append(s, 0x60) // func type
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 = append(s, ft.Results...)
s = append(s, wasm.ToApiValueType(ft.Results)...)
}
return s
})
@@ -373,7 +373,7 @@ func Test888(t *testing.T) {
ValType: wasm.ValueTypeFuncref,
Mutable: false,
},
Init: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.ValueTypeFuncref}),
Init: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.ValueTypeFuncref.Kind()}),
},
},
ExportSection: []wasm.Export{
@@ -447,7 +447,7 @@ func RunCase(t *testing.T, testDataFS embed.FS, f string, ctx context.Context, c
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)
case "get":
_, exps := c.getAssertReturnArgsExps()
+8 -8
View File
@@ -46,9 +46,9 @@ func TestEncodeCode(t *testing.T) {
Body: addLocalZeroLocalOne,
},
expected: append([]byte{
0x09, // 9 bytes to encode locals and the body
0x01, // 1 local block
0x02, wasm.ValueTypeI32, // local block 1
0x09, // 9 bytes to encode locals and the body
0x01, // 1 local block
0x02, wasm.ValueTypeI32.Kind(), // local block 1
},
addLocalZeroLocalOne..., // Body
),
@@ -60,11 +60,11 @@ func TestEncodeCode(t *testing.T) {
Body: addLocalZeroLocalTwo,
},
expected: append([]byte{
0x0d, // 13 bytes to encode locals and the body
0x03, // 3 local blocks
0x01, wasm.ValueTypeI32, // local block 1
0x01, wasm.ValueTypeI64, // local block 2
0x01, wasm.ValueTypeI32, // local block 3
0x0d, // 13 bytes to encode locals and the body
0x03, // 3 local blocks
0x01, wasm.ValueTypeI32.Kind(), // local block 1
0x01, wasm.ValueTypeI64.Kind(), // local block 2
0x01, wasm.ValueTypeI32.Kind(), // local block 3
},
addLocalZeroLocalTwo..., // Body
),
+10 -10
View File
@@ -44,8 +44,8 @@ func TestModule_Encode(t *testing.T) {
wasm.SectionIDType, 0x12, // 18 bytes in this section
0x03, // 3 types
0x60, 0x00, 0x00, // func=0x60 no param no result
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
0x60, 0x04, i32, i32, i32, i32, 0x01, i32, // func=0x60 4 params and 1 result
0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 2 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...),
wasm.SectionIDType, 0x0d, // 13 bytes in this section
0x02, // 2 types
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
0x60, 0x02, f32, f32, 0x01, f32, // func=0x60 2 params and 1 result
0x02, // 2 types
0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // 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
0x02, // 2 imports
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...),
wasm.SectionIDTable, 0x04, // 4 bytes in this section
0x01, // 1 table
wasm.RefTypeFuncref, 0x0, 0x03, // func, only min: 3
0x01, // 1 table
wasm.RefTypeFuncref.Kind(), 0x0, 0x03, // func, only min: 3
wasm.SectionIDMemory, 0x04, // 4 bytes in this section
0x01, // 1 memory
0x01, 0x01, 0x01, // min and max = 1
@@ -143,8 +143,8 @@ func TestModule_Encode(t *testing.T) {
},
expected: append(append(Magic, version...),
wasm.SectionIDType, 0x07, // 7 bytes in this section
0x01, // 1 type
0x60, 0x02, i32, i32, 0x01, i32, // func=0x60 2 params and 1 result
0x01, // 1 type
0x60, 0x02, i32.Kind(), i32.Kind(), 0x01, i32.Kind(), // func=0x60 2 params and 1 result
wasm.SectionIDFunction, 0x02, // 2 bytes in this section
0x01, // 1 function
0x00, // func[0] type index 0
@@ -187,7 +187,7 @@ func TestModule_Encode(t *testing.T) {
},
expected: append(append(Magic, version...),
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.SectionIDExport, 0x06, // 6 bytes in this section
0x01, // 1 export
+1 -1
View File
@@ -12,7 +12,7 @@ func encodeGlobal(g wasm.Global) (data []byte) {
if g.Type.Mutable {
mutable = 1
}
data = []byte{g.Type.ValType, mutable}
data = []byte{g.Type.ValType.Kind(), mutable}
data = append(data, encodeConstantExpression(g.Init)...)
return
}
@@ -20,7 +20,7 @@ func TestEncodeGlobal(t *testing.T) {
Init: wasm.NewConstantExpressionFromI32(1),
},
expected: []byte{
wasm.ValueTypeI32, 0x00, // 0 == const
wasm.ValueTypeI32.Kind(), 0x00, // 0 == const
wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
},
},
@@ -31,7 +31,7 @@ func TestEncodeGlobal(t *testing.T) {
Init: wasm.NewConstantExpressionFromI32(1),
},
expected: []byte{
wasm.ValueTypeI32, 0x01, // 1 == var
wasm.ValueTypeI32.Kind(), 0x01, // 1 == var
wasm.OpcodeI32Const, 0x01, wasm.OpcodeEnd,
},
},
+2 -2
View File
@@ -18,7 +18,7 @@ func EncodeImport(i *wasm.Import) []byte {
case wasm.ExternTypeFunc:
data = append(data, leb128.EncodeUint32(i.DescFunc)...)
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)...)
case wasm.ExternTypeMemory:
maxPtr := &i.DescMem.Max
@@ -32,7 +32,7 @@ func EncodeImport(i *wasm.Import) []byte {
if g.Mutable {
mutable = 1
}
data = append(data, g.ValType, mutable)
data = append(data, g.ValType.Kind(), mutable)
case wasm.ExternTypeTag:
data = append(data, 0x00) // attribute byte
data = append(data, leb128.EncodeUint32(i.DescTag)...)
@@ -84,7 +84,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i',
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',
0x02, 'p', 'i',
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',
0x05, 't', 'a', 'b', 'l', 'e',
wasm.ExternTypeTable,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
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
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.
var encodedValTypes = map[wasm.ValueType][]byte{
wasm.ValueTypeI32: {1, wasm.ValueTypeI32},
wasm.ValueTypeI64: {1, wasm.ValueTypeI64},
wasm.ValueTypeF32: {1, wasm.ValueTypeF32},
wasm.ValueTypeF64: {1, wasm.ValueTypeF64},
wasm.ValueTypeExternref: {1, wasm.ValueTypeExternref},
wasm.ValueTypeFuncref: {1, wasm.ValueTypeFuncref},
wasm.ValueTypeV128: {1, wasm.ValueTypeV128},
wasm.ValueTypeI32: {1, wasm.ValueTypeI32.Kind()},
wasm.ValueTypeI64: {1, wasm.ValueTypeI64.Kind()},
wasm.ValueTypeF32: {1, wasm.ValueTypeF32.Kind()},
wasm.ValueTypeF64: {1, wasm.ValueTypeF64.Kind()},
wasm.ValueTypeExternref: {1, wasm.ValueTypeExternref.Kind()},
wasm.ValueTypeFuncref: {1, wasm.ValueTypeFuncref.Kind()},
wasm.ValueTypeV128: {1, wasm.ValueTypeV128.Kind()},
}
// EncodeValTypes fast paths binary encoding of common value type lengths
@@ -29,13 +29,14 @@ func EncodeValTypes(vt []wasm.ValueType) []byte {
return encoded
}
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
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
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
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",
input: []wasm.ValueType{fref},
expected: []byte{1, fref},
expected: []byte{1, fref.Kind()},
},
{
name: "externref",
input: []wasm.ValueType{ext},
expected: []byte{1, ext},
expected: []byte{1, ext.Kind()},
},
{
name: "i32",
input: []wasm.ValueType{i32},
expected: []byte{1, i32},
expected: []byte{1, i32.Kind()},
},
{
name: "i64",
input: []wasm.ValueType{i64},
expected: []byte{1, i64},
expected: []byte{1, i64.Kind()},
},
{
name: "f32",
input: []wasm.ValueType{f32},
expected: []byte{1, f32},
expected: []byte{1, f32.Kind()},
},
{
name: "f64",
input: []wasm.ValueType{f64},
expected: []byte{1, f64},
expected: []byte{1, f64.Kind()},
},
{
name: "i32i64",
input: []wasm.ValueType{i32, i64},
expected: []byte{2, i32, i64},
expected: []byte{2, i32.Kind(), i64.Kind()},
},
{
name: "i32i64f32",
input: []wasm.ValueType{i32, i64, f32},
expected: []byte{3, i32, i64, f32},
expected: []byte{3, i32.Kind(), i64.Kind(), f32.Kind()},
},
{
name: "i32i64f32f64",
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",
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",
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",
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",
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",
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",
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,
})
case wasm.ValueTypeExternref:
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeExternref})
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind()})
case wasm.ValueTypeFuncref:
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref})
body.Write([]byte{wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind()})
}
}
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}
case wasm.ValueTypeExternref:
opcode = wasm.OpcodeRefNull
data = []byte{wasm.RefTypeExternref}
data = []byte{wasm.RefTypeExternref.Kind()}
case wasm.ValueTypeFuncref:
opcode = wasm.OpcodeRefNull
data = []byte{wasm.RefTypeFuncref}
data = []byte{wasm.RefTypeFuncref.Kind()}
}
m.GlobalSection = append(m.GlobalSection, wasm.Global{
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
for _, def := range funcDefs {
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.
+2 -2
View File
@@ -45,7 +45,7 @@ func decodeCode(r *bytes.Reader, codeSectionStart uint64, ret *wasm.Code) (err e
}
bytesRead += n + 1
switch vt := b; vt {
switch vt := b; wasm.ValueType(vt) {
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64,
wasm.ValueTypeFuncref, wasm.ValueTypeExternref, wasm.ValueTypeV128,
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++ {
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 {
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 {
return fmt.Errorf("read reference type for ref.null: %w", err)
} else if reftype != wasm.RefTypeFuncref && reftype != wasm.RefTypeExternref {
+5 -5
View File
@@ -35,18 +35,18 @@ func TestDecodeConstantExpression(t *testing.T) {
{
in: []byte{
wasm.OpcodeRefNull,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
wasm.OpcodeEnd,
},
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
{
in: []byte{
wasm.OpcodeRefNull,
wasm.RefTypeExternref,
wasm.RefTypeExternref.Kind(),
wasm.OpcodeEnd,
},
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref}),
exp: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref.Kind()}),
},
{
in: []byte{
@@ -124,7 +124,7 @@ func TestDecodeConstantExpression_errors(t *testing.T) {
{
in: []byte{
wasm.OpcodeRefNull,
wasm.RefTypeExternref,
wasm.RefTypeExternref.Kind(),
wasm.OpcodeEnd,
},
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) {
ret, err = r.ReadByte()
if err != nil {
err = fmt.Errorf("read element ref type: %w", err)
b, e := r.ReadByte()
ret = wasm.ValueType(b)
if e != nil {
err = fmt.Errorf("read element ref type: %w", e)
return
}
if ret != wasm.RefTypeFuncref && ret != wasm.RefTypeExternref {
+36 -36
View File
@@ -75,11 +75,11 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{
in: []byte{
2, // Two indexes.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc, 100, wasm.OpcodeEnd,
},
exp: []wasm.ConstantExpression{
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefFunc, []byte{100}),
},
refType: wasm.RefTypeFuncref,
@@ -88,18 +88,18 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{
in: []byte{
4, // Four indexes.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
},
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.OpcodeGlobalGet, leb128.EncodeUint32(1)),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
refType: wasm.RefTypeFuncref,
features: api.CoreFeatureBulkMemoryOperations,
@@ -107,14 +107,14 @@ func Test_decodeElementConstExprVector(t *testing.T) {
{
in: []byte{
3, // Three indexes.
wasm.OpcodeRefNull, wasm.RefTypeExternref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeGlobalGet, 1, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeExternref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeExternref.Kind(), wasm.OpcodeEnd,
},
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.OpcodeRefNull, []byte{wasm.RefTypeExternref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeExternref.Kind()}),
},
refType: wasm.RefTypeExternref,
features: api.CoreFeatureBulkMemoryOperations,
@@ -145,7 +145,7 @@ func Test_decodeElementConstExprVector_errors(t *testing.T) {
},
{
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",
},
{
@@ -335,18 +335,18 @@ func TestDecodeElementSegment(t *testing.T) {
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
// Init const expr vector.
3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
},
exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
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.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref,
@@ -357,20 +357,20 @@ func TestDecodeElementSegment(t *testing.T) {
name: "passive const expr vector - funcref",
in: []byte{
5, // Prefix.
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
// Init const expr vector.
3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
},
exp: wasm.ElementSegment{
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.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
Mode: wasm.ElementModePassive,
Type: wasm.RefTypeFuncref,
@@ -393,21 +393,21 @@ func TestDecodeElementSegment(t *testing.T) {
0,
// Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
// Init const expr vector.
3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
},
exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
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.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref,
@@ -421,21 +421,21 @@ func TestDecodeElementSegment(t *testing.T) {
10,
// Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
// Init const expr vector.
3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
},
exp: wasm.ElementSegment{
OffsetExpr: wasm.NewConstantExpressionFromOpcode(wasm.OpcodeI32Const, []byte{0x80, 1}),
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.OpcodeRefNull, []byte{wasm.RefTypeFuncref}),
wasm.NewConstantExpressionFromOpcode(wasm.OpcodeRefNull, []byte{wasm.RefTypeFuncref.Kind()}),
},
Mode: wasm.ElementModeActive,
Type: wasm.RefTypeFuncref,
@@ -450,14 +450,14 @@ func TestDecodeElementSegment(t *testing.T) {
10,
// Offset expr.
wasm.OpcodeI32Const, 0x80, 1, wasm.OpcodeEnd,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
// Init const expr vector.
3, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x80, 0x80, 0x4f, // 165675008 in varint encoding.
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`,
features: api.CoreFeatureBulkMemoryOperations,
@@ -466,17 +466,17 @@ func TestDecodeElementSegment(t *testing.T) {
name: "declarative const expr vector",
in: []byte{
7, // Prefix.
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
// Init const expr vector.
2, // number of const expr.
wasm.OpcodeRefNull, wasm.RefTypeFuncref, wasm.OpcodeEnd,
wasm.OpcodeRefNull, wasm.RefTypeFuncref.Kind(), wasm.OpcodeEnd,
wasm.OpcodeRefFunc,
0x80, 0x7f,
wasm.OpcodeEnd,
},
exp: wasm.ElementSegment{
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)),
},
Mode: wasm.ElementModeDeclarative,
+11 -11
View File
@@ -26,52 +26,52 @@ func TestFunctionType(t *testing.T) {
{
name: "one param no result",
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",
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",
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",
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",
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",
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",
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",
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",
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",
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) {
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
i32, i64 := wasm.ValueTypeI32.Kind(), wasm.ValueTypeI64.Kind()
tests := []struct {
name string
input []byte
+3 -3
View File
@@ -85,7 +85,7 @@ func TestEncodeImport(t *testing.T) {
0x04, 'm', 'a', 't', 'h',
0x02, 'p', 'i',
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',
0x02, 'p', 'i',
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',
0x05, 't', 'a', 'b', 'l', 'e',
wasm.ExternTypeTable,
wasm.RefTypeFuncref,
wasm.RefTypeFuncref.Kind(),
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",
input: []byte{
0x01, // 1 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
0x01, // 1 table
wasm.RefTypeFuncref.Kind(), 0x01, 2, 3, // (table 2 3)
},
expected: []wasm.Table{{Min: 2, Max: &three, Type: wasm.RefTypeFuncref}},
},
{
name: "min and min with max - three tables",
input: []byte{
0x03, // 3 table
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
wasm.RefTypeExternref, 0x01, 2, 3, // (table 2 3)
wasm.RefTypeFuncref, 0x01, 2, 3, // (table 2 3)
0x03, // 3 table
wasm.RefTypeFuncref.Kind(), 0x01, 2, 3, // (table 2 3)
wasm.RefTypeExternref.Kind(), 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},
@@ -62,9 +62,9 @@ func TestTableSection_Errors(t *testing.T) {
{
name: "min and min with max",
input: []byte{
0x02, // 2 tables
wasm.RefTypeFuncref, 0x00, 0x01, // (table 1)
wasm.RefTypeFuncref, 0x01, 0x02, 0x03, // (table 2 3)
0x02, // 2 tables
wasm.RefTypeFuncref.Kind(), 0x00, 0x01, // (table 1)
wasm.RefTypeFuncref.Kind(), 0x01, 0x02, 0x03, // (table 2 3)
},
expectedErr: "at most one table allowed in module as feature \"reference-types\" is disabled",
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
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 {
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",
input: wasm.Table{Type: wasm.RefTypeFuncref},
expected: []byte{wasm.RefTypeFuncref, 0x0, 0},
expected: []byte{wasm.RefTypeFuncref.Kind(), 0x0, 0},
},
{
name: "min 0 - externref",
input: wasm.Table{Type: wasm.RefTypeExternref},
expected: []byte{wasm.RefTypeExternref, 0x0, 0},
expected: []byte{wasm.RefTypeExternref.Kind(), 0x0, 0},
},
{
name: "min 0, max 0",
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",
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",
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",
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",
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",
features: api.CoreFeatureReferenceTypes,
},
{
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",
features: api.CoreFeatureReferenceTypes,
},
{
name: "shared",
input: []byte{wasm.RefTypeFuncref, 0x2, 0},
input: []byte{wasm.RefTypeFuncref.Kind(), 0x2, 0},
expectedErr: "tables cannot be marked as shared",
// Shared tables are an error even if threads are enabled.
features: experimental.CoreFeaturesThreads,
+4 -4
View File
@@ -23,10 +23,10 @@ func decodeValueTypes(r *bytes.Reader, num uint32) ([]wasm.ValueType, error) {
return nil, err
}
switch b {
case wasm.ValueTypeI32, wasm.ValueTypeF32, wasm.ValueTypeI64, wasm.ValueTypeF64,
wasm.ValueTypeExternref, wasm.ValueTypeFuncref, wasm.ValueTypeV128,
wasm.ValueTypeExnref:
ret = append(ret, b)
case wasm.ValueTypeI32.Kind(), wasm.ValueTypeF32.Kind(), wasm.ValueTypeI64.Kind(), wasm.ValueTypeF64.Kind(),
wasm.ValueTypeExternref.Kind(), wasm.ValueTypeFuncref.Kind(), wasm.ValueTypeV128.Kind(),
wasm.ValueTypeExnref.Kind():
ret = append(ret, wasm.ValueType(b))
case wasm.RefPrefixNullable, wasm.RefPrefixNonNullable:
ht, _, err := leb128.DecodeInt33AsInt64(r)
if err != nil {
+15 -15
View File
@@ -29,77 +29,77 @@ func TestEncodeValTypes(t *testing.T) {
{
name: "funcref",
input: []wasm.ValueType{fref},
expected: []byte{1, fref},
expected: []byte{1, fref.Kind()},
},
{
name: "externref",
input: []wasm.ValueType{ext},
expected: []byte{1, ext},
expected: []byte{1, ext.Kind()},
},
{
name: "i32",
input: []wasm.ValueType{i32},
expected: []byte{1, i32},
expected: []byte{1, i32.Kind()},
},
{
name: "i64",
input: []wasm.ValueType{i64},
expected: []byte{1, i64},
expected: []byte{1, i64.Kind()},
},
{
name: "f32",
input: []wasm.ValueType{f32},
expected: []byte{1, f32},
expected: []byte{1, f32.Kind()},
},
{
name: "f64",
input: []wasm.ValueType{f64},
expected: []byte{1, f64},
expected: []byte{1, f64.Kind()},
},
{
name: "i32i64",
input: []wasm.ValueType{i32, i64},
expected: []byte{2, i32, i64},
expected: []byte{2, i32.Kind(), i64.Kind()},
},
{
name: "i32i64f32",
input: []wasm.ValueType{i32, i64, f32},
expected: []byte{3, i32, i64, f32},
expected: []byte{3, i32.Kind(), i64.Kind(), f32.Kind()},
},
{
name: "i32i64f32f64",
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",
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",
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",
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",
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",
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",
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 {
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 {
@@ -997,7 +997,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
switch op {
case OpcodeRefNull:
pc++
switch reftype := body[pc]; reftype {
switch reftype := body[pc]; ValueType(reftype) {
case ValueTypeExternref:
valueTypeStack.push(ValueTypeExternref)
case ValueTypeFuncref:
@@ -1041,7 +1041,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
refType := tables[tableIndex].Type
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)
}
valueTypeStack.push(refType)
@@ -1049,7 +1049,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
if err := valueTypeStack.popAndVerifyType(refType); err != nil {
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)
}
}
@@ -2028,7 +2028,7 @@ func (m *Module) validateFunctionWithMaxStackValues(
if ifMissingElse {
// 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.
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)
}
// -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))
}
pc++
tp := body[pc]
tp := ValueType(body[pc])
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)
}
} 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)",
flag: api.CoreFeatureReferenceTypes,
body: []byte{
OpcodeRefNull, ValueTypeFuncref,
OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeDrop, OpcodeEnd,
},
},
@@ -2281,7 +2281,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.null (externref)",
flag: api.CoreFeatureReferenceTypes,
body: []byte{
OpcodeRefNull, ValueTypeExternref,
OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeDrop, OpcodeEnd,
},
},
@@ -2289,7 +2289,7 @@ func TestModule_funcValidation_RefTypes(t *testing.T) {
name: "ref.null - disabled",
flag: api.CoreFeaturesV1,
body: []byte{
OpcodeRefNull, ValueTypeFuncref,
OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeDrop, OpcodeEnd,
},
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",
flag: api.CoreFeatureReferenceTypes,
body: []byte{
OpcodeRefNull, ValueTypeFuncref,
OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeRefIsNull,
OpcodeDrop, OpcodeEnd,
},
@@ -2373,7 +2373,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{
name: "table.grow (funcref)",
body: []byte{
OpcodeRefNull, RefTypeFuncref,
OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow,
0, // Table Index.
@@ -2385,7 +2385,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{
name: "table.grow (funcref) - type mismatch",
body: []byte{
OpcodeRefNull, RefTypeFuncref,
OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow,
1, // Table of externref type -> mismatch.
@@ -2397,7 +2397,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{
name: "table.grow (externref)",
body: []byte{
OpcodeRefNull, RefTypeExternref,
OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow,
1, // Table Index.
@@ -2409,7 +2409,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{
name: "table.grow (externref) type mismatch",
body: []byte{
OpcodeRefNull, RefTypeExternref,
OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow,
0, // Table of funcref type -> mismatch.
@@ -2421,7 +2421,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
{
name: "table.grow - table not found",
body: []byte{
OpcodeRefNull, RefTypeFuncref,
OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableGrow,
10, // Table Index.
@@ -2454,7 +2454,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (funcref)",
body: []byte{
OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeFuncref,
OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill,
0, // Table Index.
@@ -2466,7 +2466,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (funcref) - type mismatch",
body: []byte{
OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeFuncref,
OpcodeRefNull, RefTypeFuncref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill,
1, // Table of externref type -> mismatch.
@@ -2479,7 +2479,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (externref)",
body: []byte{
OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeExternref,
OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill,
1, // Table Index.
@@ -2491,7 +2491,7 @@ func TestModule_funcValidation_TableGrowSizeFill(t *testing.T) {
name: "table.fill (externref) - type mismatch",
body: []byte{
OpcodeI32Const, 1, // offset
OpcodeRefNull, RefTypeExternref,
OpcodeRefNull, RefTypeExternref.Kind(),
OpcodeI32Const, 1, // number of elements
OpcodeMiscPrefix, OpcodeMiscTableFill,
0, // Table of funcref type -> mismatch.
@@ -2576,7 +2576,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set (funcref)",
body: []byte{
OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeFuncref,
OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeTableSet, 0,
OpcodeEnd,
},
@@ -2586,7 +2586,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set type mismatch (src=funcref, dst=externref)",
body: []byte{
OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeFuncref,
OpcodeRefNull, ValueTypeFuncref.Kind(),
OpcodeTableSet, 1,
OpcodeEnd,
},
@@ -2597,7 +2597,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set (externref)",
body: []byte{
OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeExternref,
OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeTableSet, 1,
OpcodeEnd,
},
@@ -2607,7 +2607,7 @@ func TestModule_funcValidation_TableGetSet(t *testing.T) {
name: "table.set type mismatch (src=externref, dst=funcref)",
body: []byte{
OpcodeI32Const, 0,
OpcodeRefNull, ValueTypeExternref,
OpcodeRefNull, ValueTypeExternref.Kind(),
OpcodeTableSet, 0,
OpcodeEnd,
},
@@ -2655,7 +2655,7 @@ func TestModule_funcValidation_Select_error(t *testing.T) {
name: "typed_select (disabled)",
body: []byte{
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,
OpcodeEnd,
},
+26 -4
View File
@@ -168,8 +168,8 @@ func (f *FunctionDefinition) GoFunction() interface{} {
}
// ParamTypes implements api.FunctionDefinition ParamTypes.
func (f *FunctionDefinition) ParamTypes() []ValueType {
return f.Functype.Params
func (f *FunctionDefinition) ParamTypes() []api.ValueType {
return ToApiValueType(f.Functype.Params)
}
// ParamNames implements the same method as documented on api.FunctionDefinition.
@@ -178,11 +178,33 @@ func (f *FunctionDefinition) ParamNames() []string {
}
// ResultTypes implements api.FunctionDefinition ResultTypes.
func (f *FunctionDefinition) ResultTypes() []ValueType {
return f.Functype.Results
func (f *FunctionDefinition) ResultTypes() []api.ValueType {
return ToApiValueType(f.Functype.Results)
}
// ResultNames implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) ResultNames() []string {
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.
func (g constantGlobal) Type() api.ValueType {
return g.g.Type.ValType
return api.ValueType(g.g.Type.ValType)
}
// Get implements api.Global.
@@ -35,7 +35,7 @@ type mutableGlobal struct {
// Type implements api.Global.
func (g mutableGlobal) Type() api.ValueType {
return g.g.Type.ValType
return api.ValueType(g.g.Type.ValType)
}
// Get implements api.Global.
+12 -12
View File
@@ -27,7 +27,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32},
Val: 1,
}},
expectedType: ValueTypeI32,
expectedType: ValueTypeI32.Kind(),
expectedVal: 1,
expectedString: "global(1)",
},
@@ -37,7 +37,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32},
Val: math.MaxInt32,
}},
expectedType: ValueTypeI32,
expectedType: ValueTypeI32.Kind(),
expectedVal: math.MaxInt32,
expectedString: "global(2147483647)",
},
@@ -47,7 +47,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64},
Val: 1,
}},
expectedType: ValueTypeI64,
expectedType: ValueTypeI64.Kind(),
expectedVal: 1,
expectedString: "global(1)",
},
@@ -57,7 +57,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64},
Val: math.MaxInt64,
}},
expectedType: ValueTypeI64,
expectedType: ValueTypeI64.Kind(),
expectedVal: math.MaxInt64,
expectedString: "global(9223372036854775807)",
},
@@ -67,7 +67,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32},
Val: api.EncodeF32(1.0),
}},
expectedType: ValueTypeF32,
expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(1.0),
expectedString: "global(1.000000)",
},
@@ -77,7 +77,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32},
Val: api.EncodeF32(math.MaxFloat32),
}},
expectedType: ValueTypeF32,
expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(math.MaxFloat32),
expectedString: "global(340282346638528859811704183484516925440.000000)",
},
@@ -87,7 +87,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64},
Val: api.EncodeF64(1.0),
}},
expectedType: ValueTypeF64,
expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(1.0),
expectedString: "global(1.000000)",
},
@@ -97,7 +97,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64},
Val: api.EncodeF64(math.MaxFloat64),
}},
expectedType: ValueTypeF64,
expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(math.MaxFloat64),
expectedString: "global(179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.000000)",
},
@@ -107,7 +107,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI32, Mutable: true},
Val: 1,
}},
expectedType: ValueTypeI32,
expectedType: ValueTypeI32.Kind(),
expectedVal: 1,
expectedString: "global(1)",
expectedMutable: true,
@@ -118,7 +118,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeI64, Mutable: true},
Val: 1,
}},
expectedType: ValueTypeI64,
expectedType: ValueTypeI64.Kind(),
expectedVal: 1,
expectedString: "global(1)",
expectedMutable: true,
@@ -129,7 +129,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF32, Mutable: true},
Val: api.EncodeF32(1.0),
}},
expectedType: ValueTypeF32,
expectedType: ValueTypeF32.Kind(),
expectedVal: api.EncodeF32(1.0),
expectedString: "global(1.000000)",
expectedMutable: true,
@@ -140,7 +140,7 @@ func TestGlobalTypes(t *testing.T) {
Type: GlobalType{ValType: ValueTypeF64, Mutable: true},
Val: api.EncodeF64(1.0),
}},
expectedType: ValueTypeF64,
expectedType: ValueTypeF64.Kind(),
expectedVal: api.EncodeF64(1.0),
expectedString: "global(1.000000)",
expectedMutable: true,
+3 -3
View File
@@ -1,12 +1,12 @@
package wasm
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"reflect"
"slices"
"github.com/tetratelabs/wazero/api"
)
@@ -47,7 +47,7 @@ func (f *reflectGoModuleFunction) EqualTo(that interface{}) bool {
return false
} else {
// 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 {
// TODO compare reflect pointers
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"
"errors"
"fmt"
"slices"
"sort"
"strings"
"sync"
@@ -776,7 +777,7 @@ func (f *FunctionType) CacheNumInUint64() {
// EqualsSignature returns true if the function type has the same parameters and results.
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
@@ -1125,21 +1126,24 @@ func SectionIDName(sectionID SectionID) string {
return "unknown"
}
// ValueType is an alias of api.ValueType defined to simplify imports.
type ValueType = api.ValueType
// ValueType represents a WebAssembly value type as a uint64.
//
// 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 (
ValueTypeI32 = api.ValueTypeI32
ValueTypeI64 = api.ValueTypeI64
ValueTypeF32 = api.ValueTypeF32
ValueTypeF64 = api.ValueTypeF64
// TODO: ValueTypeV128 is not exposed in the api pkg yet.
ValueTypeV128 ValueType = 0x7b
// TODO: ValueTypeFuncref is not exposed in the api pkg yet.
ValueTypeI32 ValueType = 0x7f
ValueTypeI64 ValueType = 0x7e
ValueTypeF32 ValueType = 0x7d
ValueTypeF64 ValueType = 0x7c
ValueTypeV128 ValueType = 0x7b
ValueTypeFuncref ValueType = 0x70
ValueTypeExternref = api.ValueTypeExternref
// ValueTypeExnref is the exception reference type used in exception handling.
ValueTypeExnref ValueType = 0x69
ValueTypeExternref ValueType = 0x6f
ValueTypeExnref ValueType = 0x69
)
const (
@@ -1167,7 +1171,11 @@ func ValueTypeName(t ValueType) string {
} else if t == ValueTypeExnref {
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 {
@@ -1207,7 +1215,7 @@ const (
)
// ExternTypeName is an alias of api.ExternTypeName defined to simplify imports.
func ExternTypeName(t ValueType) string {
func ExternTypeName(t ExternType) string {
if t == ExternTypeTag {
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")
})
t.Run("ref.null", func(t *testing.T) {
expr := NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref})
expr := NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref.Kind()})
err := validateConstExpression(nil, 0,
&expr,
ValueTypeFuncref)
require.NoError(t, err)
expr = NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref})
expr = NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()})
err = validateConstExpression(nil, 0,
&expr,
ValueTypeExternref)
@@ -799,11 +799,11 @@ func TestModule_buildGlobals(t *testing.T) {
},
{
Type: GlobalType{Mutable: false, ValType: ValueTypeExternref},
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
},
{
Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref},
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref}),
Init: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeFuncref.Kind()}),
},
{
Type: GlobalType{Mutable: false, ValType: ValueTypeFuncref},
@@ -948,7 +948,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeActive,
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
},
},
@@ -956,7 +956,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeDeclarative,
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
},
},
@@ -965,8 +965,8 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
},
},
},
@@ -993,7 +993,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeActive,
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{0}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
},
},
@@ -1001,7 +1001,7 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Mode: ElementModeDeclarative,
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{1}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
},
},
@@ -1010,8 +1010,8 @@ func TestModule_declaredFunctionIndexes(t *testing.T) {
Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{5}),
NewConstantExpressionFromOpcode(OpcodeRefFunc, []byte{2}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{ValueTypeExternref.Kind()}),
},
},
},
+6 -6
View File
@@ -602,11 +602,11 @@ func TestGlobalInstance_initialize(t *testing.T) {
}{
{
name: "ref.null (externref)",
expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref}),
expr: NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
},
{
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
t.Run(tc.name, func(t *testing.T) {
g := GlobalInstance{}
g.Type.ValType = tc.expr.Data[0]
g.Type.ValType = ValueType(tc.expr.Data[0])
g.initialize(nil, &tc.expr, nil)
require.Equal(t, uint64(0), g.Val)
})
@@ -713,7 +713,7 @@ func Test_resolveImports(t *testing.T) {
Source: &Module{
FunctionSection: []Index{0, 0, 1, 0, 0},
TypeSection: []FunctionType{
{Params: []ValueType{ExternTypeFunc}},
{Params: []ValueType{ValueType(ExternTypeFunc)}},
{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}},
},
},
@@ -722,7 +722,7 @@ func Test_resolveImports(t *testing.T) {
module := &Module{
TypeSection: []FunctionType{
{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}},
{Params: []ValueType{ExternTypeFunc}},
{Params: []ValueType{ValueType(ExternTypeFunc)}},
},
ImportFunctionCount: 2,
ImportPerModule: map[string][]*Import{
@@ -1020,7 +1020,7 @@ func TestModuleInstance_applyElements(t *testing.T) {
m.applyElements([]ElementSegment{
{Mode: ElementModeActive, OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefFunc, leb128.EncodeInt32(0)),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeFuncref.Kind()}),
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.
type RefType = byte
type RefType = ValueType
const (
// 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}},
ElementSection: []ElementSegment{
{OffsetExpr: NewConstantExpressionFromI32(5), Init: []ConstantExpression{
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{byte(RefTypeExternref)}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
NewConstantExpressionFromOpcode(OpcodeRefNull, []byte{RefTypeExternref.Kind()}),
}}, // three null refs.
},
},
@@ -886,7 +886,7 @@ func TestModule_buildTables(t *testing.T) {
ElementSection: []ElementSegment{
{
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,
},
{
+2 -2
View File
@@ -67,7 +67,7 @@ func TestRuntime_CompileModule(t *testing.T) {
{
name: "FunctionSection, but not exported",
wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}},
TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
},
@@ -79,7 +79,7 @@ func TestRuntime_CompileModule(t *testing.T) {
{
name: "FunctionSection exported",
wasm: &wasm.Module{
TypeSection: []wasm.FunctionType{{Params: []api.ValueType{api.ValueTypeI32}}},
TypeSection: []wasm.FunctionType{{Params: []wasm.ValueType{wasm.ValueTypeI32}}},
FunctionSection: []wasm.Index{0},
CodeSection: []wasm.Code{{Body: []byte{wasm.OpcodeEnd}}},
ExportSection: []wasm.Export{{