1 // Copyright 2011 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // Flags: --harmony-scoping
7 // Test for conflicting variable bindings.
11 function CheckException(e) {
12 var string = e.toString();
13 assertTrue(string.indexOf("has already been declared") >= 0 ||
14 string.indexOf("redeclaration") >= 0);
19 function TestGlobal(s,e) {
23 return CheckException(x);
28 function TestFunction(s,e) {
30 return eval("(function(){" + s + " return " + e + "})")();
32 return CheckException(x);
37 function TestBlock(s,e) {
39 return eval("(function(){ {" + s + "} return " + e + "})")();
41 return CheckException(x);
45 function TestAll(expected,s,opt_e) {
48 if (opt_e) { e = opt_e; msg += opt_e; }
49 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
50 TestGlobal(s,e), "global:'" + msg + "'");
51 assertEquals(expected === 'LocalConflict' ? 'NoConflict' : expected,
52 TestFunction(s,e), "function:'" + msg + "'");
53 assertEquals(expected === 'LocalConflict' ? 'Conflict' : expected,
54 TestBlock(s,e), "block:'" + msg + "'");
58 function TestConflict(s) {
59 TestAll('Conflict', s);
60 TestAll('Conflict', 'eval("' + s + '");');
63 function TestNoConflict(s) {
64 TestAll('NoConflict', s, "'NoConflict'");
65 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
68 function TestLocalConflict(s) {
69 TestAll('LocalConflict', s, "'NoConflict'");
70 TestAll('NoConflict', 'eval("' + s + '");', "'NoConflict'");
73 var letbinds = [ "let x;",
76 "let x = function() {};",
80 "const x = undefined;",
81 "const x = function() {};",
82 "const x = 2, y = 3;",
83 "const y = 4, x = 5;",
85 var varbinds = [ "var x;",
88 "var x = function() {};",
92 var funbind = "function x() {}";
94 for (var l = 0; l < letbinds.length; ++l) {
95 // Test conflicting let/var bindings.
96 for (var v = 0; v < varbinds.length; ++v) {
98 TestConflict(letbinds[l] + varbinds[v]);
99 TestConflict(varbinds[v] + letbinds[l]);
101 TestConflict(letbinds[l] + '{' + varbinds[v] + '}');
102 TestConflict('{' + varbinds[v] +'}' + letbinds[l]);
103 TestNoConflict(varbinds[v] + '{' + letbinds[l] + '}');
104 TestNoConflict('{' + letbinds[l] + '}' + varbinds[v]);
106 TestConflict('for (' + letbinds[l] + '0;) {' + varbinds[v] + '}');
107 TestNoConflict('for (' + varbinds[v] + '0;) {' + letbinds[l] + '}');
110 // Test conflicting let/let bindings.
111 for (var k = 0; k < letbinds.length; ++k) {
113 TestConflict(letbinds[l] + letbinds[k]);
114 TestConflict(letbinds[k] + letbinds[l]);
116 TestNoConflict(letbinds[l] + '{ ' + letbinds[k] + '}');
117 TestNoConflict('{' + letbinds[k] +'} ' + letbinds[l]);
119 TestNoConflict('for (' + letbinds[l] + '0;) {' + letbinds[k] + '}');
120 TestNoConflict('for (' + letbinds[k] + '0;) {' + letbinds[l] + '}');
123 // Test conflicting function/let bindings.
125 TestConflict(letbinds[l] + funbind);
126 TestConflict(funbind + letbinds[l]);
128 TestNoConflict(letbinds[l] + '{' + funbind + '}');
129 TestNoConflict('{' + funbind + '}' + letbinds[l]);
130 TestNoConflict(funbind + '{' + letbinds[l] + '}');
131 TestNoConflict('{' + letbinds[l] + '}' + funbind);
133 TestNoConflict('for (' + letbinds[l] + '0;) {' + funbind + '}');
135 // Test conflicting parameter/let bindings.
136 TestConflict('(function(x) {' + letbinds[l] + '})();');
139 // Test conflicting function/var bindings.
140 for (var v = 0; v < varbinds.length; ++v) {
142 TestLocalConflict(varbinds[v] + funbind);
143 TestLocalConflict(funbind + varbinds[v]);
145 TestLocalConflict(funbind + '{' + varbinds[v] + '}');
146 TestLocalConflict('{' + varbinds[v] +'}' + funbind);
147 TestNoConflict(varbinds[v] + '{' + funbind + '}');
148 TestNoConflict('{' + funbind + '}' + varbinds[v]);
150 TestNoConflict('for (' + varbinds[v] + '0;) {' + funbind + '}');
153 // Test conflicting catch/var bindings.
154 for (var v = 0; v < varbinds.length; ++v) {
155 TestNoConflict('try {} catch(x) {' + varbinds[v] + '}');
158 // Test conflicting parameter/var bindings.
159 for (var v = 0; v < varbinds.length; ++v) {
160 TestNoConflict('(function (x) {' + varbinds[v] + '})();');
163 // Test conflicting catch/function bindings.
164 TestNoConflict('try {} catch(x) {' + funbind + '}');
166 // Test conflicting parameter/function bindings.
167 TestNoConflict('(function (x) {' + funbind + '})();');