Documentation ¶
Index ¶
- Variables
- func Build[T Module](runtime wazero.Runtime, mod HostModule[T], decorators ...Decorator[T]) wazero.HostModuleBuilder
- func Configure[T any](value T, options ...Option[T])
- func Instantiate[T Module](ctx *InstantiationContext, compiled *CompiledModule[T], opts ...Option[T]) (api.Module, error)
- func NewCallContext(ctx context.Context, ins *InstantiationContext) context.Context
- func WithCallContext[T Module](ctx context.Context, mod HostModule[T], opts ...Option[T]) (context.Context, func())
- type CompilationContext
- type CompiledModule
- type Decorator
- type Function
- func F0[T any, R Result](fn func(T, context.Context) R) Function[T]
- func F1[T any, P Param[P], R Result](fn func(T, context.Context, P) R) Function[T]
- func F2[T any, P1 Param[P1], P2 Param[P2], R Result](fn func(T, context.Context, P1, P2) R) Function[T]
- func F3[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], R Result](fn func(T, context.Context, P1, P2, P3) R) Function[T]
- func F4[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], R Result](fn func(T, context.Context, P1, P2, P3, P4) R) Function[T]
- func F5[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], ...](fn func(T, context.Context, P1, P2, P3, P4, P5) R) Function[T]
- func F6[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], ...](fn func(T, context.Context, P1, P2, P3, P4, P5, P6) R) Function[T]
- func F7[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], ...](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7) R) Function[T]
- func F8[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], ...](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8) R) Function[T]
- type Functions
- type HostModule
- type InstantiationContext
- type Module
- type Option
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNoRuntime is an error returned when attempting to compile a host // module in a context which has no wazero runtime. ErrNoRuntime = errors.New("compilation context contains no wazero runtime") )
Functions ¶
func Build ¶
func Build[T Module](runtime wazero.Runtime, mod HostModule[T], decorators ...Decorator[T]) wazero.HostModuleBuilder
Build builds the host module p in the wazero runtime r, returning the instance of HostModuleBuilder that was created. This is a low level function which is only exposed for certain advanced use cases where a program might not be able to leverage Compile/Instantiate, most application should not need to use this function.
func Instantiate ¶
func Instantiate[T Module](ctx *InstantiationContext, compiled *CompiledModule[T], opts ...Option[T]) (api.Module, error)
Instantiate creates an module instance for the given compiled wazero host module. The list of options is used to pass configuration to the module instance.
The function returns the wazero module instance that was created from the underlying compiled module. The returned module is bound to the instantiation context. If the module is closed, its state is automatically removed from the parent context, as well as removed from the parent wazero runtime like any other module instance closed by the application.
func NewCallContext ¶
func NewCallContext(ctx context.Context, ins *InstantiationContext) context.Context
NewCallContext returns a Go context inheriting from ctx and containing the state needed for module instantiated from wazero host module to properly bind their methods to their receiver (e.g. the module instance).
Use this function when calling methods of an instantiated WebAssenbly module which may invoke exported functions of a wazero host module, for example:
// The program first creates the instantiation context and uses it to // instantiate compiled host module (not shown here). instiation := wazergo.NewInstantiationContext(...) ... // In this example the parent is the background context, but it might be any // other Go context relevant to the application. ctx = wazergo.NewCallContext(context.Background(), instantiation) start := module.ExportedFunction("_start") r, err := start.Call(ctx) if err != nil { ... }
func WithCallContext ¶
func WithCallContext[T Module](ctx context.Context, mod HostModule[T], opts ...Option[T]) (context.Context, func())
WithCallContext returns a Go context inheriting from ctx and containig the necessary state to be used in calls to exported functions of the given wazero host modul. This function is rarely used by applications, it is often more useful in tests to setup the test state without constructing the entire compilation and instantiation contexts (see NewCallContext instead).
Types ¶
type CompilationContext ¶
type CompilationContext struct {
// contains filtered or unexported fields
}
CompilationContext is a type carrying the state needed to perform the compilation of wazero host modules.
func NewCompilationContext ¶
func NewCompilationContext(ctx context.Context, rt wazero.Runtime) *CompilationContext
NewCompilationContext constructs a new wazero host module compilation context. The newly created instance captures the context and wazero runtime passed as arguments.
type CompiledModule ¶
type CompiledModule[T Module] struct { HostModule HostModule[T] wazero.CompiledModule }
CompiledModule represents a compiled version of a wazero host module.
func Compile ¶
func Compile[T Module](ctx *CompilationContext, mod HostModule[T], decorators ...Decorator[T]) (*CompiledModule[T], error)
Compile compiles a wazero host module within the given context.
type Decorator ¶
Decorator is an interface type which applies a transformation to a function.
func DecoratorFunc ¶
DecoratorFunc is a helper used to create decorators from functions using type inference to keep the syntax simple.
type Function ¶
type Function[T any] struct { Name string Params []Value Results []Value Func func(T, context.Context, api.Module, []uint64) }
Function represents a single function exported by a plugin. Programs may configure the fields individually but it is often preferrable to use one of the Func* constructors instead to let the Go compiler ensure type and memory safety when generating the code to bridge between WebAssembly and Go.
func F2 ¶
func F2[ T any, P1 Param[P1], P2 Param[P2], R Result, ](fn func(T, context.Context, P1, P2) R) Function[T]
F2 is the Function constructor for functions accepting two parameters.
func F3 ¶
func F3[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], R Result, ](fn func(T, context.Context, P1, P2, P3) R) Function[T]
F3 is the Function constructor for functions accepting three parameters.
func F4 ¶
func F4[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], R Result, ](fn func(T, context.Context, P1, P2, P3, P4) R) Function[T]
F4 is the Function constructor for functions accepting four parameters.
func F5 ¶
func F5[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5) R) Function[T]
F5 is the Function constructor for functions accepting five parameters.
func F6 ¶
func F6[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6) R) Function[T]
F6 is the Function constructor for functions accepting six parameters.
type Functions ¶
Functions is a map type representing the collection of functions exported by a plugin. The map keys are the names of that each function gets exported as. The function value is the description of the wazero host function to be added when building a plugin. The type parameter T is used to ensure consistency between the plugin definition and the functions that compose it.
type HostModule ¶
type HostModule[T Module] interface { // Returns the name of the host module (e.g. "wasi_snapshot_preview1"). Name() string // Returns the collection of functions exported by the host module. // The method may return the same value across multiple calls to this // method, the program is expected to treat it as a read-only value. Functions() Functions[T] // Creates a new instance of the host module type, using the list of options // passed as arguments to configure it. This method is intended to be called // automatically when instantiating a module via an instantiation context. Instantiate(...Option[T]) T }
HostModule is an interface representing type-safe wazero host modules. The interface is parametrized on the module type that it instantiates.
HostModule instances are expected to be immutable and therfore safe to use concurrently from multiple goroutines.
type InstantiationContext ¶
type InstantiationContext struct {
// contains filtered or unexported fields
}
InstantiationContext is a type carrying the state of instantiated wazero host modules. This context must be used to create call contexts to invoke exported functions of WebAssembly modules (see NewCallContext).
func NewInstantiationContext ¶
func NewInstantiationContext(ctx context.Context, rt wazero.Runtime) *InstantiationContext
NewInstantiationContext creates a new wazero host module instantiation context.
type Module ¶
Module is a type constraint used to validate that all module instances created from wazero host modules abide to the same set of requirements.
type Option ¶
type Option[T any] interface { // Configure is called to apply the configuration option to the value passed // as argument. Configure(T) }
Option is a generic interface used to represent options that apply configuration to a value.
func OptionFunc ¶
OptionFunc is a constructor which creates an option from a function. This function is useful to leverage type inference and not have to repeat the type T in the type parameter.
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
wasmtest
Package wasmtest provides building blocks useful to write tests for wazero host modules.
|
Package wasmtest provides building blocks useful to write tests for wazero host modules. |
Package wasm provides the generic components used to build wazero plugins.
|
Package wasm provides the generic components used to build wazero plugins. |