-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
127 lines (104 loc) · 3.04 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"crypto/rand"
"embed"
_ "embed"
"fmt"
"io/fs"
"log"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/tetratelabs/wazero"
"github.com/tetratelabs/wazero/experimental"
"github.com/tetratelabs/wazero/experimental/logging"
"github.com/tetratelabs/wazero/imports/emscripten"
"github.com/tetratelabs/wazero/imports/wasi_snapshot_preview1"
)
//go:embed wasm/*
var wasmBinaries embed.FS
//go:embed fonts/*
var fontFiles embed.FS
func main() {
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewHostLoggingListenerFactory(os.Stdout, logging.LogScopeFilesystem))
ctx = context.Background() // Comment this line to get debug information.
runtimeConfig := wazero.NewRuntimeConfig()
cache, err := wazero.NewCompilationCacheWithDir(".wazero-cache")
if err == nil {
runtimeConfig = runtimeConfig.WithCompilationCache(cache)
}
wazeroRuntime := wazero.NewRuntimeWithConfig(ctx, runtimeConfig)
defer wazeroRuntime.Close(ctx)
if _, err := wasi_snapshot_preview1.Instantiate(ctx, wazeroRuntime); err != nil {
log.Fatal(err)
}
availableWASMFiles, err := wasmBinaries.ReadDir("wasm")
if err != nil {
log.Fatal(err)
}
availableBinaries := make([]string, len(availableWASMFiles))
for i := range availableWASMFiles {
availableBinaries[i] = strings.TrimSuffix(availableWASMFiles[i].Name(), ".wasm")
}
incorrectStartArgument := func() {
log.Fatalf("You should minimally start the program with one of the following arguments: %s", strings.Join(availableBinaries, ", "))
}
if len(os.Args) < 2 {
incorrectStartArgument()
}
var wasmData []byte
program := os.Args[1]
for i := range availableBinaries {
if availableBinaries[i] == program {
var err error
wasmData, err = wasmBinaries.ReadFile(fmt.Sprintf("wasm/%s.wasm", program))
if err != nil {
log.Fatal(err)
}
}
}
if wasmData == nil {
incorrectStartArgument()
}
compiledModule, err := wazeroRuntime.CompileModule(ctx, wasmData)
if err != nil {
log.Fatal(err)
}
if _, err := emscripten.InstantiateForModule(ctx, wazeroRuntime, compiledModule); err != nil {
log.Fatal(err)
}
fsConfig := wazero.NewFSConfig()
fontsSub, err := fs.Sub(fontFiles, "fonts")
if err != nil {
log.Fatal(err)
}
fsConfig = fsConfig.WithFSMount(fontsSub, "/fonts")
// On Windows we mount the volume of the current working directory as
// root. On Linux we mount / as root.
if runtime.GOOS == "windows" {
cwdDir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
volumeName := filepath.VolumeName(cwdDir)
if volumeName != "" {
fsConfig = fsConfig.WithDirMount(fmt.Sprintf("%s\\", volumeName), "/")
}
} else {
fsConfig = fsConfig.WithDirMount("/", "/")
}
moduleConfig := wazero.NewModuleConfig().
WithStartFunctions("_start").
WithStdout(os.Stdout).
WithStderr(os.Stderr).
WithRandSource(rand.Reader).
WithFSConfig(fsConfig).
WithName("").
WithArgs(os.Args[1:]...)
_, err = wazeroRuntime.InstantiateModule(ctx, compiledModule, moduleConfig)
if err != nil {
log.Fatal(err)
}
}