Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2012 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 // Flags: --stack-size=100
     29 
     30 function rec1(a) { rec1(a+1); }
     31 function rec2(a) { rec3(a+1); }
     32 function rec3(a) { rec2(a+1); }
     33 
     34 // Test stack trace getter and setter.
     35 try {
     36   rec1(0);
     37 } catch (e) {
     38   assertTrue(e.stack.indexOf("rec1") > 0);
     39   e.stack = "123";
     40   assertEquals("123", e.stack);
     41 }
     42 
     43 // Test setter w/o calling the getter.
     44 try {
     45   rec2(0);
     46 } catch (e) {
     47   assertTrue(e.stack.indexOf("rec2") > 0);
     48   assertTrue(e.stack.indexOf("rec3") > 0);
     49   e.stack = "123";
     50   assertEquals("123", e.stack);
     51 }
     52 
     53 // Test getter to make sure setter does not affect the boilerplate.
     54 try {
     55   rec1(0);
     56 } catch (e) {
     57   assertTrue(e.stack.indexOf("rec1") > 0);
     58   assertInstanceof(e, RangeError);
     59 }
     60 
     61 
     62 // Check setting/getting stack property on the prototype chain.
     63 function testErrorPrototype(prototype) {
     64   var object = {};
     65   object.__proto__ = prototype;
     66   object.stack = "123";  // Overwriting stack property fails.
     67   assertEquals(prototype.stack, object.stack);
     68   assertTrue("123" != prototype.stack);
     69 }
     70 
     71 try {
     72   rec1(0);
     73 } catch (e) {
     74   e.stack;
     75   testErrorPrototype(e);
     76 }
     77 
     78 try {
     79   rec1(0);
     80 } catch (e) {
     81   testErrorPrototype(e);
     82 }
     83 
     84 try {
     85   throw new Error();
     86 } catch (e) {
     87   testErrorPrototype(e);
     88 }
     89 
     90 Error.stackTraceLimit = 3;
     91 try {
     92   rec1(0);
     93 } catch (e) {
     94   assertEquals(4, e.stack.split('\n').length);
     95 }
     96 
     97 Error.stackTraceLimit = 25.9;
     98 try {
     99   rec1(0);
    100 } catch (e) {
    101   assertEquals(26, e.stack.split('\n').length);
    102 }
    103 
    104 Error.stackTraceLimit = NaN;
    105 try {
    106   rec1(0);
    107 } catch (e) {
    108   assertEquals(1, e.stack.split('\n').length);
    109 }
    110 
    111 // A limit outside the range of integers.
    112 Error.stackTraceLimit = 1e12;
    113 try {
    114   rec1(0);
    115 } catch (e) {
    116   assertTrue(e.stack.split('\n').length > 100);
    117 }
    118 
    119 Error.stackTraceLimit = Infinity;
    120 try {
    121   rec1(0);
    122 } catch (e) {
    123   assertTrue(e.stack.split('\n').length > 100);
    124 }
    125 
    126 Error.stackTraceLimit = "not a number";
    127 try {
    128   rec1(0);
    129 } catch (e) {
    130   assertEquals(undefined, e.stack);
    131   e.stack = "abc";
    132   assertEquals("abc", e.stack);
    133 }
    134 
    135 Error.stackTraceLimit = 3;
    136 Error = "";  // Overwrite Error in the global object.
    137 try {
    138   rec1(0);
    139 } catch (e) {
    140   assertEquals(4, e.stack.split('\n').length);
    141 }
    142