OLD | NEW |
1 // Copyright 2008 the V8 project authors. All rights reserved. | 1 // Copyright 2008 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 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 return CheckException(x); | 91 return CheckException(x); |
92 } | 92 } |
93 } | 93 } |
94 | 94 |
95 | 95 |
96 function TestAll(expected,s,opt_e) { | 96 function TestAll(expected,s,opt_e) { |
97 var e = ""; | 97 var e = ""; |
98 var msg = s; | 98 var msg = s; |
99 if (opt_e) { e = opt_e; msg += "; " + opt_e; } | 99 if (opt_e) { e = opt_e; msg += "; " + opt_e; } |
100 assertEquals(expected, TestLocal(s,e), "local:'" + msg + "'"); | 100 assertEquals(expected, TestLocal(s,e), "local:'" + msg + "'"); |
101 // Redeclarations of global consts do not throw, they are silently ignored. | 101 assertEquals(expected, TestGlobal(s,e), "global:'" + msg + "'"); |
102 assertEquals(42, TestGlobal(s, 42), "global:'" + msg + "'"); | |
103 assertEquals(expected, TestContext(s,e), "context:'" + msg + "'"); | 102 assertEquals(expected, TestContext(s,e), "context:'" + msg + "'"); |
104 } | 103 } |
105 | 104 |
106 | 105 |
107 function TestConflict(def0, def1) { | 106 function TestConflict(def0, def1) { |
108 // No eval. | 107 // No eval. |
109 TestAll("TypeError", def0 +'; ' + def1); | 108 TestAll("TypeError", def0 +'; ' + def1); |
110 // Eval everything. | 109 // Eval everything. |
111 TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")'); | 110 TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")'); |
112 // Eval first definition. | 111 // Eval first definition. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 | 211 |
213 // Test that const inside loop behaves correctly. | 212 // Test that const inside loop behaves correctly. |
214 var loop = "for (var i = 0; i < 3; i++) { const x = i; }"; | 213 var loop = "for (var i = 0; i < 3; i++) { const x = i; }"; |
215 TestAll(0, loop, "x"); | 214 TestAll(0, loop, "x"); |
216 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x"); | 215 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x"); |
217 | 216 |
218 | 217 |
219 // Test that const inside with behaves correctly. | 218 // Test that const inside with behaves correctly. |
220 TestAll(87, "with ({x:42}) { const x = 87; }", "x"); | 219 TestAll(87, "with ({x:42}) { const x = 87; }", "x"); |
221 TestAll(undefined, "with ({x:42}) { const x; }", "x"); | 220 TestAll(undefined, "with ({x:42}) { const x; }", "x"); |
222 | |
223 | |
224 // Additional tests for how various combinations of re-declarations affect | |
225 // the values of the var/const in question. | |
226 try { | |
227 eval("var undefined;"); | |
228 } catch (ex) { | |
229 assertUnreachable("undefined (1) has thrown"); | |
230 } | |
231 | |
232 var original_undef = undefined; | |
233 var undefined = 1; // Should be silently ignored. | |
234 assertEquals(original_undef, undefined, "undefined got overwritten"); | |
235 undefined = original_undef; | |
236 | |
237 var a; const a; const a = 1; | |
238 assertEquals(1, a, "a has wrong value"); | |
239 a = 2; | |
240 assertEquals(2, a, "a should be writable"); | |
241 | |
242 var b = 1; const b = 2; | |
243 assertEquals(2, b, "b has wrong value"); | |
244 | |
245 var c = 1; const c = 2; const c = 3; | |
246 assertEquals(3, c, "c has wrong value"); | |
247 | |
248 const d = 1; const d = 2; | |
249 assertEquals(1, d, "d has wrong value"); | |
250 | |
251 const e = 1; var e = 2; | |
252 assertEquals(1, e, "e has wrong value"); | |
253 | |
254 const f = 1; const f; | |
255 assertEquals(1, f, "f has wrong value"); | |
256 | |
257 var g; const g = 1; | |
258 assertEquals(1, g, "g has wrong value"); | |
259 g = 2; | |
260 assertEquals(2, g, "g should be writable"); | |
261 | |
262 const h; var h = 1; | |
263 assertEquals(undefined,h, "h has wrong value"); | |
264 | |
265 eval("Object.defineProperty(this, 'i', { writable: true });" | |
266 + "const i = 7;" | |
267 + "assertEquals(7, i, \"i has wrong value\");"); | |
268 | |
269 var global = this; | |
270 assertThrows(function() { | |
271 Object.defineProperty(global, 'j', { writable: true }) | |
272 }, TypeError); | |
273 const j = 2; // This is what makes the function above throw, because the | |
274 // const declaration gets hoisted and makes the property non-configurable. | |
275 assertEquals(2, j, "j has wrong value"); | |
276 | |
277 var k = 1; const k; | |
278 // You could argue about the expected result here. For now, the winning | |
279 // argument is that "const k;" is equivalent to "const k = undefined;". | |
280 assertEquals(undefined, k, "k has wrong value"); | |
OLD | NEW |