-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
91 lines (76 loc) · 2.33 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
package main
import (
"context"
"crypto/rand"
"embed"
_ "embed"
"fmt"
"log"
"os"
"path/filepath"
"runtime"
"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/gs.wasm
var gsWasm []byte
//go:embed ghostscript
var sharedFiles embed.FS
func main() {
ctx := context.WithValue(context.Background(), experimental.FunctionListenerFactoryKey{}, logging.NewLoggingListenerFactory(os.Stdout))
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)
}
compiledModule, err := wazeroRuntime.CompileModule(ctx, gsWasm)
if err != nil {
log.Fatal(err)
}
if _, err := emscripten.InstantiateForModule(ctx, wazeroRuntime, compiledModule); err != nil {
log.Fatal(err)
}
fsConfig := wazero.NewFSConfig()
fsConfig = fsConfig.WithFSMount(sharedFiles, "/ghostscript")
// 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("/", "/")
}
args := []string{"gs"}
args = append(args, os.Args[1:]...)
moduleConfig := wazero.NewModuleConfig().
WithStartFunctions("_start").
WithStdout(os.Stdout).
WithStderr(os.Stderr).
WithStdin(os.Stdin).
WithRandSource(rand.Reader).
WithFSConfig(fsConfig).
WithName("").
WithArgs(args...).
WithSysWalltime(). // Real clocks is needed to generate correct (non-duplicate) tmp file names.
WithSysNanotime().
WithSysNanosleep()
_, err = wazeroRuntime.InstantiateModule(ctx, compiledModule, moduleConfig)
if err != nil {
log.Fatal(err)
}
}