| OLD | NEW |
| 1 // Copyright 2010 the V8 project authors. All rights reserved. | 1 // Copyright 2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --allow-natives-syntax |
| 29 |
| 28 // Test that we can inline a function that returns a constant. | 30 // Test that we can inline a function that returns a constant. |
| 29 function TestInlineConstant(o) { | 31 function TestInlineConstant(o) { |
| 30 // Effect context. | 32 // Effect context. |
| 31 o.f(); | 33 o.f(); |
| 32 // Value context. | 34 // Value context. |
| 33 var x = o.f(); | 35 var x = o.f(); |
| 34 assertEquals(42, x); | 36 assertEquals(42, x); |
| 35 assertEquals(42, o.f()); | 37 assertEquals(42, o.f()); |
| 36 // Test context. | 38 // Test context. |
| 37 if (!o.f()) { | 39 if (!o.f()) { |
| 38 assertTrue(false); // Should not happen. | 40 assertTrue(false); // Should not happen. |
| 39 } | 41 } |
| 40 } | 42 } |
| 41 | 43 |
| 42 var o1 = {}; | 44 var o1 = {}; |
| 43 o1.f = function() { return 42; }; | 45 o1.f = function() { return 42; }; |
| 44 for (var i = 0; i < 10000; i++) TestInlineConstant(o1); | 46 for (var i = 0; i < 5; i++) TestInlineConstant(o1); |
| 47 %OptimizeFunctionOnNextCall(TestInlineConstant); |
| 48 TestInlineConstant(o1); |
| 45 TestInlineConstant({f: o1.f}); | 49 TestInlineConstant({f: o1.f}); |
| 46 | 50 |
| 47 | 51 |
| 48 // Test that we can inline a function that returns 'this'. | 52 // Test that we can inline a function that returns 'this'. |
| 49 function TestInlineThis(o) { | 53 function TestInlineThis(o) { |
| 50 // Effect context. | 54 // Effect context. |
| 51 o.g(); | 55 o.g(); |
| 52 // Value context. | 56 // Value context. |
| 53 var x = o.g(); | 57 var x = o.g(); |
| 54 assertEquals(o, x); | 58 assertEquals(o, x); |
| 55 assertEquals(o, o.g()); | 59 assertEquals(o, o.g()); |
| 56 // Test context. | 60 // Test context. |
| 57 if (!o.g()) { | 61 if (!o.g()) { |
| 58 assertTrue(false); // Should not happen. | 62 assertTrue(false); // Should not happen. |
| 59 } | 63 } |
| 60 } | 64 } |
| 61 | 65 |
| 62 var o2 = {}; | 66 var o2 = {}; |
| 63 o2.g = function() { return this; }; | 67 o2.g = function() { return this; }; |
| 64 for (var i = 0; i < 10000; i++) TestInlineThis(o2); | 68 for (var i = 0; i < 5; i++) TestInlineThis(o2); |
| 69 %OptimizeFunctionOnNextCall(TestInlineThis); |
| 70 TestInlineThis(o2); |
| 65 TestInlineThis({g: o2.g}); | 71 TestInlineThis({g: o2.g}); |
| 66 | 72 |
| 67 | 73 |
| 68 // Test that we can inline a function that returns 'this.x'. | 74 // Test that we can inline a function that returns 'this.x'. |
| 69 function TestInlineThisX(o) { | 75 function TestInlineThisX(o) { |
| 70 // Effect context. | 76 // Effect context. |
| 71 o.h(); | 77 o.h(); |
| 72 // Value context. | 78 // Value context. |
| 73 var x = o.h(); | 79 var x = o.h(); |
| 74 assertEquals(42, x); | 80 assertEquals(42, x); |
| 75 assertEquals(42, o.h()); | 81 assertEquals(42, o.h()); |
| 76 // Test context. | 82 // Test context. |
| 77 if (!o.h()) { | 83 if (!o.h()) { |
| 78 assertTrue(false); // Should not happen. | 84 assertTrue(false); // Should not happen. |
| 79 } | 85 } |
| 80 } | 86 } |
| 81 | 87 |
| 82 var o3 = {y:0,x:42}; | 88 var o3 = {y:0,x:42}; |
| 83 o3.h = function() { return this.x; }; | 89 o3.h = function() { return this.x; }; |
| 84 for (var i = 0; i < 10000; i++) TestInlineThisX(o3); | 90 for (var i = 0; i < 5; i++) TestInlineThisX(o3); |
| 91 %OptimizeFunctionOnNextCall(TestInlineThisX); |
| 92 TestInlineThisX(o3); |
| 85 TestInlineThisX({h: o3.h, x:42}); | 93 TestInlineThisX({h: o3.h, x:42}); |
| 86 | 94 |
| 87 | 95 |
| 88 // Test that we can inline a function that returns 'this.x.length'. | 96 // Test that we can inline a function that returns 'this.x.length'. |
| 89 function TestInlineThisXLength(o) { | 97 function TestInlineThisXLength(o) { |
| 90 // Effect context. | 98 // Effect context. |
| 91 o.h(); | 99 o.h(); |
| 92 // Value context. | 100 // Value context. |
| 93 var x = o.h(); | 101 var x = o.h(); |
| 94 assertEquals(3, x); | 102 assertEquals(3, x); |
| 95 assertEquals(3, o.h()); | 103 assertEquals(3, o.h()); |
| 96 // Test context. | 104 // Test context. |
| 97 if (!o.h()) { | 105 if (!o.h()) { |
| 98 assertTrue(false); // Should not happen. | 106 assertTrue(false); // Should not happen. |
| 99 } | 107 } |
| 100 } | 108 } |
| 101 | 109 |
| 102 var o4 = {x:[1,2,3]}; | 110 var o4 = {x:[1,2,3]}; |
| 103 o4.h = function() { return this.x.length; }; | 111 o4.h = function() { return this.x.length; }; |
| 104 for (var i = 0; i < 10000; i++) TestInlineThisXLength(o4); | 112 for (var i = 0; i < 5; i++) TestInlineThisXLength(o4); |
| 113 %OptimizeFunctionOnNextCall(TestInlineThisXLength); |
| 114 TestInlineThisXLength(o4); |
| 105 TestInlineThisXLength({h: o4.h, x:[1,2,3]}); | 115 TestInlineThisXLength({h: o4.h, x:[1,2,3]}); |
| 106 | 116 |
| 107 | 117 |
| 108 // Test that we can inline a function that returns 'this.x.y'. | 118 // Test that we can inline a function that returns 'this.x.y'. |
| 109 function TestInlineThisXY(o) { | 119 function TestInlineThisXY(o) { |
| 110 // Effect context. | 120 // Effect context. |
| 111 o.h(); | 121 o.h(); |
| 112 // Value context. | 122 // Value context. |
| 113 var x = o.h(); | 123 var x = o.h(); |
| 114 assertEquals(42, x); | 124 assertEquals(42, x); |
| 115 assertEquals(42, o.h()); | 125 assertEquals(42, o.h()); |
| 116 // Test context. | 126 // Test context. |
| 117 if (!o.h()) { | 127 if (!o.h()) { |
| 118 assertTrue(false); // Should not happen. | 128 assertTrue(false); // Should not happen. |
| 119 } | 129 } |
| 120 } | 130 } |
| 121 | 131 |
| 122 var o6 = {y:42} | 132 var o6 = {y:42} |
| 123 var o5 = {e:o6}; | 133 var o5 = {e:o6}; |
| 124 o5.h = function() { return this.e.y; }; | 134 o5.h = function() { return this.e.y; }; |
| 125 for (var i = 0; i < 10000; i++) TestInlineThisXY(o5); | 135 for (var i = 0; i < 5; i++) TestInlineThisXY(o5); |
| 136 %OptimizeFunctionOnNextCall(TestInlineThisXY); |
| 137 TestInlineThisXY(o5); |
| 126 TestInlineThisXY({h: o5.h, e:o6}); | 138 TestInlineThisXY({h: o5.h, e:o6}); |
| 127 | 139 |
| 128 | 140 |
| 129 // Test that we can inline a function that returns 'this.x.length'. | 141 // Test that we can inline a function that returns 'this.x.length'. |
| 130 function TestInlineThisX0(o) { | 142 function TestInlineThisX0(o) { |
| 131 // Effect context. | 143 // Effect context. |
| 132 o.foo(); | 144 o.foo(); |
| 133 // Value context. | 145 // Value context. |
| 134 var x = o.foo(); | 146 var x = o.foo(); |
| 135 assertEquals(42, x); | 147 assertEquals(42, x); |
| 136 assertEquals(42, o.foo()); | 148 assertEquals(42, o.foo()); |
| 137 // Test context. | 149 // Test context. |
| 138 if (!o.foo()) { | 150 if (!o.foo()) { |
| 139 assertTrue(false); // Should not happen. | 151 assertTrue(false); // Should not happen. |
| 140 } | 152 } |
| 141 } | 153 } |
| 142 | 154 |
| 143 var o7 = {x:[42,43,44]}; | 155 var o7 = {x:[42,43,44]}; |
| 144 o7.foo = function() { return this.x[0]; }; | 156 o7.foo = function() { return this.x[0]; }; |
| 145 for (var i = 0; i < 10000; i++) TestInlineThisX0(o7); | 157 for (var i = 0; i < 5; i++) TestInlineThisX0(o7); |
| 158 %OptimizeFunctionOnNextCall(TestInlineThisX0); |
| 159 TestInlineThisX0(o7); |
| 146 TestInlineThisX0({foo: o7.foo, x:[42,0,0]}); | 160 TestInlineThisX0({foo: o7.foo, x:[42,0,0]}); |
| OLD | NEW |