This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 427
/
repl.js
547 lines (489 loc) · 15.3 KB
/
repl.js
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
function createEditor(elem) {
var editor = ace.edit(elem);
var session = editor.getSession();
editor.setTheme('ace/theme/tomorrow');
editor.setShowPrintMargin(false);
editor.commands.removeCommands(['gotoline', 'find']);
session.setMode('ace/mode/javascript');
session.setUseSoftTabs(true);
session.setTabSize(2);
session.setUseWorker(false);
editor.setOption('scrollPastEnd', 0.33);
elem.style.lineHeight = '18px';
return editor;
}
var optionsConfig = [
{
type: "string",
name: "mathRandomSeed",
defaultVal: "",
description: "If you want Prepack to evaluate Math.random() calls, please provide a seed."
},
{
type: "boolean",
name: "inlineExpressions",
defaultVal: true,
description: "Avoids naming expressions when they are only used once, and instead inline them where they are used."
},
{
type: "boolean",
name: "delayInitializations",
defaultVal: false,
description: "Delay initializations."
},
{
type: "choice",
name: "compatibility",
choices: ["browser", "jsc-600-1-4-17", "node-source-maps", "node-react"],
defaultVal: "node-react",
description: "The target environment for Prepack"
},
{
type: "string",
name: "lazyObjectsRuntime",
defaultVal: "",
description: "Enable lazy objects feature and specify the JS runtime that supports this feature."
},
{
type: "choice",
name: "invariantLevel",
choices: [0, 1, 2, 3],
defaultVal: 0,
description: "Whether and how many checks to generate that validate Prepack's assumptions about the environment."
},
{
type: "boolean",
name: "reactEnabled",
defaultVal: true,
description: "Enables support for React features, such as JSX syntax."
},
{
type: "choice",
name: "reactOutput",
choices: ["jsx", "create-element"],
defaultVal: "jsx",
description: "Specifies the serialization output of JSX nodes when React mode is enabled."
},
{
type: "boolean",
name: "stripFlow",
defaultVal: true,
description: "Removes Flow type annotations from the output."
},
];
var demos = [];
/**generate select */
function generateDemosSelect(obj, dom) {
// var keys = ['<option value='+-1+'>select demo</option>'];
var keys = [];
demos = [];
for (var name in obj) {
demos.push(name);
keys.push('<option value=' + name + '>' + name + '</option>');
}
dom.innerHTML = keys.join('');
}
var worker;
var debounce;
var messagesOutput = document.querySelector('.input .messages');
var replOutput = document.querySelector('.output .repl');
var isEmpty = /^\s*$/;
function terminateWorker() {
if (worker) {
worker.terminate();
worker = null;
}
}
function createWikiLink(code) {
const wikiLink = document.createElement('a');
wikiLink.href = 'https://github.com/facebook/prepack/wiki/' + encodeURIComponent(code);
wikiLink.text = code;
wikiLink.setAttribute('target', '_blank');
return wikiLink;
}
function createLineLink(location) {
const lineLink = document.createElement('a');
let lineNumber = location.start ? location.start.line : location.line;
let colNumber = location.start ? location.start.column : location.column;
colNumber++;
let lineText = lineNumber + ':' + colNumber;
lineLink.href = '';
lineLink.onclick = function() {
input.gotoLine(lineNumber);
return false;
};
lineLink.text = lineText;
lineLink.classList.add("line-link");
return lineLink;
}
function processMessage(messageNode, data) {
// TODO: syntax errors need their location stripped
if (data.location) {
const wikiLink = createWikiLink(data.errorCode);
const lineLink = createLineLink(data.location);
messageNode.appendChild(wikiLink);
messageNode.appendChild(document.createTextNode(' ('));
messageNode.appendChild(lineLink);
messageNode.appendChild(document.createTextNode('): ' + data.message + '\n'));
} else if (!data.code) {
messageNode.appendChild(document.createTextNode(data.message + '\n'));
} else {
const wikiLink = createWikiLink(data.errorCode);
messageNode.appendChild(wikiLink);
messageNode.appendChild(document.createTextNode(': ' + data.message + '\n'));
}
}
function getMessageClassType(severity) {
switch(severity) {
case "FatalError":
return "error";
case "RecoverableError":
return "error";
case "Warning":
return "warning";
case "Information":
return "warning";
default:
return "error";
}
}
function showMessages(messages) {
messagesOutput.style.display = 'inline-block';
for (var i in messages) {
const message = document.createElement('div');
message.classList.add("message", getMessageClassType(messages[i].severity));
processMessage(message, messages[i]);
messagesOutput.appendChild(message);
}
}
function getHashedDemo(hash) {
if (hash[0] !== '#' || hash.length < 2) return null;
var encoded = hash.slice(1);
if (encoded.match(/^[a-zA-Z0-9+/=_-]+$/)) {
return LZString.decompressFromEncodedURIComponent(encoded)
}
return null;
}
function makeDemoSharable() {
var encoded = LZString.compressToEncodedURIComponent(input.getValue());
history.replaceState(undefined, undefined, `#${encoded}`);
}
function showGeneratedCode(code) {
if (isEmpty.test(code) && !isEmpty.test(input.getValue())) {
code =
'// Your code was all dead code and thus eliminated.\n' + '// Try storing a property on the global object.';
}
output.setValue(code, -1);
}
function showGenerationGraph(graph) {
drawGraphCallback = () => {
if (graph) {
var graphData = JSON.parse(graph);
var visData = {
nodes: graphData.nodes,
edges: graphData.edges,
};
var visOptions = {};
var boxNetwork = new vis.Network(graphBox, visData, visOptions);
}
};
if (showGraphDiv) drawGraphCallback();
}
function compile() {
clearTimeout(debounce);
terminateWorker();
messagesOutput.innerHTML = '';
messagesOutput.style.display = 'none';
replOutput.style.display = 'block';
output.setValue('// Compiling...', -1);
debounce = setTimeout(function() {
worker = new Worker('js/repl-worker.js');
worker.onmessage = function(e) {
const result = e.data;
if (result.type === 'success') {
const { data, graph, messages } = result;
showGeneratedCode(data);
showGenerationGraph(graph);
showMessages(messages);
} else if (result.type === 'error') {
const errors = result.data;
showMessages(errors);
output.setValue('// Prepack is unable to produce output for this input.\n// Please check the left pane for diagnostic information.', -1);
}
terminateWorker();
};
var options = {}
for (var configIndex in optionsConfig) {
var config = optionsConfig[configIndex];
var domE = document.querySelector("#prepack-option-" + config.name);
if (config.type === "choice") {
options[config.name] = domE.options[domE.selectedIndex].value;
} else if (config.type === "boolean") {
options[config.name] = (domE.checked === true);
} else if (config.type === "string") {
if (domE.value) {
options[config.name] = domE.value;
}
}
}
worker.postMessage({code: input.getValue(), options: options});
}, 500);
}
var output = createEditor(replOutput);
output.setReadOnly(true);
output.setHighlightActiveLine(false);
output.setHighlightGutterLine(false);
var input = createEditor(document.querySelector('.input .repl'));
input.on('change', compile);
input.on('change', makeDemoSharable);
/**record **/
var selectRecord = document.querySelector('select.select-record');
var optionsRecord = document.querySelector('#optionsMenuRecord');
var selectInput = document.querySelector('#recordName');
var optionsButton = document.querySelector('#optionsMenuButton');
var saveButton = document.querySelector('#saveBtn');
var deleteButton = document.querySelector('#deleteBtn');
var storage = window.localStorage;
/** graph **/
var graphButton = document.querySelector('#graphBtn');
var graphBox = document.getElementById('graphBox');
var graphDiv = document.querySelector('#graph');
var inputDiv = document.getElementById('inputDiv');
var outputDiv = document.getElementById('outputDiv');
var showGraphDiv = false;
var drawGraphCallback = null;
function changeDemosSelect(val) {
if (!val.value) return;
selectInput.value = val.value;
var localCache = getDemosCache();
var code = localCache[val.value] || '';
input.setValue(code);
compile();
}
var demoSelector = new Select({
el: selectRecord,
className: 'select-theme-dark',
});
demoSelector.on('change', changeDemosSelect);
function getDemosCache() {
return JSON.parse(storage.getItem('prepackDemos') || '{}');
}
function setDemosCache(data) {
storage.setItem('prepackDemos', JSON.stringify(data || {}));
}
function addDefaultExamples() {
var cache = getDemosCache();
var code, name;
name = 'EliminationOfAbstractionTax';
code = [
'(function () {',
' var self = this;',
' ["A", "B", 42].forEach(function(x) {',
' var name = "_" + x.toString()[0].toLowerCase();',
' var y = parseInt(x);',
' self[name] = y ? y : x;',
' });',
'})();',
].join('\n');
cache[name] = code;
name = 'EnvironmentInteractionsAndBranching';
code = [
'(function(){',
' function fib(x) { return x <= 1 ? x : fib(x - 1) + fib(x - 2); }',
' let x = Date.now();',
' if (x === 0) x = fib(10);',
' global.result = x;',
'})();',
].join('\n');
cache[name] = code;
name = 'Fibonacci';
code = [
'(function () {',
' function fibonacci(x) {',
' return x <= 1 ? x : fibonacci(x - 1) + fibonacci(x - 2);',
' }',
' global.x = fibonacci(10);',
'})();',
].join('\n');
cache[name] = code;
name = 'HelloWorld';
code = [
'(function () {',
' function hello() { return "hello"; }',
' function world() { return "world"; }',
' global.s = hello() + " " + world();',
'})();',
].join('\n');
cache[name] = code;
name = 'ModuleInitialization';
code = [
'(function () {',
' let moduleTable = {};',
' function define(id, f) { moduleTable[id] = f; }',
' function require(id) {',
' let x = moduleTable[id];',
' return x instanceof Function ? (moduleTable[id] = x()) : x;',
' }',
' global.require = require;',
' define("one", function() { return 1; });',
' define("two", function() { return require("one") + require("one"); });',
' define("three", function() { return require("two") + require("one"); });',
' define("four", function() { return require("three") + require("one"); });',
'})();',
'three = require("three");'
].join('\n');
cache[name] = code;
var hashedDemo = getHashedDemo(location.hash);
name = null;
if (hashedDemo) {
name = createHashedDemoName(hashedDemo, cache);
cache[name] = hashedDemo;
}
generateDemosSelect(cache, selectRecord);
setDemosCache(cache);
setTimeout(() => {
demoSelector.change(name || 'Fibonacci');
});
}
function createHashedDemoName(hashedDemo, cache) {
var name = '\xa0';
for (var key in cache) {
if (cache[key] === hashedDemo) {
name = key;
break;
}
}
return name;
}
function addOptions() {
var optionStrings = [];
for (var configIndex in optionsConfig) {
var config = optionsConfig[configIndex];
var configId = 'prepack-option-' + config.name;
optionStrings.push("<div class='prepack-option'>");
optionStrings.push("<label class='prepack-option-label' for='");
optionStrings.push(configId);
optionStrings.push("'>");
optionStrings.push(config.name);
optionStrings.push("<div class='prepack-option-description'>");
optionStrings.push(config.description);
optionStrings.push("</div>");
if (config.type === "choice") {
optionStrings.push("<select id='");
optionStrings.push(configId);
optionStrings.push("'>");
for (var nameIndex in config.choices) {
var name = config.choices[nameIndex];
if (name === config.defaultVal) {
optionStrings.push('<option value=' + name + ' selected>' + name + '</option>');
} else {
optionStrings.push('<option value=' + name + '>' + name + '</option>');
}
}
optionStrings.push("</select>");
} else if (config.type === "boolean") {
optionStrings.push("<input type='checkbox' id='");
optionStrings.push(configId);
if (config.defaultVal === true) {
optionStrings.push("' checked=true>")
} else {
optionStrings.push("'>");
}
} else if (config.type === "string") {
optionStrings.push("<input type='text' id='");
optionStrings.push(configId);
if (config.defaultVal != null) {
optionStrings.push("' value='");
optionStrings.push(config.defaultVal);
optionStrings.push("'>");
} else {
optionStrings.push("'>");
}
}
optionStrings.push("</label>");
optionStrings.push("</div>");
}
optionsRecord.innerHTML = optionStrings.join('');
for (var configIndex in optionsConfig) {
var config = optionsConfig[configIndex];
var domE = document.querySelector("#prepack-option-" + config.name);
if (config.type === "choice") {
var demoSelector = new Select({
el: domE,
className: 'select-theme-dark',
});
domE.addEventListener('change', compile);
} else if (config.type === "boolean") {
domE.addEventListener('change', compile);
} else if (config.type === "string") {
domE.addEventListener('input', compile);
}
}
optionsButton.onclick = function() {
if (optionsRecord.style.display !== "inline-block") {
optionsRecord.style.display = "inline-block" ;
} else {
optionsRecord.style.display = "none" ;
}
};
}
addDefaultExamples();
addOptions();
deleteButton.addEventListener('click', () => {
var name = selectInput.value;
if (name == null || name.replace(/\s+/, '') === '') return;
if (demos.length === 0) {
selectInput.value = '';
return;
}
var cache = getDemosCache();
delete cache[name];
input.setValue('');
generateDemosSelect(cache, selectRecord);
setDemosCache(cache);
if (demos.length > 0) {
selectInput.value = demos[0];
setTimeout(() => {
demoSelector.change(demos[0]);
});
} else {
selectInput.value = '';
input.setValue(defaultCode);
compile();
}
});
saveButton.addEventListener('click', () => {
var name = selectInput.value;
if (name == null || name.replace(/\s+/, '') === '') return;
var code = input.getValue();
var cache = getDemosCache();
cache[name] = code;
generateDemosSelect(cache, selectRecord);
setDemosCache(cache);
setTimeout(() => {
demoSelector.change(name);
});
});
graphButton.addEventListener('click', () => {
if (!showGraphDiv) {
inputDiv.style.width = "33%";
outputDiv.style.width = "33%";
graphDiv.style.width = "34%";
outputDiv.style.left = "33%";
graphDiv.style.display = "block";
showGraphDiv = true;
graphButton.innerHTML = "HIDE HEAP";
if (drawGraphCallback !== null) {
drawGraphCallback();
drawGraphCallback = null;
}
} else {
inputDiv.style.width = "50%";
outputDiv.style.width = "50%";
graphDiv.style.width = "50%";
outputDiv.style.left = "50%";
graphDiv.style.display = "none";
showGraphDiv = false;
graphButton.innerHTML = "SHOW HEAP";
}
});