package emscripten

import "github.com/tetratelabs/wazero/imports/emscripten"

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.

Code:play 

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)

}
Example (Instantiate)

This shows how to instantiate Emscripten function imports.

Code:play 

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)

}

Index

Examples

Functions

func Instantiate

func Instantiate(ctx context.Context, r wazero.Runtime) (api.Closer, error)

Instantiate instantiates the "env" module used by Emscripten into the runtime.

Notes

func MustInstantiate

func MustInstantiate(ctx context.Context, r wazero.Runtime)

MustInstantiate calls Instantiate or panics on error.

This is a simpler function for those who know the module "env" is not already instantiated, and don't need to unload it.

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.

func NewFunctionExporter

func NewFunctionExporter() FunctionExporter

NewFunctionExporter returns a FunctionExporter object with trace disabled.

Source Files

emscripten.go

Version
v1.0.1
Published
Mar 29, 2023
Platform
linux/amd64
Imports
4 packages
Last checked
18 seconds ago

Tools for package owners.