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
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.
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.
28 // Flags: --harmony-scoping
30 // Test for conflicting variable bindings.
32 // TODO(ES6): properly activate extended mode
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';
42 function TestFunction(s,e) {
44 return eval("(function(){" + s + ";return " + e + "})")();
46 return CheckException(x);
51 function TestBlock(s,e) {
53 return eval("(function(){ if (true) { " + s + "; }; return " + e + "})")();
55 return CheckException(x);
59 function TestAll(expected,s,opt_e) {
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 + "'");
68 function TestConflict(s) {
69 TestAll('Conflict', s);
70 TestAll('Conflict', 'eval("' + s + '")');
74 function TestNoConflict(s) {
75 TestAll('NoConflict', s, "'NoConflict'");
76 TestAll('NoConflict', 'eval("' + s + '")', "'NoConflict'");
79 var letbinds = [ "let x",
83 "let x = function() {}",
87 "const x = undefined",
88 "const x = function() {}",
92 var varbinds = [ "var x",
95 "var x = function() {}",
101 for (var l = 0; l < letbinds.length; ++l) {
102 // Test conflicting let/var bindings.
103 for (var v = 0; v < varbinds.length; ++v) {
105 TestConflict(letbinds[l] +'; ' + varbinds[v]);
106 TestConflict(varbinds[v] +'; ' + letbinds[l]);
108 TestConflict(letbinds[l] +'; {' + varbinds[v] + '; }');
109 TestConflict('{ ' + varbinds[v] +'; }' + letbinds[l]);
112 // Test conflicting let/let bindings.
113 for (var k = 0; k < letbinds.length; ++k) {
115 TestConflict(letbinds[l] +'; ' + letbinds[k]);
116 TestConflict(letbinds[k] +'; ' + letbinds[l]);
118 TestNoConflict(letbinds[l] +'; { ' + letbinds[k] + '; }');
119 TestNoConflict('{ ' + letbinds[k] +'; } ' + letbinds[l]);
122 // Test conflicting parameter/let bindings.
123 TestConflict('(function (x) { ' + letbinds[l] + '; })()');
126 // Test conflicting catch/var bindings.
127 for (var v = 0; v < varbinds.length; ++v) {
128 TestConflict('try {} catch (x) { ' + varbinds[v] + '; }');
131 // Test conflicting parameter/var bindings.
132 for (var v = 0; v < varbinds.length; ++v) {
133 TestNoConflict('(function (x) { ' + varbinds[v] + '; })()');