Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

deprecated emscripten.Instantiate and it's related functions in favor of InstantiateForModule #1782

Merged
merged 1 commit into from
Oct 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
}
Loading