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 for conflicting variable bindings. 31 32 "use strict"; 33 34 function CheckException(e) { 35 var string = e.toString(); 36 assertTrue(string.indexOf("has already been declared") >= 0 || 37 string.indexOf("redeclaration") >= 0); 38 return 'Conflict'; 39 } 40 41 42 function TestGlobal(s,e) { 43 try { 44 return eval(s + e); 45 } catch (x) { 46 return CheckException(x); 47 } 48 } 49 50 51 function TestFunction(s,e) { 52 try { 53 return eval("(function(){" + s + " return " + e + "})")(); 54 } catch (x) { 55 return CheckException(x); 56 } 57 } 58 59 60 function TestBlock(s,e) { 61 try { 62 return eval("(function(){ {" + s + "} return " + e + "})")(); 63 } catch (x) { 64 return CheckException(x); 65 } 66 } 67 68 function TestAll(expected,s,opt_e) { 69 var e = ""; 70 var msg = s; 71 if (opt_e) { e = opt_e; msg += opt_e; } 72 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected, 73 TestGlobal(s,e), "global:'" + msg + "'"); 74 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected, 75 TestFunction(s,e), "function:'" + msg + "'"); 76 assertEquals(expected === 'LocalConflict' ? 'Conflict' : expected, 77 TestBlock(s,e), "block:'" + msg + "'"); 78 } 79 80 81 function TestConflict(s) { 82 TestAll('Conflict', s); 83 TestAll('Conflict', 'eval("' + s + '");'); 84 } 85 86 function TestNoConflict(s) { 87 TestAll('NoConflict', s, "'NoConflict'"); 88 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'"); 89 } 90 91 function TestLocalConflict(s) { 92 TestAll('LocalConflict', s, "'NoConflict'"); 93 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'"); 94 } 95 96 var letbinds = [ "let x;", 97 "let x = 0;", 98 "let x = undefined;", 99 "let x = function() {};", 100 "let x, y;", 101 "let y, x;", 102 "const x = 0;", 103 "const x = undefined;", 104 "const x = function() {};", 105 "const x = 2, y = 3;", 106 "const y = 4, x = 5;", 107 ]; 108 var varbinds = [ "var x;", 109 "var x = 0;", 110 "var x = undefined;", 111 "var x = function() {};", 112 "var x, y;", 113 "var y, x;", 114 ]; 115 var funbind = "function x() {}"; 116 117 for (var l = 0; l < letbinds.length; ++l) { 118 // Test conflicting let/var bindings. 119 for (var v = 0; v < varbinds.length; ++v) { 120 // Same level. 121 TestConflict(letbinds[l] + varbinds[v]); 122 TestConflict(varbinds[v] + letbinds[l]); 123 // Different level. 124 TestConflict(letbinds[l] + '{' + varbinds[v] + '}'); 125 TestConflict('{' + varbinds[v] +'}' + letbinds[l]); 126 TestNoConflict(varbinds[v] + '{' + letbinds[l] + '}'); 127 TestNoConflict('{' + letbinds[l] + '}' + varbinds[v]); 128 // For loop. 129 TestConflict('for (' + letbinds[l] + '0;) {' + varbinds[v] + '}'); 130 TestNoConflict('for (' + varbinds[v] + '0;) {' + letbinds[l] + '}'); 131 } 132 133 // Test conflicting let/let bindings. 134 for (var k = 0; k < letbinds.length; ++k) { 135 // Same level. 136 TestConflict(letbinds[l] + letbinds[k]); 137 TestConflict(letbinds[k] + letbinds[l]); 138 // Different level. 139 TestNoConflict(letbinds[l] + '{ ' + letbinds[k] + '}'); 140 TestNoConflict('{' + letbinds[k] +'} ' + letbinds[l]); 141 // For loop. 142 TestNoConflict('for (' + letbinds[l] + '0;) {' + letbinds[k] + '}'); 143 TestNoConflict('for (' + letbinds[k] + '0;) {' + letbinds[l] + '}'); 144 } 145 146 // Test conflicting function/let bindings. 147 // Same level. 148 TestConflict(letbinds[l] + funbind); 149 TestConflict(funbind + letbinds[l]); 150 // Different level. 151 TestNoConflict(letbinds[l] + '{' + funbind + '}'); 152 TestNoConflict('{' + funbind + '}' + letbinds[l]); 153 TestNoConflict(funbind + '{' + letbinds[l] + '}'); 154 TestNoConflict('{' + letbinds[l] + '}' + funbind); 155 // For loop. 156 TestNoConflict('for (' + letbinds[l] + '0;) {' + funbind + '}'); 157 158 // Test conflicting parameter/let bindings. 159 TestConflict('(function(x) {' + letbinds[l] + '})();'); 160 } 161 162 // Test conflicting function/var bindings. 163 for (var v = 0; v < varbinds.length; ++v) { 164 // Same level. 165 TestLocalConflict(varbinds[v] + funbind); 166 TestLocalConflict(funbind + varbinds[v]); 167 // Different level. 168 TestLocalConflict(funbind + '{' + varbinds[v] + '}'); 169 TestLocalConflict('{' + varbinds[v] +'}' + funbind); 170 TestNoConflict(varbinds[v] + '{' + funbind + '}'); 171 TestNoConflict('{' + funbind + '}' + varbinds[v]); 172 // For loop. 173 TestNoConflict('for (' + varbinds[v] + '0;) {' + funbind + '}'); 174 } 175 176 // Test conflicting catch/var bindings. 177 for (var v = 0; v < varbinds.length; ++v) { 178 TestConflict('try {} catch(x) {' + varbinds[v] + '}'); 179 } 180 181 // Test conflicting parameter/var bindings. 182 for (var v = 0; v < varbinds.length; ++v) { 183 TestNoConflict('(function (x) {' + varbinds[v] + '})();'); 184 } 185 186 // Test conflicting catch/function bindings. 187 TestNoConflict('try {} catch(x) {' + funbind + '}'); 188 189 // Test conflicting parameter/function bindings. 190 TestNoConflict('(function (x) {' + funbind + '})();'); 191