Skip to content

Commit

Permalink
deprecated emscripten.Instantiate and it's related functions in favor…
Browse files Browse the repository at this point in the history
… of InstantiateForModule.

rewrote emscripten examples to use InstantiateForModule

Signed-off-by: Daniel Lockhart <[email protected]>
  • Loading branch information
Daniel Lockhart authored and Danlock committed Oct 15, 2023
1 parent 44f23e2 commit 81a8ecf
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions imports/emscripten/emscripten.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ const i32 = wasm.ValueTypeI32
//
// This is a simpler function for those who know the module "env" is not
// already instantiated, and don't need to unload it.
//
// Deprecated: Due to Emscripten dynamic import generation, InstantiateForModule should be used instead.
func MustInstantiate(ctx context.Context, r wazero.Runtime) {
if _, err := Instantiate(ctx, r); err != nil {
panic(err)
Expand All @@ -42,6 +44,8 @@ func MustInstantiate(ctx context.Context, r wazero.Runtime) {
// - Failure cases are documented on wazero.Runtime InstantiateModule.
// - Closing the wazero.Runtime has the same effect as closing the result.
// - To add more functions to the "env" module, use FunctionExporter.
//
// Deprecated: Due to Emscripten dynamic import generation, InstantiateForModule should be used instead.
func Instantiate(ctx context.Context, r wazero.Runtime) (api.Closer, error) {
builder := r.NewHostModuleBuilder("env")
NewFunctionExporter().ExportFunctions(builder)
Expand All @@ -62,6 +66,7 @@ type FunctionExporter interface {
}

// NewFunctionExporter returns a FunctionExporter object with trace disabled.
// Deprecated: Due to Emscripten dynamic import generation, NewFunctionExporterForModule should be used instead.
func NewFunctionExporter() FunctionExporter {
return &functionExporter{}
}
Expand Down
30 changes: 25 additions & 5 deletions imports/emscripten/emscripten_example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)

//go:embed testdata/invoke.wasm
var invokeWasm []byte

// This shows how to instantiate Emscripten function imports.
func Example_instantiate() {
func Example_instantiateForModule() {
ctx := context.Background()

r := wazero.NewRuntime(ctx)
Expand All @@ -19,8 +22,17 @@ func Example_instantiate() {
// 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)
// Compile the WASM so wazero can handle dynamically generated imports.
compiled, err := r.CompileModule(ctx, invokeWasm)
if err != nil {
panic(err)
}

envCloser, err := emscripten.InstantiateForModule(ctx, r, compiled)
if err != nil {
panic(err)
}
defer envCloser.Close(ctx) // This closes the env module.

// Output:
}
Expand All @@ -36,6 +48,15 @@ func Example_functionExporter() {
// Add WASI which is typically required when using Emscripten.
wasi_snapshot_preview1.MustInstantiate(ctx, r)

// Compile the WASM so wazero can handle dynamically generated imports.
compiled, err := r.CompileModule(ctx, invokeWasm)
if err != nil {
panic(err)
}
exporter, err := emscripten.NewFunctionExporterForModule(compiled)
if err != nil {
panic(err)
}
// Next, construct your own module builder for "env" with any functions
// you need.
envBuilder := r.NewHostModuleBuilder("env").
Expand All @@ -44,7 +65,6 @@ func Example_functionExporter() {
Export("get_int")

// Now, add Emscripten special function imports into it.
emscripten.NewFunctionExporter().ExportFunctions(envBuilder)

exporter.ExportFunctions(envBuilder)
// Output:
}

0 comments on commit 81a8ecf

Please sign in to comment.