Allows wasm-defined host functions to use memory in interpreter (#713)

Before, we allowed stubbed host functions to be defined in wasm instead
of Go. This improves performance and reduces a chance of side-effects vs
Go. In fact, any pure function was supported in wasm, provided it only
called pure functions.

This changes internals so that a wasm-defined host function can use
memory. Notably, host functions use the caller's memory, so this is
simpler to initially support in the interpreter.

This is needed to simplify and reduce performance hit of GOARCH=wasm,
GOOS=js code, which perform a lot of memory reads and do not have
idiomatic signatures.

Note: wasm-defined host functions remain internal until we gain
experience, at least conclusion of the wasm_exec host module.


Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-07-25 09:12:44 +08:00
committed by GitHub
parent 6a62b794f5
commit 1689fc1bbf
26 changed files with 688 additions and 352 deletions

View File

@@ -1,7 +1,6 @@
package wasm
import (
"reflect"
"testing"
"github.com/tetratelabs/wazero/api"
@@ -10,7 +9,7 @@ import (
func TestModule_BuildFunctionDefinitions(t *testing.T) {
nopCode := &Code{Body: []byte{OpcodeEnd}}
fnV := reflect.ValueOf(func() {})
fn := func() {}
tests := []struct {
name string
m *Module
@@ -32,18 +31,18 @@ func TestModule_BuildFunctionDefinitions(t *testing.T) {
expectedExports: map[string]api.FunctionDefinition{},
},
{
name: "host func",
name: "host func go",
m: &Module{
TypeSection: []*FunctionType{v_v},
FunctionSection: []Index{0},
CodeSection: []*Code{{GoFunc: &fnV}},
CodeSection: []*Code{MustParseGoFuncCode(fn)},
},
expected: []*FunctionDefinition{
{
index: 0,
debugName: ".$0",
isHostFunction: true,
funcType: v_v,
index: 0,
debugName: ".$0",
goFunc: MustParseGoFuncCode(fn).GoFunc,
funcType: v_v,
},
},
expectedExports: map[string]api.FunctionDefinition{},