Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect cycles during development #119

Merged
merged 5 commits into from
Apr 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,18 @@ Immutable(new Square(2), {prototype: Square.prototype}).area();

Beyond [the usual Object fare](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Methods_of_Object_instances), the following methods have been added.

### Stack overflow protection

Currently you can't construct Immutable from an object with circular references. To protect from ugly stack overflows, we provide a simple protection during development. We stop at suspiciously deep stack level and [show an error message][deep].

If your objects are deep, but not circular, you can increase this level from default `64`. For example:

```javascript
Immutable(deepObject, null, 256);
```

[deep]: https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected

### merge

```javascript
Expand Down
17 changes: 15 additions & 2 deletions src/seamless-immutable.js
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@
return makeImmutable(obj, mutatingObjectMethods);
}

function Immutable(obj, options) {
function Immutable(obj, options, stackRemaining) {
if (isImmutable(obj)) {
return obj;
} else if (obj instanceof Array) {
Expand All @@ -512,9 +512,22 @@
instantiatePlainObject : (function() { return Object.create(prototype); });
var clone = instantiateEmptyObject();

if (process.env.NODE_ENV !== "production") {
/*jshint eqnull:true */
if (stackRemaining == null) {
stackRemaining = 64;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

level = level || 0;

if (stackRemaining <= 0) {
throw new ImmutableError("Attempt to construct Immutable from a deeply nested object was detected." +
" Have you tried to wrap an object with circular references (e.g. React element)?" +
" See https://github.com/rtfeldman/seamless-immutable/wiki/Deeply-nested-object-was-detected for details.");
}
stackRemaining -= 1;
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Increment?
level++;


for (var key in obj) {
if (Object.getOwnPropertyDescriptor(obj, key)) {
clone[key] = Immutable(obj[key]);
clone[key] = Immutable(obj[key], undefined, stackRemaining);
}
}

Expand Down
25 changes: 25 additions & 0 deletions test/Immutable.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,31 @@ var getTestUtils = require("./TestUtils.js");
});
});
});

it("detects cycles", function() {
var obj = {};
obj.prop = obj;

if (config.id === 'prod') {
assert.throws(function() { Immutable(obj); }, RangeError);
} else {
assert.throws(function() { Immutable(obj); }, /deeply nested/);
}
});

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert.throws(Immutable.bind(null, obj), config.id === 'prod' ? RangeError : /deeply nested/);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice suggestion, thank you! But I find my version a little easier to follow.


it("can configure stackRemaining", function() {
var mutable = {bottom: true};
_.range(65).forEach(function() {
mutable = {prop: mutable};
});

if (config.id === 'prod') {
TestUtils.assertJsonEqual(mutable, Immutable(mutable));
} else {
assert.throws(function() { Immutable(mutable); }, /deeply nested/);
TestUtils.assertJsonEqual(mutable, Immutable(mutable, null, 66));
}
});
});
});
});