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// Verifies that the KeyedStoreIC correctly handles out-of-bounds stores
29// to an array that grow it by a single element. Test functions are
30// called twice to make sure that the IC is used, first call is handled
31// by the runtime in the miss stub.
32
33function array_store_1(a,b,c) {
34  return (a[b] = c);
35}
36
37// Check handling of the empty array.
38var a = [];
39array_store_1(a, 0, 1);
40a = [];
41array_store_1(a, 0, 1);
42assertEquals(1, a[0]);
43assertEquals(1, array_store_1([], 0, 1));
44
45a = [];
46for (x=0;x<100000;++x) {
47  assertEquals(x, array_store_1(a, x, x));
48}
49
50for (x=0;x<100000;++x) {
51  assertEquals(x, array_store_1([], 0, x));
52}
53
54function array_store_2(a,b,c) {
55  return (a[b] = c);
56}
57
58a = [];
59array_store_2(a, 0, 0.5);
60a = [];
61array_store_2(a, 0, 0.5);
62assertEquals(0.5, a[0]);
63assertEquals(0.5, array_store_2([], 0, 0.5));
64
65function array_store_3(a,b,c) {
66  return (a[b] = c);
67}
68
69x = new Object();
70a = [];
71array_store_3(a, 0, x);
72a = [];
73array_store_3(a, 0, x);
74assertEquals(x, a[0]);
75assertEquals(x, array_store_3([], 0, x));
76
77// Check the handling of COW arrays
78function makeCOW() {
79  return [1];
80}
81
82function array_store_4(a,b,c) {
83  return (a[b] = c);
84}
85
86a = makeCOW();
87array_store_4(a, 1, 1);
88a = makeCOW();
89array_store_4(a, 1, 1);
90assertEquals(1, a[1]);
91assertEquals(1, array_store_4([], 1, 1));
92
93function array_store_5(a,b,c) {
94  return (a[b] = c);
95}
96
97a = makeCOW();
98array_store_5(a, 1, 0.5);
99a = makeCOW();
100array_store_5(a, 1, 0.5);
101assertEquals(0.5, a[1]);
102assertEquals(0.5, array_store_5([], 1, 0.5));
103
104function array_store_6(a,b,c) {
105  return (a[b] = c);
106}
107
108a = makeCOW();
109array_store_6(a, 1, x);
110a = makeCOW();
111array_store_6(a, 1, x);
112assertEquals(x, a[1]);
113assertEquals(x, array_store_6([], 1, x));
114
115// Check the handling of mutable arrays.
116a = new Array(1,2,3);
117array_store_4(a, 3, 1);
118a = new Array(1,2,3);
119array_store_4(a, 3, 1);
120assertEquals(1, a[3]);
121assertEquals(1, array_store_4([], 3, 1));
122
123function array_store_5(a,b,c) {
124  return (a[b] = c);
125}
126
127a = new Array(1,2,3);
128array_store_5(a, 3, 0.5);
129a = new Array(1,2,3);
130array_store_5(a, 3, 0.5);
131assertEquals(0.5, a[3]);
132assertEquals(0.5, array_store_5([], 3, 0.5));
133
134function array_store_6(a,b,c) {
135  return (a[b] = c);
136}
137
138a = new Array(1,2,3);
139array_store_6(a, 3, x);
140a = new Array(1,2,3);
141array_store_6(a, 3, x);
142assertEquals(x, a[3]);
143assertEquals(x, array_store_6([], 3, x));
144
145function array_store_7(a,b,c) {
146  return (a[b] = c);
147}
148
149// Check the handling of mutable arrays of doubles
150var a = new Array(0.5, 1.5);
151array_store_7(a, 2, .5);
152a = new Array(0.5, 1.5);
153array_store_7(a, 2, .5);
154assertEquals(0.5, a[2]);
155a = new Array(0.5, 1.5);
156assertEquals(0.5, array_store_7(a, 2, 0.5));
157
158for (x=0;x<100000;++x) {
159  a = new Array(0.5, 1.5);
160  assertEquals(x, array_store_7(a, 2, x));
161}
162
163function array_store_8(a,b,c) {
164  return (a[b] = c);
165}
166
167var a = new Array(0.5, 1.5);
168array_store_8(a, 2, .5);
169a = new Array(0.5, 1.5);
170array_store_8(a, 10, .5);
171assertEquals(0.5, a[10]);
172
173// Grow the empty array with a double store.
174function array_store_9(a,b,c) {
175  return (a[b] = c);
176}
177
178var a = [];
179array_store_9(a, 0, 0.5);
180a = [];
181array_store_1(a, 0, 0.5);
182assertEquals(0.5, a[0]);
183assertEquals(0.5, array_store_1([], 0, 0.5));
184