Formerly, we introduced `wazero.Namespace` to help avoid module name or import conflicts while still sharing the runtime's compilation cache. Now that we've introduced `CompilationCache` `wazero.Namespace` is no longer necessary. By removing it, we reduce the conceptual load on end users as well internal complexity. Since most users don't use namespace, the change isn't very impactful.
Users who are only trying to avoid module name conflict can generate a name like below instead of using multiple runtimes:
```go
moduleName := fmt.Sprintf("%d", atomic.AddUint64(&m.instanceCounter, 1))
module, err := runtime.InstantiateModule(ctx, compiled, config.WithName(moduleName))
```
For `HostModuleBuilder` users, we no longer take `Namespace` as the last parameter of `Instantiate` method:
```diff
// log to the console.
_, err := r.NewHostModuleBuilder("env").
NewFunctionBuilder().WithFunc(logString).Export("log").
- Instantiate(ctx, r)
+ Instantiate(ctx)
if err != nil {
log.Panicln(err)
}
```
The following is an example diff a use of namespace can use to keep compilation cache while also ensuring their modules don't conflict:
```diff
func useMultipleRuntimes(ctx context.Context, cache) {
- r := wazero.NewRuntime(ctx)
+ cache := wazero.NewCompilationCache()
for i := 0; i < N; i++ {
- // Create a new namespace to instantiate modules into.
- ns := r.NewNamespace(ctx) // Note: this is closed when the Runtime is
+ r := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCompilationCache(cache))
// Instantiate a new "env" module which exports a stateful function.
_, err := r.NewHostModuleBuilder("env").
```
Signed-off-by: Takeshi Yoneda <takeshi@tetrate.io>
97 lines
2.8 KiB
Go
97 lines
2.8 KiB
Go
package binary
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/tetratelabs/wazero/internal/testing/require"
|
|
"github.com/tetratelabs/wazero/internal/wasm"
|
|
)
|
|
|
|
var addLocalZeroLocalTwo = []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 2, wasm.OpcodeI32Add, wasm.OpcodeEnd}
|
|
|
|
func TestEncodeCode(t *testing.T) {
|
|
addLocalZeroLocalOne := []byte{wasm.OpcodeLocalGet, 0, wasm.OpcodeLocalGet, 1, wasm.OpcodeI32Add, wasm.OpcodeEnd}
|
|
tests := []struct {
|
|
name string
|
|
input *wasm.Code
|
|
expected []byte
|
|
}{
|
|
{
|
|
name: "smallest function body",
|
|
input: &wasm.Code{ // e.g. (func)
|
|
Body: []byte{wasm.OpcodeEnd},
|
|
},
|
|
expected: []byte{
|
|
0x02, // 2 bytes to encode locals and the body
|
|
0x00, // no local blocks
|
|
wasm.OpcodeEnd, // Body
|
|
},
|
|
},
|
|
{
|
|
name: "params and instructions", // local.get index is params, then locals
|
|
input: &wasm.Code{ // e.g. (func (type 3) local.get 0 local.get 1 i32.add)
|
|
Body: addLocalZeroLocalOne,
|
|
},
|
|
expected: append([]byte{
|
|
0x07, // 7 bytes to encode locals and the body
|
|
0x00, // no local blocks
|
|
},
|
|
addLocalZeroLocalOne..., // Body
|
|
),
|
|
},
|
|
{
|
|
name: "locals and instructions",
|
|
input: &wasm.Code{ // e.g. (func (result i32) (local i32, i32) local.get 0 local.get 1 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalOne,
|
|
},
|
|
expected: append([]byte{
|
|
0x09, // 9 bytes to encode locals and the body
|
|
0x01, // 1 local block
|
|
0x02, wasm.ValueTypeI32, // local block 1
|
|
},
|
|
addLocalZeroLocalOne..., // Body
|
|
),
|
|
},
|
|
{
|
|
name: "mixed locals and instructions",
|
|
input: &wasm.Code{ // e.g. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalTwo,
|
|
},
|
|
expected: append([]byte{
|
|
0x0d, // 13 bytes to encode locals and the body
|
|
0x03, // 3 local blocks
|
|
0x01, wasm.ValueTypeI32, // local block 1
|
|
0x01, wasm.ValueTypeI64, // local block 2
|
|
0x01, wasm.ValueTypeI32, // local block 3
|
|
},
|
|
addLocalZeroLocalTwo..., // Body
|
|
),
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tc := tt
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
bytes := encodeCode(tc.input)
|
|
require.Equal(t, tc.expected, bytes)
|
|
})
|
|
}
|
|
}
|
|
|
|
func BenchmarkEncodeCode(b *testing.B) {
|
|
input := &wasm.Code{ // e.g. (func (result i32) (local i32) (local i64) (local i32) local.get 0 local.get 2 i32.add)
|
|
LocalTypes: []wasm.ValueType{wasm.ValueTypeI32, wasm.ValueTypeI64, wasm.ValueTypeI32},
|
|
Body: addLocalZeroLocalTwo,
|
|
}
|
|
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
if bytes := encodeCode(input); len(bytes) == 0 {
|
|
b.Fatal("didn't encode anything")
|
|
}
|
|
}
|
|
}
|