-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
198 lines (172 loc) · 6.47 KB
/
index.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
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import { Terminal } from "xterm";
import { FitAddon } from 'xterm-addon-fit';
import Split from "split-grid";
import { spinner, previewTemplate, miniBrowserTemplate } from "./preview-template.mjs";
import { render, html } from "lit";
import * as Comlink from "comlink";
import EmceptionWorker from "./emception.worker.js";
import "./style.css";
import "xterm/css/xterm.css";
const emception = Comlink.wrap(new EmceptionWorker());
window.emception = emception;
window.Comlink = Comlink;
const editorContainer = document.createElement("div");
const editor = monaco.editor.create(editorContainer, {
value: "",
language: "cpp",
theme: "vs-dark",
});
const terminalContainer = document.createElement("div");
const terminal = new Terminal({
convertEol: true,
theme: {
background: "#1e1e1e",
foreground: "#d4d4d4",
},
});
terminal.open(terminalContainer);
const terminalFitAddon = new FitAddon();
terminal.loadAddon(terminalFitAddon);
window.editor = editor;
window.terminal = terminal;
editor.setValue(`#include <iostream>
int main(void) {
std::cout << "hello world!\\n";
return 0;
}
`);
emception.onstdout = Comlink.proxy((str) => terminal.write(str + "\n"));
emception.onstderr = Comlink.proxy((str) => terminal.write(str + "\n"));
window.addEventListener("resize", () => {
editor.layout();
terminalFitAddon.fit();
});
async function main() {
render(html`
<div id="layout">
<div id="header">
<div id="title">Emception</div>
<input id="flags" type="text"></input>
<button disabled id="compile">Loading</button>
</div>
<div id="editor">${editorContainer}</div>
<div id="vgutter"></div>
<div id="preview">
<iframe id="preview-frame"></iframe>
</div>
<div id="hgutter"></div>
<div id="output">
<div id="terminal">
${terminalContainer}
</div>
<div id="status"></div>
</div>
</div>
`, document.body);
const flags = document.getElementById("flags");
flags.value = "-O2 -fexceptions --proxy-to-worker -sEXIT_RUNTIME=1";
window.split = Split({
onDrag: () => {
editor.layout();
terminalFitAddon.fit();
},
columnGutters: [{
track: 1,
element: document.getElementById("vgutter"),
}],
rowGutters: [{
track: 2,
element: document.getElementById("hgutter"),
}],
});
const frame = document.getElementById("preview-frame");
let url = "";
function preview(html_content) {
if (url) URL.revokeObjectURL(url);
url = URL.createObjectURL(new Blob([html_content], { type: 'text/html' }));
frame.src = url;
}
let miniUrl = "";
function previewMiniBrowser(html_content) {
if (miniUrl) URL.revokeObjectURL(miniUrl);
miniUrl = URL.createObjectURL(new Blob([html_content], { type: 'text/html' }));
preview(miniBrowserTemplate("main.html", miniUrl));
}
preview(previewTemplate(spinner(80), "Loading", ""));
const status = document.getElementById("status");
const statusElements = [];
const onprocessstart = (argv) => {
const lastEl = statusElements[statusElements.length - 1] || status;
const newEl = document.createElement("div");
newEl.className = "process-status";
render(html`
<div class="process-argv" title=${argv.join(" ")}>${argv.join(" ")}</div>
`, newEl);
statusElements.push(newEl);
lastEl.appendChild(newEl);
terminalFitAddon.fit();
requestAnimationFrame(() => {
terminalFitAddon.fit();
});
};
const onprocessend = () => {
const lastEl = statusElements.pop();
if (lastEl) lastEl.remove();
terminalFitAddon.fit();
requestAnimationFrame(() => {
terminalFitAddon.fit();
});
};
emception.onprocessstart = Comlink.proxy(onprocessstart);
emception.onprocessend = Comlink.proxy(onprocessend);
const compile = document.getElementById("compile");
compile.addEventListener("click", async () => {
compile.disabled = true;
compile.textContent = "Compiling";
status.textContent = "Running:";
preview(previewTemplate(spinner(80), "Compiling", ""));
try {
terminal.reset();
await emception.fileSystem.writeFile("/working/main.cpp", editor.getValue());
const cmd = `em++ ${flags.value} -sSINGLE_FILE=1 -sMINIFY_HTML=0 -sUSE_CLOSURE_COMPILER=0 main.cpp -o main.html`;
onprocessstart(`/emscripten/${cmd}`.split(/\s+/g));
terminal.write(`$ ${cmd}\n\n`);
const result = await emception.run(cmd);
terminal.write("\n");
if (result.returncode == 0) {
terminal.write("Emception compilation finished");
const content = await emception.fileSystem.readFile("/working/main.html", { encoding: "utf8" });
previewMiniBrowser(content);
} else {
terminal.write(`Emception compilation failed`);
preview(previewTemplate("", "", "The compilation failed, check the output bellow"));
}
terminal.write("\n");
} catch (err) {
preview(previewTemplate("", "", "Something went wrong, please file a bug report"));
console.error(err);
} finally {
status.textContent = "Iddle";
statusElements.splice(0, statusElements.length);
compile.textContent = "Compile!";
compile.disabled = false;
}
});
requestAnimationFrame(() => {
requestAnimationFrame(() => {
editor.layout();
terminalFitAddon.fit();
});
});
terminal.write("Loading Emception...\n");
status.textContent = "Loading...";
await emception.init();
terminal.reset();
terminal.write("Emception is ready\n");
status.textContent = "Iddle";
compile.disabled = false;
compile.textContent = "Compile!";
preview(previewTemplate("", "", "<div>Your compiled code will run here.</div><div>Click <div style=\"display: inline-block;border: 1px solid #858585;background: #454545;color: #cfcfcf;font-size: 15px;padding: 5px 10px;border-radius: 3px;\">Compile!</div> above to start.</div>"));
}
main();