| Index: tests/compiler/dart2js/async_await_js_transform_test.dart
|
| diff --git a/tests/compiler/dart2js/async_await_js_transform_test.dart b/tests/compiler/dart2js/async_await_js_transform_test.dart
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..2b2a619c96b8e6420baae8f020c72745c91ce986
|
| --- /dev/null
|
| +++ b/tests/compiler/dart2js/async_await_js_transform_test.dart
|
| @@ -0,0 +1,331 @@
|
| +// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
|
| +// for details. All rights reserved. Use of this source code is governed by a
|
| +// BSD-style license that can be found in the LICENSE file.
|
| +
|
| +import "package:expect/expect.dart";
|
| +import "package:compiler/src/js/js.dart";
|
| +import "package:compiler/src/js/rewrite_async.dart";
|
| +
|
| +import "backend_dart/dart_printer_test.dart" show PrintDiagnosticListener;
|
| +
|
| +void testTransform(String source, String expected) {
|
| + Fun fun = js(source);
|
| + Fun rewritten = new AsyncRewriter().rewrite(fun);
|
| + Printer printer = new Printer(new PrintDiagnosticListener(), null);
|
| + printer.visit(rewritten);
|
| + Expect.stringEquals(expected, printer.outBuffer.getText());
|
| +}
|
| +
|
| +main() {
|
| + testTransform("""
|
| +function(c) async {
|
| + try {
|
| + var x = c ? await foo() : foo();
|
| + var y = {};
|
| + } catch (error) {
|
| + try {
|
| + x = c ? await fooError(error) : fooError(error);
|
| + } catch (error) {
|
| + y.x = foo(error);
|
| + } finally {
|
| + foo(x);
|
| + }
|
| + }
|
| +}
|
| +""", """
|
| +function(c) {
|
| + var __goto = 0, __handler = null, x, y, __error, __error1;
|
| + function __helper(__result) {
|
| + while (true)
|
| + try {
|
| + switch (__goto) {
|
| + case 0:
|
| + // Function start
|
| + __handler = 1;
|
| + __goto = c ? 3 : 5;
|
| + break;
|
| + case 3:
|
| + // then
|
| + __goto = 6;
|
| + return foo().then(__helper, function(__error) {
|
| + __goto = 1;
|
| + __helper(__error);
|
| + });
|
| + case 6:
|
| + // Returning from await.
|
| + __result = __result;
|
| + // goto join
|
| + __goto = 4;
|
| + break;
|
| + case 5:
|
| + // else
|
| + __result = foo();
|
| + case 4:
|
| + // join
|
| + x = __result;
|
| + y = {};
|
| + __handler = null;
|
| + // goto finally
|
| + __goto = 2;
|
| + break;
|
| + case 1:
|
| + // catch
|
| + __error = __result;
|
| + __handler = 7;
|
| + __goto = c ? 9 : 11;
|
| + break;
|
| + case 9:
|
| + // then
|
| + __goto = 12;
|
| + return fooError(__error).then(__helper, function(__error) {
|
| + __goto = 7;
|
| + __helper(__error);
|
| + });
|
| + case 12:
|
| + // Returning from await.
|
| + __result = __result;
|
| + // goto join
|
| + __goto = 10;
|
| + break;
|
| + case 11:
|
| + // else
|
| + __result = fooError(__error);
|
| + case 10:
|
| + // join
|
| + x = __result;
|
| + __handler = null;
|
| + // goto finally
|
| + __goto = 8;
|
| + break;
|
| + case 7:
|
| + // catch
|
| + __error1 = __result;
|
| + y.x = foo(__error1);
|
| + case 8:
|
| + // finally
|
| + foo(x);
|
| + case 2:
|
| + // finally
|
| + }
|
| + } catch (__error) {
|
| + if (__handler === null)
|
| + throw __error;
|
| + __result = __error;
|
| + __goto = __handler;
|
| + }
|
| +
|
| + }
|
| + return new Future.microtask(__helper);
|
| +}""");
|
| + testTransform("""
|
| +function(x, y) async {
|
| + print(await(foo(x)));
|
| + (await print)(foo(x));
|
| + print(foo(await x));
|
| + await (print(foo(await x)));
|
| + print(foo(x, await y, z));
|
| +}
|
| +""", """
|
| +function(x, y) {
|
| + var __goto = 0, __handler = null, __temp1, __temp2, __temp3;
|
| + function __helper(__result) {
|
| + while (true)
|
| + switch (__goto) {
|
| + case 0:
|
| + // Function start
|
| + __temp1 = print;
|
| + __goto = 1;
|
| + return foo(x).then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 1:
|
| + // Returning from await.
|
| + __temp1(__result);
|
| + __goto = 2;
|
| + return print.then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 2:
|
| + // Returning from await.
|
| + __result(foo(x));
|
| + __temp1 = print;
|
| + __temp2 = foo;
|
| + __goto = 3;
|
| + return x.then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 3:
|
| + // Returning from await.
|
| + __temp1(__temp2(__result));
|
| + __temp1 = print;
|
| + __temp2 = foo;
|
| + __goto = 5;
|
| + return x.then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 5:
|
| + // Returning from await.
|
| + __goto = 4;
|
| + return __temp1(__temp2(__result)).then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 4:
|
| + // Returning from await.
|
| + __result;
|
| + __temp1 = print;
|
| + __temp2 = foo;
|
| + __temp3 = x;
|
| + __goto = 6;
|
| + return y.then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 6:
|
| + // Returning from await.
|
| + __temp1(__temp2(__temp3, __result, z));
|
| + }
|
| + }
|
| + return new Future.microtask(__helper);
|
| +}""");
|
| + testTransform("""
|
| +function(x, y) async {
|
| + while (await(foo())) {
|
| + lab: {
|
| + switch(y) {
|
| + case 0:
|
| + foo();
|
| + case 1:
|
| + print(await foo1(x));
|
| + return y;
|
| + case await bar():
|
| + print(await foobar(x));
|
| + return y;
|
| + case x:
|
| + if (a) {
|
| + throw new Error();
|
| + } else {
|
| + continue;
|
| + }
|
| + default:
|
| + break lab;
|
| + }
|
| + foo();
|
| + }
|
| + }
|
| +}""", """
|
| +function(x, y) {
|
| + var __goto = 0, __handler = null, __temp1;
|
| + function __helper(__result) {
|
| + while (true)
|
| + switch (__goto) {
|
| + case 0:
|
| + // Function start
|
| + case 1:
|
| + // while condition
|
| + __goto = 4;
|
| + return foo().then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 4:
|
| + // Returning from await.
|
| + __goto = __result ? 2 : 3;
|
| + break;
|
| + case 2:
|
| + // while body
|
| + case 6:
|
| + // continue lab
|
| + case 7:
|
| + // switch
|
| + __temp1 = y;
|
| + if (__temp1 === 0) {
|
| + // goto case
|
| + __goto = 9;
|
| + break;
|
| + }
|
| + if (__temp1 === 1) {
|
| + // goto case
|
| + __goto = 10;
|
| + break;
|
| + }
|
| + __goto = 12;
|
| + return bar().then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 12:
|
| + // Returning from await.
|
| + if (__temp1 === __result) {
|
| + // goto case
|
| + __goto = 11;
|
| + break;
|
| + }
|
| + if (__temp1 === x) {
|
| + // goto case
|
| + __goto = 13;
|
| + break;
|
| + }
|
| + // goto default
|
| + __goto = 14;
|
| + break;
|
| + case 9:
|
| + // case
|
| + foo();
|
| + case 10:
|
| + // case
|
| + __temp1 = print;
|
| + __goto = 15;
|
| + return foo1(x).then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 15:
|
| + // Returning from await.
|
| + __temp1(__result);
|
| + return y;
|
| + case 11:
|
| + // case
|
| + __temp1 = print;
|
| + __goto = 16;
|
| + return foobar(x).then(__helper, function(__error) {
|
| + __goto = null;
|
| + __helper(__error);
|
| + });
|
| + case 16:
|
| + // Returning from await.
|
| + __temp1(__result);
|
| + return y;
|
| + case 13:
|
| + // case
|
| + if (a) {
|
| + throw new Error();
|
| + } else {
|
| + // goto while condition
|
| + __goto = 1;
|
| + break;
|
| + }
|
| + case 14:
|
| + // default
|
| + // goto break lab
|
| + __goto = 5;
|
| + break;
|
| + case 8:
|
| + // after switch
|
| + foo();
|
| + case 5:
|
| + // break lab
|
| + // goto while condition
|
| + __goto = 1;
|
| + break;
|
| + case 3:
|
| + // after while
|
| + }
|
| + }
|
| + return new Future.microtask(__helper);
|
| +}""");
|
| +}
|
|
|