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 assertEquals(expected, TestGlobal(s,e), "global:'" + msg + "'"); | 101 // Redeclarations of global consts do not throw, they are silently ignored. |
| 102 assertEquals(42, TestGlobal(s, 42), "global:'" + msg + "'"); |
102 assertEquals(expected, TestContext(s,e), "context:'" + msg + "'"); | 103 assertEquals(expected, TestContext(s,e), "context:'" + msg + "'"); |
103 } | 104 } |
104 | 105 |
105 | 106 |
106 function TestConflict(def0, def1) { | 107 function TestConflict(def0, def1) { |
107 // No eval. | 108 // No eval. |
108 TestAll("TypeError", def0 +'; ' + def1); | 109 TestAll("TypeError", def0 +'; ' + def1); |
109 // Eval everything. | 110 // Eval everything. |
110 TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")'); | 111 TestAll("TypeError", 'eval("' + def0 + '; ' + def1 + '")'); |
111 // Eval first definition. | 112 // Eval first definition. |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
211 | 212 |
212 // Test that const inside loop behaves correctly. | 213 // Test that const inside loop behaves correctly. |
213 var loop = "for (var i = 0; i < 3; i++) { const x = i; }"; | 214 var loop = "for (var i = 0; i < 3; i++) { const x = i; }"; |
214 TestAll(0, loop, "x"); | 215 TestAll(0, loop, "x"); |
215 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x"); | 216 TestAll(0, "var a,b,c,d,e,f,g,h; " + loop, "x"); |
216 | 217 |
217 | 218 |
218 // Test that const inside with behaves correctly. | 219 // Test that const inside with behaves correctly. |
219 TestAll(87, "with ({x:42}) { const x = 87; }", "x"); | 220 TestAll(87, "with ({x:42}) { const x = 87; }", "x"); |
220 TestAll(undefined, "with ({x:42}) { const x; }", "x"); | 221 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 // According to ES5 10.5.8.c, the assignment "= 1" should be ignored. |
| 239 assertEquals(a, undefined, "a has wrong value"); |
| 240 a = 2; |
| 241 assertEquals(a, 2, "a should be writable"); |
| 242 |
| 243 var b = 1; const b = 2; |
| 244 assertEquals(b, 1, "b has wrong value"); |
| 245 |
| 246 var c = 1; const c = 2; const c = 3; |
| 247 assertEquals(c, 1, "c has wrong value"); |
| 248 |
| 249 const d = 1; const d = 2; |
| 250 assertEquals(d, 1, "d has wrong value"); |
| 251 |
| 252 const e = 1; var e = 2; |
| 253 assertEquals(e, 1, "e has wrong value"); |
| 254 |
| 255 const f = 1; const f; |
| 256 assertEquals(f, 1, "f has wrong value"); |
| 257 |
| 258 var g; const g = 1; |
| 259 assertEquals(g, undefined, "g has wrong value"); |
OLD | NEW |