forked from toolness/slowmo-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
368 lines (332 loc) · 9.85 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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>JS Playback</title>
<link rel="stylesheet" type="text/css" href="jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="jquery-ui.structure.min.css">
<link rel="stylesheet" type="text/css" href="jquery-ui.theme.min.css">
<style>
html {
font-family: "Helvetica Neue";
font-size: 12px;
}
* {
-moz-box-sizing: border-box;
-webkit-box-sizing: border-box;
}
#output .error {
color: red;
}
#log .debug {
color: gray;
}
#log .print {
color: black;
background: #f0f0f0;
padding: 4px;
}
#input {
display: block;
width: 100%;
height: 500px;
}
.desc {
/*font-size: 12px;*/
padding: 0.5em;
background: gray;
color: white;
}
.desc .code {
color: gray;
}
.active-code {
background-color: lightgreen;
position: absolute;
}
.supporting-code {
background: rgba(0, 0, 0, 0.1);
position: absolute;
}
#slider {
margin: 0 1em;
}
</style>
</head>
<body>
<h1>JS Playback</h1>
<div id="slider"></div>
<p>
<span id="info-line" class="info"></span>
</p>
<pre id="input">
function binaryIndexOf(list, searchElement) {
'use strict';
var minIndex = 0;
var maxIndex = list.length - 1;
var currentIndex;
var currentElement;
while (minIndex <= maxIndex) {
currentIndex = (minIndex + maxIndex) / 2 | 0;
currentElement = list[currentIndex];
if (currentElement < searchElement) {
minIndex = currentIndex + 1;
}
else if (currentElement > searchElement) {
maxIndex = currentIndex - 1;
}
else {
return currentIndex;
}
}
return -1;
}
var arr = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15];
binaryIndexOf(arr, 5);
</pre>
<div id="output">
</div>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/ace/1.1.3/ace.js"></script>
<script type="text/javascript" src="jquery-ui.min.js"></script>
<script src="require.js"></script>
<script>
var MAX_EXEC_MS = 250;
var RE_EXEC_MS = 300;
var EXPOSED_GLOBALS = [
"console",
"escape",
"unescape",
"encodeURIComponent",
"decodeURIComponent",
"encodeURI",
"decodeURI",
"JSON",
"Math",
"undefined"
];
var input, frames, frameIndex, markers = [];
function output(type, msg) {
var div = $('<div></div>');
div.addClass(type).text(msg).appendTo("#output");
}
var Range = ace.require("ace/range").Range;
function removeMarkers() {
while (markers.length) {
input.getSession().removeMarker(markers.pop().id);
}
}
function addMarker(location, secondary) {
var doc = input.getSession().getDocument()
, start = doc.indexToPosition(location[0], 0)
, end = doc.indexToPosition(location[1], 0)
, range = new Range(start.row, start.column, end.row, end.column)
, marker = {
id : input.getSession().addMarker(
range
, secondary ? "supporting-code" : "active-code"
)
, range : range
};
markers.push(marker);
}
function outputHighlight(code, range, caption, secondaryRange,
highlightTitle, secondaryTitle) {
frames.push({
caption : caption
, ranges : secondaryRange ? [range, secondaryRange] : [range]
})
}
function LogListeners(listeners) {
return function(name) {
if (name in listeners) {
var args = [this].concat(Array.prototype.slice.call(arguments, 1));
listeners[name].apply(listeners, args);
}
};
}
function describeVar(name, value) {
var span = $('<div class="desc"><code class="name"></code> is ' +
'<span class="value"></span></div>');
$('.name', span).text(name);
if (typeof(value) == "function")
$('.value', span).text("a function");
else {
if (value === undefined) {
value = "undefined";
} else {
value = JSON.stringify(value);
}
$('<code></code>').text(value).appendTo($('.value', span));
}
return span;
}
define("main", function(require) {
var Scope = require("slowmo/scope");
var Attr = require("slowmo/attr");
var ScopeMangler = require("slowmo/scope-mangler");
var AttrMangler = require("slowmo/attr-mangler");
var LoopInserter = require("slowmo/loop-inserter");
var falafel = require("falafel");
var timeout;
var lastCode;
input = window.ace.edit($('#input')[0]);
input.setTheme("ace/theme/xcode");
input.getSession().setMode("ace/mode/javascript");
input.getSession().setUseSoftTabs(true);
input.getSession().setTabSize(2);
input.setShowPrintMargin(false);
input.setFontSize('inherit');
function selectFrame(index) {
var frame = frames[index];
removeMarkers();
var ranges = frame.ranges;
for(var i = 0; i < ranges.length; i++) {
addMarker(ranges[i], i);
}
$('#info-line').empty()
if (frame.caption) {
$('#info-line').append(frame.caption.clone());
}
}
$('#slider').slider({
min : 0
, step : 1
, slide : function( event, ui ) {
selectFrame(ui.value);
}
});
var execute = function execute() {
$('#output').empty();
frameIndex = -1;
frames = [];
var code = input.getValue();
var attrLog = LogListeners({
call: function(attr, obj, prop, args, range) {
outputHighlight(code, range,
describeVar("[" + JSON.stringify(prop) + "]",
obj[prop]),
null, "call of property " + prop);
},
get: function(attr, obj, prop, range) {
outputHighlight(code, range,
describeVar("[" + JSON.stringify(prop) + "]",
obj[prop]),
null, "retrieval of property " + prop);
},
update: function(attr, obj, prop, operator, prefix, oldValue, range) {
outputHighlight(code, range,
describeVar("[" + JSON.stringify(prop) + "]",
obj[prop]),
null, "update of property " + prop);
},
assign: function(attr, obj, prop, operator, oldValue, range) {
outputHighlight(code, range,
describeVar("[" + JSON.stringify(prop) + "]",
obj[prop]),
null, "assignment of property " + prop);
}
});
var scopeLog = LogListeners({
declare: function(scope, name, value, range) {
if (!range) return;
outputHighlight(code, range, describeVar(name, value), null,
"declaration of " + name);
},
get: function(scope, name, value, range) {
outputHighlight(code, range, describeVar(name, value),
scope.declRanges[name],
"retrieval of " + name,
"original declaration of " + name);
},
update: function(scope, name, operator, prefix, oldValue, range) {
outputHighlight(code, range, describeVar(name, scope.vars[name]),
scope.declRanges[name],
"update of " + name,
"original declaration of " + name);
},
assign: function(scope, name, operator, value, oldValue, range) {
outputHighlight(code, range, describeVar(name, value),
scope.declRanges[name],
"assignment of " + name,
"original declaration of " + name);
}
});
var startTime = 0;
var loopCheck = function loopCheck(range) {
if (Date.now() - startTime > MAX_EXEC_MS) {
outputHighlight(code, range);
throw new Error("max run time exceeded (" + MAX_EXEC_MS + " ms)");
}
};
var loopInserter = LoopInserter(function(node) {
return "loopCheck(" + JSON.stringify(node.range) + ");";
});
var logResult = function(result, name, range, type) {
var typeTitles = {
CallExpression: "result of function call",
BinaryExpression: "result of binary operation",
UnaryExpression: "result of unary operation"
};
outputHighlight(code, range, describeVar(name, result), null,
typeTitles[type]);
return result;
};
if (lastCode == code)
return;
lastCode = code;
if (!code.trim())
return output("error", "no code to run!");
try {
var mangled = falafel(code, function(node) {
AttrMangler(node);
ScopeMangler(node);
loopInserter(node);
if (node.type == "CallExpression" ||
node.type == "BinaryExpression" ||
node.type == "UnaryExpression") {
var codeSlice = code.slice(node.range[0], node.range[1]);
node.update("logResult(" + node.source() + ", " +
JSON.stringify(codeSlice) + ", " +
JSON.stringify(node.range) + ", " +
JSON.stringify(node.type) + ")");
}
}).toString();
} catch (e) {
if (e.index) {
outputHighlight(code, [e.index, e.index+1]);
}
return output("error", e.toString());
}
var attr = new Attr(attrLog);
var scope = new Scope(null, "GLOBAL", [0, code.length], scopeLog);
scope.declare("print", function(msg) {
output("print", msg);
});
EXPOSED_GLOBALS.forEach(function(name) {
scope.declare(name, window[name]);
});
startTime = Date.now();
try {
eval(mangled);
} catch (e) {
if (e.range && Array.isArray(e.range))
outputHighlight(code, e.range);
return output("error", e.toString());
}
$('#slider').slider("value", 0);
$('#slider').slider("option", "max", frames.length-1);
selectFrame(0);
};
input.on('change', function() {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(execute, RE_EXEC_MS);
});
execute();
});
require(["main"], function() {});
</script>
</body>
</html>