-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathcounter.go
96 lines (81 loc) · 2.55 KB
/
counter.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"context"
_ "embed"
"fmt"
"log"
"os"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// counterWasm was generated by the following:
//
// cd testdata; wat2wasm --debug-names counter.wat
//
//go:embed testdata/counter.wasm
var counterWasm []byte
// main shows how to share the same compilation cache across the multiple runtimes.
func main() {
// Choose the context to use for function calls.
ctx := context.Background()
// Prepare a cache directory.
cacheDir, err := os.MkdirTemp("", "example")
if err != nil {
log.Panicln(err)
}
defer os.RemoveAll(cacheDir)
// Initializes the new compilation cache with the cache directory.
// This allows the compilation caches to be shared even across multiple OS processes.
cache, err := wazero.NewCompilationCacheWithDir(cacheDir)
if err != nil {
log.Panicln(err)
}
defer cache.Close(ctx)
// Creates a shared runtime config to share the cache across multiple wazero.Runtime.
runtimeConfig := wazero.NewRuntimeConfig().WithCompilationCache(cache)
// Creates two wazero.Runtimes with the same compilation cache.
runtimeFoo := wazero.NewRuntimeWithConfig(ctx, runtimeConfig)
runtimeBar := wazero.NewRuntimeWithConfig(ctx, runtimeConfig)
// Instantiate two modules on separate Runtimes with identical configuration, which allows each instance
// has the isolated states of "env" module.
m1 := instantiateWithEnv(ctx, runtimeFoo)
m2 := instantiateWithEnv(ctx, runtimeBar)
for i := 0; i < 2; i++ {
fmt.Printf("m1 counter=%d\n", counterGet(ctx, m1))
fmt.Printf("m2 counter=%d\n", counterGet(ctx, m2))
}
}
// count calls "counter.get" in the given namespace
func counterGet(ctx context.Context, mod api.Module) uint64 {
results, err := mod.ExportedFunction("get").Call(ctx)
if err != nil {
log.Panicln(err)
}
return results[0]
}
// counter is an example showing state that needs to be independent per importing module.
type counter struct {
counter uint32
}
func (e *counter) getAndIncrement() (ret uint32) {
ret = e.counter
e.counter++
return
}
// instantiateWithEnv returns a module instance.
func instantiateWithEnv(ctx context.Context, r wazero.Runtime) api.Module {
// Instantiate a new "env" module which exports a stateful function.
c := &counter{}
_, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(c.getAndIncrement).Export("next_i32").
Instantiate(ctx)
if err != nil {
log.Panicln(err)
}
// Instantiate the module that imports "env".
mod, err := r.Instantiate(ctx, counterWasm)
if err != nil {
log.Panicln(err)
}
return mod
}