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 // Test let declarations in various settings.
     31 // TODO(ES6): properly activate extended mode
     32 "use strict";
     33 
     34 // Global
     35 let x;
     36 let y = 2;
     37 const z = 4;
     38 
     39 // Block local
     40 {
     41   let y;
     42   let x = 3;
     43   const z = 5;
     44 }
     45 
     46 assertEquals(undefined, x);
     47 assertEquals(2,y);
     48 assertEquals(4,z);
     49 
     50 if (true) {
     51   let y;
     52   assertEquals(undefined, y);
     53 }
     54 
     55 // Invalid declarations are early errors in harmony mode and thus should trigger
     56 // an exception in eval code during parsing, before even compiling or executing
     57 // the code. Thus the generated function is not called here.
     58 function TestLocalThrows(str, expect) {
     59   assertThrows("(function(){ 'use strict'; " + str + "})", expect);
     60 }
     61 
     62 function TestLocalDoesNotThrow(str) {
     63   assertDoesNotThrow("(function(){ 'use strict'; " + str + "})()");
     64 }
     65 
     66 // Test let declarations in statement positions.
     67 TestLocalThrows("if (true) let x;", SyntaxError);
     68 TestLocalThrows("if (true) {} else let x;", SyntaxError);
     69 TestLocalThrows("do let x; while (false)", SyntaxError);
     70 TestLocalThrows("while (false) let x;", SyntaxError);
     71 TestLocalThrows("label: let x;", SyntaxError);
     72 TestLocalThrows("for (;false;) let x;", SyntaxError);
     73 TestLocalThrows("switch (true) { case true: let x; }", SyntaxError);
     74 TestLocalThrows("switch (true) { default: let x; }", SyntaxError);
     75 
     76 // Test const declarations with initialisers in statement positions.
     77 TestLocalThrows("if (true) const x = 1;", SyntaxError);
     78 TestLocalThrows("if (true) {} else const x = 1;", SyntaxError);
     79 TestLocalThrows("do const x = 1; while (false)", SyntaxError);
     80 TestLocalThrows("while (false) const x = 1;", SyntaxError);
     81 TestLocalThrows("label: const x = 1;", SyntaxError);
     82 TestLocalThrows("for (;false;) const x = 1;", SyntaxError);
     83 TestLocalThrows("switch (true) { case true: const x = 1; }", SyntaxError);
     84 TestLocalThrows("switch (true) { default: const x = 1; }", SyntaxError);
     85 
     86 // Test const declarations without initialisers.
     87 TestLocalThrows("const x;", SyntaxError);
     88 TestLocalThrows("const x = 1, y;", SyntaxError);
     89 TestLocalThrows("const x, y = 1;", SyntaxError);
     90 
     91 // Test const declarations without initialisers in statement positions.
     92 TestLocalThrows("if (true) const x;", SyntaxError);
     93 TestLocalThrows("if (true) {} else const x;", SyntaxError);
     94 TestLocalThrows("do const x; while (false)", SyntaxError);
     95 TestLocalThrows("while (false) const x;", SyntaxError);
     96 TestLocalThrows("label: const x;", SyntaxError);
     97 TestLocalThrows("for (;false;) const x;", SyntaxError);
     98 TestLocalThrows("switch (true) { case true: const x; }", SyntaxError);
     99 TestLocalThrows("switch (true) { default: const x; }", SyntaxError);
    100 
    101 // Test var declarations in statement positions.
    102 TestLocalDoesNotThrow("if (true) var x;");
    103 TestLocalDoesNotThrow("if (true) {} else var x;");
    104 TestLocalDoesNotThrow("do var x; while (false)");
    105 TestLocalDoesNotThrow("while (false) var x;");
    106 TestLocalDoesNotThrow("label: var x;");
    107 TestLocalDoesNotThrow("for (;false;) var x;");
    108 TestLocalDoesNotThrow("switch (true) { case true: var x; }");
    109 TestLocalDoesNotThrow("switch (true) { default: var x; }");
    110 
    111 // Test function declarations in source element and
    112 // non-strict statement positions.
    113 function f() {
    114   // Non-strict source element positions.
    115   function g0() {
    116     "use strict";
    117     // Strict source element positions.
    118     function h() { }
    119     {
    120       function h1() { }
    121     }
    122   }
    123   {
    124     function g1() { }
    125   }
    126 }
    127 f();
    128 
    129 // Test function declarations in statement position in strict mode.
    130 TestLocalThrows("function f() { if (true) function g() {}", SyntaxError);
    131 TestLocalThrows("function f() { if (true) {} else function g() {}", SyntaxError);
    132 TestLocalThrows("function f() { do function g() {} while (false)", SyntaxError);
    133 TestLocalThrows("function f() { while (false) function g() {}", SyntaxError);
    134 TestLocalThrows("function f() { label: function g() {}", SyntaxError);
    135 TestLocalThrows("function f() { for (;false;) function g() {}", SyntaxError);
    136 TestLocalThrows("function f() { switch (true) { case true: function g() {} }", SyntaxError);
    137 TestLocalThrows("function f() { switch (true) { default: function g() {} }", SyntaxError);
    138