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
56
57
58
59
60
61
62
63
64
65
66
67
|
package run
import (
"context"
"time"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
)
type Process struct {
Output []byte
}
func (p *Process) Putchar(ctx context.Context, stack []uint64) {
x := api.DecodeI32(stack[0])
p.putchar(x)
}
func (p *Process) putchar(char int32) {
p.Output = append(p.Output, byte(char))
}
func Run(bin []byte) (*ExecResult, error) {
ctx := context.Background()
r := wazero.NewRuntime(ctx)
defer r.Close(ctx)
p := &Process{}
env, err := r.NewHostModuleBuilder("h").
NewFunctionBuilder().
WithGoFunction(api.GoFunc(p.Putchar), []api.ValueType{api.ValueTypeI32}, nil).
Export("h").
Instantiate(ctx)
if err != nil {
return nil, err
}
defer env.Close(ctx)
code, err := r.CompileModule(ctx, bin)
if err != nil {
return nil, err
}
mod, err := r.InstantiateModule(ctx, code, wazero.NewModuleConfig())
if err != nil {
return nil, err
}
defer mod.Close(ctx)
t0 := time.Now()
if _, err = mod.ExportedFunction("h").Call(ctx); err != nil {
return nil, err
}
runTime := time.Since(t0)
return &ExecResult{
Output: string(p.Output),
ExecTime: runTime,
}, nil
}
type ExecResult struct {
Output string `json:"out"`
ExecTime time.Duration `json:"exec_duration"`
}
|