Title: [167797] trunk
Revision
167797
Author
grao...@webkit.org
Date
2014-04-25 01:25:59 -0700 (Fri, 25 Apr 2014)

Log Message

Implement Array.prototype.find()
https://bugs.webkit.org/show_bug.cgi?id=130966

Reviewed by Oliver Hunt.

Source/_javascript_Core:
Implement Array.prototype.find() and Array.prototype.findIndex() as proposed in the Harmony spec.

* builtins/Array.prototype.js:
(find):
(findIndex):
* runtime/ArrayPrototype.cpp:

LayoutTests:
* js/Object-getOwnPropertyNames-expected.txt:
* js/array-find-expected.txt: Added.
* js/array-find.html: Added.
* js/array-findIndex-expected.txt: Added.
* js/array-findIndex.html: Added.
* js/script-tests/Object-getOwnPropertyNames.js:
* js/script-tests/array-find.js: Added.
* js/script-tests/array-findIndex.js: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (167796 => 167797)


--- trunk/LayoutTests/ChangeLog	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/LayoutTests/ChangeLog	2014-04-25 08:25:59 UTC (rev 167797)
@@ -1,3 +1,19 @@
+2014-04-25  Antoine Quint  <grao...@webkit.org>
+
+        Implement Array.prototype.find()
+        https://bugs.webkit.org/show_bug.cgi?id=130966
+
+        Reviewed by Oliver Hunt.
+
+        * js/Object-getOwnPropertyNames-expected.txt:
+        * js/array-find-expected.txt: Added.
+        * js/array-find.html: Added.
+        * js/array-findIndex-expected.txt: Added.
+        * js/array-findIndex.html: Added.
+        * js/script-tests/Object-getOwnPropertyNames.js:
+        * js/script-tests/array-find.js: Added.
+        * js/script-tests/array-findIndex.js: Added.
+
 2014-04-25  Ion Rosca  <ro...@adobe.com>
 
         Incomplete body painting when using blend modes

Modified: trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt (167796 => 167797)


--- trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/LayoutTests/js/Object-getOwnPropertyNames-expected.txt	2014-04-25 08:25:59 UTC (rev 167797)
@@ -45,7 +45,7 @@
 PASS getSortedOwnPropertyNames(Function) is ['length', 'name', 'prototype']
 PASS getSortedOwnPropertyNames(Function.prototype) is ['apply', 'bind', 'call', 'constructor', 'length', 'name', 'toString']
 PASS getSortedOwnPropertyNames(Array) is ['isArray', 'length', 'name', 'prototype']
