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