-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-node
executable file
·60 lines (46 loc) · 1.28 KB
/
run-node
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
#!/usr/bin/env -S node --experimental-wasi-unstable-preview1 --no-warnings
const { spawnSync } = require("child_process");
const { readFileSync } = require("fs");
const { WASI } = require("wasi");
if (process.argv[2] == "-V") {
const result = spawnSync(process.argv[0], ["--version"], {
stdio: "pipe",
encoding: "utf-8",
});
process.stdout.write(`node ${result.stdout.slice(1)}`);
process.exit(result.status);
}
const options = {
args: [],
env: {},
preopens: {},
};
const inputs = [];
const args = process.argv.slice(2);
while (args.length > 0) {
const arg = args.shift();
switch (arg) {
case "--dir":
const dir = args.shift();
options.preopens[dir] = dir;
break;
case "--env":
const [ key, value ] = args.shift().split("=");
options.env[key] = value;
break;
case "--":
options.args.push(...args.splice(0, args.length));
break;
default:
if (options.args.length == 0) {
options.args.push(arg);
}
inputs.push(arg);
break;
}
}
const wasi = new WASI(options);
const importObject = { wasi_snapshot_preview1: wasi.wasiImport };
const wasm = new WebAssembly.Module(readFileSync(inputs[0]));
const instance = new WebAssembly.Instance(wasm, importObject);
wasi.start(instance);