OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 import "package:expect/expect.dart"; |
| 6 import "package:compiler/src/js/js.dart"; |
| 7 import "package:compiler/src/js/rewrite_async.dart"; |
| 8 |
| 9 import "backend_dart/dart_printer_test.dart" show PrintDiagnosticListener; |
| 10 |
| 11 void testTransform(String source, String expected) { |
| 12 Fun fun = js(source); |
| 13 Fun rewritten = new AsyncRewriter( |
| 14 null, // The diagnostic helper should not be used in these tests. |
| 15 null, |
| 16 thenHelper: new VariableUse("thenHelper"), |
| 17 newController: new New(new VariableUse("newCompleter"), []), |
| 18 endOfIteration: new VariableUse("endOfIteration"), |
| 19 newIterable: new New(new VariableUse("newCompleter"), []), |
| 20 safeVariableName: (String name) => "__$name").rewrite(fun); |
| 21 Printer printer = new Printer(new PrintDiagnosticListener(), null); |
| 22 printer.visit(rewritten); |
| 23 Expect.stringEquals(expected, printer.outBuffer.getText()); |
| 24 } |
| 25 |
| 26 main() { |
| 27 testTransform(""" |
| 28 function() async { |
| 29 print(this.x); // Ensure `this` is translated in the helper function. |
| 30 await foo(); |
| 31 }""", """ |
| 32 function() { |
| 33 var __goto = 0, __completer = new newCompleter(), __self = this; |
| 34 function __helper(__result) { |
| 35 while (true) |
| 36 switch (__goto) { |
| 37 case 0: |
| 38 // Function start |
| 39 print(__self.x); |
| 40 __goto = 1; |
| 41 return thenHelper(foo(), __helper, __completer, null); |
| 42 case 1: |
| 43 // returning from await. |
| 44 // implicit return |
| 45 return thenHelper(null, null, __completer, null); |
| 46 } |
| 47 } |
| 48 return thenHelper(null, __helper, __completer, null); |
| 49 }"""); |
| 50 |
| 51 testTransform(""" |
| 52 function() async { |
| 53 try { |
| 54 __outer: while (true) { // Overlapping label name. |
| 55 try { |
| 56 inner: while (true) { |
| 57 break __outer; // Break from untranslated loop to translated target. |
| 58 break; // break to untranslated target. |
| 59 } |
| 60 while (true) { |
| 61 return; // Return via finallies. |
| 62 } |
| 63 var __helper = await foo(); // Overlapping variable name. |
| 64 } finally { |
| 65 foo(); |
| 66 continue; // Continue from finally with pending finally. |
| 67 return 2; // Return from finally with pending finally. |
| 68 } |
| 69 } |
| 70 } finally { |
| 71 return 3; // Return from finally with no pending finally. |
| 72 } |
| 73 return 4; |
| 74 }""", """ |
| 75 function() { |
| 76 var __goto = 0, __completer = new newCompleter(), __handler = null, __next, __
returnValue, __helper; |
| 77 function __helper1(__result) { |
| 78 while (true) |
| 79 try { |
| 80 __outer1: |
| 81 switch (__goto) { |
| 82 case 0: |
| 83 // Function start |
| 84 __handler = 2; |
| 85 case 6: |
| 86 // continue __outer |
| 87 case 7: |
| 88 // while condition |
| 89 __handler = 9; |
| 90 inner: { |
| 91 while (true) { |
| 92 __next = [5]; |
| 93 // goto finally |
| 94 __goto = 10; |
| 95 break __outer1; |
| 96 break; |
| 97 } |
| 98 } |
| 99 while (true) { |
| 100 __next = [1, 3]; |
| 101 // goto finally |
| 102 __goto = 10; |
| 103 break __outer1; |
| 104 } |
| 105 __goto = 12; |
| 106 return thenHelper(foo(), __helper1, __completer, function(__error)
{ |
| 107 __goto = 9; |
| 108 __helper1(__error); |
| 109 }); |
| 110 case 12: |
| 111 // returning from await. |
| 112 __helper = __result; |
| 113 __handler = 2; |
| 114 __next = [11]; |
| 115 // goto finally |
| 116 __goto = 10; |
| 117 break; |
| 118 case 9: |
| 119 // catch |
| 120 __next = [11]; |
| 121 case 10: |
| 122 // finally |
| 123 foo(); |
| 124 // goto while condition |
| 125 __goto = 7; |
| 126 break; |
| 127 __returnValue = 2; |
| 128 __next = [1]; |
| 129 // goto finally |
| 130 __goto = 3; |
| 131 break; |
| 132 // goto the next finally handler |
| 133 __goto = __next.pop(); |
| 134 break; |
| 135 case 11: |
| 136 // after finally |
| 137 // goto while condition |
| 138 __goto = 7; |
| 139 break; |
| 140 case 8: |
| 141 // after while |
| 142 case 5: |
| 143 // break __outer |
| 144 __handler = null; |
| 145 __next = [4]; |
| 146 // goto finally |
| 147 __goto = 3; |
| 148 break; |
| 149 case 2: |
| 150 // catch |
| 151 __next = [4]; |
| 152 case 3: |
| 153 // finally |
| 154 __returnValue = 3; |
| 155 // goto Return |
| 156 __goto = 1; |
| 157 break; |
| 158 // goto the next finally handler |
| 159 __goto = __next.pop(); |
| 160 break; |
| 161 case 4: |
| 162 // after finally |
| 163 __returnValue = 4; |
| 164 // goto Return |
| 165 __goto = 1; |
| 166 break; |
| 167 case 1: |
| 168 // Return |
| 169 return thenHelper(__returnValue, null, __completer, null); |
| 170 } |
| 171 } catch (__error) { |
| 172 if (__handler === null) |
| 173 throw __error; |
| 174 __result = __error; |
| 175 __goto = __handler; |
| 176 } |
| 177 |
| 178 } |
| 179 return thenHelper(null, __helper1, __completer, null); |
| 180 }"""); |
| 181 testTransform(""" |
| 182 function() async { |
| 183 var a, b, c, d, e, f; |
| 184 a = b++; // post- and preincrements. |
| 185 b = --b; |
| 186 c = (await foo()).a++; |
| 187 d = ++(await foo()).a; |
| 188 e = foo1()[await foo2()]--; |
| 189 f = --foo1()[await foo2()]; |
| 190 }""", """ |
| 191 function() { |
| 192 var __goto = 0, __completer = new newCompleter(), a, b, c, d, e, f, __temp1; |
| 193 function __helper(__result) { |
| 194 while (true) |
| 195 switch (__goto) { |
| 196 case 0: |
| 197 // Function start |
| 198 a = b++; |
| 199 b = --b; |
| 200 __goto = 1; |
| 201 return thenHelper(foo(), __helper, __completer, null); |
| 202 case 1: |
| 203 // returning from await. |
| 204 c = __result.a++; |
| 205 __goto = 2; |
| 206 return thenHelper(foo(), __helper, __completer, null); |
| 207 case 2: |
| 208 // returning from await. |
| 209 d = ++__result.a; |
| 210 __temp1 = foo1(); |
| 211 __goto = 3; |
| 212 return thenHelper(foo2(), __helper, __completer, null); |
| 213 case 3: |
| 214 // returning from await. |
| 215 e = __temp1[__result]--; |
| 216 __temp1 = foo1(); |
| 217 __goto = 4; |
| 218 return thenHelper(foo2(), __helper, __completer, null); |
| 219 case 4: |
| 220 // returning from await. |
| 221 f = --__temp1[__result]; |
| 222 // implicit return |
| 223 return thenHelper(null, null, __completer, null); |
| 224 } |
| 225 } |
| 226 return thenHelper(null, __helper, __completer, null); |
| 227 }"""); |
| 228 testTransform(""" |
| 229 function() async { |
| 230 var a, b, c, d, e, f, g, h; // empty initializer |
| 231 a = foo1() || await foo2(); // short circuiting operators |
| 232 b = await foo1() || foo2(); |
| 233 c = await foo1() || foo3(await foo2()); |
| 234 d = foo1() || foo2(); |
| 235 e = foo1() && await foo2(); |
| 236 f = await foo1() && foo2(); |
| 237 g = await foo1() && await foo2(); |
| 238 h = foo1() && foo2(); |
| 239 }""", """ |
| 240 function() { |
| 241 var __goto = 0, __completer = new newCompleter(), a, b, c, d, e, f, g, h, __te
mp1; |
| 242 function __helper(__result) { |
| 243 while (true) |
| 244 switch (__goto) { |
| 245 case 0: |
| 246 // Function start |
| 247 __temp1 = foo1(); |
| 248 if (__temp1) { |
| 249 // goto then |
| 250 __goto = 1; |
| 251 break; |
| 252 } else |
| 253 __result = __temp1; |
| 254 // goto join |
| 255 __goto = 2; |
| 256 break; |
| 257 case 1: |
| 258 // then |
| 259 __goto = 3; |
| 260 return thenHelper(foo2(), __helper, __completer, null); |
| 261 case 3: |
| 262 // returning from await. |
| 263 case 2: |
| 264 // join |
| 265 a = __result; |
| 266 __goto = 4; |
| 267 return thenHelper(foo1(), __helper, __completer, null); |
| 268 case 4: |
| 269 // returning from await. |
| 270 b = __result || foo2(); |
| 271 __goto = 7; |
| 272 return thenHelper(foo1(), __helper, __completer, null); |
| 273 case 7: |
| 274 // returning from await. |
| 275 __temp1 = __result; |
| 276 if (__temp1) { |
| 277 // goto then |
| 278 __goto = 5; |
| 279 break; |
| 280 } else |
| 281 __result = __temp1; |
| 282 // goto join |
| 283 __goto = 6; |
| 284 break; |
| 285 case 5: |
| 286 // then |
| 287 __temp1 = foo3; |
| 288 __goto = 8; |
| 289 return thenHelper(foo2(), __helper, __completer, null); |
| 290 case 8: |
| 291 // returning from await. |
| 292 __result = __temp1(__result); |
| 293 case 6: |
| 294 // join |
| 295 c = __result; |
| 296 d = foo1() || foo2(); |
| 297 __temp1 = foo1(); |
| 298 if (__temp1) |
| 299 __result = __temp1; |
| 300 else { |
| 301 // goto then |
| 302 __goto = 9; |
| 303 break; |
| 304 } |
| 305 // goto join |
| 306 __goto = 10; |
| 307 break; |
| 308 case 9: |
| 309 // then |
| 310 __goto = 11; |
| 311 return thenHelper(foo2(), __helper, __completer, null); |
| 312 case 11: |
| 313 // returning from await. |
| 314 case 10: |
| 315 // join |
| 316 e = __result; |
| 317 __goto = 12; |
| 318 return thenHelper(foo1(), __helper, __completer, null); |
| 319 case 12: |
| 320 // returning from await. |
| 321 f = __result && foo2(); |
| 322 __goto = 15; |
| 323 return thenHelper(foo1(), __helper, __completer, null); |
| 324 case 15: |
| 325 // returning from await. |
| 326 __temp1 = __result; |
| 327 if (__temp1) |
| 328 __result = __temp1; |
| 329 else { |
| 330 // goto then |
| 331 __goto = 13; |
| 332 break; |
| 333 } |
| 334 // goto join |
| 335 __goto = 14; |
| 336 break; |
| 337 case 13: |
| 338 // then |
| 339 __goto = 16; |
| 340 return thenHelper(foo2(), __helper, __completer, null); |
| 341 case 16: |
| 342 // returning from await. |
| 343 case 14: |
| 344 // join |
| 345 g = __result; |
| 346 h = foo1() && foo2(); |
| 347 // implicit return |
| 348 return thenHelper(null, null, __completer, null); |
| 349 } |
| 350 } |
| 351 return thenHelper(null, __helper, __completer, null); |
| 352 }"""); |
| 353 testTransform(""" |
| 354 function(x, y) async { |
| 355 while (true) { |
| 356 switch(y) { // Switch with no awaits in case key expressions |
| 357 case 0: |
| 358 case 1: |
| 359 await foo(); |
| 360 continue; // Continue the loop, not the switch |
| 361 case 1: // Duplicate case |
| 362 await foo(); |
| 363 break; // Break the switch |
| 364 case 2: |
| 365 foo(); // No default |
| 366 } |
| 367 } |
| 368 }""", """ |
| 369 function(x, y) { |
| 370 var __goto = 0, __completer = new newCompleter(); |
| 371 function __helper(__result) { |
| 372 while (true) |
| 373 switch (__goto) { |
| 374 case 0: |
| 375 // Function start |
| 376 case 1: |
| 377 // while condition |
| 378 case 3: |
| 379 // switch |
| 380 switch (y) { |
| 381 case 0: |
| 382 // goto case |
| 383 __goto = 5; |
| 384 break; |
| 385 case 1: |
| 386 // goto case |
| 387 __goto = 6; |
| 388 break; |
| 389 case 1: |
| 390 // goto case |
| 391 __goto = 7; |
| 392 break; |
| 393 case 2: |
| 394 // goto case |
| 395 __goto = 8; |
| 396 break; |
| 397 } |
| 398 // goto after switch |
| 399 __goto = 4; |
| 400 break; |
| 401 case 5: |
| 402 // case |
| 403 case 6: |
| 404 // case |
| 405 __goto = 9; |
| 406 return thenHelper(foo(), __helper, __completer, null); |
| 407 case 9: |
| 408 // returning from await. |
| 409 // goto while condition |
| 410 __goto = 1; |
| 411 break; |
| 412 case 7: |
| 413 // case |
| 414 __goto = 10; |
| 415 return thenHelper(foo(), __helper, __completer, null); |
| 416 case 10: |
| 417 // returning from await. |
| 418 // goto after switch |
| 419 __goto = 4; |
| 420 break; |
| 421 case 8: |
| 422 // case |
| 423 foo(); |
| 424 case 4: |
| 425 // after switch |
| 426 // goto while condition |
| 427 __goto = 1; |
| 428 break; |
| 429 case 2: |
| 430 // after while |
| 431 // implicit return |
| 432 return thenHelper(null, null, __completer, null); |
| 433 } |
| 434 } |
| 435 return thenHelper(null, __helper, __completer, null); |
| 436 }"""); |
| 437 testTransform(""" |
| 438 function() async { |
| 439 do { |
| 440 var a = await foo(); |
| 441 if (a) // If with no awaits in body |
| 442 break; |
| 443 else |
| 444 continue; |
| 445 } while (await foo()); |
| 446 } |
| 447 """, """ |
| 448 function() { |
| 449 var __goto = 0, __completer = new newCompleter(), a; |
| 450 function __helper(__result) { |
| 451 while (true) |
| 452 switch (__goto) { |
| 453 case 0: |
| 454 // Function start |
| 455 case 1: |
| 456 // do body |
| 457 __goto = 4; |
| 458 return thenHelper(foo(), __helper, __completer, null); |
| 459 case 4: |
| 460 // returning from await. |
| 461 a = __result; |
| 462 if (a) { |
| 463 // goto after do |
| 464 __goto = 3; |
| 465 break; |
| 466 } else { |
| 467 // goto do condition |
| 468 __goto = 2; |
| 469 break; |
| 470 } |
| 471 case 2: |
| 472 // do condition |
| 473 __goto = 5; |
| 474 return thenHelper(foo(), __helper, __completer, null); |
| 475 case 5: |
| 476 // returning from await. |
| 477 if (__result) { |
| 478 // goto do body |
| 479 __goto = 1; |
| 480 break; |
| 481 } |
| 482 case 3: |
| 483 // after do |
| 484 // implicit return |
| 485 return thenHelper(null, null, __completer, null); |
| 486 } |
| 487 } |
| 488 return thenHelper(null, __helper, __completer, null); |
| 489 }"""); |
| 490 |
| 491 testTransform(""" |
| 492 function() async { |
| 493 for (var i = 0; i < await foo1(); i += await foo2()) { |
| 494 if (foo(i)) |
| 495 continue; |
| 496 else |
| 497 break; |
| 498 if (!foo(i)) { // If with no else and await in body. |
| 499 await foo(); |
| 500 return; |
| 501 } |
| 502 print(await(foo(i))); |
| 503 } |
| 504 } |
| 505 """, """ |
| 506 function() { |
| 507 var __goto = 0, __completer = new newCompleter(), __returnValue, i, __temp1; |
| 508 function __helper(__result) { |
| 509 while (true) |
| 510 switch (__goto) { |
| 511 case 0: |
| 512 // Function start |
| 513 i = 0; |
| 514 case 2: |
| 515 // for condition |
| 516 __temp1 = i; |
| 517 __goto = 5; |
| 518 return thenHelper(foo1(), __helper, __completer, null); |
| 519 case 5: |
| 520 // returning from await. |
| 521 if (!(__temp1 < __result)) { |
| 522 // goto after for |
| 523 __goto = 4; |
| 524 break; |
| 525 } |
| 526 if (foo(i)) { |
| 527 // goto for update |
| 528 __goto = 3; |
| 529 break; |
| 530 } else { |
| 531 // goto after for |
| 532 __goto = 4; |
| 533 break; |
| 534 } |
| 535 __goto = !foo(i) ? 6 : 7; |
| 536 break; |
| 537 case 6: |
| 538 // then |
| 539 __goto = 8; |
| 540 return thenHelper(foo(), __helper, __completer, null); |
| 541 case 8: |
| 542 // returning from await. |
| 543 // goto Return |
| 544 __goto = 1; |
| 545 break; |
| 546 case 7: |
| 547 // join |
| 548 __temp1 = print; |
| 549 __goto = 9; |
| 550 return thenHelper(foo(i), __helper, __completer, null); |
| 551 case 9: |
| 552 // returning from await. |
| 553 __temp1(__result); |
| 554 case 3: |
| 555 // for update |
| 556 __goto = 10; |
| 557 return thenHelper(foo2(), __helper, __completer, null); |
| 558 case 10: |
| 559 // returning from await. |
| 560 i = __result; |
| 561 // goto for condition |
| 562 __goto = 2; |
| 563 break; |
| 564 case 4: |
| 565 // after for |
| 566 case 1: |
| 567 // Return |
| 568 return thenHelper(__returnValue, null, __completer, null); |
| 569 } |
| 570 } |
| 571 return thenHelper(null, __helper, __completer, null); |
| 572 }"""); |
| 573 |
| 574 testTransform(""" |
| 575 function(a) async { |
| 576 var x = {"a": foo1(), "b": await foo2(), "c": foo3()}; |
| 577 x["a"] = 2; // Different assignments |
| 578 (await foo()).a = 3; |
| 579 x[await foo()] = 4; |
| 580 x[(await foo1()).a = await foo2()] = 5; |
| 581 (await foo1())[await foo2()] = await foo3(6); |
| 582 } |
| 583 """, """ |
| 584 function(a) { |
| 585 var __goto = 0, __completer = new newCompleter(), x, __temp1, __temp2; |
| 586 function __helper(__result) { |
| 587 while (true) |
| 588 switch (__goto) { |
| 589 case 0: |
| 590 // Function start |
| 591 __temp1 = foo1(); |
| 592 __goto = 1; |
| 593 return thenHelper(foo2(), __helper, __completer, null); |
| 594 case 1: |
| 595 // returning from await. |
| 596 x = {a: __temp1, b: __result, c: foo3()}; |
| 597 x.a = 2; |
| 598 __goto = 2; |
| 599 return thenHelper(foo(), __helper, __completer, null); |
| 600 case 2: |
| 601 // returning from await. |
| 602 __result.a = 3; |
| 603 __temp1 = x; |
| 604 __goto = 3; |
| 605 return thenHelper(foo(), __helper, __completer, null); |
| 606 case 3: |
| 607 // returning from await. |
| 608 __temp1[__result] = 4; |
| 609 __temp1 = x; |
| 610 __goto = 4; |
| 611 return thenHelper(foo1(), __helper, __completer, null); |
| 612 case 4: |
| 613 // returning from await. |
| 614 __temp2 = __result; |
| 615 __goto = 5; |
| 616 return thenHelper(foo2(), __helper, __completer, null); |
| 617 case 5: |
| 618 // returning from await. |
| 619 __temp1[__temp2.a = __result] = 5; |
| 620 __goto = 6; |
| 621 return thenHelper(foo1(), __helper, __completer, null); |
| 622 case 6: |
| 623 // returning from await. |
| 624 __temp1 = __result; |
| 625 __goto = 7; |
| 626 return thenHelper(foo2(), __helper, __completer, null); |
| 627 case 7: |
| 628 // returning from await. |
| 629 __temp2 = __result; |
| 630 __goto = 8; |
| 631 return thenHelper(foo3(6), __helper, __completer, null); |
| 632 case 8: |
| 633 // returning from await. |
| 634 __temp1[__temp2] = __result; |
| 635 // implicit return |
| 636 return thenHelper(null, null, __completer, null); |
| 637 } |
| 638 } |
| 639 return thenHelper(null, __helper, __completer, null); |
| 640 }"""); |
| 641 testTransform(""" |
| 642 function(c) async { |
| 643 try { |
| 644 var x = c ? await foo() : foo(); // conditional |
| 645 var y = {}; |
| 646 } catch (error) { |
| 647 try { |
| 648 x = c ? await fooError(error) : fooError(error); |
| 649 } catch (error) { // nested error handler with overlapping name |
| 650 y.x = foo(error); |
| 651 } finally { |
| 652 foo(x); |
| 653 } |
| 654 } |
| 655 } |
| 656 """, """ |
| 657 function(c) { |
| 658 var __goto = 0, __completer = new newCompleter(), __handler = null, x, y, __er
ror1, __error2; |
| 659 function __helper(__result) { |
| 660 while (true) |
| 661 try { |
| 662 switch (__goto) { |
| 663 case 0: |
| 664 // Function start |
| 665 __handler = 1; |
| 666 __goto = c ? 4 : 6; |
| 667 break; |
| 668 case 4: |
| 669 // then |
| 670 __goto = 7; |
| 671 return thenHelper(foo(), __helper, __completer, function(__error) { |
| 672 __goto = 1; |
| 673 __helper(__error); |
| 674 }); |
| 675 case 7: |
| 676 // returning from await. |
| 677 // goto join |
| 678 __goto = 5; |
| 679 break; |
| 680 case 6: |
| 681 // else |
| 682 __result = foo(); |
| 683 case 5: |
| 684 // join |
| 685 x = __result; |
| 686 y = {}; |
| 687 __handler = null; |
| 688 __next = [3]; |
| 689 // goto after finally |
| 690 __goto = 3; |
| 691 break; |
| 692 case 1: |
| 693 // catch |
| 694 __handler = null; |
| 695 __error1 = __result; |
| 696 __handler = 8; |
| 697 __goto = c ? 11 : 13; |
| 698 break; |
| 699 case 11: |
| 700 // then |
| 701 __goto = 14; |
| 702 return thenHelper(fooError(__error1), __helper, __completer, functio
n(__error) { |
| 703 __goto = 8; |
| 704 __helper(__error); |
| 705 }); |
| 706 case 14: |
| 707 // returning from await. |
| 708 // goto join |
| 709 __goto = 12; |
| 710 break; |
| 711 case 13: |
| 712 // else |
| 713 __result = fooError(__error1); |
| 714 case 12: |
| 715 // join |
| 716 x = __result; |
| 717 __handler = null; |
| 718 __next = [10]; |
| 719 // goto finally |
| 720 __goto = 9; |
| 721 break; |
| 722 case 8: |
| 723 // catch |
| 724 __handler = null; |
| 725 __error2 = __result; |
| 726 y.x = foo(__error2); |
| 727 __next = [10]; |
| 728 case 9: |
| 729 // finally |
| 730 foo(x); |
| 731 // goto the next finally handler |
| 732 __goto = __next.pop(); |
| 733 break; |
| 734 case 10: |
| 735 // after finally |
| 736 case 3: |
| 737 // after finally |
| 738 // implicit return |
| 739 return thenHelper(null, null, __completer, null); |
| 740 } |
| 741 } catch (__error) { |
| 742 if (__handler === null) |
| 743 throw __error; |
| 744 __result = __error; |
| 745 __goto = __handler; |
| 746 } |
| 747 |
| 748 } |
| 749 return thenHelper(null, __helper, __completer, null); |
| 750 }"""); |
| 751 testTransform(""" |
| 752 function(x, y) async { |
| 753 print(await(foo(x))); // calls |
| 754 (await print)(foo(x)); |
| 755 print(foo(await x)); |
| 756 await (print(foo(await x))); |
| 757 print(foo(x, await y, z)); |
| 758 } |
| 759 """, """ |
| 760 function(x, y) { |
| 761 var __goto = 0, __completer = new newCompleter(), __temp1, __temp2, __temp3; |
| 762 function __helper(__result) { |
| 763 while (true) |
| 764 switch (__goto) { |
| 765 case 0: |
| 766 // Function start |
| 767 __temp1 = print; |
| 768 __goto = 1; |
| 769 return thenHelper(foo(x), __helper, __completer, null); |
| 770 case 1: |
| 771 // returning from await. |
| 772 __temp1(__result); |
| 773 __goto = 2; |
| 774 return thenHelper(print, __helper, __completer, null); |
| 775 case 2: |
| 776 // returning from await. |
| 777 __result(foo(x)); |
| 778 __temp1 = print; |
| 779 __temp2 = foo; |
| 780 __goto = 3; |
| 781 return thenHelper(x, __helper, __completer, null); |
| 782 case 3: |
| 783 // returning from await. |
| 784 __temp1(__temp2(__result)); |
| 785 __temp1 = print; |
| 786 __temp2 = foo; |
| 787 __goto = 5; |
| 788 return thenHelper(x, __helper, __completer, null); |
| 789 case 5: |
| 790 // returning from await. |
| 791 __goto = 4; |
| 792 return thenHelper(__temp1(__temp2(__result)), __helper, __completer, n
ull); |
| 793 case 4: |
| 794 // returning from await. |
| 795 __temp1 = print; |
| 796 __temp2 = foo; |
| 797 __temp3 = x; |
| 798 __goto = 6; |
| 799 return thenHelper(y, __helper, __completer, null); |
| 800 case 6: |
| 801 // returning from await. |
| 802 __temp1(__temp2(__temp3, __result, z)); |
| 803 // implicit return |
| 804 return thenHelper(null, null, __completer, null); |
| 805 } |
| 806 } |
| 807 return thenHelper(null, __helper, __completer, null); |
| 808 }"""); |
| 809 testTransform(""" |
| 810 function(x, y) async { |
| 811 while (await(foo())) { |
| 812 lab: { // labelled statement |
| 813 switch(y) { |
| 814 case 0: |
| 815 foo(); |
| 816 case 0: // Duplicate case |
| 817 print(await foo1(x)); |
| 818 return y; |
| 819 case await bar(): // await in case |
| 820 print(await foobar(x)); |
| 821 return y; |
| 822 case x: |
| 823 if (a) { |
| 824 throw new Error(); |
| 825 } else { |
| 826 continue; |
| 827 } |
| 828 default: // defaul case |
| 829 break lab; // break to label |
| 830 } |
| 831 foo(); |
| 832 } |
| 833 } |
| 834 }""", """ |
| 835 function(x, y) { |
| 836 var __goto = 0, __completer = new newCompleter(), __returnValue, __temp1; |
| 837 function __helper(__result) { |
| 838 while (true) |
| 839 switch (__goto) { |
| 840 case 0: |
| 841 // Function start |
| 842 case 2: |
| 843 // while condition |
| 844 __goto = 4; |
| 845 return thenHelper(foo(), __helper, __completer, null); |
| 846 case 4: |
| 847 // returning from await. |
| 848 if (!__result) { |
| 849 // goto after while |
| 850 __goto = 3; |
| 851 break; |
| 852 } |
| 853 case 6: |
| 854 // continue lab |
| 855 case 7: |
| 856 // switch |
| 857 __temp1 = y; |
| 858 if (__temp1 === 0) { |
| 859 // goto case |
| 860 __goto = 9; |
| 861 break; |
| 862 } |
| 863 if (__temp1 === 0) { |
| 864 // goto case |
| 865 __goto = 10; |
| 866 break; |
| 867 } |
| 868 __goto = 12; |
| 869 return thenHelper(bar(), __helper, __completer, null); |
| 870 case 12: |
| 871 // returning from await. |
| 872 if (__temp1 === __result) { |
| 873 // goto case |
| 874 __goto = 11; |
| 875 break; |
| 876 } |
| 877 if (__temp1 === x) { |
| 878 // goto case |
| 879 __goto = 13; |
| 880 break; |
| 881 } |
| 882 // goto default |
| 883 __goto = 14; |
| 884 break; |
| 885 case 9: |
| 886 // case |
| 887 foo(); |
| 888 case 10: |
| 889 // case |
| 890 __temp1 = print; |
| 891 __goto = 15; |
| 892 return thenHelper(foo1(x), __helper, __completer, null); |
| 893 case 15: |
| 894 // returning from await. |
| 895 __temp1(__result); |
| 896 __returnValue = y; |
| 897 // goto Return |
| 898 __goto = 1; |
| 899 break; |
| 900 case 11: |
| 901 // case |
| 902 __temp1 = print; |
| 903 __goto = 16; |
| 904 return thenHelper(foobar(x), __helper, __completer, null); |
| 905 case 16: |
| 906 // returning from await. |
| 907 __temp1(__result); |
| 908 __returnValue = y; |
| 909 // goto Return |
| 910 __goto = 1; |
| 911 break; |
| 912 case 13: |
| 913 // case |
| 914 if (a) { |
| 915 throw new Error(); |
| 916 } else { |
| 917 // goto while condition |
| 918 __goto = 2; |
| 919 break; |
| 920 } |
| 921 case 14: |
| 922 // default |
| 923 // goto break lab |
| 924 __goto = 5; |
| 925 break; |
| 926 case 8: |
| 927 // after switch |
| 928 foo(); |
| 929 case 5: |
| 930 // break lab |
| 931 // goto while condition |
| 932 __goto = 2; |
| 933 break; |
| 934 case 3: |
| 935 // after while |
| 936 case 1: |
| 937 // Return |
| 938 return thenHelper(__returnValue, null, __completer, null); |
| 939 } |
| 940 } |
| 941 return thenHelper(null, __helper, __completer, null); |
| 942 }"""); |
| 943 } |
OLD | NEW |