Files
wazero/imports/assemblyscript/assemblyscript_example_test.go
Crypt Keeper d108ce4c43 Restores ability to define host functions w/o context via reflection (#832)
This restores the ability to leave out the initial context parameter
when defining functions with reflection. This is important because some
projects are porting from a different library to wazero, and all the
alternatives are not contextualized.

For example, this project is porting envoy host functions, and the
original definitions (in mosn) don't have a context parameter. By being
lenient, they can migrate easier.

See 6b813482b6/pkg/proxywasm/wazero/imports_v1.go

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-10-28 12:44:12 -07:00

46 lines
1.2 KiB
Go

package assemblyscript_test
import (
"context"
_ "embed"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/assemblyscript"
)
// This shows how to instantiate AssemblyScript's special imports.
func Example_instantiate() {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.
// This adds the "env" module to the runtime, with AssemblyScript's special
// function imports.
assemblyscript.MustInstantiate(ctx, r)
// Output:
}
// This shows how to instantiate AssemblyScript's special imports when you also
// need other functions in the "env" module.
func Example_functionExporter() {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.
// First construct your own module builder for "env"
envBuilder := r.NewHostModuleBuilder("env").
NewFunctionBuilder().
WithFunc(func() uint32 { return 1 }).
Export("get_int")
// Now, add AssemblyScript special function imports into it.
assemblyscript.NewFunctionExporter().
WithAbortMessageDisabled().
ExportFunctions(envBuilder)
// Output:
}