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 --expose-gc
29 // Flags: --noalways-opt
32 fast_smi_only : 'fast smi only elements',
33 fast : 'fast elements',
34 fast_double : 'fast double elements',
35 dictionary : 'dictionary elements',
36 external_byte : 'external byte elements',
37 external_unsigned_byte : 'external unsigned byte elements',
38 external_short : 'external short elements',
39 external_unsigned_short : 'external unsigned short elements',
40 external_int : 'external int elements',
41 external_unsigned_int : 'external unsigned int elements',
42 external_float : 'external float elements',
43 external_double : 'external double elements',
44 external_pixel : 'external pixel elements'
47 function getKind(obj) {
48 if (%HasFastSmiElements(obj)) return elements_kind.fast_smi_only;
49 if (%HasFastObjectElements(obj)) return elements_kind.fast;
50 if (%HasFastDoubleElements(obj)) return elements_kind.fast_double;
51 if (%HasDictionaryElements(obj)) return elements_kind.dictionary;
54 function isHoley(obj) {
55 if (%HasFastHoleyElements(obj)) return true;
59 function assertKind(expected, obj, name_opt) {
60 assertEquals(expected, getKind(obj), name_opt);
63 // Verify that basic elements kind feedback works for non-constructor
64 // array calls (as long as the call is made through an IC, and not
71 // Calls through ICs need warm up through uninitialized, then
72 // premonomorphic first.
75 assertKind(elements_kind.fast_smi_only, a);
78 assertKind(elements_kind.fast_double, b);
80 function create1(arg) {
87 assertFalse(isHoley(a));
88 assertKind(elements_kind.fast_smi_only, a);
91 assertTrue(isHoley(b));
92 assertKind(elements_kind.fast, b);
95 assertKind(elements_kind.fast_smi_only, a);
97 function create3(arg1, arg2, arg3) {
98 return Array(arg1, arg2, arg3);
105 assertKind(elements_kind.fast_double, a);
107 assertKind(elements_kind.fast_double, b);
108 assertFalse(isHoley(b));
112 // Verify that keyed calls work
114 function create0(name) {
124 assertKind(elements_kind.fast_double, b);
128 // Verify that feedback is turned off if the call site goes megamorphic.
130 function foo(arg) { return arg(); }
140 // b doesn't benefit from elements kind feedback at a megamorphic site.
141 assertKind(elements_kind.fast_smi_only, b);
145 // Verify that crankshaft consumes type feedback.
155 %OptimizeFunctionOnNextCall(create0);
159 assertKind(elements_kind.fast_double, b);
160 assertOptimized(create0);
162 function create1(arg) {
170 %OptimizeFunctionOnNextCall(create1);
172 assertKind(elements_kind.fast_double, b);
173 assertOptimized(create1);
175 function createN(arg1, arg2, arg3) {
176 return Array(arg1, arg2, arg3);
181 a = createN(1, 2, 3);
183 %OptimizeFunctionOnNextCall(createN);
184 b = createN(1, 2, 3);
185 assertKind(elements_kind.fast_double, b);
186 assertOptimized(createN);
189 // Verify that cross context calls work
191 var realmA = Realm.current();
192 var realmB = Realm.create();
193 assertEquals(0, realmA);
194 assertEquals(1, realmB);
196 function instanceof_check(type) {
197 assertTrue(type() instanceof type);
198 assertTrue(type(5) instanceof type);
199 assertTrue(type(1,2,3) instanceof type);
202 var realmBArray = Realm.eval(realmB, "Array");
203 instanceof_check(Array);
204 instanceof_check(Array);
205 instanceof_check(Array);
206 instanceof_check(realmBArray);
207 instanceof_check(realmBArray);
208 instanceof_check(realmBArray);