OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
rossberg
2015/04/23 12:20:53
Perhaps add a few sanity tests with class expressi
marja
2015/04/23 13:12:43
Done.
| |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Flags: --strong-mode | 5 // Flags: --strong-mode --harmony-arrow-functions |
6 "use strict" | |
6 | 7 |
7 // Note that it's essential for these tests that the reference is inside dead | 8 let prologue_dead = "(function outer() { if (false) { "; |
8 // code (because we already produce ReferenceErrors for run-time unresolved | 9 let epilogue_dead = " } })();"; |
9 // variables and don't want to confuse those with strong mode errors). But the | |
10 // errors should *not* be inside lazy, unexecuted functions, since lazy parsing | |
11 // doesn't produce strong mode scoping errors). | |
12 | 10 |
13 // In addition, assertThrows will call eval and that changes variable binding | 11 let prologue_live = "(function outer() { "; |
14 // types (see e.g., UNBOUND_EVAL_SHADOWED). We can avoid unwanted side effects | 12 let epilogue_live = "})();"; |
15 // by wrapping the code to be tested inside an outer function. | |
16 function assertThrowsHelper(code) { | |
17 "use strict"; | |
18 let prologue_dead = "(function outer() { if (false) { "; | |
19 let epilogue_dead = " } })();"; | |
20 | 13 |
21 assertThrows("'use strong'; " + prologue_dead + code + epilogue_dead, Referenc eError); | 14 // For code which already throws a run-time error in non-strong mode; we assert |
15 // that we now get the error already compilation time. | |
16 function assertLateErrorsBecomeEarly(code) { | |
17 assertThrows("'use strong'; " + prologue_dead + code + epilogue_dead, | |
18 ReferenceError); | |
22 | 19 |
23 // Make sure the error happens only in strong mode (note that we need strict | 20 // Make sure the error happens only in strong mode (note that we need strict |
24 // mode here because of let). | 21 // mode here because of let). |
25 assertDoesNotThrow("'use strict'; " + prologue_dead + code + epilogue_dead); | 22 assertDoesNotThrow("'use strict'; " + prologue_dead + code + epilogue_dead); |
26 | 23 |
27 // But if we don't put the references inside a dead code, it throws a run-time | 24 // But if we don't put the references inside a dead code, it throws a run-time |
28 // error (also in strict mode). | 25 // error (also in strict mode). |
29 let prologue_live = "(function outer() { "; | 26 assertThrows("'use strong'; " + prologue_live + code + epilogue_live, |
30 let epilogue_live = "})();"; | 27 ReferenceError); |
28 assertThrows("'use strict'; " + prologue_live + code + epilogue_live, | |
29 ReferenceError); | |
30 } | |
31 | 31 |
32 assertThrows("'use strong'; " + prologue_live + code + epilogue_live, Referenc eError); | 32 // For code which doesn't throw an error at all in non-strong mode. |
33 assertThrows("'use strict'; " + prologue_live + code + epilogue_live, Referenc eError); | 33 function assertNonErrorsBecomeEarly(code) { |
34 assertThrows("'use strong'; " + prologue_dead + code + epilogue_dead, | |
35 ReferenceError); | |
36 assertDoesNotThrow("'use strict'; " + prologue_dead + code + epilogue_dead); | |
37 | |
38 assertThrows("'use strong'; " + prologue_live + code + epilogue_live, | |
39 ReferenceError); | |
40 assertDoesNotThrow("'use strict'; " + prologue_live + code + epilogue_live, | |
41 ReferenceError); | |
34 } | 42 } |
35 | 43 |
36 (function InitTimeReferenceForward() { | 44 (function InitTimeReferenceForward() { |
37 // It's never OK to have an init time reference to a class which hasn't been | 45 // It's never OK to have an init time reference to a class which hasn't been |
38 // declared. | 46 // declared. |
39 assertThrowsHelper( | 47 assertLateErrorsBecomeEarly( |
40 `class A extends B { }; | 48 `class A extends B { } |
41 class B {}`); | 49 class B {}`); |
42 | 50 |
43 assertThrowsHelper( | 51 assertLateErrorsBecomeEarly( |
44 `class A { | 52 `class A { |
45 [B.sm()]() { } | 53 [B.sm()]() { } |
46 }; | 54 } |
47 class B { | 55 class B { |
48 static sm() { return 0; } | 56 static sm() { return 0; } |
49 }`); | 57 }`); |
50 })(); | 58 })(); |
51 | 59 |
52 (function InitTimeReferenceBackward() { | 60 (function InitTimeReferenceBackward() { |
53 // Backwards is of course fine. | 61 // Backwards is of course fine. |
54 "use strong"; | 62 "use strong"; |
55 class A { | 63 class A { |
56 static sm() { return 0; } | 64 static sm() { return 0; } |
57 }; | 65 } |
58 let i = "making these classes non-consecutive"; | 66 let i = "making these classes non-consecutive"; |
59 class B extends A {}; | 67 class B extends A {}; |
60 "by inserting statements and declarations in between"; | 68 "by inserting statements and declarations in between"; |
61 class C { | 69 class C { |
62 [A.sm()]() { } | 70 [A.sm()]() { } |
63 }; | 71 }; |
64 })(); | 72 })(); |
65 | 73 |
66 (function BasicMutualRecursion() { | 74 (function BasicMutualRecursion() { |
67 "use strong"; | 75 "use strong"; |
68 class A { | 76 class A { |
69 m() { B; } | 77 m() { B; } |
70 static sm() { B; } | 78 static sm() { B; } |
71 }; | 79 } |
72 // No statements or declarations between the classes. | 80 // No statements or declarations between the classes. |
73 class B { | 81 class B { |
74 m() { A; } | 82 m() { A; } |
75 static sm() { A; } | 83 static sm() { A; } |
76 }; | 84 } |
77 })(); | 85 })(); |
86 | |
87 (function MutualRecursionWithMoreClasses() { | |
88 "use strong"; | |
89 class A { | |
90 m() { B; C; } | |
91 static sm() { B; C; } | |
92 } | |
93 class B { | |
94 m() { A; C; } | |
95 static sm() { A; C; } | |
96 } | |
97 class C { | |
98 m() { A; B; } | |
99 static sm() { A; B; } | |
100 } | |
101 })(); | |
102 | |
103 (function ReferringForwardInDeeperScopes() { | |
104 "use strong"; | |
105 | |
106 function foo() { | |
107 class A1 { | |
108 m() { B1; } | |
109 } | |
110 class B1 { } | |
111 } | |
112 | |
113 class Outer { | |
114 m() { | |
115 class A2 { | |
116 m() { B2; } | |
117 } | |
118 class B2 { } | |
119 } | |
120 } | |
121 | |
122 for (let i = 0; i < 1; ++i) { | |
123 class A3 { | |
124 m() { B3; } | |
125 } | |
126 class B3 { } | |
127 } | |
128 | |
129 (a, b) => { | |
130 class A4 { | |
131 m() { B4; } | |
132 } | |
133 class B4 { } | |
134 } | |
135 })(); | |
136 | |
137 (function ReferringForwardButClassesNotConsecutive() { | |
138 assertNonErrorsBecomeEarly( | |
139 `class A { | |
140 m() { B; } | |
141 } | |
142 ; | |
143 class B {}`); | |
144 | |
145 assertNonErrorsBecomeEarly( | |
146 `let A = class { | |
147 m() { B; } | |
148 } | |
149 class B {}`); | |
150 | |
151 assertNonErrorsBecomeEarly( | |
152 `class A { | |
153 m() { B1; } // Just a normal use-before-declaration. | |
154 } | |
155 let B1 = class B2 {}`); | |
156 | |
157 // Note that this is also an error: | |
158 // if (false) { | |
159 // class A { | |
160 // m() { B2; } | |
161 // } | |
162 // // Class declared inside a let statement -> not a consecutive group. | |
163 // let B1 = class B2 {} | |
164 // } | |
165 // But we cannot test it here, because B2 will be unbound (potentially bound | |
166 // to the global object), and not subject to static strong mode scoping | |
167 // checks. | |
168 | |
169 assertNonErrorsBecomeEarly( | |
170 `class A { | |
171 m() { B; } | |
172 } | |
173 let i = 0; | |
174 class B {}`); | |
175 | |
176 assertNonErrorsBecomeEarly( | |
177 `class A { | |
178 m() { B; } | |
179 } | |
180 function foo() {} | |
181 class B {}`); | |
182 | |
183 assertNonErrorsBecomeEarly( | |
184 `function foo() { | |
185 class A { | |
186 m() { B; } | |
187 } | |
188 } | |
189 class B {}`); | |
190 })(); | |
191 | |
192 | |
193 (function RegressionForClassResolution() { | |
194 assertNonErrorsBecomeEarly( | |
195 `let A = class B { | |
196 m() { C; } | |
197 } | |
198 ;;;; | |
199 class C {} | |
200 class B {}`); | |
201 })(); | |
202 | |
203 | |
204 (function TestMultipleMethodScopes() { | |
205 "use strong"; | |
206 | |
207 // Test cases where the reference is inside multiple method scopes. | |
208 class A1 { | |
209 m() { | |
210 class C1 { | |
211 m() { B1; } | |
212 } | |
213 } | |
214 } | |
215 class B1 { } | |
216 | |
217 ; | |
218 | |
219 class A2 { | |
220 m() { | |
221 class C2 extends B2 { | |
222 } | |
223 } | |
224 } | |
225 class B2 { } | |
226 })(); | |
OLD | NEW |