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

@@ -5,6 +5,7 @@ import (
"context"
"fmt"
"math"
"reflect"
)
// ExternType classifies imports and exports with their respective types.
@@ -227,12 +228,16 @@ type FunctionDefinition interface {
// is possible.
ExportNames() []string
// IsHostFunction returns true if the function was implemented by the
// embedder (ex via wazero.ModuleBuilder) instead of a wasm binary.
// GoFunc is present when the function was implemented by the embedder
// (ex via wazero.ModuleBuilder) instead of a wasm binary.
//
// This function can be non-deterministic or cause side effects. It also
// has special properties not defined in the WebAssembly Core
// specification. Notably, it uses the caller's memory, which might be
// different from its defining module.
//
// Note: Host functions can be non-deterministic or cause side effects.
// See https://www.w3.org/TR/wasm-core-1/#host-functions%E2%91%A0
IsHostFunction() bool
GoFunc() *reflect.Value
// ParamTypes are the possibly empty sequence of value types accepted by a
// function with this signature.