Commit 36e72aed authored by arv's avatar arv Committed by Commit bot

ES6 collections: Fix order of constructor logic

The adder should be gotten before the iterator.

Motivation: Once this is done we should be able to use a for-of loop
instead which leads to cleaner code and correct behavior once the
for-of loop correctly supports abrupt completion.

BUG=None
LOG=N
R=adamk

Review URL: https://codereview.chromium.org/949933002

Cr-Commit-Position: refs/heads/master@{#26803}
parent 74c0cdbf
......@@ -23,11 +23,11 @@ function SetConstructor(iterable) {
var iter, adder;
if (!IS_NULL_OR_UNDEFINED(iterable)) {
iter = GetIterator(iterable);
adder = this.add;
if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['add', this]);
}
iter = GetIterator(iterable);
}
%_SetInitialize(this);
......@@ -163,11 +163,11 @@ function MapConstructor(iterable) {
var iter, adder;
if (!IS_NULL_OR_UNDEFINED(iterable)) {
iter = GetIterator(iterable);
adder = this.set;
if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['set', this]);
}
iter = GetIterator(iterable);
}
%_MapInitialize(this);
......
......@@ -23,11 +23,11 @@ function WeakMapConstructor(iterable) {
var iter, adder;
if (!IS_NULL_OR_UNDEFINED(iterable)) {
iter = GetIterator(iterable);
adder = this.set;
if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['set', this]);
}
iter = GetIterator(iterable);
}
%WeakCollectionInitialize(this);
......@@ -130,11 +130,11 @@ function WeakSetConstructor(iterable) {
var iter, adder;
if (!IS_NULL_OR_UNDEFINED(iterable)) {
iter = GetIterator(iterable);
adder = this.add;
if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['add', this]);
}
iter = GetIterator(iterable);
}
%WeakCollectionInitialize(this);
......
......@@ -1408,3 +1408,38 @@ TestCollectionToString(Map);
TestCollectionToString(Set);
TestCollectionToString(WeakMap);
TestCollectionToString(WeakSet);
function TestConstructorOrderOfAdderIterator(ctor, adderName) {
var iterable = new Map();
iterable.set({}, {});
iterable.set({}, {});
var iterableFunction = iterable[Symbol.iterator];
Object.defineProperty(iterable, Symbol.iterator, {
get: function() {
log += 'iterator';
return iterableFunction;
}
});
var log = '';
var adderFunction = ctor.prototype[adderName];
Object.defineProperty(ctor.prototype, adderName, {
get: function() {
log += adderName;
return adderFunction;
}
});
new ctor(iterable);
assertEquals(adderName + 'iterator', log);
Object.defineProperty(ctor.prototype, adderName, {
value: adderFunction
});
}
TestConstructorOrderOfAdderIterator(Map, 'set');
TestConstructorOrderOfAdderIterator(Set, 'add');
TestConstructorOrderOfAdderIterator(WeakMap, 'set');
TestConstructorOrderOfAdderIterator(WeakSet, 'add');
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment