forked from jprendes/emception
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
273 lines (246 loc) · 8.38 KB
/
index.html
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<!doctype html><html>
<head><meta charset="utf-8"><title>Emception</title><meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f4f4f4;
color: #333;
line-height: 1.6;
padding: 20px;
margin: 0;
}
.container {
display: flex;
flex-direction: column;
gap: 10px;
}
h1 {
color: #333;
}
a {
color: #007bff;
}
#compile {
background-color: #007bff;
color: white;
padding: 10px 15px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 16px;
}
#compile:hover {
background-color: #0056b3;
}
textarea {
border: 1px solid #ccc;
border-radius: 4px;
padding: 10px;
font-family: monospace;
}
#source, #stdout, #expected {
width: 100%;
}
#source {
height: 300px;
}
#stdout, #expected {
background-color: #000;
color: #0f0;
height: 150px;
}
.input-group {
display: flex;
width: 100%;
}
#stdin, #expected {
flex: 1;
height: 150px;
}
.resizeNone {
resize: none;
}
</style>
<div class="container">
<h1>Emception</h1>
<p>Emception is a web-based C++ compiler and runtime environment.</p>
<p>It uses <a href="https://emscripten.org/">Emscripten</a> to compile C++ code to WebAssembly and runs it in a sandboxed environment.</p>
<p>It is a work in progress and not ready for production use.</p>
<p>Try it out by entering some C++ code below and clicking the "Compile" button.</p>
<button id="compile">Compile</button>
<textarea id="source" >
#include<iostream>
#include<string>
using namespace std;
int main(){
string name;
cin >> name;
cout << "Hello " << name << "!" << endl;
return 0;
}
</textarea>
<textarea id="stdout" class="resizeNone" disabled> </textarea>
<div class="input-group">
<textarea id="stdin">username</textarea>
<textarea id="expected" disabled >Hello username!</textarea>
</div>
</div>
<script>
// intercept stdin
window.prompt = function() {
return document.getElementById("stdin").value;
}
var stdoutElement = document.getElementById("stdout");
var stdinElement = document.getElementById("stdin");
var expectedElement = document.getElementById("expected");
var sourceElement = document.getElementById("source");
// prejs
// todo: improve and make it not search for the element every time
const prejs = `
var Module = {
print: (function() {
var element = stdoutElement;
if (element) element.value = ''; // clear browser cache
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
// todo: do replacements needed
console.log(text);
if (element) {
element.value += text + "";
element.scrollTop = element.scrollHeight; // focus on bottom
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) { // XXX disabled for safety typeof dump == 'function') {
dump(text + '\\n'); // fast, straight to the real console
} else {
console.error(text);
}
var element = stdoutElement;
if (element) {
element.value += text + "";
element.scrollTop = element.scrollHeight; // focus on bottom
}
}
};
`;
const onprocessstart = (argv) => {
console.log("onprocessstart", argv);
// stdoutElement.value += '# ' + argv.join(' ') + "\n";
};
const onprocessend = () => {
console.log("onprocessend");
// document.getElementById("stdout").value += "onprocessend" + "\n";
};
const onstdout = (str) => {
console.log(str);
stdoutElement.value += str + "\n";
stdoutElement.scrollTop = stdoutElement.scrollHeight; // focus on bottom
};
const onstderr = (str) =>{
console.error(str);
stdoutElement.value += str + "\n";
// stdoutElement.scrollTop = stdoutElement.scrollHeight; // focus on bottom
};
const EmceptionState = {
MainNotLoaded:0,
MainLoading:1,
MainLoaded:2,
EmceptionLoading:3,
EmceptionLoaded:4,
}
var loadingState = EmceptionState.MainNotLoaded;
var emception;
// todo: bug: this state machine is not running for the second time
async function preCompile(){
switch (loadingState) {
case EmceptionState.MainNotLoaded:
loadingState = EmceptionState.MainLoading;
var script = document.createElement('script');
script.src = 'main.js';
script.onload = ()=>{
onstdout("main.js loaded");
loadingState = EmceptionState.MainLoaded;
preCompile();
}
document.head.appendChild(script);
return false;
case EmceptionState.MainLoading:
onstdout("Main script is still loading. Please wait.");
return false
case EmceptionState.MainLoaded:
onstdout("initializing Emception...");
loadingState = EmceptionState.EmceptionLoading;
emception = new Emception.default();
emception.onstdout = onstdout;
emception.onstderr = onstderr;
emception.onprocessstart = onprocessstart;
emception.onprocessend = onprocessend;
await emception.init();
loadingState = EmceptionState.EmceptionLoaded;
onstdout("Emception initialized");
preCompile();
return false;
case EmceptionState.EmceptionLoading:
onstdout("Emception is still loading. Please wait.");
return false;
case EmceptionState.EmceptionLoaded:
await compile();
return true;
}
}
window.onerror = function(event) {
// TODO: do not warn on ok events like simulating an infinite loop or exitStatus
// Module.setStatus('Exception thrown, see JavaScript console');
//Module.setStatus = function(text) {
if (text) console.error('[post-exception status] ' + text);
//};
};
async function compile(){
stdoutElement.value = "";
const sourceValue = sourceElement.value;
console.log("Compiling C++ code...\n");
try {
await emception.fileSystem.writeFile("/working/main.cpp", sourceValue);
await emception.fileSystem.writeFile("/working/pre.js", prejs);
// todo: add prejs to emscripten command --pre-js pre.js
const cmd = `em++ -O2 -fexceptions --pre-js pre.js -sEXIT_RUNTIME=1 -sSINGLE_FILE=1 -sUSE_CLOSURE_COMPILER=0 main.cpp -o main.js`;
onprocessstart(`/emscripten/${cmd}`.split(/\s+/g));
onstdout(`# ${cmd}`);
// wait 100ms
await new Promise(resolve => setTimeout(resolve, 100));
const result = await emception.run(cmd);
if (result.returncode == 0) {
const content = new TextDecoder().decode(await emception.fileSystem.readFile("/working/main.js"));
onstdout(`# node ./main.js`);
await new Promise(resolve => setTimeout(resolve, 100));
eval(content);
//wait 100ms
await new Promise(resolve => setTimeout(resolve, 100));
// compare stdout with expected
const stdout = stdoutElement.value;
const expected = expectedElement.value;
console.log(`stdout: ${stdout}`);
console.log(`expected: ${expected}`);
if (stdout.replace(/\s/g, '') === expected.replace(/\s/g, ''))
console.log(`Test Passed`);
else
console.error(`Test failed`);
} else {
console.log(`Emception compilation failed`);
}
} catch (err) {
console.error(err);
} finally {
// add your finally code here
}
}
document.getElementById("compile").addEventListener("click", async function() {
await preCompile();
});
</script>
</body></html>