[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}
This commit is contained in:
arv 2015-06-05 07:18:01 -07:00 committed by Commit bot
parent a6f23850a5
commit d269e22de9
4 changed files with 58 additions and 18 deletions

View File

@ -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;
}
}

View File

@ -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);
})();

View File

@ -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);
})();

View File

@ -48,8 +48,10 @@
# https://code.google.com/p/v8/issues/detail?id=705
'language/statements/for-in/12.6.4-2': [PASS, FAIL_OK],
# Array.find (currently requires --harmony-arrays)
'built-ins/Array/prototype/find/Array.prototype.find_skip-empty': [FAIL],
######################### HARNESS ISSUES ##########################
# These are failures due to our harness not working as supposed
# Uses [onlyStrict]
'built-ins/Array/prototype/find/Array.prototype.find_this-undefined': [FAIL],
###################### MISSING ES6 FEATURES #######################
@ -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-5': [FAIL_OK],
# This invalid test has been fixed upstream.
'built-ins/Array/prototype/find/Array.prototype.find_remove-after-start': [FAIL],
############################ SKIPPED TESTS #############################