Repository navigation
Expand file tree
/
Copy pathpytutor.ts
More file actions
3682 lines (3103 loc) · 139 KB
/
Copy pathpytutor.ts
File metadata and controls
3682 lines (3103 loc) · 139 KB
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Python Tutor: https://github.com/pgbovine/OnlinePythonTutor/
// Copyright (C) Philip Guo (philip@pgbovine.net)
// LICENSE: https://github.com/pgbovine/OnlinePythonTutor/blob/master/LICENSE.txt
/* TODO:
- substitute in a non-live version of the live editor from opt-live.js
in addition to the janky current version of the editor
- the 'hide attributes' link on objects don't work; also, while i'm at
it, maybe i can keep those changes persistent over time? err or not :/
*/
/* pytutor coding gotchas:
- *NEVER* use raw $(__) or d3.select(__) statements to select DOM elements.
*ALWAYS* use myViz.domRoot or myViz.domRootD3 for jQuery and D3, respectively.
Otherwise things will break in weird ways when you have more than one
visualization embedded within a webpage, due to multiple matches in
the global namespace.
- always use generateID and generateHeapObjID to generate unique CSS
IDs, or else things will break when multiple ExecutionVisualizer
instances are on a webpage
*/
require('./lib/d3.v2.min.js');
require('./lib/jquery-3.0.0.min.js');
require('./lib/jquery.jsPlumb-1.3.10-all-min.js'); // DO NOT UPGRADE ABOVE 1.3.10 OR ELSE BREAKAGE WILL OCCUR
require('./lib/jquery-ui-1.11.4/jquery-ui.js');
require('./lib/jquery-ui-1.11.4/jquery-ui.css');
require('./lib/jquery.ba-bbq.js'); // contains slight pgbovine modifications
require('../css/pytutor.css');
// for TypeScript
declare var jQuery: JQueryStatic;
declare var jsPlumb: any;
export var SVG_ARROW_POLYGON = '0,3 12,3 12,0 18,5 12,10 12,7 0,7';
var SVG_ARROW_HEIGHT = 10; // must match height of SVG_ARROW_POLYGON
/* colors - see pytutor.css for more colors */
export var brightRed = '#e93f34';
var connectorBaseColor = '#005583';
var connectorHighlightColor = brightRed;
var connectorInactiveColor = '#cccccc';
var errorColor = brightRed;
var breakpointColor = brightRed;
// Unicode arrow types: '\u21d2', '\u21f0', '\u2907'
export var darkArrowColor = brightRed;
export var lightArrowColor = '#c9e6ca';
var heapPtrSrcRE = /__heap_pointer_src_/;
var rightwardNudgeHack = true; // suggested by John DeNero, toggle with global
// the main event!
export class ExecutionVisualizer {
static curVisualizerID = 1;
static DEFAULT_EMBEDDED_CODE_DIV_WIDTH = 350;
static DEFAULT_EMBEDDED_CODE_DIV_HEIGHT = 400;
params: any = {};
curInputCode: string;
curTrace: any[];
// an array of objects with the following fields:
// 'text' - the text of the line of code
// 'lineNumber' - one-indexed (always the array index + 1)
// 'executionPoints' - an ordered array of zero-indexed execution points where this line was executed
// 'breakpointHere' - has a breakpoint been set here?
codeOutputLines: {text: string, lineNumber: number, executionPoints: number[], breakpointHere: boolean}[] = [];
promptForUserInput: boolean;
userInputPromptStr: string;
promptForMouseInput: boolean;
codDisplay: CodeDisplay;
navControls: NavigationController;
outputBox: ProgramOutputBox;
dataViz: DataVisualizer;
domRoot: any;
domRootD3: any;
curInstr: number = 0;
updateHistory: any[];
creationTime: number;
// API for adding a hook, created by David Pritchard
// keys, hook names; values, list of functions
pytutor_hooks: {string?: any[]} = {};
// represent the current state of the visualizer object; i.e., which
// step is it currently visualizing?
prevLineIsReturn: boolean;
curLineIsReturn: boolean;
prevLineNumber: number;
curLineNumber: number;
curLineExceptionMsg: string;
// true iff trace ended prematurely since maximum instruction limit has
// been reached
instrLimitReached: boolean = false;
instrLimitReachedWarningMsg: string;
hasRendered: boolean = false;
visualizerID: number;
breakpoints: d3.Map<{}> = d3.map(); // set of execution points to set as breakpoints
sortedBreakpointsList: any[] = []; // sorted and synced with breakpoints
// Constructor with an ever-growing feature-crepped list of options :)
// domRootID is the string ID of the root element where to render this instance
//
// dat is data returned by the Python Tutor backend consisting of two fields:
// code - string of executed code
// trace - a full execution trace
//
// params is an object containing optional parameters, such as:
// jumpToEnd - if non-null, jump to the very end of execution if
// there's no error, or if there's an error, jump to the
// FIRST ENTRY with an error
// startingInstruction - the (zero-indexed) execution point to display upon rendering
// if this is set, then it *overrides* jumpToEnd
// codeDivHeight - maximum height of #pyCodeOutputDiv (in integer pixels)
// codeDivWidth - maximum width of #pyCodeOutputDiv (in integer pixels)
// editCodeBaseURL - the base URL to visit when the user clicks 'Edit code' (if null, then 'Edit code' link hidden)
// embeddedMode - shortcut for codeDivWidth=DEFAULT_EMBEDDED_CODE_DIV_WIDTH,
// codeDivHeight=DEFAULT_EMBEDDED_CODE_DIV_HEIGHT
// (and don't activate keyboard shortcuts!)
// disableHeapNesting - if true, then render all heap objects at the top level (i.e., no nested objects)
// drawParentPointers - if true, then draw environment diagram parent pointers for all frames
// WARNING: there are hard-to-debug MEMORY LEAKS associated with activating this option
// textualMemoryLabels - render references using textual memory labels rather than as jsPlumb arrows.
// this is good for slow browsers or when used with disableHeapNesting
// to prevent "arrow overload"
// updateOutputCallback - function to call (with 'this' as parameter)
// whenever this.updateOutput() is called
// (BEFORE rendering the output display)
// heightChangeCallback - function to call (with 'this' as parameter)
// whenever the HEIGHT of #dataViz changes
// verticalStack - if true, then stack code display ON TOP of visualization
// (else place side-by-side)
// visualizerIdOverride - override visualizer ID instead of auto-assigning it
// (BE CAREFUL ABOUT NOT HAVING DUPLICATE IDs ON THE SAME PAGE,
// OR ELSE ARROWS AND OTHER STUFF WILL GO HAYWIRE!)
// executeCodeWithRawInputFunc - function to call when you want to re-execute the given program
// with some new user input (somewhat hacky!)
// compactFuncLabels - render functions with a 'func' prefix and no type label
// showAllFrameLabels - display frame and parent frame labels for all functions (default: false)
// hideCode - hide the code display and show only the data structure viz
// lang - to render labels in a style appropriate for other languages,
// and to display the proper language in langDisplayDiv:
// 'py2' for Python 2, 'py3' for Python 3, 'js' for JavaScript, 'java' for Java,
// 'ts' for TypeScript, 'ruby' for Ruby, 'c' for C, 'cpp' for C++
// [default is Python-style labels]
constructor(domRootID, dat, params) {
this.curInputCode = dat.code.rtrim(); // kill trailing spaces
this.params = params;
this.curTrace = dat.trace;
// postprocess the trace
if (this.curTrace.length > 0) {
var lastEntry = this.curTrace[this.curTrace.length - 1];
// if the final entry is raw_input or mouse_input, then trim it from the trace and
// set a flag to prompt for user input when execution advances to the
// end of the trace
if (lastEntry.event === 'raw_input') {
this.promptForUserInput = true;
this.userInputPromptStr = htmlspecialchars(lastEntry.prompt);
this.curTrace.pop() // kill last entry so that it doesn't get displayed
}
else if (lastEntry.event === 'mouse_input') {
this.promptForMouseInput = true;
this.userInputPromptStr = htmlspecialchars(lastEntry.prompt);
this.curTrace.pop() // kill last entry so that it doesn't get displayed
}
else if (lastEntry.event === 'instruction_limit_reached') {
this.instrLimitReached = true;
this.instrLimitReachedWarningMsg = lastEntry.exception_msg;
this.curTrace.pop() // postprocess to kill last entry
}
}
// if you have multiple ExecutionVisualizer on a page, their IDs
// better be unique or else you'll run into rendering problems
if (this.params.visualizerIdOverride) {
this.visualizerID = this.params.visualizerIdOverride;
} else {
this.visualizerID = ExecutionVisualizer.curVisualizerID;
assert(this.visualizerID > 0);
ExecutionVisualizer.curVisualizerID++;
}
// sanitize to avoid nasty 'undefined' states
this.params.disableHeapNesting = (this.params.disableHeapNesting === true);
this.params.drawParentPointers = (this.params.drawParentPointers === true);
this.params.textualMemoryLabels = (this.params.textualMemoryLabels === true);
this.params.showAllFrameLabels = (this.params.showAllFrameLabels === true);
// insert ExecutionVisualizer into domRootID in the DOM
var tmpRoot = $('#' + domRootID);
var tmpRootD3 = d3.select('#' + domRootID);
tmpRoot.html('<div class="ExecutionVisualizer"></div>');
// the root elements for jQuery and D3 selections, respectively.
// ALWAYS use these and never use raw $(__) or d3.select(__)
this.domRoot = tmpRoot.find('div.ExecutionVisualizer');
this.domRootD3 = tmpRootD3.select('div.ExecutionVisualizer');
if (this.params.lang === 'java') {
this.activateJavaFrontend(); // ohhhh yeah! do this before initializing codeOutputLines (ugh order dependency)
}
var lines = this.curInputCode.split('\n');
for (var i = 0; i < lines.length; i++) {
var cod = lines[i];
var n: {text: string, lineNumber: number, executionPoints: number[], breakpointHere: boolean} = {
text: cod,
lineNumber: i + 1,
executionPoints: [],
breakpointHere: false,
};
$.each(this.curTrace, function(j, elt) {
if (elt.line === n.lineNumber) {
n.executionPoints.push(j);
}
});
// if there is a comment containing 'breakpoint' and this line was actually executed,
// then set a breakpoint on this line
var breakpointInComment = false;
var toks = cod.split('#');
for (var j = 1 /* start at index 1, not 0 */; j < toks.length; j++) {
if (toks[j].indexOf('breakpoint') != -1) {
breakpointInComment = true;
}
}
if (breakpointInComment && n.executionPoints.length > 0) {
n.breakpointHere = true;
this.addToBreakpoints(n.executionPoints);
}
this.codeOutputLines.push(n);
}
this.try_hook("end_constructor", {myViz:this});
this.render(); // go for it!
}
/* API for adding a hook, created by David Pritchard
https://github.com/daveagp
[this documentation is a bit deprecated since Philip made try_hook a
method of ExecutionVisualizer, but the general ideas remain]
An external user should call
add_pytutor_hook("hook_name_here", function(args) {...})
args will be a javascript object with several named properties;
this is meant to be similar to Python's keyword arguments.
The hooked function should return an array whose first element is a boolean:
true if it completely handled the situation (no further hooks
nor the base function should be called); false otherwise (wasn't handled).
If the hook semantically represents a function that returns something,
the second value of the returned array is that semantic return value.
E.g. for the Java visualizer a simplified version of a hook we use is:
add_pytutor_hook(
"isPrimitiveType",
function(args) {
var obj = args.obj; // unpack
if (obj instanceof Array && obj[0] == "CHAR-LITERAL")
return [true, true]; // yes we handled it, yes it's primitive
return [false]; // didn't handle it, let someone else
});
Hook callbacks can return false or undefined (i.e. no return
value) in lieu of [false].
NB: If multiple functions are added to a hook, the oldest goes first. */
add_pytutor_hook(hook_name, func) {
if (this.pytutor_hooks[hook_name])
this.pytutor_hooks[hook_name].push(func);
else
this.pytutor_hooks[hook_name] = [func];
}
/* [this documentation is a bit deprecated since Philip made try_hook a
method of ExecutionVisualizer, but the general ideas remain]
try_hook(hook_name, args): how the internal codebase invokes a hook.
args will be a javascript object with several named properties;
this is meant to be similar to Python's keyword arguments. E.g.,
function isPrimitiveType(obj) {
var hook_result = try_hook("isPrimitiveType", {obj:obj});
if (hook_result[0]) return hook_result[1];
// go on as normal if the hook didn't handle it
Although add_pytutor_hook allows the hooked function to
return false or undefined, try_hook will always return
something with the strict format [false], [true] or [true, ...]. */
try_hook(hook_name, args) {
if (this.pytutor_hooks[hook_name]) {
for (var i=0; i<this.pytutor_hooks[hook_name].length; i++) {
// apply w/o "this", and pack sole arg into array as required by apply
var handled_and_result
= this.pytutor_hooks[hook_name][i].apply(null, [args]);
if (handled_and_result && handled_and_result[0])
return handled_and_result;
}
}
return [false];
}
// create a unique ID, which is often necessary so that jsPlumb doesn't get confused
// due to multiple ExecutionVisualizer instances being displayed simultaneously
generateID(original_id) {
// (it's safer to start names with a letter rather than a number)
return 'v' + this.visualizerID + '__' + original_id;
}
render() {
if (this.hasRendered) {
alert('ERROR: You should only call render() ONCE on an ExecutionVisualizer object.');
return;
}
var myViz = this; // to prevent confusion of 'this' inside of nested functions
if (this.params.verticalStack) {
this.domRoot.html('<table border="0" class="visualizer">\
<tr><td class="vizLayoutTd" id="vizLayoutTdFirst""></td></tr>\
<tr><td class="vizLayoutTd" id="vizLayoutTdSecond"></td></tr>\
</table>');
}
else {
this.domRoot.html('<table border="0" class="visualizer"><tr>\
<td class="vizLayoutTd" id="vizLayoutTdFirst"></td>\
<td class="vizLayoutTd" id="vizLayoutTdSecond"></td>\
</tr></table>');
}
// create a container for a resizable slider to encompass
// both CodeDisplay and NavigationController
this.domRoot.find('#vizLayoutTdFirst').append('<div id="codAndNav" style="width: 550px;"/>');
var base = this.domRoot.find('#vizLayoutTdFirst #codAndNav');
var baseD3 = this.domRootD3.select('#vizLayoutTdFirst #codAndNav');
this.codDisplay = new CodeDisplay(this, base, baseD3,
this.curInputCode, this.params.lang, this.params.editCodeBaseURL);
this.navControls = new NavigationController(this, base, baseD3, this.curTrace.length);
if (this.params.embeddedMode) {
// don't override if they've already been set!
if (this.params.codeDivWidth === undefined) {
this.params.codeDivWidth = ExecutionVisualizer.DEFAULT_EMBEDDED_CODE_DIV_WIDTH;
}
if (this.params.codeDivHeight === undefined) {
this.params.codeDivHeight = ExecutionVisualizer.DEFAULT_EMBEDDED_CODE_DIV_HEIGHT;
}
// add an extra label to link back to the main site, so that viewers
// on the embedded page know that they're seeing an OPT visualization
base.append('<div style="font-size: 8pt; margin-bottom: 20px;">Visualized using <a href="http://pythontutor.com" target="_blank" style="color: #3D58A2;">Online Python Tutor</a> by <a href="http://www.pgbovine.net/" target="_blank" style="color: #3D58A2;">Philip Guo</a></div>');
base.find('#codeFooterDocs').hide(); // cut out extraneous docs
}
// not enough room for these extra buttons ...
if (this.params.codeDivWidth &&
this.params.codeDivWidth < 470) {
this.domRoot.find('#jmpFirstInstr').hide();
this.domRoot.find('#jmpLastInstr').hide();
}
if (this.params.codeDivWidth) {
this.domRoot.find('#codAndNav').width(this.params.codeDivWidth);
}
if (this.params.codeDivHeight) {
this.domRoot.find('#pyCodeOutputDiv')
.css('max-height', this.params.codeDivHeight + 'px');
}
// enable left-right draggable pane resizer (originally from David Pritchard)
base.resizable({
handles: "e", // "east" (i.e., right)
minWidth: 100, //otherwise looks really goofy
resize: (event, ui) => {
this.domRoot.find("#codeDisplayDiv").css("height", "auto"); // redetermine height if necessary
this.navControls.renderSliderBreakpoints(this.sortedBreakpointsList); // update breakpoint display accordingly on resize
if (this.params.updateOutputCallback) // report size change
this.params.updateOutputCallback(this);
}});
this.outputBox = new ProgramOutputBox(this, this.domRoot.find('#vizLayoutTdSecond'),
this.params.embeddedMode ? '45px' : null);
this.dataViz = new DataVisualizer(this,
this.domRoot.find('#vizLayoutTdSecond'),
this.domRootD3.select('#vizLayoutTdSecond'));
myViz.navControls.showError(this.instrLimitReachedWarningMsg);
myViz.navControls.setupSlider(this.curTrace.length - 1);
if (this.params.startingInstruction) {
this.params.jumpToEnd = false; // override! make sure to handle FIRST
// weird special case for something like:
// e=raw_input(raw_input("Enter something:"))
if (this.params.startingInstruction == this.curTrace.length) {
this.params.startingInstruction--;
}
// fail-soft with out-of-bounds startingInstruction values:
if (this.params.startingInstruction < 0) {
this.params.startingInstruction = 0;
}
if (this.params.startingInstruction >= this.curTrace.length) {
this.params.startingInstruction = this.curTrace.length - 1;
}
assert(0 <= this.params.startingInstruction &&
this.params.startingInstruction < this.curTrace.length);
this.curInstr = this.params.startingInstruction;
}
if (this.params.jumpToEnd) {
var firstErrorStep = -1;
for (var i = 0; i < this.curTrace.length; i++) {
var e = this.curTrace[i];
if (e.event == 'exception' || e.event == 'uncaught_exception') {
firstErrorStep = i;
break;
}
}
// set to first error step if relevant since that's more informative
// than simply jumping to the very end
if (firstErrorStep >= 0) {
this.curInstr = firstErrorStep;
} else {
this.curInstr = this.curTrace.length - 1;
}
}
if (this.params.hideCode) {
this.domRoot.find('#vizLayoutTdFirst').hide(); // gigantic hack!
}
this.dataViz.precomputeCurTraceLayouts();
if (!this.params.hideCode) {
this.codDisplay.renderPyCodeOutput();
}
this.updateOutput();
this.hasRendered = true;
this.try_hook("end_render", {myViz:this});
}
_getSortedBreakpointsList() {
var ret = [];
this.breakpoints.forEach(function(k, v) {
ret.push(Number(k)); // these should be NUMBERS, not strings
});
ret.sort(function(x,y){return x-y}); // WTF, javascript sort is lexicographic by default!
return ret;
}
addToBreakpoints(executionPoints) {
$.each(executionPoints, (i, ep) => {
this.breakpoints.set(ep, 1);
});
this.sortedBreakpointsList = this._getSortedBreakpointsList(); // keep synced!
}
removeFromBreakpoints(executionPoints) {
$.each(executionPoints, (i, ep) => {
this.breakpoints.remove(ep);
});
this.sortedBreakpointsList = this._getSortedBreakpointsList(); // keep synced!
}
setBreakpoint(d) {
this.addToBreakpoints(d.executionPoints);
this.navControls.renderSliderBreakpoints(this.sortedBreakpointsList);
}
unsetBreakpoint(d) {
this.removeFromBreakpoints(d.executionPoints);
this.navControls.renderSliderBreakpoints(this.sortedBreakpointsList);
}
// find the previous/next breakpoint to c or return -1 if it doesn't exist
findPrevBreakpoint() {
var c = this.curInstr;
if (this.sortedBreakpointsList.length == 0) {
return -1;
}
else {
for (var i = 1; i < this.sortedBreakpointsList.length; i++) {
var prev = this.sortedBreakpointsList[i-1];
var cur = this.sortedBreakpointsList[i];
if (c <= prev)
return -1;
if (cur >= c)
return prev;
}
// final edge case:
var lastElt = this.sortedBreakpointsList[this.sortedBreakpointsList.length - 1];
return (lastElt < c) ? lastElt : -1;
}
}
findNextBreakpoint() {
var c = this.curInstr;
if (this.sortedBreakpointsList.length == 0) {
return -1;
}
// usability hack: if you're currently on a breakpoint, then
// single-step forward to the next execution point, NOT the next
// breakpoint. it's often useful to see what happens when the line
// at a breakpoint executes.
else if ($.inArray(c, this.sortedBreakpointsList) >= 0) {
return c + 1;
}
else {
for (var i = 0; i < this.sortedBreakpointsList.length - 1; i++) {
var cur = this.sortedBreakpointsList[i];
var next = this.sortedBreakpointsList[i+1];
if (c < cur)
return cur;
if (cur <= c && c < next) // subtle
return next;
}
// final edge case:
var lastElt = this.sortedBreakpointsList[this.sortedBreakpointsList.length - 1];
return (lastElt > c) ? lastElt : -1;
}
}
// returns true if action successfully taken
stepForward() {
var myViz = this;
if (myViz.curInstr < myViz.curTrace.length - 1) {
// if there is a next breakpoint, then jump to it ...
if (myViz.sortedBreakpointsList.length > 0) {
var nextBreakpoint = myViz.findNextBreakpoint();
if (nextBreakpoint != -1)
myViz.curInstr = nextBreakpoint;
else
myViz.curInstr += 1; // prevent "getting stuck" on a solitary breakpoint
}
else {
myViz.curInstr += 1;
}
myViz.updateOutput(true);
return true;
}
return false;
}
// returns true if action successfully taken
stepBack() {
var myViz = this;
if (myViz.curInstr > 0) {
// if there is a prev breakpoint, then jump to it ...
if (myViz.sortedBreakpointsList.length > 0) {
var prevBreakpoint = myViz.findPrevBreakpoint();
if (prevBreakpoint != -1)
myViz.curInstr = prevBreakpoint;
else
myViz.curInstr -= 1; // prevent "getting stuck" on a solitary breakpoint
}
else {
myViz.curInstr -= 1;
}
myViz.updateOutput();
return true;
}
return false;
}
// This function is called every time the display needs to be updated
updateOutput(smoothTransition=false) {
if (this.params.hideCode) {
this.updateOutputMini();
}
else {
this.updateOutputFull(smoothTransition);
}
this.outputBox.renderOutput(this.curTrace[this.curInstr].stdout);
this.try_hook("end_updateOutput", {myViz:this});
}
// does a LOT of stuff, called by updateOutput
updateOutputFull(smoothTransition) {
assert(this.curTrace);
assert(!this.params.hideCode);
var myViz = this; // to prevent confusion of 'this' inside of nested functions
// there's no point in re-rendering if this pane isn't even visible in the first place!
if (!myViz.domRoot.is(':visible')) {
return;
}
myViz.updateLineAndExceptionInfo(); // very important to call this before rendering code (argh order dependency)
var prevDataVizHeight = myViz.dataViz.height();
this.codDisplay.updateCodOutput(smoothTransition);
// call the callback if necessary (BEFORE rendering)
if (this.params.updateOutputCallback) {
this.params.updateOutputCallback(this);
}
var totalInstrs = this.curTrace.length;
var isFirstInstr = (this.curInstr == 0);
var isLastInstr = (this.curInstr == (totalInstrs-1));
var msg = "Step " + String(this.curInstr + 1) + " of " + String(totalInstrs-1);
if (isLastInstr) {
if (this.promptForUserInput || this.promptForMouseInput) {
msg = '<b><font color="' + brightRed + '">Enter user input below:</font></b>';
} else if (this.instrLimitReached) {
msg = "Instruction limit reached";
} else {
msg = "Program terminated";
}
}
this.navControls.setVcrControls(msg, isFirstInstr, isLastInstr);
this.navControls.setSliderVal(this.curInstr);
// render error (if applicable):
if (myViz.curLineExceptionMsg) {
if (myViz.curLineExceptionMsg === "Unknown error") {
myViz.navControls.showError('Unknown error: Please email a bug report to philip@pgbovine.net');
} else {
myViz.navControls.showError(myViz.curLineExceptionMsg);
}
} else if (!this.instrLimitReached) { // ugly, I know :/
myViz.navControls.showError(null);
}
// finally, render all of the data structures
this.dataViz.renderDataStructures(this.curInstr);
// call the callback if necessary (AFTER rendering)
if (myViz.dataViz.height() != prevDataVizHeight) {
if (this.params.heightChangeCallback) {
this.params.heightChangeCallback(this);
}
}
if (isLastInstr &&
myViz.params.executeCodeWithRawInputFunc &&
myViz.promptForUserInput) {
this.navControls.showUserInputDiv();
} else {
this.navControls.hideUserInputDiv();
}
} // end of updateOutputFull
updateOutputMini() {
assert(this.params.hideCode);
this.dataViz.renderDataStructures(this.curInstr);
}
renderStep(step) {
assert(0 <= step);
assert(step < this.curTrace.length);
// ignore redundant calls
if (this.curInstr == step) {
return;
}
this.curInstr = step;
this.updateOutput();
}
redrawConnectors() {
this.dataViz.redrawConnectors();
}
// All of the Java frontend code in this function was written by David
// Pritchard and Will Gwozdz, and integrated into pytutor.js by Philip Guo
activateJavaFrontend() {
var prevLine = null;
this.curTrace.forEach((e, i) => {
// ugh the Java backend doesn't attach line numbers to exception
// events, so just take the previous line number as our best guess
if (e.event === 'exception' && !e.line) {
e.line = prevLine;
}
// super hack by Philip that reverses the direction of the stack so
// that it grows DOWN and renders the same way as the Python and JS
// visualizer stacks
if (e.stack_to_render !== undefined) {
e.stack_to_render.reverse();
}
prevLine = e.line;
});
this.add_pytutor_hook(
"renderPrimitiveObject",
function(args) {
var obj = args.obj, d3DomElement = args.d3DomElement;
var typ = typeof obj;
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
});
this.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
});
this.add_pytutor_hook(
"end_updateOutput",
function(args) {
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];
});
this.add_pytutor_hook(
"end_render",
function(args) {
var myViz = args.myViz;
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); // TODO: leaky abstraction with #dataViz
}
myViz.domRoot.find('#'+myViz.generateID('globals_header')).html("Static fields");
});
this.add_pytutor_hook(
"isLinearObject",
function(args) {
var heapObj = args.heapObj;
if (heapObj[0]=='STACK' || heapObj[0]=='QUEUE')
return ['true', 'true'];
return ['false'];
});
this.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++) {
var 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
});
this.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
// VERY important to bind(this) so that when it's called, 'this' is this current object
var old_generateID = ExecutionVisualizer.prototype.generateID.bind(this);
this.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];
});
};
}
// update fields corresponding to the current and previously executed lines
// in the trace so that they can be properly highlighted; must call before
// rendering code output (NB: argh pesky ordering dependency)
updateLineAndExceptionInfo() {
var myViz = this;
var totalInstrs = myViz.curTrace.length;
var isLastInstr = myViz.curInstr === (totalInstrs-1);
myViz.curLineNumber = undefined;
myViz.prevLineNumber = undefined;
myViz.curLineIsReturn = undefined;
myViz.prevLineIsReturn = undefined;
myViz.curLineExceptionMsg = undefined;
/* if instrLimitReached, then treat like a normal non-terminating line */
var isTerminated = (!myViz.instrLimitReached && isLastInstr);
var curLineNumber = null;
var prevLineNumber = null;
var curEntry = myViz.curTrace[myViz.curInstr];
var curIsReturn = (curEntry.event == 'return');
var prevIsReturn = false;
if (myViz.curInstr > 0) {
prevLineNumber = myViz.curTrace[myViz.curInstr - 1].line;
prevIsReturn = (myViz.curTrace[myViz.curInstr - 1].event == 'return');
/* kinda nutsy hack: if the previous line is a return line, don't
highlight it. instead, highlight the line in the enclosing
function that called this one (i.e., the call site). e.g.,:
1. def foo(lst):
2. return len(lst)
3.
4. y = foo([1,2,3])
5. print y
If prevLineNumber is 2 and prevIsReturn, then curLineNumber is
5, since that's the line that executes right after line 2
finishes. However, this looks confusing to the user since what
actually happened here was that the return value of foo was
assigned to y on line 4. I want to have prevLineNumber be line
4 so that it gets highlighted. There's no ideal solution, but I
think that looks more sensible, since line 4 was the previous