From f6bdedbbcc981a82361b478e87f564b295f1a8f2 Mon Sep 17 00:00:00 2001 From: Rezvan Mahdavi Hezaveh Date: Thu, 6 Apr 2023 16:35:49 +0000 Subject: [PATCH] [iterator-helpers] Add toArray helper 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 Commit-Queue: Rezvan Mahdavi Hezaveh Cr-Commit-Position: refs/heads/main@{#86987} --- src/builtins/iterator-helpers.tq | 41 ++++++++++++++++++++ src/init/bootstrapper.cc | 2 + test/mjsunit/harmony/iterator-helpers.js | 17 +++++++++ tools/v8heapconst.py | 48 ++++++++++++------------ 4 files changed, 84 insertions(+), 24 deletions(-) diff --git a/src/builtins/iterator-helpers.tq b/src/builtins/iterator-helpers.tq index c7b0a1cff834..d71811ff46bb 100644 --- a/src/builtins/iterator-helpers.tq +++ b/src/builtins/iterator-helpers.tq @@ -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(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 diff --git a/src/init/bootstrapper.cc b/src/init/bootstrapper.cc index 505604f27149..e52be9678a35 100644 --- a/src/init/bootstrapper.cc +++ b/src/init/bootstrapper.cc @@ -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, \ diff --git a/test/mjsunit/harmony/iterator-helpers.js b/test/mjsunit/harmony/iterator-helpers.js index dded7b23b5f0..d823c59830eb 100644 --- a/test/mjsunit/harmony/iterator-helpers.js +++ b/test/mjsunit/harmony/iterator-helpers.js @@ -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); +})(); diff --git a/tools/v8heapconst.py b/tools/v8heapconst.py index 9978796b28e6..845426f6bc77 100644 --- a/tools/v8heapconst.py +++ b/tools/v8heapconst.py @@ -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.