diff --git a/imports/emscripten/emscripten.go b/imports/emscripten/emscripten.go index 32e499572d3..0e1d1f17119 100644 --- a/imports/emscripten/emscripten.go +++ b/imports/emscripten/emscripten.go @@ -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) @@ -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) @@ -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{} } diff --git a/imports/emscripten/emscripten_example_test.go b/imports/emscripten/emscripten_example_test.go index 8f6d0858069..af322fb9bce 100644 --- a/imports/emscripten/emscripten_example_test.go +++ b/imports/emscripten/emscripten_example_test.go @@ -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) @@ -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: } @@ -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"). @@ -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: }