Skip to content

Commit 8a611b9

Browse files
author
Philip Guo
committed
implemented first version of environment parent pointer rendering
1 parent 1a3d9f4 commit 8a611b9

1 file changed

Lines changed: 113 additions & 17 deletions

File tree

‎v3/js/pytutor.js‎

Lines changed: 113 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ var curVisualizerID = 1; // global to uniquely identify each ExecutionVisualizer
8585
// disableHeapNesting - if true, then render all heap objects at the top level (i.e., no nested objects)
8686
// codeDivWidth=350, codeDivHeight=400
8787
// drawParentPointers - if true, then draw environment diagram parent pointers for all frames
88+
// WARNING: there are hard-to-debug MEMORY LEAKS associated with activating this option
8889
// updateOutputCallback - function to call (with 'this' as parameter)
8990
// whenever this.updateOutput() is called
9091
// (BEFORE rendering the output display)
@@ -1686,8 +1687,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
16861687
// div contains, say, a "position: relative;" CSS tag
16871688
// (which happens in the IPython Notebook)
16881689
var existingConnectionEndpointIDs = d3.map();
1689-
1690-
myViz.jsPlumbInstance.select().each(function(c) {
1690+
myViz.jsPlumbInstance.select({scope: 'varValuePointer'}).each(function(c) {
16911691
// This is VERY crude, but to prevent multiple redundant HEAP->HEAP
16921692
// connectors from being drawn with the same source and origin, we need to first
16931693
// DELETE ALL existing HEAP->HEAP connections, and then re-render all of
@@ -1701,12 +1701,10 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
17011701
}
17021702
});
17031703

1704-
/*
1705-
existingConnectionEndpointIDs.forEach(function(varID, valueID) {
1706-
console.log('existingConnectionEndpointIDs:', varID, valueID);
1704+
var existingParentPointerConnectionEndpointIDs = d3.map();
1705+
myViz.jsPlumbInstance.select({scope: 'frameParentPointer'}).each(function(c) {
1706+
existingParentPointerConnectionEndpointIDs.set(c.sourceId, c.targetId);
17071707
});
1708-
console.log('---');
1709-
*/
17101708

17111709

17121710
// Heap object rendering phase:
@@ -1726,6 +1724,9 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
17261724
var connectionEndpointIDs = d3.map();
17271725
var heapConnectionEndpointIDs = d3.map(); // subset of connectionEndpointIDs for heap->heap connections
17281726

1727+
// analogous to connectionEndpointIDs, except for environment parent pointers
1728+
var parentPointerConnectionEndpointIDs = d3.map();
1729+
17291730
var heap_pointer_src_id = 1; // increment this to be unique for each heap_pointer_src_*
17301731

17311732

@@ -2265,9 +2266,53 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
22652266
// HTML5 custom data attributes
22662267
.attr('data-frame_id', function(frame, i) {return frame.frame_id;})
22672268
.attr('data-parent_frame_id', function(frame, i) {
2268-
return (frame.parent_frame_id_list.length > 0) ? frame.parent_frame_id_list[0] : null;
2269+
return (frame.parent_frame_id_list.length > 0) ? frame.parent_frame_id_list[0] : null;
2270+
})
2271+
.each(function(frame, i) {
2272+
if (!myViz.drawParentPointers) {
2273+
return;
2274+
}
2275+
// only run if myViz.drawParentPointers is true ...
2276+
2277+
var my_CSS_id = $(this).attr('id');
2278+
2279+
//console.log(my_CSS_id, 'ENTER');
2280+
2281+
// render a parent pointer whose SOURCE node is this frame
2282+
// i.e., connect this frame to p, where this.parent_frame_id == p.frame_id
2283+
// (if this.parent_frame_id is null, then p is the global frame)
2284+
if (frame.parent_frame_id_list.length > 0) {
2285+
var parent_frame_id = frame.parent_frame_id_list[0];
2286+
// tricky turkey!
2287+
// ok this hack just HAPPENS to work by luck ... usually there will only be ONE frame
2288+
// that matches this selector, but sometimes multiple frames match, in which case the
2289+
// FINAL frame wins out (since parentPointerConnectionEndpointIDs is a map where each
2290+
// key can be mapped to only ONE value). it so happens that the final frame winning
2291+
// out looks "desirable" for some of the closure test cases that I've tried. but
2292+
// this code is quite brittle :(
2293+
myViz.domRoot.find('div#stack [data-frame_id=' + parent_frame_id + ']').each(function(i, e) {
2294+
var parent_CSS_id = $(this).attr('id');
2295+
//console.log('connect', my_CSS_id, parent_CSS_id);
2296+
parentPointerConnectionEndpointIDs.set(my_CSS_id, parent_CSS_id);
2297+
});
2298+
}
2299+
else {
2300+
// render a parent pointer to the global frame
2301+
//console.log('connect', my_CSS_id, globalsID);
2302+
parentPointerConnectionEndpointIDs.set(my_CSS_id, globalsID);
2303+
}
2304+
2305+
// tricky turkey: render parent pointers whose TARGET node is this frame.
2306+
// i.e., for all frames f such that f.parent_frame_id == my_frame_id,
2307+
// connect f to this frame.
2308+
// (make sure not to confuse frame IDs with CSS IDs!!!)
2309+
var my_frame_id = frame.frame_id;
2310+
myViz.domRoot.find('div#stack [data-parent_frame_id=' + my_frame_id + ']').each(function(i, e) {
2311+
var child_CSS_id = $(this).attr('id');
2312+
//console.log('connect', child_CSS_id, my_CSS_id);
2313+
parentPointerConnectionEndpointIDs.set(child_CSS_id, my_CSS_id);
2314+
});
22692315
});
2270-
//.each(function(frame, i) {console.log('NEW STACK FRAME', frame.unique_hash);})
22712316

22722317
sfdEnter
22732318
.append('div')
@@ -2319,7 +2364,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
23192364
.attr('id', function(d, i) {
23202365
return myViz.generateID(varnameToCssID(d.frame.unique_hash + '__' + d.varname + '_tr')); // make globally unique (within the page)
23212366
});
2322-
2367+
23232368

23242369
var stackVarTableCells = stackVarTable
23252370
.selectAll('td.stackFrameVar,td.stackFrameValue')
@@ -2345,7 +2390,7 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
23452390
// always delete and re-render the stack var ...
23462391
// NB: trying to cache and compare the old value using,
23472392
// say -- $(this).attr('data-curvalue', valStringRepr) -- leads to
2348-
// a mysterious and killer memory leak that I can't figure out yet
2393+
// a mysterious and killer memory leak that I can't figure out yet
23492394
$(this).empty();
23502395

23512396
// make sure varname and frame.unique_hash don't contain any weird
@@ -2398,6 +2443,18 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
23982443
existingConnectionEndpointIDs.remove($(sp).attr('id'));
23992444
});
24002445

