OLD | NEW |
1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
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 assertNonErrorsBecomeEarly( |
| 158 `class A { |
| 159 m() { B; } |
| 160 } |
| 161 let i = 0; |
| 162 class B {}`); |
| 163 |
| 164 assertNonErrorsBecomeEarly( |
| 165 `class A { |
| 166 m() { B; } |
| 167 } |
| 168 function foo() {} |
| 169 class B {}`); |
| 170 |
| 171 assertNonErrorsBecomeEarly( |
| 172 `function foo() { |
| 173 class A { |
| 174 m() { B; } |
| 175 } |
| 176 } |
| 177 class B {}`); |
| 178 |
| 179 assertNonErrorsBecomeEarly( |
| 180 `class A extends class B { m() { C; } } { |
| 181 } |
| 182 class C { }`); |
| 183 |
| 184 assertLateErrorsBecomeEarly( |
| 185 `class A extends class B { [C.sm()]() { } } { |
| 186 } |
| 187 class C { static sm() { return 'a';} }`); |
| 188 |
| 189 assertLateErrorsBecomeEarly( |
| 190 `class A extends class B extends C { } { |
| 191 } |
| 192 class C { }`); |
| 193 })(); |
| 194 |
| 195 |
| 196 (function RegressionForClassResolution() { |
| 197 assertNonErrorsBecomeEarly( |
| 198 `let A = class B { |
| 199 m() { C; } |
| 200 } |
| 201 ;;;; |
| 202 class C {} |
| 203 class B {}`); |
| 204 })(); |
| 205 |
| 206 |
| 207 (function TestMultipleMethodScopes() { |
| 208 "use strong"; |
| 209 |
| 210 // Test cases where the reference is inside multiple method scopes. |
| 211 class A1 { |
| 212 m() { |
| 213 class C1 { |
| 214 m() { B1; } |
| 215 } |
| 216 } |
| 217 } |
| 218 class B1 { } |
| 219 |
| 220 ; |
| 221 |
| 222 class A2 { |
| 223 m() { |
| 224 class C2 extends B2 { |
| 225 } |
| 226 } |
| 227 } |
| 228 class B2 { } |
| 229 })(); |
OLD | NEW |