-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathquickjs.html
More file actions
633 lines (575 loc) · 20.3 KB
/
quickjs.html
File metadata and controls
633 lines (575 loc) · 20.3 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>QuickJS Code Executor</title>
<style>
* {
box-sizing: border-box;
}
body {
font-family: system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
max-width: 900px;
margin: 0 auto;
padding: 20px;
line-height: 1.6;
}
h1 {
margin-bottom: 10px;
font-size: 28px;
}
.description {
margin-top: 0;
color: #666;
margin-bottom: 20px;
}
label {
display: block;
margin-bottom: 8px;
font-weight: 600;
color: #333;
}
textarea {
width: 100%;
padding: 12px;
border: 1px solid #ccc;
border-radius: 4px;
font-size: 16px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
min-height: 200px;
resize: vertical;
tab-size: 2;
}
textarea:focus {
outline: none;
border-color: #007bff;
box-shadow: 0 0 0 2px rgba(0, 123, 255, 0.25);
}
.input-group {
margin-bottom: 20px;
}
.button-group {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
button {
padding: 12px 24px;
cursor: pointer;
background-color: #007bff;
color: white;
border: none;
border-radius: 4px;
font-size: 16px;
font-weight: 500;
transition: background-color 0.2s;
}
button:hover:not(:disabled) {
background-color: #0056b3;
}
button:disabled {
background-color: #6c757d;
cursor: wait;
opacity: 0.7;
}
button.secondary {
background-color: #6c757d;
}
button.secondary:hover:not(:disabled) {
background-color: #545b62;
}
#status {
padding: 12px;
margin-bottom: 20px;
border-radius: 4px;
display: none;
}
#status.visible {
display: block;
}
#status.info {
background-color: #d1ecf1;
color: #0c5460;
border: 1px solid #bee5eb;
}
#status.error {
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
#status.success {
background-color: #d4edda;
color: #155724;
border: 1px solid #c3e6cb;
}
#output-section {
display: none;
}
#output-section.visible {
display: block;
}
.output-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.output-header h2 {
margin: 0;
font-size: 20px;
}
#copy-btn {
padding: 6px 12px;
font-size: 14px;
}
#output {
background-color: #1e1e1e;
color: #d4d4d4;
padding: 16px;
border-radius: 4px;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
font-size: 14px;
white-space: pre-wrap;
word-wrap: break-word;
min-height: 100px;
max-height: 500px;
overflow-y: auto;
}
#output.error-output {
background-color: #2d1f1f;
color: #f8d7da;
}
.execution-time {
font-size: 12px;
color: #666;
margin-top: 8px;
}
.examples-section {
margin-bottom: 20px;
}
.examples-section h3 {
font-size: 14px;
color: #666;
margin: 0 0 10px 0;
font-weight: 500;
}
.examples-list {
display: flex;
flex-wrap: wrap;
gap: 8px;
}
.example-btn {
padding: 6px 12px;
font-size: 13px;
background: #f8f9fa;
border: 1px solid #dee2e6;
border-radius: 20px;
color: #495057;
cursor: pointer;
transition: all 0.15s ease;
}
.example-btn:hover {
background: #e9ecef;
border-color: #adb5bd;
color: #212529;
}
.example-btn:active {
background: #dee2e6;
}
@media (max-width: 600px) {
body {
padding: 10px;
}
h1 {
font-size: 24px;
}
.button-group {
flex-direction: column;
}
.button-group button {
width: 100%;
}
}
</style>
</head>
<body>
<h1>QuickJS Code Executor</h1>
<p class="description">Execute JavaScript code in a sandboxed QuickJS environment running via WebAssembly. Code is saved in the URL for easy sharing. Includes a synchronous <code>fetch(url)</code> function that returns text content.</p>
<div class="examples-section">
<h3>Try an example:</h3>
<div class="examples-list" id="examples-list"></div>
</div>
<div class="input-group">
<label for="code-input">JavaScript Code:</label>
<textarea id="code-input" placeholder="// Enter your JavaScript code here, or click an example above"></textarea>
</div>
<div class="button-group">
<button id="run-btn" disabled>Initializing...</button>
<button id="clear-btn" class="secondary">Clear</button>
</div>
<div id="status"></div>
<div id="output-section">
<div class="output-header">
<h2>Output</h2>
<button id="copy-btn" class="secondary">Copy</button>
</div>
<div id="output"></div>
<div class="execution-time" id="execution-time"></div>
</div>
<!-- Load QuickJS as IIFE global build -->
<script src="https://cdn.jsdelivr.net/npm/quickjs-emscripten@0.31.0/dist/index.global.js"></script>
<script>
// Example code snippets
const examples = [
{
name: 'Hello World',
code: `console.log('Hello, World!');`
},
{
name: 'Factorial',
code: `function factorial(n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
console.log('5! =', factorial(5));
console.log('10! =', factorial(10));`
},
{
name: 'Fibonacci',
code: `function fibonacci(n) {
const seq = [0, 1];
for (let i = 2; i < n; i++) {
seq.push(seq[i-1] + seq[i-2]);
}
return seq;
}
console.log('First 15 Fibonacci numbers:');
console.log(fibonacci(15).join(', '));`
},
{
name: 'Array Methods',
code: `const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const doubled = numbers.map(n => n * 2);
console.log('Doubled:', doubled);
const evens = numbers.filter(n => n % 2 === 0);
console.log('Evens:', evens);
const sum = numbers.reduce((a, b) => a + b, 0);
console.log('Sum:', sum);`
},
{
name: 'Objects & JSON',
code: `const person = {
name: 'Alice',
age: 30,
hobbies: ['reading', 'coding', 'hiking'],
address: {
city: 'San Francisco',
country: 'USA'
}
};
console.log(JSON.stringify(person, null, 2));
console.log('\\nName:', person.name);
console.log('First hobby:', person.hobbies[0]);`
},
{
name: 'Prime Numbers',
code: `function isPrime(n) {
if (n < 2) return false;
for (let i = 2; i <= Math.sqrt(n); i++) {
if (n % i === 0) return false;
}
return true;
}
const primes = [];
for (let i = 2; i <= 100; i++) {
if (isPrime(i)) primes.push(i);
}
console.log('Primes up to 100:');
console.log(primes.join(', '));`
},
{
name: 'String Fun',
code: `const text = 'JavaScript is awesome!';
console.log('Original:', text);
console.log('Uppercase:', text.toUpperCase());
console.log('Reversed:', text.split('').reverse().join(''));
console.log('Word count:', text.split(' ').length);
console.log('Contains "awesome":', text.includes('awesome'));`
},
{
name: 'FizzBuzz',
code: `for (let i = 1; i <= 30; i++) {
if (i % 15 === 0) console.log('FizzBuzz');
else if (i % 3 === 0) console.log('Fizz');
else if (i % 5 === 0) console.log('Buzz');
else console.log(i);
}`
},
{
name: 'Sorting',
code: `const fruits = ['banana', 'apple', 'orange', 'mango', 'kiwi'];
console.log('Original:', fruits);
console.log('Sorted:', [...fruits].sort());
const nums = [42, 8, 15, 23, 4, 16];
console.log('\\nNumbers:', nums);
console.log('Ascending:', [...nums].sort((a, b) => a - b));
console.log('Descending:', [...nums].sort((a, b) => b - a));`
},
{
name: 'Date & Time',
code: `const now = new Date();
console.log('Current time:', now.toString());
console.log('ISO format:', now.toISOString());
console.log('Year:', now.getFullYear());
console.log('Month:', now.getMonth() + 1);
console.log('Day:', now.getDate());
console.log('Day of week:', ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'][now.getDay()]);`
},
{
name: 'Fetch Data',
code: `// fetch() works with dynamic URLs!
const baseUrl = "https://api.github.com";
// Fetch GitHub zen (random quote)
const zen = fetch(baseUrl + "/zen");
console.log("GitHub Zen:", zen);
// Build URL dynamically and fetch repo info
const owner = "simonw";
const repo = "tools";
const repoUrl = baseUrl + "/repos/" + owner + "/" + repo;
const data = fetch(repoUrl);
const info = JSON.parse(data);
console.log("\\nRepo:", info.full_name);
console.log("Stars:", info.stargazers_count);
console.log("Description:", info.description);`
}
];
const codeInput = document.getElementById('code-input');
const runBtn = document.getElementById('run-btn');
const clearBtn = document.getElementById('clear-btn');
const copyBtn = document.getElementById('copy-btn');
const statusEl = document.getElementById('status');
const outputSection = document.getElementById('output-section');
const outputEl = document.getElementById('output');
const executionTimeEl = document.getElementById('execution-time');
const examplesList = document.getElementById('examples-list');
let QuickJS = null;
// Populate examples
examples.forEach((example, index) => {
const btn = document.createElement('button');
btn.className = 'example-btn';
btn.textContent = example.name;
btn.addEventListener('click', () => {
codeInput.value = example.code;
codeInput.focus();
// Auto-run if QuickJS is ready
if (QuickJS && !runBtn.disabled) {
executeCode(example.code);
}
});
examplesList.appendChild(btn);
});
// Status helpers
function showStatus(message, type = 'info') {
statusEl.textContent = message;
statusEl.className = `visible ${type}`;
}
function hideStatus() {
statusEl.className = '';
}
function showOutput(text, isError = false) {
outputEl.textContent = text || '(no output)';
outputEl.className = isError ? 'error-output' : '';
outputSection.classList.add('visible');
}
function hideOutput() {
outputSection.classList.remove('visible');
}
// URL hash management
function updateUrlHash(code) {
const encoded = encodeURIComponent(code);
window.history.replaceState(null, null, '#' + encoded);
}
function loadFromHash() {
if (!window.location.hash) return null;
try {
const hash = window.location.hash.substring(1);
return decodeURIComponent(hash);
} catch (e) {
console.error('Error decoding hash:', e);
return null;
}
}
// Execute code using QuickJS
async function executeCode(code) {
if (!QuickJS) {
showStatus('QuickJS not initialized', 'error');
return;
}
if (!code.trim()) {
showStatus('Please enter some code to execute', 'error');
return;
}
runBtn.disabled = true;
runBtn.textContent = 'Running...';
hideStatus();
const startTime = performance.now();
const logs = [];
try {
const vm = QuickJS.newContext();
// Create console.log function
const logHandle = vm.newFunction('log', (...args) => {
const output = args.map(arg => {
if (typeof arg === 'undefined') return 'undefined';
const str = vm.dump(arg);
if (typeof str === 'string') return str;
if (typeof str === 'object') return JSON.stringify(str, null, 2);
return String(str);
}).join(' ');
logs.push(output);
});
// Create console object with log, warn, error methods
const consoleHandle = vm.newObject();
vm.setProp(consoleHandle, 'log', logHandle);
vm.setProp(consoleHandle, 'warn', logHandle);
vm.setProp(consoleHandle, 'error', logHandle);
vm.setProp(consoleHandle, 'info', logHandle);
vm.setProp(vm.global, 'console', consoleHandle);
consoleHandle.dispose();
logHandle.dispose();
// Create fetch function using newAsyncifiedFunction for true async support
const fetchHandle = vm.newAsyncifiedFunction('fetch', async (urlHandle) => {
const url = vm.getString(urlHandle);
try {
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP ${response.status} ${response.statusText}`);
}
const text = await response.text();
return vm.newString(text);
} catch (e) {
throw vm.newError(`Fetch error: ${e.message}`);
}
});
vm.setProp(vm.global, 'fetch', fetchHandle);
fetchHandle.dispose();
// Execute the code (use evalCodeAsync to support async functions)
const result = await vm.evalCodeAsync(code);
const endTime = performance.now();
const executionTime = (endTime - startTime).toFixed(2);
if (result.error) {
const error = vm.dump(result.error);
result.error.dispose();
vm.dispose();
const errorMessage = typeof error === 'object' ?
(error.message || JSON.stringify(error)) : String(error);
showOutput(`Error: ${errorMessage}`, true);
executionTimeEl.textContent = `Execution time: ${executionTime}ms`;
} else {
const value = vm.dump(result.value);
result.value.dispose();
vm.dispose();
// Build output
let output = '';
if (logs.length > 0) {
output = logs.join('\n');
}
// Show return value if it's not undefined
if (value !== undefined) {
const valueStr = typeof value === 'object' ?
JSON.stringify(value, null, 2) : String(value);
if (output) {
output += '\n\n=> ' + valueStr;
} else {
output = '=> ' + valueStr;
}
}
showOutput(output || '(no output)');
executionTimeEl.textContent = `Execution time: ${executionTime}ms`;
}
// Update URL with the code
updateUrlHash(code);
} catch (e) {
const endTime = performance.now();
const executionTime = (endTime - startTime).toFixed(2);
showOutput(`Error: ${e.message}`, true);
executionTimeEl.textContent = `Execution time: ${executionTime}ms`;
} finally {
runBtn.disabled = false;
runBtn.textContent = 'Run Code';
}
}
// Initialize QuickJS with async support for fetch
async function initialize() {
try {
showStatus('Loading QuickJS WebAssembly runtime (with async support)...', 'info');
// Use the async variant for newAsyncifiedFunction support
QuickJS = await QJS.newQuickJSAsyncWASMModule();
runBtn.textContent = 'Run Code';
runBtn.disabled = false;
showStatus('Ready!', 'success');
setTimeout(hideStatus, 1500);
// Check for code in URL hash and execute
const hashCode = loadFromHash();
if (hashCode) {
codeInput.value = hashCode;
executeCode(hashCode);
}
} catch (e) {
showStatus('Failed to initialize QuickJS: ' + e.message, 'error');
console.error('Initialization error:', e);
}
}
// Event listeners
runBtn.addEventListener('click', () => {
executeCode(codeInput.value);
});
clearBtn.addEventListener('click', () => {
codeInput.value = '';
hideOutput();
hideStatus();
window.history.replaceState(null, null, window.location.pathname);
});
copyBtn.addEventListener('click', async () => {
try {
await navigator.clipboard.writeText(outputEl.textContent);
copyBtn.textContent = 'Copied!';
setTimeout(() => {
copyBtn.textContent = 'Copy';
}, 1500);
} catch (e) {
showStatus('Failed to copy to clipboard', 'error');
}
});
// Keyboard shortcut: Ctrl/Cmd + Enter to run
codeInput.addEventListener('keydown', (e) => {
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
e.preventDefault();
if (!runBtn.disabled) {
executeCode(codeInput.value);
}
}
// Handle Tab key for indentation
if (e.key === 'Tab') {
e.preventDefault();
const start = codeInput.selectionStart;
const end = codeInput.selectionEnd;
codeInput.value = codeInput.value.substring(0, start) + ' ' + codeInput.value.substring(end);
codeInput.selectionStart = codeInput.selectionEnd = start + 2;
}
});
// Handle hash changes (e.g., browser back/forward)
window.addEventListener('hashchange', () => {
const hashCode = loadFromHash();
if (hashCode && hashCode !== codeInput.value) {
codeInput.value = hashCode;
if (QuickJS) {
executeCode(hashCode);
}
}
});
// Initialize on page load
initialize();
</script>
</body>
</html>