Changeset 13155

Show
Ignore:
Timestamp:
03/20/08 05:23:00 (10 months ago)
Author:
alex
Message:

merging adam peller's patch for infinite loop catching. Fixes #5961. !strict

Location:
dojo/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • dojo/trunk/tests/_base/array.js

    r12668 r13155  
    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 
     
    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 
     
    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 
  • dojo/trunk/_base/array.js

    r12827 r13155  
    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 
     
    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                }, 
     
    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 
     
    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; }); 
     
    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; }); 
     
    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 }); 
     
    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; });