Files
wazero/internal/wasm/engine.go
Crypt Keeper 45ccab589b Refactors API to ensure context propagation (#482)
This is an API breaking change that does a few things:

* Stop encouraging practice that can break context propagation:
  * Stops caching `context.Context` in `wazero.RuntimeConfig`
  * Stops caching `context.Context` in `api.Module`

* Fixes context propagation in function calls:
  * Changes `api.Function`'s arg0 from `api.Module` to `context.Context`
  * Adds `context.Context` parameter in instantiation (propagates to
    .start)

* Allows context propagation for heavy operations like compile:
  * Adds `context.Context` as the initial parameter of `CompileModule`

The design we had earlier was a good start, but this is the only way to
ensure coherence when users start correlating or tracing. While adding a
`context.Context` parameter may seem difficult, wazero is a low-level
library and WebAssembly is notoriously difficult to troubleshoot. In
other words, it will be easier to explain to users to pass (even nil) as
the context parameter vs try to figure out things without coherent
context.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-04-19 16:52:57 +08:00

44 lines
2.0 KiB
Go

package wasm
import "context"
// Engine is a Store-scoped mechanism to compile functions declared or imported by a module.
// This is a top-level type implemented by an interpreter or JIT compiler.
type Engine interface {
// CompileModule implements the same method as documented on wasm.Engine.
CompileModule(ctx context.Context, module *Module) error
// NewModuleEngine compiles down the function instances in a module, and returns ModuleEngine for the module.
//
// * name is the name the module was instantiated with used for error handling.
// * module is the source module from which moduleFunctions are instantiated. This is used for caching.
// * importedFunctions: functions this module imports, already compiled in this engine.
// * moduleFunctions: functions declared in this module that must be compiled.
// * table: a possibly shared table used by this module. When nil tableInit will be nil.
// * tableInit: a mapping of TableInstance.Table index to the function index it should point to.
//
// Note: Input parameters must be pre-validated with wasm.Module Validate, to ensure no fields are invalid
// due to reasons such as out-of-bounds.
NewModuleEngine(
name string,
module *Module,
importedFunctions, moduleFunctions []*FunctionInstance,
table *TableInstance,
tableInit map[Index]Index,
) (ModuleEngine, error)
// DeleteCompiledModule releases compilation caches for the given module (source).
// Note: it is safe to call this function for a module from which module instances are instantiated even when these module instances
// are having outstanding calls.
DeleteCompiledModule(module *Module)
}
// ModuleEngine implements function calls for a given module.
type ModuleEngine interface {
// Name returns the name of the module this engine was compiled for.
Name() string
// Call invokes a function instance f with given parameters.
Call(ctx context.Context, m *CallContext, f *FunctionInstance, params ...uint64) (results []uint64, err error)
}