2446+
var my_CSS_id = $(this).attr('id');
2447+
2448+
//console.log(my_CSS_id, 'EXIT');
2449+
2450+
// Remove all pointers where either the source or destination end is my_CSS_id
2451+
existingParentPointerConnectionEndpointIDs.forEach(function(k, v) {
2452+
if (k == my_CSS_id || v == my_CSS_id) {
2453+
//console.log('remove EPP', k, v);
2454+
existingParentPointerConnectionEndpointIDs.remove(k);
2455+
}
2456+
});
2457+
24012458
$(this).empty(); // crucial for garbage collecting jsPlumb connectors!
24022459
})
24032460
.remove();
@@ -2411,15 +2468,54 @@ ExecutionVisualizer.prototype.renderDataStructures = function() {
24112468
// will properly garbage collect the connectors
24122469
myViz.jsPlumbInstance.reset();
24132470

2471+
2472+
// use jsPlumb scopes to keep the different kinds of pointers separated
2473+
function renderVarValueConnector(varID, valueID) {
2474+
myViz.jsPlumbInstance.connect({source: varID, target: valueID, scope: 'varValuePointer'});
2475+
}
2476+
2477+
2478+
var totalParentPointersRendered = 0;
2479+
2480+
function renderParentPointerConnector(srcID, dstID) {
2481+
// SUPER-DUPER-ugly hack since I can't figure out a cleaner solution for now:
2482+
// if either srcID or dstID no longer exists, then SKIP rendering ...
2483+
//
2484+
// Note that until we solve this problem once and for all and no longer need this
2485+
// hacky guard, there will likely be MEMORY LEAKS associated with rendering parent pointers
2486+
if ((myViz.domRoot.find('#' + srcID).length == 0) ||
2487+
(myViz.domRoot.find('#' + dstID).length == 0)) {
2488+
return;
2489+
}
2490+
2491+
//console.log('renderParentPointerConnector:', srcID, dstID);
2492+
2493+
myViz.jsPlumbInstance.connect({source: srcID, target: dstID,
2494+
anchors: ["LeftMiddle", "LeftMiddle"],
2495+
2496+
// 'horizontally offset' the parent pointers up so that they don't look as ugly ...
2497+
//connector: ["Flowchart", { stub: 9 + (6 * (totalParentPointersRendered + 1)) }],
2498+
2499+
// actually let's try a bezier curve ...
2500+
connector: [ "Bezier", { curviness: 45 }],
2501+
2502+
endpoint: ["Dot", {radius: 4}],
2503+
//hoverPaintStyle: {lineWidth: 1, strokeStyle: connectorInactiveColor}, // no hover colors
2504+
scope: 'frameParentPointer'});
2505+
totalParentPointersRendered++;
2506+
}
2507+
24142508
// re-render existing connectors ...
2415-
existingConnectionEndpointIDs.forEach(function(varID, valueID) {
2416-
myViz.jsPlumbInstance.connect({source: varID, target: valueID});
2417-
});
2509+
existingConnectionEndpointIDs.forEach(renderVarValueConnector);
2510+
if (myViz.drawParentPointers) {
2511+
existingParentPointerConnectionEndpointIDs.forEach(renderParentPointerConnector);
2512+
}
24182513

24192514
// finally add all the NEW connectors that have arisen in this call to renderDataStructures
2420-
connectionEndpointIDs.forEach(function(varID, valueID) {
2421-
myViz.jsPlumbInstance.connect({source: varID, target: valueID});
2422-
});
2515+
connectionEndpointIDs.forEach(renderVarValueConnector);
2516+
if (myViz.drawParentPointers) {
2517+
parentPointerConnectionEndpointIDs.forEach(renderParentPointerConnector);
2518+
}
24232519

24242520
/*
24252521
myViz.jsPlumbInstance.select().each(function(c) {

0 commit comments

Comments
 (0)