Skip to content

Commit

Permalink
[iterator-helpers] Add toArray helper
Browse files Browse the repository at this point in the history
This CL adds toArray helper to iterator helpers.

Bug: v8:13558
Change-Id: I2bba17f0c36475818f8d4b25eb6e8351953618d2
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4400335
Reviewed-by: Shu-yu Guo <syg@chromium.org>
Commit-Queue: Rezvan Mahdavi Hezaveh <rezvan@chromium.org>
Cr-Commit-Position: refs/heads/main@{#86987}
  • Loading branch information
rmahdav authored and V8 LUCI CQ committed Apr 6, 2023
1 parent 0df4387 commit f6bdedb
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 24 deletions.
41 changes: 41 additions & 0 deletions src/builtins/iterator-helpers.tq
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,45 @@ transitioning javascript builtin IteratorPrototypeReduce(
unreachable;
}

// --- toArray helper

// https://tc39.es/proposal-iterator-helpers/#sec-iteratorprototype.toarray
transitioning javascript builtin IteratorPrototypeToArray(
js-implicit context: NativeContext, receiver: JSAny)(): JSAny {
const methodName: constexpr string = 'Iterator.prototype.toArray';

// 1. Let O be the this value.
// 2. If O is not an Object, throw a TypeError exception.
const o = Cast<JSReceiver>(receiver)
otherwise ThrowTypeError(MessageTemplate::kCalledOnNonObject, methodName);

// 3. Let iterated be ? GetIteratorDirect(O).
const iterated = GetIteratorDirect(o);

// 4. Let items be a new empty List.
let items = growable_fixed_array::NewGrowableFixedArray();

const fastIteratorResultMap = GetIteratorResultMap();
let next: JSReceiver;

// 5. Repeat,
while (true) {
try {
// a. Let next be ? IteratorStep(iterated).
next = IteratorStep(iterated, fastIteratorResultMap)
otherwise Done;
} label Done {
// b. If next is false, return CreateArrayFromList(items).
return items.ToJSArray();
}

// c. Let value be ? IteratorValue(next).
const value = IteratorValue(next, fastIteratorResultMap);

// d. Append value to items.
items.Push(value);
}
unreachable;
}

} // namespace iterator
2 changes: 2 additions & 0 deletions src/init/bootstrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4582,6 +4582,8 @@ void Genesis::InitializeGlobal_harmony_iterator_helpers() {
Builtin::kIteratorHelperPrototypeReturn, 0, true);
SimpleInstallFunction(isolate(), iterator_prototype, "reduce",
Builtin::kIteratorPrototypeReduce, 1, false);
SimpleInstallFunction(isolate(), iterator_prototype, "toArray",
Builtin::kIteratorPrototypeToArray, 0, true);

// --- Helper maps
#define INSTALL_ITERATOR_HELPER(lowercase_name, Capitalized_name, \
Expand Down
17 changes: 17 additions & 0 deletions test/mjsunit/harmony/iterator-helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,3 +608,20 @@ function TestHelperPrototypeSurface(helper) {
return sum + x;
})});
})();

// --- Test ToArray helper

(function TestToArray() {
const iter = gen();
assertEquals('function', typeof iter.toArray);
assertEquals(0, iter.toArray.length);
assertEquals('toArray', iter.toArray.name);
const toArrayResult = iter.toArray();
assertEquals([42, 43], toArrayResult);
})();

