Home | History | Annotate | Download | only in compiler
      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 --expose-gc --inline-construct
     29 
     30 // Test inlining of constructor calls.
     31 
     32 function TestInlinedConstructor(closure) {
     33   var result;
     34   var counter = { value:0 };
     35   result = closure(11, 12, counter);
     36   assertEquals(23, result);
     37   assertEquals(1, counter.value);
     38   result = closure(23, 19, counter);
     39   assertEquals(42, result);
     40   assertEquals(2, counter.value);
     41   %OptimizeFunctionOnNextCall(closure);
     42   result = closure(1, 42, counter)
     43   assertEquals(43, result);
     44   assertEquals(3, counter.value);
     45   result = closure("foo", "bar", counter)
     46   assertEquals("foobar", result)
     47   assertEquals(4, counter.value);
     48 }
     49 
     50 function TestInAllContexts(constructor) {
     51   function value_context(a, b, counter) {
     52     var obj = new constructor(a, b, counter);
     53     return obj.x;
     54   }
     55   function test_context(a, b, counter) {
     56     if (!new constructor(a, b, counter)) {
     57       assertUnreachable("should not happen");
     58     }
     59     return a + b;
     60   }
     61   function effect_context(a, b, counter) {
     62     new constructor(a, b, counter);
     63     return a + b;
     64   }
     65   TestInlinedConstructor(value_context);
     66   TestInlinedConstructor(test_context);
     67   TestInlinedConstructor(effect_context);
     68   %DeoptimizeFunction(value_context);
     69   %DeoptimizeFunction(test_context);
     70   %DeoptimizeFunction(effect_context);
     71   gc();  // Makes V8 forget about type information for *_context.
     72 }
     73 
     74 
     75 // Test constructor returning nothing in all contexts.
     76 function c1(a, b, counter) {
     77   this.x = a + b;
     78   counter.value++;
     79 }
     80 TestInAllContexts(c1);
     81 
     82 
     83 // Test constructor returning an object in all contexts.
     84 function c2(a, b, counter) {
     85   var obj = new Object();
     86   obj.x = a + b;
     87   counter.value++;
     88   return obj;
     89 }
     90 TestInAllContexts(c2);
     91 
     92 
     93 // Test constructor returning a primitive value in all contexts.
     94 function c3(a, b, counter) {
     95   this.x = a + b;
     96   counter.value++;
     97   return "not an object";
     98 }
     99 TestInAllContexts(c3);
    100 
    101 
    102 // Test constructor called with too many arguments.
    103 function c_too_many(a, b) {
    104   this.x = a + b;
    105 }
    106 function f_too_many(a, b, c) {
    107   var obj = new c_too_many(a, b, c);
    108   return obj.x;
    109 }
    110 assertEquals(23, f_too_many(11, 12, 1));
    111 assertEquals(42, f_too_many(23, 19, 1));
    112 %OptimizeFunctionOnNextCall(f_too_many);
    113 assertEquals(43, f_too_many(1, 42, 1));
    114 assertEquals("foobar", f_too_many("foo", "bar", "baz"))
    115 
    116 
    117 // Test constructor called with too few arguments.
    118 function c_too_few(a, b) {
    119   assertSame(undefined, b);
    120   this.x = a + 1;
    121 }
    122 function f_too_few(a) {
    123   var obj = new c_too_few(a);
    124   return obj.x;
    125 }
    126 assertEquals(12, f_too_few(11));
    127 assertEquals(24, f_too_few(23));
    128 %OptimizeFunctionOnNextCall(f_too_few);
    129 assertEquals(2, f_too_few(1));
    130 assertEquals("foo1", f_too_few("foo"))
    131 
    132 
    133 // Test constructor that cannot be inlined.
    134 function c_unsupported_syntax(a, b, counter) {
    135   try {
    136     this.x = a + b;
    137     counter.value++;
    138   } catch(e) {
    139     throw new Error();
    140   }
    141 }
    142 TestInAllContexts(c_unsupported_syntax);
    143 
    144 
    145 // Regression test: Inlined constructors called as functions do not get their
    146 // implicit receiver object set to undefined, even in strict mode.
    147 function c_strict(a, b, counter) {
    148   "use strict";
    149   this.x = a + b;
    150   counter.value++;
    151 }
    152 TestInAllContexts(c_strict);
    153