Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1 | // Copyright 2010 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 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 28 | // Flags: --allow-natives-syntax |
| 29 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 30 | // Test that we can inline a function that calls another function. |
| 31 | function TestInlineX(o) { |
| 32 | // Effect context. |
| 33 | o.g(); |
| 34 | // Value context. |
| 35 | var x = o.g(); |
| 36 | assertEquals(42, x); |
| 37 | assertEquals(42, o.g()); |
| 38 | // Test context. |
| 39 | if (!o.g()) { |
| 40 | assertTrue(false); // Should not happen. |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | var o2 = {}; |
| 45 | o2.size = function() { return 42; } |
| 46 | o2.g = function() { return this.size(); }; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 47 | for (var i = 0; i < 5; i++) TestInlineX(o2); |
| 48 | %OptimizeFunctionOnNextCall(TestInlineX); |
| 49 | TestInlineX(o2); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 50 | TestInlineX({g: o2.g, size:o2.size}); |
| 51 | |
| 52 | |
| 53 | // Test that we can inline a call on a non-variable receiver. |
| 54 | function TestInlineX2(o) { |
| 55 | // Effect context. |
| 56 | o.h(); |
| 57 | // Value context. |
| 58 | var x = o.h(); |
| 59 | assertEquals(42, x); |
| 60 | assertEquals(42, o.h()); |
| 61 | // Test context. |
| 62 | if (!o.h()) { |
| 63 | assertTrue(false); // Should not happen. |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | var obj = {} |
| 68 | obj.foo = function() { return 42; } |
| 69 | var o3 = {}; |
| 70 | o3.v = obj; |
| 71 | o3.h = function() { return this.v.foo(); }; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 72 | for (var i = 0; i < 5; i++) TestInlineX2(o3); |
| 73 | %OptimizeFunctionOnNextCall(TestInlineX2); |
| 74 | TestInlineX2(o3); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 75 | TestInlineX2({h: o3.h, v:obj}); |
| 76 | |
| 77 | |
| 78 | // Test that we can inline a call on a non-variable receiver. |
| 79 | function TestInlineFG(o) { |
| 80 | // Effect context. |
| 81 | o.h(); |
| 82 | // Value context. |
| 83 | var x = o.h(); |
| 84 | assertEquals(42, x); |
| 85 | assertEquals(42, o.h()); |
| 86 | // Test context. |
| 87 | if (!o.h()) { |
| 88 | assertTrue(false); // Should not happen. |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | var obj = {} |
| 93 | obj.g = function() { return 42; } |
| 94 | var o3 = {}; |
| 95 | o3.v = obj; |
| 96 | o3.f = function() { return this.v; } |
| 97 | o3.h = function() { return this.f().g(); }; |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame^] | 98 | for (var i = 0; i < 5; i++) TestInlineFG(o3); |
| 99 | %OptimizeFunctionOnNextCall(TestInlineFG); |
| 100 | TestInlineFG(o3); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 101 | TestInlineFG({h: o3.h, f: o3.f, v:obj}); |