[debugger] deep cloning (depth = 3)
authorFedor Indutny <fedor.indutny@gmail.com>
Sat, 10 Sep 2011 11:39:34 +0000 (18:39 +0700)
committerFedor Indutny <fedor.indutny@gmail.com>
Sat, 10 Sep 2011 11:39:34 +0000 (18:39 +0700)
lib/_debugger.js

index 22a9909..9864a2a 100644 (file)
@@ -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);
     });