-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathWasmerRunnable.js
256 lines (219 loc) · 6.82 KB
/
WasmerRunnable.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { WASI } from "@wasmer/wasi"
import browserBindings from "@wasmer/wasi/lib/bindings/browser"
import { WasmFs } from "@wasmer/wasmfs"
const S_IFCHR = 8192 // magic constant from memFS
class WasmerRunnable {
/* method wrapper class for wasmer wasm modules.
initializes memory filesystem and can execute wasm. */
programName
wasmModule
constructor(programName, wasmModule) {
this.programName = programName
this.wasmModule = wasmModule
}
run(
argv,
stdin,
stdout,
stderr,
files,
onFinish,
onError,
onSuccess,
stdinPreset
) {
console.log("wasmer runnable run:", this.programName, argv)
// initialize default methods and values
if (typeof stdin != "function") stdin = () => {}
if (typeof stdout != "function") stdout = () => {}
if (typeof stderr != "function") stderr = () => {}
if (!(files instanceof Array)) files = []
// initialize default callbacks
if (typeof onFinish != "function") onFinish = () => {}
if (typeof onError != "function")
onError = (e) => {
console.error(e)
}
if (typeof onSuccess != "function") onSuccess = () => {}
// init new memory filesystem
const wasmFs = new WasmFs()
// write all files to wasmfs
this._writeFilesToFS(wasmFs, files)
// set /dev/stdin to stdin function
wasmFs.volume.fds[0].node.read = stdin
if (stdinPreset) {
// with stdinPreset you can preset a value for stdin
if (typeof stdinPreset != "string")
stdinPreset = (stdinPreset || "").toString()
let stdinCallCounter = 0
wasmFs.volume.fds[0].node.read = (stdinBuffer) => {
// second read means end of string
if (stdinCallCounter % 2 !== 0) {
stdinCallCounter++
return 0
}
// copy stdin preset to stdinBuffer
for (let i = 0; i < stdinPreset.length; i++)
stdinBuffer[i] = stdinPreset.charCodeAt(i)
// indicate we've read once
stdinCallCounter++
// return how much to read
return stdinPreset.length
}
}
// set /dev/stdout to stdout function
wasmFs.volume.fds[1].node.write = (
stdoutBuffer,
offset,
length,
position
) => {
stdout(new TextDecoder("utf-8").decode(stdoutBuffer))
return stdoutBuffer.length
}
// set /dev/stderr to stderr function
wasmFs.volume.fds[2].node.write = (
stderrBuffer,
offset,
length,
position
) => {
stderr(new TextDecoder("utf-8").decode(stderrBuffer))
return stderrBuffer.length
}
// map /dev/tty to /dev/stdin and /dev/stdout
const ttyFd = wasmFs.volume.openSync("/dev/tty", "w+")
wasmFs.volume.fds[ttyFd].node.read = wasmFs.volume.fds[0].node.read
wasmFs.volume.fds[ttyFd].node.write = wasmFs.volume.fds[1].node.write
// mark /dev/{stdin,stdout,stderr,tty} as character devices
wasmFs.volume.fds[0].node.setModeProperty(S_IFCHR)
wasmFs.volume.fds[1].node.setModeProperty(S_IFCHR)
wasmFs.volume.fds[2].node.setModeProperty(S_IFCHR)
wasmFs.volume.fds[ttyFd].node.setModeProperty(S_IFCHR)
// create wasi runtime
let wasi = new WASI({
args: [this.programName, ...argv],
env: {}, // todo: maybe use environment variables?
bindings: {
...browserBindings,
fs: wasmFs.fs,
},
preopens: {
".": ".",
"/": "/",
},
})
// instantiate wasm module
const imports = wasi.getImports(this.wasmModule) // WebAssembly.Module.imports(this.wasmModule)
WebAssembly.instantiate(this.wasmModule, { ...imports }).then(
(instance) => {
let filesPostRun
try {
// write submitted files to wasm
this._writeFilesToFS(wasmFs, files)
// execute command
try {
wasi.start(instance)
} catch (e) {
// make browserBindings not throw on code 0 (normal exit)
if (e.code != 0) browserBindings.exit(e.code)
}
// read created files from wasm
filesPostRun = this._readFilesFromFS(wasmFs)
// success callback
onSuccess(filesPostRun)
} catch (e) {
onError(e.message)
} finally {
onFinish(filesPostRun || files)
}
}
)
}
runHeadless(argv, files, onFinish, onError, onSuccess, stdinPreset) {
console.log("wasmer runnable run headless:", this.programName, argv)
// initialize default callback
if (typeof onFinish != "function") onFinish = () => {}
// stdin is not needed
const stdin = () => {
console.log("called runHeadless stdin")
return 0
}
// output is redirected into buffer
let outputBuffer = "",
stdoutBuffer = "",
stderrBuffer = ""
const stdout = (stdoutVal) => {
outputBuffer += stdoutVal
stdoutBuffer += stdoutVal
return stdoutVal.length
}
const stderr = (stderrVal) => {
outputBuffer += stderrVal
stderrBuffer += stderrVal
return stderrVal.length
}
// run command with custom input/output
this.run(
argv,
stdin,
stdout,
stderr,
files,
() =>
onFinish({
output: outputBuffer,
stdout: stdoutBuffer,
stderr: stderrBuffer,
}),
onError,
onSuccess,
stdinPreset
)
}
/* file handling */
_readFilesFromFS(wasmFs, directory = "/", includeBinary = true) {
const rootLink = wasmFs.volume.getLinkAsDirOrThrow(directory)
const getFilesFromLink = (parentLink) => {
let files = []
Object.values(parentLink.children).forEach((link) => {
let linkPath = link.getPath()
let node = link.getNode()
if (node.isFile())
files.push({
name: linkPath,
timestamp: node.mtime.getTime(),
bytes: includeBinary
? wasmFs.fs.readFileSync(linkPath)
: new Uint8Array(),
})
if (node.isDirectory()) files = [...files, ...getFilesFromLink(link)]
})
return files
}
let files = getFilesFromLink(rootLink)
return files
}
_writeFilesToFS(wasmFs, files = []) {
files.forEach((file) => {
try {
if (file.bytes instanceof Uint8Array) {
const path = file.name.split("/").slice(0, -1).join("/")
wasmFs.fs.mkdirSync(path, { recursive: true })
wasmFs.fs.writeFileSync(file.name, file.bytes)
if (file.timestamp instanceof Date)
wasmFs.fs.utimesSync(file.name, file.timestamp, file.timestamp)
else if (typeof file.timestamp === "number")
wasmFs.fs.utimesSync(
file.name,
file.timestamp / 1000,
file.timestamp / 1000
)
}
} catch (e) {
console.error(e.name + ": " + e.message)
}
})
}
}
export default WasmerRunnable