OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library rewrite_async; | 5 library rewrite_async; |
6 | 6 |
7 import "dart:math" show max; | 7 import "dart:math" show max; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 | 9 |
10 import 'package:_internal/compiler/js_lib/shared/async_await_error_codes.dart' | 10 import 'package:_internal/compiler/js_lib/shared/async_await_error_codes.dart' |
(...skipping 1653 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1664 /// - The error to complete the completer with. | 1664 /// - The error to complete the completer with. |
1665 /// - [error_codes.ERROR] | 1665 /// - [error_codes.ERROR] |
1666 /// - The completer object [completer] | 1666 /// - The completer object [completer] |
1667 final js.Expression asyncHelper; | 1667 final js.Expression asyncHelper; |
1668 | 1668 |
1669 /// Contructor used to initialize the [completer] variable. | 1669 /// Contructor used to initialize the [completer] variable. |
1670 /// | 1670 /// |
1671 /// Specific to async methods. | 1671 /// Specific to async methods. |
1672 final js.Expression newCompleter; | 1672 final js.Expression newCompleter; |
1673 | 1673 |
| 1674 final js.Expression wrapBody; |
1674 | 1675 |
1675 AsyncRewriter(DiagnosticListener diagnosticListener, | 1676 AsyncRewriter(DiagnosticListener diagnosticListener, |
1676 spannable, | 1677 spannable, |
1677 {this.asyncHelper, | 1678 {this.asyncHelper, |
1678 this.newCompleter, | 1679 this.newCompleter, |
| 1680 this.wrapBody, |
1679 String safeVariableName(String proposedName), | 1681 String safeVariableName(String proposedName), |
1680 String bodyName}) | 1682 String bodyName}) |
1681 : super(diagnosticListener, | 1683 : super(diagnosticListener, |
1682 spannable, | 1684 spannable, |
1683 safeVariableName, | 1685 safeVariableName, |
1684 bodyName); | 1686 bodyName); |
1685 | 1687 |
1686 @override | 1688 @override |
1687 void addYield(js.DartYield node, js.Expression expression) { | 1689 void addYield(js.DartYield node, js.Expression expression) { |
1688 diagnosticListener.internalError(spannable, | 1690 diagnosticListener.internalError(spannable, |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1749 "completer": completer}); | 1751 "completer": completer}); |
1750 } | 1752 } |
1751 | 1753 |
1752 @override | 1754 @override |
1753 js.Fun finishFunction(List<js.Parameter> parameters, | 1755 js.Fun finishFunction(List<js.Parameter> parameters, |
1754 js.Statement rewrittenBody, | 1756 js.Statement rewrittenBody, |
1755 js.VariableDeclarationList variableDeclarations) { | 1757 js.VariableDeclarationList variableDeclarations) { |
1756 return js.js(""" | 1758 return js.js(""" |
1757 function (#parameters) { | 1759 function (#parameters) { |
1758 #variableDeclarations; | 1760 #variableDeclarations; |
1759 function #bodyName(#errorCode, #result) { | 1761 var #bodyName = #wrapBody(function (#errorCode, #result) { |
1760 if (#errorCode === #ERROR) { | 1762 if (#errorCode === #ERROR) { |
1761 #currentError = #result; | 1763 #currentError = #result; |
1762 #goto = #handler; | 1764 #goto = #handler; |
1763 } | 1765 } |
1764 #rewrittenBody; | 1766 #rewrittenBody; |
1765 } | 1767 }); |
1766 return #asyncHelper(null, #bodyName, #completer, null); | 1768 return #asyncHelper(null, #bodyName, #completer, null); |
1767 }""", { | 1769 }""", { |
1768 "parameters": parameters, | 1770 "parameters": parameters, |
1769 "variableDeclarations": variableDeclarations, | 1771 "variableDeclarations": variableDeclarations, |
1770 "ERROR": js.number(error_codes.ERROR), | 1772 "ERROR": js.number(error_codes.ERROR), |
1771 "rewrittenBody": rewrittenBody, | 1773 "rewrittenBody": rewrittenBody, |
1772 "bodyName": bodyName, | 1774 "bodyName": bodyName, |
1773 "currentError": currentError, | 1775 "currentError": currentError, |
1774 "goto": goto, | 1776 "goto": goto, |
1775 "handler": handler, | 1777 "handler": handler, |
1776 "errorCode": errorCodeName, | 1778 "errorCode": errorCodeName, |
1777 "result": resultName, | 1779 "result": resultName, |
1778 "asyncHelper": asyncHelper, | 1780 "asyncHelper": asyncHelper, |
1779 "completer": completer, | 1781 "completer": completer, |
| 1782 "wrapBody": wrapBody, |
1780 }); | 1783 }); |
1781 } | 1784 } |
1782 } | 1785 } |
1783 | 1786 |
1784 class SyncStarRewriter extends AsyncRewriterBase { | 1787 class SyncStarRewriter extends AsyncRewriterBase { |
1785 | 1788 |
1786 bool get isSyncStar => true; | 1789 bool get isSyncStar => true; |
1787 | 1790 |
1788 /// Contructor creating the Iterable for a sync* method. Called with | 1791 /// Contructor creating the Iterable for a sync* method. Called with |
1789 /// [bodyName]. | 1792 /// [bodyName]. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1964 /// A JS Expression that creates a marker indicating a 'yield' statement. | 1967 /// A JS Expression that creates a marker indicating a 'yield' statement. |
1965 /// | 1968 /// |
1966 /// Called with the value to yield. | 1969 /// Called with the value to yield. |
1967 final js.Expression yieldExpression; | 1970 final js.Expression yieldExpression; |
1968 | 1971 |
1969 /// A JS Expression that creates a marker indication a 'yield*' statement. | 1972 /// A JS Expression that creates a marker indication a 'yield*' statement. |
1970 /// | 1973 /// |
1971 /// Called with the stream to yield from. | 1974 /// Called with the stream to yield from. |
1972 final js.Expression yieldStarExpression; | 1975 final js.Expression yieldStarExpression; |
1973 | 1976 |
| 1977 final js.Expression wrapBody; |
| 1978 |
1974 AsyncStarRewriter(DiagnosticListener diagnosticListener, | 1979 AsyncStarRewriter(DiagnosticListener diagnosticListener, |
1975 spannable, | 1980 spannable, |
1976 {this.asyncStarHelper, | 1981 {this.asyncStarHelper, |
1977 this.streamOfController, | 1982 this.streamOfController, |
1978 this.newController, | 1983 this.newController, |
1979 this.yieldExpression, | 1984 this.yieldExpression, |
1980 this.yieldStarExpression, | 1985 this.yieldStarExpression, |
| 1986 this.wrapBody, |
1981 String safeVariableName(String proposedName), | 1987 String safeVariableName(String proposedName), |
1982 String bodyName}) | 1988 String bodyName}) |
1983 : super(diagnosticListener, | 1989 : super(diagnosticListener, |
1984 spannable, | 1990 spannable, |
1985 safeVariableName, | 1991 safeVariableName, |
1986 bodyName); | 1992 bodyName); |
1987 | 1993 |
1988 | 1994 |
1989 /// Translates a yield/yield* in an async* function. | 1995 /// Translates a yield/yield* in an async* function. |
1990 /// | 1996 /// |
(...skipping 25 matching lines...) Expand all Loading... |
2016 "controller": controllerName, | 2022 "controller": controllerName, |
2017 })); | 2023 })); |
2018 } | 2024 } |
2019 | 2025 |
2020 @override | 2026 @override |
2021 js.Fun finishFunction(List<js.Parameter> parameters, | 2027 js.Fun finishFunction(List<js.Parameter> parameters, |
2022 js.Statement rewrittenBody, | 2028 js.Statement rewrittenBody, |
2023 js.VariableDeclarationList variableDeclarations) { | 2029 js.VariableDeclarationList variableDeclarations) { |
2024 return js.js(""" | 2030 return js.js(""" |
2025 function (#parameters) { | 2031 function (#parameters) { |
2026 #variableDeclarations; | 2032 var #bodyName = #wrapBody(function (#errorCode, #result) { |
2027 function #bodyName(#errorCode, #result) { | |
2028 if (#hasYield) { | 2033 if (#hasYield) { |
2029 switch (#errorCode) { | 2034 switch (#errorCode) { |
2030 case #STREAM_WAS_CANCELED: | 2035 case #STREAM_WAS_CANCELED: |
2031 #next = #nextWhenCanceled; | 2036 #next = #nextWhenCanceled; |
2032 #goto = #next.pop(); | 2037 #goto = #next.pop(); |
2033 break; | 2038 break; |
2034 case #ERROR: | 2039 case #ERROR: |
2035 #currentError = #result; | 2040 #currentError = #result; |
2036 #goto = #handler; | 2041 #goto = #handler; |
2037 } | 2042 } |
2038 } else { | 2043 } else { |
2039 if (#errorCode === #ERROR) { | 2044 if (#errorCode === #ERROR) { |
2040 #currentError = #result; | 2045 #currentError = #result; |
2041 #goto = #handler; | 2046 #goto = #handler; |
2042 } | 2047 } |
2043 } | 2048 } |
2044 #rewrittenBody; | 2049 #rewrittenBody; |
2045 } | 2050 }); |
| 2051 #variableDeclarations; |
2046 return #streamOfController(#controller); | 2052 return #streamOfController(#controller); |
2047 }""", { | 2053 }""", { |
2048 "parameters": parameters, | 2054 "parameters": parameters, |
2049 "variableDeclarations": variableDeclarations, | 2055 "variableDeclarations": variableDeclarations, |
2050 "STREAM_WAS_CANCELED": js.number(error_codes.STREAM_WAS_CANCELED), | 2056 "STREAM_WAS_CANCELED": js.number(error_codes.STREAM_WAS_CANCELED), |
2051 "ERROR": js.number(error_codes.ERROR), | 2057 "ERROR": js.number(error_codes.ERROR), |
2052 "hasYield": analysis.hasYield, | 2058 "hasYield": analysis.hasYield, |
2053 "rewrittenBody": rewrittenBody, | 2059 "rewrittenBody": rewrittenBody, |
2054 "bodyName": bodyName, | 2060 "bodyName": bodyName, |
2055 "currentError": currentError, | 2061 "currentError": currentError, |
2056 "goto": goto, | 2062 "goto": goto, |
2057 "handler": handler, | 2063 "handler": handler, |
2058 "next": next, | 2064 "next": next, |
2059 "nextWhenCanceled": nextWhenCanceled, | 2065 "nextWhenCanceled": nextWhenCanceled, |
2060 "errorCode": errorCodeName, | 2066 "errorCode": errorCodeName, |
2061 "result": resultName, | 2067 "result": resultName, |
2062 "streamOfController": streamOfController, | 2068 "streamOfController": streamOfController, |
2063 "controller": controllerName, | 2069 "controller": controllerName, |
| 2070 "wrapBody": wrapBody, |
2064 }); | 2071 }); |
2065 } | 2072 } |
2066 | 2073 |
2067 @override | 2074 @override |
2068 void addErrorExit() { | 2075 void addErrorExit() { |
2069 beginLabel(rethrowLabel); | 2076 beginLabel(rethrowLabel); |
2070 addStatement(js.js.statement( | 2077 addStatement(js.js.statement( |
2071 "return #asyncHelper(#currentError, #errorCode, #controller);", { | 2078 "return #asyncHelper(#currentError, #errorCode, #controller);", { |
2072 "asyncHelper": asyncStarHelper, | 2079 "asyncHelper": asyncStarHelper, |
2073 "errorCode": js.number(error_codes.ERROR), | 2080 "errorCode": js.number(error_codes.ERROR), |
(...skipping 12 matching lines...) Expand all Loading... |
2086 "streamHelper": asyncStarHelper, | 2093 "streamHelper": asyncStarHelper, |
2087 "successCode": js.number(error_codes.SUCCESS), | 2094 "successCode": js.number(error_codes.SUCCESS), |
2088 "controller": controllerName})); | 2095 "controller": controllerName})); |
2089 } | 2096 } |
2090 | 2097 |
2091 @override | 2098 @override |
2092 Iterable<js.VariableInitialization> variableInitializations() { | 2099 Iterable<js.VariableInitialization> variableInitializations() { |
2093 List<js.VariableInitialization> variables = | 2100 List<js.VariableInitialization> variables = |
2094 new List<js.VariableInitialization>(); | 2101 new List<js.VariableInitialization>(); |
2095 variables.add(_makeVariableInitializer(controller, | 2102 variables.add(_makeVariableInitializer(controller, |
2096 js.js('#(#)', [newController, bodyName]))); | 2103 js.js('#(#)', |
| 2104 [newController, bodyName]))); |
2097 if (analysis.hasYield) { | 2105 if (analysis.hasYield) { |
2098 variables.add(_makeVariableInitializer(nextWhenCanceled, null)); | 2106 variables.add(_makeVariableInitializer(nextWhenCanceled, null)); |
2099 } | 2107 } |
2100 return variables; | 2108 return variables; |
2101 } | 2109 } |
2102 | 2110 |
2103 @override | 2111 @override |
2104 void initializeNames() { | 2112 void initializeNames() { |
2105 controllerName = freshName("controller"); | 2113 controllerName = freshName("controller"); |
2106 nextWhenCanceledName = freshName("nextWhenCanceled"); | 2114 nextWhenCanceledName = freshName("nextWhenCanceled"); |
(...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2542 return condition || body; | 2550 return condition || body; |
2543 } | 2551 } |
2544 | 2552 |
2545 @override | 2553 @override |
2546 bool visitDartYield(js.DartYield node) { | 2554 bool visitDartYield(js.DartYield node) { |
2547 hasYield = true; | 2555 hasYield = true; |
2548 visit(node.expression); | 2556 visit(node.expression); |
2549 return true; | 2557 return true; |
2550 } | 2558 } |
2551 } | 2559 } |
OLD | NEW |