-
Notifications
You must be signed in to change notification settings - Fork 2
/
runner.go
56 lines (46 loc) · 1.41 KB
/
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
47
48
49
50
51
52
53
54
55
56
package runner
import (
"context"
"crypto/rand"
"log"
"os"
"strings"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/api"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
"github.com/tetratelabs/wazero/sys"
"github.com/wasilibs/wazero-helpers/allocator"
"github.com/wasilibs/go-protoc-gen-builtins/internal/wasm"
)
func Run(name string, wasmBin []byte) {
ctx := context.Background()
ctx = experimental.WithMemoryAllocator(ctx, allocator.NewNonMoving())
rt := wazero.NewRuntimeWithConfig(ctx, wazero.NewRuntimeConfig().WithCoreFeatures(api.CoreFeaturesV2|experimental.CoreFeaturesThreads))
wasi_snapshot_preview1.MustInstantiate(ctx, rt)
if _, err := rt.InstantiateWithConfig(ctx, wasm.Memory, wazero.NewModuleConfig().WithName("env")); err != nil {
log.Fatal(err)
}
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, wasmBin, cfg)
if err != nil {
if sErr, ok := err.(*sys.ExitError); ok { //nolint:errorlint
os.Exit(int(sErr.ExitCode()))
}
log.Fatal(err)
}
}