-PASS getSortedOwnPropertyNames(Array.prototype) is ['concat', 'constructor', 'entries', 'every', 'fill', 'filter', 'forEach', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift']
+PASS getSortedOwnPropertyNames(Array.prototype) is ['concat', 'constructor', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'forEach', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift']
 PASS getSortedOwnPropertyNames(String) is ['fromCharCode', 'length', 'name', 'prototype']
 PASS getSortedOwnPropertyNames(String.prototype) is ['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'concat', 'constructor', 'fixed', 'fontcolor', 'fontsize', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'replace', 'search', 'slice', 'small', 'split', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'valueOf']
 PASS getSortedOwnPropertyNames(Boolean) is ['length', 'name', 'prototype']

Added: trunk/LayoutTests/js/array-find-expected.txt (0 => 167797)


--- trunk/LayoutTests/js/array-find-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/js/array-find-expected.txt	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,46 @@
+Tests for Array.prototype.find
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS [undefined, 0, null, false, ''].find(passUndefined) is undefined
+PASS [undefined, 0, null, false, ''].find(passZero) is 0
+PASS [undefined, 0, null, false, ''].find(passNull) is null
+PASS [undefined, 0, null, false, ''].find(passFalse) is false
+PASS [undefined, 0, null, false, ''].find(passEmptyString) is ''
+PASS [0, null, false, ''].find(passUndefined) is undefined
+PASS [undefined, 0, false, ''].find(passNull) is undefined
+PASS [undefined, 0, null, ''].find(passFalse) is undefined
+PASS [undefined, 0, null, false].find(passEmptyString) is undefined
+PASS [undefined, null, false, ''].find(passZero) is undefined
+PASS (new Array(20)).find(passUndefined) is undefined
+PASS arrayWithHoles.find(passUndefined) is undefined
+PASS arrayWithHoles.find(passZero) is 0
+PASS arrayWithHoles.find(passNull) is null
+PASS arrayWithHoles.find(passFalse) is false
+PASS arrayWithHoles.find(passEmptyString) is ''
+PASS toObject([undefined, 0, null, false, '']).find(passUndefined) is undefined
+PASS toObject([undefined, 0, null, false, '']).find(passZero) is 0
+PASS toObject([undefined, 0, null, false, '']).find(passNull) is null
+PASS toObject([undefined, 0, null, false, '']).find(passFalse) is false
+PASS toObject([undefined, 0, null, false, '']).find(passEmptyString) is ''
+PASS toObject([0, null, false, '']).find(passUndefined) is undefined
+PASS toObject([undefined, 0, false, '']).find(passNull) is undefined
+PASS toObject([undefined, 0, null, '']).find(passFalse) is undefined
+PASS toObject([undefined, 0, null, false]).find(passEmptyString) is undefined
+PASS toObject([undefined, null, false, '']).find(passZero) is undefined
+PASS toObject(new Array(20)).find(passUndefined) is undefined
+PASS [0,1,2,3,4,5,6,7,8,9].find(findItemAddedDuringSearch) is undefined
+PASS [0,1,2,3,4,5,6,7,8,9].find(findItemRemovedDuringSearch) is undefined
+PASS Array.prototype.find.call(undefined, function() {}) threw exception TypeError: Array.prototype.find requires that |this| not be undefined.
+PASS Array.prototype.find.call(null, function() {}) threw exception TypeError: Array.prototype.find requires that |this| not be null.
+PASS [].find(1) threw exception TypeError: Array.prototype.find callback must be a function.
+PASS [].find('hello') threw exception TypeError: Array.prototype.find callback must be a function.
+PASS [].find([]) threw exception TypeError: Array.prototype.find callback must be a function.
+PASS [].find({}) threw exception TypeError: Array.prototype.find callback must be a function.
+PASS [].find(null) threw exception TypeError: Array.prototype.find callback must be a function.
+PASS [].find(undefined) threw exception TypeError: Array.prototype.find callback must be a function.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/js/array-find.html (0 => 167797)


--- trunk/LayoutTests/js/array-find.html	                        (rev 0)
+++ trunk/LayoutTests/js/array-find.html	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script src=""
+<script src=""
+</body>
+</html>

Added: trunk/LayoutTests/js/array-findIndex-expected.txt (0 => 167797)


--- trunk/LayoutTests/js/array-findIndex-expected.txt	                        (rev 0)
+++ trunk/LayoutTests/js/array-findIndex-expected.txt	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,46 @@
+Tests for Array.prototype.findIndex
+
+On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
+
+
+PASS [undefined, 0, null, false, ''].findIndex(passUndefined) is 0
+PASS [undefined, 0, null, false, ''].findIndex(passZero) is 1
+PASS [undefined, 0, null, false, ''].findIndex(passNull) is 2
+PASS [undefined, 0, null, false, ''].findIndex(passFalse) is 3
+PASS [undefined, 0, null, false, ''].findIndex(passEmptyString) is 4
+PASS [0, null, false, ''].findIndex(passUndefined) is -1
+PASS [undefined, 0, false, ''].findIndex(passNull) is -1
+PASS [undefined, 0, null, ''].findIndex(passFalse) is -1
+PASS [undefined, 0, null, false].findIndex(passEmptyString) is -1
+PASS [undefined, null, false, ''].findIndex(passZero) is -1
+PASS (new Array(20)).findIndex(passUndefined) is 0
+PASS arrayWithHoles.findIndex(passUndefined) is 0
+PASS arrayWithHoles.findIndex(passZero) is 10
+PASS arrayWithHoles.findIndex(passNull) is 20
+PASS arrayWithHoles.findIndex(passFalse) is 30
+PASS arrayWithHoles.findIndex(passEmptyString) is 40
+PASS toObject([undefined, 0, null, false, '']).findIndex(passUndefined) is 0
+PASS toObject([undefined, 0, null, false, '']).findIndex(passZero) is 1
+PASS toObject([undefined, 0, null, false, '']).findIndex(passNull) is 2
+PASS toObject([undefined, 0, null, false, '']).findIndex(passFalse) is 3
+PASS toObject([undefined, 0, null, false, '']).findIndex(passEmptyString) is 4
+PASS toObject([0, null, false, '']).findIndex(passUndefined) is -1
+PASS toObject([undefined, 0, false, '']).findIndex(passNull) is -1
+PASS toObject([undefined, 0, null, '']).findIndex(passFalse) is -1
+PASS toObject([undefined, 0, null, false]).findIndex(passEmptyString) is -1
+PASS toObject([undefined, null, false, '']).findIndex(passZero) is -1
+PASS toObject(new Array(20)).findIndex(passUndefined) is 0
+PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemAddedDuringSearch) is -1
+PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemRemovedDuringSearch) is -1
+PASS Array.prototype.findIndex.call(undefined, function() {}) threw exception TypeError: Array.prototype.findIndex requires that |this| not be undefined.
+PASS Array.prototype.findIndex.call(null, function() {}) threw exception TypeError: Array.prototype.findIndex requires that |this| not be null.
+PASS [].findIndex(1) threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS [].findIndex('hello') threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS [].findIndex([]) threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS [].findIndex({}) threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS [].findIndex(null) threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS [].findIndex(undefined) threw exception TypeError: Array.prototype.findIndex callback must be a function.
+PASS successfullyParsed is true
+
+TEST COMPLETE
+

Added: trunk/LayoutTests/js/array-findIndex.html (0 => 167797)


--- trunk/LayoutTests/js/array-findIndex.html	                        (rev 0)
+++ trunk/LayoutTests/js/array-findIndex.html	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
+<html>
+<head>
+<script src=""
+</head>
+<body>
+<script src=""
+<script src=""
+</body>
+</html>

Modified: trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js (167796 => 167797)


--- trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/LayoutTests/js/script-tests/Object-getOwnPropertyNames.js	2014-04-25 08:25:59 UTC (rev 167797)
@@ -53,7 +53,7 @@
     "Function": "['length', 'name', 'prototype']",
     "Function.prototype": "['apply', 'bind', 'call', 'constructor', 'length', 'name', 'toString']",
     "Array": "['isArray', 'length', 'name', 'prototype']",
-    "Array.prototype": "['concat', 'constructor', 'entries', 'every', 'fill', 'filter', 'forEach', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift']",
+    "Array.prototype": "['concat', 'constructor', 'entries', 'every', 'fill', 'filter', 'find', 'findIndex', 'forEach', 'indexOf', 'join', 'keys', 'lastIndexOf', 'length', 'map', 'pop', 'push', 'reduce', 'reduceRight', 'reverse', 'shift', 'slice', 'some', 'sort', 'splice', 'toLocaleString', 'toString', 'unshift']",
     "String": "['fromCharCode', 'length', 'name', 'prototype']",
     "String.prototype": "['anchor', 'big', 'blink', 'bold', 'charAt', 'charCodeAt', 'concat', 'constructor', 'fixed', 'fontcolor', 'fontsize', 'indexOf', 'italics', 'lastIndexOf', 'length', 'link', 'localeCompare', 'match', 'replace', 'search', 'slice', 'small', 'split', 'strike', 'sub', 'substr', 'substring', 'sup', 'toLocaleLowerCase', 'toLocaleUpperCase', 'toLowerCase', 'toString', 'toUpperCase', 'trim', 'trimLeft', 'trimRight', 'valueOf']",
     "Boolean": "['length', 'name', 'prototype']",

Added: trunk/LayoutTests/js/script-tests/array-find.js (0 => 167797)


--- trunk/LayoutTests/js/script-tests/array-find.js	                        (rev 0)
+++ trunk/LayoutTests/js/script-tests/array-find.js	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,92 @@
+description("Tests for Array.prototype.find");
+
+function passUndefined(element, index, array) {
+    return typeof element === "undefined";
+}
+function passZero(element, index, array) {
+    return element === 0;
+}
+function passNull(element, index, array) {
+    return element === null;
+}
+function passFalse(element, index, array) {
+    return element === false;
+}
+function passEmptyString(element, index, array) {
+    return element === "";
+}
+function passEven(a) {
+    return !(a & 1);
+}
+function passAfter5(element, index) {
+    return index >= 5;
+}
+function toObject(array) {
+    var result = {};
+    result.length = array.length;
+    for (var i in array)
+        result[i] = array[i];
+    result.find=Array.prototype.find;
+    return result;
+}
+function findItemAddedDuringSearch(element, index, array) {
+    if (index === 0)
+        array.push(array.length);
+    return (index === array.length - 1);
+}
+function findItemRemovedDuringSearch(element, index, array) {
+    if (index === 0)
+        array.shift();
+    return (index === 0 && array[0] === element);
+}
+arrayWithHoles = [];
+arrayWithHoles[10] = 0;
+arrayWithHoles[20] = null;
+arrayWithHoles[30] = false;
+arrayWithHoles[40] = "";
+
+shouldBe("[undefined, 0, null, false, ''].find(passUndefined)", "undefined");
+shouldBe("[undefined, 0, null, false, ''].find(passZero)", "0");
+shouldBe("[undefined, 0, null, false, ''].find(passNull)", "null");
+shouldBe("[undefined, 0, null, false, ''].find(passFalse)", "false");
+shouldBe("[undefined, 0, null, false, ''].find(passEmptyString)", "''");
+shouldBe("[0, null, false, ''].find(passUndefined)", "undefined");
+shouldBe("[undefined, 0, false, ''].find(passNull)", "undefined");
+shouldBe("[undefined, 0, null, ''].find(passFalse)", "undefined");
+shouldBe("[undefined, 0, null, false].find(passEmptyString)", "undefined");
+shouldBe("[undefined, null, false, ''].find(passZero)", "undefined");
+shouldBe("(new Array(20)).find(passUndefined)", "undefined");
+
+// Array with holes.
+shouldBe("arrayWithHoles.find(passUndefined)", "undefined");
+shouldBe("arrayWithHoles.find(passZero)", "0");
+shouldBe("arrayWithHoles.find(passNull)", "null");
+shouldBe("arrayWithHoles.find(passFalse)", "false");
+shouldBe("arrayWithHoles.find(passEmptyString)", "''");
+
+// Generic Object
+shouldBe("toObject([undefined, 0, null, false, '']).find(passUndefined)", "undefined");
+shouldBe("toObject([undefined, 0, null, false, '']).find(passZero)", "0");
+shouldBe("toObject([undefined, 0, null, false, '']).find(passNull)", "null");
+shouldBe("toObject([undefined, 0, null, false, '']).find(passFalse)", "false");
+shouldBe("toObject([undefined, 0, null, false, '']).find(passEmptyString)", "''");
+shouldBe("toObject([0, null, false, '']).find(passUndefined)", "undefined");
+shouldBe("toObject([undefined, 0, false, '']).find(passNull)", "undefined");
+shouldBe("toObject([undefined, 0, null, '']).find(passFalse)", "undefined");
+shouldBe("toObject([undefined, 0, null, false]).find(passEmptyString)", "undefined");
+shouldBe("toObject([undefined, null, false, '']).find(passZero)", "undefined");
+shouldBe("toObject(new Array(20)).find(passUndefined)", "undefined");
+
+// Modification during search
+shouldBe("[0,1,2,3,4,5,6,7,8,9].find(findItemAddedDuringSearch)", "undefined");
+shouldBe("[0,1,2,3,4,5,6,7,8,9].find(findItemRemovedDuringSearch)", "undefined");
+
+// Exeptions
+shouldThrow("Array.prototype.find.call(undefined, function() {})", "'TypeError: Array.prototype.find requires that |this| not be undefined'");
+shouldThrow("Array.prototype.find.call(null, function() {})", "'TypeError: Array.prototype.find requires that |this| not be null'");
+shouldThrow("[].find(1)", "'TypeError: Array.prototype.find callback must be a function'");
+shouldThrow("[].find('hello')", "'TypeError: Array.prototype.find callback must be a function'");
+shouldThrow("[].find([])", "'TypeError: Array.prototype.find callback must be a function'");
+shouldThrow("[].find({})", "'TypeError: Array.prototype.find callback must be a function'");
+shouldThrow("[].find(null)", "'TypeError: Array.prototype.find callback must be a function'");
+shouldThrow("[].find(undefined)", "'TypeError: Array.prototype.find callback must be a function'");

Added: trunk/LayoutTests/js/script-tests/array-findIndex.js (0 => 167797)


--- trunk/LayoutTests/js/script-tests/array-findIndex.js	                        (rev 0)
+++ trunk/LayoutTests/js/script-tests/array-findIndex.js	2014-04-25 08:25:59 UTC (rev 167797)
@@ -0,0 +1,92 @@
+description("Tests for Array.prototype.findIndex");
+
+function passUndefined(element, index, array) {
+    return typeof element === "undefined";
+}
+function passZero(element, index, array) {
+    return element === 0;
+}
+function passNull(element, index, array) {
+    return element === null;
+}
+function passFalse(element, index, array) {
+    return element === false;
+}
+function passEmptyString(element, index, array) {
+    return element === "";
+}
+function passEven(a) {
+    return !(a & 1);
+}
+function passAfter5(element, index) {
+    return index >= 5;
+}
+function toObject(array) {
+    var result = {};
+    result.length = array.length;
+    for (var i in array)
+        result[i] = array[i];
+    result.findIndex=Array.prototype.findIndex;
+    return result;
+}
+function findItemAddedDuringSearch(element, index, array) {
+    if (index === 0)
+        array.push(array.length);
+    return (index === array.length - 1);
+}
+function findItemRemovedDuringSearch(element, index, array) {
+    if (index === 0)
+        array.shift();
+    return (index === 0 && array[0] === element);
+}
+arrayWithHoles = [];
+arrayWithHoles[10] = 0;
+arrayWithHoles[20] = null;
+arrayWithHoles[30] = false;
+arrayWithHoles[40] = "";
+
+shouldBe("[undefined, 0, null, false, ''].findIndex(passUndefined)", "0");
+shouldBe("[undefined, 0, null, false, ''].findIndex(passZero)", "1");
+shouldBe("[undefined, 0, null, false, ''].findIndex(passNull)", "2");
+shouldBe("[undefined, 0, null, false, ''].findIndex(passFalse)", "3");
+shouldBe("[undefined, 0, null, false, ''].findIndex(passEmptyString)", "4");
+shouldBe("[0, null, false, ''].findIndex(passUndefined)", "-1");
+shouldBe("[undefined, 0, false, ''].findIndex(passNull)", "-1");
+shouldBe("[undefined, 0, null, ''].findIndex(passFalse)", "-1");
+shouldBe("[undefined, 0, null, false].findIndex(passEmptyString)", "-1");
+shouldBe("[undefined, null, false, ''].findIndex(passZero)", "-1");
+shouldBe("(new Array(20)).findIndex(passUndefined)", "0");
+
+// Array with holes.
+shouldBe("arrayWithHoles.findIndex(passUndefined)", "0");
+shouldBe("arrayWithHoles.findIndex(passZero)", "10");
+shouldBe("arrayWithHoles.findIndex(passNull)", "20");
+shouldBe("arrayWithHoles.findIndex(passFalse)", "30");
+shouldBe("arrayWithHoles.findIndex(passEmptyString)", "40");
+
+// Generic Object
+shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passUndefined)", "0");
+shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passZero)", "1");
+shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passNull)", "2");
+shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passFalse)", "3");
+shouldBe("toObject([undefined, 0, null, false, '']).findIndex(passEmptyString)", "4");
+shouldBe("toObject([0, null, false, '']).findIndex(passUndefined)", "-1");
+shouldBe("toObject([undefined, 0, false, '']).findIndex(passNull)", "-1");
+shouldBe("toObject([undefined, 0, null, '']).findIndex(passFalse)", "-1");
+shouldBe("toObject([undefined, 0, null, false]).findIndex(passEmptyString)", "-1");
+shouldBe("toObject([undefined, null, false, '']).findIndex(passZero)", "-1");
+shouldBe("toObject(new Array(20)).findIndex(passUndefined)", "0");
+
+// Modification during search
+shouldBe("[0,1,2,3,4,5,6,7,8,9].findIndex(findItemAddedDuringSearch)", "-1");
+shouldBe("[0,1,2,3,4,5,6,7,8,9].findIndex(findItemRemovedDuringSearch)", "-1");
+
+// Exeptions
+shouldThrow("Array.prototype.findIndex.call(undefined, function() {})", "'TypeError: Array.prototype.findIndex requires that |this| not be undefined'");
+shouldThrow("Array.prototype.findIndex.call(null, function() {})", "'TypeError: Array.prototype.findIndex requires that |this| not be null'");
+shouldThrow("[].findIndex(1)", "'TypeError: Array.prototype.findIndex callback must be a function'");
+shouldThrow("[].findIndex('hello')", "'TypeError: Array.prototype.findIndex callback must be a function'");
+shouldThrow("[].findIndex([])", "'TypeError: Array.prototype.findIndex callback must be a function'");
+shouldThrow("[].findIndex({})", "'TypeError: Array.prototype.findIndex callback must be a function'");
+shouldThrow("[].findIndex(null)", "'TypeError: Array.prototype.findIndex callback must be a function'");
+shouldThrow("[].findIndex(undefined)", "'TypeError: Array.prototype.findIndex callback must be a function'");

