forked from daveagp/java_visualize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pytutor-customizations.js
executable file
·291 lines (258 loc) · 10 KB
/
pytutor-customizations.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
rightwardNudgeHack = false;
// turning on does minor tweaks to make things look more like C++, see README
faking_cpp = false;
// but allow overriding via URL
// http://stackoverflow.com/questions/901115/
function getParameterByName(name) {
var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}
if (getParameterByName("faking_cpp")) {
console.log(getParameterByName("faking_cpp"));
var p = getParameterByName("faking_cpp").toUpperCase();
faking_cpp = (p == "TRUE" || p == "T" || p == "1");
}
add_pytutor_hook(
"renderPrimitiveObject",
function(args) {
var obj = args.obj, d3DomElement = args.d3DomElement;
var typ = typeof obj;
if (obj == null)
d3DomElement.append('<span class="nullObj">null</span>');
else if (typ == "number")
d3DomElement.append('<span class="numberObj">' + obj + '</span>');
else if (typ == "boolean") {
if (obj)
d3DomElement.append('<span class="boolObj">true</span>');
else
d3DomElement.append('<span class="boolObj">false</span>');
}
else if (obj instanceof Array && obj[0] == "VOID") {
d3DomElement.append('<span class="voidObj">void</span>');
}
else if (obj instanceof Array && obj[0] == "NUMBER-LITERAL") {
// actually transmitted as a string
d3DomElement.append('<span class="numberObj">' + obj[1] + '</span>');
}
else if (obj instanceof Array && obj[0] == "CHAR-LITERAL") {
var asc = obj[1].charCodeAt(0);
var ch = obj[1];
// default
var show = asc.toString(16);
while (show.length < 4) show = "0" + show;
show = "\\u" + show;
if (ch == "\n") show = "\\n";
else if (ch == "\r") show = "\\r";
else if (ch == "\t") show = "\\t";
else if (ch == "\b") show = "\\b";
else if (ch == "\f") show = "\\f";
else if (ch == "\'") show = "\\\'";
else if (ch == "\"") show = "\\\"";
else if (ch == "\\") show = "\\\\";
else if (asc >= 32) show = ch;
// stringObj to make monospace
d3DomElement.append('<span class="stringObj">\'' + show + '\'</span>');
}
else
return [false]; // we didn't handle it
return [true]; // we handled it
});
add_pytutor_hook(
"isPrimitiveType",
function(args) {
var obj = args.obj;
if ((obj instanceof Array && obj[0] == "VOID")
|| (obj instanceof Array && obj[0] == "NUMBER-LITERAL")
|| (obj instanceof Array && obj[0] == "CHAR-LITERAL")
|| (obj instanceof Array && obj[0] == "ELIDE"))
return [true, true]; // we handled it, it's primitive
return [false]; // didn't handle it
});
add_pytutor_hook(
"end_updateOutput",
function(args) {
// find the colophon if it exists, and change it to something that makes more sense
$('#codeDisplayDiv div a').each(function(i, elt) {
if (elt.parentElement.innerHTML.indexOf('Code visualized') > -1) {
if (faking_cpp) {
elt.parentElement.parentElement.removeChild(elt.parentElement);
}
else {
elt.setAttribute('href', 'http://cscircles.cemc.uwaterloo.ca/java_visualize/');
elt.innerHTML = 'Java Visualizer';
}
}
});
if (faking_cpp) {
$("#stackHeader").html('Variables');
$("#heapHeader").html('Memory');
}
var myViz = args.myViz;
var curEntry = myViz.curTrace[myViz.curInstr];
if (myViz.params.stdin && myViz.params.stdin != "") {
var stdinPosition = curEntry.stdinPosition || 0;
var stdinContent =
'<span style="color:lightgray;text-decoration: line-through">'+
escapeHtml(myViz.params.stdin.substr(0, stdinPosition))+
'</span>'+
escapeHtml(myViz.params.stdin.substr(stdinPosition));
myViz.domRoot.find('#stdinShow').html(stdinContent);
}
return [false];
});
add_pytutor_hook(
"end_constructor",
function(args) {
var myViz = args.myViz;
if ((myViz.curTrace.length > 0)
&& myViz.curTrace[myViz.curTrace.length-1]
&& myViz.curTrace[myViz.curTrace.length-1].stdout) {
myViz.hasStdout = true;
myViz.stdoutLines = myViz.curTrace[myViz.curTrace.length-1].stdout.split("\n").length;
}
// if last frame is a step limit
else if ((myViz.curTrace.length > 1)
&& myViz.curTrace[myViz.curTrace.length-2]
&& myViz.curTrace[myViz.curTrace.length-2].stdout) {
myViz.hasStdout = true;
myViz.stdoutLines = myViz.curTrace[myViz.curTrace.length-2].stdout.split("\n").length;
}
else {
myViz.stdoutLines = -1;
}
if (myViz.hasStdout)
for (var i=0; i<myViz.curTrace.length; i++)
if (!(myViz.curTrace[i].stdout))
myViz.curTrace[i].stdout=" "; // always show it, if it's ever used
});
add_pytutor_hook(
"end_render",
function(args) {
var myViz = args.myViz;
myViz.domRoot.find('#pyStdout').attr('cols', 1);
myViz.domRoot.find('#pyStdout').attr('rows', Math.min(10, myViz.stdoutLines));
if (myViz.params.stdin && myViz.params.stdin != "") {
var stdinHTML = '<div id="stdinWrap">stdin:<pre id="stdinShow" style="border:1px solid gray"></pre></div>';
myViz.domRoot.find('#dataViz').append(stdinHTML);
}
myViz.domRoot.find('#'+myViz.generateID('globals_header')).html("Static fields");
});
add_pytutor_hook(
"isLinearObject",
function(args) {
var heapObj = args.heapObj;
if (heapObj[0]=='STACK' || heapObj[0]=='QUEUE')
return ['true', 'true'];
return ['false'];
});
add_pytutor_hook(
"renderCompoundObject",
function(args) {
var objID = args.objID;
var d3DomElement = args.d3DomElement;
var obj = args.obj;
var typeLabelPrefix = args.typeLabelPrefix;
var myViz = args.myViz;
var stepNum = args.stepNum;
if (!(obj[0] == 'LIST' || obj[0] == 'QUEUE' || obj[0] == 'STACK'))
return [false]; // didn't handle
var label = obj[0].toLowerCase();
var visibleLabel = {list:'array', queue:'queue', stack:'stack'}[label];
if (obj.length == 1) {
d3DomElement.append('<div class="typeLabel">' + typeLabelPrefix + 'empty ' + visibleLabel + '</div>');
return [true]; //handled
}
d3DomElement.append('<div class="typeLabel">' + typeLabelPrefix + visibleLabel + '</div>');
d3DomElement.append('<table class="' + label + 'Tbl"></table>');
var tbl = d3DomElement.children('table');
if (obj[0] == 'LIST') {
tbl.append('<tr></tr><tr></tr>');
var headerTr = tbl.find('tr:first');
var contentTr = tbl.find('tr:last');
// i: actual index in json object; ind: apparent index
for (var i=1, ind=0; i<obj.length; i++) {
val = obj[i];
var elide = val instanceof Array && val[0] == 'ELIDE';
// add a new column and then pass in that newly-added column
// as d3DomElement to the recursive call to child:
headerTr.append('<td class="' + label + 'Header"></td>');
headerTr.find('td:last').append(elide ? "…" : ind);
contentTr.append('<td class="'+ label + 'Elt"></td>');
if (!elide) {
myViz.renderNestedObject(val, stepNum, contentTr.find('td:last'));
ind++;
}
else {
contentTr.find('td:last').append("…");
ind += val[1]; // val[1] is the number of cells to skip
}
}
} // end of LIST handling
// Stack and Queue handling code by Will Gwozdz
/* The table produced for stacks and queues is formed slightly differently than the others,
missing the header row. Two rows made the dashed border not line up properly */
if (obj[0] == 'STACK') {
tbl.append('<tr></tr><tr></tr>');
var contentTr = tbl.find('tr:last');
contentTr.append('<td class="'+ label + 'FElt">'+'<span class="stringObj symbolic">↔</span>'+'</td>');
$.each(obj, function(ind, val) {
if (ind < 1) return; // skip type tag and ID entry
contentTr.append('<td class="'+ label + 'Elt"></td>');
myViz.renderNestedObject(val, stepNum, contentTr.find('td:last'));
});
contentTr.append('<td class="'+ label + 'LElt">'+'</td>');
}
if (obj[0] == 'QUEUE') {
tbl.append('<tr></tr><tr></tr>');
var contentTr = tbl.find('tr:last');
// Add arrows showing in/out direction
contentTr.append('<td class="'+ label + 'FElt">'+'<span class="stringObj symbolic">←</span></td>');
$.each(obj, function(ind, val) {
if (ind < 1) return; // skip type tag and ID entry
contentTr.append('<td class="'+ label + 'Elt"></td>');
myViz.renderNestedObject(val, stepNum, contentTr.find('td:last'));
});
contentTr.append('<td class="'+ label + 'LElt">'+'<span class="stringObj symbolic">←</span></td>');
}
return [true]; // did handle
});
add_pytutor_hook(
"end_renderDataStructures",
function(args) {
var myViz = args.myViz;
myViz.domRoot.find("td.instKey:contains('___NO_LABEL!___')").hide();
myViz.domRoot.find(".typeLabel:contains('dict')").each(
function(i) {
if ($(this).html()=='dict')
$(this).html('symbol table');
if ($(this).html()=='empty dict')
$(this).html('empty symbol table');
});
});
// java synthetics cause things which javascript doesn't like in an id
var old_generateID = ExecutionVisualizer.prototype.generateID;
ExecutionVisualizer.prototype.generateID = function(original_id) {
var sanitized = original_id.replace(
/[^0-9a-zA-Z_]/g,
function(match) {return '-'+match.charCodeAt(0)+'-';}
);
return old_generateID(sanitized);
}
// utility functions
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
var escapeHtml = function(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
};
// fix bug where suppressed lines confuse the ui when they are executed
String.prototype.rtrim = function() {
return this;
};