From 49cd1bbf84271f5a59954a176fc79a99814afa8b Mon Sep 17 00:00:00 2001 From: Benjamin Thomas Date: Mon, 22 Feb 2010 06:47:15 +0000 Subject: [PATCH] Fix bug in process.mixin where deep copies would not work at all. Before, doing this: var sys = require("sys"); var obj = { one: 1, two: 2, three: { value: 3 } }; sys.p(process.mixin(true, {}, obj)); Would output this: { "two": 2, "three": { "one": 1, "two": 2, "three": { "value": 3 }, "value": 3 }, "one": 1 } When it should have outputed this: { "one": 1, "two": 2, "three": { "value": 3 } } --- src/node.js | 10 ++++++---- test/mjsunit/test-process-mixin.js | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/node.js b/src/node.js index 57c7a06ea7..b9419510db 100644 --- a/src/node.js +++ b/src/node.js @@ -135,18 +135,20 @@ process.mixin = function() { var d = Object.getOwnPropertyDescriptor(source, k); if (d.get) { target.__defineGetter__(k, d.get); - if (d.set) + if (d.set) { target.__defineSetter__(k, d.set); + } } else { // Prevent never-ending loop - if (target === d.value) + if (target === d.value) { continue; - + } + if (deep && d.value && typeof d.value === "object") { target[k] = process.mixin(deep, // Never move original objects, clone them - source || (d.value.length != null ? [] : {}) + source[k] || (d.value.length != null ? [] : {}) , d.value); } else { diff --git a/test/mjsunit/test-process-mixin.js b/test/mjsunit/test-process-mixin.js index 502c229b35..cf5fe6c7f4 100644 --- a/test/mjsunit/test-process-mixin.js +++ b/test/mjsunit/test-process-mixin.js @@ -13,7 +13,7 @@ var fakeDomElement = {deep: {nodeType: 4}}; target = {}; process.mixin(true, target, fakeDomElement); -assert.notStrictEqual(target.deep, fakeDomElement.deep); +assert.deepEqual(target.deep, fakeDomElement.deep); var objectWithUndefinedValue = {foo: undefined}; target = {};