Makes all examples and docs use Runtime.Close (#537)

This removes tedium in our examples and docs by using `Runtime.Close`
instead of tracking everything. Internal tests still track too much, but
anyway at least this stops suggesting others should do it.

This also changes our examples to use log.PanicXX so that the line
number goes into the console output.

Signed-off-by: Adrian Cole <adrian@tetrate.io>
This commit is contained in:
Crypt Keeper
2022-05-10 12:08:25 +08:00
committed by GitHub
parent 8dd797e108
commit 03bfa31928
17 changed files with 127 additions and 109 deletions

View File

@@ -15,6 +15,9 @@ import (
// Ex. Below defines and instantiates a module named "env" with one function:
//
// ctx := context.Background()
// r := wazero.NewRuntime()
// defer r.Close(ctx) // This closes everything this Runtime created.
//
// hello := func() {
// fmt.Fprintln(stdout, "hello!")
// }
@@ -29,10 +32,8 @@ import (
// Compile(ctx, wazero.NewCompileConfig())
//
// env1, _ := r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig().WithName("env.1"))
// defer env1.Close(ctx)
//
// env2, _ := r.InstantiateModule(ctx, compiled, wazero.NewModuleConfig().WithName("env.2"))
// defer env2.Close(ctx)
//
// Notes:
// * ModuleBuilder is mutable. WithXXX functions return the same instance for chaining.
@@ -157,10 +158,13 @@ type ModuleBuilder interface {
ExportGlobalF64(name string, v float64) ModuleBuilder
// Compile returns a module to instantiate, or an error if any of the configuration is invalid.
//
// Note: Closing the wazero.Runtime closes any CompiledModule it compiled.
Compile(context.Context, CompileConfig) (CompiledModule, error)
// Instantiate is a convenience that calls Build, then Runtime.InstantiateModule, using default configuration.
//
// Note: Closing the wazero.Runtime closes any api.Module it instantiated.
// Note: Fields in the builder are copied during instantiation: Later changes do not affect the instantiated result.
// Note: To avoid using configuration defaults, use Compile instead.
Instantiate(context.Context) (api.Module, error)