Commit 8ad35cb2 authored by arv's avatar arv Committed by Commit bot

Use for-of loops in collection constructors

This is to reduce code duplication but also to get the correct
behavior when we make for-of handle abrupt completion correctly.

BUG=None
LOG=N
R=adamk

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

Cr-Commit-Position: refs/heads/master@{#26834}
parent bf3691ae
...@@ -20,26 +20,17 @@ function SetConstructor(iterable) { ...@@ -20,26 +20,17 @@ function SetConstructor(iterable) {
throw MakeTypeError('constructor_not_function', ['Set']); throw MakeTypeError('constructor_not_function', ['Set']);
} }
var iter, adder; %_SetInitialize(this);
if (!IS_NULL_OR_UNDEFINED(iterable)) { if (!IS_NULL_OR_UNDEFINED(iterable)) {
adder = this.add; var adder = this.add;
if (!IS_SPEC_FUNCTION(adder)) { if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['add', this]); throw MakeTypeError('property_not_function', ['add', this]);
} }
iter = GetIterator(iterable);
}
%_SetInitialize(this);
if (IS_UNDEFINED(iter)) return;
var next, done; for (var value of iterable) {
while (!(next = iter.next()).done) { %_CallFunction(this, value, adder);
if (!IS_SPEC_OBJECT(next)) {
throw MakeTypeError('iterator_result_not_an_object', [next]);
} }
%_CallFunction(this, next.value, adder);
} }
} }
...@@ -160,30 +151,20 @@ function MapConstructor(iterable) { ...@@ -160,30 +151,20 @@ function MapConstructor(iterable) {
throw MakeTypeError('constructor_not_function', ['Map']); throw MakeTypeError('constructor_not_function', ['Map']);
} }
var iter, adder; %_MapInitialize(this);
if (!IS_NULL_OR_UNDEFINED(iterable)) { if (!IS_NULL_OR_UNDEFINED(iterable)) {
adder = this.set; var adder = this.set;
if (!IS_SPEC_FUNCTION(adder)) { if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['set', this]); throw MakeTypeError('property_not_function', ['set', this]);
} }
iter = GetIterator(iterable);
}
%_MapInitialize(this);
if (IS_UNDEFINED(iter)) return; for (var nextItem of iterable) {
if (!IS_SPEC_OBJECT(nextItem)) {
var next, done, nextItem; throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
while (!(next = iter.next()).done) { }
if (!IS_SPEC_OBJECT(next)) { %_CallFunction(this, nextItem[0], nextItem[1], adder);
throw MakeTypeError('iterator_result_not_an_object', [next]);
}
nextItem = next.value;
if (!IS_SPEC_OBJECT(nextItem)) {
throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
} }
%_CallFunction(this, nextItem[0], nextItem[1], adder);
} }
} }
......
...@@ -20,30 +20,19 @@ function WeakMapConstructor(iterable) { ...@@ -20,30 +20,19 @@ function WeakMapConstructor(iterable) {
throw MakeTypeError('constructor_not_function', ['WeakMap']); throw MakeTypeError('constructor_not_function', ['WeakMap']);
} }
var iter, adder; %WeakCollectionInitialize(this);
if (!IS_NULL_OR_UNDEFINED(iterable)) { if (!IS_NULL_OR_UNDEFINED(iterable)) {
adder = this.set; var adder = this.set;
if (!IS_SPEC_FUNCTION(adder)) { if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['set', this]); throw MakeTypeError('property_not_function', ['set', this]);
} }
iter = GetIterator(iterable); for (var nextItem of iterable) {
} if (!IS_SPEC_OBJECT(nextItem)) {
throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
%WeakCollectionInitialize(this); }
%_CallFunction(this, nextItem[0], nextItem[1], adder);
if (IS_UNDEFINED(iter)) return;
var next, done, nextItem;
while (!(next = iter.next()).done) {
if (!IS_SPEC_OBJECT(next)) {
throw MakeTypeError('iterator_result_not_an_object', [next]);
}
nextItem = next.value;
if (!IS_SPEC_OBJECT(nextItem)) {
throw MakeTypeError('iterator_value_not_an_object', [nextItem]);
} }
%_CallFunction(this, nextItem[0], nextItem[1], adder);
} }
} }
...@@ -127,26 +116,16 @@ function WeakSetConstructor(iterable) { ...@@ -127,26 +116,16 @@ function WeakSetConstructor(iterable) {
throw MakeTypeError('constructor_not_function', ['WeakSet']); throw MakeTypeError('constructor_not_function', ['WeakSet']);
} }
var iter, adder; %WeakCollectionInitialize(this);
if (!IS_NULL_OR_UNDEFINED(iterable)) { if (!IS_NULL_OR_UNDEFINED(iterable)) {
adder = this.add; var adder = this.add;
if (!IS_SPEC_FUNCTION(adder)) { if (!IS_SPEC_FUNCTION(adder)) {
throw MakeTypeError('property_not_function', ['add', this]); throw MakeTypeError('property_not_function', ['add', this]);
} }
iter = GetIterator(iterable); for (var value of iterable) {
} %_CallFunction(this, value, adder);
%WeakCollectionInitialize(this);
if (IS_UNDEFINED(iter)) return;
var next, done;
while (!(next = iter.next()).done) {
if (!IS_SPEC_OBJECT(next)) {
throw MakeTypeError('iterator_result_not_an_object', [next]);
} }
%_CallFunction(this, next.value, adder);
} }
} }
......
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