Skip to content

Commit 79f831d

Browse files
committed
[Fix] Ensure Map and Set do not have an own constructor property.
Per es-shims/es5-shim#368 (comment)
1 parent 42c0217 commit 79f831d

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

es6-shim.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2965,11 +2965,12 @@
29652965
if (arguments.length > 0) {
29662966
addIterableToMap(Map, m, arguments[0]);
29672967
}
2968+
delete m.constructor;
29682969
Object.setPrototypeOf(m, globals.Map.prototype);
2969-
defineProperty(m, 'constructor', Map, true);
29702970
return m;
29712971
};
29722972
globals.Map.prototype = create(OrigMapNoArgs.prototype);
2973+
defineProperty(globals.Map.prototype, 'constructor', globals.Map, true);
29732974
Value.preserveToString(globals.Map, OrigMapNoArgs);
29742975
}
29752976
var testMap = new Map();
@@ -3053,11 +3054,12 @@
30533054
if (arguments.length > 0) {
30543055
addIterableToMap(Map, m, arguments[0]);
30553056
}
3057+
delete m.constructor;
30563058
Object.setPrototypeOf(m, Map.prototype);
3057-
defineProperty(m, 'constructor', Map, true);
30583059
return m;
30593060
};
30603061
globals.Map.prototype = OrigMap.prototype;
3062+
defineProperty(globals.Map.prototype, 'constructor', globals.Map, true);
30613063
Value.preserveToString(globals.Map, OrigMap);
30623064
}
30633065
var setSupportsSubclassing = supportsSubclassing(globals.Set, function (S) {
@@ -3083,11 +3085,12 @@
30833085
if (arguments.length > 0) {
30843086
addIterableToSet(Set, s, arguments[0]);
30853087
}
3088+
delete s.constructor;
30863089
Object.setPrototypeOf(s, Set.prototype);
3087-
defineProperty(s, 'constructor', Set, true);
30883090
return s;
30893091
};
30903092
globals.Set.prototype = OrigSet.prototype;
3093+
defineProperty(globals.Set.prototype, 'constructor', globals.Set, true);
30913094
Value.preserveToString(globals.Set, OrigSet);
30923095
}
30933096
var mapIterationThrowsStopIterator = !valueOrFalseIfThrows(function () {

test/collections.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,12 @@ describe('Collections', function () {
251251
expectNotEnumerable(new Map());
252252
});
253253

254+
it('should not have an own constructor', function () {
255+
var m = new Map();
256+
expect(m).not.to.haveOwnPropertyDescriptor('constructor');
257+
expect(m.constructor).to.equal(Map);
258+
});
259+
254260
it('should allow common ecmascript idioms', function () {
255261
expect(map).to.be.an.instanceOf(Map);
256262
expect(typeof Map.prototype.get).to.equal('function');
@@ -259,6 +265,10 @@ describe('Collections', function () {
259265
expect(typeof Map.prototype['delete']).to.equal('function');
260266
});
261267

268+
it('should have a unique constructor', function () {
269+
expect(Map.prototype).to.not.equal(Object.prototype);
270+
});
271+
262272
describe('#clear()', function () {
263273
ifFunctionsHaveNamesIt('has the right name', function () {
264274
expect(Map.prototype.clear).to.have.property('name', 'clear');
@@ -351,10 +361,6 @@ describe('Collections', function () {
351361
});
352362
});
353363

354-
it('should has unique constructor', function () {
355-
expect(Map.prototype).to.not.equal(Object.prototype);
356-
});
357-
358364
describe('#size', function () {
359365
it('throws TypeError when accessed directly', function () {
360366
// see https://github.com/paulmillr/es6-shim/issues/176
@@ -913,14 +919,20 @@ describe('Collections', function () {
913919
expectNotEnumerable(new Set());
914920
});
915921

922+
it('should not have an own constructor', function () {
923+
var s = new Set();
924+
expect(s).not.to.haveOwnPropertyDescriptor('constructor');
925+
expect(s.constructor).to.equal(Set);
926+
});
927+
916928
it('should allow common ecmascript idioms', function () {
917929
expect(set instanceof Set).to.equal(true);
918930
expect(typeof Set.prototype.add).to.equal('function');
919931
expect(typeof Set.prototype.has).to.equal('function');
920932
expect(typeof Set.prototype['delete']).to.equal('function');
921933
});
922934

923-
it('should has unique constructor', function () {
935+
it('should have a unique constructor', function () {
924936
expect(Set.prototype).to.not.equal(Object.prototype);
925937
});
926938

0 commit comments

Comments
 (0)