Home | History | Annotate | Download | only in compiler
      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 
     28 // Flags: --allow-natives-syntax
     29 
     30 // Test that we can inline a function that returns a constant.
     31 function TestInlineConstant(o) {
     32   // Effect context.
     33   o.f();
     34   // Value context.
     35   var x = o.f();
     36   assertEquals(42, x);
     37   assertEquals(42, o.f());
     38   // Test context.
     39   if (!o.f()) {
     40     assertTrue(false);  // Should not happen.
     41   }
     42 }
     43 
     44 var o1 = {};
     45 o1.f = function() { return 42; };
     46 for (var i = 0; i < 5; i++) TestInlineConstant(o1);
     47 %OptimizeFunctionOnNextCall(TestInlineConstant);
     48 TestInlineConstant(o1);
     49 TestInlineConstant({f: o1.f});
     50 
     51 
     52 // Test that we can inline a function that returns 'this'.
     53 function TestInlineThis(o) {
     54   // Effect context.
     55   o.g();
     56   // Value context.
     57   var x = o.g();
     58   assertEquals(o, x);
     59   assertEquals(o, o.g());
     60   // Test context.
     61   if (!o.g()) {
     62     assertTrue(false);  // Should not happen.
     63   }
     64 }
     65 
     66 var o2 = {};
     67 o2.g = function() { return this; };
     68 for (var i = 0; i < 5; i++) TestInlineThis(o2);
     69 %OptimizeFunctionOnNextCall(TestInlineThis);
     70 TestInlineThis(o2);
     71 TestInlineThis({g: o2.g});
     72 
     73 
     74 // Test that we can inline a function that returns 'this.x'.
     75 function TestInlineThisX(o) {
     76   // Effect context.
     77   o.h();
     78   // Value context.
     79   var x = o.h();
     80   assertEquals(42, x);
     81   assertEquals(42, o.h());
     82   // Test context.
     83   if (!o.h()) {
     84     assertTrue(false);  // Should not happen.
     85   }
     86 }
     87 
     88 var o3 = {y:0,x:42};
     89 o3.h = function() { return this.x; };
     90 for (var i = 0; i < 5; i++) TestInlineThisX(o3);
     91 %OptimizeFunctionOnNextCall(TestInlineThisX);
     92 TestInlineThisX(o3);
     93 TestInlineThisX({h: o3.h, x:42});
     94 
     95 
     96 // Test that we can inline a function that returns 'this.x.length'.
     97 function TestInlineThisXLength(o) {
     98   // Effect context.
     99   o.h();
    100   // Value context.
    101   var x = o.h();
    102   assertEquals(3, x);
    103   assertEquals(3, o.h());
    104   // Test context.
    105   if (!o.h()) {
    106     assertTrue(false);  // Should not happen.
    107   }
    108 }
    109 
    110 var o4 = {x:[1,2,3]};
    111 o4.h = function() { return this.x.length; };
    112 for (var i = 0; i < 5; i++) TestInlineThisXLength(o4);
    113 %OptimizeFunctionOnNextCall(TestInlineThisXLength);
    114 TestInlineThisXLength(o4);
    115 TestInlineThisXLength({h: o4.h, x:[1,2,3]});
    116 
    117 
    118 // Test that we can inline a function that returns 'this.x.y'.
    119 function TestInlineThisXY(o) {
    120   // Effect context.
    121   o.h();
    122   // Value context.
    123   var x = o.h();
    124   assertEquals(42, x);
    125   assertEquals(42, o.h());
    126   // Test context.
    127   if (!o.h()) {
    128     assertTrue(false);  // Should not happen.
    129   }
    130 }
    131 
    132 var o6 = {y:42}
    133 var o5 = {e:o6};
    134 o5.h = function() { return this.e.y; };
    135 for (var i = 0; i < 5; i++) TestInlineThisXY(o5);
    136 %OptimizeFunctionOnNextCall(TestInlineThisXY);
    137 TestInlineThisXY(o5);
    138 TestInlineThisXY({h: o5.h, e:o6});
    139 
    140 
    141 // Test that we can inline a function that returns 'this.x.length'.
    142 function TestInlineThisX0(o) {
    143   // Effect context.
    144   o.foo();
    145   // Value context.
    146   var x = o.foo();
    147   assertEquals(42, x);
    148   assertEquals(42, o.foo());
    149   // Test context.
    150   if (!o.foo()) {
    151     assertTrue(false);  // Should not happen.
    152   }
    153 }
    154 
    155 var o7 = {x:[42,43,44]};
    156 o7.foo = function() { return this.x[0]; };
    157 for (var i = 0; i < 5; i++) TestInlineThisX0(o7);
    158 %OptimizeFunctionOnNextCall(TestInlineThisX0);
    159 TestInlineThisX0(o7);
    160 TestInlineThisX0({foo: o7.foo, x:[42,0,0]});
    161