1// Copyright 2013 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 --turbo-deoptimization
29
30var o1 = {x:1};
31var o2 = {};
32var deopt_getter = false;
33var deopt_setter = false;
34
35function f_mono(o) {
36  return 5 + o.x++;
37}
38
39var to_deopt = f_mono;
40
41var v = 1;
42var g = 0;
43var s = 0;
44
45Object.defineProperty(o2, "x",
46    {get:function() {
47       g++;
48       if (deopt_getter) {
49         deopt_getter = false;
50         %DeoptimizeFunction(to_deopt);
51       }
52       return v;
53     },
54     set:function(new_v) {
55       v = new_v;
56       s++;
57       if (deopt_setter) {
58         deopt_setter = false;
59         %DeoptimizeFunction(to_deopt);
60       }
61     }});
62
63assertEquals(6, f_mono(o2));
64assertEquals(1, g);
65assertEquals(1, s);
66assertEquals(7, f_mono(o2));
67assertEquals(2, g);
68assertEquals(2, s);
69%OptimizeFunctionOnNextCall(f_mono);
70deopt_setter = true;
71assertEquals(8, f_mono(o2));
72assertEquals(3, g);
73assertEquals(3, s);
74
75function f_poly(o) {
76  return 5 + o.x++;
77}
78
79v = 1;
80to_deopt = f_poly;
81
82f_poly(o1);
83f_poly(o1);
84assertEquals(6, f_poly(o2));
85assertEquals(4, g);
86assertEquals(4, s);
87assertEquals(7, f_poly(o2));
88assertEquals(5, g);
89assertEquals(5, s);
90%OptimizeFunctionOnNextCall(f_poly);
91deopt_setter = true;
92assertEquals(8, f_poly(o2));
93assertEquals(6, g);
94assertEquals(6, s);
95
96%OptimizeFunctionOnNextCall(f_poly);
97v = undefined;
98assertEquals(NaN, f_poly(o2));
99assertEquals(7, g);
100assertEquals(7, s);
101
102function f_pre(o) {
103  return 5 + ++o.x;
104}
105
106v = 1;
107to_deopt = f_pre;
108
109f_pre(o1);
110f_pre(o1);
111assertEquals(7, f_pre(o2));
112assertEquals(8, g);
113assertEquals(8, s);
114assertEquals(8, f_pre(o2));
115assertEquals(9, g);
116assertEquals(9, s);
117%OptimizeFunctionOnNextCall(f_pre);
118deopt_setter = true;
119assertEquals(9, f_pre(o2));
120assertEquals(10, g);
121assertEquals(10, s);
122
123%OptimizeFunctionOnNextCall(f_pre);
124v = undefined;
125assertEquals(NaN, f_pre(o2));
126assertEquals(11, g);
127assertEquals(11, s);
128
129
130function f_get(o) {
131  return 5 + o.x++;
132}
133
134v = 1;
135to_deopt = f_get;
136
137f_get(o1);
138f_get(o1);
139assertEquals(6, f_get(o2));
140assertEquals(12, g);
141assertEquals(12, s);
142assertEquals(7, f_get(o2));
143assertEquals(13, g);
144assertEquals(13, s);
145%OptimizeFunctionOnNextCall(f_get);
146deopt_getter = true;
147assertEquals(8, f_get(o2));
148assertEquals(14, g);
149assertEquals(14, s);
150
151%OptimizeFunctionOnNextCall(f_get);
152v = undefined;
153assertEquals(NaN, f_get(o2));
154assertEquals(15, g);
155assertEquals(15, s);
156