OLD | NEW |
| (Empty) |
1 // Copyright 2016 the V8 project authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Flags: --allow-natives-syntax --harmony-explicit-tailcalls | |
6 // Flags: --harmony-do-expressions --harmony-async-await | |
7 "use strict"; | |
8 | |
9 var SyntaxErrorTests = [ | |
10 { msg: "Unexpected expression inside tail call", | |
11 tests: [ | |
12 { src: `()=>{ return continue foo ; }`, | |
13 err: ` ^^^`, | |
14 }, | |
15 { src: `()=>{ return continue 42 ; }`, | |
16 err: ` ^^`, | |
17 }, | |
18 { src: `()=>{ return continue new foo () ; }`, | |
19 err: ` ^^^^^^^^^^`, | |
20 }, | |
21 { src: `()=>{ loop: return continue loop ; }`, | |
22 err: ` ^^^^`, | |
23 }, | |
24 { src: `class A { foo() { return continue super.x ; } }`, | |
25 err: ` ^^^^^^^`, | |
26 }, | |
27 { src: `()=>{ return continue this ; }`, | |
28 err: ` ^^^^`, | |
29 }, | |
30 { src: `()=>{ return continue class A {} ; }`, | |
31 err: ` ^^^^^^^^^^`, | |
32 }, | |
33 { src: `()=>{ return continue class A extends B {} ; }`, | |
34 err: ` ^^^^^^^^^^^^^^^^^^^^`, | |
35 }, | |
36 { src: `()=>{ return continue function A() { } ; }`, | |
37 err: ` ^^^^^^^^^^^^^^^^`, | |
38 }, | |
39 { src: `()=>{ return continue { a: b, c: d} ; }`, | |
40 err: ` ^^^^^^^^^^^^^`, | |
41 }, | |
42 { src: `()=>{ return continue function* Gen() { yield 1; } ; }`, | |
43 err: ` ^^^^^^^^^^^^^^^^^^^^^^^^^^^^`, | |
44 }, | |
45 { src: `function A() { return continue new.target ; }`, | |
46 err: ` ^^^^^^^^^^`, | |
47 }, | |
48 { src: `()=>{ return continue () ; }`, | |
49 err: ` ^^`, | |
50 }, | |
51 { src: `()=>{ return continue ( 42 ) ; }`, | |
52 err: ` ^^^^^^`, | |
53 }, | |
54 { src: "()=>{ return continue `123 ${foo} 34lk` ; }", | |
55 err: ` ^^^^^^^^^^^^^^^^^`, | |
56 }, | |
57 { src: `()=>{ return continue do { x ? foo() : bar() ; } }`, | |
58 err: ` ^^^^^^^^^^^^^^^^^^^^^^^^^^`, | |
59 }, | |
60 ], | |
61 }, | |
62 { msg: "Tail call expression is not allowed here", | |
63 tests: [ | |
64 { src: `()=>{ return continue continue continue b() ; }`, | |
65 err: ` ^^^^^^^^^^^^`, | |
66 }, | |
67 { src: `()=>{ return continue ( continue b() ) ; }`, | |
68 err: ` ^^^^^^^^^^^^`, | |
69 }, | |
70 { src: `()=>{ return continue f() - a ; }`, | |
71 err: ` ^^^^^^^^^^^^^`, | |
72 }, | |
73 { src: `()=>{ return b + continue f() ; }`, | |
74 err: ` ^^^^^^^^^^^^^^`, | |
75 }, | |
76 { src: `()=>{ return 1, 2, 3, continue f() , 4 ; }`, | |
77 err: ` ^^^^^^^^^^^^^`, | |
78 }, | |
79 { src: `()=>{ var x = continue f ( ) ; }`, | |
80 err: ` ^^^^^^^^^^^^^^^`, | |
81 }, | |
82 { src: `()=>{ return continue f () ? 1 : 2 ; }`, | |
83 err: ` ^^^^^^^^^^^^^`, | |
84 }, | |
85 { src: `()=>{ return (1, 2, 3, continue f()), 4; }`, | |
86 err: ` ^^^^^^^^^^^^`, | |
87 }, | |
88 { src: `()=>{ return [1, 2, continue f() ] ; }`, | |
89 err: ` ^^^^^^^^^^^^`, | |
90 }, | |
91 { src: `()=>{ return [1, 2, ... continue f() ] ; }`, | |
92 err: ` ^^^^^^^^^^^^`, | |
93 }, | |
94 { src: `()=>{ return [1, 2, continue f(), 3 ] ; }`, | |
95 err: ` ^^^^^^^^^^^^`, | |
96 }, | |
97 { src: "()=>{ return `123 ${a} ${ continue foo ( ) } 34lk` ; }", | |
98 err: ` ^^^^^^^^^^^^^^^^`, | |
99 }, | |
100 { src: `()=>{ return g( 1, 2, continue f() ); }`, | |
101 err: ` ^^^^^^^^^^^^`, | |
102 }, | |
103 { src: `()=>{ return continue f() || a; }`, | |
104 err: ` ^^^^^^^^^^^^`, | |
105 }, | |
106 { src: `()=>{ return a || b || c || continue f() || d; }`, | |
107 err: ` ^^^^^^^^^^^^`, | |
108 }, | |
109 { src: `()=>{ return a && b && c && continue f() && d; }`, | |
110 err: ` ^^^^^^^^^^^^`, | |
111 }, | |
112 { src: `()=>{ return a && b || c && continue f() ? d : e; }`, | |
113 err: ` ^^^^^^^^^^^^`, | |
114 }, | |
115 { src: `()=>{ return a ? b : c && continue f() && d || e; }`, | |
116 err: ` ^^^^^^^^^^^^`, | |
117 }, | |
118 { src: `()=>{ return continue foo() instanceof bar ; }`, | |
119 err: ` ^^^^^^^^^^^^^^`, | |
120 }, | |
121 { src: `()=>{ return bar instanceof continue foo() ; }`, | |
122 err: ` ^^^^^^^^^^^^^^`, | |
123 }, | |
124 { src: `()=>{ return continue foo() in bar ; }`, | |
125 err: ` ^^^^^^^^^^^^^^`, | |
126 }, | |
127 { src: `()=>{ return bar in continue foo() ; }`, | |
128 err: ` ^^^^^^^^^^^^^^`, | |
129 }, | |
130 { src: `()=>{ function* G() { yield continue foo(); } }`, | |
131 err: ` ^^^^^`, | |
132 }, | |
133 { src: `()=>{ function* G() { return continue foo(); } }`, | |
134 err: ` ^^^^^`, | |
135 }, | |
136 { src: `()=>{ (1, 2, 3, continue f() ) => {} }`, | |
137 err: ` ^^^^^^^^^^^^`, | |
138 }, | |
139 { src: `()=>{ return a <= continue f(); }`, | |
140 err: ` ^^^^^^^^^^^^`, | |
141 }, | |
142 { src: `()=>{ return b > continue f(); }`, | |
143 err: ` ^^^^^^^^^^^^`, | |
144 }, | |
145 { src: `()=>{ return a << continue f(); }`, | |
146 err: ` ^^^^^^^^^^^^`, | |
147 }, | |
148 { src: `()=>{ return b >> continue f(); }`, | |
149 err: ` ^^^^^^^^^^^^`, | |
150 }, | |
151 { src: `()=>{ return c >>> continue f(); }`, | |
152 err: ` ^^^^^^^^^^^^`, | |
153 }, | |
154 { src: `()=>{ return continue f() = a ; }`, | |
155 err: ` ^^^^^^^^^^^^`, | |
156 }, | |
157 { src: `()=>{ return a = continue f() ; }`, | |
158 err: ` ^^^^^^^^^^^^`, | |
159 }, | |
160 { src: `()=>{ return a += continue f(); }`, | |
161 err: ` ^^^^^^^^^^^^`, | |
162 }, | |
163 { src: `()=>{ return a ** continue f() ; }`, | |
164 err: ` ^^^^^^^^^^^^`, | |
165 }, | |
166 { src: `()=>{ return delete continue foo() ; }`, | |
167 err: ` ^^^^^^^^^^^^^^`, | |
168 }, | |
169 { src: `()=>{ typeof continue foo() ; }`, | |
170 err: ` ^^^^^^^^^^^^^^`, | |
171 }, | |
172 { src: `()=>{ return ~ continue foo() ; }`, | |
173 err: ` ^^^^^^^^^^^^^^`, | |
174 }, | |
175 { src: `()=>{ return void continue foo() ; }`, | |
176 err: ` ^^^^^^^^^^^^^^`, | |
177 }, | |
178 { src: `()=>{ return !continue foo() ; }`, | |
179 err: ` ^^^^^^^^^^^^^^`, | |
180 }, | |
181 { src: `()=>{ return -continue foo() ; }`, | |
182 err: ` ^^^^^^^^^^^^^^`, | |
183 }, | |
184 { src: `()=>{ return +continue foo() ; }`, | |
185 err: ` ^^^^^^^^^^^^^^`, | |
186 }, | |
187 { src: `()=>{ return ++ continue f( ) ; }`, | |
188 err: ` ^^^^^^^^^^^^^`, | |
189 }, | |
190 { src: `()=>{ return continue f() ++; }`, | |
191 err: ` ^^^^^^^^^^^^`, | |
192 }, | |
193 { src: `()=>{ return continue f() --; }`, | |
194 err: ` ^^^^^^^^^^^^`, | |
195 }, | |
196 { src: `()=>{ return (continue foo()) () ; }`, | |
197 err: ` ^^^^^^^^^^^^^^`, | |
198 }, | |
199 { src: `()=>{ for (var i = continue foo(); i < 10; i++) bar(); }`, | |
200 err: ` ^^^^^^^^^^^^^^`, | |
201 }, | |
202 { src: `()=>{ for (var i = 0; i < continue foo(); i++) bar(); }`, | |
203 err: ` ^^^^^^^^^^^^^^`, | |
204 }, | |
205 { src: `()=>{ for (var i = 0; i < 10; continue foo()) bar(); }`, | |
206 err: ` ^^^^^^^^^^^^^^`, | |
207 }, | |
208 { src: `()=>{ if (continue foo()) bar(); }`, | |
209 err: ` ^^^^^^^^^^^^^^`, | |
210 }, | |
211 { src: `()=>{ while (continue foo()) bar(); }`, | |
212 err: ` ^^^^^^^^^^^^^^`, | |
213 }, | |
214 { src: `()=>{ do { smth; } while (continue foo()) ; }`, | |
215 err: ` ^^^^^^^^^^^^^^`, | |
216 }, | |
217 { src: `()=>{ throw continue foo() ; }`, | |
218 err: ` ^^^^^^^^^^^^^^`, | |
219 }, | |
220 { src: `()=>{ switch (continue foo()) { case 1: break; } ; }`, | |
221 err: ` ^^^^^^^^^^^^^^`, | |
222 }, | |
223 { src: `()=>{ let x = continue foo() }`, | |
224 err: ` ^^^^^^^^^^^^^^`, | |
225 }, | |
226 { src: `()=>{ const c = continue foo() }`, | |
227 err: ` ^^^^^^^^^^^^^^^`, | |
228 }, | |
229 { src: `class A {}; class B extends A { constructor() { return continue fo
o () ; } }`, | |
230 err: ` ^^^^^^^^^^^
^^^^`, | |
231 }, | |
232 { src: `class A extends continue f () {}; }`, | |
233 err: ` ^^^^^^^^^^^^^`, | |
234 }, | |
235 { src: `async() => continue foo()`, | |
236 err: ` ^^^^^`, | |
237 }, | |
238 ], | |
239 }, | |
240 { msg: "Tail call expression in try block", | |
241 tests: [ | |
242 { src: `()=>{ try { return continue f ( ) ; } catch(e) {} }`, | |
243 err: ` ^^^^^^^^^^^^^^^^`, | |
244 }, | |
245 { src: `()=>{ try { try { smth; } catch(e) { return continue f( ) ; } }`
, | |
246 err: ` ^^^^^^^^^^^^^^`, | |
247 }, | |
248 { src: `()=>{ try { try { smth; } catch(e) { return continue f( ) ; } }
finally { bla; } }`, | |
249 err: ` ^^^^^^^^^^^^^^`, | |
250 }, | |
251 ], | |
252 }, | |
253 { msg: "Tail call expression in catch block when finally block is also present
", | |
254 tests: [ | |
255 { src: `()=>{ try { smth; } catch(e) { return continue f ( ) ; } finall
y { blah; } }`, | |
256 err: ` ^^^^^^^^^^^^^^^^`, | |
257 }, | |
258 { src: `()=>{ try { smth; } catch(e) { try { smth; } catch (e) { return c
ontinue f ( ) ; } } finally { blah; } }`, | |
259 err: ` ^
^^^^^^^^^^^^^^^`, | |
260 }, | |
261 ], | |
262 }, | |
263 { msg: "Tail call expression in for-in/of body", | |
264 tests: [ | |
265 { src: `()=>{ for (var v in {a:0}) { return continue foo () ; } }`, | |
266 err: ` ^^^^^^^^^^^^^^^^`, | |
267 }, | |
268 { src: `()=>{ for (var v of [1, 2, 3]) { return continue foo () ; } }`, | |
269 err: ` ^^^^^^^^^^^^^^^^`, | |
270 }, | |
271 ], | |
272 }, | |
273 { msg: "Tail call of a direct eval is not allowed", | |
274 tests: [ | |
275 { src: `()=>{ return continue eval(" foo () " ) ; }`, | |
276 err: ` ^^^^^^^^^^^^^^^^^`, | |
277 }, | |
278 { src: `()=>{ return a || continue eval("", 1, 2) ; }`, | |
279 err: ` ^^^^^^^^^^^^^^`, | |
280 }, | |
281 { src: `()=>{ return a, continue eval ( ) ; }`, | |
282 err: ` ^^^^^^^^^`, | |
283 }, | |
284 { src: `()=> a, continue eval ( ) ; `, | |
285 err: ` ^^^^^^^^^`, | |
286 }, | |
287 { src: `()=> a || continue eval (' ' ) ; `, | |
288 err: ` ^^^^^^^^^^^^`, | |
289 }, | |
290 ], | |
291 }, | |
292 { msg: "Unexpected token continue", | |
293 tests: [ | |
294 { src: `()=>{ (... continue f()) => {} }`, | |
295 err: ` ^^^^^^^^`, | |
296 }, | |
297 { src: `()=>{ (a, b, c, ... continue f() ) => {} }`, | |
298 err: ` ^^^^^^^^`, | |
299 }, | |
300 ], | |
301 }, | |
302 { msg: "Undefined label 'foo'", | |
303 tests: [ | |
304 { src: `()=>{ continue foo () ; }`, | |
305 err: ` ^^^`, | |
306 }, | |
307 ], | |
308 }, | |
309 ]; | |
310 | |
311 | |
312 // Should parse successfully. | |
313 var NoErrorTests = [ | |
314 `()=>{ return continue a.b.c.foo () ; }`, | |
315 `()=>{ return continue a().b.c().d.foo () ; }`, | |
316 `()=>{ return continue foo (1)(2)(3, 4) ; }`, | |
317 `()=>{ return continue (0, eval)(); }`, | |
318 `()=>{ return ( continue b() ) ; }`, | |
319 "()=>{ return continue bar`ab cd ef` ; }", | |
320 "()=>{ return continue bar`ab ${cd} ef` ; }", | |
321 `()=>{ return a || continue f() ; }`, | |
322 `()=>{ return a && continue f() ; }`, | |
323 `()=>{ return a , continue f() ; }`, | |
324 `()=>{ class A { foo() { return continue super.f() ; } } }`, | |
325 `()=>{ function B() { return continue new.target() ; } }`, | |
326 `()=>{ return continue do { x ? foo() : bar() ; }() }`, | |
327 `()=>{ return continue (do { x ? foo() : bar() ; })() }`, | |
328 `()=>{ return do { 1, continue foo() } }`, | |
329 `()=>{ return do { x ? continue foo() : y } }`, | |
330 `()=>{ return a || (b && continue c()); }`, | |
331 `()=>{ return a && (b || continue c()); }`, | |
332 `()=>{ return a || (b ? c : continue d()); }`, | |
333 `()=>{ return 1, 2, 3, a || (b ? c : continue d()); }`, | |
334 `()=> continue (foo ()) ;`, | |
335 `()=> a || continue foo () ;`, | |
336 `()=> a && continue foo () ;`, | |
337 `()=> a ? continue foo () : b;`, | |
338 ]; | |
339 | |
340 | |
341 (function() { | |
342 for (var test_set of SyntaxErrorTests) { | |
343 var expected_message = "SyntaxError: " + test_set.msg; | |
344 for (var test of test_set.tests) { | |
345 var passed = true; | |
346 var e = null; | |
347 try { | |
348 eval(test.src); | |
349 } catch (ee) { | |
350 e = ee; | |
351 } | |
352 print("======================================="); | |
353 print("Expected | " + expected_message); | |
354 print("Source | " + test.src); | |
355 print(" | " + test.err); | |
356 | |
357 if (e === null) { | |
358 print("FAILED"); | |
359 throw new Error("SyntaxError was not thrown"); | |
360 } | |
361 | |
362 var details = %GetExceptionDetails(e); | |
363 if (details.start_pos == undefined || | |
364 details.end_pos == undefined) { | |
365 throw new Error("Bad message object returned"); | |
366 } | |
367 var underline = " ".repeat(details.start_pos) + | |
368 "^".repeat(details.end_pos - details.start_pos); | |
369 var passed = expected_message === e.toString() && | |
370 test.err === underline; | |
371 | |
372 if (passed) { | |
373 print("PASSED"); | |
374 print(); | |
375 } else { | |
376 print("---------------------------------------"); | |
377 print("Actual | " + e); | |
378 print("Source | " + test.src); | |
379 print(" | " + underline); | |
380 print("FAILED"); | |
381 throw new Error("Test failed"); | |
382 } | |
383 } | |
384 } | |
385 })(); | |
386 | |
387 | |
388 (function() { | |
389 for (var src of NoErrorTests) { | |
390 print("======================================="); | |
391 print("Source | " + src); | |
392 src = `"use strict"; ` + src; | |
393 Realm.eval(0, src); | |
394 print("PASSED"); | |
395 print(); | |
396 } | |
397 })(); | |
OLD | NEW |