Permalink
Browse files

print heap

  • Loading branch information...
1 parent fccc4bb commit 43e8d39636d36620fbfcaaf201e24264ee1151f0 @zdwalter committed Mar 8, 2012
Showing with 142 additions and 64 deletions.
  1. +76 −61 public/javascripts/debug.js
  2. +28 −0 public/javascripts/examples/sumList.js
  3. +38 −3 public/javascripts/learnjs.js
View
@@ -29,9 +29,9 @@ var _typeof = function(obj) {
if (type === 'number' || type === 'string' || type == 'function' || type == 'boolean') {
return type;
}
- var s = JSON.stringify(obj);
- if (s[0] === '[') return 'LIST';
- if (s[0] === '{') return 'DICT';
+ if (obj instanceof Array) return 'LIST';
+ var s = Object.prototype.toString.call(obj);
+ if (s === '[object Object]') return 'DICT';
return 'NONE';
}
@@ -71,70 +71,71 @@ function listener(event, exec_state, event_data, data) {
var globals_list = [];
var globals_dict = globals_list;
var globals_function = globals_list;
- for (var _key in globals_) {
- if (globals_.hasOwnProperty(_key) && _globals_internals.indexOf(_key) < 0) {
- var _value = globals_[_key];
- var _type = _typeof(_value);
- //_print(_type);
- if (_type === 'number' || _type === 'string' || _type === 'boolean') {
- globals[_key] = _value;
- }
- if (_type === 'LIST') {
- var _id = globals_list.indexOf(_value);
- if (_id < 0) {
- _id = globals_list.push(_value);
- }
- else {
- _id++;
- }
- //_print(_id);
- var _copy = ['LIST',_id].concat(_value);
- globals[_key] = _copy;
- }
- if (_type === 'DICT') {
- var _id = globals_dict.indexOf(_value);
- if (_id < 0) {
- _id = globals_dict.push(_value);
- }
- else {
- _id++;
- }
- //_print(_id);
- var _copy = ['DICT',_id];
- for (var _j in _value) {
- if (_value.hasOwnProperty(_j)) {
- var _v = _value[_j];
- _copy.push([_j, _v]);
- }
- }
- globals[_key] = _copy;
+ function globals_parse(_value) {
+ var _type = _typeof(_value);
+ if (_type === 'number' || _type === 'string' || _type === 'boolean') {
+ return _value;
+ }
+ if (_type === 'LIST') {
+ var _id = globals_list.indexOf(_value);
+ if (_id < 0) {
+ _id = globals_list.push(_value);
}
- if (_type === 'function') {
- var _id = globals_function.indexOf(_value);
- if (_id < 0) {
- _id = globals_function.push(_value);
- }
- else {
- _id++;
+ else {
+ _id++;
+ }
+ //_print(_id);
+ var _copy = ['LIST',_id];
+ for (var _i=0; _i < _value.length; _i++) {
+ var _v = _value[_i];
+ _copy.push(globals_parse(_v));
+ }
+ return _copy;
+ }
+ if (_type === 'DICT') {
+ var _id = globals_dict.indexOf(_value);
+ if (_id < 0) {
+ _id = globals_dict.push(_value);
+ }
+ else {
+ _id++;
+ }
+ //_print(_id);
+ var _copy = ['DICT',_id];
+ for (var _j in _value) {
+ if (_value.hasOwnProperty(_j)) {
+ var _v = globals_parse(_value[_j]);
+ _copy.push([_j, _v]);
}
- globals[_key] = ['function',_id, '<function '+_key+'>'];
}
+ return _copy;
+ }
+ if (_type === 'function') {
+ var _id = globals_function.indexOf(_value);
+ if (_id < 0) {
+ _id = globals_function.push(_value);
+ }
+ else {
+ _id++;
+ }
+ return ['function',_id, '<function '+_key+'>'];
+ }
+
+ }
+ for (var _key in globals_) {
+ if (globals_.hasOwnProperty(_key) && _globals_internals.indexOf(_key) < 0) {
+ var _value = globals_[_key];
+ globals[_key] = globals_parse(_value);
}
}
//_print(JSON.stringify(globals));
//_print(JSON.stringify(details_));
var local = 9;
var locals = {};
- while(local+1 < details_.length) {
- var _key = details_[local];
- if (_key === null || typeof _key === 'undefined') {
- _key = 'anonymous';
- }
- var _value = details_[local+1];
- local += 2;
+ function locals_parse(_value) {
var _type = _typeof(_value);
if (_type === 'number' || _type === 'string' || _type === 'boolean' || _type === 'undefined' || _type === null) {
- locals[_key] = _value;
+ return _value;
}
if (_type === 'LIST') {
var _id = globals_list.indexOf(_value);
@@ -145,8 +146,12 @@ function listener(event, exec_state, event_data, data) {
_id++;
}
//_print(_id);
- var _copy = ['LIST',_id].concat(_value);
- locals[_key] = _copy;
+ var _copy = ['LIST',_id];
+ for (var _i=0; _i < _value.length; _i++) {
+ var _v = _value[_i];
+ _copy.push(locals_parse(_v));
+ }
+ return _copy;
}
if (_type === 'DICT') {
var _id = globals_dict.indexOf(_value);
@@ -160,11 +165,11 @@ function listener(event, exec_state, event_data, data) {
var _copy = ['DICT',_id];
for (var _j in _value) {
if (_value.hasOwnProperty(_j)) {
- var _v = _value[_j];
+ var _v = locals_parse(_value[_j]);
_copy.push([_j, _v]);
}
}
- locals[_key] = _copy;
+ return _copy;
}
if (_type === 'function') {
var _id = globals_function.indexOf(_value);
@@ -174,8 +179,18 @@ function listener(event, exec_state, event_data, data) {
else {
_id++;
}
- locals[_key] = ['function',_id, '<function '+_key+'>'];
+ return ['function',_id, '<function '+_key+'>'];
+ }
+
+ }
+ while(local+1 < details_.length) {
+ var _key = details_[local];
+ if (_key === null || typeof _key === 'undefined') {
+ _key = 'anonymous';
}
+ var _value = details_[local+1];
+ local += 2;
+ locals[_key] = locals_parse(_value);
}
var step = {
event: "step_line",
@@ -0,0 +1,28 @@
+// Tutorial code from Prof. Peter Wentworth
+// Rhodes University, South Africa (http://www.ru.ac.za/)
+
+sumList = function(xs){
+ /*
+ Sum a list that can contain nested lists.
+ Precondition: All leaf elements are numbers.
+ */
+ var sum = 0
+ for (var i in xs) {
+ var e = xs[i];
+ if (Object.prototype.toString.call(e) === '[object Array]') {
+ print("Calling sumList("+JSON.stringify(e)+") recursively")
+ var v = sumList(e)
+ print("sumList("+JSON.stringify(e)+") returned "+v)
+ sum += v
+ }
+ else
+ sum += e
+ }
+ return sum
+}
+
+
+testData = [10, [20, 30, [40], 50], 60]
+print("Calling sumList("+JSON.stringify(testData)+")")
+result = sumList(testData)
+print("Final sum of all numbers in initial list is "+result)
@@ -389,10 +389,10 @@ function renderDataStructures(curEntry, vizDiv) {
var nonEmptyLocals = false;
var curLocalFields = {};
- if (curEntry.globals != undefined) {
+ if (curEntry.locals != undefined) {
// use plain ole' iteration rather than jQuery $.each() since
// the latter breaks when a variable is named "length"
- for (varname in curEntry.globals) {
+ for (varname in curEntry.locals) {
curLocalFields[varname] = true;
nonEmptyLocals = true;
}
@@ -482,7 +482,7 @@ function renderDataStructures(curEntry, vizDiv) {
}
function renderLocals() {
- // render global variables:
+ // render locals variables:
if (orderedLocals.length > 0) {
$(vizDiv + " #stack").append('<div class="stackFrame" id="locals"><div id="locals_header" class="stackFrameHeader inactiveStackFrameHeader">Local variables</div></div>');
@@ -606,6 +606,13 @@ function renderDataStructures(curEntry, vizDiv) {
if (stackGrowsDown) {
renderGlobals();
renderLocals();
+ //if (curEntry.locals) {
+ // for (var i in curEntry.locals) {
+ // if (curEntry.locals.hasOwnProperty(i)) {
+ // renderStackFrame(i);
+ // }
+ // }
+ //}
if (curEntry.stack_locals) {
for (var i = curEntry.stack_locals.length - 1; i >= 0; i--) {
var frame = curEntry.stack_locals[i];
@@ -620,7 +627,15 @@ function renderDataStructures(curEntry, vizDiv) {
renderStackFrame(frame);
}
}
+ //if (curEntry.locals) {
+ // for (var i in curEntry.locals) {
+ // if (curEntry.locals.hasOwnProperty(i)) {
+ // renderStackFrame(i);
+ // }
+ // }
+ //}
renderLocals();
+ renderGlobals();
}
@@ -669,6 +684,16 @@ function renderDataStructures(curEntry, vizDiv) {
}
});
+ $.each(orderedLocals, function(i, varname) {
+ var val = curEntry.locals[varname];
+
+ // primitive types are already rendered in the stack
+ if (!isPrimitiveType(val)) {
+ renderHeapObject(val, true); // APPEND
+ }
+ });
+
+
if (curEntry.stack_locals) {
$.each(curEntry.stack_locals, function(i, frame) {
var localVars = frame[1];
@@ -708,6 +733,16 @@ function renderDataStructures(curEntry, vizDiv) {
}
}
+ for (var i = orderedLocals.length - 1; i >= 0; i--) {
+ var varname = orderedLocals[i];
+ var val = curEntry.locals[varname];
+
+ // primitive types are already rendered in the stack
+ if (!isPrimitiveType(val)) {
+ renderHeapObject(val, false); // PREPEND
+ }
+ }
+
if (curEntry.stack_locals) {
// go BACKWARDS
for (var i = curEntry.stack_locals.length - 1; i >= 0; i--) {

0 comments on commit 43e8d39

Please sign in to comment.