blob: dcd82f8774b8df2d0e922648380b2105ba4f4d2a [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2010 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// Test deopt with count operation on parameter.
29var max_smi = 1073741823;
30var o = {x:0};
31
32function inc1(x) { x++; o.x = x; }
33inc1(max_smi);
34assertEquals(max_smi + 1, o.x);
35
36inc1(1.1);
37assertEquals(2.1, o.x);
38
39
40// Test deopt with count operation on named property.
41function inc2(p) { p.x++ }
42
43o.x = "42";
44inc2(o);
45assertEquals(43, o.x);
46
47var s = max_smi - 10000;
48o.x = s;
49for(var i = 0; i < 20000; i++) {
50 inc2(o);
51}
52assertEquals(max_smi + 10000, o.x);
53
54
55// Test deopt with count operation on keyed property.
56function inc3(a, b) { a[b]++; }
57
58o = ["42"];
59inc3(o, 0);
60assertEquals(43, o[0]);
61
62var s = max_smi - 10000;
63o[0] = s;
64for(var i = 0; i < 20000; i++) {
65 inc3(o, 0);
66}
67assertEquals(max_smi + 10000, o[0]);
68
69inc3(o,"0");
70
71assertEquals(max_smi + 10001, o[0]);
72
73// Test bailout when accessing a non-existing array element.
74o[0] = 0;
75for(var i = 0; i < 10000; i++) {
76 inc3(o, 0);
77}
78inc3(o,1);
79
80// Test bailout with count operation in a value context.
81function inc4(x,y) { return (x++) + y; }
82for (var i = 0; i < 100000; ++i) assertEquals(3, inc4(2, 1));
83assertEquals(3.1, inc4(2, 1.1));
84
85function inc5(x,y) { return (++x) + y; }
86for (var i = 0; i < 100000; ++i) assertEquals(4, inc5(2, 1));
87assertEquals(4.1, inc5(2, 1.1));
88assertEquals(4.1, inc5(2.1, 1));
89
90function inc6(o,y) { return (o.x++) + y; }
91o = {x:0};
92for (var i = 0; i < 10000; ++i) {
93 o.x = 42;
94 assertEquals(43, inc6(o, 1));
95}
96o.x = 42;
97assertEquals(43.1, inc6(o, 1.1));
98o.x = 42.1;
99assertEquals(43.1, inc6(o, 1));
100
101function inc7(o,y) { return (++o.x) + y; }
102o = {x:0};
103for (var i = 0; i < 10000; ++i) {
104 o.x = 42;
105 assertEquals(44, inc7(o, 1));
106}
107o.x = 42;
108assertEquals(44.1, inc7(o, 1.1));
109o.x = 42.1;
110assertEquals(44.1, inc7(o, 1));
111
112function inc8(o,y) { return (o[0]++) + y; }
113var q = [0];
114for (var i = 0; i < 100000; ++i) {
115 q[0] = 42;
116 assertEquals(43, inc8(q, 1));
117}
118q[0] = 42;
119assertEquals(43.1, inc8(q, 1.1));
120q[0] = 42.1;
121assertEquals(43.1, inc8(q, 1));
122
123function inc9(o,y) { return (++o[0]) + y; }
124q = [0];
125for (var i = 0; i < 100000; ++i) {
126 q[0] = 42;
127 assertEquals(44, inc9(q, 1));
128}
129q[0] = 42;
130assertEquals(44.1, inc9(q, 1.1));
131q[0] = 42.1;
132assertEquals(44.1, inc9(q, 1));
133
134// Test deopt because of a failed map check.
135function inc10(p) { return p.x++ }
136var g1 = {x:0};
137var g2 = {y:0, x:42}
138for (var i = 0; i < 10000; ++i) {
139 g1.x = 42;
140 assertEquals(42, inc10(g1));
141 assertEquals(43, g1.x);
142}
143assertEquals(42, inc10(g2));
144assertEquals(43, g2.x);
145
146// Test deoptimization with postfix operation in a value context.
147function inc11(a) { return a[this.x++]; }
148var g3 = {x:null, f:inc11};
149var g4 = [42];
150assertEquals(42, g3.f(g4));