Reduces FunctionDefinition usages (#1419)

Signed-off-by: Takeshi Yoneda <t.y.mathetake@gmail.com>
This commit is contained in:
Takeshi Yoneda
2023-04-30 19:37:43 -07:00
committed by GitHub
parent f3f7352584
commit f47b0d3362
12 changed files with 136 additions and 144 deletions

View File

@@ -45,8 +45,8 @@ type ModuleEngine interface {
// - `importedModuleEngine` is the ModuleEngine for the imported ModuleInstance.
ResolveImportedFunction(index, indexInImportedModule Index, importedModuleEngine ModuleEngine)
// LookupFunction returns the index of the function in the function table.
LookupFunction(t *TableInstance, typeId FunctionTypeID, tableOffset Index) (Index, error)
// LookupFunction returns the api.Function created from the function in the function table at the given offset.
LookupFunction(t *TableInstance, typeId FunctionTypeID, tableOffset Index) (api.Function, error)
// FunctionInstanceReference returns Reference for the given Index for a FunctionInstance. The returned values are used by
// the initialization via ElementSegment.

View File

@@ -60,7 +60,7 @@ func (m *Module) BuildFunctionDefinitions() {
def := &m.FunctionDefinitionSection[importFuncIdx]
def.importDesc = imp
def.index = importFuncIdx
def.funcType = &m.TypeSection[imp.DescFunc]
def.Functype = &m.TypeSection[imp.DescFunc]
importFuncIdx++
}
@@ -69,7 +69,7 @@ func (m *Module) BuildFunctionDefinitions() {
idx := importFuncIdx + Index(codeIndex)
def := &m.FunctionDefinitionSection[idx]
def.index = idx
def.funcType = &m.TypeSection[typeIndex]
def.Functype = &m.TypeSection[typeIndex]
def.goFunc = code.GoFunc
}
@@ -92,9 +92,9 @@ func (m *Module) BuildFunctionDefinitions() {
d.moduleName = moduleName
d.name = funcName
d.debugName = wasmdebug.FuncName(moduleName, funcName, funcIdx)
d.paramNames = paramNames(localNames, funcIdx, len(d.funcType.Params))
d.resultNames = paramNames(resultNames, funcIdx, len(d.funcType.Results))
d.Debugname = wasmdebug.FuncName(moduleName, funcName, funcIdx)
d.paramNames = paramNames(localNames, funcIdx, len(d.Functype.Params))
d.resultNames = paramNames(resultNames, funcIdx, len(d.Functype.Results))
for i := range m.ExportSection {
e := &m.ExportSection[i]
@@ -108,12 +108,14 @@ func (m *Module) BuildFunctionDefinitions() {
// FunctionDefinition implements api.FunctionDefinition
type FunctionDefinition struct {
internalapi.WazeroOnlyType
moduleName string
index Index
name string
debugName string
goFunc interface{}
funcType *FunctionType
moduleName string
index Index
name string
// Debugname is exported for testing purpose.
Debugname string
goFunc interface{}
// Functype is exported for testing purpose.
Functype *FunctionType
importDesc *Import
exportNames []string
paramNames []string
@@ -137,7 +139,7 @@ func (f *FunctionDefinition) Name() string {
// DebugName implements the same method as documented on api.FunctionDefinition.
func (f *FunctionDefinition) DebugName() string {
return f.debugName
return f.Debugname
}
// Import implements the same method as documented on api.FunctionDefinition.
@@ -161,7 +163,7 @@ func (f *FunctionDefinition) GoFunction() interface{} {
// ParamTypes implements api.FunctionDefinition ParamTypes.
func (f *FunctionDefinition) ParamTypes() []ValueType {
return f.funcType.Params
return f.Functype.Params
}
// ParamNames implements the same method as documented on api.FunctionDefinition.
@@ -171,7 +173,7 @@ func (f *FunctionDefinition) ParamNames() []string {
// ResultTypes implements api.FunctionDefinition ResultTypes.
func (f *FunctionDefinition) ResultTypes() []ValueType {
return f.funcType.Results
return f.Functype.Results
}
// ResultNames implements the same method as documented on api.FunctionDefinition.

View File

@@ -40,7 +40,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
{
name: "only imported functions",
expected: []FunctionDefinition{
{name: "fn", debugName: ".fn", importDesc: &Import{Module: "foo", Name: "bar", Type: ExternTypeFunc}, funcType: &FunctionType{}},
{name: "fn", Debugname: ".fn", importDesc: &Import{Module: "foo", Name: "bar", Type: ExternTypeFunc}, Functype: &FunctionType{}},
},
m: &Module{
ExportSection: []Export{{Type: ExternTypeGlobal, Index: 0}},
@@ -52,8 +52,8 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
},
expectedImports: []api.FunctionDefinition{
&FunctionDefinition{
name: "fn", debugName: ".fn", importDesc: &Import{Module: "foo", Name: "bar", Type: ExternTypeFunc},
funcType: &FunctionType{},
name: "fn", Debugname: ".fn", importDesc: &Import{Module: "foo", Name: "bar", Type: ExternTypeFunc},
Functype: &FunctionType{},
},
},
expectedExports: map[string]api.FunctionDefinition{},
@@ -76,9 +76,9 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
index: 0,
name: "fn",
moduleName: "m",
debugName: "m.fn",
Debugname: "m.fn",
goFunc: MustParseGoReflectFuncCode(fn).GoFunc,
funcType: &i32_i32,
Functype: &i32_i32,
paramNames: []string{"x"},
resultNames: []string{"y"},
},
@@ -110,41 +110,41 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
expected: []FunctionDefinition{
{
index: 0,
debugName: ".$0",
Debugname: ".$0",
exportNames: []string{"function_index=0"},
funcType: &f64i32_v128i64,
Functype: &f64i32_v128i64,
},
{
index: 1,
debugName: ".$1",
Debugname: ".$1",
exportNames: []string{"function_index=1"},
funcType: &f64f32_i64,
Functype: &f64f32_i64,
},
{
index: 2,
debugName: ".$2",
Debugname: ".$2",
exportNames: []string{"function_index=2"},
funcType: &v_v,
Functype: &v_v,
},
},
expectedExports: map[string]api.FunctionDefinition{
"function_index=0": &FunctionDefinition{
index: 0,
debugName: ".$0",
Debugname: ".$0",
exportNames: []string{"function_index=0"},
funcType: &f64i32_v128i64,
Functype: &f64i32_v128i64,
},
"function_index=1": &FunctionDefinition{
index: 1,
exportNames: []string{"function_index=1"},
debugName: ".$1",
funcType: &f64f32_i64,
Debugname: ".$1",
Functype: &f64f32_i64,
},
"function_index=2": &FunctionDefinition{
index: 2,
debugName: ".$2",
Debugname: ".$2",
exportNames: []string{"function_index=2"},
funcType: &v_v,
Functype: &v_v,
},
},
},
@@ -169,52 +169,52 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
expected: []FunctionDefinition{
{
index: 0,
debugName: ".$0",
Debugname: ".$0",
importDesc: imp,
exportNames: []string{"imported_function"},
funcType: &f64f32_i64,
Functype: &f64f32_i64,
},
{
index: 1,
debugName: ".$1",
Debugname: ".$1",
exportNames: []string{"function_index=1"},
funcType: &f64i32_v128i64,
Functype: &f64i32_v128i64,
},
{
index: 2,
debugName: ".$2",
Debugname: ".$2",
exportNames: []string{"function_index=2"},
funcType: &v_v,
Functype: &v_v,
},
},
expectedImports: []api.FunctionDefinition{
&FunctionDefinition{
index: 0,
debugName: ".$0",
Debugname: ".$0",
importDesc: imp,
exportNames: []string{"imported_function"},
funcType: &f64f32_i64,
Functype: &f64f32_i64,
},
},
expectedExports: map[string]api.FunctionDefinition{
"imported_function": &FunctionDefinition{
index: 0,
debugName: ".$0",
Debugname: ".$0",
importDesc: imp,
exportNames: []string{"imported_function"},
funcType: &f64f32_i64,
Functype: &f64f32_i64,
},
"function_index=1": &FunctionDefinition{
index: 1,
debugName: ".$1",
Debugname: ".$1",
exportNames: []string{"function_index=1"},
funcType: &f64i32_v128i64,
Functype: &f64i32_v128i64,
},
"function_index=2": &FunctionDefinition{
index: 2,
debugName: ".$2",
Debugname: ".$2",
exportNames: []string{"function_index=2"},
funcType: &v_v,
Functype: &v_v,
},
},
},
@@ -236,15 +236,15 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
CodeSection: []Code{nopCode, nopCode, nopCode, nopCode, nopCode},
},
expected: []FunctionDefinition{
{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &Import{Module: "i", Name: "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"},
{moduleName: "module", index: 0, Debugname: "module.$0", importDesc: &Import{Module: "i", Name: "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: &Import{Module: "i", Name: "f"}, funcType: &v_v},
&FunctionDefinition{moduleName: "module", index: 0, Debugname: "module.$0", importDesc: &Import{Module: "i", Name: "f"}, Functype: &v_v},
},
expectedExports: map[string]api.FunctionDefinition{},
},

View File

@@ -397,8 +397,8 @@ func (m *ModuleInstance) resolveImports(module *Module) (err error) {
case ExternTypeFunc:
expectedType := &module.TypeSection[i.DescFunc]
actual := &importedModule.Definitions[imported.Index]
if !actual.funcType.EqualsSignature(expectedType.Params, expectedType.Results) {
err = errorInvalidImport(i, fmt.Errorf("signature mismatch: %s != %s", expectedType, actual.funcType))
if !actual.Functype.EqualsSignature(expectedType.Params, expectedType.Results) {
err = errorInvalidImport(i, fmt.Errorf("signature mismatch: %s != %s", expectedType, actual.Functype))
return
}

View File

@@ -145,7 +145,7 @@ func TestStore_CloseWithExitCode(t *testing.T) {
FunctionSection: []uint32{0},
CodeSection: []Code{{Body: []byte{OpcodeEnd}}},
Exports: map[string]*Export{"fn": {Type: ExternTypeFunc, Name: "fn"}},
FunctionDefinitionSection: []FunctionDefinition{{funcType: &v_v}},
FunctionDefinitionSection: []FunctionDefinition{{Functype: &v_v}},
}, importedModuleName, nil, []FunctionTypeID{0})
require.NoError(t, err)
@@ -439,8 +439,8 @@ func (e *mockEngine) CompileModule(context.Context, *Module, []experimental.Func
}
// LookupFunction implements the same method as documented on wasm.Engine.
func (e *mockModuleEngine) LookupFunction(*TableInstance, FunctionTypeID, Index) (Index, error) {
return 0, nil
func (e *mockModuleEngine) LookupFunction(*TableInstance, FunctionTypeID, Index) (api.Function, error) {
return nil, nil
}
// CompiledModuleCount implements the same method as documented on wasm.Engine.
@@ -695,9 +695,9 @@ func Test_resolveImports(t *testing.T) {
Definitions: []FunctionDefinition{
{},
{},
{funcType: &FunctionType{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}}},
{Functype: &FunctionType{Params: []ValueType{i32}, Results: []ValueType{ValueTypeV128}}},
{},
{funcType: &FunctionType{Params: []ValueType{ExternTypeFunc}, Results: []ValueType{}}},
{Functype: &FunctionType{Params: []ValueType{ExternTypeFunc}, Results: []ValueType{}}},
},
}
module := &Module{
@@ -729,7 +729,7 @@ func Test_resolveImports(t *testing.T) {
},
ModuleName: moduleName,
TypeIDs: []FunctionTypeID{123435},
Definitions: []FunctionDefinition{{funcType: &FunctionType{}}},
Definitions: []FunctionDefinition{{Functype: &FunctionType{}}},
}
module := &Module{
TypeSection: []FunctionType{{Results: []ValueType{ValueTypeF32}}},