wa-lang.org/wazero@v1.0.2/experimental/compilation_cache_example_test.go (about) 1 package experimental_test 2 3 import ( 4 "context" 5 "log" 6 "os" 7 8 "wa-lang.org/wazero" 9 "wa-lang.org/wazero/experimental" 10 ) 11 12 // This is a basic example of using the file system compilation cache via WithCompilationCacheDirName. 13 // The main goal is to show how it is configured. 14 func Example_withCompilationCacheDirName() { 15 // Prepare a cache directory. 16 cacheDir, err := os.MkdirTemp("", "example") 17 if err != nil { 18 log.Panicln(err) 19 } 20 defer os.RemoveAll(cacheDir) 21 22 // Append the directory into the context for configuration. 23 ctx, err := experimental.WithCompilationCacheDirName(context.Background(), cacheDir) 24 if err != nil { 25 log.Panicln(err) 26 } 27 28 // Repeat newRuntimeCompileClose with the same cache directory. 29 newRuntimeCompileClose(ctx) 30 // Since the above stored compiled functions to dist, below won't compile. 31 // Instead, code stored in the file cache is re-used. 32 newRuntimeCompileClose(ctx) 33 newRuntimeCompileClose(ctx) 34 35 // Output: 36 // 37 } 38 39 // newRuntimeCompileDestroy creates a new wazero.Runtime, compile a binary, and then delete the runtime. 40 func newRuntimeCompileClose(ctx context.Context) { 41 r := wazero.NewRuntime(ctx) 42 defer r.Close(ctx) // This closes everything this Runtime created except the file system cache. 43 44 _, err := r.CompileModule(ctx, fsWasm) 45 if err != nil { 46 log.Panicln(err) 47 } 48 }