Reviewers: adamk,

Message:
PTAL

Description:
[es6] Array.prototype.find and findIndex should include holes

We should not skip holes for these 2 functions.

BUG=v8:3895
LOG=N
R=adamk

Please review this at https://codereview.chromium.org/1165003005/

Base URL: https://chromium.googlesource.com/v8/v8.git@master

Affected files (+52, -16 lines):
  M src/harmony-array.js
  M test/mjsunit/harmony/array-find.js
  M test/mjsunit/harmony/array-findindex.js


Index: src/harmony-array.js
diff --git a/src/harmony-array.js b/src/harmony-array.js
index 5df1abde0ff05d60afdb8d54abfc66572333c570..51e2d01b50147ded30436174685b23aca3484ac9 100644
--- a/src/harmony-array.js
+++ b/src/harmony-array.js
@@ -100,12 +100,10 @@ function InnerArrayFind(predicate, thisArg, array, length) {
   }

   for (var i = 0; i < length; i++) {
-    if (i in array) {
-      var element = array[i];
-      var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
-      if (%_CallFunction(newThisArg, element, i, array, predicate)) {
-        return element;
-      }
+    var element = array[i];
+    var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
+    if (%_CallFunction(newThisArg, element, i, array, predicate)) {
+      return element;
     }
   }

@@ -135,12 +133,10 @@ function InnerArrayFindIndex(predicate, thisArg, array, length) {
   }

   for (var i = 0; i < length; i++) {
-    if (i in array) {
-      var element = array[i];
-      var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
-      if (%_CallFunction(newThisArg, element, i, array, predicate)) {
-        return i;
-      }
+    var element = array[i];
+    var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
+    if (%_CallFunction(newThisArg, element, i, array, predicate)) {
+      return i;
     }
   }

Index: test/mjsunit/harmony/array-find.js
diff --git a/test/mjsunit/harmony/array-find.js b/test/mjsunit/harmony/array-find.js index eb3208227706e6e3654549dac10d47b8197728dd..44fc78292a81123a9535999717c1085159af9874 100644
--- a/test/mjsunit/harmony/array-find.js
+++ b/test/mjsunit/harmony/array-find.js
@@ -201,7 +201,7 @@ assertEquals(22, a.find(function(val) { return 22 === val; }), undefined);


 //
-// Test predicate is only called for existing elements
+// Test predicate is called for holes
 //
 (function() {
   var a = new Array(30);
@@ -211,7 +211,27 @@ assertEquals(22, a.find(function(val) { return 22 === val; }), undefined);

   var count = 0;
   a.find(function() { count++; return false; });
-  assertEquals(3, count);
+  assertEquals(30, count);
+})();
+
+
+(function() {
+  var a = [0, 1, , 3];
+  var count = 0;
+  var found = a.find(function(val) { return val === undefined; });
+  assertEquals(undefined, found);
+})();
+
+
+(function() {
+  var a = [0, 1, , 3];
+  a.__proto__ = {
+    __proto__: Array.prototype,
+    2: 42,
+  };
+  var count = 0;
+  var found = a.find(function(val) { return val === 42; });
+  assertEquals(42, found);
 })();


Index: test/mjsunit/harmony/array-findindex.js
diff --git a/test/mjsunit/harmony/array-findindex.js b/test/mjsunit/harmony/array-findindex.js index a5df05a05c0a2b0fdd1505763dc3773ed9991a00..7068a9cb4005935b2a58af05240e6062627b6cc8 100644
--- a/test/mjsunit/harmony/array-findindex.js
+++ b/test/mjsunit/harmony/array-findindex.js
@@ -201,7 +201,7 @@ assertEquals(3, a.findIndex(function(val) { return 24 === val; }));


 //
-// Test predicate is only called for existing elements
+// Test predicate is called for holes
 //
 (function() {
   var a = new Array(30);
@@ -211,7 +211,27 @@ assertEquals(3, a.findIndex(function(val) { return 24 === val; }));

   var count = 0;
   a.findIndex(function() { count++; return false; });
-  assertEquals(3, count);
+  assertEquals(30, count);
+})();
+
+
+(function() {
+  var a = [0, 1, , 3];
+  var count = 0;
+  var index = a.findIndex(function(val) { return val === undefined; });
+  assertEquals(2, index);
+})();
+
+
+(function() {
+  var a = [0, 1, , 3];
+  a.__proto__ = {
+    __proto__: Array.prototype,
+    2: 42,
+  };
+  var count = 0;
+  var index = a.findIndex(function(val) { return val === 42; });
+  assertEquals(2, index);
 })();




--
--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
--- You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to