Skip to content

Instantly share code, notes, and snippets.

@bentomas
Created February 22, 2010 06:16
Show Gist options
  • Save bentomas/310857 to your computer and use it in GitHub Desktop.
Save bentomas/310857 to your computer and use it in GitHub Desktop.
From 13ca65a21232b1f14b4b2213803f48884b00bb96 Mon Sep 17 00:00:00 2001
From: Benjamin Thomas <benjamin@benjaminthomas.org>
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 415df7b..e4dd33f 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 502c229..cf5fe6c 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 = {};
--
1.6.3.3
var sys = require("sys");
var obj = {
one: 1,
two: 2,
three: {
value: 3
}
};
sys.p(process.mixin(true, {}, obj));
/*
Expected:
{
"one": 1,
"two": 2,
"three": {
"value": 3
}
}
Actual:
{
"two": 2,
"three": {
"one": 1,
"two": 2,
"three": {
"value": 3
},
"value": 3
},
"one": 1
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment