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 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.
36 // Function local const.
37 function constDecl0(use) {
38 return "(function() { const constvar = 1; " + use + "; })();";
42 function constDecl1(use) {
43 return "(function() { " + use + "; const constvar = 1; })();";
47 // Function local const, assign from eval.
48 function constDecl2(use) {
49 use = "eval('(function() { " + use + " })')()";
50 return "(function() { const constvar = 1; " + use + "; })();";
54 function constDecl3(use) {
55 use = "eval('(function() { " + use + " })')()";
56 return "(function() { " + use + "; const constvar = 1; })();";
61 function constDecl4(use) {
62 return "(function() { { const constvar = 1; " + use + "; } })();";
66 function constDecl5(use) {
67 return "(function() { { " + use + "; const constvar = 1; } })();";
71 // Block local const, assign from eval.
72 function constDecl6(use) {
73 use = "eval('(function() {" + use + "})')()";
74 return "(function() { { const constvar = 1; " + use + "; } })();";
78 function constDecl7(use) {
79 use = "eval('(function() {" + use + "})')()";
80 return "(function() { { " + use + "; const constvar = 1; } })();";
84 // Function expression name.
85 function constDecl8(use) {
86 return "(function constvar() { " + use + "; })();";
90 // Function expression name, assign from eval.
91 function constDecl9(use) {
92 use = "eval('(function(){" + use + "})')()";
93 return "(function constvar() { " + use + "; })();";
96 let decls = [ constDecl0,
107 let declsForTDZ = new Set([constDecl1, constDecl3, constDecl5, constDecl7]);
108 let uses = [ 'constvar = 1;',
120 if (declsForTDZ.has(d) && u !== uses[0]) {
121 // In these cases, read of a const variable occurs
122 // before a write to it, so TDZ kicks in before const check.
123 assertInstanceof(e, ReferenceError);
126 assertInstanceof(e, TypeError);
127 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0);
133 for (var d = 0; d < decls.length; ++d) {
134 for (var u = 0; u < uses.length; ++u) {
135 Test(decls[d], uses[u]);