Home | History | Annotate | Download | only in mjsunit
      1 // Copyright 2011 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 var y = 3;
     31 
     32 function get_y() { return this.y; }
     33 function strict_get_y() { "use strict"; return this.y; }
     34 
     35 // Test calls to strict mode function as methods.
     36 for (var i = 0; i < 10; i++) assertEquals(3, strict_get_y.call(this));
     37 var o = { y: 42 };
     38 for (var i = 0; i < 10; i++) assertEquals(42, strict_get_y.call(o));
     39 
     40 // Test calls to strict mode function with implicit receiver.
     41 function g() {
     42   var exception = false;
     43   try { strict_get_y(); } catch(e) { exception = true; }
     44   assertTrue(exception);
     45 }
     46 for (var i = 0; i < 3; i++) g();
     47 
     48 // Test calls to local strict mode function with implicit receiver.
     49 function local_function_test() {
     50   function get_y() { return this.y; }
     51   function strict_get_y() { "use strict"; return this.y; }
     52   assertEquals(3, get_y());
     53   assertEquals(3, get_y(23));
     54   var exception = false;
     55   try {
     56     strict_get_y();
     57   } catch(e) {
     58     exception = true;
     59   }
     60   assertTrue(exception);
     61 }
     62 
     63 for (var i = 0; i < 10; i++) {
     64   local_function_test();
     65 }
     66 
     67 // Test call to catch variable strict-mode function. Implicit
     68 // receiver.
     69 var exception = false;
     70 try {
     71   throw strict_get_y;
     72 } catch(f) {
     73   try {
     74     f();
     75   } catch(e) {
     76     exception = true;
     77   }
     78   assertTrue(exception);
     79 }
     80 
     81 
     82 // Test calls to strict-mode function with the object from a with
     83 // statement as the receiver.
     84 with(this) {
     85   assertEquals(3, strict_get_y());
     86   assertEquals(3, strict_get_y(32));
     87 }
     88 
     89 var o = { y: 27 };
     90 o.f = strict_get_y;
     91 with(o) {
     92   assertEquals(27, f());
     93   assertEquals(27, f(23));
     94 }
     95 
     96 
     97 // Check calls to eval within a function with 'undefined' as receiver.
     98 function implicit_receiver_eval() {
     99   "use strict";
    100   return eval("this");
    101 }
    102 
    103 assertEquals(void 0, implicit_receiver_eval());
    104 assertEquals(void 0, implicit_receiver_eval(32));
    105 
    106 
    107 // Strict mode function to get inlined.
    108 function strict_return_receiver() {
    109   "use strict";
    110   return this;
    111 }
    112 
    113 // Inline with implicit receiver.
    114 function g() {
    115   return strict_return_receiver();
    116 }
    117 
    118 for (var i = 0; i < 5; i++) {
    119   assertEquals(void 0, g());
    120   assertEquals(void 0, g(42));
    121 }
    122 %OptimizeFunctionOnNextCall(g);
    123 assertEquals(void 0, g(42));
    124 assertEquals(void 0, g());
    125 
    126 // Inline with explicit receiver.
    127 function g2() {
    128   var o = {};
    129   o.f = strict_return_receiver;
    130   return o.f();
    131 }
    132 
    133 for (var i = 0; i < 5; i++) {
    134   assertTrue(typeof g2() == "object");
    135   assertTrue(typeof g2(42) == "object");
    136 }
    137 %OptimizeFunctionOnNextCall(g2);
    138 assertTrue(typeof g2() == "object");
    139 assertTrue(typeof g2(42) == "object");
    140 
    141 // Test calls of aliased eval.
    142 function outer_eval_receiver() {
    143   var eval = function() { return this; }
    144   function inner_strict() {
    145     "use strict";
    146     assertEquals('object', typeof eval());
    147   }
    148   inner_strict();
    149 }
    150 outer_eval_receiver();
    151 
    152 function outer_eval_conversion3(eval, expected) {
    153   function inner_strict() {
    154     "use strict";
    155     var x = eval("this");
    156     assertEquals(expected, typeof x);
    157   }
    158   inner_strict();
    159 }
    160 
    161 function strict_return_this() { "use strict"; return this; }
    162 function return_this() { return this; }
    163 function strict_eval(s) { "use strict"; return eval(s); }
    164 function non_strict_eval(s) { return eval(s); }
    165 
    166 outer_eval_conversion3(strict_return_this, 'undefined');
    167 outer_eval_conversion3(return_this, 'object');
    168 outer_eval_conversion3(strict_eval, 'undefined');
    169 outer_eval_conversion3(non_strict_eval, 'object');
    170 
    171 outer_eval_conversion3(eval, 'undefined');
    172 
    173 function test_constant_function() {
    174   var o = { f: function() { "use strict"; return this; } };
    175   this.__proto__ = o;
    176   for (var i = 0; i < 10; i++) assertEquals(void 0, f());
    177 }
    178 test_constant_function();
    179 
    180 function test_field() {
    181   var o = { };
    182   o.f = function() {};
    183   o.f = function() { "use strict"; return this; };
    184   this.__proto__ = o;
    185   for (var i = 0; i < 10; i++) assertEquals(void 0, f());
    186 }
    187 test_field();
    188