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