...
1 package wazero_test
2
3 import (
4 "context"
5 _ "embed"
6 "log"
7 "os"
8
9 "github.com/tetratelabs/wazero"
10 )
11
12
13
14 func Example_compileCache() {
15
16 cacheDir, err := os.MkdirTemp("", "example")
17 if err != nil {
18 log.Panicln(err)
19 }
20 defer os.RemoveAll(cacheDir)
21
22 ctx := context.Background()
23
24
25 cache := newCompilationCacheWithDir(cacheDir)
26 defer cache.Close(ctx)
27 config := wazero.NewRuntimeConfig().WithCompilationCache(cache)
28
29
30 newRuntimeCompileClose(ctx, config)
31 newRuntimeCompileClose(ctx, config)
32
33
34
35 newRuntimeCompileClose(ctx, config.WithCompilationCache(newCompilationCacheWithDir(cacheDir)))
36 newRuntimeCompileClose(ctx, config.WithCompilationCache(newCompilationCacheWithDir(cacheDir)))
37
38
39
40 }
41
42 func newCompilationCacheWithDir(cacheDir string) wazero.CompilationCache {
43 cache, err := wazero.NewCompilationCacheWithDir(cacheDir)
44 if err != nil {
45 log.Panicln(err)
46 }
47 return cache
48 }
49
50
51 func newRuntimeCompileClose(ctx context.Context, config wazero.RuntimeConfig) {
52 r := wazero.NewRuntimeWithConfig(ctx, config)
53 defer r.Close(ctx)
54
55 _, err := r.CompileModule(ctx, addWasm)
56 if err != nil {
57 log.Panicln(err)
58 }
59 }
60
View as plain text