Top-levels FunctionDefinition to allow access to all function metadata (#686)

This top-levels `api.FunctionDefinition` which was formerly
experimental, and also adds import metadata to it. Now, it holds all
metadata known at compile time.

Here are the public API visible changes:
* api.ExportedFunction - replaced with api.FunctionDefinition as it is
  usable for all types of functions.
* api.Function - `.ParamTypes/ResultTypes()` are replaced with
  `.Definition().
* api.FunctionDefinition - extracted from experimental and adds
  `.Import()` to get the any imported module and function name.
* experimental.FunctionDefinition - replaced with
  api.FunctionDefinition.
* experimental.FunctionListenerFactory - adds first arg of the
  instantiated module name, as it can be different than compiled.
* wazero.CompiledModule - Adds `.ImportedFunctions()` and changes result
  type of `.ExportedFunctions()` to api.FunctionDefinition.

Internally, logic to create function definition are consolidated between
host and wasm-defined functions, notably wasm.Module now includes
`.BuildFunctionDefinitions()` which reduces duplication in
wasm.ModuleInstance `.BuildFunctions()`,

This obviates #681 by deleting the `ExportedFunction` type which
overlaps with this information.

This fixes #637 as it includes more metadata including imports.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
Co-authored-by: Takeshi Yoneda <takeshi@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-07-13 14:16:18 +08:00
committed by GitHub
parent e85e713eb3
commit 49e5bcb8c7
27 changed files with 858 additions and 689 deletions

View File

@@ -0,0 +1,197 @@
package wasm
import (
"testing"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/internal/testing/require"
)
func TestModule_BuildFunctionDefinitions(t *testing.T) {
nopCode := &Code{nil, []byte{OpcodeEnd}}
tests := []struct {
name string
m *Module
expected []*FunctionDefinition
expectedImports, expectedExports []api.FunctionDefinition
}{
{
name: "no exports",
m: &Module{},
},
{
name: "no functions",
m: &Module{
ExportSection: []*Export{{Type: ExternTypeGlobal, Index: 0}},
GlobalSection: []*Global{{}},
},
},
{
name: "without imports",
m: &Module{
ExportSection: []*Export{
{Name: "function_index=0", Type: ExternTypeFunc, Index: 0},
{Name: "function_index=2", Type: ExternTypeFunc, Index: 2},
{Name: "", Type: ExternTypeGlobal, Index: 0},
{Name: "function_index=1", Type: ExternTypeFunc, Index: 1},
},
GlobalSection: []*Global{{}},
FunctionSection: []Index{1, 2, 0},
TypeSection: []*FunctionType{
v_v,
{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
},
expected: []*FunctionDefinition{
{
index: 0,
debugName: ".$0",
exportNames: []string{"function_index=0"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
},
{
index: 1,
debugName: ".$1",
exportNames: []string{"function_index=1"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
{
index: 2,
debugName: ".$2",
exportNames: []string{"function_index=2"},
funcType: v_v,
},
},
expectedExports: []api.FunctionDefinition{
&FunctionDefinition{
index: 0,
debugName: ".$0",
exportNames: []string{"function_index=0"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
},
&FunctionDefinition{
index: 1,
exportNames: []string{"function_index=1"},
debugName: ".$1",
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
&FunctionDefinition{
index: 2,
debugName: ".$2",
exportNames: []string{"function_index=2"},
funcType: v_v,
},
},
},
{
name: "with imports",
m: &Module{
ImportSection: []*Import{{
Type: ExternTypeFunc,
DescFunc: 2, // Index of type.
}},
ExportSection: []*Export{
{Name: "imported_function", Type: ExternTypeFunc, Index: 0},
{Name: "function_index=1", Type: ExternTypeFunc, Index: 1},
{Name: "function_index=2", Type: ExternTypeFunc, Index: 2},
},
FunctionSection: []Index{1, 0},
TypeSection: []*FunctionType{
v_v,
{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
},
expected: []*FunctionDefinition{
{
index: 0,
debugName: ".$0",
importDesc: &[2]string{"", ""},
exportNames: []string{"imported_function"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
{
index: 1,
debugName: ".$1",
exportNames: []string{"function_index=1"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
},
{
index: 2,
debugName: ".$2",
exportNames: []string{"function_index=2"},
funcType: v_v,
},
},
expectedImports: []api.FunctionDefinition{
&FunctionDefinition{
index: 0,
debugName: ".$0",
importDesc: &[2]string{"", ""},
exportNames: []string{"imported_function"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
},
expectedExports: []api.FunctionDefinition{
&FunctionDefinition{
index: 0,
debugName: ".$0",
importDesc: &[2]string{"", ""},
exportNames: []string{"imported_function"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeF32}, Results: []ValueType{ValueTypeI64}},
},
&FunctionDefinition{
index: 1,
debugName: ".$1",
exportNames: []string{"function_index=1"},
funcType: &FunctionType{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
},
&FunctionDefinition{
index: 2,
debugName: ".$2",
exportNames: []string{"function_index=2"},
funcType: v_v,
},
},
},
{
name: "with names",
m: &Module{
TypeSection: []*FunctionType{v_v},
ImportSection: []*Import{{Module: "i", Name: "f", Type: ExternTypeFunc}},
NameSection: &NameSection{
ModuleName: "module",
FunctionNames: NameMap{
{Index: Index(2), Name: "two"},
{Index: Index(4), Name: "four"},
{Index: Index(5), Name: "five"},
},
},
FunctionSection: []Index{0, 0, 0, 0, 0},
CodeSection: []*Code{nopCode, nopCode, nopCode, nopCode},
},
expected: []*FunctionDefinition{
{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &[2]string{"i", "f"}, funcType: v_v},
{moduleName: "module", index: 1, debugName: "module.$1", funcType: v_v},
{moduleName: "module", index: 2, debugName: "module.two", funcType: v_v, name: "two"},
{moduleName: "module", index: 3, debugName: "module.$3", funcType: v_v},
{moduleName: "module", index: 4, debugName: "module.four", funcType: v_v, name: "four"},
{moduleName: "module", index: 5, debugName: "module.five", funcType: v_v, name: "five"},
},
expectedImports: []api.FunctionDefinition{
&FunctionDefinition{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &[2]string{"i", "f"}, funcType: v_v},
},
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
tc.m.BuildFunctionDefinitions()
require.Equal(t, tc.expected, tc.m.FunctionDefinitionSection)
require.Equal(t, tc.expectedImports, tc.m.ImportedFunctions())
require.Equal(t, tc.expectedExports, tc.m.ExportedFunctions())
})
}
}