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: --allow-natives-syntax
29
30// Test optimized versions of array and object literals.
31
32function TestOptimizedLiteral(create, verify) {
33  verify(create(1, 2, 3), 1, 2, 3);
34  verify(create(3, 5, 7), 3, 5, 7);
35  %OptimizeFunctionOnNextCall(create);
36  verify(create(11, 23, 42), 11, 23, 42);
37}
38
39
40// Test shallow array literal.
41function create_arr_shallow(a, b, c) {
42  return [0, a, 0, b, 0, c];
43}
44function verify_arr_shallow(array, a, b, c) {
45  assertSame(6, array.length);
46  assertSame(0, array[0]);
47  assertSame(a, array[1]);
48  assertSame(0, array[2]);
49  assertSame(b, array[3]);
50  assertSame(0, array[4]);
51  assertSame(c, array[5]);
52}
53TestOptimizedLiteral(create_arr_shallow, verify_arr_shallow);
54
55
56// Test nested array literal.
57function create_arr_nested(a, b, c) {
58  return [[0, a], [b, c], [1, 2], 3];
59}
60function verify_arr_nested(array, a, b, c) {
61  assertSame(4, array.length);
62  assertSame(2, array[0].length);
63  assertSame(0, array[0][0]);
64  assertSame(a, array[0][1]);
65  assertSame(2, array[1].length);
66  assertSame(b, array[1][0]);
67  assertSame(c, array[1][1]);
68  assertSame(2, array[2].length);
69  assertSame(1, array[2][0]);
70  assertSame(2, array[2][1]);
71  assertSame(3, array[3]);
72}
73TestOptimizedLiteral(create_arr_nested, verify_arr_nested);
74
75
76// Test shallow object literal.
77function create_obj_shallow(a, b, c) {
78  return { x:a, y:b, z:c, v:'foo', 9:'bar' };
79}
80function verify_obj_shallow(object, a, b, c) {
81  assertSame(a, object.x);
82  assertSame(b, object.y);
83  assertSame(c, object.z);
84  assertSame('foo', object.v);
85  assertSame('bar', object[9]);
86}
87TestOptimizedLiteral(create_obj_shallow, verify_obj_shallow);
88
89
90// Test nested object literal.
91function create_obj_nested(a, b, c) {
92  return { x:{ v:a, w:b }, y:{ v:1, w:2 }, z:c, v:'foo', 9:'bar' };
93}
94function verify_obj_nested(object, a, b, c) {
95  assertSame(a, object.x.v);
96  assertSame(b, object.x.w);
97  assertSame(1, object.y.v);
98  assertSame(2, object.y.w);
99  assertSame(c, object.z);
100  assertSame('foo', object.v);
101  assertSame('bar', object[9]);
102}
103TestOptimizedLiteral(create_obj_nested, verify_obj_nested);
104
105
106// Test mixed array and object literal.
107function create_mixed_nested(a, b, c) {
108  return { x:[1, 2], y:[a, b], z:c, v:{ v:'foo' }, 9:'bar' };
109}
110function verify_mixed_nested(object, a, b, c) {
111  assertSame(2, object.x.length);
112  assertSame(1, object.x[0]);
113  assertSame(2, object.x[1]);
114  assertSame(2, object.y.length);
115  assertSame(a, object.y[0]);
116  assertSame(b, object.y[1]);
117  assertSame(c, object.z);
118  assertSame('foo', object.v.v);
119  assertSame('bar', object[9]);
120}
121TestOptimizedLiteral(create_mixed_nested, verify_mixed_nested);
122