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
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.
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.
28 // Flags: --allow-natives-syntax
30 // Verifies that the KeyedStoreIC correctly handles out-of-bounds stores
31 // to an array that grow it by a single element. Test functions are
32 // called twice to make sure that the IC is used, first call is handled
33 // by the runtime in the miss stub.
35 function array_store_1(a,b,c) {
39 // Check handling of the empty array.
41 array_store_1(a, 0, 1);
43 array_store_1(a, 0, 1);
44 assertEquals(1, a[0]);
45 assertEquals(1, array_store_1([], 0, 1));
48 for (x=0;x<100000;++x) {
49 assertEquals(x, array_store_1(a, x, x));
52 for (x=0;x<100000;++x) {
53 assertEquals(x, array_store_1([], 0, x));
56 function array_store_2(a,b,c) {
61 array_store_2(a, 0, 0.5);
63 array_store_2(a, 0, 0.5);
64 assertEquals(0.5, a[0]);
65 assertEquals(0.5, array_store_2([], 0, 0.5));
67 function array_store_3(a,b,c) {
73 array_store_3(a, 0, x);
75 array_store_3(a, 0, x);
76 assertEquals(x, a[0]);
77 assertEquals(x, array_store_3([], 0, x));
79 // Check the handling of COW arrays
84 function array_store_4(a,b,c) {
89 array_store_4(a, 1, 1);
91 array_store_4(a, 1, 1);
92 assertEquals(1, a[1]);
93 assertEquals(1, array_store_4([], 1, 1));
95 function array_store_5(a,b,c) {
100 array_store_5(a, 1, 0.5);
102 array_store_5(a, 1, 0.5);
103 assertEquals(0.5, a[1]);
105 assertEquals(0.5, array_store_5(a, 1, 0.5));
106 assertEquals(undefined, a[0]);
107 assertEquals(0.5, a[1]);
109 function array_store_6(a,b,c) {
114 array_store_6(a, 1, x);
116 array_store_6(a, 1, x);
117 assertEquals(x, a[1]);
118 assertEquals(x, array_store_6([], 1, x));
120 // Check the handling of mutable arrays.
121 a = new Array(1,2,3);
122 array_store_4(a, 3, 1);
123 a = new Array(1,2,3);
124 array_store_4(a, 3, 1);
125 assertEquals(1, a[3]);
126 assertEquals(1, array_store_4([], 3, 1));
128 function array_store_5(a,b,c) {
132 a = new Array(1,2,3);
133 array_store_5(a, 3, 0.5);
134 a = new Array(1,2,3);
135 array_store_5(a, 3, 0.5);
136 assertEquals(0.5, a[3]);
137 assertEquals(0.5, array_store_5([], 3, 0.5));
139 function array_store_6(a,b,c) {
143 a = new Array(1,2,3);
144 array_store_6(a, 3, x);
145 a = new Array(1,2,3);
146 array_store_6(a, 3, x);
147 assertEquals(x, a[3]);
148 assertEquals(x, array_store_6([], 3, x));
150 function array_store_7(a,b,c) {
154 // Check the handling of mutable arrays of doubles
155 var a = new Array(0.5, 1.5);
156 array_store_7(a, 2, .5);
157 a = new Array(0.5, 1.5);
158 array_store_7(a, 2, .5);
159 assertEquals(0.5, a[2]);
160 a = new Array(0.5, 1.5);
161 assertEquals(0.5, array_store_7(a, 2, 0.5));
163 for (x=0;x<100000;++x) {
164 a = new Array(0.5, 1.5);
165 assertEquals(x, array_store_7(a, 2, x));
168 function array_store_8(a,b,c) {
172 var a = new Array(0.5, 1.5);
173 array_store_8(a, 2, .5);
174 a = new Array(0.5, 1.5);
175 array_store_8(a, 10, .5);
176 assertEquals(0.5, a[10]);
178 // Grow the empty array with a double store.
179 function array_store_9(a,b,c) {
184 array_store_9(a, 0, 0.5);
186 array_store_1(a, 0, 0.5);
187 assertEquals(0.5, a[0]);
188 assertEquals(0.5, array_store_1([], 0, 0.5));
191 // Verify that a grow store will deoptimize if the max gap (difference between
192 // the end of an array capacity and a new index) is passed. The wrapper is to
193 // make sure array_store_10 isn't inlined.
196 function grow_store(a,b,c) {
203 %OptimizeFunctionOnNextCall(grow_store);
205 assertOptimized(grow_store);
206 grow_store(a,2048,1);
207 assertUnoptimized(grow_store);
208 %ClearFunctionTypeFeedback(grow_store);
212 // Verify that a polymorphic store and grow IC when crankshafted is still
213 // a grow IC (earlier it would revert to a standard store in the polymorphic
216 function f(o, k, v) {
221 f(a, 1, "hi"); // DOUBLE packed array -> tagged packed grow
227 %OptimizeFunctionOnNextCall(f);
228 // Should be a polymorphic grow stub. If not a grow stub it will deopt.
229 f(new Array("hi"), 1, 3);
231 %ClearFunctionTypeFeedback(f);
235 // Now verify that a polymorphic store (non-growing) IC when crankshafted WILL
236 // deopt if you pass an element out of bounds.
238 function f(o, k, v) {
243 f(a, 0, "hi"); // DOUBLE packed array -> tagged packed grow
249 %OptimizeFunctionOnNextCall(f);
250 f(new Array("hi"), 0, 3);
252 // An attempt to grow should cause deopt
253 f(new Array("hi"), 1, 3);
254 assertUnoptimized(f);
255 %ClearFunctionTypeFeedback(f);