Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 let declarations in various settings. |
| 29 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 30 | // Global |
| 31 | let x; |
| 32 | let y = 2; |
| 33 | const z = 4; |
| 34 | class c { static foo() { return 1; } } |
| 35 | |
| 36 | // Block local |
| 37 | { |
| 38 | let y; |
| 39 | let x = 3; |
| 40 | const z = 5; |
| 41 | class c { static foo() { return 2; } } |
| 42 | } |
| 43 | |
| 44 | assertEquals(undefined, x); |
| 45 | assertEquals(2,y); |
| 46 | assertEquals(4,z); |
| 47 | assertEquals(1, c.foo()); |
| 48 | |
| 49 | if (true) { |
| 50 | let y; |
| 51 | assertEquals(undefined, y); |
| 52 | } |
| 53 | |
| 54 | // Invalid declarations are early errors in harmony mode and thus should trigger |
| 55 | // an exception in eval code during parsing, before even compiling or executing |
| 56 | // the code. Thus the generated function is not called here. |
| 57 | function TestLocalThrows(str, expect) { |
| 58 | assertThrows("(function(arg){ 'use strict'; " + str + "})", expect); |
| 59 | } |
| 60 | |
| 61 | function TestLocalDoesNotThrow(str) { |
| 62 | assertDoesNotThrow("(function(arg){ 'use strict'; " + str + "})()"); |
| 63 | } |
| 64 | |
| 65 | // Test let declarations in statement positions. |
| 66 | TestLocalThrows("if (true) let x;", SyntaxError); |
| 67 | TestLocalThrows("if (true) {} else let x;", SyntaxError); |
| 68 | TestLocalThrows("do let x; while (false)", SyntaxError); |
| 69 | TestLocalThrows("while (false) let x;", SyntaxError); |
| 70 | TestLocalThrows("label: let x;", SyntaxError); |
| 71 | TestLocalThrows("for (;false;) let x;", SyntaxError); |
| 72 | TestLocalDoesNotThrow("switch (true) { case true: let x; }"); |
| 73 | TestLocalDoesNotThrow("switch (true) { default: let x; }"); |
| 74 | |
| 75 | // Test const declarations with initialisers in statement positions. |
| 76 | TestLocalThrows("if (true) const x = 1;", SyntaxError); |
| 77 | TestLocalThrows("if (true) {} else const x = 1;", SyntaxError); |
| 78 | TestLocalThrows("do const x = 1; while (false)", SyntaxError); |
| 79 | TestLocalThrows("while (false) const x = 1;", SyntaxError); |
| 80 | TestLocalThrows("label: const x = 1;", SyntaxError); |
| 81 | TestLocalThrows("for (;false;) const x = 1;", SyntaxError); |
| 82 | TestLocalDoesNotThrow("switch (true) { case true: const x = 1; }"); |
| 83 | TestLocalDoesNotThrow("switch (true) { default: const x = 1; }"); |
| 84 | |
| 85 | // Test const declarations without initialisers. |
| 86 | TestLocalThrows("const x;", SyntaxError); |
| 87 | TestLocalThrows("const x = 1, y;", SyntaxError); |
| 88 | TestLocalThrows("const x, y = 1;", SyntaxError); |
| 89 | |
| 90 | // Test const declarations without initialisers in statement positions. |
| 91 | TestLocalThrows("if (true) const x;", SyntaxError); |
| 92 | TestLocalThrows("if (true) {} else const x;", SyntaxError); |
| 93 | TestLocalThrows("do const x; while (false)", SyntaxError); |
| 94 | TestLocalThrows("while (false) const x;", SyntaxError); |
| 95 | TestLocalThrows("label: const x;", SyntaxError); |
| 96 | TestLocalThrows("for (;false;) const x;", SyntaxError); |
| 97 | TestLocalThrows("switch (true) { case true: const x; }", SyntaxError); |
| 98 | TestLocalThrows("switch (true) { default: const x; }", SyntaxError); |
| 99 | |
| 100 | // Test var declarations in statement positions. |
| 101 | TestLocalDoesNotThrow("if (true) var x;"); |
| 102 | TestLocalDoesNotThrow("if (true) {} else var x;"); |
| 103 | TestLocalDoesNotThrow("do var x; while (false)"); |
| 104 | TestLocalDoesNotThrow("while (false) var x;"); |
| 105 | TestLocalDoesNotThrow("label: var x;"); |
| 106 | TestLocalDoesNotThrow("for (;false;) var x;"); |
| 107 | TestLocalDoesNotThrow("switch (true) { case true: var x; }"); |
| 108 | TestLocalDoesNotThrow("switch (true) { default: var x; }"); |
| 109 | |
| 110 | // Test class declarations with initialisers in statement positions. |
| 111 | TestLocalThrows("if (true) class x { };", SyntaxError); |
| 112 | TestLocalThrows("if (true) {} else class x { };", SyntaxError); |
| 113 | TestLocalThrows("do class x { }; while (false)", SyntaxError); |
| 114 | TestLocalThrows("while (false) class x { };", SyntaxError); |
| 115 | TestLocalThrows("label: class x { };", SyntaxError); |
| 116 | TestLocalThrows("for (;false;) class x { };", SyntaxError); |
| 117 | TestLocalDoesNotThrow("switch (true) { case true: class x { }; }"); |
| 118 | TestLocalDoesNotThrow("switch (true) { default: class x { }; }"); |
| 119 | |
| 120 | // Test that redeclarations of functions are only allowed in outermost scope. |
| 121 | TestLocalThrows("{ let f; var f; }"); |
| 122 | TestLocalThrows("{ var f; let f; }"); |
| 123 | TestLocalThrows("{ function f() {} let f; }"); |
| 124 | TestLocalThrows("{ let f; function f() {} }"); |
| 125 | TestLocalThrows("{ function f() {} var f; }"); |
| 126 | TestLocalThrows("{ var f; function f() {} }"); |
| 127 | TestLocalThrows("{ function f() {} class f {} }"); |
| 128 | TestLocalThrows("{ class f {}; function f() {} }"); |
| 129 | TestLocalThrows("{ function f() {} function f() {} }"); |
| 130 | TestLocalThrows("function f() {} let f;"); |
| 131 | TestLocalThrows("let f; function f() {}"); |
| 132 | TestLocalThrows("function f() {} class f {}"); |
| 133 | TestLocalThrows("class f {}; function f() {}"); |
| 134 | TestLocalDoesNotThrow("function arg() {}"); |
| 135 | TestLocalDoesNotThrow("function f() {} var f;"); |
| 136 | TestLocalDoesNotThrow("var f; function f() {}"); |
| 137 | TestLocalDoesNotThrow("function f() {} function f() {}"); |
| 138 | |
| 139 | function g(f) { |
| 140 | function f() { return 1 } |
| 141 | return f() |
| 142 | } |
| 143 | assertEquals(1, g(function() { return 2 })) |
| 144 | |
| 145 | |
| 146 | // Test function declarations in source element and |
| 147 | // sloppy statement positions. |
| 148 | function f() { |
| 149 | // Sloppy source element positions. |
| 150 | function g0() { |
| 151 | "use strict"; |
| 152 | // Strict source element positions. |
| 153 | function h() { } |
| 154 | { |
| 155 | function h1() { } |
| 156 | } |
| 157 | } |
| 158 | { |
| 159 | function g1() { } |
| 160 | } |
| 161 | } |
| 162 | f(); |
| 163 | |
| 164 | // Test function declarations in statement position in strict mode. |
| 165 | TestLocalThrows("function f() { if (true) function g() {} }", SyntaxError); |
| 166 | TestLocalThrows("function f() { if (true) {} else function g() {} }", SyntaxError); |
| 167 | TestLocalThrows("function f() { do function g() {} while (false) }", SyntaxError); |
| 168 | TestLocalThrows("function f() { while (false) function g() {} }", SyntaxError); |
| 169 | TestLocalThrows("function f() { label: function g() {} }", SyntaxError); |
| 170 | TestLocalThrows("function f() { for (;false;) function g() {} }", SyntaxError); |
| 171 | TestLocalDoesNotThrow("function f() { switch (true) { case true: function g() {} } }"); |
| 172 | TestLocalDoesNotThrow("function f() { switch (true) { default: function g() {} } }"); |