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>
124 lines
2.9 KiB
Go
124 lines
2.9 KiB
Go
package wasm
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"github.com/tetratelabs/wazero/api"
|
|
)
|
|
|
|
// moduleListNode is a node in a doubly linked list of names.
|
|
type moduleListNode struct {
|
|
name string
|
|
module *ModuleInstance
|
|
next, prev *moduleListNode
|
|
}
|
|
|
|
// setModule makes the module visible for import.
|
|
func (s *Store) setModule(m *ModuleInstance) error {
|
|
s.mux.Lock()
|
|
defer s.mux.Unlock()
|
|
node, ok := s.nameToNode[m.Name]
|
|
if !ok {
|
|
return fmt.Errorf("module[%s] name has not been required", m.Name)
|
|
}
|
|
|
|
node.module = m
|
|
return nil
|
|
}
|
|
|
|
// deleteModule makes the moduleName available for instantiation again.
|
|
func (s *Store) deleteModule(moduleName string) error {
|
|
s.mux.Lock()
|
|
defer s.mux.Unlock()
|
|
node, ok := s.nameToNode[moduleName]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
|
|
// remove this module name
|
|
if node.prev != nil {
|
|
node.prev.next = node.next
|
|
} else {
|
|
s.moduleList = node.next
|
|
}
|
|
if node.next != nil {
|
|
node.next.prev = node.prev
|
|
}
|
|
delete(s.nameToNode, moduleName)
|
|
return nil
|
|
}
|
|
|
|
// module returns the module of the given name or error if not in this store
|
|
func (s *Store) module(moduleName string) (*ModuleInstance, error) {
|
|
s.mux.RLock()
|
|
defer s.mux.RUnlock()
|
|
node, ok := s.nameToNode[moduleName]
|
|
if !ok {
|
|
return nil, fmt.Errorf("module[%s] not in store", moduleName)
|
|
}
|
|
|
|
if node.module == nil {
|
|
return nil, fmt.Errorf("module[%s] not set in store", moduleName)
|
|
}
|
|
|
|
return node.module, nil
|
|
}
|
|
|
|
// requireModules returns all instantiated modules whose names equal the keys in the input, or errs if any are missing.
|
|
func (s *Store) requireModules(moduleNames map[string]struct{}) (map[string]*ModuleInstance, error) {
|
|
ret := make(map[string]*ModuleInstance, len(moduleNames))
|
|
|
|
s.mux.RLock()
|
|
defer s.mux.RUnlock()
|
|
|
|
for n := range moduleNames {
|
|
node, ok := s.nameToNode[n]
|
|
if !ok {
|
|
return nil, fmt.Errorf("module[%s] not instantiated", n)
|
|
}
|
|
ret[n] = node.module
|
|
}
|
|
return ret, nil
|
|
}
|
|
|
|
// requireModuleName is a pre-flight check to reserve a module.
|
|
// This must be reverted on error with deleteModule if initialization fails.
|
|
func (s *Store) requireModuleName(moduleName string) error {
|
|
s.mux.Lock()
|
|
defer s.mux.Unlock()
|
|
if _, ok := s.nameToNode[moduleName]; ok {
|
|
return fmt.Errorf("module[%s] has already been instantiated", moduleName)
|
|
}
|
|
|
|
// add the newest node to the moduleNamesList as the head.
|
|
node := &moduleListNode{
|
|
name: moduleName,
|
|
next: s.moduleList,
|
|
}
|
|
if node.next != nil {
|
|
node.next.prev = node
|
|
}
|
|
s.moduleList = node
|
|
s.nameToNode[moduleName] = node
|
|
return nil
|
|
}
|
|
|
|
// AliasModule aliases the instantiated module named `src` as `dst`.
|
|
//
|
|
// Note: This is only used for spectests.
|
|
func (s *Store) AliasModule(src, dst string) error {
|
|
s.mux.Lock()
|
|
defer s.mux.Unlock()
|
|
s.nameToNode[dst] = s.nameToNode[src]
|
|
return nil
|
|
}
|
|
|
|
// Module implements wazero.Runtime Module
|
|
func (s *Store) Module(moduleName string) api.Module {
|
|
m, err := s.module(moduleName)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
return m.CallCtx
|
|
}
|