Chromium Code Reviews| Index: test/mjsunit/compiler/inline-accessors.js |
| diff --git a/test/mjsunit/compiler/inline-accessors.js b/test/mjsunit/compiler/inline-accessors.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..84fbc82dbaa91e96786abb1968ea935063d50f36 |
| --- /dev/null |
| +++ b/test/mjsunit/compiler/inline-accessors.js |
| @@ -0,0 +1,246 @@ |
| +// Copyright 2012 the V8 project authors. All rights reserved. |
| +// Redistribution and use in source and binary forms, with or without |
| +// modification, are permitted provided that the following conditions are |
| +// met: |
| +// |
| +// * Redistributions of source code must retain the above copyright |
| +// notice, this list of conditions and the following disclaimer. |
| +// * Redistributions in binary form must reproduce the above |
| +// copyright notice, this list of conditions and the following |
| +// disclaimer in the documentation and/or other materials provided |
| +// with the distribution. |
| +// * Neither the name of Google Inc. nor the names of its |
| +// contributors may be used to endorse or promote products derived |
| +// from this software without specific prior written permission. |
| +// |
| +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| + |
| +// Flags: --allow-natives-syntax --inline-accessors |
| + |
| +var accessorCallCount, setterArgument, setterValue, obj, forceDeopt; |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Helpers for testing inlining of getters. |
| + |
| +function TestInlinedGetter(context, expected) { |
| + forceDeopt = 0; |
| + accessorCallCount = 0; |
| + |
| + assertEquals(expected, context()); |
| + assertEquals(1, accessorCallCount); |
| + |
| + assertEquals(expected, context()); |
| + assertEquals(2, accessorCallCount); |
| + |
| + %OptimizeFunctionOnNextCall(context); |
| + assertEquals(expected, context()); |
| + assertEquals(3, accessorCallCount); |
| + |
| + %DeoptimizeFunction(context); |
| + %ClearFunctionTypeFeedback(context); |
| +} |
| + |
| + |
| +function TestGetterInAllContexts(obj, expected) { |
| + function value_context() { |
| + return obj.getterProperty; |
| + } |
| + TestInlinedGetter(value_context, expected); |
| + |
| + function test_context() { |
| + if (obj.getterProperty) { |
| + return 111; |
| + } else { |
| + return 222; |
| + } |
| + } |
| + TestInlinedGetter(test_context, expected ? 111 : 222); |
| + |
| + function effect_context() { |
| + obj.getterProperty; |
| + return 5678; |
| + } |
| + TestInlinedGetter(effect_context, 5678); |
| +} |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test getter returning something 'true'ish in all contexts. |
| + |
| +function getter1() { |
| + assertEquals(obj, this); |
|
Michael Starzinger
2012/08/09 12:20:55
I would really use assertSame() here instead, beca
Sven Panne
2012/08/09 14:06:37
Done.
|
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + return 1234; |
| +} |
| + |
| +function ConstrG1() { } |
| +obj = Object.defineProperty(new ConstrG1(), "getterProperty", { get: getter1 }); |
| +TestGetterInAllContexts(obj, 1234); |
| +TestGetterInAllContexts(Object.create(obj), 1234); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test getter returning false in all contexts. |
| + |
| +function getter2() { |
| + assertEquals(obj, this); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + return false; |
| +} |
| + |
| +function ConstrG2() { } |
| +obj = Object.defineProperty(new ConstrG2(), "getterProperty", { get: getter2 }); |
| +TestGetterInAllContexts(obj, false); |
| +TestGetterInAllContexts(Object.create(obj), false); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test getter without a return in all contexts. |
| + |
| +function getter3() { |
| + assertEquals(obj, this); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| +} |
| + |
| +function ConstrG3() { } |
| +obj = Object.defineProperty(new ConstrG3(), "getterProperty", { get: getter3 }); |
| +TestGetterInAllContexts(obj, undefined); |
| +TestGetterInAllContexts(Object.create(obj), undefined); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test getter with a mismatch in the number of arguments in all contexts. |
| + |
| +function getter4(a) { |
| + assertEquals(obj, this); |
| + assertEquals(undefined, a); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + return 9876; |
| +} |
| + |
| +function ConstrG4() { } |
| +obj = Object.defineProperty(new ConstrG4(), "getterProperty", { get: getter4 }); |
| +TestGetterInAllContexts(obj, 9876); |
| +TestGetterInAllContexts(Object.create(obj), 9876); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Helpers for testing inlining of setters. |
| + |
| +function TestInlinedSetter(context, value, expected) { |
| + forceDeopt = 0; |
| + accessorCallCount = 0; |
| + setterArgument = value; |
| + |
| + assertEquals(expected, context(value)); |
| + assertEquals(value, setterValue); |
| + assertEquals(1, accessorCallCount); |
| + |
| + assertEquals(expected, context(value)); |
| + assertEquals(value, setterValue); |
| + assertEquals(2, accessorCallCount); |
| + |
| + %OptimizeFunctionOnNextCall(context); |
| + assertEquals(expected, context(value)); |
| + assertEquals(value, setterValue); |
| + assertEquals(3, accessorCallCount); |
| + |
| + %DeoptimizeFunction(context); |
| + %ClearFunctionTypeFeedback(context); |
| +} |
| + |
| +function TestSetterInAllContexts(obj) { |
| + function value_context(value) { |
| + return obj.setterProperty = value; |
| + } |
| + TestInlinedSetter(value_context, 111, 111); |
| + |
| + function test_context(value) { |
| + if (obj.setterProperty = value) { |
| + return 333; |
| + } else { |
| + return 444; |
| + } |
| + } |
| + TestInlinedSetter(test_context, true, 333); |
| + TestInlinedSetter(test_context, false, 444); |
| + |
| + function effect_context(value) { |
| + obj.setterProperty = value; |
| + return 666; |
| + } |
| + TestInlinedSetter(effect_context, 555, 666); |
| +} |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test setter returning nothing in all contexts. |
| + |
| +function setter1(value) { |
| + assertEquals(obj, this); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + setterValue = value; |
| +} |
| + |
| +function ConstrS1() { } |
| +obj = Object.defineProperty(new ConstrS1(), "setterProperty", { set: setter1 }); |
| +TestSetterInAllContexts(obj); |
| +TestSetterInAllContexts(Object.create(obj)); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test setter returning something different than the RHS in all contexts. |
| + |
| +function setter2(value) { |
| + assertEquals(obj, this); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + setterValue = value; |
| + return 1000000; |
| +} |
| + |
| +function ConstrS2() { } |
| +obj = Object.defineProperty(new ConstrS2(), "setterProperty", { set: setter2 }); |
| +TestSetterInAllContexts(obj); |
| +TestSetterInAllContexts(Object.create(obj)); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test setter with too few arguments in all contexts. |
| + |
| +function setter3() { |
| + assertEquals(obj, this); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + setterValue = setterArgument; |
| + return 1000000; |
| +} |
| + |
| +function ConstrS3() { } |
| +obj = Object.defineProperty(new ConstrS3(), "setterProperty", { set: setter3 }); |
| +TestSetterInAllContexts(obj); |
| +TestSetterInAllContexts(Object.create(obj)); |
| + |
| +// ----------------------------------------------------------------------------- |
| +// Test setter with too many arguments in all contexts. |
| + |
| +function setter4(value, foo) { |
| + assertEquals(obj, this); |
| + assertEquals(undefined, foo); |
| + accessorCallCount++; |
| + forceDeopt + 1; |
| + setterValue = value; |
| + return 1000000; |
| +} |
| + |
| +function ConstrS4() { } |
| +obj = Object.defineProperty(new ConstrS4(), "setterProperty", { set: setter4 }); |
| +TestSetterInAllContexts(obj); |
| +TestSetterInAllContexts(Object.create(obj)); |