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.
31function 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 += "[";
41  }
42
43  literal += array_in_middle ? " [42.2]" : "{a:42.2}";
44
45  for (var i = 0; i < size; i++) {
46    literal += "]";
47  }
48
49  literal += "; }";
50
51  // Create the object literal.
52  eval(literal);
53
54  var x = f();
55
56  // Check that the properties have the expected values.
57  for (var i = 0; i < size; i++) {
58    x = x[0];
59  }
60
61  if (array_in_middle) {
62    assertEquals(42.2, x[0]), "x array in middle";
63    x[0] = 41.2;
64  } else {
65    assertEquals(42.2, x.a, "x object in middle");
66    x.a = 41.2;
67  }
68
69  var y = f();
70  for (var i = 0; i < size; i++) {
71    y = y[0];
72  }
73
74  if (array_in_middle) {
75    assertEquals(42.2, y[0], "y array in middle");
76    y[0] = 41.2;
77  } else {
78    assertEquals(42.2, y.a, "y object in middle");
79    y.a = 41.2;
80  }
81}
82
83// The sizes to test.
84var sizes = [1, 2, 100, 200, 300];
85
86// Run the test.
87for (var i = 0; i < sizes.length; i++) {
88  testLiteral(sizes[i], false);
89  testLiteral(sizes[i], true);
90}
91
92function testLiteralAndCatch(size) {
93  var big_enough = false;
94  try {
95    testLiteral(size, false);
96  } catch (e) {
97    big_enough = true;
98  }
99  try {
100    testLiteral(size, true);
101  } catch (e) {
102    big_enough = true;
103  }
104  return big_enough;
105}
106
107// Catch stack overflows.
108
109testLiteralAndCatch(1000) ||
110testLiteralAndCatch(20000) ||
111testLiteralAndCatch(200000);
112