Skip to content

Commit cc78968

Browse files
author
Philip Guo
committed
added first (inactive) prototype of 2.0 rendering
1 parent 40e34fc commit cc78968

2 files changed

Lines changed: 153 additions & 5 deletions

File tree

‎edu-python.css‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ button.bigBtn {
248248
font-size: 10pt;
249249
}
250250

251+
.retval {
252+
font-size: 10pt;
253+
font-style: italic;
254+
}
255+
251256
table.listTbl {
252257
border: 0px solid black;
253258
background-color: #F5F798;
@@ -423,11 +428,16 @@ div#stack {
423428
}
424429

425430
div.stackFrame {
431+
border-left: 1px solid #999999;
432+
}
433+
434+
div.stackFrame,
435+
div.globalFrame {
426436
background-color: #ffffff;
427437
margin-bottom: 15px;
428438
padding: 2px;
429439
padding-left: 8px;
430-
border-left: 1px solid #999999;
440+
font-size: 12pt;
431441
}
432442

433443
/* apply to the currently selected stack frame (e.g., on mouse hover) */
@@ -453,13 +463,14 @@ div.inactiveStackFrameHeader {
453463
cursor: pointer;
454464
}
455465

456-
div.stackFrame .stackFrameVar {
466+
td.stackFrameVar {
457467
text-align: right;
458468
padding-right: 10px;
469+
min-width: 120px; /* to prevent excess jittering as new variables are added*/
459470
}
460471

461-
div.stackFrame .stackFrameValue {
462-
text-align: right;
472+
td.stackFrameValue {
473+
text-align: left;
463474
}
464475

465476
div.stackFrame .stackFrameVarTable {

‎edu-python.js‎

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,6 @@ function updateOutput() {
240240
function renderDataStructures(curEntry, vizDiv) {
241241
renderDataStructuresVersion1(curEntry, vizDiv);
242242

243-
// Version 2.0 forthcoming!
244243
//renderDataStructuresVersion2(curEntry, vizDiv);
245244
}
246245

@@ -380,6 +379,144 @@ function renderDataStructuresVersion1(curEntry, vizDiv) {
380379
//
381380
// This version was originally created in September 2011
382381
function renderDataStructuresVersion2(curEntry, vizDiv) {
382+
console.log(curEntry);
383+
384+
$(vizDiv).html(''); // CLEAR IT!
385+
386+
// first render the stack (and global vars)
387+
$(vizDiv).append('<div id="stack"></div>');
388+
389+
// render locals on stack:
390+
if (curEntry.stack_locals != undefined) {
391+
$.each(curEntry.stack_locals, function (i, frame) {
392+
var funcName = htmlspecialchars(frame[0]); // might contain '<' or '>' for weird names like <genexpr>
393+
var localVars = frame[1];
394+
395+
// the stackFrame div's id is simply its index ("stack_<index>")
396+
var divClass = (i==0) ? "stackFrame topStackFrame" : "stackFrame";
397+
var divID = "stack_" + i;
398+
$(vizDiv + " #stack").append('<div class="' + divClass + '" id="' + divID + '"></div>');
399+
400+
var headerDivID = "stack_header_" + i;
401+
$(vizDiv + " #stack #" + divID).append('<div id="' + headerDivID + '" class="stackFrameHeader inactiveStackFrameHeader">' + funcName + '</div>');
402+
403+
// render locals in alphabetical order for tidiness:
404+
// TODO: later on, render locals in order of first appearance, for consistency!!!
405+
var orderedVarnames = [];
406+
407+
// use plain ole' iteration rather than jQuery $.each() since
408+
// the latter breaks when a variable is named "length"
409+
for (varname in localVars) {
410+
orderedVarnames.push(varname);
411+
}
412+
orderedVarnames.sort();
413+
414+
415+
/*
416+
<table class="stackFrameVarTable" id="TowerOfHanoi1_table">
417+
<tr>
418+
<td class="stackFrameVar">a</td>
419+
<!-- IE needs the div to be NON-EMPTY in order to properly
420+
render the plumb endpoints!!! -->
421+
<td class="stackFrameValue"><div id="TowerOfHanoi1_a">&nbsp;</div></td>
422+
</tr>
423+
</table>
424+
*/
425+
426+
if (orderedVarnames.length > 0) {
427+
var tableID = divID + '_table';
428+
$(vizDiv + " #stack #" + divID).append('<table class="stackFrameVarTable" id="' + tableID + '"></table>');
429+
430+
var tbl = $(vizDiv + " #" + tableID);
431+
$.each(orderedVarnames, function(i, varname) {
432+
var val = localVars[varname];
433+
tbl.append('<tr><td class="stackFrameVar"></td><td class="stackFrameValue"></td></tr>');
434+
var curTr = tbl.find('tr:last');
435+
if (varname == '__return__') {
436+
curTr.find("td.stackFrameVar").html('<span class="retval">return value</span>');
437+
}
438+
else {
439+
curTr.find("td.stackFrameVar").html(varname);
440+
}
441+
renderData(val, curTr.find("td.stackFrameValue"));
442+
});
443+
}
444+
});
445+
}
446+
447+
448+
// render globals LAST:
449+
$(vizDiv + " #stack").append('<div class="globalFrame" id="globals"><div class="stackFrameHeader inactiveStackFrameHeader">Global variables</div></div>');
450+
451+
var nonEmptyGlobals = false;
452+
var curGlobalFields = {};
453+
if (curEntry.globals != undefined) {
454+
// use plain ole' iteration rather than jQuery $.each() since
455+
// the latter breaks when a variable is named "length"
456+
for (varname in curEntry.globals) {
457+
curGlobalFields[varname] = true;
458+
nonEmptyGlobals = true;
459+
}
460+
}
461+
462+
if (nonEmptyGlobals) {
463+
// render all global variables IN THE ORDER they were created by the program,
464+
// in order to ensure continuity:
465+
var orderedGlobals = []
466+
467+
// iterating over ALL instructions up to curInstr
468+
// (could be SLOW if not for our optimization below)
469+
//
470+
// TODO: this loop still seems like it can be optimized further if necessary
471+
for (var i = 0; i <= curInstr; i++) {
472+
// some entries (like for exceptions) don't have GLOBALS
473+
if (curTrace[i].globals == undefined) continue;
474+
475+
// use plain ole' iteration rather than jQuery $.each() since
476+
// the latter breaks when a variable is named "length"
477+
for (varname in curTrace[i].globals) {
478+
// eliminate duplicates (act as an ordered set)
479+
if ($.inArray(varname, orderedGlobals) == -1) {
480+
orderedGlobals.push(varname);
481+
curGlobalFields[varname] = undefined; // 'unset it'
482+
}
483+
}
484+
485+
var earlyStop = true;
486+
// as an optimization, STOP as soon as you've found everything in curGlobalFields:
487+
for (o in curGlobalFields) {
488+
if (curGlobalFields[o] != undefined) {
489+
earlyStop = false;
490+
break;
491+
}
492+
}
493+
494+
if (earlyStop) {
495+
break;
496+
}
497+
}
498+
499+
500+
$(vizDiv + " #stack #globals").append('<table class="stackFrameVarTable" id="global_table"></table>');
501+
502+
var tbl = $(vizDiv + " #global_table");
503+
// iterate IN ORDER (it's possible that not all vars are in curEntry.globals)
504+
$.each(orderedGlobals, function(i, varname) {
505+
var val = curEntry.globals[varname];
506+
// (use '!==' to do an EXACT match against undefined)
507+
if (val !== undefined) { // might not be defined at this line, which is OKAY!
508+
tbl.append('<tr><td class="stackFrameVar"></td><td class="stackFrameValue"></td></tr>');
509+
var curTr = tbl.find('tr:last');
510+
curTr.find("td.stackFrameVar").html(varname);
511+
renderData(val, curTr.find("td.stackFrameValue"));
512+
}
513+
});
514+
}
515+
516+
517+
// then render the heap
518+
519+
// finally connect stack variables to heap objects via connectors
383520

384521
}
385522

0 commit comments

Comments
 (0)