1 | var falafel = require('../');
|
2 | var vm = require('vm');
|
3 |
|
4 | var termExps = [
|
5 | 'Identifier',
|
6 | 'CallExpression',
|
7 | 'BinaryExpression',
|
8 | 'UpdateExpression',
|
9 | 'UnaryExpression'
|
10 | ].reduce(function (acc, key) { acc[key] = true; return acc }, {});
|
11 |
|
12 | function terminated (node) {
|
13 | for (var p = node; p.parent; p = p.parent) {
|
14 | if (termExps[p.type]) return true;
|
15 | }
|
16 | return false;
|
17 | }
|
18 |
|
19 | var src = '{"a":[2,~9,prompt(":d")],"b":4,"c":prompt("beep"),"d":6}';
|
20 |
|
21 | var offsets = [];
|
22 | var output = falafel('(' + src + ')', function (node) {
|
23 | var isLeaf = node.parent
|
24 | && !terminated(node.parent) && terminated(node)
|
25 | ;
|
26 |
|
27 | if (isLeaf) {
|
28 | var s = node.source();
|
29 | var prompted = false;
|
30 | var res = vm.runInNewContext('(' + s + ')', {
|
31 | prompt : function (x) {
|
32 | setTimeout(function () {
|
33 | node.update(x.toUpperCase());
|
34 | }, Math.random() * 50);
|
35 | prompted = true;
|
36 | }
|
37 | });
|
38 | if (!prompted) {
|
39 | var s_ = JSON.stringify(res);
|
40 | node.update(s_);
|
41 | }
|
42 | }
|
43 | });
|
44 |
|
45 | setTimeout(function () {
|
46 | console.log(src);
|
47 | console.log('---');
|
48 | console.log(output);
|
49 | }, 200);
|