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

@@ -7,6 +7,7 @@ import (
"fmt"
"math"
"os"
"reflect"
"strings"
"github.com/tetratelabs/wazero/internal/buildoptions"
@@ -176,6 +177,10 @@ func (c *compiler) resetUnreachable() {
}
type CompilationResult struct {
// GoFunc is present when the result is a host function.
// In this case, other fields can be ignored.
GoFunc *reflect.Value
// Operations holds wazeroir operations compiled from Wasm instructions in a Wasm function.
Operations []Operation
// LabelCallers maps Label.String() to the number of callers to that label.
@@ -233,6 +238,10 @@ func CompileFunctions(_ context.Context, enabledFeatures wasm.Features, module *
typeID := module.FunctionSection[funcIndex]
sig := module.TypeSection[typeID]
code := module.CodeSection[funcIndex]
if code.GoFunc != nil {
ret = append(ret, &CompilationResult{GoFunc: code.GoFunc})
continue
}
r, err := compile(enabledFeatures, sig, code.Body, code.LocalTypes, module.TypeSection, functions, globals)
if err != nil {
def := module.FunctionDefinitionSection[uint32(funcIndex)+module.ImportFuncCount()]