-
Notifications
You must be signed in to change notification settings - Fork 1
/
emception.js
164 lines (142 loc) · 5.55 KB
/
emception.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
import FileSystem from "emception/FileSystem.mjs";
import LlvmBoxProcess from "emception/LlvmBoxProcess.mjs";
import BinaryenBoxProcess from "emception/BinaryenBoxProcess.mjs";
import Python3Process from "emception/Python3Process.mjs";
import NodeProcess from "emception/QuickNodeProcess.mjs";
import packs from "emception/packs.mjs";
const tools_info = {
"/usr/bin/clang": "llvm-box",
"/usr/bin/clang++": "llvm-box",
"/usr/bin/llc": "llvm-box",
"/usr/bin/lld": "llvm-box",
"/usr/bin/llvm-ar": "llvm-box",
"/usr/bin/llvm-nm": "llvm-box",
"/usr/bin/llvm-objcopy": "llvm-box",
"/usr/bin/wasm-ld": "llvm-box",
"/usr/bin/node": "node",
"/usr/bin/python": "python",
"/usr/bin/wasm-as": "binaryen-box",
"/usr/bin/wasm-ctor-eval": "binaryen-box",
"/usr/bin/wasm-emscripten-finalize": "binaryen-box",
"/usr/bin/wasm-metadce": "binaryen-box",
"/usr/bin/wasm-opt": "binaryen-box",
"/usr/bin/wasm-shell": "binaryen-box",
};
// packages needed for the startup example
const preloads = [
"cpython",
"emscripten",
"emscripten_node_modules",
"emscripten_sysroot_lib_wasm32-emscripten_libGL.a",
"emscripten_sysroot_lib_wasm32-emscripten_libal.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc++.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc++abi.a",
"emscripten_sysroot_lib_wasm32-emscripten_libc.a",
"emscripten_sysroot_lib_wasm32-emscripten_libcompiler_rt.a",
"emscripten_sysroot_lib_wasm32-emscripten_libdlmalloc.a",
"emscripten_sysroot_lib_wasm32-emscripten_libhtml5.a",
"emscripten_sysroot_lib_wasm32-emscripten_libsockets.a",
"emscripten_sysroot_lib_wasm32-emscripten_libstubs.a",
"emscripten_system_include",
"emscripten_system_include_SDL",
"emscripten_system_include_compat",
"emscripten_system_lib_compiler-rt_lib",
"emscripten_system_lib_libcxx_include",
"emscripten_third_party",
"wasm"
];
class Emception {
fileSystem = null;
tools = {};
async init() {
const fileSystem = await new FileSystem();
this.fileSystem = fileSystem;
fileSystem.mkdirTree("/lazy");
for (const [name, url] of Object.entries(packs)) {
fileSystem.cachedLazyFolder(`/lazy/${name}`, url, 0o777, `/lazy/${name}`);
}
fileSystem.mkdirTree("/usr/local");
fileSystem.symlink("/lazy/emscripten", "/emscripten");
fileSystem.symlink("/lazy/cpython", "/usr/local/lib");
fileSystem.symlink("/lazy/wasm", "/wasm");
await Promise.all(preloads.map((preload) => fileSystem.preloadLazy(`/lazy/${preload}`)));
fileSystem.mkdirTree("/working");
fileSystem.mkdirTree("/usr/bin");
for (const tool of Object.keys(tools_info)) {
// Emscripten checks the existence of a few of these files
fileSystem.writeFile(tool, "");
}
const processConfig = {
FS: fileSystem.FS,
onrunprocess: (...args) => this._run_process(...args),
};
const tools = {
"llvm-box": new LlvmBoxProcess(processConfig),
"binaryen-box": new BinaryenBoxProcess(processConfig),
"node": new NodeProcess(processConfig),
"python": [
new Python3Process(processConfig),
new Python3Process(processConfig),
new Python3Process(processConfig),
],
};
this.tools = tools;
for (let tool in tools) {
tools[tool] = await Promise.all([].concat(tools[tool]));
}
}
onprocessstart = () => {};
onprocessend = () => {};
onstdout = () => {};
onstderr = () => {};
run(...args) {
if (this.fileSystem.exists("/emscripten/cache/cache.lock")) {
this.fileSystem.unlink("/emscripten/cache/cache.lock");
}
if (args.length == 1) args = args[0].split(/ +/);
return this._run_process_impl([
`/emscripten/${args[0]}.py`,
...args.slice(1)
], {
print: (...args) => this.onstdout(...args),
printErr: (...args) => this.onstderr(...args),
cwd: "/working",
path: ["/emscripten"],
});
};
_run_process(argv, opts = {}) {
this.onprocessstart(argv);
const result = this._run_process_impl(argv, opts);
this.onprocessend(result);
return result;
}
_run_process_impl(argv, opts = {}) {
const emscripten_script = argv[0].match(/^((\/lazy)?\/emscripten\/.+?)(?:\.py)?$/)?.[1]
if (emscripten_script && this.fileSystem.exists(`${emscripten_script}.py`)) {
argv = [
"/usr/bin/python",
"-E",
`${emscripten_script}.py`,
...argv.slice(1)
];
}
const tool_name = tools_info[argv[0]];
const tool = this.tools[tool_name]?.find(p => !p.running);
if (!tool) {
const result = {
returncode: 1,
stdout: "",
stderr: `Emception tool not found: ${JSON.stringify(argv[0])}`,
};
return result;
}
const result = tool.exec(argv, {
...opts,
cwd: opts.cwd || "/",
path: ["/emscripten"]
});
this.fileSystem.push();
return result;
};
}
export default Emception;