Bug 1058394 - Don't skip holes in Array#find and Array#findIndex. r=till

--HG--
extra : rebase_source : 8a766b57c90e4b55a827555fd6bf8bfa5b3dc6b8
This commit is contained in:
ziyunfei 2014-08-28 13:11:54 +08:00
parent eb3f645a9a
commit 73a6db3fcb
2 changed files with 89 additions and 15 deletions

View File

@ -432,20 +432,18 @@ function ArrayFind(predicate/*, thisArg*/) {
var T = arguments.length > 1 ? arguments[1] : undefined;
/* Steps 8-9. */
/* Steps a (implicit), and e. */
/* Steps a (implicit), and g. */
/* Note: this will hang in some corner-case situations, because of IEEE-754 numbers'
* imprecision for large values. Example:
* var obj = { 18014398509481984: true, length: 18014398509481988 };
* Array.prototype.find.call(obj, () => true);
*/
for (var k = 0; k < len; k++) {
/* Steps b and c (implicit) */
if (k in O) {
/* Step d. */
var kValue = O[k];
if (callFunction(predicate, T, kValue, k, O))
return kValue;
}
/* Steps a-c. */
var kValue = O[k];
/* Steps d-f. */
if (callFunction(predicate, T, kValue, k, O))
return kValue;
}
/* Step 10. */
@ -470,19 +468,16 @@ function ArrayFindIndex(predicate/*, thisArg*/) {
var T = arguments.length > 1 ? arguments[1] : undefined;
/* Steps 8-9. */
/* Steps a (implicit), and e. */
/* Steps a (implicit), and g. */
/* Note: this will hang in some corner-case situations, because of IEEE-754 numbers'
* imprecision for large values. Example:
* var obj = { 18014398509481984: true, length: 18014398509481988 };
* Array.prototype.find.call(obj, () => true);
*/
for (var k = 0; k < len; k++) {
/* Steps b and c (implicit) */
if (k in O) {
/* Step d. */
if (callFunction(predicate, T, O[k], k, O))
return k;
}
/* Steps a-f. */
if (callFunction(predicate, T, O[k], k, O))
return k;
}
/* Step 10. */

View File

@ -204,3 +204,82 @@ catch(e)
actual = dumpError(e);
}
reportCompare(expect, actual, 'indexedArray: findIndex finds first string element');
// Bug 1058394 - Array#find and Array#findIndex no longer skip holes
var sparseArray = [,,1];
var sparseArrayWithInheritedDataProperty = Object.setPrototypeOf([,,1], {
__proto__: [].__proto__,
0 : 0
});
var sparseArrayWithInheritedAccessorProperty = Object.setPrototypeOf([,,1], {
__proto__: [].__proto__,
get 0(){
throw "get 0";
}
});
try
{
expect = undefined;
actual = sparseArray.find(() => true);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Don't skip holes in Array#find.");
try
{
expect = 0;
actual = sparseArray.findIndex(() => true);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Don't skip holes in Array#findIndex.");
try
{
expect = 0;
actual = sparseArrayWithInheritedDataProperty.find(v => v === 0);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Array#find can find inherited data property.");
try
{
expect = 0;
actual = sparseArrayWithInheritedDataProperty.findIndex(v => v === 0);
}
catch(e)
{
actual = dumpError(e);
}
reportCompare(expect, actual, "Array#findIndex can find inherited data property.");
try
{
expect = "get 0";
actual = sparseArrayWithInheritedAccessorProperty.find(() => true);
}
catch(e)
{
actual = e;
}
reportCompare(expect, actual, "Array#find can find inherited accessor property.");
try
{
expect = "get 0";
actual = sparseArrayWithInheritedAccessorProperty.findIndex(() => true);
}
catch(e)
{
actual = e;
}
reportCompare(expect, actual, "Array#findIndex can find inherited accessor property.");