(function TestToArrayEmptyIterator() {
const iter = gen();
const toArrayResult = iter.take(0).toArray();
assertEquals([], toArrayResult);
})();
48 changes: 24 additions & 24 deletions tools/v8heapconst.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,30 +580,30 @@
("old_space", 0x045c9): "StringSplitCache",
("old_space", 0x049d1): "RegExpMultipleCache",
("old_space", 0x04dd9): "BuiltinsConstantsTable",
("old_space", 0x054a9): "AsyncFunctionAwaitRejectSharedFun",
("old_space", 0x054cd): "AsyncFunctionAwaitResolveSharedFun",
("old_space", 0x054f1): "AsyncGeneratorAwaitRejectSharedFun",
("old_space", 0x05515): "AsyncGeneratorAwaitResolveSharedFun",
("old_space", 0x05539): "AsyncGeneratorYieldWithAwaitResolveSharedFun",
("old_space", 0x0555d): "AsyncGeneratorReturnResolveSharedFun",
("old_space", 0x05581): "AsyncGeneratorReturnClosedRejectSharedFun",
("old_space", 0x055a5): "AsyncGeneratorReturnClosedResolveSharedFun",
("old_space", 0x055c9): "AsyncIteratorValueUnwrapSharedFun",
("old_space", 0x055ed): "PromiseAllResolveElementSharedFun",
("old_space", 0x05611): "PromiseAllSettledResolveElementSharedFun",
("old_space", 0x05635): "PromiseAllSettledRejectElementSharedFun",
("old_space", 0x05659): "PromiseAnyRejectElementSharedFun",
("old_space", 0x0567d): "PromiseCapabilityDefaultRejectSharedFun",
("old_space", 0x056a1): "PromiseCapabilityDefaultResolveSharedFun",
("old_space", 0x056c5): "PromiseCatchFinallySharedFun",
("old_space", 0x056e9): "PromiseGetCapabilitiesExecutorSharedFun",
("old_space", 0x0570d): "PromiseThenFinallySharedFun",
("old_space", 0x05731): "PromiseThrowerFinallySharedFun",
("old_space", 0x05755): "PromiseValueThunkFinallySharedFun",
("old_space", 0x05779): "ProxyRevokeSharedFun",
("old_space", 0x0579d): "ShadowRealmImportValueFulfilledSFI",
("old_space", 0x057c1): "SourceTextModuleExecuteAsyncModuleFulfilledSFI",
("old_space", 0x057e5): "SourceTextModuleExecuteAsyncModuleRejectedSFI",
("old_space", 0x054ad): "AsyncFunctionAwaitRejectSharedFun",
("old_space", 0x054d1): "AsyncFunctionAwaitResolveSharedFun",
("old_space", 0x054f5): "AsyncGeneratorAwaitRejectSharedFun",
("old_space", 0x05519): "AsyncGeneratorAwaitResolveSharedFun",
("old_space", 0x0553d): "AsyncGeneratorYieldWithAwaitResolveSharedFun",
("old_space", 0x05561): "AsyncGeneratorReturnResolveSharedFun",
("old_space", 0x05585): "AsyncGeneratorReturnClosedRejectSharedFun",
("old_space", 0x055a9): "AsyncGeneratorReturnClosedResolveSharedFun",
("old_space", 0x055cd): "AsyncIteratorValueUnwrapSharedFun",
("old_space", 0x055f1): "PromiseAllResolveElementSharedFun",
("old_space", 0x05615): "PromiseAllSettledResolveElementSharedFun",
("old_space", 0x05639): "PromiseAllSettledRejectElementSharedFun",
("old_space", 0x0565d): "PromiseAnyRejectElementSharedFun",
("old_space", 0x05681): "PromiseCapabilityDefaultRejectSharedFun",
("old_space", 0x056a5): "PromiseCapabilityDefaultResolveSharedFun",
("old_space", 0x056c9): "PromiseCatchFinallySharedFun",
("old_space", 0x056ed): "PromiseGetCapabilitiesExecutorSharedFun",
("old_space", 0x05711): "PromiseThenFinallySharedFun",
("old_space", 0x05735): "PromiseThrowerFinallySharedFun",
("old_space", 0x05759): "PromiseValueThunkFinallySharedFun",
("old_space", 0x0577d): "ProxyRevokeSharedFun",
("old_space", 0x057a1): "ShadowRealmImportValueFulfilledSFI",
("old_space", 0x057c5): "SourceTextModuleExecuteAsyncModuleFulfilledSFI",
("old_space", 0x057e9): "SourceTextModuleExecuteAsyncModuleRejectedSFI",
}

# Lower 32 bits of first page addresses for various heap spaces.
Expand Down

0 comments on commit f6bdedb

Please sign in to comment.