Commit d269e22d authored by arv's avatar arv Committed by Commit bot

[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

Review URL: https://codereview.chromium.org/1165003005

Cr-Commit-Position: refs/heads/master@{#28814}
parent a6f23850
...@@ -100,12 +100,10 @@ function InnerArrayFind(predicate, thisArg, array, length) { ...@@ -100,12 +100,10 @@ function InnerArrayFind(predicate, thisArg, array, length) {
} }
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (i in array) { var element = array[i];
var element = array[i]; var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; if (%_CallFunction(newThisArg, element, i, array, predicate)) {
if (%_CallFunction(newThisArg, element, i, array, predicate)) { return element;
return element;
}
} }
} }
...@@ -135,12 +133,10 @@ function InnerArrayFindIndex(predicate, thisArg, array, length) { ...@@ -135,12 +133,10 @@ function InnerArrayFindIndex(predicate, thisArg, array, length) {
} }
for (var i = 0; i < length; i++) { for (var i = 0; i < length; i++) {
if (i in array) { var element = array[i];
var element = array[i]; var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg;
var newThisArg = needs_wrapper ? $toObject(thisArg) : thisArg; if (%_CallFunction(newThisArg, element, i, array, predicate)) {
if (%_CallFunction(newThisArg, element, i, array, predicate)) { return i;
return i;
}
} }
} }
......
...@@ -201,7 +201,7 @@ assertEquals(22, a.find(function(val) { return 22 === val; }), undefined); ...@@ -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() { (function() {
var a = new Array(30); var a = new Array(30);
...@@ -211,7 +211,27 @@ assertEquals(22, a.find(function(val) { return 22 === val; }), undefined); ...@@ -211,7 +211,27 @@ assertEquals(22, a.find(function(val) { return 22 === val; }), undefined);
var count = 0; var count = 0;
a.find(function() { count++; return false; }); 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);
})(); })();
......
...@@ -201,7 +201,7 @@ assertEquals(3, a.findIndex(function(val) { return 24 === val; })); ...@@ -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() { (function() {
var a = new Array(30); var a = new Array(30);
...@@ -211,7 +211,27 @@ assertEquals(3, a.findIndex(function(val) { return 24 === val; })); ...@@ -211,7 +211,27 @@ assertEquals(3, a.findIndex(function(val) { return 24 === val; }));
var count = 0; var count = 0;
a.findIndex(function() { count++; return false; }); 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);
})(); })();
......
...@@ -48,8 +48,10 @@ ...@@ -48,8 +48,10 @@
# https://code.google.com/p/v8/issues/detail?id=705 # https://code.google.com/p/v8/issues/detail?id=705
'language/statements/for-in/12.6.4-2': [PASS, FAIL_OK], 'language/statements/for-in/12.6.4-2': [PASS, FAIL_OK],
# Array.find (currently requires --harmony-arrays) ######################### HARNESS ISSUES ##########################
'built-ins/Array/prototype/find/Array.prototype.find_skip-empty': [FAIL], # These are failures due to our harness not working as supposed
# Uses [onlyStrict]
'built-ins/Array/prototype/find/Array.prototype.find_this-undefined': [FAIL], 'built-ins/Array/prototype/find/Array.prototype.find_this-undefined': [FAIL],
###################### MISSING ES6 FEATURES ####################### ###################### MISSING ES6 FEATURES #######################
...@@ -596,6 +598,8 @@ ...@@ -596,6 +598,8 @@
'built-ins/Function/prototype/bind/15.3.4.5-21-4': [FAIL_OK], 'built-ins/Function/prototype/bind/15.3.4.5-21-4': [FAIL_OK],
'built-ins/Function/prototype/bind/15.3.4.5-21-5': [FAIL_OK], 'built-ins/Function/prototype/bind/15.3.4.5-21-5': [FAIL_OK],
# This invalid test has been fixed upstream.
'built-ins/Array/prototype/find/Array.prototype.find_remove-after-start': [FAIL],
############################ SKIPPED TESTS ############################# ############################ SKIPPED TESTS #############################
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment