-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathshell.html
More file actions
executable file
·273 lines (250 loc) · 7.25 KB
/
Copy pathshell.html
File metadata and controls
executable file
·273 lines (250 loc) · 7.25 KB
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 lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Nari Interpreter</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
margin: 0;
padding: 20px;
background: #1e1e1e;
color: #d4d4d4;
}
h1 {
color: #4ec9b0;
margin-bottom: 10px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.panel {
background: #252526;
border: 1px solid #3c3c3c;
border-radius: 4px;
padding: 15px;
margin-bottom: 15px;
}
textarea, pre {
font-family: 'Consolas', 'Monaco', monospace;
font-size: 14px;
width: 100%;
box-sizing: border-box;
}
#code-input {
background: #1e1e1e;
color: #d4d4d4;
border: 1px solid #3c3c3c;
padding: 10px;
min-height: 200px;
resize: vertical;
border-radius: 4px;
}
#output {
background: #1e1e1e;
color: #cccccc;
padding: 10px;
min-height: 300px;
max-height: 500px;
overflow-y: auto;
border: 1px solid #3c3c3c;
border-radius: 4px;
white-space: pre-wrap;
word-wrap: break-word;
}
button {
background: #0e639c;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 14px;
margin-right: 10px;
}
button:hover {
background: #1177bb;
}
button:disabled {
background: #3c3c3c;
cursor: not-allowed;
}
.status {
padding: 8px 12px;
border-radius: 4px;
display: inline-block;
margin-bottom: 10px;
}
.status.loading { background: #1e3a5f; color: #4ec9b0; }
.status.ready { background: #1e3a1e; color: #4ec9b0; }
.status.running { background: #3a3a1e; color: #dcdcaa; }
.status.error { background: #4a1e1e; color: #f48771; }
#stop-btn {
background: #8b1a1a;
display: none;
}
#stop-btn:hover { background: #b22222; }
#stop-btn.visible { display: inline-block; }
label {
display: block;
margin-bottom: 8px;
color: #4ec9b0;
font-weight: 500;
}
</style>
</head>
<body>
<div class="container">
<h1>Nari Interpreter</h1>
<div id="status" class="status loading">Loading interpreter...</div>
<div class="panel">
<label for="code-input">Nari Code:</label>
<textarea id="code-input" placeholder='print("Hello from Nari in the browser!");'>print("Hello from Nari in the browser!");</textarea>
<div style="margin-top: 10px;">
<button id="run-btn" onclick="runCode()" disabled>Run Code</button>
<button id="stop-btn" onclick="stopCode()">Stop</button>
<button onclick="clearOutput()">Clear Output</button>
</div>
</div>
<div class="panel">
<label>Output:</label>
<pre id="output">Waiting for interpreter to load...</pre>
</div>
</div>
<script>
const outputElem = document.getElementById('output');
const statusElem = document.getElementById('status');
const runBtn = document.getElementById('run-btn');
const stopBtn = document.getElementById('stop-btn');
// URL of the emscripten JS glue, resolved relative to this page.
const interpreterJsUrl = new URL('nari.js', location.href).href;
// Main-page Module: loads the wasm silently just to show "Interpreter ready".
// All user code execution happens in a separate Worker so the main thread
// stays responsive and long-running programs can be cancelled.
var Module = {
noInitialRun: true,
print: function() {},
printErr: function() {},
onRuntimeInitialized: function() {
updateStatus('Interpreter ready!', 'ready');
runBtn.disabled = false;
clearOutput();
outputElem.textContent = 'Nari interpreter loaded.\nReady to run code.\n';
},
onAbort: function(what) {
updateStatus('Failed to load interpreter', 'error');
outputElem.textContent = 'Failed to load: ' + what + '\n';
}
};
function updateStatus(message, type) {
statusElem.textContent = message;
statusElem.className = 'status ' + type;
}
function appendOutput(text) {
outputElem.textContent += text;
outputElem.scrollTop = outputElem.scrollHeight;
}
function clearOutput() {
outputElem.textContent = '';
}
// worker management
let activeWorker = null;
// Inline Worker source. The main thread posts:
// { type: 'run', code, scriptUrl }
// The worker replies with:
// { type: 'print'|'printErr', text }
// { type: 'done', exitCode }
// { type: 'error', message }
const workerSource = `
self.onmessage = function(e) {
if (e.data.type !== 'run') return;
let code = e.data.code;
let scriptUrl = e.data.scriptUrl;
// Assign to global scope BEFORE importScripts so Emscripten picks it up.
self.Module = {
noInitialRun: true,
print: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
self.postMessage({ type: 'print', text: text + '\\n' });
},
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
self.postMessage({ type: 'printErr', text: text + '\\n' });
},
// Resolve .wasm relative to nari.js (not the blob worker URL).
locateFile: function(path) {
return new URL(path, scriptUrl).href;
},
onRuntimeInitialized: function() {
try { Module.FS.mkdir('/tmp'); } catch(_) {}
try {
Module.FS.writeFile('/tmp/code.nari', code);
let result = Module.callMain(['/tmp/code.nari']);
self.postMessage({ type: 'done', exitCode: result || 0 });
} catch(ex) {
self.postMessage({ type: 'error', message: ex.message });
}
},
onAbort: function(what) {
self.postMessage({ type: 'error', message: 'Aborted: ' + what });
}
};
importScripts(scriptUrl);
};
`;
function runCode() {
const code = document.getElementById('code-input').value;
if (!code.trim()) { appendOutput('No code to run.\n'); return; }
clearOutput();
appendOutput('Running...\n\n');
updateStatus('Running...', 'running');
runBtn.disabled = true;
stopBtn.classList.add('visible');
const blob = new Blob([workerSource], { type: 'application/javascript' });
const blobUrl = URL.createObjectURL(blob);
activeWorker = new Worker(blobUrl);
URL.revokeObjectURL(blobUrl);
activeWorker.onmessage = function(e) {
const msg = e.data;
if (msg.type === 'print' || msg.type === 'printErr') {
appendOutput(msg.text);
} else if (msg.type === 'done') {
if (msg.exitCode !== 0)
appendOutput('\nProcess exited with code: ' + msg.exitCode + '\n');
finishRun();
} else if (msg.type === 'error') {
appendOutput('\nError: ' + msg.message + '\n');
finishRun();
}
};
activeWorker.onerror = function(err) {
appendOutput('\nWorker error: ' + err.message + '\n');
finishRun();
};
activeWorker.postMessage({ type: 'run', code: code, scriptUrl: interpreterJsUrl });
}
function stopCode() {
if (activeWorker) {
activeWorker.terminate();
activeWorker = null;
appendOutput('\n[Execution stopped]\n');
}
finishRun();
}
function finishRun() {
if (activeWorker) { activeWorker.terminate(); activeWorker = null; }
runBtn.disabled = false;
stopBtn.classList.remove('visible');
updateStatus('Interpreter ready!', 'ready');
}
document.getElementById('code-input').addEventListener('keydown', function(e) {
if (e.ctrlKey && e.key === 'Enter') runCode();
});
</script>
<!-- Loads nari.js on the main thread for the initial "ready" signal only.
Each user code run happens inside a fresh Worker; the main thread never blocks. -->
{{{ SCRIPT }}}
</body>
</html>