Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2009 the V8 project authors. All rights reserved.
      2 // Redistribution and use in source and binary forms, with or without
      3 // modification, are permitted provided that the following conditions are
      4 // met:
      5 //
      6 //     * Redistributions of source code must retain the above copyright
      7 //       notice, this list of conditions and the following disclaimer.
      8 //     * Redistributions in binary form must reproduce the above
      9 //       copyright notice, this list of conditions and the following
     10 //       disclaimer in the documentation and/or other materials provided
     11 //       with the distribution.
     12 //     * Neither the name of Google Inc. nor the names of its
     13 //       contributors may be used to endorse or promote products derived
     14 //       from this software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27 
     28 // On MacOS X 10.7.5, this test needs a stack size of at least 788 kBytes.
     29 // On PPC64, this test needs a stack size of at least 698 kBytes.
     30 // Flags: --stack-size=1000
     31 
     32 // Test that we can make large object literals that work.
     33 // Also test that we can attempt to make even larger object literals without
     34 // crashing.
     35 function testLiteral(size, array_in_middle) {
     36   print(size);
     37 
     38   var f;
     39 
     40   // Build object-literal string.
     41   var literal = "function f() { return ";
     42 
     43   for (var i = 0; i < size; i++) {
     44     literal += "[";
     45   }
     46 
     47   literal += array_in_middle ? " [42.2]" : "{a:42.2}";
     48 
     49   for (var i = 0; i < size; i++) {
     50     literal += "]";
     51   }
     52 
     53   literal += "; }";
     54 
     55   // Create the object literal.
     56   eval(literal);
     57 
     58   var x = f();
     59 
     60   // Check that the properties have the expected values.
     61   for (var i = 0; i < size; i++) {
     62     x = x[0];
     63   }
     64 
     65   if (array_in_middle) {
     66     assertEquals(42.2, x[0]), "x array in middle";
     67     x[0] = 41.2;
     68   } else {
     69     assertEquals(42.2, x.a, "x object in middle");
     70     x.a = 41.2;
     71   }
     72 
     73   var y = f();
     74   for (var i = 0; i < size; i++) {
     75     y = y[0];
     76   }
     77 
     78   if (array_in_middle) {
     79     assertEquals(42.2, y[0], "y array in middle");
     80     y[0] = 41.2;
     81   } else {
     82     assertEquals(42.2, y.a, "y object in middle");
     83     y.a = 41.2;
     84   }
     85 }
     86 
     87 // The sizes to test.
     88 var sizes = [1, 2, 100, 200, 300];
     89 
     90 // Run the test.
     91 for (var i = 0; i < sizes.length; i++) {
     92   testLiteral(sizes[i], false);
     93   testLiteral(sizes[i], true);
     94 }
     95 
     96 
     97 function checkExpectedException(e) {
     98   assertInstanceof(e, RangeError);
     99   assertTrue(e.message.indexOf("Maximum call stack size exceeded") >= 0);
    100 }
    101 
    102 
    103 function testLiteralAndCatch(size) {
    104   var big_enough = false;
    105   try {
    106     testLiteral(size, false);
    107   } catch (e) {
    108     checkExpectedException(e);
    109     big_enough = true;
    110   }
    111   try {
    112     testLiteral(size, true);
    113   } catch (e) {
    114     checkExpectedException(e);
    115     big_enough = true;
    116   }
    117   return big_enough;
    118 }
    119 
    120 // Catch stack overflows.
    121 
    122 testLiteralAndCatch(1000) ||
    123 testLiteralAndCatch(20000) ||
    124 testLiteralAndCatch(200000);
    125