Home | History | Annotate | Download | only in harmony
      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: --harmony-scoping
     29 
     30 // TODO(ES6): properly activate extended mode
     31 "use strict";
     32 
     33 // Test temporal dead zone semantics of let bound variables in
     34 // function and block scopes.
     35 
     36 function TestFunctionLocal(s) {
     37   try {
     38     eval("(function(){" + s + "; })")();
     39   } catch (e) {
     40     assertInstanceof(e, ReferenceError);
     41     return;
     42   }
     43   assertUnreachable();
     44 }
     45 
     46 function TestBlockLocal(s,e) {
     47   try {
     48     eval("(function(){ {" + s + ";} })")();
     49   } catch (e) {
     50     assertInstanceof(e, ReferenceError);
     51     return;
     52   }
     53   assertUnreachable();
     54 }
     55 
     56 
     57 function TestAll(s) {
     58   TestBlockLocal(s);
     59   TestFunctionLocal(s);
     60 }
     61 
     62 // Use before initialization in declaration statement.
     63 TestAll('let x = x + 1');
     64 TestAll('let x = x += 1');
     65 TestAll('let x = x++');
     66 TestAll('let x = ++x');
     67 TestAll('const x = x + 1');
     68 
     69 // Use before initialization in prior statement.
     70 TestAll('x + 1; let x;');
     71 TestAll('x = 1; let x;');
     72 TestAll('x += 1; let x;');
     73 TestAll('++x; let x;');
     74 TestAll('x++; let x;');
     75 TestAll('let y = x; const x = 1;');
     76 
     77 TestAll('f(); let x; function f() { return x + 1; }');
     78 TestAll('f(); let x; function f() { x = 1; }');
     79 TestAll('f(); let x; function f() { x += 1; }');
     80 TestAll('f(); let x; function f() { ++x; }');
     81 TestAll('f(); let x; function f() { x++; }');
     82 TestAll('f(); const x = 1; function f() { return x; }');
     83 
     84 TestAll('f()(); let x; function f() { return function() { return x + 1; } }');
     85 TestAll('f()(); let x; function f() { return function() { x = 1; } }');
     86 TestAll('f()(); let x; function f() { return function() { x += 1; } }');
     87 TestAll('f()(); let x; function f() { return function() { ++x; } }');
     88 TestAll('f()(); let x; function f() { return function() { x++; } }');
     89 TestAll('f()(); const x = 1; function f() { return function() { return x; } }');
     90 
     91 // Use before initialization with a dynamic lookup.
     92 TestAll('eval("x + 1;"); let x;');
     93 TestAll('eval("x = 1;"); let x;');
     94 TestAll('eval("x += 1;"); let x;');
     95 TestAll('eval("++x;"); let x;');
     96 TestAll('eval("x++;"); let x;');
     97 TestAll('eval("x"); const x = 1;');
     98 
     99 // Use before initialization with check for eval-shadowed bindings.
    100 TestAll('function f() { eval("var y = 2;"); x + 1; }; f(); let x;');
    101 TestAll('function f() { eval("var y = 2;"); x = 1; }; f(); let x;');
    102 TestAll('function f() { eval("var y = 2;"); x += 1; }; f(); let x;');
    103 TestAll('function f() { eval("var y = 2;"); ++x; }; f(); let x;');
    104 TestAll('function f() { eval("var y = 2;"); x++; }; f(); let x;');
    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 TestAll('{ function k() { return 0; } }; k(); ');
    130 
    131 // Test that a function declaration sees the scope it resides in.
    132 function f2() {
    133   let m, n, o, p;
    134   {
    135     m = g;
    136     function g() {
    137       return a;
    138     }
    139     let a = 1;
    140   }
    141   assertEquals(1, m());
    142 
    143   try {
    144     throw 2;
    145   } catch(b) {
    146     n = h;
    147     function h() {
    148       return b + c;
    149     }
    150     let c = 3;
    151   }
    152   assertEquals(5, n());
    153 
    154   {
    155     o = i;
    156     function i() {
    157       return d;
    158     }
    159     let d = 4;
    160   }
    161   assertEquals(4, o());
    162 
    163   try {
    164     throw 5;
    165   } catch(e) {
    166     p = j;
    167     function j() {
    168       return e + f;
    169     }
    170     let f = 6;
    171   }
    172   assertEquals(11, p());
    173 }
    174 f2();
    175 
    176 // Test that resolution of let bound variables works with scopes that call eval.
    177 function outer() {
    178   function middle() {
    179     function inner() {
    180       return x;
    181     }
    182     eval("1 + 1");
    183     return x + inner();
    184   }
    185 
    186   let x = 1;
    187   return middle();
    188 }
    189 
    190 assertEquals(2, outer());
    191