Supports mix of wasm and go funcs in the same module (#707)

This removes the constraint of a module being exclusively wasm or host
functions. Later pull requests can optimize special imports to be
implemented in wasm, particularly useful for disabled logging callbacks.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-07-19 11:55:37 +08:00
committed by GitHub
parent 0d76b11d66
commit 0da1af2d51
19 changed files with 131 additions and 188 deletions

View File

@@ -9,7 +9,7 @@ import (
)
func TestModule_BuildFunctionDefinitions(t *testing.T) {
nopCode := &Code{nil, []byte{OpcodeEnd}}
nopCode := &Code{Body: []byte{OpcodeEnd}}
fnV := reflect.ValueOf(func() {})
tests := []struct {
name string
@@ -34,9 +34,9 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
{
name: "host func",
m: &Module{
TypeSection: []*FunctionType{v_v},
FunctionSection: []Index{0},
HostFunctionSection: []*reflect.Value{&fnV},
TypeSection: []*FunctionType{v_v},
FunctionSection: []Index{0},
CodeSection: []*Code{{GoFunc: &fnV}},
},
expected: []*FunctionDefinition{
{
@@ -59,6 +59,11 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
},
GlobalSection: []*Global{{}},
FunctionSection: []Index{1, 2, 0},
CodeSection: []*Code{
{Body: []byte{OpcodeEnd}},
{Body: []byte{OpcodeEnd}},
{Body: []byte{OpcodeEnd}},
},
TypeSection: []*FunctionType{
v_v,
{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
@@ -119,6 +124,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
{Name: "function_index=2", Type: ExternTypeFunc, Index: 2},
},
FunctionSection: []Index{1, 0},
CodeSection: []*Code{{Body: []byte{OpcodeEnd}}, {Body: []byte{OpcodeEnd}}},
TypeSection: []*FunctionType{
v_v,
{Params: []ValueType{ValueTypeF64, ValueTypeI32}, Results: []ValueType{ValueTypeV128, ValueTypeI64}},
@@ -191,7 +197,7 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
},
},
FunctionSection: []Index{0, 0, 0, 0, 0},
CodeSection: []*Code{nopCode, nopCode, nopCode, nopCode},
CodeSection: []*Code{nopCode, nopCode, nopCode, nopCode, nopCode},
},
expected: []*FunctionDefinition{
{moduleName: "module", index: 0, debugName: "module.$0", importDesc: &[2]string{"i", "f"}, funcType: v_v},