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, setterArgument, 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() { |
| 80 assertSame(obj, this); |
| 81 accessorCallCount++; |
| 82 forceDeopt + 1; |
| 83 return 1234; |
| 84 } |
| 85 |
| 86 function ConstrG1() { } |
| 87 obj = Object.defineProperty(new ConstrG1(), "getterProperty", { get: getter1 }); |
| 88 TestGetterInAllContexts(obj, 1234); |
| 89 obj = Object.create(obj); |
| 90 TestGetterInAllContexts(obj, 1234); |
| 91 |
| 92 // ----------------------------------------------------------------------------- |
| 93 // Test getter returning false in all contexts. |
| 94 |
| 95 function getter2() { |
| 96 assertSame(obj, this); |
| 97 accessorCallCount++; |
| 98 forceDeopt + 1; |
| 99 return false; |
| 100 } |
| 101 |
| 102 function ConstrG2() { } |
| 103 obj = Object.defineProperty(new ConstrG2(), "getterProperty", { get: getter2 }); |
| 104 TestGetterInAllContexts(obj, false); |
| 105 obj = Object.create(obj); |
| 106 TestGetterInAllContexts(obj, false); |
| 107 |
| 108 // ----------------------------------------------------------------------------- |
| 109 // Test getter without a return in all contexts. |
| 110 |
| 111 function getter3() { |
| 112 assertSame(obj, this); |
| 113 accessorCallCount++; |
| 114 forceDeopt + 1; |
| 115 } |
| 116 |
| 117 function ConstrG3() { } |
| 118 obj = Object.defineProperty(new ConstrG3(), "getterProperty", { get: getter3 }); |
| 119 TestGetterInAllContexts(obj, undefined); |
| 120 obj = Object.create(obj); |
| 121 TestGetterInAllContexts(obj, undefined); |
| 122 |
| 123 // ----------------------------------------------------------------------------- |
| 124 // Test getter with too many arguments without a return in all contexts. |
| 125 |
| 126 function getter4(a) { |
| 127 assertSame(obj, this); |
| 128 assertEquals(undefined, a); |
| 129 accessorCallCount++; |
| 130 forceDeopt + 1; |
| 131 } |
| 132 |
| 133 function ConstrG4() { } |
| 134 obj = Object.defineProperty(new ConstrG4(), "getterProperty", { get: getter4 }); |
| 135 TestGetterInAllContexts(obj, undefined); |
| 136 obj = Object.create(obj); |
| 137 TestGetterInAllContexts(obj, undefined); |
| 138 |
| 139 // ----------------------------------------------------------------------------- |
| 140 // Test getter with too many arguments with a return in all contexts. |
| 141 |
| 142 function getter5(a) { |
| 143 assertSame(obj, this); |
| 144 assertEquals(undefined, a); |
| 145 accessorCallCount++; |
| 146 forceDeopt + 1; |
| 147 return 9876; |
| 148 } |
| 149 |
| 150 function ConstrG5() { } |
| 151 obj = Object.defineProperty(new ConstrG5(), "getterProperty", { get: getter5 }); |
| 152 TestGetterInAllContexts(obj, 9876); |
| 153 obj = Object.create(obj); |
| 154 TestGetterInAllContexts(obj, 9876); |
| 155 |
| 156 // ----------------------------------------------------------------------------- |
| 157 // Helpers for testing inlining of setters. |
| 158 |
| 159 function TestInlinedSetter(context, value, expected) { |
| 160 forceDeopt = 0; |
| 161 accessorCallCount = 0; |
| 162 setterArgument = value; |
| 163 |
| 164 assertEquals(expected, context(value)); |
| 165 assertEquals(value, setterValue); |
| 166 assertEquals(1, accessorCallCount); |
| 167 |
| 168 assertEquals(expected, context(value)); |
| 169 assertEquals(value, setterValue); |
| 170 assertEquals(2, accessorCallCount); |
| 171 |
| 172 %OptimizeFunctionOnNextCall(context); |
| 173 assertEquals(expected, context(value)); |
| 174 assertEquals(value, setterValue); |
| 175 assertEquals(3, accessorCallCount); |
| 176 |
| 177 %DeoptimizeFunction(context); |
| 178 %ClearFunctionTypeFeedback(context); |
| 179 } |
| 180 |
| 181 function TestSetterInAllContexts(obj) { |
| 182 function value_context(value) { |
| 183 return obj.setterProperty = value; |
| 184 } |
| 185 TestInlinedSetter(value_context, 111, 111); |
| 186 |
| 187 function test_context(value) { |
| 188 if (obj.setterProperty = value) { |
| 189 return 333; |
| 190 } else { |
| 191 return 444; |
| 192 } |
| 193 } |
| 194 TestInlinedSetter(test_context, true, 333); |
| 195 TestInlinedSetter(test_context, false, 444); |
| 196 |
| 197 function effect_context(value) { |
| 198 obj.setterProperty = value; |
| 199 return 666; |
| 200 } |
| 201 TestInlinedSetter(effect_context, 555, 666); |
| 202 } |
| 203 |
| 204 // ----------------------------------------------------------------------------- |
| 205 // Test setter without a return in all contexts. |
| 206 |
| 207 function setter1(value) { |
| 208 assertSame(obj, this); |
| 209 accessorCallCount++; |
| 210 forceDeopt + 1; |
| 211 setterValue = value; |
| 212 } |
| 213 |
| 214 function ConstrS1() { } |
| 215 obj = Object.defineProperty(new ConstrS1(), "setterProperty", { set: setter1 }); |
| 216 TestSetterInAllContexts(obj); |
| 217 obj = Object.create(obj); |
| 218 TestSetterInAllContexts(obj); |
| 219 |
| 220 // ----------------------------------------------------------------------------- |
| 221 // Test setter returning something different than the RHS in all contexts. |
| 222 |
| 223 function setter2(value) { |
| 224 assertSame(obj, this); |
| 225 accessorCallCount++; |
| 226 forceDeopt + 1; |
| 227 setterValue = value; |
| 228 return 1000000; |
| 229 } |
| 230 |
| 231 function ConstrS2() { } |
| 232 obj = Object.defineProperty(new ConstrS2(), "setterProperty", { set: setter2 }); |
| 233 TestSetterInAllContexts(obj); |
| 234 obj = Object.create(obj); |
| 235 TestSetterInAllContexts(obj); |
| 236 |
| 237 // ----------------------------------------------------------------------------- |
| 238 // Test setter with too few arguments without a return in all contexts. |
| 239 |
| 240 function setter3() { |
| 241 assertSame(obj, this); |
| 242 accessorCallCount++; |
| 243 forceDeopt + 1; |
| 244 setterValue = setterArgument; |
| 245 } |
| 246 |
| 247 function ConstrS3() { } |
| 248 obj = Object.defineProperty(new ConstrS3(), "setterProperty", { set: setter3 }); |
| 249 TestSetterInAllContexts(obj); |
| 250 obj = Object.create(obj); |
| 251 TestSetterInAllContexts(obj); |
| 252 |
| 253 // ----------------------------------------------------------------------------- |
| 254 // Test setter with too few arguments with a return in all contexts. |
| 255 |
| 256 function setter4() { |
| 257 assertSame(obj, this); |
| 258 accessorCallCount++; |
| 259 forceDeopt + 1; |
| 260 setterValue = setterArgument; |
| 261 return 2000000; |
| 262 } |
| 263 |
| 264 function ConstrS4() { } |
| 265 obj = Object.defineProperty(new ConstrS4(), "setterProperty", { set: setter4 }); |
| 266 TestSetterInAllContexts(obj); |
| 267 obj = Object.create(obj); |
| 268 TestSetterInAllContexts(obj); |
| 269 |
| 270 // ----------------------------------------------------------------------------- |
| 271 // Test setter with too many arguments without a return in all contexts. |
| 272 |
| 273 function setter5(value, foo) { |
| 274 assertSame(obj, this); |
| 275 assertEquals(undefined, foo); |
| 276 accessorCallCount++; |
| 277 forceDeopt + 1; |
| 278 setterValue = value; |
| 279 } |
| 280 |
| 281 function ConstrS5() { } |
| 282 obj = Object.defineProperty(new ConstrS5(), "setterProperty", { set: setter5 }); |
| 283 TestSetterInAllContexts(obj); |
| 284 obj = Object.create(obj); |
| 285 TestSetterInAllContexts(obj); |
| 286 |
| 287 // ----------------------------------------------------------------------------- |
| 288 // Test setter with too many arguments with a return in all contexts. |
| 289 |
| 290 function setter6(value, foo) { |
| 291 assertSame(obj, this); |
| 292 assertEquals(undefined, foo); |
| 293 accessorCallCount++; |
| 294 forceDeopt + 1; |
| 295 setterValue = value; |
| 296 return 3000000; |
| 297 } |
| 298 |
| 299 function ConstrS6() { } |
| 300 obj = Object.defineProperty(new ConstrS6(), "setterProperty", { set: setter6 }); |
| 301 TestSetterInAllContexts(obj); |
| 302 obj = Object.create(obj); |
| 303 TestSetterInAllContexts(obj); |
OLD | NEW |