| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| 11 // with the distribution. | 11 // with the distribution. |
| 12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
| 13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
| 14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
| 15 // | 15 // |
| 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 // Flags: --harmony-scoping | 28 // Flags: --harmony-scoping --harmony-computed-property-names |
| 29 | 29 |
| 30 // Test that we throw early syntax errors in harmony mode | 30 // Test that we throw early syntax errors in harmony mode |
| 31 // when using an immutable binding in an assigment or with | 31 // when using an immutable binding in an assigment or with |
| 32 // prefix/postfix decrement/increment operators. | 32 // prefix/postfix decrement/increment operators. |
| 33 | 33 |
| 34 "use strict"; | 34 "use strict"; |
| 35 | 35 |
| 36 // Function local const. | 36 const decls = [ |
| 37 function constDecl0(use) { | 37 // Const declaration. |
| 38 return "(function() { const constvar = 1; " + use + "; })();"; | 38 function(use) { return "const c = 1; " + use + ";" }, TypeError, |
| 39 } | 39 function(use) { return "const x = 0, c = 1; " + use + ";" }, TypeError, |
| 40 function(use) { return "const c = 1, x = (" + use + ");" }, TypeError, |
| 41 function(use) { return use + "; const c = 1;" }, ReferenceError, |
| 42 function(use) { return use + "; const x = 0, c = 1;" }, ReferenceError, |
| 43 function(use) { return "const x = (" + use + "), c = 1;" }, ReferenceError, |
| 44 function(use) { return "const c = (" + use + ");" }, ReferenceError, |
| 40 | 45 |
| 46 // Function expression. |
| 47 function(use) { return "(function c() { " + use + "; })();"; }, TypeError, |
| 48 // TODO(rossberg): Once we have default parameters, test using 'c' there. |
| 41 | 49 |
| 42 function constDecl1(use) { | 50 // Class expression. |
| 43 return "(function() { " + use + "; const constvar = 1; })();"; | 51 function(use) { |
| 44 } | 52 return "new class c { constructor() { " + use + " } };"; |
| 53 }, TypeError, |
| 54 function(use) { |
| 55 return "(new class c { m() { " + use + " } }).m();"; |
| 56 }, TypeError, |
| 57 function(use) { |
| 58 return "(new class c { get a() { " + use + " } }).a;"; |
| 59 }, TypeError, |
| 60 function(use) { |
| 61 return "(new class c { set a(x) { " + use + " } }).a = 0;"; |
| 62 }, TypeError, |
| 63 function(use) { |
| 64 return "(class c { static m() { " + use + " } }).s();"; |
| 65 }, TypeError, |
| 66 function(use) { |
| 67 return "(class c extends (" + use + ") {});"; |
| 68 }, ReferenceError, |
| 69 function(use) { |
| 70 return "(class c { [" + use + "]() {} });"; |
| 71 }, ReferenceError, |
| 72 function(use) { |
| 73 return "(class c { get [" + use + "]() {} });"; |
| 74 }, ReferenceError, |
| 75 function(use) { |
| 76 return "(class c { set [" + use + "](x) {} });"; |
| 77 }, ReferenceError, |
| 78 function(use) { |
| 79 return "(class c { static [" + use + "]() {} });"; |
| 80 }, ReferenceError, |
| 45 | 81 |
| 82 // For loop. |
| 83 function(use) { |
| 84 return "for (const c = 0; " + use + ";) {}" |
| 85 }, TypeError, |
| 86 function(use) { |
| 87 return "for (const x = 0, c = 0; " + use + ";) {}" |
| 88 }, TypeError, |
| 89 function(use) { |
| 90 return "for (const c = 0; ; " + use + ") {}" |
| 91 }, TypeError, |
| 92 function(use) { |
| 93 return "for (const x = 0, c = 0; ; " + use + ") {}" |
| 94 }, TypeError, |
| 95 function(use) { |
| 96 return "for (const c = 0; ;) { " + use + "; }" |
| 97 }, TypeError, |
| 98 function(use) { |
| 99 return "for (const x = 0, c = 0; ;) { " + use + "; }" |
| 100 }, TypeError, |
| 101 function(use) { |
| 102 return "for (const c in {a: 1}) { " + use + "; }" |
| 103 }, TypeError, |
| 104 function(use) { |
| 105 return "for (const c of [1]) { " + use + "; }" |
| 106 }, TypeError, |
| 107 function(use) { |
| 108 return "for (const x = (" + use + "), c = 0; ;) {}" |
| 109 }, ReferenceError, |
| 110 function(use) { |
| 111 return "for (const c = (" + use + "); ;) {}" |
| 112 }, ReferenceError, |
| 113 ] |
| 46 | 114 |
| 47 // Function local const, assign from eval. | 115 let uses = [ |
| 48 function constDecl2(use) { | 116 'c = 1', |
| 49 use = "eval('(function() { " + use + " })')()"; | 117 'c += 1', |
| 50 return "(function() { const constvar = 1; " + use + "; })();"; | 118 '++c', |
| 51 } | 119 'c--', |
| 120 ]; |
| 52 | 121 |
| 122 let declcontexts = [ |
| 123 function(decl) { return decl; }, |
| 124 function(decl) { return "eval(\'" + decl + "\')"; }, |
| 125 function(decl) { return "{ " + decl + " }"; }, |
| 126 function(decl) { return "(function() { " + decl + " })()"; }, |
| 127 ]; |
| 53 | 128 |
| 54 function constDecl3(use) { | 129 let usecontexts = [ |
| 55 use = "eval('(function() { " + use + " })')()"; | 130 function(use) { return use; }, |
| 56 return "(function() { " + use + "; const constvar = 1; })();"; | 131 function(use) { return "eval(\"" + use + "\")"; }, |
| 57 } | 132 function(use) { return "(function() { " + use + " })()"; }, |
| 133 function(use) { return "(function() { eval(\"" + use + "\"); })()"; }, |
| 134 function(use) { return "eval(\"(function() { " + use + "; })\")()"; }, |
| 135 ]; |
| 58 | 136 |
| 59 | 137 function Test(program, error) { |
| 60 // Block local const. | 138 program = "'use strict'; " + program; |
| 61 function constDecl4(use) { | |
| 62 return "(function() { { const constvar = 1; " + use + "; } })();"; | |
| 63 } | |
| 64 | |
| 65 | |
| 66 function constDecl5(use) { | |
| 67 return "(function() { { " + use + "; const constvar = 1; } })();"; | |
| 68 } | |
| 69 | |
| 70 | |
| 71 // Block local const, assign from eval. | |
| 72 function constDecl6(use) { | |
| 73 use = "eval('(function() {" + use + "})')()"; | |
| 74 return "(function() { { const constvar = 1; " + use + "; } })();"; | |
| 75 } | |
| 76 | |
| 77 | |
| 78 function constDecl7(use) { | |
| 79 use = "eval('(function() {" + use + "})')()"; | |
| 80 return "(function() { { " + use + "; const constvar = 1; } })();"; | |
| 81 } | |
| 82 | |
| 83 | |
| 84 // Function expression name. | |
| 85 function constDecl8(use) { | |
| 86 return "(function constvar() { " + use + "; })();"; | |
| 87 } | |
| 88 | |
| 89 | |
| 90 // Function expression name, assign from eval. | |
| 91 function constDecl9(use) { | |
| 92 use = "eval('(function(){" + use + "})')()"; | |
| 93 return "(function constvar() { " + use + "; })();"; | |
| 94 } | |
| 95 | |
| 96 // For loop variable. | |
| 97 function constDecl10(use) { | |
| 98 return "(function() { for (const constvar = 0; ;) { " + use + "; } })();"; | |
| 99 } | |
| 100 | |
| 101 // For-in loop variable. | |
| 102 function constDecl11(use) { | |
| 103 return "(function() { for (const constvar in {a: 1}) { " + use + "; } })();"; | |
| 104 } | |
| 105 | |
| 106 // For-of loop variable. | |
| 107 function constDecl12(use) { | |
| 108 return "(function() { for (const constvar of [1]) { " + use + "; } })();"; | |
| 109 } | |
| 110 | |
| 111 | |
| 112 let decls = [ constDecl0, | |
| 113 constDecl1, | |
| 114 constDecl2, | |
| 115 constDecl3, | |
| 116 constDecl4, | |
| 117 constDecl5, | |
| 118 constDecl6, | |
| 119 constDecl7, | |
| 120 constDecl8, | |
| 121 constDecl9, | |
| 122 constDecl10, | |
| 123 constDecl11, | |
| 124 constDecl12 | |
| 125 ]; | |
| 126 let declsForTDZ = new Set([constDecl1, constDecl3, constDecl5, constDecl7]); | |
| 127 let uses = [ 'constvar = 1;', | |
| 128 'constvar += 1;', | |
| 129 '++constvar;', | |
| 130 'constvar++;' | |
| 131 ]; | |
| 132 | |
| 133 function Test(d,u) { | |
| 134 'use strict'; | |
| 135 try { | 139 try { |
| 136 print(d(u)); | 140 print(program, " // throw " + error.name); |
| 137 eval(d(u)); | 141 eval(program); |
| 138 } catch (e) { | 142 } catch (e) { |
| 139 if (declsForTDZ.has(d) && u !== uses[0]) { | 143 assertInstanceof(e, error); |
| 140 // In these cases, read of a const variable occurs | 144 if (e === TypeError) { |
| 141 // before a write to it, so TDZ kicks in before const check. | 145 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); |
| 142 assertInstanceof(e, ReferenceError); | |
| 143 return; | |
| 144 } | 146 } |
| 145 assertInstanceof(e, TypeError); | |
| 146 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); | |
| 147 return; | 147 return; |
| 148 } | 148 } |
| 149 assertUnreachable(); | 149 assertUnreachable(); |
| 150 } | 150 } |
| 151 | 151 |
| 152 for (var d = 0; d < decls.length; ++d) { | 152 for (var d = 0; d < decls.length; d += 2) { |
| 153 for (var u = 0; u < uses.length; ++u) { | 153 for (var u = 0; u < uses.length; ++u) { |
| 154 Test(decls[d], uses[u]); | 154 for (var o = 0; o < declcontexts.length; ++o) { |
| 155 for (var i = 0; i < usecontexts.length; ++i) { |
| 156 Test(declcontexts[o](decls[d](usecontexts[i](uses[u]))), decls[d + 1]); |
| 157 } |
| 158 } |
| 155 } | 159 } |
| 156 } | 160 } |
| OLD | NEW |