| 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-block-scoping |
| 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 |
| 65 // Use before initialization in prior statement. |
| 66 TestAll('x + 1; let x;'); |
| 67 TestAll('x = 1; let x;'); |
| 68 TestAll('x += 1; let x;'); |
| 69 TestAll('++x; let x;'); |
| 70 TestAll('x++; let x;'); |
| 71 |
| 72 TestAll('f(); let x; function f() { return x + 1; }'); |
| 73 TestAll('f(); let x; function f() { x = 1; }'); |
| 74 TestAll('f(); let x; function f() { x += 1; }'); |
| 75 TestAll('f(); let x; function f() { ++x; }'); |
| 76 TestAll('f(); let x; function f() { x++; }'); |
| 77 |
| 78 TestAll('f()(); let x; function f() { return function() { return x + 1; } }'); |
| 79 TestAll('f()(); let x; function f() { return function() { x = 1; } }'); |
| 80 TestAll('f()(); let x; function f() { return function() { x += 1; } }'); |
| 81 TestAll('f()(); let x; function f() { return function() { ++x; } }'); |
| 82 TestAll('f()(); let x; function f() { return function() { x++; } }'); |
| 83 |
| 84 // Use in before initialization with a dynamic lookup. |
| 85 TestAll('eval("x + 1;"); let x;'); |
| 86 TestAll('eval("x = 1;"); let x;'); |
| 87 TestAll('eval("x += 1;"); let x;'); |
| 88 TestAll('eval("++x;"); let x;'); |
| 89 TestAll('eval("x++;"); let x;'); |
| 90 |
| 91 // Test that variables introduced by function declarations are created and |
| 92 // initialized upon entering a function / block scope. |
| 93 function f() { |
| 94 { |
| 95 assertEquals(2, g1()); |
| 96 assertEquals(2, eval("g1()")); |
| 97 |
| 98 // block scoped function declaration |
| 99 function g1() { |
| 100 return 2; |
| 101 } |
| 102 } |
| 103 |
| 104 assertEquals(3, g2()); |
| 105 assertEquals(3, eval("g2()")); |
| 106 // function scoped function declaration |
| 107 function g2() { |
| 108 return 3; |
| 109 } |
| 110 } |
| 111 f(); |
| 112 |
| 113 // Test that a function declaration introduces a block scoped variable. |
| 114 TestAll('{ function k() { return 0; } }; k(); '); |
| 115 |
| 116 // Test that a function declaration sees the scope it resides in. |
| 117 function f2() { |
| 118 let m, n; |
| 119 { |
| 120 m = g; |
| 121 function g() { |
| 122 return a; |
| 123 } |
| 124 let a = 1; |
| 125 } |
| 126 assertEquals(1, m()); |
| 127 |
| 128 try { |
| 129 throw 2; |
| 130 } catch(b) { |
| 131 n = h; |
| 132 function h() { |
| 133 return b + c; |
| 134 } |
| 135 let b = 3; |
| 136 } |
| 137 assertEquals(5, n()); |
| 138 } |
| OLD | NEW |