| OLD | NEW |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2016, 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 kernel.transformations.continuation; | 5 library kernel.transformations.continuation; |
| 6 | 6 |
| 7 import 'dart:math' as math; | 7 import 'dart:math' as math; |
| 8 | 8 |
| 9 import '../ast.dart'; | 9 import '../ast.dart'; |
| 10 import '../core_types.dart'; | 10 import '../core_types.dart'; |
| (...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 208 // sync* functions cannot return a value. | 208 // sync* functions cannot return a value. |
| 209 assert(node.expression == null || node.expression is NullLiteral); | 209 assert(node.expression == null || node.expression is NullLiteral); |
| 210 node.expression = new BoolLiteral(false)..parent = node; | 210 node.expression = new BoolLiteral(false)..parent = node; |
| 211 return node; | 211 return node; |
| 212 } | 212 } |
| 213 } | 213 } |
| 214 | 214 |
| 215 abstract class AsyncRewriterBase extends ContinuationRewriterBase { | 215 abstract class AsyncRewriterBase extends ContinuationRewriterBase { |
| 216 final VariableDeclaration nestedClosureVariable = | 216 final VariableDeclaration nestedClosureVariable = |
| 217 new VariableDeclaration(":async_op"); | 217 new VariableDeclaration(":async_op"); |
| 218 final VariableDeclaration stackTraceVariable = | |
| 219 new VariableDeclaration(":async_stack_trace"); | |
| 220 final VariableDeclaration thenContinuationVariable = | 218 final VariableDeclaration thenContinuationVariable = |
| 221 new VariableDeclaration(":async_op_then"); | 219 new VariableDeclaration(":async_op_then"); |
| 222 final VariableDeclaration catchErrorContinuationVariable = | 220 final VariableDeclaration catchErrorContinuationVariable = |
| 223 new VariableDeclaration(":async_op_error"); | 221 new VariableDeclaration(":async_op_error"); |
| 224 | 222 |
| 225 LabeledStatement labeledBody; | 223 LabeledStatement labeledBody; |
| 226 | 224 |
| 227 ExpressionLifter expressionRewriter; | 225 ExpressionLifter expressionRewriter; |
| 228 | 226 |
| 229 AsyncRewriterBase(helper, enclosingFunction) | 227 AsyncRewriterBase(helper, enclosingFunction) |
| 230 : super(helper, enclosingFunction) {} | 228 : super(helper, enclosingFunction) {} |
| 231 | 229 |
| 232 void setupAsyncContinuations(List<Statement> statements) { | 230 void setupAsyncContinuations(List<Statement> statements) { |
| 233 expressionRewriter = new ExpressionLifter(this); | 231 expressionRewriter = new ExpressionLifter(this); |
| 234 | 232 |
| 235 // var :async_stack_trace; | |
| 236 statements.add(stackTraceVariable); | |
| 237 | |
| 238 // var :async_op_then; | 233 // var :async_op_then; |
| 239 statements.add(thenContinuationVariable); | 234 statements.add(thenContinuationVariable); |
| 240 | 235 |
| 241 // var :async_op_error; | 236 // var :async_op_error; |
| 242 statements.add(catchErrorContinuationVariable); | 237 statements.add(catchErrorContinuationVariable); |
| 243 | 238 |
| 244 // :async_op([:result, :exception, :stack_trace]) { | 239 // :async_op([:result, :exception, :stack_trace]) { |
| 245 // modified <node.body>; | 240 // modified <node.body>; |
| 246 // } | 241 // } |
| 247 final parameters = <VariableDeclaration>[ | 242 final parameters = <VariableDeclaration>[ |
| (...skipping 19 matching lines...) Expand all Loading... |
| 267 // these. | 262 // these. |
| 268 statements.addAll(variableDeclarations()); | 263 statements.addAll(variableDeclarations()); |
| 269 statements.addAll(expressionRewriter.variables); | 264 statements.addAll(expressionRewriter.variables); |
| 270 | 265 |
| 271 // Now add the closure function itself. | 266 // Now add the closure function itself. |
| 272 final closureFunction = | 267 final closureFunction = |
| 273 new FunctionDeclaration(nestedClosureVariable, function) | 268 new FunctionDeclaration(nestedClosureVariable, function) |
| 274 ..fileOffset = enclosingFunction.parent.fileOffset; | 269 ..fileOffset = enclosingFunction.parent.fileOffset; |
| 275 statements.add(closureFunction); | 270 statements.add(closureFunction); |
| 276 | 271 |
| 277 // :async_stack_trace = _asyncStackTraceHelper(asyncBody); | |
| 278 final stackTrace = new StaticInvocation(helper.asyncStackTraceHelper, | |
| 279 new Arguments(<Expression>[new VariableGet(nestedClosureVariable)])); | |
| 280 final stackTraceAssign = new ExpressionStatement( | |
| 281 new VariableSet(stackTraceVariable, stackTrace)); | |
| 282 statements.add(stackTraceAssign); | |
| 283 | |
| 284 // :async_op_then = _asyncThenWrapperHelper(asyncBody); | 272 // :async_op_then = _asyncThenWrapperHelper(asyncBody); |
| 285 final boundThenClosure = new StaticInvocation(helper.asyncThenWrapper, | 273 final boundThenClosure = new StaticInvocation(helper.asyncThenWrapper, |
| 286 new Arguments(<Expression>[new VariableGet(nestedClosureVariable)])); | 274 new Arguments(<Expression>[new VariableGet(nestedClosureVariable)])); |
| 287 final thenClosureVariableAssign = new ExpressionStatement( | 275 final thenClosureVariableAssign = new ExpressionStatement( |
| 288 new VariableSet(thenContinuationVariable, boundThenClosure)); | 276 new VariableSet(thenContinuationVariable, boundThenClosure)); |
| 289 statements.add(thenClosureVariableAssign); | 277 statements.add(thenClosureVariableAssign); |
| 290 | 278 |
| 291 // :async_op_error = _asyncErrorWrapperHelper(asyncBody); | 279 // :async_op_error = _asyncErrorWrapperHelper(asyncBody); |
| 292 final boundCatchErrorClosure = new StaticInvocation( | 280 final boundCatchErrorClosure = new StaticInvocation( |
| 293 helper.asyncErrorWrapper, | 281 helper.asyncErrorWrapper, |
| (...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 910 final Class iteratorClass; | 898 final Class iteratorClass; |
| 911 final Class futureClass; | 899 final Class futureClass; |
| 912 final Class futureOrClass; | 900 final Class futureOrClass; |
| 913 final Class completerClass; | 901 final Class completerClass; |
| 914 final Procedure printProcedure; | 902 final Procedure printProcedure; |
| 915 final Procedure completerConstructor; | 903 final Procedure completerConstructor; |
| 916 final Procedure futureMicrotaskConstructor; | 904 final Procedure futureMicrotaskConstructor; |
| 917 final Constructor streamControllerConstructor; | 905 final Constructor streamControllerConstructor; |
| 918 final Constructor syncIterableConstructor; | 906 final Constructor syncIterableConstructor; |
| 919 final Constructor streamIteratorConstructor; | 907 final Constructor streamIteratorConstructor; |
| 920 final Procedure asyncStackTraceHelper; | |
| 921 final Procedure asyncThenWrapper; | 908 final Procedure asyncThenWrapper; |
| 922 final Procedure asyncErrorWrapper; | 909 final Procedure asyncErrorWrapper; |
| 923 final Procedure awaitHelper; | 910 final Procedure awaitHelper; |
| 924 final CoreTypes coreTypes; | 911 final CoreTypes coreTypes; |
| 925 | 912 |
| 926 HelperNodes( | 913 HelperNodes( |
| 927 this.asyncLibrary, | 914 this.asyncLibrary, |
| 928 this.coreLibrary, | 915 this.coreLibrary, |
| 929 this.iteratorClass, | 916 this.iteratorClass, |
| 930 this.futureClass, | 917 this.futureClass, |
| 931 this.futureOrClass, | 918 this.futureOrClass, |
| 932 this.completerClass, | 919 this.completerClass, |
| 933 this.printProcedure, | 920 this.printProcedure, |
| 934 this.completerConstructor, | 921 this.completerConstructor, |
| 935 this.syncIterableConstructor, | 922 this.syncIterableConstructor, |
| 936 this.streamIteratorConstructor, | 923 this.streamIteratorConstructor, |
| 937 this.futureMicrotaskConstructor, | 924 this.futureMicrotaskConstructor, |
| 938 this.streamControllerConstructor, | 925 this.streamControllerConstructor, |
| 939 this.asyncStackTraceHelper, | |
| 940 this.asyncThenWrapper, | 926 this.asyncThenWrapper, |
| 941 this.asyncErrorWrapper, | 927 this.asyncErrorWrapper, |
| 942 this.awaitHelper, | 928 this.awaitHelper, |
| 943 this.coreTypes); | 929 this.coreTypes); |
| 944 | 930 |
| 945 factory HelperNodes.fromCoreTypes(CoreTypes coreTypes) { | 931 factory HelperNodes.fromCoreTypes(CoreTypes coreTypes) { |
| 946 return new HelperNodes( | 932 return new HelperNodes( |
| 947 coreTypes.asyncLibrary, | 933 coreTypes.asyncLibrary, |
| 948 coreTypes.coreLibrary, | 934 coreTypes.coreLibrary, |
| 949 coreTypes.iteratorClass, | 935 coreTypes.iteratorClass, |
| 950 coreTypes.futureClass, | 936 coreTypes.futureClass, |
| 951 coreTypes.futureOrClass, | 937 coreTypes.futureOrClass, |
| 952 coreTypes.completerClass, | 938 coreTypes.completerClass, |
| 953 coreTypes.printProcedure, | 939 coreTypes.printProcedure, |
| 954 coreTypes.completerSyncConstructor, | 940 coreTypes.completerSyncConstructor, |
| 955 coreTypes.syncIterableDefaultConstructor, | 941 coreTypes.syncIterableDefaultConstructor, |
| 956 coreTypes.streamIteratorDefaultConstructor, | 942 coreTypes.streamIteratorDefaultConstructor, |
| 957 coreTypes.futureMicrotaskConstructor, | 943 coreTypes.futureMicrotaskConstructor, |
| 958 coreTypes.asyncStarStreamControllerDefaultConstructor, | 944 coreTypes.asyncStarStreamControllerDefaultConstructor, |
| 959 coreTypes.asyncStackTraceHelperProcedure, | |
| 960 coreTypes.asyncThenWrapperHelperProcedure, | 945 coreTypes.asyncThenWrapperHelperProcedure, |
| 961 coreTypes.asyncErrorWrapperHelperProcedure, | 946 coreTypes.asyncErrorWrapperHelperProcedure, |
| 962 coreTypes.awaitHelperProcedure, | 947 coreTypes.awaitHelperProcedure, |
| 963 coreTypes); | 948 coreTypes); |
| 964 } | 949 } |
| 965 } | 950 } |
| OLD | NEW |