From db6526f9622258f18f3f66841c3b196164bcef13 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Sat, 10 Sep 2011 18:39:34 +0700 Subject: [PATCH] [debugger] deep cloning (depth = 3) --- lib/_debugger.js | 42 ++++++++++++++++++++++++++++++++---------- 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/_debugger.js b/lib/_debugger.js index 22a9909..9864a2a 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -445,7 +445,7 @@ Client.prototype.step = function(action, count, cb) { }; -Client.prototype.mirrorObject = function(handle, cb) { +Client.prototype.mirrorObject = function(handle, depth, cb) { var self = this; var val; @@ -476,15 +476,19 @@ Client.prototype.mirrorObject = function(handle, cb) { return; } - var mirror; + var mirror, + waiting = 1; + if (handle.className == 'Array') { mirror = []; } else { mirror = {}; } - for (var i = 0; i < handle.properties.length; i++) { - var value = res.body[handle.properties[i].ref]; + + var keyValues = []; + handle.properties.forEach(function(prop, i) { + var value = res.body[prop.ref]; var mirrorValue; if (value) { mirrorValue = value.value ? value.value : value.text; @@ -494,15 +498,33 @@ Client.prototype.mirrorObject = function(handle, cb) { if (Array.isArray(mirror) && - typeof handle.properties[i].name != 'number') { + typeof prop.name != 'number') { // Skip the 'length' property. - continue; + return; } - mirror[handle.properties[i].name] = mirrorValue; - } + keyValues[i] = { + name: prop.name, + value: mirrorValue + }; + if (value.handle && depth > 0) { + waiting++; + self.mirrorObject(value, depth - 1, function(result) { + keyValues[i].value = result; + waitForOthers(); + }); + } + }); - if (cb) cb(mirror); + waitForOthers(); + function waitForOthers() { + if (--waiting === 0 && cb) { + keyValues.forEach(function(kv) { + mirror[kv.name] = kv.value; + }); + cb(mirror); + } + }; }); return; } else if (handle.type === 'function') { @@ -775,7 +797,7 @@ Interface.prototype.debugEval = function(code, context, filename, callback) { return; } - client.mirrorObject(res.body, function(mirror) { + client.mirrorObject(res.body, 3, function(mirror) { callback(null, mirror); self.resume(true); }); -- 2.7.4