Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1)

Side by Side Diff: test/mjsunit/const-redecl.js

Issue 1819123002: Remove support for legacy const, part 1 (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: Rebased, deleted one more file Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « test/mjsunit/const-eval-init.js ('k') | test/mjsunit/constant-folding.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2008 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: --legacy-const
29
30 // Test for const semantics.
31
32
33 function CheckException(e) {
34 var string = e.toString();
35 var index = string.indexOf(':');
36 assertTrue(index >= 0);
37 var name = string.slice(0, index);
38 assertTrue(string.indexOf("has already been declared") >= 0 ||
39 string.indexOf("redeclaration") >= 0);
40 if (name == 'SyntaxError') return 'TypeError';
41 return name;
42 }
43
44
45 function TestLocal(s,e) {
46 try {
47 return eval("(function(){" + s + ";return " + e + "})")();
48 } catch (x) {
49 return CheckException(x);
50 }
51 }
52
53
54 function TestContext(s,e) {
55 try {
56 // Use a with-statement to force the system to do dynamic
57 // declarations of the introduced variables or constants.
58 with ({}) {
59 return eval(s + ";" + e);
60 }
61 } catch (x) {
62 return CheckException(x);
63 }
64 }
65
66
67 function TestAll(expected,s,opt_e) {
68 var e = "";
69 var msg = s;
70 if (opt_e) { e = opt_e; msg += "; " + opt_e; }
71 assertEquals(expected, TestLocal(s,e), "local:'" + msg + "'");
72 assertEquals(expected, TestContext(s,e), "context:'" + msg + "'");
73 }
74
75
76 function TestConflict(def0, def1) {
77 // No eval.
78 TestAll("TypeError", def0 +'; ' + def1);
79 // Eval everything.
80 TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")');
81 // Eval first definition.
82 TestAll("TypeError", 'eval("' + def0 +'"); ' + def1);
83 // Eval second definition.
84 TestAll("TypeError", def0 + '; eval("' + def1 +'")');
85 // Eval both definitions separately.
86 TestAll("TypeError", 'eval("' + def0 +'"); eval("' + def1 + '")');
87 }
88
89
90 // Test conflicting definitions.
91 TestConflict("const x", "var x");
92 TestConflict("const x = 0", "var x");
93 TestConflict("const x", "var x = 0");
94 TestConflict("const x = 0", "var x = 0");
95
96 TestConflict("var x", "const x");
97 TestConflict("var x = 0", "const x");
98 TestConflict("var x", "const x = 0");
99 TestConflict("var x = 0", "const x = 0");
100
101 TestConflict("const x = undefined", "var x");
102 TestConflict("const x", "var x = undefined");
103 TestConflict("const x = undefined", "var x = undefined");
104
105 TestConflict("var x = undefined", "const x");
106 TestConflict("var x", "const x = undefined");
107 TestConflict("var x = undefined", "const x = undefined");
108
109 TestConflict("const x = undefined", "var x = 0");
110 TestConflict("const x = 0", "var x = undefined");
111
112 TestConflict("var x = undefined", "const x = 0");
113 TestConflict("var x = 0", "const x = undefined");
114
115 TestConflict("const x", "function x() { }");
116 TestConflict("const x = 0", "function x() { }");
117 TestConflict("const x = undefined", "function x() { }");
118
119 TestConflict("function x() { }", "const x");
120 TestConflict("function x() { }", "const x = 0");
121 TestConflict("function x() { }", "const x = undefined");
122
123 TestConflict("const x, y", "var x");
124 TestConflict("const x, y", "var y");
125 TestConflict("const x = 0, y", "var x");
126 TestConflict("const x = 0, y", "var y");
127 TestConflict("const x, y = 0", "var x");
128 TestConflict("const x, y = 0", "var y");
129 TestConflict("const x = 0, y = 0", "var x");
130 TestConflict("const x = 0, y = 0", "var y");
131
132 TestConflict("var x", "const x, y");
133 TestConflict("var y", "const x, y");
134 TestConflict("var x", "const x = 0, y");
135 TestConflict("var y", "const x = 0, y");
136 TestConflict("var x", "const x, y = 0");
137 TestConflict("var y", "const x, y = 0");
138 TestConflict("var x", "const x = 0, y = 0");
139 TestConflict("var y", "const x = 0, y = 0");
140
141
142 // Test that multiple conflicts do not cause issues.
143 TestConflict("var x, y", "const x, y");
144
145
146 // Test that repeated const declarations throw redeclaration errors.
147 TestConflict("const x", "const x");
148 TestConflict("const x = 0", "const x");
149 TestConflict("const x", "const x = 0");
150 TestConflict("const x = 0", "const x = 0");
151
152 TestConflict("const x = undefined", "const x");
153 TestConflict("const x", "const x = undefined");
154 TestConflict("const x = undefined", "const x = undefined");
155
156 TestConflict("const x = undefined", "const x = 0");
157 TestConflict("const x = 0", "const x = undefined");
158
159 TestConflict("const x, y", "const x");
160 TestConflict("const x, y", "const y");
161 TestConflict("const x = 0, y", "const x");
162 TestConflict("const x = 0, y", "const y");
163 TestConflict("const x, y = 0", "const x");
164 TestConflict("const x, y = 0", "const y");
165 TestConflict("const x = 0, y = 0", "const x");
166 TestConflict("const x = 0, y = 0", "const y");
167
168 TestConflict("const x", "const x, y");
169 TestConflict("const y", "const x, y");
170 TestConflict("const x", "const x = 0, y");
171 TestConflict("const y", "const x = 0, y");
172 TestConflict("const x", "const x, y = 0");
173 TestConflict("const y", "const x, y = 0");
174 TestConflict("const x", "const x = 0, y = 0");
175 TestConflict("const y", "const x = 0, y = 0");
176
177
178 // Test that multiple const conflicts do not cause issues.
179 TestConflict("const x, y", "const x, y");
180
181
182 // Test that const inside loop behaves correctly.
183 var loop = "for (var i = 0; i < 3; i++) { const x = i; }";
184 TestAll(0, loop, "x");
185 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x");
186
187
188 // Test that const inside with behaves correctly.
189 TestAll(87, "with ({x:42}) { const x = 87; }", "x");
190 TestAll(undefined, "with ({x:42}) { const x; }", "x");
191
192
193 // Additional tests for how various combinations of re-declarations affect
194 // the values of the var/const in question.
195 try {
196 eval("var undefined;");
197 } catch (ex) {
198 assertUnreachable("undefined (1) has thrown");
199 }
200
201 var original_undef = undefined;
202 var undefined = 1; // Should be silently ignored.
203 assertEquals(original_undef, undefined, "undefined got overwritten");
204 undefined = original_undef;
205
206 const e = 1; eval('var e = 2');
207 assertEquals(1, e, "e has wrong value");
208
209 const h; eval('var h = 1');
210 assertEquals(undefined, h, "h has wrong value");
211
212 eval("Object.defineProperty(this, 'i', { writable: true });"
213 + "const i = 7;"
214 + "assertEquals(7, i, \"i has wrong value\");");
215
216 var global = this;
217 Object.defineProperty(global, 'j', { value: 100, writable: true });
218 assertEquals(100, j);
219 // The const declaration stays configurable, so the declaration above goes
220 // through even though the const declaration is hoisted above.
221 const j = 2;
222 assertEquals(2, j, "j has wrong value");
223
224 var k = 1;
225 try { eval('const k'); } catch(e) { }
226 assertEquals(1, k, "k has wrong value");
227 try { eval('const k = 10'); } catch(e) { }
228 assertEquals(1, k, "k has wrong value");
OLDNEW
« no previous file with comments | « test/mjsunit/const-eval-init.js ('k') | test/mjsunit/constant-folding.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698