...
1 package main
2
3 import (
4 "context"
5 _ "embed"
6 "fmt"
7 "log"
8 "sync"
9
10 "github.com/tetratelabs/wazero"
11 )
12
13
14
15
16
17
18 var addWasm []byte
19
20 func main() {
21
22 ctx := context.Background()
23
24
25 r := wazero.NewRuntime(ctx)
26 defer r.Close(ctx)
27
28
29 compiledWasm, err := r.CompileModule(ctx, addWasm)
30 if err != nil {
31 log.Panicf("failed to compile Wasm binary: %v", err)
32 }
33
34 var wg sync.WaitGroup
35 const goroutines = 50
36 wg.Add(goroutines)
37
38
39 for i := 0; i < goroutines; i++ {
40 go func(i int) {
41 defer wg.Done()
42
43
44 instance, err := r.InstantiateModule(ctx, compiledWasm, wazero.NewModuleConfig().WithName(""))
45 if err != nil {
46 log.Panicf("[%d] failed to instantiate %v", i, err)
47 }
48
49
50 result, err := instance.ExportedFunction("add").Call(ctx, uint64(i), uint64(i))
51 if err != nil {
52 log.Panicf("[%d] failed to invoke \"add\": %v", i, err)
53 }
54
55
56 expected := uint64(i * 2)
57 if result[0] != expected {
58 log.Panicf("expected %d, but got %d", expected, result[0])
59 }
60
61
62 fmt.Println(expected)
63 }(i)
64 }
65
66 wg.Wait()
67 }
68
View as plain text