-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwazero.go
55 lines (49 loc) · 1.29 KB
/
wazero.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//go:build !nowazero
// +build !nowazero
package wypes
import (
"context"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
// DefineWazero registers all the host modules in the given wazero runtime.
func (ms Modules) DefineWazero(runtime wazero.Runtime, refs Refs) error {
if refs == nil {
refs = NewMapRefs()
}
for modName, funcs := range ms {
err := funcs.DefineWazero(runtime, modName, refs)
if err != nil {
return err
}
}
return nil
}
// DefineWazero registers the host module in the given wazero runtime.
func (m Module) DefineWazero(runtime wazero.Runtime, modName string, refs Refs) error {
var err error
mb := runtime.NewHostModuleBuilder(modName)
for funcName, funcDef := range m {
fb := mb.NewFunctionBuilder()
fb = fb.WithGoModuleFunction(
wazeroAdaptHostFunc(funcDef, refs),
funcDef.ParamValueTypes(),
funcDef.ResultValueTypes(),
)
mb = fb.Export(funcName)
}
_, err = mb.Instantiate(context.Background())
return err
}
func wazeroAdaptHostFunc(hf HostFunc, refs Refs) api.GoModuleFunction {
return api.GoModuleFunc(func(ctx context.Context, mod api.Module, stack []uint64) {
adaptedStack := SliceStack(stack)
store := Store{
Memory: mod.Memory(),
Stack: &adaptedStack,
Refs: refs,
Context: ctx,
}
hf.Call(&store)
})
}