Modified: trunk/Source/_javascript_Core/ChangeLog (167796 => 167797)


--- trunk/Source/_javascript_Core/ChangeLog	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/Source/_javascript_Core/ChangeLog	2014-04-25 08:25:59 UTC (rev 167797)
@@ -1,3 +1,17 @@
+2014-04-25  Antoine Quint  <grao...@webkit.org>
+
+        Implement Array.prototype.find()
+        https://bugs.webkit.org/show_bug.cgi?id=130966
+
+        Reviewed by Oliver Hunt.
+
+        Implement Array.prototype.find() and Array.prototype.findIndex() as proposed in the Harmony spec.
+
+        * builtins/Array.prototype.js:
+        (find):
+        (findIndex):
+        * runtime/ArrayPrototype.cpp:
+
 2014-04-24  Brady Eidson  <beid...@apple.com>
 
         Rename "IMAGE_CONTROLS" feature to "SERVICE_CONTROLS"

Modified: trunk/Source/_javascript_Core/builtins/Array.prototype.js (167796 => 167797)


--- trunk/Source/_javascript_Core/builtins/Array.prototype.js	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/Source/_javascript_Core/builtins/Array.prototype.js	2014-04-25 08:25:59 UTC (rev 167797)
@@ -189,3 +189,46 @@
     return O;
 }
 
