-
Notifications
You must be signed in to change notification settings - Fork 243
/
emscripten_example_test.go
55 lines (42 loc) · 1.49 KB
/
emscripten_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package emscripten_test
import (
"context"
_ "embed"
"log"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/emscripten"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
// This shows how to instantiate Emscripten function imports.
func Example_instantiate() {
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.
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
log.Panicln(err)
}
// Now, add the "env" module to the runtime, Emscripten default imports.
if _, err := emscripten.Instantiate(ctx, r); err != nil {
log.Panicln(err)
}
// Output:
}
// This shows how to instantiate Emscripten function imports when you also need
// other functions in the "env" module.
func Example_functionExporter() {
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.
if _, err := wasi_snapshot_preview1.Instantiate(ctx, r); err != nil {
log.Panicln(err)
}
// Next, construct your own module builder for "env" with any functions
// you need.
envBuilder := r.NewModuleBuilder("env").
ExportFunction("get_int", func() uint32 { return 1 })
// Now, add Emscripten special function imports into it.
emscripten.NewFunctionExporter().ExportFunctions(envBuilder)
// Output:
}