Skip to content

Commit bb7bcac

Browse files
committed
Array.prototype.find and findIndex should skip holes
https://bugs.webkit.org/show_bug.cgi?id=132658 Reviewed by Geoffrey Garen. Source/JavaScriptCore: Skip holes in the array when iterating such that callback isn't called. * builtins/Array.prototype.js: (find): (findIndex): LayoutTests: * js/array-find-expected.txt: * js/array-findIndex-expected.txt: * js/script-tests/array-find.js: * js/script-tests/array-findIndex.js: Canonical link: https://commits.webkit.org/151174@main git-svn-id: https://svn.webkit.org/repository/webkit/trunk@169162 268f45cc-cd09-0410-ab3c-d52691b4dbfc
1 parent 6729d21 commit bb7bcac

7 files changed

Lines changed: 73 additions & 6 deletions

File tree

LayoutTests/ChangeLog

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
2014-05-21 Antoine Quint <graouts@webkit.org>
2+
3+
Array.prototype.find and findIndex should skip holes
4+
https://bugs.webkit.org/show_bug.cgi?id=132658
5+
6+
Reviewed by Geoffrey Garen.
7+
8+
* js/array-find-expected.txt:
9+
* js/array-findIndex-expected.txt:
10+
* js/script-tests/array-find.js:
11+
* js/script-tests/array-findIndex.js:
12+
113
2014-05-21 Radu Stavila <stavila@adobe.com>
214

315
REGRESSION (r168046): Invalid layout in WebCore::RenderBox::containingBlockLogicalWidthForPositioned

LayoutTests/js/array-find-expected.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ PASS [].find([]) threw exception TypeError: Array.prototype.find callback must b
4040
PASS [].find({}) threw exception TypeError: Array.prototype.find callback must be a function.
4141
PASS [].find(null) threw exception TypeError: Array.prototype.find callback must be a function.
4242
PASS [].find(undefined) threw exception TypeError: Array.prototype.find callback must be a function.
43+
find callback called with index 10
44+
find callback called with index 20
45+
find callback called with index 30
46+
find callback called with index 40
47+
find callback called with index 50
48+
PASS numberOfCallbacksInFindInArrayWithHoles() is 5
4349
PASS successfullyParsed is true
4450

4551
TEST COMPLETE

LayoutTests/js/array-findIndex-expected.txt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ PASS [undefined, 0, false, ''].findIndex(passNull) is -1
1313
PASS [undefined, 0, null, ''].findIndex(passFalse) is -1
1414
PASS [undefined, 0, null, false].findIndex(passEmptyString) is -1
1515
PASS [undefined, null, false, ''].findIndex(passZero) is -1
16-
PASS (new Array(20)).findIndex(passUndefined) is 0
17-
PASS arrayWithHoles.findIndex(passUndefined) is 0
16+
PASS (new Array(20)).findIndex(passUndefined) is -1
17+
PASS arrayWithHoles.findIndex(passUndefined) is -1
1818
PASS arrayWithHoles.findIndex(passZero) is 10
1919
PASS arrayWithHoles.findIndex(passNull) is 20
2020
PASS arrayWithHoles.findIndex(passFalse) is 30
2121
PASS arrayWithHoles.findIndex(passEmptyString) is 40
22+
PASS arrayWithHoles.findIndex(passUndefined) is 50
2223
PASS toObject([undefined, 0, null, false, '']).findIndex(passUndefined) is 0
2324
PASS toObject([undefined, 0, null, false, '']).findIndex(passZero) is 1
2425
PASS toObject([undefined, 0, null, false, '']).findIndex(passNull) is 2
@@ -29,7 +30,7 @@ PASS toObject([undefined, 0, false, '']).findIndex(passNull) is -1
2930
PASS toObject([undefined, 0, null, '']).findIndex(passFalse) is -1
3031
PASS toObject([undefined, 0, null, false]).findIndex(passEmptyString) is -1
3132
PASS toObject([undefined, null, false, '']).findIndex(passZero) is -1
32-
PASS toObject(new Array(20)).findIndex(passUndefined) is 0
33+
PASS toObject(new Array(20)).findIndex(passUndefined) is -1
3334
PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemAddedDuringSearch) is -1
3435
PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemRemovedDuringSearch) is -1
3536
PASS Array.prototype.findIndex.call(undefined, function() {}) threw exception TypeError: Array.prototype.findIndex requires that |this| not be undefined.
@@ -40,6 +41,12 @@ PASS [].findIndex([]) threw exception TypeError: Array.prototype.findIndex callb
4041
PASS [].findIndex({}) threw exception TypeError: Array.prototype.findIndex callback must be a function.
4142
PASS [].findIndex(null) threw exception TypeError: Array.prototype.findIndex callback must be a function.
4243
PASS [].findIndex(undefined) threw exception TypeError: Array.prototype.findIndex callback must be a function.
44+
find callback called with index 10
45+
find callback called with index 20
46+
find callback called with index 30
47+
find callback called with index 40
48+
find callback called with index 50
49+
PASS numberOfCallbacksInFindIndexInArrayWithHoles() is 5
4350
PASS successfullyParsed is true
4451

4552
TEST COMPLETE

LayoutTests/js/script-tests/array-find.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ arrayWithHoles[10] = 0;
4444
arrayWithHoles[20] = null;
4545
arrayWithHoles[30] = false;
4646
arrayWithHoles[40] = "";
47+
arrayWithHoles[50] = undefined;
48+
function numberOfCallbacksInFindInArrayWithHoles() {
49+
var count = 0;
50+
arrayWithHoles.find(function(element, index, array) {
51+
debug("find callback called with index " + index);
52+
count++;
53+
});
54+
return count;
55+
}
4756

