OLD | NEW |
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 15 matching lines...) Expand all Loading... |
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | 27 |
28 // Flags: --harmony-scoping | 28 // Flags: --harmony-scoping |
29 | 29 |
30 // Test that we throw early syntax errors in harmony mode | 30 // Test that we throw early syntax errors in harmony mode |
31 // when using an immutable binding in an assigment or with | 31 // when using an immutable binding in an assigment or with |
32 // prefix/postfix decrement/increment operators. | 32 // prefix/postfix decrement/increment operators. |
33 | 33 |
34 "use strict"; | 34 "use strict"; |
35 | 35 |
36 // Function local const. | 36 const decls = [ |
37 function constDecl0(use) { | 37 // Const declaration. |
38 return "(function() { const constvar = 1; " + use + "; })();"; | 38 function(use) { return "const c = 1; " + use + ";" }, TypeError, |
39 } | 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, |
40 | 44 |
| 45 // Function expression. |
| 46 function(use) { return "(function c() { " + use + "; })();"; }, TypeError, |
41 | 47 |
42 function constDecl1(use) { | 48 // For loop. |
43 return "(function() { " + use + "; const constvar = 1; })();"; | 49 function(use) { |
44 } | 50 return "for (const c = 0; ;) { " + use + "; }" |
| 51 }, TypeError, |
| 52 function(use) { |
| 53 return "for (const x = 0, c = 0; ;) { " + use + "; }" |
| 54 }, TypeError, |
| 55 function(use) { |
| 56 return "for (const c in {a: 1}) { " + use + "; }" |
| 57 }, TypeError, |
| 58 function(use) { |
| 59 return "for (const c of [1]) { " + use + "; }" |
| 60 }, TypeError, |
| 61 ] |
45 | 62 |
| 63 let uses = [ |
| 64 'c = 1', |
| 65 'c += 1', |
| 66 '++c', |
| 67 'c--', |
| 68 ]; |
46 | 69 |
47 // Function local const, assign from eval. | 70 let declcontexts = [ |
48 function constDecl2(use) { | 71 function(decl) { return decl; }, |
49 use = "eval('(function() { " + use + " })')()"; | 72 function(decl) { return "eval(\'" + decl + "\')"; }, |
50 return "(function() { const constvar = 1; " + use + "; })();"; | 73 function(decl) { return "{ " + decl + " }"; }, |
51 } | 74 function(decl) { return "(function() { " + decl + " })()"; }, |
| 75 ]; |
52 | 76 |
| 77 let usecontexts = [ |
| 78 function(use) { return use; }, |
| 79 function(use) { return "eval(\"" + use + "\")"; }, |
| 80 function(use) { return "(function() { " + use + " })()"; }, |
| 81 function(use) { return "(function() { eval(\"" + use + "\"); })()"; }, |
| 82 ]; |
53 | 83 |
54 function constDecl3(use) { | 84 function Test(program, error) { |
55 use = "eval('(function() { " + use + " })')()"; | 85 program = "'use strict'; " + program; |
56 return "(function() { " + use + "; const constvar = 1; })();"; | |
57 } | |
58 | |
59 | |
60 // Block local const. | |
61 function constDecl4(use) { | |
62 return "(function() { { const constvar = 1; " + use + "; } })();"; | |
63 } | |
64 | |
65 | |
66 function constDecl5(use) { | |
67 return "(function() { { " + use + "; const constvar = 1; } })();"; | |
68 } | |
69 | |
70 | |
71 // Block local const, assign from eval. | |
72 function constDecl6(use) { | |
73 use = "eval('(function() {" + use + "})')()"; | |
74 return "(function() { { const constvar = 1; " + use + "; } })();"; | |
75 } | |
76 | |
77 | |
78 function constDecl7(use) { | |
79 use = "eval('(function() {" + use + "})')()"; | |
80 return "(function() { { " + use + "; const constvar = 1; } })();"; | |
81 } | |
82 | |
83 | |
84 // Function expression name. | |
85 function constDecl8(use) { | |
86 return "(function constvar() { " + use + "; })();"; | |
87 } | |
88 | |
89 | |
90 // Function expression name, assign from eval. | |
91 function constDecl9(use) { | |
92 use = "eval('(function(){" + use + "})')()"; | |
93 return "(function constvar() { " + use + "; })();"; | |
94 } | |
95 | |
96 // For loop variable. | |
97 function constDecl10(use) { | |
98 return "(function() { for (const constvar = 0; ;) { " + use + "; } })();"; | |
99 } | |
100 | |
101 // For-in loop variable. | |
102 function constDecl11(use) { | |
103 return "(function() { for (const constvar in {a: 1}) { " + use + "; } })();"; | |
104 } | |
105 | |
106 // For-of loop variable. | |
107 function constDecl12(use) { | |
108 return "(function() { for (const constvar of [1]) { " + use + "; } })();"; | |
109 } | |
110 | |
111 | |
112 let decls = [ constDecl0, | |
113 constDecl1, | |
114 constDecl2, | |
115 constDecl3, | |
116 constDecl4, | |
117 constDecl5, | |
118 constDecl6, | |
119 constDecl7, | |
120 constDecl8, | |
121 constDecl9, | |
122 constDecl10, | |
123 constDecl11, | |
124 constDecl12 | |
125 ]; | |
126 let declsForTDZ = new Set([constDecl1, constDecl3, constDecl5, constDecl7]); | |
127 let uses = [ 'constvar = 1;', | |
128 'constvar += 1;', | |
129 '++constvar;', | |
130 'constvar++;' | |
131 ]; | |
132 | |
133 function Test(d,u) { | |
134 'use strict'; | |
135 try { | 86 try { |
136 print(d(u)); | 87 print(program, "(* throw " + error.name + " *)"); |
137 eval(d(u)); | 88 eval(program); |
138 } catch (e) { | 89 } catch (e) { |
139 if (declsForTDZ.has(d) && u !== uses[0]) { | 90 assertInstanceof(e, error); |
140 // In these cases, read of a const variable occurs | 91 if (e === TypeError) { |
141 // before a write to it, so TDZ kicks in before const check. | 92 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); |
142 assertInstanceof(e, ReferenceError); | |
143 return; | |
144 } | 93 } |
145 assertInstanceof(e, TypeError); | |
146 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); | |
147 return; | 94 return; |
148 } | 95 } |
149 assertUnreachable(); | 96 assertUnreachable(); |
150 } | 97 } |
151 | 98 |
152 for (var d = 0; d < decls.length; ++d) { | 99 for (var d = 0; d < decls.length; d += 2) { |
153 for (var u = 0; u < uses.length; ++u) { | 100 for (var u = 0; u < uses.length; ++u) { |
154 Test(decls[d], uses[u]); | 101 for (var o = 0; o < declcontexts.length; ++o) { |
| 102 for (var i = 0; i < usecontexts.length; ++i) { |
| 103 Test(declcontexts[o](decls[d](usecontexts[i](uses[u]))), decls[d + 1]); |
| 104 } |
| 105 } |
155 } | 106 } |
156 } | 107 } |
OLD | NEW |