+function find(callback /*, thisArg */) {
+    "use strict";
+    if (this === null)
+        throw new @TypeError("Array.prototype.find requires that |this| not be null");
+    
+    if (this === undefined)
+        throw new @TypeError("Array.prototype.find requires that |this| not be undefined");
+    
+    var array = @Object(this);
+    var length = array.length >>> 0;
+    
+    if (typeof callback !== "function")
+        throw new @TypeError("Array.prototype.find callback must be a function");
+    
+    var thisArg = arguments.length > 1 ? arguments[1] : undefined;
+    for (var i = 0; i < length; i++) {
+        if (callback.@call(thisArg, array[i], i, array))
+            return array[i];
+    }
+    return undefined;
+}
+
+function findIndex(callback /*, thisArg */) {
+    "use strict";
+    if (this === null)
+        throw new @TypeError("Array.prototype.findIndex requires that |this| not be null");
+    
+    if (this === undefined)
+        throw new @TypeError("Array.prototype.findIndex requires that |this| not be undefined");
+    
+    var array = @Object(this);
+    var length = array.length >>> 0;
+    
+    if (typeof callback !== "function")
+        throw new @TypeError("Array.prototype.findIndex callback must be a function");
+    
+    var thisArg = arguments.length > 1 ? arguments[1] : undefined;
+    for (var i = 0; i < length; i++) {
+        if (callback.@call(thisArg, array[i], i, array))
+            return i;
+    }
+    return -1;
+}

Modified: trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp (167796 => 167797)


--- trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2014-04-25 08:04:28 UTC (rev 167796)
+++ trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2014-04-25 08:25:59 UTC (rev 167797)
@@ -115,6 +115,8 @@
   map            arrayProtoFuncMap            DontEnum|Function 1
   entries        arrayProtoFuncEntries        DontEnum|Function 0
   keys           arrayProtoFuncKeys           DontEnum|Function 0
+  find           arrayProtoFuncFind           DontEnum|Function 1
+  findIndex      arrayProtoFuncFindIndex      DontEnum|Function 1
 @end
 */
 
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to