OLD | NEW |
---|---|
(Empty) | |
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 | |
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 --inline-accessors | |
29 | |
30 var accessorCallCount, setterValue, obj, forceDeopt; | |
31 | |
32 // ----------------------------------------------------------------------------- | |
33 // Helpers for testing inlining of getters. | |
34 | |
35 function TestInlinedGetter(context, expected) { | |
36 forceDeopt = 0; | |
37 accessorCallCount = 0; | |
38 | |
39 assertEquals(expected, context()); | |
40 assertEquals(1, accessorCallCount); | |
41 | |
42 assertEquals(expected, context()); | |
43 assertEquals(2, accessorCallCount); | |
44 | |
45 %OptimizeFunctionOnNextCall(context); | |
46 assertEquals(expected, context()); | |
47 assertEquals(3, accessorCallCount); | |
48 | |
49 %DeoptimizeFunction(context); | |
50 %ClearFunctionTypeFeedback(context); | |
51 } | |
52 | |
53 | |
54 function TestGetterInAllContexts(obj, expected) { | |
55 function value_context() { | |
56 return obj.getterProperty; | |
57 } | |
58 TestInlinedGetter(value_context, expected); | |
59 | |
60 function test_context() { | |
61 if (obj.getterProperty) { | |
62 return 111; | |
63 } else { | |
64 return 222; | |
65 } | |
66 } | |
67 TestInlinedGetter(test_context, expected ? 111 : 222); | |
68 | |
69 function effect_context() { | |
70 obj.getterProperty; | |
71 return 5678; | |
72 } | |
73 TestInlinedGetter(effect_context, 5678); | |
74 } | |
75 | |
76 // ----------------------------------------------------------------------------- | |
77 // Test getter returning something 'true'ish in all contexts. | |
78 | |
79 function getter1() { | |
Michael Starzinger
2012/08/08 15:01:29
It might be a good idea to assert that the receive
Sven Panne
2012/08/09 12:15:32
Done.
| |
80 accessorCallCount++; | |
81 forceDeopt + 1; | |
82 return 1234; | |
83 } | |
84 | |
85 function Constr1() { } | |
86 obj = Object.defineProperty(new Constr1(), "getterProperty", { get: getter1 }); | |
87 TestGetterInAllContexts(obj, 1234); | |
88 TestGetterInAllContexts(Object.create(obj), 1234); | |
89 | |
90 // ----------------------------------------------------------------------------- | |
91 // Test getter returning false in all contexts. | |
92 | |
93 function getter2() { | |
94 accessorCallCount++; | |
95 forceDeopt + 1; | |
96 return false; | |
97 } | |
98 | |
99 function Constr2() { } | |
100 obj = Object.defineProperty(new Constr2(), "getterProperty", { get: getter2 }); | |
101 TestGetterInAllContexts(obj, false); | |
102 TestGetterInAllContexts(Object.create(obj), false); | |
103 | |
104 // ----------------------------------------------------------------------------- | |
105 // Test getter without a return in all contexts. | |
106 | |
107 function getter3() { | |
108 accessorCallCount++; | |
109 forceDeopt + 1; | |
110 } | |
111 | |
112 function Constr3() { } | |
113 obj = Object.defineProperty(new Constr3(), "getterProperty", { get: getter3 }); | |
114 TestGetterInAllContexts(obj, undefined); | |
115 TestGetterInAllContexts(Object.create(obj), undefined); | |
116 | |
117 // ----------------------------------------------------------------------------- | |
118 // Test getter with a mismatch in the number of arguments in all contexts. | |
119 | |
120 function getter4(a) { | |
121 assertEquals(undefined, a); | |
122 accessorCallCount++; | |
123 forceDeopt + 1; | |
124 return 9876; | |
125 } | |
126 | |
127 function Constr4() { } | |
128 obj = Object.defineProperty(new Constr4(), "getterProperty", { get: getter4 }); | |
129 TestGetterInAllContexts(obj, 9876); | |
130 TestGetterInAllContexts(Object.create(obj), 9876); | |
131 | |
132 // ----------------------------------------------------------------------------- | |
133 // Helpers for testing inlining of setters. | |
134 | |
135 function TestInlinedSetter(context, value, expected) { | |
136 accessorCallCount = 0; | |
137 | |
138 assertEquals(expected, context(value)); | |
139 assertEquals(value, setterValue); | |
140 assertEquals(1, accessorCallCount); | |
141 | |
142 assertEquals(expected, context(value)); | |
143 assertEquals(value, setterValue); | |
144 assertEquals(2, accessorCallCount); | |
145 | |
146 %OptimizeFunctionOnNextCall(context); | |
147 assertEquals(expected, context(value)); | |
148 assertEquals(value, setterValue); | |
149 assertEquals(3, accessorCallCount); | |
150 | |
151 %DeoptimizeFunction(context); | |
152 %ClearFunctionTypeFeedback(context); | |
153 } | |
154 | |
155 function TestSetterInAllContexts(obj) { | |
156 function value_context(value) { | |
157 return obj.setterProperty = value; | |
158 } | |
159 TestInlinedSetter(value_context, 111, 111); | |
160 | |
161 function test_context(value) { | |
162 if (obj.setterProperty = value) { | |
163 return 333; | |
164 } else { | |
165 return 444; | |
166 } | |
167 } | |
168 TestInlinedSetter(test_context, true, 333); | |
169 TestInlinedSetter(test_context, false, 444); | |
170 | |
171 function effect_context(value) { | |
172 obj.setterProperty = value; | |
173 return 666; | |
174 } | |
175 TestInlinedSetter(effect_context, 555, 666); | |
176 } | |
177 | |
178 // ----------------------------------------------------------------------------- | |
179 // Test setter returning nothing in all contexts. | |
180 | |
181 function setter1(value) { | |
182 accessorCallCount++; | |
183 forceDeopt + 1; | |
184 setterValue = value; | |
185 } | |
186 | |
187 function Constr5() { } | |
188 obj = Object.defineProperty(new Constr5(), "setterProperty", { set: setter1 }); | |
189 TestSetterInAllContexts(obj); | |
190 TestSetterInAllContexts(Object.create(obj)); | |
191 | |
192 // ----------------------------------------------------------------------------- | |
193 // Test setter returning something different than the RHS in all contexts. | |
194 | |
195 function setter2(value) { | |
196 accessorCallCount++; | |
197 forceDeopt + 1; | |
198 setterValue = value; | |
199 return 1000000; | |
200 } | |
201 | |
202 function Constr6() { } | |
203 obj = Object.defineProperty(new Constr6(), "setterProperty", { set: setter2 }); | |
204 TestSetterInAllContexts(obj); | |
205 TestSetterInAllContexts(Object.create(obj)); | |
206 | |
207 // ----------------------------------------------------------------------------- | |
208 // Test setter with too few arguments in all contexts. | |
209 // TODO(svenpanne) Write me! | |
210 | |
211 // ----------------------------------------------------------------------------- | |
212 // Test setter with too many arguments in all contexts. | |
213 // TODO(svenpanne) Write me! | |
OLD | NEW |