-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexec_wazero.go
More file actions
61 lines (54 loc) · 1.46 KB
/
exec_wazero.go
File metadata and controls
61 lines (54 loc) · 1.46 KB
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
//go:build wazero
package main
import (
"context"
"io"
"log/slog"
"os"
"path/filepath"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
// WazeroRunner implements CGI Runner execute by wasmer
type WazeroRunner struct {
}
func (runner WazeroRunner) Run(conf SrvConfig, cmdname string, envvar map[string]string,
stdin io.ReadCloser, stdout io.Writer, stderr io.Writer,
ctx context.Context) error {
fn := filepath.Join(conf.BaseDir, cmdname)
bytecode, err := os.ReadFile(fn)
if err != nil {
slog.Error("read bytecode", "error", err, "filename", fn)
return err
}
slog.Debug("bytecode read", "length", len(bytecode), "filename", fn)
rt := wazero.NewRuntime(ctx)
defer rt.Close(ctx)
wconf := wazero.NewModuleConfig().
WithStdout(stdout).
WithStderr(stderr).
WithStdin(stdin).
WithStartFunctions("_start")
for k, v := range envvar {
wconf = wconf.WithEnv(k, v)
}
code, err := rt.CompileModule(ctx, bytecode)
if err != nil {
slog.Error("compile", "error", err)
return err
}
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
_, err = rt.InstantiateModule(ctx, code, wconf)
if err != nil {
slog.Error("instantiate", "error", err)
}
return err
}
func (runner WazeroRunner) Exists(conf SrvConfig, path string, ctx context.Context) (string, string, error) {
return splitPathInfo(conf.BaseDir, path, conf.Suffix)
}
func init() {
runnerMap["wazero"] = func(SrvConfig) Runner {
return WazeroRunner{}
}
}