Skip to content

Commit 054c7d4

Browse files
committed
Add Babel demo.
1 parent e04a077 commit 054c7d4

File tree

5 files changed

+104
-5
lines changed

5 files changed

+104
-5
lines changed

demos/async.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script>
1010
var myInterpreter;
1111
function initAlert(interpreter, globalObject) {
12-
var wrapper = function(text) {
12+
var wrapper = function alert(text) {
1313
return window.alert(arguments.length ? text : '');
1414
};
1515
interpreter.setProperty(globalObject, 'alert',

demos/babel.html

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JS-Interpreter Babel Demo</title>
6+
<link href="style.css" rel="stylesheet" type="text/css">
7+
<script src="../acorn.js"></script>
8+
<script src="../interpreter.js"></script>
9+
<script src="babel.min.js"></script>
10+
<script>
11+
var myInterpreter;
12+
function initAlert(interpreter, scope) {
13+
var wrapper = function alert(text) {
14+
return window.alert(arguments.length ? text : '');
15+
};
16+
interpreter.setProperty(scope, 'alert',
17+
interpreter.createNativeFunction(wrapper));
18+
}
19+
20+
function parseButton() {
21+
var code = document.getElementById('code').value;
22+
var options = {
23+
'presets': ['es2015']
24+
};
25+
code = Babel.transform(code, options).code;
26+
27+
alert('Code transpiled to:\n' + code);
28+
myInterpreter = new Interpreter(code, initAlert);
29+
disable('');
30+
}
31+
32+
function runButton() {
33+
disable('disabled');
34+
if (myInterpreter.run()) {
35+
// Async function hit. There's more code to run.
36+
disable('');
37+
}
38+
}
39+
40+
function disable(disabled) {
41+
document.getElementById('runButton').disabled = disabled;
42+
}
43+
</script>
44+
</head>
45+
<body>
46+
<h1>JS-Interpreter Babel Demo</h1>
47+
48+
<p>A transpiler is required to run ES6 or greater using the JS-Interpreter.
49+
Babel is a good option which can generate ES5 either client-side or server-side.</p>
50+
51+
<p>For client-side transpiling, download
52+
<a href="https://unpkg.com/@babel/standalone/babel.min.js">@babel/standalone</a>
53+
and include it as a script tag on your page. Then convert your code like this:</p>
54+
55+
<pre>code = Babel.transform(code, {'presets': ['es2015']}).code;</pre>
56+
57+
<p>One issue is that highlighting the user's code as it executes won't work
58+
since it is the transpiled code that's actually being executed and the line
59+
numbers won't match.</p>
60+
61+
<p>Type some ES6, click <em>Parse</em>, then click <em>Run</em> once.
62+
Open your browser's console for errors.</p>
63+
64+
<p><textarea id="code" spellcheck="false">
65+
const result = [];
66+
const fibonacci = (n, output) => {
67+
let a = 1, b = 1, sum;
68+
for (let i = 0; i < n; i++) {
69+
output.push(a);
70+
sum = a + b;
71+
a = b;
72+
b = sum;
73+
}
74+
}
75+
fibonacci(16, result);
76+
alert(result.join(', '));
77+
</textarea><br>
78+
<button onclick="parseButton()">Parse</button>
79+
<button onclick="runButton()" id="runButton" disabled="disabled">Run</button>
80+
</p>
81+
82+
<p>Back to the <a href="../docs.html">JS-Interpreter documentation</a>.</p>
83+
84+
</body>
85+
</html>

demos/babel.min.js

+13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs.html

+3-2
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ <h2>Limitations</h2>
237237
sandbox. If you need these, write your own interfaces.</dd>
238238
<dt>ES6</dt>
239239
<dd>More recent additions to JavaScript such as <code>let</code> or
240-
<code>Set</code> aren't implemented. <a href="https://babeljs.io/">Babel</a>
241-
is a good method of transpiling newer versions of JavaScript to ES5.</dd>
240+
<code>Set</code> aren't implemented. Babel is a good method of transpiling
241+
newer versions of JavaScript to ES5. For a working example, see the
242+
<a href="demos/babel.html">Babel demo</a>.</dd>
242243
<dt>toString &amp; valueOf</dt>
243244
<dd>User-created functions are not called when casting objects to primitives.</dd>
244245
<dt>Performance</dt>

index.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616
}
1717

1818
var myInterpreter;
19-
function initAlert(interpreter, scope) {
19+
function initAlert(interpreter, globalObject) {
2020
var wrapper = function alert(text) {
2121
return window.alert(arguments.length ? text : '');
2222
};
23-
interpreter.setProperty(scope, 'alert',
23+
interpreter.setProperty(globalObject, 'alert',
2424
interpreter.createNativeFunction(wrapper));
2525
}
2626

0 commit comments

Comments
 (0)