Commit defcc642 authored by littledan's avatar littledan Committed by Commit bot

Use a for-of loop in Array.from

If Array.from is passed an iterable, then it will copy the contents
to the newly created Array (or subclass). The iteration protocol here
includes calling IteratorClose if the loop is exited early due to an
exception thrown. This patch converts Array.from to use a for-of loop
rather than explicitly invoking the iteration protocol so that, when
IteratorClose is invoked on early for-of exit, then Array.from will
call IteratorClose in the appropriate case.

R=neis
LOG=Y
BUG=v8:4739

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

Cr-Commit-Position: refs/heads/master@{#33859}
parent 3ce9e808
......@@ -1776,23 +1776,10 @@ function ArrayFrom(arrayLike, mapfn, receiver) {
if (!IS_UNDEFINED(iterable)) {
result = %IsConstructor(this) ? new this() : [];
var iterator = GetIterator(items, iterable);
k = 0;
while (true) {
var next = iterator.next();
if (!IS_RECEIVER(next)) {
throw MakeTypeError(kIteratorResultNotAnObject, next);
}
if (next.done) {
result.length = k;
return result;
}
nextValue = next.value;
for (nextValue of
{ [iteratorSymbol]() { return GetIterator(items, iterable) } }) {
if (mapping) {
mappedValue = %_Call(mapfn, receiver, nextValue, k);
} else {
......@@ -1801,6 +1788,8 @@ function ArrayFrom(arrayLike, mapfn, receiver) {
AddArrayElement(this, result, k, mappedValue);
k++;
}
result.length = k;
return result;
} else {
var len = TO_LENGTH(items.length);
result = %IsConstructor(this) ? new this(len) : new GlobalArray(len);
......
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