Changeset 169162 in webkit


Ignore:
Timestamp:
May 21, 2014 9:14:55 AM (10 years ago)
Author:
graouts@webkit.org
Message:

Array.prototype.find and findIndex should skip holes
https://bugs.webkit.org/show_bug.cgi?id=132658

Reviewed by Geoffrey Garen.

Source/JavaScriptCore:
Skip holes in the array when iterating such that callback isn't called.

  • builtins/Array.prototype.js:

(find):
(findIndex):

LayoutTests:

  • js/array-find-expected.txt:
  • js/array-findIndex-expected.txt:
  • js/script-tests/array-find.js:
  • js/script-tests/array-findIndex.js:
Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r169160 r169162  
     12014-05-21  Antoine Quint  <graouts@webkit.org>
     2
     3        Array.prototype.find and findIndex should skip holes
     4        https://bugs.webkit.org/show_bug.cgi?id=132658
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        * js/array-find-expected.txt:
     9        * js/array-findIndex-expected.txt:
     10        * js/script-tests/array-find.js:
     11        * js/script-tests/array-findIndex.js:
     12
    1132014-05-21  Radu Stavila  <stavila@adobe.com>
    214
  • trunk/LayoutTests/js/array-find-expected.txt

    r167797 r169162  
    4141PASS [].find(null) threw exception TypeError: Array.prototype.find callback must be a function.
    4242PASS [].find(undefined) threw exception TypeError: Array.prototype.find callback must be a function.
     43find callback called with index 10
     44find callback called with index 20
     45find callback called with index 30
     46find callback called with index 40
     47find callback called with index 50
     48PASS numberOfCallbacksInFindInArrayWithHoles() is 5
    4349PASS successfullyParsed is true
    4450
  • trunk/LayoutTests/js/array-findIndex-expected.txt

    r167797 r169162  
    1414PASS [undefined, 0, null, false].findIndex(passEmptyString) is -1
    1515PASS [undefined, null, false, ''].findIndex(passZero) is -1
    16 PASS (new Array(20)).findIndex(passUndefined) is 0
    17 PASS arrayWithHoles.findIndex(passUndefined) is 0
     16PASS (new Array(20)).findIndex(passUndefined) is -1
     17PASS arrayWithHoles.findIndex(passUndefined) is -1
    1818PASS arrayWithHoles.findIndex(passZero) is 10
    1919PASS arrayWithHoles.findIndex(passNull) is 20
    2020PASS arrayWithHoles.findIndex(passFalse) is 30
    2121PASS arrayWithHoles.findIndex(passEmptyString) is 40
     22PASS arrayWithHoles.findIndex(passUndefined) is 50
    2223PASS toObject([undefined, 0, null, false, '']).findIndex(passUndefined) is 0
    2324PASS toObject([undefined, 0, null, false, '']).findIndex(passZero) is 1
     
    3031PASS toObject([undefined, 0, null, false]).findIndex(passEmptyString) is -1
    3132PASS toObject([undefined, null, false, '']).findIndex(passZero) is -1
    32 PASS toObject(new Array(20)).findIndex(passUndefined) is 0
     33PASS toObject(new Array(20)).findIndex(passUndefined) is -1
    3334PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemAddedDuringSearch) is -1
    3435PASS [0,1,2,3,4,5,6,7,8,9].findIndex(findItemRemovedDuringSearch) is -1
     
    4142PASS [].findIndex(null) threw exception TypeError: Array.prototype.findIndex callback must be a function.
    4243PASS [].findIndex(undefined) threw exception TypeError: Array.prototype.findIndex callback must be a function.
     44find callback called with index 10
     45find callback called with index 20
     46find callback called with index 30
     47find callback called with index 40
     48find callback called with index 50
     49PASS numberOfCallbacksInFindIndexInArrayWithHoles() is 5
    4350PASS successfullyParsed is true
    4451
  • trunk/LayoutTests/js/script-tests/array-find.js

    r167797 r169162  
    4545arrayWithHoles[30] = false;
    4646arrayWithHoles[40] = "";
     47arrayWithHoles[50] = undefined;
     48function numberOfCallbacksInFindInArrayWithHoles() {
     49    var count = 0;
     50    arrayWithHoles.find(function(element, index, array) {
     51        debug("find callback called with index " + index);
     52        count++;
     53    });
     54    return count;
     55}
    4756
    4857shouldBe("[undefined, 0, null, false, ''].find(passUndefined)", "undefined");
     
    91100shouldThrow("[].find(null)", "'TypeError: Array.prototype.find callback must be a function'");
    92101shouldThrow("[].find(undefined)", "'TypeError: Array.prototype.find callback must be a function'");
     102
     103// Callbacks in the expected order and skipping holes.
     104shouldBe("numberOfCallbacksInFindInArrayWithHoles()", "5");
  • trunk/LayoutTests/js/script-tests/array-findIndex.js

    r167797 r169162  
    4545arrayWithHoles[30] = false;
    4646arrayWithHoles[40] = "";
     47function numberOfCallbacksInFindIndexInArrayWithHoles() {
     48    var count = 0;
     49    arrayWithHoles.find(function(element, index, array) {
     50        debug("find callback called with index " + index);
     51        count++;
     52    });
     53    return count;
     54}
    4755
    4856shouldBe("[undefined, 0, null, false, ''].findIndex(passUndefined)", "0");
     
    5664shouldBe("[undefined, 0, null, false].findIndex(passEmptyString)", "-1");
    5765shouldBe("[undefined, null, false, ''].findIndex(passZero)", "-1");
    58 shouldBe("(new Array(20)).findIndex(passUndefined)", "0");
     66shouldBe("(new Array(20)).findIndex(passUndefined)", "-1");
    5967
    6068// Array with holes.
    61 shouldBe("arrayWithHoles.findIndex(passUndefined)", "0");
     69shouldBe("arrayWithHoles.findIndex(passUndefined)", "-1");
    6270shouldBe("arrayWithHoles.findIndex(passZero)", "10");
    6371shouldBe("arrayWithHoles.findIndex(passNull)", "20");
    6472shouldBe("arrayWithHoles.findIndex(passFalse)", "30");
    6573shouldBe("arrayWithHoles.findIndex(passEmptyString)", "40");
     74arrayWithHoles[50] = undefined;
     75shouldBe("arrayWithHoles.findIndex(passUndefined)", "50");
    6676
    6777// Generic Object
     
    7686shouldBe("toObject([undefined, 0, null, false]).findIndex(passEmptyString)", "-1");
    7787shouldBe("toObject([undefined, null, false, '']).findIndex(passZero)", "-1");
    78 shouldBe("toObject(new Array(20)).findIndex(passUndefined)", "0");
     88shouldBe("toObject(new Array(20)).findIndex(passUndefined)", "-1");
    7989
    8090// Modification during search
     
    91101shouldThrow("[].findIndex(null)", "'TypeError: Array.prototype.findIndex callback must be a function'");
    92102shouldThrow("[].findIndex(undefined)", "'TypeError: Array.prototype.findIndex callback must be a function'");
     103
     104// Callbacks in the expected order and skipping holes.
     105shouldBe("numberOfCallbacksInFindIndexInArrayWithHoles()", "5");
  • trunk/Source/JavaScriptCore/ChangeLog

    r169159 r169162  
     12014-05-21  Antoine Quint  <graouts@webkit.org>
     2
     3        Array.prototype.find and findIndex should skip holes
     4        https://bugs.webkit.org/show_bug.cgi?id=132658
     5
     6        Reviewed by Geoffrey Garen.
     7
     8        Skip holes in the array when iterating such that callback isn't called.
     9
     10        * builtins/Array.prototype.js:
     11        (find):
     12        (findIndex):
     13
    1142014-05-21  Eva Balazsfalvi  <evab.u-szeged@partner.samsung.com>
    215
  • trunk/Source/JavaScriptCore/builtins/Array.prototype.js

    r167797 r169162  
    206206    var thisArg = arguments.length > 1 ? arguments[1] : undefined;
    207207    for (var i = 0; i < length; i++) {
     208        if (!(i in array))
     209            continue;
    208210        if (callback.@call(thisArg, array[i], i, array))
    209211            return array[i];
     
    228230    var thisArg = arguments.length > 1 ? arguments[1] : undefined;
    229231    for (var i = 0; i < length; i++) {
     232        if (!(i in array))
     233            continue;
    230234        if (callback.@call(thisArg, array[i], i, array))
    231235            return i;
Note: See TracChangeset for help on using the changeset viewer.