blob: 74f185bb1cd065b85ae1a2fed5b2d2200a309fdd [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 assign1(x) { x += 1; o.x = x; }
33assign1(max_smi);
34assertEquals(max_smi + 1, o.x);
35
36assign1(1.1);
37assertEquals(2.1, o.x);
38
39
40// Test deopt with count operation on named property.
41function assign2(p) { p.x += 1 }
42
43o.x = "42";
44assign2(o);
45assertEquals("421", o.x);
46
47var s = max_smi - 10000;
48o.x = s;
49for(var i = 0; i < 20000; i++) {
50 assign2(o);
51}
52assertEquals(max_smi + 10000, o.x);
53
54
55// Test deopt with count operation on keyed property.
56function assign3(a, b) { a[b] += 1; }
57
58o = ["42"];
59assign3(o, 0);
60assertEquals("421", o[0]);
61
62var s = max_smi - 10000;
63o[0] = s;
64for(var i = 0; i < 20000; i++) {
65 assign3(o, 0);
66}
67assertEquals(max_smi + 10000, o[0]);
68
69assign3(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 assign3(o, 0);
77}
78assign3(o,1);
79
80// Test bailout with count operation in a value context.
81function assign5(x,y) { return (x += 1) + y; }
82for (var i = 0; i < 10000; ++i) assertEquals(4, assign5(2, 1));
83assertEquals(4.1, assign5(2, 1.1));
84assertEquals(4.1, assign5(2.1, 1));
85
86function assign7(o,y) { return (o.x += 1) + y; }
87o = {x:0};
88for (var i = 0; i < 10000; ++i) {
89 o.x = 42;
90 assertEquals(44, assign7(o, 1));
91}
92o.x = 42;
93assertEquals(44.1, assign7(o, 1.1));
94o.x = 42.1;
95assertEquals(44.1, assign7(o, 1));
96
97function assign9(o,y) { return (o[0] += 1) + y; }
98q = [0];
99for (var i = 0; i < 10000; ++i) {
100 q[0] = 42;
101 assertEquals(44, assign9(q, 1));
102}
103q[0] = 42;
104assertEquals(44.1, assign9(q, 1.1));
105q[0] = 42.1;
106assertEquals(44.1, assign9(q, 1));
107
108// Test deopt because of a failed map check on the load.
109function assign10(p) { return p.x += 1 }
110var g1 = {x:0};
111var g2 = {y:0, x:42};
112for (var i = 0; i < 10000; ++i) {
113 g1.x = 42;
114 assertEquals(43, assign10(g1));
115 assertEquals(43, g1.x);
116}
117assertEquals(43, assign10(g2));
118assertEquals(43, g2.x);
119
120// Test deopt because of a failed map check on the store.
121// The binary operation changes the map as a side effect.
122o = {x:0};
123var g3 = { valueOf: function() { o.y = "bar"; return 42; }};
124function assign11(p) { return p.x += 1; }
125
126for (var i = 0; i < 10000; i++) {
127 o.x = "a";
128 assign11(o);
129}
130assertEquals("a11", assign11(o));
131o.x = g3;
132assertEquals(43, assign11(o));
133assertEquals("bar", o.y);
134
135o = [0];
136var g4 = { valueOf: function() { o.y = "bar"; return 42; }};
137function assign12(p) { return p[0] += 1; }
138
139for (var i = 0; i < 1000000; i++) {
140 o[0] = "a";
141 assign12(o);
142}
143assertEquals("a11", assign12(o));
144o[0] = g4;
145assertEquals(43, assign12(o));
146assertEquals("bar", o.y);