Documentation ¶
Index ¶
- func Build[T Module](runtime wazero.Runtime, mod HostModule[T]) wazero.HostModuleBuilder
- func Configure[T any](value T, options ...Option[T])
- func WithModuleInstance[T Module](ctx context.Context, ins *ModuleInstance[T]) context.Context
- 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 F10[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, P9, P10) R) Function[T]
- func F11[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, P9, P10, P11) R) Function[T]
- func F12[T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], ...](...) 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]
- func F9[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, P9) R) Function[T]
- type Functions
- type HostModule
- type Module
- type ModuleInstance
- func (m *ModuleInstance[T]) Close(ctx context.Context) error
- func (m *ModuleInstance[T]) CloseWithExitCode(ctx context.Context, _ uint32) error
- func (m *ModuleInstance[T]) ExportedFunction(name string) api.Function
- func (m *ModuleInstance[T]) Name() string
- func (m *ModuleInstance[T]) String() string
- type Option
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Build ¶
func Build[T Module](runtime wazero.Runtime, mod HostModule[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 WithModuleInstance ¶ added in v0.6.0
WithModuleInstance 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 modules instances for the host modules. instance1 := wazergo.MustInstantiate(ctx, runtime, firstHostModule) instance2 := wazergo.MustInstantiate(ctx, runtime, otherHostModule) ... // In this example the parent is the background context, but it might be any // other Go context relevant to the application. ctx := context.Background() ctx = wazergo.WithModuleInstance(ctx, instance1) ctx = wazergo.WithModuleInstance(ctx, instance2) start := module.ExportedFunction("_start") r, err := start.Call(ctx) if err != nil { ... }
Types ¶
type CompiledModule ¶
type CompiledModule[T Module] struct { HostModule HostModule[T] wazero.CompiledModule // contains filtered or unexported fields }
CompiledModule represents a compiled version of a wazero host module.
func Compile ¶
func Compile[T Module](ctx context.Context, runtime wazero.Runtime, mod HostModule[T]) (*CompiledModule[T], error)
Compile compiles a wazero host module within the given context.
func MustCompile ¶ added in v0.12.0
func MustCompile[T Module](ctx context.Context, runtime wazero.Runtime, mod HostModule[T]) *CompiledModule[T]
MustCompile is like Compile but it panics if there is an error.
func (*CompiledModule[T]) Instantiate ¶ added in v0.6.0
func (c *CompiledModule[T]) Instantiate(ctx context.Context, options ...Option[T]) (*ModuleInstance[T], error)
Instantiate creates an instance of the compiled module for in the given runtime
Instantiate may be called multiple times to create multiple copies of the host module state. This is useful to allow the program to create scopes where the state of the host module needs to bind uniquely to a subset of the guest modules instantiated in the runtime.
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 F10 ¶ added in v0.9.0
func F10[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], P8 Param[P8], P9 Param[P9], P10 Param[P10], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10) R) Function[T]
F10 is the Function constructor for functions accepting ten parameters.
func F11 ¶ added in v0.9.0
func F11[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], P8 Param[P8], P9 Param[P9], P10 Param[P10], P11 Param[P11], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11) R) Function[T]
F11 is the Function constructor for functions accepting eleven parameters.
func F12 ¶ added in v0.9.0
func F12[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], P8 Param[P8], P9 Param[P9], P10 Param[P10], P11 Param[P11], P12 Param[P12], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12) R) Function[T]
F12 is the Function constructor for functions accepting twelve parameters.
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.
func F7 ¶
func F7[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7) R) Function[T]
F7 is the Function constructor for functions accepting seven parameters.
func F8 ¶
func F8[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], P8 Param[P8], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8) R) Function[T]
F8 is the Function constructor for functions accepting eight parameters.
func F9 ¶ added in v0.9.0
func F9[ T any, P1 Param[P1], P2 Param[P2], P3 Param[P3], P4 Param[P4], P5 Param[P5], P6 Param[P6], P7 Param[P7], P8 Param[P8], P9 Param[P9], R Result, ](fn func(T, context.Context, P1, P2, P3, P4, P5, P6, P7, P8, P9) R) Function[T]
F9 is the Function constructor for functions accepting nine parameters.
func (*Function[T]) NumParams ¶ added in v0.19.0
NumParams is the number of parameters this function reads from the stack.
Note that this is not necessarily the same as len(f.Params), since Params holds higher level values that may correspond to more than one stack param.
func (*Function[T]) NumResults ¶ added in v0.19.0
NumResults is the number of return values this function writes to the stack.
Note that this is not necessarily the same as len(f.Results), since Results holds higher level values that may correspond to more than one stack result.
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(ctx context.Context, options ...Option[T]) (T, error) }
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.
func Decorate ¶ added in v0.7.0
func Decorate[T Module](mod HostModule[T], decorators ...Decorator[T]) HostModule[T]
Decorate returns a version of the given host module where the decorators were applied to all its functions.
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 ModuleInstance ¶ added in v0.8.0
ModuleInstance represents a module instance created from a compiled host module.
func Instantiate ¶
func Instantiate[T Module](ctx context.Context, runtime wazero.Runtime, mod HostModule[T], options ...Option[T]) (*ModuleInstance[T], error)
Instantiate compiles and instantiates a host module.
func MustInstantiate ¶ added in v0.6.0
func MustInstantiate[T Module](ctx context.Context, runtime wazero.Runtime, mod HostModule[T], options ...Option[T]) *ModuleInstance[T]
MustInstantiate is like Instantiate but it panics if an error is encountered.
func (*ModuleInstance[T]) Close ¶ added in v0.12.0
func (m *ModuleInstance[T]) Close(ctx context.Context) error
func (*ModuleInstance[T]) CloseWithExitCode ¶ added in v0.12.0
func (m *ModuleInstance[T]) CloseWithExitCode(ctx context.Context, _ uint32) error
func (*ModuleInstance[T]) ExportedFunction ¶ added in v0.12.0
func (m *ModuleInstance[T]) ExportedFunction(name string) api.Function
func (*ModuleInstance[T]) Name ¶ added in v0.12.0
func (m *ModuleInstance[T]) Name() string
func (*ModuleInstance[T]) String ¶ added in v0.12.0
func (m *ModuleInstance[T]) String() string
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.