4857
shouldBe("[undefined, 0, null, false, ''].find(passUndefined)", "undefined");
4958
shouldBe("[undefined, 0, null, false, ''].find(passZero)", "0");
@@ -90,3 +99,6 @@ shouldThrow("[].find([])", "'TypeError: Array.prototype.find callback must be a
9099
shouldThrow("[].find({})", "'TypeError: Array.prototype.find callback must be a function'");
91100
shouldThrow("[].find(null)", "'TypeError: Array.prototype.find callback must be a function'");
92101
shouldThrow("[].find(undefined)", "'TypeError: Array.prototype.find callback must be a function'");
102+
103+
// Callbacks in the expected order and skipping holes.
104+
shouldBe("numberOfCallbacksInFindInArrayWithHoles()", "5");

LayoutTests/js/script-tests/array-findIndex.js

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ arrayWithHoles[10] = 0;
4444
arrayWithHoles[20] = null;
4545
arrayWithHoles[30] = false;
4646
arrayWithHoles[40] = "";
47+
function numberOfCallbacksInFindIndexInArrayWithHoles() {
48+
var count = 0;
49+
arrayWithHoles.find(function(element, index, array) {
50+
debug("find callback called with index " + index);
51+
count++;
52+
});
53+
return count;
54+
}
4755

4856
shouldBe("[undefined, 0, null, false, ''].findIndex(passUndefined)", "0");
4957
shouldBe("[undefined, 0, null, false, ''].findIndex(passZero)", "1");
@@ -55,14 +63,16 @@ shouldBe("[undefined, 0, false, ''].findIndex(passNull)", "-1");
5563
shouldBe("[undefined, 0, null, ''].findIndex(passFalse)", "-1");
5664
shouldBe("[undefined, 0, null, false].findIndex(passEmptyString)", "-1");
5765
shouldBe("[undefined, null, false, ''].findIndex(passZero)", "-1");
58-
shouldBe("(new Array(20)).findIndex(passUndefined)", "0");
66+
shouldBe("(new Array(20)).findIndex(passUndefined)", "-1");
5967

6068
// Array with holes.
61-
shouldBe("arrayWithHoles.findIndex(passUndefined)", "0");
69+
shouldBe("arrayWithHoles.findIndex(passUndefined)", "-1");
6270
shouldBe("arrayWithHoles.findIndex(passZero)", "10");
6371
shouldBe("arrayWithHoles.findIndex(passNull)", "20");
6472
shouldBe("arrayWithHoles.findIndex(passFalse)", "30");
6573
shouldBe("arrayWithHoles.findIndex(passEmptyString)", "40");
74+
arrayWithHoles[50] = undefined;
75+
shouldBe("arrayWithHoles.findIndex(passUndefined)", "50");
6676

6777
// Generic Object
6878
shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passUndefined)", "0");
@@ -75,7 +85,7 @@ shouldBe("toObject([undefined, 0, false, '']).findIndex(passNull)", "-1");
7585
shouldBe("toObject([undefined, 0, null, '']).findIndex(passFalse)", "-1");
7686
shouldBe("toObject([undefined, 0, null, false]).findIndex(passEmptyString)", "-1");
7787
shouldBe("toObject([undefined, null, false, '']).findIndex(passZero)", "-1");
78-
shouldBe("toObject(new Array(20)).findIndex(passUndefined)", "0");
88+
shouldBe("toObject(new Array(20)).findIndex(passUndefined)", "-1");
7989

8090
// Modification during search
8191
shouldBe("[0,1,2,3,4,5,6,7,8,9].findIndex(findItemAddedDuringSearch)", "-1");
@@ -90,3 +100,6 @@ shouldThrow("[].findIndex([])", "'TypeError: Array.prototype.findIndex callback
90100
shouldThrow("[].findIndex({})", "'TypeError: Array.prototype.findIndex callback must be a function'");
91101
shouldThrow("[].findIndex(null)", "'TypeError: Array.prototype.findIndex callback must be a function'");
92102
shouldThrow("[].findIndex(undefined)", "'TypeError: Array.prototype.findIndex callback must be a function'");
103+
104+
// Callbacks in the expected order and skipping holes.
105+
shouldBe("numberOfCallbacksInFindIndexInArrayWithHoles()", "5");

Source/JavaScriptCore/ChangeLog

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
2014-05-21 Antoine Quint <graouts@webkit.org>
2+
3+
Array.prototype.find and findIndex should skip holes
4+
https://bugs.webkit.org/show_bug.cgi?id=132658
5+
6+
Reviewed by Geoffrey Garen.
7+
8+
Skip holes in the array when iterating such that callback isn't called.
9+
10+
* builtins/Array.prototype.js:
11+
(find):
12+
(findIndex):
13+
114
2014-05-21 Eva Balazsfalvi <evab.u-szeged@partner.samsung.com>
215

316
REGRESSION(r169092 and r169102): Skip failing JSC tests on ARM64 properly

Source/JavaScriptCore/builtins/Array.prototype.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ function find(callback /*, thisArg */) {
205205

206206
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
207207
for (var i = 0; i < length; i++) {
208+
if (!(i in array))
209+
continue;
208210
if (callback.@call(thisArg, array[i], i, array))
209211
return array[i];
210212
}
@@ -227,6 +229,8 @@ function findIndex(callback /*, thisArg */) {
227229

228230
var thisArg = arguments.length > 1 ? arguments[1] : undefined;
229231
for (var i = 0; i < length; i++) {
232+
if (!(i in array))
233+
continue;
230234
if (callback.@call(thisArg, array[i], i, array))
231235
return i;
232236
}

0 commit comments

Comments
 (0)