blob: f296b08798b0153d656b05c1fa436ee871dcab1d [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// Copyright 2014 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000028// Flags: --allow-natives-syntax
Emily Bernierd0a1eb72015-03-24 16:35:39 -040029
30var global = this;
31
32// For the HConstant
33Array.prototype.fun = function() {
34 funRecv = this;
35 called++;
36 assertEquals(0, arguments.length);
37};
38
39Array.prototype.funStrict = function() {
40 "use strict";
41 funStrictRecv = this;
42 called++;
43 assertEquals(0, arguments.length);
44};
45
46Array.prototype.manyArgs = function() {
47 "use strict";
48 assertEquals(5, arguments.length);
49 assertEquals(0, this);
50 assertEquals(5, arguments[4]);
51 called++;
52}
53
54Array.prototype.manyArgsSloppy = function() {
55 assertEquals(global, this);
56 assertEquals(5, arguments.length);
57 assertEquals(5, arguments[4]);
58 called++;
59}
60
61var array = [];
62for (var i = 0; i < 100; ++i) {
63 array[i] = i;
64}
65
66var copy = array.slice();
67
68function unshiftsArray(num) {
69 [].unshift.call(array, num);
70}
71
72unshiftsArray(50);
73unshiftsArray(60);
74%OptimizeFunctionOnNextCall(unshiftsArray);
75unshiftsArray(80);
76unshiftsArray(50);
77unshiftsArray(60);
78
79copy.unshift(50);
80copy.unshift(60);
81copy.unshift(80);
82copy.unshift(50);
83copy.unshift(60);
84
85assertOptimized(unshiftsArray);
86assertArrayEquals(array, copy);
87
88
89var called = 0;
90var funRecv;
91
92function callNoArgs() {
93 [].fun.call();
94}
95
96callNoArgs();
97callNoArgs();
98assertEquals(this, funRecv);
99%OptimizeFunctionOnNextCall(callNoArgs);
100callNoArgs();
101assertEquals(this, funRecv);
102assertEquals(3, called);
103assertOptimized(callNoArgs);
104
105var funStrictRecv;
106called = 0;
107
108function callStrictNoArgs() {
109 [].funStrict.call();
110}
111
112callStrictNoArgs();
113callStrictNoArgs();
114assertEquals(undefined, funStrictRecv);
115%OptimizeFunctionOnNextCall(callStrictNoArgs);
116callStrictNoArgs();
117assertEquals(undefined, funStrictRecv);
118assertEquals(3, called);
119assertOptimized(callStrictNoArgs);
120
121called = 0;
122
123
124function callManyArgs() {
125 [].manyArgs.call(0, 1, 2, 3, 4, 5);
126}
127
128callManyArgs();
129callManyArgs();
130%OptimizeFunctionOnNextCall(callManyArgs);
131callManyArgs();
132assertOptimized(callManyArgs);
133assertEquals(called, 3);
134
135called = 0;
136
137
138function callManyArgsSloppy() {
139 [].manyArgsSloppy.call(null, 1, 2, 3, 4, 5);
140}
141
142callManyArgsSloppy();
143callManyArgsSloppy();
144%OptimizeFunctionOnNextCall(callManyArgsSloppy);
145callManyArgsSloppy();
146assertOptimized(callManyArgsSloppy);
147assertEquals(called, 3);
148
149var str = "hello";
150var code = str.charCodeAt(3);
151called = 0;
152function callBuiltinIndirectly() {
153 called++;
154 return "".charCodeAt.call(str, 3);
155}
156
157callBuiltinIndirectly();
158callBuiltinIndirectly();
159%OptimizeFunctionOnNextCall(callBuiltinIndirectly);
160assertEquals(code, callBuiltinIndirectly());
161assertOptimized(callBuiltinIndirectly);
162assertEquals(3, called);
163
164this.array = [1,2,3,4,5,6,7,8,9];
165var copy = this.array.slice();
166called = 0;
167
168function callInlineableBuiltinIndirectlyWhileInlined() {
169 called++;
170 return [].push.apply(array, arguments);
171}
172
173function callInlined(num) {
174 return callInlineableBuiltinIndirectlyWhileInlined(num);
175}
176
177callInlineableBuiltinIndirectlyWhileInlined(1);
178callInlineableBuiltinIndirectlyWhileInlined(2);
179%OptimizeFunctionOnNextCall(callInlineableBuiltinIndirectlyWhileInlined);
180callInlineableBuiltinIndirectlyWhileInlined(3);
181assertOptimized(callInlineableBuiltinIndirectlyWhileInlined);
182
183callInlined(1);
184callInlined(2);
185%OptimizeFunctionOnNextCall(callInlined);
186callInlined(3);
187copy.push(1, 2, 3, 1, 2, 3);
188assertOptimized(callInlined);
189assertArrayEquals(copy, this.array);
190assertEquals(6, called);