Files
wazero/imports/emscripten/emscripten_example_test.go
Crypt Keeper 1561c4ca7b Adds MustInstantiate to host imports (#814)
Signed-off-by: Adrian Cole <adrian@tetrate.io>
2022-09-28 16:21:30 +08:00

49 lines
1.4 KiB
Go

package emscripten_test
import (
"context"
_ "embed"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/emscripten"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
// This shows how to instantiate Emscripten function imports.
func Example_instantiate() {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx) // This closes everything this Runtime created.
// Add WASI which is typically required when using Emscripten.
wasi_snapshot_preview1.MustInstantiate(ctx, r)
// Now, add the "env" module to the runtime, Emscripten default imports.
emscripten.MustInstantiate(ctx, r)
// Output:
}
// This shows how to instantiate Emscripten function 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.
// Add WASI which is typically required when using Emscripten.
wasi_snapshot_preview1.MustInstantiate(ctx, r)
// Next, construct your own module builder for "env" with any functions
// you need.
envBuilder := r.NewHostModuleBuilder("env").
ExportFunction("get_int", func() uint32 { return 1 })
// Now, add Emscripten special function imports into it.
emscripten.NewFunctionExporter().ExportFunctions(envBuilder)
// Output:
}