Skip to content

Commit ea7b828

Browse files
sosukesuzukiAlexey Shvayka
authored andcommitted
[JSC] Iterator Helpers methods should not iterate an array
https://bugs.webkit.org/show_bug.cgi?id=279876 Reviewed by Alexey Shvayka. We implemented a function called `forEachInIteratorProtocol` to implement `Iterator.prototype.toArray`[1]. As the name suggests, this function iterates over objects that conform to the iterator protocol, but it also iterates over fast arrays. The methods of Iterator Helpers use the `GetIteratorDirect` abstract operation[2] to directly get the object's `next` property. Unlike the `GetIterator` abstract operation[3], it does not get the `@@iterator`. This patch changes the `forEachInIteratorProtocol` function to stop iterating over arrays. It also removes the fast path for fast arrays in `Iterator.prototype.toArray`[4]. [1]: https://commits.webkit.org/283381@main [2]: https://tc39.es/proposal-iterator-helpers/#sec-getiteratordirect [3]: https://tc39.es/ecma262/#sec-getiterator [4]: https://commits.webkit.org/283381@main * JSTests/microbenchmarks/iterator-prototype-toArray-for-array.js: Removed. * JSTests/stress/iterator-prototype-forEach.js: (sameArray): (const.validIter): Deleted. (const.invalidCallback.of.invalidCallbacks.shouldThrow): Deleted. * JSTests/stress/iterator-prototype-toArray-empty.js: Removed. * JSTests/stress/iterator-prototype-toArray.js: (sameArray): * Source/JavaScriptCore/runtime/IteratorOperations.h: (JSC::forEachInIteratorProtocol): * Source/JavaScriptCore/runtime/JSIteratorPrototype.cpp: (JSC::JSC_DEFINE_HOST_FUNCTION): Canonical link: https://commits.webkit.org/283933@main
1 parent 83db213 commit ea7b828

File tree

6 files changed

+6
-128
lines changed

6 files changed

+6
-128
lines changed

JSTests/microbenchmarks/iterator-prototype-toArray-for-array.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

JSTests/stress/iterator-prototype-forEach.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ function shouldThrow(fn, error, message) {
101101

102102
{
103103
const arr = [1, 2, 3, 4, 5];
104-
const result = [];
105-
Iterator.prototype.forEach.call(arr, (item, i) => { result.push([item, i]); });
106-
sameArray(result, [[1, 0], [2, 1], [3, 2], [4, 3], [5, 4]]);
104+
shouldThrow(function () {
105+
Iterator.prototype.forEach.call(arr, (item, i) => {});
106+
}, TypeError, "Type error")
107107
}
108108

109109
{

JSTests/stress/iterator-prototype-toArray-empty.js

Lines changed: 0 additions & 102 deletions
This file was deleted.

JSTests/stress/iterator-prototype-toArray.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ function shouldThrow(fn, error, message) {
9595

9696
{
9797
const arr1 = [1, 2, 3, 4, 5];
98-
const arr2 = Iterator.prototype.toArray.call(arr1);
99-
sameValue(arr1 === arr2, false);
100-
sameArray(arr1, arr2);
98+
shouldThrow(function () {
99+
Iterator.prototype.toArray.call(arr1);
100+
}, TypeError, "Type error");
101101
}
102102

103103
{

Source/JavaScriptCore/runtime/IteratorOperations.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,6 @@ void forEachInIteratorProtocol(JSGlobalObject* globalObject, JSValue iterable, c
173173
auto& vm = getVM(globalObject);
174174
auto scope = DECLARE_THROW_SCOPE(vm);
175175

176-
if (getIterationMode(vm, globalObject, iterable) == IterationMode::FastArray) {
177-
auto* array = jsCast<JSArray*>(iterable);
178-
forEachInFastArray(globalObject, iterable, array, callback);
179-
RETURN_IF_EXCEPTION(scope, void());
180-
return;
181-
}
182-
183176
IterationRecord iterationRecord = iteratorDirect(globalObject, iterable);
184177
RETURN_IF_EXCEPTION(scope, void());
185178
scope.release();

Source/JavaScriptCore/runtime/JSIteratorPrototype.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,6 @@ JSC_DEFINE_HOST_FUNCTION(iteratorProtoFuncToArray, (JSGlobalObject* globalObject
152152
if (!thisValue.isObject())
153153
return throwVMTypeError(globalObject, scope, "Iterator.prototype.toArray requires that |this| be an Object."_s);
154154

155-
if (getIterationMode(vm, globalObject, thisValue) == IterationMode::FastArray) {
156-
JSArray* array = tryCloneArrayFromFast(globalObject, thisValue);
157-
RETURN_IF_EXCEPTION(scope, { });
158-
if (array)
159-
return JSValue::encode(array);
160-
}
161-
162155
MarkedArgumentBuffer value;
163156
forEachInIteratorProtocol(globalObject, thisValue, [&value, &scope](VM&, JSGlobalObject* globalObject, JSValue nextItem) {
164157
value.append(nextItem);

0 commit comments

Comments
 (0)