Browse Source

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
     }
    }
v0.7.4-release
Benjamin Thomas 15 years ago
committed by Ryan Dahl
parent
commit
49cd1bbf84
  1. 8
      src/node.js
  2. 2
      test/mjsunit/test-process-mixin.js

8
src/node.js

@ -135,18 +135,20 @@ process.mixin = function() {
var d = Object.getOwnPropertyDescriptor(source, k); var d = Object.getOwnPropertyDescriptor(source, k);
if (d.get) { if (d.get) {
target.__defineGetter__(k, d.get); target.__defineGetter__(k, d.get);
if (d.set) if (d.set) {
target.__defineSetter__(k, d.set); target.__defineSetter__(k, d.set);
} }
}
else { else {
// Prevent never-ending loop // Prevent never-ending loop
if (target === d.value) if (target === d.value) {
continue; continue;
}
if (deep && d.value && typeof d.value === "object") { if (deep && d.value && typeof d.value === "object") {
target[k] = process.mixin(deep, target[k] = process.mixin(deep,
// Never move original objects, clone them // Never move original objects, clone them
source || (d.value.length != null ? [] : {}) source[k] || (d.value.length != null ? [] : {})
, d.value); , d.value);
} }
else { else {

2
test/mjsunit/test-process-mixin.js

@ -13,7 +13,7 @@ var fakeDomElement = {deep: {nodeType: 4}};
target = {}; target = {};
process.mixin(true, target, fakeDomElement); process.mixin(true, target, fakeDomElement);
assert.notStrictEqual(target.deep, fakeDomElement.deep); assert.deepEqual(target.deep, fakeDomElement.deep);
var objectWithUndefinedValue = {foo: undefined}; var objectWithUndefinedValue = {foo: undefined};
target = {}; target = {};

Loading…
Cancel
Save