Upstream version 5.34.92.0
[platform/framework/web/crosswalk.git] / src / v8 / test / mjsunit / elements-kind.js
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 --smi-only-arrays --expose-gc --nostress-opt
29
30 // Test element kind of objects.
31 // Since --smi-only-arrays affects builtins, its default setting at compile
32 // time sticks if built with snapshot.  If --smi-only-arrays is deactivated
33 // by default, only a no-snapshot build actually has smi-only arrays enabled
34 // in this test case.  Depending on whether smi-only arrays are actually
35 // enabled, this test takes the appropriate code path to check smi-only arrays.
36
37 support_smi_only_arrays = %HasFastSmiElements(new Array(1,2,3,4,5,6,7,8));
38
39 if (support_smi_only_arrays) {
40   print("Tests include smi-only arrays.");
41 } else {
42   print("Tests do NOT include smi-only arrays.");
43 }
44
45 var elements_kind = {
46   fast_smi_only            :  'fast smi only elements',
47   fast                     :  'fast elements',
48   fast_double              :  'fast double elements',
49   dictionary               :  'dictionary elements',
50   external_byte            :  'external byte elements',
51   external_unsigned_byte   :  'external unsigned byte elements',
52   external_short           :  'external short elements',
53   external_unsigned_short  :  'external unsigned short elements',
54   external_int             :  'external int elements',
55   external_unsigned_int    :  'external unsigned int elements',
56   external_float           :  'external float elements',
57   external_double          :  'external double elements',
58   external_pixel           :  'external pixel elements'
59 }
60
61 function getKind(obj) {
62   if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only;
63   if (%HasFastObjectElements(obj)) return elements_kind.fast;
64   if (%HasFastDoubleElements(obj)) return elements_kind.fast_double;
65   if (%HasDictionaryElements(obj)) return elements_kind.dictionary;
66   // Every external kind is also an external array.
67   assertTrue(%HasExternalArrayElements(obj));
68   if (%HasExternalInt8Elements(obj)) {
69     return elements_kind.external_byte;
70   }
71   if (%HasExternalUint8Elements(obj)) {
72     return elements_kind.external_unsigned_byte;
73   }
74   if (%HasExternalInt16Elements(obj)) {
75     return elements_kind.external_short;
76   }
77   if (%HasExternalUint16Elements(obj)) {
78     return elements_kind.external_unsigned_short;
79   }
80   if (%HasExternalInt32Elements(obj)) {
81     return elements_kind.external_int;
82   }
83   if (%HasExternalUint32Elements(obj)) {
84     return elements_kind.external_unsigned_int;
85   }
86   if (%HasExternalFloat32Elements(obj)) {
87     return elements_kind.external_float;
88   }
89   if (%HasExternalFloat64Elements(obj)) {
90     return elements_kind.external_double;
91   }
92   if (%HasExternalUint8ClampedElements(obj)) {
93     return elements_kind.external_pixel;
94   }
95 }
96
97 function assertKind(expected, obj, name_opt) {
98   if (!support_smi_only_arrays &&
99       expected == elements_kind.fast_smi_only) {
100     expected = elements_kind.fast;
101   }
102   assertEquals(expected, getKind(obj), name_opt);
103 }
104
105 var me = {};
106 assertKind(elements_kind.fast, me);
107 me.dance = 0xD15C0;
108 me.drink = 0xC0C0A;
109 assertKind(elements_kind.fast, me);
110
111 if (support_smi_only_arrays) {
112   var too = [1,2,3];
113   assertKind(elements_kind.fast_smi_only, too);
114   too.dance = 0xD15C0;
115   too.drink = 0xC0C0A;
116   assertKind(elements_kind.fast_smi_only, too);
117 }
118
119 // Make sure the element kind transitions from smi when a non-smi is stored.
120 function test_wrapper() {
121   var you = new Array();
122   assertKind(elements_kind.fast_smi_only, you);
123   for (var i = 0; i < 1337; i++) {
124     var val = i;
125     if (i == 1336) {
126       assertKind(elements_kind.fast_smi_only, you);
127       val = new Object();
128     }
129     you[i] = val;
130   }
131   assertKind(elements_kind.fast, you);
132
133   assertKind(elements_kind.dictionary, new Array(0xDECAF));
134
135   var fast_double_array = new Array(0xDECAF);
136   for (var i = 0; i < 0xDECAF; i++) fast_double_array[i] = i / 2;
137   assertKind(elements_kind.fast_double, fast_double_array);
138
139   assertKind(elements_kind.external_byte,           new Int8Array(9001));
140   assertKind(elements_kind.external_unsigned_byte,  new Uint8Array(007));
141   assertKind(elements_kind.external_short,          new Int16Array(666));
142   assertKind(elements_kind.external_unsigned_short, new Uint16Array(42));
143   assertKind(elements_kind.external_int,            new Int32Array(0xF));
144   assertKind(elements_kind.external_unsigned_int,   new Uint32Array(23));
145   assertKind(elements_kind.external_float,          new Float32Array(7));
146   assertKind(elements_kind.external_double,         new Float64Array(0));
147   assertKind(elements_kind.external_pixel,          new Uint8ClampedArray(512));
148
149   // Crankshaft support for smi-only array elements.
150   function monomorphic(array) {
151     assertKind(elements_kind.fast_smi_only, array);
152     for (var i = 0; i < 3; i++) {
153       array[i] = i + 10;
154     }
155     assertKind(elements_kind.fast_smi_only, array);
156     for (var i = 0; i < 3; i++) {
157       var a = array[i];
158       assertEquals(i + 10, a);
159     }
160   }
161   var smi_only = new Array(1, 2, 3);
162   assertKind(elements_kind.fast_smi_only, smi_only);
163   for (var i = 0; i < 3; i++) monomorphic(smi_only);
164     %OptimizeFunctionOnNextCall(monomorphic);
165   monomorphic(smi_only);
166 }
167
168 // The test is called in a wrapper function to eliminate the transition learning
169 // feedback of AllocationSites.
170 test_wrapper();
171 %ClearFunctionTypeFeedback(test_wrapper);
172
173 if (support_smi_only_arrays) {
174   %NeverOptimizeFunction(construct_smis);
175
176   // This code exists to eliminate the learning influence of AllocationSites
177   // on the following tests.
178   var __sequence = 0;
179   function make_array_string() {
180     this.__sequence = this.__sequence + 1;
181     return "/* " + this.__sequence + " */  [0, 0, 0];"
182   }
183   function make_array() {
184     return eval(make_array_string());
185   }
186
187   function construct_smis() {
188     var a = make_array();
189     a[0] = 0;  // Send the COW array map to the steak house.
190     assertKind(elements_kind.fast_smi_only, a);
191     return a;
192   }
193   %NeverOptimizeFunction(construct_doubles);
194   function construct_doubles() {
195     var a = construct_smis();
196     a[0] = 1.5;
197     assertKind(elements_kind.fast_double, a);
198     return a;
199   }
200   %NeverOptimizeFunction(construct_objects);
201   function construct_objects() {
202     var a = construct_smis();
203     a[0] = "one";
204     assertKind(elements_kind.fast, a);
205     return a;
206   }
207
208   // Test crankshafted transition SMI->DOUBLE.
209   %NeverOptimizeFunction(convert_to_double);
210   function convert_to_double(array) {
211     array[1] = 2.5;
212     assertKind(elements_kind.fast_double, array);
213     assertEquals(2.5, array[1]);
214   }
215   var smis = construct_smis();
216   for (var i = 0; i < 3; i++) convert_to_double(smis);
217   %OptimizeFunctionOnNextCall(convert_to_double);
218   smis = construct_smis();
219   convert_to_double(smis);
220   // Test crankshafted transitions SMI->FAST and DOUBLE->FAST.
221   %NeverOptimizeFunction(convert_to_fast);
222   function convert_to_fast(array) {
223     array[1] = "two";
224     assertKind(elements_kind.fast, array);
225     assertEquals("two", array[1]);
226   }
227   smis = construct_smis();
228   for (var i = 0; i < 3; i++) convert_to_fast(smis);
229   var doubles = construct_doubles();
230   for (var i = 0; i < 3; i++) convert_to_fast(doubles);
231   smis = construct_smis();
232   doubles = construct_doubles();
233   %OptimizeFunctionOnNextCall(convert_to_fast);
234   convert_to_fast(smis);
235   convert_to_fast(doubles);
236   // Test transition chain SMI->DOUBLE->FAST (crankshafted function will
237   // transition to FAST directly).
238   %NeverOptimizeFunction(convert_mixed);
239   function convert_mixed(array, value, kind) {
240     array[1] = value;
241     assertKind(kind, array);
242     assertEquals(value, array[1]);
243   }
244   smis = construct_smis();
245   for (var i = 0; i < 3; i++) {
246     convert_mixed(smis, 1.5, elements_kind.fast_double);
247   }
248   doubles = construct_doubles();
249   for (var i = 0; i < 3; i++) {
250     convert_mixed(doubles, "three", elements_kind.fast);
251   }
252   convert_mixed(construct_smis(), "three", elements_kind.fast);
253   convert_mixed(construct_doubles(), "three", elements_kind.fast);
254   %OptimizeFunctionOnNextCall(convert_mixed);
255   smis = construct_smis();
256   doubles = construct_doubles();
257   convert_mixed(smis, 1, elements_kind.fast);
258   convert_mixed(doubles, 1, elements_kind.fast);
259   assertTrue(%HaveSameMap(smis, doubles));
260 }
261
262 // Crankshaft support for smi-only elements in dynamic array literals.
263 function get(foo) { return foo; }  // Used to generate dynamic values.
264
265 function crankshaft_test() {
266   if (support_smi_only_arrays) {
267     var a1 = [get(1), get(2), get(3)];
268     assertKind(elements_kind.fast_smi_only, a1);
269   }
270   var a2 = new Array(get(1), get(2), get(3));
271   assertKind(elements_kind.fast_smi_only, a2);
272   var b = [get(1), get(2), get("three")];
273   assertKind(elements_kind.fast, b);
274   var c = [get(1), get(2), get(3.5)];
275   if (support_smi_only_arrays) {
276     assertKind(elements_kind.fast_double, c);
277   }
278 }
279 for (var i = 0; i < 3; i++) {
280   crankshaft_test();
281 }
282 %OptimizeFunctionOnNextCall(crankshaft_test);
283 crankshaft_test();
284
285 // Elements_kind transitions for arrays.
286
287 // A map can have three different elements_kind transitions: SMI->DOUBLE,
288 // DOUBLE->OBJECT, and SMI->OBJECT. No matter in which order these three are
289 // created, they must always end up with the same FAST map.
290
291 // This test is meaningless without FAST_SMI_ONLY_ELEMENTS.
292 if (support_smi_only_arrays) {
293   // Preparation: create one pair of identical objects for each case.
294   var a = [1, 2, 3];
295   var b = [1, 2, 3];
296   assertTrue(%HaveSameMap(a, b));
297   assertKind(elements_kind.fast_smi_only, a);
298   var c = [1, 2, 3];
299   c["case2"] = true;
300   var d = [1, 2, 3];
301   d["case2"] = true;
302   assertTrue(%HaveSameMap(c, d));
303   assertFalse(%HaveSameMap(a, c));
304   assertKind(elements_kind.fast_smi_only, c);
305   var e = [1, 2, 3];
306   e["case3"] = true;
307   var f = [1, 2, 3];
308   f["case3"] = true;
309   assertTrue(%HaveSameMap(e, f));
310   assertFalse(%HaveSameMap(a, e));
311   assertFalse(%HaveSameMap(c, e));
312   assertKind(elements_kind.fast_smi_only, e);
313   // Case 1: SMI->DOUBLE, DOUBLE->OBJECT, SMI->OBJECT.
314   a[0] = 1.5;
315   assertKind(elements_kind.fast_double, a);
316   a[0] = "foo";
317   assertKind(elements_kind.fast, a);
318   b[0] = "bar";
319   assertTrue(%HaveSameMap(a, b));
320   // Case 2: SMI->DOUBLE, SMI->OBJECT, DOUBLE->OBJECT.
321   c[0] = 1.5;
322   assertKind(elements_kind.fast_double, c);
323   assertFalse(%HaveSameMap(c, d));
324   d[0] = "foo";
325   assertKind(elements_kind.fast, d);
326   assertFalse(%HaveSameMap(c, d));
327   c[0] = "bar";
328   assertTrue(%HaveSameMap(c, d));
329   // Case 3: SMI->OBJECT, SMI->DOUBLE, DOUBLE->OBJECT.
330   e[0] = "foo";
331   assertKind(elements_kind.fast, e);
332   assertFalse(%HaveSameMap(e, f));
333   f[0] = 1.5;
334   assertKind(elements_kind.fast_double, f);
335   assertFalse(%HaveSameMap(e, f));
336   f[0] = "bar";
337   assertKind(elements_kind.fast, f);
338   assertTrue(%HaveSameMap(e, f));
339 }
340
341 // Test if Array.concat() works correctly with DOUBLE elements.
342 if (support_smi_only_arrays) {
343   var a = [1, 2];
344   assertKind(elements_kind.fast_smi_only, a);
345   var b = [4.5, 5.5];
346   assertKind(elements_kind.fast_double, b);
347   var c = a.concat(b);
348   assertEquals([1, 2, 4.5, 5.5], c);
349   assertKind(elements_kind.fast_double, c);
350 }
351
352 // Test that Array.push() correctly handles SMI elements.
353 if (support_smi_only_arrays) {
354   var a = [1, 2];
355   assertKind(elements_kind.fast_smi_only, a);
356   a.push(3, 4, 5);
357   assertKind(elements_kind.fast_smi_only, a);
358   assertEquals([1, 2, 3, 4, 5], a);
359 }
360
361 // Test that Array.splice() and Array.slice() return correct ElementsKinds.
362 if (support_smi_only_arrays) {
363   var a = ["foo", "bar"];
364   assertKind(elements_kind.fast, a);
365   var b = a.splice(0, 1);
366   assertKind(elements_kind.fast, b);
367   var c = a.slice(0, 1);
368   assertKind(elements_kind.fast, c);
369 }
370
371 // Throw away type information in the ICs for next stress run.
372 gc();