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 17 matching lines...) Expand all Loading... |
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 // Function local const. |
37 function constDecl0(use) { | 37 function constDecl0(use) { |
38 return "(function() { const constvar = 1; " + use + "; });"; | 38 return "(function() { const constvar = 1; " + use + "; })();"; |
39 } | 39 } |
40 | 40 |
41 | 41 |
42 function constDecl1(use) { | 42 function constDecl1(use) { |
43 return "(function() { " + use + "; const constvar = 1; });"; | 43 return "(function() { " + use + "; const constvar = 1; })();"; |
44 } | 44 } |
45 | 45 |
46 | 46 |
47 // Function local const, assign from eval. | 47 // Function local const, assign from eval. |
48 function constDecl2(use) { | 48 function constDecl2(use) { |
49 use = "eval('(function() { " + use + " })')"; | 49 use = "eval('(function() { " + use + " })')()"; |
50 return "(function() { const constvar = 1; " + use + "; })();"; | 50 return "(function() { const constvar = 1; " + use + "; })();"; |
51 } | 51 } |
52 | 52 |
53 | 53 |
54 function constDecl3(use) { | 54 function constDecl3(use) { |
55 use = "eval('(function() { " + use + " })')"; | 55 use = "eval('(function() { " + use + " })')()"; |
56 return "(function() { " + use + "; const constvar = 1; })();"; | 56 return "(function() { " + use + "; const constvar = 1; })();"; |
57 } | 57 } |
58 | 58 |
59 | 59 |
60 // Block local const. | 60 // Block local const. |
61 function constDecl4(use) { | 61 function constDecl4(use) { |
62 return "(function() { { const constvar = 1; " + use + "; } });"; | 62 return "(function() { { const constvar = 1; " + use + "; } })();"; |
63 } | 63 } |
64 | 64 |
65 | 65 |
66 function constDecl5(use) { | 66 function constDecl5(use) { |
67 return "(function() { { " + use + "; const constvar = 1; } });"; | 67 return "(function() { { " + use + "; const constvar = 1; } })();"; |
68 } | 68 } |
69 | 69 |
70 | 70 |
71 // Block local const, assign from eval. | 71 // Block local const, assign from eval. |
72 function constDecl6(use) { | 72 function constDecl6(use) { |
73 use = "eval('(function() {" + use + "})')"; | 73 use = "eval('(function() {" + use + "})')()"; |
74 return "(function() { { const constvar = 1; " + use + "; } })();"; | 74 return "(function() { { const constvar = 1; " + use + "; } })();"; |
75 } | 75 } |
76 | 76 |
77 | 77 |
78 function constDecl7(use) { | 78 function constDecl7(use) { |
79 use = "eval('(function() {" + use + "})')"; | 79 use = "eval('(function() {" + use + "})')()"; |
80 return "(function() { { " + use + "; const constvar = 1; } })();"; | 80 return "(function() { { " + use + "; const constvar = 1; } })();"; |
81 } | 81 } |
82 | 82 |
83 | 83 |
84 // Function expression name. | 84 // Function expression name. |
85 function constDecl8(use) { | 85 function constDecl8(use) { |
86 return "(function constvar() { " + use + "; });"; | 86 return "(function constvar() { " + use + "; })();"; |
87 } | 87 } |
88 | 88 |
89 | 89 |
90 // Function expression name, assign from eval. | 90 // Function expression name, assign from eval. |
91 function constDecl9(use) { | 91 function constDecl9(use) { |
92 use = "eval('(function(){" + use + "})')"; | 92 use = "eval('(function(){" + use + "})')()"; |
93 return "(function constvar() { " + use + "; })();"; | 93 return "(function constvar() { " + use + "; })();"; |
94 } | 94 } |
95 | 95 |
96 let decls = [ constDecl0, | 96 let decls = [ constDecl0, |
97 constDecl1, | 97 constDecl1, |
98 constDecl2, | 98 constDecl2, |
99 constDecl3, | 99 constDecl3, |
100 constDecl4, | 100 constDecl4, |
101 constDecl5, | 101 constDecl5, |
102 constDecl6, | 102 constDecl6, |
103 constDecl7, | 103 constDecl7, |
104 constDecl8, | 104 constDecl8, |
105 constDecl9 | 105 constDecl9 |
106 ]; | 106 ]; |
| 107 let declsForTDZ = new Set([constDecl1, constDecl3, constDecl5, constDecl7]); |
107 let uses = [ 'constvar = 1;', | 108 let uses = [ 'constvar = 1;', |
108 'constvar += 1;', | 109 'constvar += 1;', |
109 '++constvar;', | 110 '++constvar;', |
110 'constvar++;' | 111 'constvar++;' |
111 ]; | 112 ]; |
112 | 113 |
113 function Test(d,u) { | 114 function Test(d,u) { |
114 'use strict'; | 115 'use strict'; |
115 try { | 116 try { |
116 print(d(u)); | 117 print(d(u)); |
117 eval(d(u)); | 118 eval(d(u)); |
118 } catch (e) { | 119 } catch (e) { |
119 assertInstanceof(e, SyntaxError); | 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); |
| 124 return; |
| 125 } |
| 126 assertInstanceof(e, TypeError); |
120 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); | 127 assertTrue(e.toString().indexOf("Assignment to constant variable") >= 0); |
121 return; | 128 return; |
122 } | 129 } |
123 assertUnreachable(); | 130 assertUnreachable(); |
124 } | 131 } |
125 | 132 |
126 for (var d = 0; d < decls.length; ++d) { | 133 for (var d = 0; d < decls.length; ++d) { |
127 for (var u = 0; u < uses.length; ++u) { | 134 for (var u = 0; u < uses.length; ++u) { |
128 Test(decls[d], uses[u]); | 135 Test(decls[d], uses[u]); |
129 } | 136 } |
130 } | 137 } |
OLD | NEW |