Adds ResultNames to HostFunctionBuilder and FunctionDefinition (#887)

This adds ResultNames to HostFunctionBuilder and FunctionDefinition
which helps for multi-results or special-cased ones.

End users can access result names in `FunctionDefinition.ResultNames` or
set for their own host functions via
`HostFunctionBuilder.WithResultNames`. This change adds them for all
built-in functions where result names help.

Most notably, GOOS=js uses `ProxyFunc` to allow logging when a function
returns multiple results. Before, the results were returned without
names: e.g. `11231,1` and now they are named like `n=11231,ok=1`.

We soon plan to allow more visibility in WASI, for example, logging
results that will write to memory offsets. This infrastructure makes it
possible to do that.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-12-06 14:30:55 +09:00
committed by GitHub
parent 6c4dd1cfd9
commit af8105ba0e
41 changed files with 813 additions and 823 deletions

View File

@@ -89,6 +89,29 @@ func TestNewHostModuleBuilder_Compile(t *testing.T) {
},
},
},
{
name: "WithFunc WithName WithResultNames",
input: func(r Runtime) HostModuleBuilder {
return r.NewHostModuleBuilder("").NewFunctionBuilder().
WithFunc(uint32_uint32).
WithName("get").WithResultNames("x").
Export("1")
},
expected: &wasm.Module{
TypeSection: []*wasm.FunctionType{
{Params: []api.ValueType{i32}, Results: []api.ValueType{i32}},
},
FunctionSection: []wasm.Index{0},
CodeSection: []*wasm.Code{wasm.MustParseGoReflectFuncCode(uint32_uint32)},
ExportSection: []*wasm.Export{
{Name: "1", Type: wasm.ExternTypeFunc, Index: 0},
},
NameSection: &wasm.NameSection{
FunctionNames: wasm.NameMap{{Index: 0, Name: "get"}},
ResultNames: []*wasm.NameMapAssoc{{Index: 0, NameMap: wasm.NameMap{{Index: 0, Name: "x"}}}},
},
},
},
{
name: "WithFunc overwrites existing",
input: func(r Runtime) HostModuleBuilder {