Documentation ¶
Overview ¶
Package emscripten contains Go-defined special functions imported by Emscripten under the module name "env".
Emscripten has many imports which are triggered on build flags. Use FunctionExporter, instead of Instantiate, to define more "env" functions.
Relationship to WASI ¶
Emscripten typically requires wasi_snapshot_preview1 to implement exit.
See wasi_snapshot_preview1.Instantiate and https://github.com/emscripten-core/emscripten/wiki/WebAssembly-Standalone
Example (FunctionExporter) ¶
This shows how to instantiate Emscripten function imports when you also need other functions in the "env" module.
package main import ( "context" _ "embed" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/imports/emscripten" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) func main() { 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"). NewFunctionBuilder(). WithFunc(func() uint32 { return 1 }). Export("get_int") // Now, add Emscripten special function imports into it. emscripten.NewFunctionExporter().ExportFunctions(envBuilder) }
Output:
Example (Instantiate) ¶
This shows how to instantiate Emscripten function imports.
package main import ( "context" _ "embed" "github.com/tetratelabs/wazero" "github.com/tetratelabs/wazero/imports/emscripten" "github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1" ) func main() { 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:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Instantiate ¶
Instantiate instantiates the "env" module used by Emscripten into the runtime.
Notes ¶
- 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.
Types ¶
type FunctionExporter ¶
type FunctionExporter interface { // ExportFunctions builds functions to export with a wazero.HostModuleBuilder // named "env". ExportFunctions(wazero.HostModuleBuilder) }
FunctionExporter configures the functions in the "env" module used by Emscripten.
Notes ¶
- This is an interface for decoupling, not third-party implementations. All implementations are in wazero.
func NewFunctionExporter ¶
func NewFunctionExporter() FunctionExporter
NewFunctionExporter returns a FunctionExporter object with trace disabled.
func NewFunctionExporterForModule ¶ added in v1.1.0
func NewFunctionExporterForModule(guest wazero.CompiledModule) (FunctionExporter, error)
NewFunctionExporterForModule returns a guest-specific FunctionExporter, populated with any known functions used in emscripten.