-
Notifications
You must be signed in to change notification settings - Fork 354
/
index.html
124 lines (113 loc) · 3.6 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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS-Interpreter Demo</title>
<link href="demos/style.css" rel="stylesheet" type="text/css">
<script>
// Depending on the URL argument, load the compressed or uncompressed version.
var compressed = (document.location.search === '?compressed');
if (compressed) {
document.write('<scr'+'ipt src="acorn_interpreter.js"></scr'+'ipt>');
} else {
document.write('<scr'+'ipt src="acorn.js"></scr'+'ipt>');
document.write('<scr'+'ipt src="interpreter.js"></scr'+'ipt>');
}
var myInterpreter;
function initAlert(interpreter, globalObject) {
var wrapper = function alert(text) {
return window.alert(arguments.length ? text : '');
};
interpreter.setProperty(globalObject, 'alert',
interpreter.createNativeFunction(wrapper));
}
function parseButton() {
var code = document.getElementById('code').value;
myInterpreter = new Interpreter(code, initAlert);
disable('');
}
function stepButton() {
var stack = myInterpreter.getStateStack();
if (stack.length) {
var node = stack[stack.length - 1].node;
var start = node.start;
var end = node.end;
} else {
var start = 0;
var end = 0;
}
createSelection(start, end);
try {
var ok = myInterpreter.step();
} finally {
if (!ok) {
disable('disabled');
}
}
}
function runButton() {
disable('disabled');
if (myInterpreter.run()) {
// Async function hit. There's more code to run.
setTimeout(runButton, 100);
}
}
function disable(disabled) {
document.getElementById('stepButton').disabled = disabled;
document.getElementById('runButton').disabled = disabled;
}
function createSelection(start, end) {
var field = document.getElementById('code');
if (field.createTextRange) {
var selRange = field.createTextRange();
selRange.collapse(true);
selRange.moveStart('character', start);
selRange.moveEnd('character', end);
selRange.select();
} else if (field.setSelectionRange) {
field.setSelectionRange(start, end);
} else if (field.selectionStart) {
field.selectionStart = start;
field.selectionEnd = end;
}
field.focus();
}
</script>
</head>
<body>
<h1>JS-Interpreter Demo</h1>
<p>Enter JavaScript code below, then click <em>Parse</em>. To execute, either
click <em>Step</em> repeatedly, or click <em>Run</em> once.
Open your browser's console for errors.</p>
<p><textarea id="code" spellcheck="false">
var result = [];
function fibonacci(n, output) {
var a = 1, b = 1, sum;
for (var i = 0; i < n; i++) {
output.push(a);
sum = a + b;
a = b;
b = sum;
}
}
fibonacci(16, result);
alert(result.join(', '));
</textarea><br>
<button onclick="parseButton()">Parse</button>
<button onclick="stepButton()" id="stepButton" disabled="disabled">Step</button>
<button onclick="runButton()" id="runButton" disabled="disabled">Run</button>
</p>
<p><small>
<script>
if (compressed) {
document.write('[ Running compressed bundle. Switch to <A HREF="?uncompressed">raw sources</A>. ]');
} else {
document.write('[ Running raw sources. Switch to <A HREF="?compressed">compressed bundle</A>. ]');
}
disable('disabled');
</script>
</small></p>
<p>Read the <a href="docs.html">JS-Interpreter documentation</a>.</p>
<p>Get the <a href="https://github.com/NeilFraser/JS-Interpreter">source code</a>.</p>
</body>
</html>