Skip to content

Commit 28b46d0

Browse files
committed
added frontend support for rendering python default arguments
1 parent 5461d71 commit 28b46d0

1 file changed

Lines changed: 33 additions & 4 deletions

File tree

‎v5-unity/js/pytutor.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1447,11 +1447,23 @@ class DataVisualizer {
14471447
}
14481448
});
14491449
}
1450-
else if (heapObj[0] == 'JS_FUNCTION') {
1450+
else if (heapObj[0] == 'FUNCTION' || heapObj[0] == 'JS_FUNCTION') {
1451+
// a Python function object has an optional element at index=3
1452+
// (zero-indexed) that represents default arg names/values
1453+
//
14511454
// a JavaScript function object has funcProperties that we should
14521455
// recurse into in order to precompute their layouts
1453-
assert(heapObj.length == 5);
1454-
var funcProperties = heapObj[3]; // either null or a non-empty list of key-value pairs
1456+
var funcProperties = null;
1457+
// traverse into default argument values to precompute their
1458+
// layouts, if applicable
1459+
if (heapObj[0] == 'FUNCTION' && heapObj.length > 3) {
1460+
funcProperties = heapObj[3];
1461+
}
1462+
if (heapObj[0] == 'JS_FUNCTION') {
1463+
assert(heapObj.length == 5);
1464+
funcProperties = heapObj[3];
1465+
}
1466+
14551467
if (funcProperties) {
14561468
assert(funcProperties.length > 0);
14571469
$.each(funcProperties, function(ind, kvPair) {
@@ -2936,7 +2948,7 @@ class DataVisualizer {
29362948
d3DomElement.append('<table class="customObjTbl"><tr><td class="customObjElt">' + strRepr + '</td></tr></table>');
29372949
}
29382950
else if (obj[0] == 'FUNCTION') {
2939-
assert(obj.length == 3);
2951+
assert(obj.length == 3 || obj.length == 4);
29402952

29412953
// pretty-print lambdas and display other weird characters:
29422954
var funcName = htmlspecialchars(obj[1]).replace('&lt;lambda&gt;', '\u03bb');
@@ -2957,6 +2969,23 @@ class DataVisualizer {
29572969
else {
29582970
d3DomElement.append('<div class="funcObj">' + funcPrefix + ' ' + funcName + '</div>');
29592971
}
2972+
2973+
// render default argument names/values (see ../pg_encoder.py)
2974+
if (obj.length > 3) {
2975+
var funcProperties = obj[3];
2976+
assert(funcProperties.length > 0);
2977+
d3DomElement.append('<div class="typeLabel" style="margin-top: 3px;">default arguments:</div>');
2978+
d3DomElement.append('<table class="instTbl"></table>');
2979+
var tbl = d3DomElement.children('table');
2980+
$.each(funcProperties, function(ind, kvPair) {
2981+
tbl.append('<tr class="instEntry"><td class="instKey"></td><td class="instVal"></td></tr>');
2982+
var newRow = tbl.find('tr:last');
2983+
var keyTd = newRow.find('td:first');
2984+
var valTd = newRow.find('td:last');
2985+
keyTd.append('<span class="keyObj">' + htmlspecialchars(kvPair[0]) + '</span>');
2986+
myViz.renderNestedObject(kvPair[1], stepNum, valTd);
2987+
});
2988+
}
29602989
}
29612990
else if (obj[0] == 'JS_FUNCTION') { /* TODO: refactor me */
29622991
// JavaScript function

0 commit comments

Comments
 (0)