-
Notifications
You must be signed in to change notification settings - Fork 1
/
runner.go
46 lines (38 loc) · 918 Bytes
/
runner.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
package runner
import (
"context"
"crypto/rand"
"log"
"os"
"strings"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/sys"
)
func Run(name string, wasm []byte) {
ctx := context.Background()
rt := wazero.NewRuntime(ctx)
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
args := []string{name}
args = append(args, os.Args[1:]...)
cfg := wazero.NewModuleConfig().
WithSysNanosleep().
WithSysNanotime().
WithSysWalltime().
WithStderr(os.Stderr).
WithStdout(os.Stdout).
WithStdin(os.Stdin).
WithRandSource(rand.Reader).
WithArgs(args...)
for _, env := range os.Environ() {
k, v, _ := strings.Cut(env, "=")
cfg = cfg.WithEnv(k, v)
}
_, err := rt.InstantiateWithConfig(ctx, wasm, cfg)
if err != nil {
if sErr, ok := err.(*sys.ExitError); ok {
os.Exit(int(sErr.ExitCode()))
}
log.Fatal(err)
}
}