Upstream version 11.40.277.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / big-object-literal.js
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 // Test that we can make large object literals that work.
29 // Also test that we can attempt to make even larger object literals without
30 // crashing.
31 function testLiteral(size, array_in_middle) {
32   print(size);
33
34   var f;
35
36   // Build object-literal string.
37   var literal = "function f() { return ";
38
39   for (var i = 0; i < size; i++) {
40     literal += "{a:";
41   }
42
43   literal += array_in_middle ? " [42.2]" : "{a:42.2}";
44
45   for (var i = 0; i < size; i++) {
46     literal += "}";
47     if (i < size - 1) {
48       literal += ", b:42, c:/asd/, x:'foo', y:[], z:new Object()";
49     }
50   }
51
52   literal += "; }";
53
54   // Create the object literal.
55   eval(literal);
56
57   var x = f();
58
59   // Check that the properties have the expected values.
60   for (var i = 0; i < size; i++) {
61     x = x.a;
62   }
63
64   if (array_in_middle) {
65     assertEquals(42.2, x[0]), "x array in middle";
66     x[0] = 41.2;
67   } else {
68     assertEquals(42.2, x.a, "x object in middle");
69     x.a = 41.2;
70   }
71
72   var y = f();
73   for (var i = 0; i < size; i++) {
74     y = y.a;
75   }
76
77   if (array_in_middle) {
78     assertEquals(42.2, y[0], "y array in middle");
79     y[0] = 41.2;
80   } else {
81     assertEquals(42.2, y.a, "y object in middle");
82     y.a = 41.2;
83   }
84 }
85
86 // The sizes to test.
87 var sizes = [1, 2, 100, 200];
88
89 // Run the test.
90 for (var i = 0; i < sizes.length; i++) {
91   testLiteral(sizes[i], false);
92   testLiteral(sizes[i], true);
93 }
94
95
96 function checkExpectedException(e) {
97   assertInstanceof(e, RangeError);
98   assertTrue(e.message.indexOf("Maximum call stack size exceeded") >= 0);
99 }
100
101
102 function testLiteralAndCatch(size) {
103   var big_enough = false;
104   try {
105     testLiteral(size, false);
106   } catch (e) {
107     checkExpectedException(e);
108     big_enough = true;
109   }
110   try {
111     testLiteral(size, true);
112   } catch (e) {
113     checkExpectedException(e);
114     big_enough = true;
115   }
116   return big_enough;
117 }
118
119 // Catch stack overflows.
120
121 testLiteralAndCatch(1000) ||
122 testLiteralAndCatch(20000) ||
123 testLiteralAndCatch(200000);