Ticket #5961: 5961.patch

File 5961.patch, 6.9 kB (added by peller, 10 months ago)

fix passing in array (array.length is undefined) as well as bounds checking to prevent infinite loop

  • Users/peller/Sites/workspace/trunk4/dojo/tests/_base/array.js

     
    66                        var foo = [128, 256, 512]; 
    77                        var bar = ["aaa", "bbb", "ccc"]; 
    88                         
    9                         t.assertTrue(dojo.indexOf([45, 56, 85], 56) == 1); 
    10                         t.assertTrue(dojo.indexOf([Number, String, Date], String) == 1); 
    11                         t.assertTrue(dojo.indexOf(foo, foo[1]) == 1); 
    12                         t.assertTrue(dojo.indexOf(foo, foo[2]) == 2); 
    13                         t.assertTrue(dojo.indexOf(bar, bar[1]) == 1); 
    14                         t.assertTrue(dojo.indexOf(bar, bar[2]) == 2); 
    15                          
     9                        t.assertEqual(1, dojo.indexOf([45, 56, 85], 56)); 
     10                        t.assertEqual(1, dojo.indexOf([Number, String, Date], String)); 
     11                        t.assertEqual(1, dojo.indexOf(foo, foo[1])); 
     12                        t.assertEqual(2, dojo.indexOf(foo, foo[2])); 
     13                        t.assertEqual(1, dojo.indexOf(bar, bar[1])); 
     14                        t.assertEqual(2, dojo.indexOf(bar, bar[2])); 
     15                        t.assertEqual(-1, dojo.indexOf({a:1}, "a")); 
     16 
    1617                        foo.push(bar); 
    17                         t.assertTrue(dojo.indexOf(foo, bar) == 3); 
     18                        t.assertEqual(3, dojo.indexOf(foo, bar)); 
    1819                }, 
    1920 
    2021                function testIndexOfFromIndex(t){ 
    2122                        var foo = [128, 256, 512]; 
    2223                        var bar = ["aaa", "bbb", "ccc"]; 
    2324                         
    24                         // FIXME: what happens w/ negative indexes? 
    2525                        t.assertEqual(-1, dojo.indexOf([45, 56, 85], 56, 2)); 
    2626                        t.assertEqual(1, dojo.indexOf([45, 56, 85], 56, 1)); 
     27                        t.assertEqual(1, dojo.indexOf([45, 56, 85], 56, -1)); 
     28                        // Make sure going out of bounds doesn't throw us in an infinite loop 
     29                        t.assertEqual(-1, dojo.indexOf([45, 56, 85], 56, 3)); 
    2730                }, 
    2831 
    2932                function testLastIndexOf(t){ 
    3033                        var foo = [128, 256, 512]; 
    3134                        var bar = ["aaa", "bbb", "aaa", "ccc"]; 
    3235                         
    33                         t.assertTrue(dojo.indexOf([45, 56, 85], 56) == 1); 
    34                         t.assertTrue(dojo.indexOf([Number, String, Date], String) == 1); 
    35                         t.assertTrue(dojo.lastIndexOf(foo, foo[1]) == 1); 
    36                         t.assertTrue(dojo.lastIndexOf(foo, foo[2]) == 2); 
    37                         t.assertTrue(dojo.lastIndexOf(bar, bar[1]) == 1); 
    38                         t.assertTrue(dojo.lastIndexOf(bar, bar[2]) == 2); 
    39                         t.assertTrue(dojo.lastIndexOf(bar, bar[0]) == 2); 
     36                        t.assertEqual(1, dojo.indexOf([45, 56, 85], 56)); 
     37                        t.assertEqual(1, dojo.indexOf([Number, String, Date], String)); 
     38                        t.assertEqual(1, dojo.lastIndexOf(foo, foo[1])); 
     39                        t.assertEqual(2, dojo.lastIndexOf(foo, foo[2])); 
     40                        t.assertEqual(1, dojo.lastIndexOf(bar, bar[1])); 
     41                        t.assertEqual(2, dojo.lastIndexOf(bar, bar[2])); 
     42                        t.assertEqual(2, dojo.lastIndexOf(bar, bar[0])); 
    4043                }, 
    4144 
    4245                function testLastIndexOfFromIndex(t){ 
    43                         // FIXME: what happens w/ negative indexes? 
    4446                        t.assertEqual(1, dojo.lastIndexOf([45, 56, 85], 56, 1)); 
    4547                        t.assertEqual(-1, dojo.lastIndexOf([45, 56, 85], 85, 1)); 
     48                        t.assertEqual(-1, dojo.lastIndexOf([45, 56, 85], 85, -1)); 
     49                        t.assertEqual(0, dojo.lastIndexOf([45, 56, 45], 45, 0)); 
    4650                }, 
    4751 
    4852                function testForEach(t){ 
  • Users/peller/Sites/workspace/trunk4/dojo/_base/array.js

     
    2323                        //              For details on this method, see: 
    2424                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:indexOf> 
    2525 
    26                         var step = 1, end = array.length, i = (fromIndex||0); 
     26                        var step = 1, end = array.length || 0, i = 0; 
    2727                        if(findLast){ 
    28                                 step = -1, i = (fromIndex||array.length - 1), end = -1; 
     28                                i = end - 1; 
     29                                step = end = -1; 
    2930                        } 
    30                         for(; i!=end; i+=step){ 
    31                                 if(array[i] == value){ return i; } 
     31                        if(fromIndex !== undefined){ i = fromIndex; } 
     32                        if((findLast && i > end) || i < end){ 
     33                                for(; i != end; i += step){ 
     34                                        if(array[i] == value){ return i; } 
     35                                } 
    3236                        } 
    3337                        return -1;      // Number 
    3438                }, 
     
    3943                        //              If the value is not found, -1 is returned. 
    4044                        // description: 
    4145                        //              For details on this method, see: 
    42                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf 
     46                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:lastIndexOf> 
    4347                        return dojo.indexOf(array, value, fromIndex, true); // Number 
    4448                }, 
    4549 
     
    5357                        //              This function corresponds to the JavaScript 1.6 Array.forEach() method. 
    5458                        //              In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 
    5559                        //              For more details, see: 
    56                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach 
     60                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:forEach> 
    5761 
    5862                        // match the behavior of the built-in forEach WRT empty arrs 
    5963                        if(!arr || !arr.length){ return; } 
     
    8993                        //              This function corresponds to the JavaScript 1.6 Array.every() method. 
    9094                        //              In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 
    9195                        //              For more details, see: 
    92                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every 
     96                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:every> 
    9397                        // example: 
    9498                        //      |       dojo.every([1, 2, 3, 4], function(item){ return item>1; }); 
    9599                        //              returns false 
     
    111115                        //              This function corresponds to the JavaScript 1.6 Array.some() method. 
    112116                        //              In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 
    113117                        //              For more details, see: 
    114                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some 
     118                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:some> 
    115119                        // example: 
    116120                        //      |       dojo.some([1, 2, 3, 4], function(item){ return item>1; }); 
    117121                        //              returns true 
     
    132136                        //              This function corresponds to the JavaScript 1.6 Array.map() method. 
    133137                        //              In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 
    134138                        //              For more details, see: 
    135                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map 
     139                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:map> 
    136140                        // example: 
    137141                        //      |       dojo.map([1, 2, 3, 4], function(item){ return item+1 }); 
    138142                        //              returns [2, 3, 4, 5] 
     
    156160                        //              This function corresponds to the JavaScript 1.6 Array.filter() method. 
    157161                        //              In environments that support JavaScript 1.6, this function is a passthrough to the built-in method. 
    158162                        //              For more details, see: 
    159                         //                      http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter 
     163                        //                      <http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Reference:Global_Objects:Array:filter> 
    160164                        // example: 
    161165                        //      |       dojo.filter([1, 2, 3, 4], function(item){ return item>1; }); 
    162166                        //              returns [2, 3, 4]