Home | History | Annotate | Download | only in es6
      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 // Test temporal dead zone semantics of let bound variables in
     29 // function and block scopes.
     30 
     31 function TestFunctionLocal(s) {
     32   try {
     33     eval("(function(){" + s + "; })")();
     34   } catch (e) {
     35     assertInstanceof(e, ReferenceError);
     36     return;
     37   }
     38   assertUnreachable();
     39 }
     40 
     41 function TestBlockLocal(s,e) {
     42   try {
     43     eval("(function(){ {" + s + ";} })")();
     44   } catch (e) {
     45     assertInstanceof(e, ReferenceError);
     46     return;
     47   }
     48   assertUnreachable();
     49 }
     50 
     51 
     52 function TestAll(s) {
     53   TestBlockLocal(s);
     54   TestFunctionLocal(s);
     55 }
     56 
     57 // Use before initialization in declaration statement.
     58 TestAll('let x = x + 1');
     59 TestAll('let x = x += 1');
     60 TestAll('let x = x++');
     61 TestAll('let x = ++x');
     62 TestAll('const x = x + 1');
     63 
     64 // Use before initialization in prior statement.
     65 TestAll('x + 1; let x;');
     66 TestAll('x = 1; let x;');
     67 TestAll('x += 1; let x;');
     68 TestAll('++x; let x;');
     69 TestAll('x++; let x;');
     70 TestAll('let y = x; const x = 1;');
     71 TestAll('let y = x; class x {}');
     72 
     73 TestAll('f(); let x; function f() { return x + 1; }');
     74 TestAll('f(); let x; function f() { x = 1; }');
     75 TestAll('f(); let x; function f() { x += 1; }');
     76 TestAll('f(); let x; function f() { ++x; }');
     77 TestAll('f(); let x; function f() { x++; }');
     78 TestAll('f(); const x = 1; function f() { return x; }');
     79 TestAll('f(); class x { }; function f() { return x; }');
     80 
     81 TestAll('f()(); let x; function f() { return function() { return x + 1; } }');
     82 TestAll('f()(); let x; function f() { return function() { x = 1; } }');
     83 TestAll('f()(); let x; function f() { return function() { x += 1; } }');
     84 TestAll('f()(); let x; function f() { return function() { ++x; } }');
     85 TestAll('f()(); let x; function f() { return function() { x++; } }');
     86 TestAll('f()(); const x = 1; function f() { return function() { return x; } }');
     87 TestAll('f()(); class x { }; function f() { return function() { return x; } }');
     88 
     89 for (var kw of ['let x = 2', 'const x = 2', 'class x { }']) {
     90   // Use before initialization with a dynamic lookup.
     91   TestAll(`eval("x"); ${kw};`);
     92   TestAll(`eval("x + 1;"); ${kw};`);
     93   TestAll(`eval("x = 1;"); ${kw};`);
     94   TestAll(`eval("x += 1;"); ${kw};`);
     95   TestAll(`eval("++x;"); ${kw};`);
     96   TestAll(`eval("x++;"); ${kw};`);
     97 
     98   // Use before initialization with check for eval-shadowed bindings.
     99   TestAll(`function f() { eval("var y = 2;"); x + 1; }; f(); ${kw};`);
    100   TestAll(`function f() { eval("var y = 2;"); x = 1; }; f(); ${kw};`);
    101   TestAll(`function f() { eval("var y = 2;"); x += 1; }; f(); ${kw};`);
    102   TestAll(`function f() { eval("var y = 2;"); ++x; }; f(); ${kw};`);
    103   TestAll(`function f() { eval("var y = 2;"); x++; }; f(); ${kw};`);
    104 }
    105 
    106 // Test that variables introduced by function declarations are created and
    107 // initialized upon entering a function / block scope.
    108 function f() {
    109   {
    110     assertEquals(2, g1());
    111     assertEquals(2, eval("g1()"));
    112 
    113     // block scoped function declaration
    114     function g1() {
    115       return 2;
    116     }
    117   }
    118 
    119   assertEquals(3, g2());
    120   assertEquals(3, eval("g2()"));
    121   // function scoped function declaration
    122   function g2() {
    123     return 3;
    124   }
    125 }
    126 f();
    127 
    128 // Test that a function declaration introduces a block scoped variable
    129 // and no function hoisting if there is a conflict.
    130 TestFunctionLocal('{ function k() { return 0; } }; k(); let k;');
    131 
    132 // Test that a function declaration sees the scope it resides in.
    133 function f2() {
    134   let m, n, o, p;
    135   {
    136     m = g;
    137     function g() {
    138       return a;
    139     }
    140     let a = 1;
    141   }
    142   assertEquals(1, m());
    143 
    144   try {
    145     throw 2;
    146   } catch(b) {
    147     n = h;
    148     function h() {
    149       return b + c;
    150     }
    151     let c = 3;
    152   }
    153   assertEquals(5, n());
    154 
    155   {
    156     o = i;
    157     function i() {
    158       return d;
    159     }
    160     let d = 4;
    161   }
    162   assertEquals(4, o());
    163 
    164   try {
    165     throw 5;
    166   } catch(e) {
    167     p = j;
    168     function j() {
    169       return e + f;
    170     }
    171     let f = 6;
    172   }
    173   assertEquals(11, p());
    174 }
    175 f2();
    176 
    177 // Test that resolution of let bound variables works with scopes that call eval.
    178 function outer() {
    179   function middle() {
    180     function inner() {
    181       return x;
    182     }
    183     eval("1 + 1");
    184     return x + inner();
    185   }
    186 
    187   let x = 1;
    188   return middle();
    189 }
    190 
    191 assertEquals(2, outer());
    192