Skip to content
Open
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
5 changes: 5 additions & 0 deletions Source/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,17 @@ var typeOf = this.typeOf = function(item){
};

var instanceOf = this.instanceOf = function(item, object){
var ctors = [];
if (item == null) return false;
var constructor = item.$constructor || item.constructor;
while (constructor){
if (ctors.contains(constructor)) break;
ctors.push(constructor);

if (constructor === object) return true;
constructor = constructor.parent;
}
ctors = null;
/*<ltIE8>*/
if (!item.hasOwnProperty) return false;
/*</ltIE8>*/
Expand Down
32 changes: 32 additions & 0 deletions Specs/Core/Core.js
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,38 @@ describe('instanceOf', function(){
expect(instanceOf(new X, Array)).to.equal(true);
});

it('should not go into infinite loop', function(){
// self reference case
var Foo = function Foo(){
var ctor = this.constructor;
ctor.parent = ctor;
};
expect(instanceOf(new Foo(), Foo)).to.equal(true);
expect(instanceOf(new Foo(), String)).to.equal(false);

// circular reference case
var A, B;
A = function(){this.init();};
B = function(){this.init();};

A.prototype.init = function(){
this.constructor = B;
};
A.prototype.parent = B;

B.prototype.init = function(){
this.constructor = A;
};
B.prototype.parent = A;

expect(instanceOf(new A(), B)).to.equal(true);
expect(instanceOf(new A(), A)).to.equal(true);
expect(instanceOf(new B(), B)).to.equal(true);
expect(instanceOf(new B(), A)).to.equal(true);

expect(instanceOf(new A(), String)).to.equal(false);
});

// todo(ibolmo)
var dit = typeof window != 'undefined' && window.Element && Element.set ? it : xit;
dit('should return true for Element instances', function(){
Expand Down