Skip to content

Commit a3374cf

Browse files
authored
refactor: wasm.ValueType as type definition + widening (uint64) (#2495)
1 parent 8755611 commit a3374cf

59 files changed

Lines changed: 426 additions & 392 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

builder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,13 +243,13 @@ type hostFunctionBuilder struct {
243243

244244
// WithGoFunction implements HostFunctionBuilder.WithGoFunction
245245
func (h *hostFunctionBuilder) WithGoFunction(fn api.GoFunction, params, results []api.ValueType) HostFunctionBuilder {
246-
h.fn = &wasm.HostFunc{ParamTypes: params, ResultTypes: results, Code: wasm.Code{GoFunc: fn}}
246+
h.fn = &wasm.HostFunc{ParamTypes: wasm.FromApiValueType(params), ResultTypes: wasm.FromApiValueType(results), Code: wasm.Code{GoFunc: fn}}
247247
return h
248248
}
249249

250250
// WithGoModuleFunction implements HostFunctionBuilder.WithGoModuleFunction
251251
func (h *hostFunctionBuilder) WithGoModuleFunction(fn api.GoModuleFunction, params, results []api.ValueType) HostFunctionBuilder {
252-
h.fn = &wasm.HostFunc{ParamTypes: params, ResultTypes: results, Code: wasm.Code{GoFunc: fn}}
252+
h.fn = &wasm.HostFunc{ParamTypes: wasm.FromApiValueType(params), ResultTypes: wasm.FromApiValueType(results), Code: wasm.Code{GoFunc: fn}}
253253
return h
254254
}
255255

builder_test.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
// TestNewHostModuleBuilder_Compile only covers a few scenarios to avoid duplicating tests in internal/wasm/host_test.go
1313
func TestNewHostModuleBuilder_Compile(t *testing.T) {
14-
i32, i64 := api.ValueTypeI32, api.ValueTypeI64
14+
i32, i64 := wasm.ValueTypeI32, wasm.ValueTypeI64
1515

1616
uint32_uint32 := func(context.Context, uint32) uint32 {
1717
return 0
@@ -54,7 +54,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
5454
},
5555
expected: &wasm.Module{
5656
TypeSection: []wasm.FunctionType{
57-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
57+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
5858
},
5959
FunctionSection: []wasm.Index{0},
6060
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -80,7 +80,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
8080
},
8181
expected: &wasm.Module{
8282
TypeSection: []wasm.FunctionType{
83-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
83+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
8484
},
8585
FunctionSection: []wasm.Index{0},
8686
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -107,7 +107,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
107107
},
108108
expected: &wasm.Module{
109109
TypeSection: []wasm.FunctionType{
110-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
110+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
111111
},
112112
FunctionSection: []wasm.Index{0},
113113
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -133,7 +133,7 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
133133
},
134134
expected: &wasm.Module{
135135
TypeSection: []wasm.FunctionType{
136-
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
136+
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
137137
},
138138
FunctionSection: []wasm.Index{0},
139139
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32)},
@@ -159,8 +159,8 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
159159
},
160160
expected: &wasm.Module{
161161
TypeSection: []wasm.FunctionType{
162-
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
163-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
162+
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
163+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
164164
},
165165
FunctionSection: []wasm.Index{0, 1},
166166
CodeSection: []wasm.Code{wasm.MustParseGoReflectFuncCode(uint64_uint32), wasm.MustParseGoReflectFuncCode(uint32_uint32)},
@@ -183,12 +183,12 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
183183
input: func(r Runtime) HostModuleBuilder {
184184
return r.NewHostModuleBuilder("host").
185185
NewFunctionBuilder().
186-
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}).
186+
WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
187187
Export("1")
188188
},
189189
expected: &wasm.Module{
190190
TypeSection: []wasm.FunctionType{
191-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
191+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
192192
},
193193
FunctionSection: []wasm.Index{0},
194194
CodeSection: []wasm.Code{
@@ -210,13 +210,13 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
210210
name: "WithGoFunction WithName WithParameterNames",
211211
input: func(r Runtime) HostModuleBuilder {
212212
return r.NewHostModuleBuilder("host").NewFunctionBuilder().
213-
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}).
213+
WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
214214
WithName("get").WithParameterNames("x").
215215
Export("1")
216216
},
217217
expected: &wasm.Module{
218218
TypeSection: []wasm.FunctionType{
219-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
219+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
220220
},
221221
FunctionSection: []wasm.Index{0},
222222
CodeSection: []wasm.Code{
@@ -240,15 +240,15 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
240240
input: func(r Runtime) HostModuleBuilder {
241241
return r.NewHostModuleBuilder("host").
242242
NewFunctionBuilder().
243-
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}).
243+
WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
244244
Export("1").
245245
NewFunctionBuilder().
246-
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}).
246+
WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
247247
Export("1")
248248
},
249249
expected: &wasm.Module{
250250
TypeSection: []wasm.FunctionType{
251-
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
251+
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
252252
},
253253
FunctionSection: []wasm.Index{0},
254254
CodeSection: []wasm.Code{
@@ -272,16 +272,16 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
272272
// Intentionally not in lexicographic order
273273
return r.NewHostModuleBuilder("host").
274274
NewFunctionBuilder().
275-
WithGoFunction(gofunc2, []api.ValueType{i64}, []api.ValueType{i32}).
275+
WithGoFunction(gofunc2, wasm.ToApiValueType([]wasm.ValueType{i64}), wasm.ToApiValueType([]wasm.ValueType{i32})).
276276
Export("2").
277277
NewFunctionBuilder().
278-
WithGoFunction(gofunc1, []api.ValueType{i32}, []api.ValueType{i32}).
278+
WithGoFunction(gofunc1, wasm.ToApiValueType([]wasm.ValueType{i32}), wasm.ToApiValueType([]wasm.ValueType{i32})).
279279
Export("1")
280280
},
281281
expected: &wasm.Module{
282282
TypeSection: []wasm.FunctionType{
283-
{Params: []api.ValueType{i64}, Results: []api.ValueType{i32}},
284-
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
283+
{Params: []wasm.ValueType{i64}, Results: []wasm.ValueType{i32}},
284+
{Params: []wasm.ValueType{i32}, Results: []wasm.ValueType{i32}},
285285
},
286286
FunctionSection: []wasm.Index{0, 1},
287287
CodeSection: []wasm.Code{

experimental/listener_example_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ func (f *mockFunctionDefinition) DebugName() string {
177177
return f.debugName
178178
}
179179

180-
func (f *mockFunctionDefinition) ParamTypes() []wasm.ValueType {
181-
return []wasm.ValueType{}
180+
func (f *mockFunctionDefinition) ParamTypes() []api.ValueType {
181+
return []api.ValueType{}
182182
}
183183

184-
func (f *mockFunctionDefinition) ResultTypes() []wasm.ValueType {
185-
return []wasm.ValueType{}
184+
func (f *mockFunctionDefinition) ResultTypes() []api.ValueType {
185+
return []api.ValueType{}
186186
}

experimental/logging/log_listener_test.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ var testCtx = context.WithValue(context.Background(), arbitrary{}, "arbitrary")
2323
func Test_loggingListener(t *testing.T) {
2424
wasiFuncName := wasi.RandomGetName
2525
wasiFuncType := wasm.FunctionType{
26-
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI32},
27-
Results: []api.ValueType{api.ValueTypeI32},
26+
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
27+
Results: []wasm.ValueType{wasm.ValueTypeI32},
2828
}
2929
wasiParamNames := []string{"buf", "buf_len"}
3030
wasiParams := []uint64{0, 8}
@@ -84,15 +84,15 @@ func Test_loggingListener(t *testing.T) {
8484
},
8585
{
8686
name: "i32",
87-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}},
87+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
8888
params: []uint64{math.MaxUint32},
8989
expected: `--> test.fn(-1)
9090
<--
9191
`,
9292
},
9393
{
9494
name: "i32 named",
95-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI32}},
95+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI32}},
9696
params: []uint64{math.MaxUint32},
9797
paramNames: []string{"x"},
9898
expected: `--> test.fn(x=-1)
@@ -101,15 +101,15 @@ func Test_loggingListener(t *testing.T) {
101101
},
102102
{
103103
name: "i64",
104-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}},
104+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
105105
params: []uint64{math.MaxUint64},
106106
expected: `--> test.fn(-1)
107107
<--
108108
`,
109109
},
110110
{
111111
name: "i64 named",
112-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeI64}},
112+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeI64}},
113113
params: []uint64{math.MaxUint64},
114114
paramNames: []string{"x"},
115115
expected: `--> test.fn(x=-1)
@@ -118,15 +118,15 @@ func Test_loggingListener(t *testing.T) {
118118
},
119119
{
120120
name: "f32",
121-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}},
121+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF32}},
122122
params: []uint64{api.EncodeF32(math.MaxFloat32)},
123123
expected: `--> test.fn(3.4028235e+38)
124124
<--
125125
`,
126126
},
127127
{
128128
name: "f32 named",
129-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF32}},
129+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF32}},
130130
params: []uint64{api.EncodeF32(math.MaxFloat32)},
131131
paramNames: []string{"x"},
132132
expected: `--> test.fn(x=3.4028235e+38)
@@ -135,15 +135,15 @@ func Test_loggingListener(t *testing.T) {
135135
},
136136
{
137137
name: "f64",
138-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}},
138+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF64}},
139139
params: []uint64{api.EncodeF64(math.MaxFloat64)},
140140
expected: `--> test.fn(1.7976931348623157e+308)
141141
<--
142142
`,
143143
},
144144
{
145145
name: "f64 named",
146-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeF64}},
146+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeF64}},
147147
params: []uint64{api.EncodeF64(math.MaxFloat64)},
148148
paramNames: []string{"x"},
149149
expected: `--> test.fn(x=1.7976931348623157e+308)
@@ -152,15 +152,15 @@ func Test_loggingListener(t *testing.T) {
152152
},
153153
{
154154
name: "externref",
155-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}},
155+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
156156
params: []uint64{0},
157157
expected: `--> test.fn(0000000000000000)
158158
<--
159159
`,
160160
},
161161
{
162162
name: "externref named",
163-
functype: wasm.FunctionType{Params: []api.ValueType{api.ValueTypeExternref}},
163+
functype: wasm.FunctionType{Params: []wasm.ValueType{wasm.ValueTypeExternref}},
164164
params: []uint64{0},
165165
paramNames: []string{"x"},
166166
expected: `--> test.fn(x=0000000000000000)
@@ -169,15 +169,15 @@ func Test_loggingListener(t *testing.T) {
169169
},
170170
{
171171
name: "v128",
172-
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}},
172+
functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
173173
params: []uint64{0, 1},
174174
expected: `--> test.fn(00000000000000000000000000000001)
175175
<--
176176
`,
177177
},
178178
{
179179
name: "v128 named",
180-
functype: wasm.FunctionType{Params: []api.ValueType{0x7b}},
180+
functype: wasm.FunctionType{Params: []wasm.ValueType{0x7b}},
181181
params: []uint64{0, 1},
182182
paramNames: []string{"x"},
183183
expected: `--> test.fn(x=00000000000000000000000000000001)
@@ -186,15 +186,15 @@ func Test_loggingListener(t *testing.T) {
186186
},
187187
{
188188
name: "funcref",
189-
functype: wasm.FunctionType{Params: []api.ValueType{0x70}},
189+
functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
190190
params: []uint64{0},
191191
expected: `--> test.fn(0000000000000000)
192192
<--
193193
`,
194194
},
195195
{
196196
name: "funcref named",
197-
functype: wasm.FunctionType{Params: []api.ValueType{0x70}},
197+
functype: wasm.FunctionType{Params: []wasm.ValueType{0x70}},
198198
params: []uint64{0},
199199
paramNames: []string{"x"},
200200
expected: `--> test.fn(x=0000000000000000)
@@ -203,7 +203,7 @@ func Test_loggingListener(t *testing.T) {
203203
},
204204
{
205205
name: "no params, one result",
206-
functype: wasm.FunctionType{Results: []api.ValueType{api.ValueTypeI32}},
206+
functype: wasm.FunctionType{Results: []wasm.ValueType{wasm.ValueTypeI32}},
207207
results: []uint64{math.MaxUint32},
208208
expected: `--> test.fn()
209209
<-- -1
@@ -212,8 +212,8 @@ func Test_loggingListener(t *testing.T) {
212212
{
213213
name: "one param, one result",
214214
functype: wasm.FunctionType{
215-
Params: []api.ValueType{api.ValueTypeI32},
216-
Results: []api.ValueType{api.ValueTypeF32},
215+
Params: []wasm.ValueType{wasm.ValueTypeI32},
216+
Results: []wasm.ValueType{wasm.ValueTypeF32},
217217
},
218218
params: []uint64{math.MaxUint32},
219219
results: []uint64{api.EncodeF32(math.MaxFloat32)},
@@ -224,8 +224,8 @@ func Test_loggingListener(t *testing.T) {
224224
{
225225
name: "two params, two results",
226226
functype: wasm.FunctionType{
227-
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64},
228-
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64},
227+
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
228+
Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
229229
},
230230
params: []uint64{math.MaxUint32, math.MaxUint64},
231231
results: []uint64{api.EncodeF32(math.MaxFloat32), api.EncodeF64(math.MaxFloat64)},
@@ -236,8 +236,8 @@ func Test_loggingListener(t *testing.T) {
236236
{
237237
name: "two params, two results named",
238238
functype: wasm.FunctionType{
239-
Params: []api.ValueType{api.ValueTypeI32, api.ValueTypeI64},
240-
Results: []api.ValueType{api.ValueTypeF32, api.ValueTypeF64},
239+
Params: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64},
240+
Results: []wasm.ValueType{wasm.ValueTypeF32, wasm.ValueTypeF64},
241241
},
242242
params: []uint64{math.MaxUint32, math.MaxUint64},
243243
paramNames: []string{"x", "y"},

experimental/table/lookup.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func LookupFunction(
2222
expectedParamTypes, expectedResultTypes []api.ValueType,
2323
) api.Function {
2424
m := module.(*wasm.ModuleInstance)
25-
typ := &wasm.FunctionType{Params: expectedParamTypes, Results: expectedResultTypes}
25+
typ := &wasm.FunctionType{Params: wasm.FromApiValueType(expectedParamTypes), Results: wasm.FromApiValueType(expectedResultTypes)}
2626
typ.CacheNumInUint64()
2727
typeID := m.GetFunctionTypeID(typ)
2828
if int(tableIndex) >= len(m.Tables) {

0 commit comments

Comments
 (0)