Skip to content

Commit

Permalink
[Fix] Ensure Map and Set do not have an own constructor property.
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 13, 2015
1 parent 42c0217 commit 79f831d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
9 changes: 6 additions & 3 deletions es6-shim.js
Expand Up @@ -2965,11 +2965,12 @@
if (arguments.length > 0) {
addIterableToMap(Map, m, arguments[0]);
}
delete m.constructor;
Object.setPrototypeOf(m, globals.Map.prototype);
defineProperty(m, 'constructor', Map, true);
return m;
};
globals.Map.prototype = create(OrigMapNoArgs.prototype);
defineProperty(globals.Map.prototype, 'constructor', globals.Map, true);
Value.preserveToString(globals.Map, OrigMapNoArgs);
}
var testMap = new Map();
Expand Down Expand Up @@ -3053,11 +3054,12 @@
if (arguments.length > 0) {
addIterableToMap(Map, m, arguments[0]);
}
delete m.constructor;
Object.setPrototypeOf(m, Map.prototype);
defineProperty(m, 'constructor', Map, true);
return m;
};
globals.Map.prototype = OrigMap.prototype;
defineProperty(globals.Map.prototype, 'constructor', globals.Map, true);
Value.preserveToString(globals.Map, OrigMap);
}
var setSupportsSubclassing = supportsSubclassing(globals.Set, function (S) {
Expand All @@ -3083,11 +3085,12 @@
if (arguments.length > 0) {
addIterableToSet(Set, s, arguments[0]);
}
delete s.constructor;
Object.setPrototypeOf(s, Set.prototype);
defineProperty(s, 'constructor', Set, true);
return s;
};
globals.Set.prototype = OrigSet.prototype;
defineProperty(globals.Set.prototype, 'constructor', globals.Set, true);
Value.preserveToString(globals.Set, OrigSet);
}
var mapIterationThrowsStopIterator = !valueOrFalseIfThrows(function () {
Expand Down
22 changes: 17 additions & 5 deletions test/collections.js
Expand Up @@ -251,6 +251,12 @@ describe('Collections', function () {
expectNotEnumerable(new Map());
});

it('should not have an own constructor', function () {
var m = new Map();
expect(m).not.to.haveOwnPropertyDescriptor('constructor');
expect(m.constructor).to.equal(Map);
});

it('should allow common ecmascript idioms', function () {
expect(map).to.be.an.instanceOf(Map);
expect(typeof Map.prototype.get).to.equal('function');
Expand All @@ -259,6 +265,10 @@ describe('Collections', function () {
expect(typeof Map.prototype['delete']).to.equal('function');
});

it('should have a unique constructor', function () {
expect(Map.prototype).to.not.equal(Object.prototype);
});

describe('#clear()', function () {
ifFunctionsHaveNamesIt('has the right name', function () {
expect(Map.prototype.clear).to.have.property('name', 'clear');
Expand Down Expand Up @@ -351,10 +361,6 @@ describe('Collections', function () {
});
});

it('should has unique constructor', function () {
expect(Map.prototype).to.not.equal(Object.prototype);
});

describe('#size', function () {
it('throws TypeError when accessed directly', function () {
// see https://github.com/paulmillr/es6-shim/issues/176
Expand Down Expand Up @@ -913,14 +919,20 @@ describe('Collections', function () {
expectNotEnumerable(new Set());
});

it('should not have an own constructor', function () {
var s = new Set();
expect(s).not.to.haveOwnPropertyDescriptor('constructor');
expect(s.constructor).to.equal(Set);
});

it('should allow common ecmascript idioms', function () {
expect(set instanceof Set).to.equal(true);
expect(typeof Set.prototype.add).to.equal('function');
expect(typeof Set.prototype.has).to.equal('function');
expect(typeof Set.prototype['delete']).to.equal('function');
});

it('should has unique constructor', function () {
it('should have a unique constructor', function () {
expect(Set.prototype).to.not.equal(Object.prototype);
});

Expand Down

0 comments on commit 79f831d

Please sign in to comment.