Files
wazero/examples/multiple-runtimes
Crypt Keeper b758344212 API BREAK: renames InstantiateModuleFromBinary to Instantiate (#1129)
This renames `InstantiateModuleFromBinary` to `Instantiate` to both make
first time use simpler to write and also de-complicate adding a
`WithConfig` variant as requested in #1105

End users in simple case need to change their signature like so.
```diff
-       mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
+       mod, err := r.Instantiate(ctx, addWasm)
```

In practice, many will not need to change their signature because they
had to use the `InstantiateModule` function in order to assign
configuration such as the module name, filesystem or use a real clock.
Instead, they had to use the more complicated chain of `CompileModule`
and `InstantiateModule` even when only assigning config. Users in this
situation can opt into the more simplified syntax below:

```go
mod, err := r.InstantiateWithConfig(ctx, addWasm,
	wazero.NewModuleConfig().WithName("adder"))
```



```diff
-       mod, err := r.InstantiateModuleFromBinary(ctx, addWasm)
+       mod, err := r.Instantiate(ctx, addWasm)
```

Signed-off-by: Adrian Cole <adrian@tetrate.io>
2023-02-15 14:52:17 -10:00
..
2023-01-10 14:11:46 +09:00
2023-01-10 14:11:46 +09:00
2023-01-10 14:11:46 +09:00

Multiple runtimes

Sometimes, a Wasm module might want a stateful host module. In that case, we have to create multiple wazero.Runtime if we want to run it multiple times. This example shows how to use multiple Runtimes while sharing the same compilation caches so that we could reduce the compilation time of Wasm modules.

In this example, we create two wazero.Runtime which shares the underlying cache, and instantiate a Wasm module which requires the stateful "env" module on each runtime.

$ go run counter.go
m1 count=0
m2 count=0
m1 count=1
m2 count=1