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-computed-property-names
30 // Test that we throw early syntax errors in harmony mode
31 // when using an immutable binding in an assigment or with
32 // prefix/postfix decrement/increment operators.
38 function(use) { return "const c = 1; " + use + ";" }, TypeError,
39 function(use) { return "const x = 0, c = 1; " + use + ";" }, TypeError,
40 function(use) { return "const c = 1, x = (" + use + ");" }, TypeError,
41 function(use) { return use + "; const c = 1;" }, ReferenceError,
42 function(use) { return use + "; const x = 0, c = 1;" }, ReferenceError,
43 function(use) { return "const x = (" + use + "), c = 1;" }, ReferenceError,
44 function(use) { return "const c = (" + use + ");" }, ReferenceError,
46 // Function expression.
47 function(use) { return "(function c() { " + use + "; })();"; }, TypeError,
48 // TODO(rossberg): Once we have default parameters, test using 'c' there.
52 return "new class c { constructor() { " + use + " } };";
55 return "(new class c { m() { " + use + " } }).m();";
58 return "(new class c { get a() { " + use + " } }).a;";
61 return "(new class c { set a(x) { " + use + " } }).a = 0;";
64 return "(class c { static m() { " + use + " } }).s();";
67 return "(class c extends (" + use + ") {});";
70 return "(class c { [" + use + "]() {} });";
73 return "(class c { get [" + use + "]() {} });";
76 return "(class c { set [" + use + "](x) {} });";
79 return "(class c { static [" + use + "]() {} });";
84 return "for (const c = 0; " + use + ";) {}"
87 return "for (const x = 0, c = 0; " + use + ";) {}"
90 return "for (const c = 0; ; " + use + ") {}"
93 return "for (const x = 0, c = 0; ; " + use + ") {}"
96 return "for (const c = 0; ;) { " + use + "; }"
99 return "for (const x = 0, c = 0; ;) { " + use + "; }"
102 return "for (const c in {a: 1}) { " + use + "; }"
105 return "for (const c of [1]) { " + use + "; }"
108 return "for (const x = (" + use + "), c = 0; ;) {}"
111 return "for (const c = (" + use + "); ;) {}"
123 function(decl) { return decl; },
124 function(decl) { return "eval(\'" + decl + "\')"; },
125 function(decl) { return "{ " + decl + " }"; },
126 function(decl) { return "(function() { " + decl + " })()"; },
130 function(use) { return use; },
131 function(use) { return "eval(\"" + use + "\")"; },
132 function(use) { return "(function() { " + use + " })()"; },
133 function(use) { return "(function() { eval(\"" + use + "\"); })()"; },
134 function(use) { return "eval(\"(function() { " + use + "; })\")()"; },
137 function Test(program, error) {
138 program = "'use strict'; " + program;
140 print(program, " // throw " + error.name);
143 assertInstanceof(e, error);
144 if (e === TypeError) {
145 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0);
152 for (var d = 0; d < decls.length; d += 2) {
153 for (var u = 0; u < uses.length; ++u) {
154 for (var o = 0; o < declcontexts.length; ++o) {
155 for (var i = 0; i < usecontexts.length; ++i) {
156 Test(declcontexts[o](decls[d](usecontexts[i](uses[u]))), decls[d + 1]);