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 // TODO(ES6): properly activate extended mode 33 "use strict"; 34 35 function CheckException(e) { 36 var string = e.toString(); 37 assertTrue(string.indexOf("has already been declared") >= 0 || 38 string.indexOf("redeclaration") >= 0); return 'Conflict'; 39 } 40 41 42 function TestFunction(s,e) { 43 try { 44 return eval("(function(){" + s + ";return " + e + "})")(); 45 } catch (x) { 46 return CheckException(x); 47 } 48 } 49 50 51 function TestBlock(s,e) { 52 try { 53 return eval("(function(){ if (true) { " + s + "; }; return " + e + "})")(); 54 } catch (x) { 55 return CheckException(x); 56 } 57 } 58 59 function TestAll(expected,s,opt_e) { 60 var e = ""; 61 var msg = s; 62 if (opt_e) { e = opt_e; msg += "; " + opt_e; } 63 assertEquals(expected, TestFunction(s,e), "function:'" + msg + "'"); 64 assertEquals(expected, TestBlock(s,e), "block:'" + msg + "'"); 65 } 66 67 68 function TestConflict(s) { 69 TestAll('Conflict', s); 70 TestAll('Conflict', 'eval("' + s + '")'); 71 } 72 73 74 function TestNoConflict(s) { 75 TestAll('NoConflict', s, "'NoConflict'"); 76 TestAll('NoConflict', 'eval("' + s + '")', "'NoConflict'"); 77 } 78 79 var letbinds = [ "let x", 80 "let x = 0", 81 "let x = undefined", 82 "function x() { }", 83 "let x = function() {}", 84 "let x, y", 85 "let y, x", 86 "const x = 0", 87 "const x = undefined", 88 "const x = function() {}", 89 "const x = 2, y = 3", 90 "const y = 4, x = 5", 91 ]; 92 var varbinds = [ "var x", 93 "var x = 0", 94 "var x = undefined", 95 "var x = function() {}", 96 "var x, y", 97 "var y, x", 98 ]; 99 100 101 for (var l = 0; l < letbinds.length; ++l) { 102 // Test conflicting let/var bindings. 103 for (var v = 0; v < varbinds.length; ++v) { 104 // Same level. 105 TestConflict(letbinds[l] +'; ' + varbinds[v]); 106 TestConflict(varbinds[v] +'; ' + letbinds[l]); 107 // Different level. 108 TestConflict(letbinds[l] +'; {' + varbinds[v] + '; }'); 109 TestConflict('{ ' + varbinds[v] +'; }' + letbinds[l]); 110 } 111 112 // Test conflicting let/let bindings. 113 for (var k = 0; k < letbinds.length; ++k) { 114 // Same level. 115 TestConflict(letbinds[l] +'; ' + letbinds[k]); 116 TestConflict(letbinds[k] +'; ' + letbinds[l]); 117 // Different level. 118 TestNoConflict(letbinds[l] +'; { ' + letbinds[k] + '; }'); 119 TestNoConflict('{ ' + letbinds[k] +'; } ' + letbinds[l]); 120 } 121 122 // Test conflicting parameter/let bindings. 123 TestConflict('(function (x) { ' + letbinds[l] + '; })()'); 124 } 125 126 // Test conflicting catch/var bindings. 127 for (var v = 0; v < varbinds.length; ++v) { 128 TestConflict('try {} catch (x) { ' + varbinds[v] + '; }'); 129 } 130 131 // Test conflicting parameter/var bindings. 132 for (var v = 0; v < varbinds.length; ++v) { 133 TestNoConflict('(function (x) { ' + varbinds[v] + '; })()'); 134 } 135