Chromium Code Reviews| 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 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 139 | 139 |
| 140 // return new _SyncIterable(:sync_body); | 140 // return new _SyncIterable(:sync_body); |
| 141 final arguments = new Arguments([new VariableGet(nestedClosureVariable)]); | 141 final arguments = new Arguments([new VariableGet(nestedClosureVariable)]); |
| 142 final returnStatement = new ReturnStatement( | 142 final returnStatement = new ReturnStatement( |
| 143 new ConstructorInvocation(helper.syncIterableConstructor, arguments)); | 143 new ConstructorInvocation(helper.syncIterableConstructor, arguments)); |
| 144 | 144 |
| 145 enclosingFunction.body = new Block([] | 145 enclosingFunction.body = new Block([] |
| 146 ..addAll(variableDeclarations()) | 146 ..addAll(variableDeclarations()) |
| 147 ..addAll([closureFunction, returnStatement])); | 147 ..addAll([closureFunction, returnStatement])); |
| 148 enclosingFunction.body.parent = enclosingFunction; | 148 enclosingFunction.body.parent = enclosingFunction; |
| 149 enclosingFunction.originalAsyncMarker = enclosingFunction.asyncMarker; | |
| 149 enclosingFunction.asyncMarker = AsyncMarker.Sync; | 150 enclosingFunction.asyncMarker = AsyncMarker.Sync; |
| 150 return enclosingFunction; | 151 return enclosingFunction; |
| 151 } | 152 } |
| 152 | 153 |
| 153 Statement buildClosureBody() { | 154 Statement buildClosureBody() { |
| 154 // The body will insert calls to | 155 // The body will insert calls to |
| 155 // :iterator.current_= | 156 // :iterator.current_= |
| 156 // :iterator.isYieldEach= | 157 // :iterator.isYieldEach= |
| 157 // and return `true` as long as it did something and `false` when it's done. | 158 // and return `true` as long as it did something and `false` when it's done. |
| 158 return new Block(<Statement>[ | 159 return new Block(<Statement>[ |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 191 } | 192 } |
| 192 } | 193 } |
| 193 | 194 |
| 194 abstract class AsyncRewriterBase extends ContinuationRewriterBase { | 195 abstract class AsyncRewriterBase extends ContinuationRewriterBase { |
| 195 final VariableDeclaration nestedClosureVariable = | 196 final VariableDeclaration nestedClosureVariable = |
| 196 new VariableDeclaration(":async_op"); | 197 new VariableDeclaration(":async_op"); |
| 197 final VariableDeclaration thenContinuationVariable = | 198 final VariableDeclaration thenContinuationVariable = |
| 198 new VariableDeclaration(":async_op_then"); | 199 new VariableDeclaration(":async_op_then"); |
| 199 final VariableDeclaration catchErrorContinuationVariable = | 200 final VariableDeclaration catchErrorContinuationVariable = |
| 200 new VariableDeclaration(":async_op_error"); | 201 new VariableDeclaration(":async_op_error"); |
| 202 final VariableDeclaration asyncStackTrace = | |
| 203 new VariableDeclaration(":async_stack_trace"); | |
| 201 | 204 |
| 202 LabeledStatement labeledBody; | 205 LabeledStatement labeledBody; |
| 203 | 206 |
| 204 ExpressionLifter expressionRewriter; | 207 ExpressionLifter expressionRewriter; |
| 205 | 208 |
| 206 AsyncRewriterBase(helper, enclosingFunction) | 209 AsyncRewriterBase(helper, enclosingFunction) |
| 207 : super(helper, enclosingFunction) {} | 210 : super(helper, enclosingFunction) {} |
| 208 | 211 |
| 209 void setupAsyncContinuations(List<Statement> statements) { | 212 void setupAsyncContinuations(List<Statement> statements) { |
| 210 expressionRewriter = new ExpressionLifter(this); | 213 expressionRewriter = new ExpressionLifter(this); |
| 211 | 214 |
| 212 // var :async_op_then; | 215 // var :async_op_then; |
| 213 statements.add(thenContinuationVariable); | 216 statements.add(thenContinuationVariable); |
| 214 | 217 |
| 215 // var :async_op_error; | 218 // var :async_op_error; |
| 216 statements.add(catchErrorContinuationVariable); | 219 statements.add(catchErrorContinuationVariable); |
| 217 | 220 |
| 221 // var :async_stack_trace; | |
| 222 statements.add(asyncStackTrace); | |
| 223 | |
| 218 // :async_op([:result, :exception, :stack_trace]) { | 224 // :async_op([:result, :exception, :stack_trace]) { |
| 219 // modified <node.body>; | 225 // modified <node.body>; |
| 220 // } | 226 // } |
| 221 final parameters = <VariableDeclaration>[ | 227 final parameters = <VariableDeclaration>[ |
| 222 expressionRewriter.asyncResult, | 228 expressionRewriter.asyncResult, |
| 223 new VariableDeclaration(':exception'), | 229 new VariableDeclaration(':exception'), |
| 224 new VariableDeclaration(':stack_trace'), | 230 new VariableDeclaration(':stack_trace'), |
| 225 ]; | 231 ]; |
| 226 final function = new FunctionNode(buildWrappedBody(), | 232 final function = new FunctionNode(buildWrappedBody(), |
| 227 positionalParameters: parameters, | 233 positionalParameters: parameters, |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 251 statements.add(thenClosureVariableAssign); | 257 statements.add(thenClosureVariableAssign); |
| 252 | 258 |
| 253 // :async_op_error = _asyncErrorWrapperHelper(asyncBody); | 259 // :async_op_error = _asyncErrorWrapperHelper(asyncBody); |
| 254 final boundCatchErrorClosure = new StaticInvocation( | 260 final boundCatchErrorClosure = new StaticInvocation( |
| 255 helper.asyncErrorWrapper, | 261 helper.asyncErrorWrapper, |
| 256 new Arguments(<Expression>[new VariableGet(nestedClosureVariable)])); | 262 new Arguments(<Expression>[new VariableGet(nestedClosureVariable)])); |
| 257 final catchErrorClosureVariableAssign = new ExpressionStatement( | 263 final catchErrorClosureVariableAssign = new ExpressionStatement( |
| 258 new VariableSet( | 264 new VariableSet( |
| 259 catchErrorContinuationVariable, boundCatchErrorClosure)); | 265 catchErrorContinuationVariable, boundCatchErrorClosure)); |
| 260 statements.add(catchErrorClosureVariableAssign); | 266 statements.add(catchErrorClosureVariableAssign); |
| 267 | |
| 268 // :async_stack_trace = _asyncStackTraceHelper(); | |
| 269 final boundAsyncStackTrace = new StaticInvocation( | |
| 270 helper.asyncStackTraceHelper, new Arguments.empty()); | |
| 271 final asyncStackTraceVariableAssign = new ExpressionStatement( | |
| 272 new VariableSet(asyncStackTrace, boundAsyncStackTrace)); | |
| 273 statements.add(asyncStackTraceVariableAssign); | |
|
kustermann
2017/02/22 11:08:25
We could actually just create the variable here an
jensj
2017/02/23 09:40:26
Attempt made.
| |
| 261 } | 274 } |
| 262 | 275 |
| 263 Statement buildWrappedBody() { | 276 Statement buildWrappedBody() { |
| 264 ++currentTryDepth; | 277 ++currentTryDepth; |
| 265 labeledBody = new LabeledStatement(null); | 278 labeledBody = new LabeledStatement(null); |
| 266 labeledBody.body = visitDelimited(enclosingFunction.body) | 279 labeledBody.body = visitDelimited(enclosingFunction.body) |
| 267 ..parent = labeledBody; | 280 ..parent = labeledBody; |
| 268 --currentTryDepth; | 281 --currentTryDepth; |
| 269 | 282 |
| 270 var exceptionVariable = new VariableDeclaration(":exception"); | 283 var exceptionVariable = new VariableDeclaration(":exception"); |
| (...skipping 420 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 691 statements.add(setController); | 704 statements.add(setController); |
| 692 | 705 |
| 693 // return :controller.stream; | 706 // return :controller.stream; |
| 694 var completerGet = new VariableGet(controllerVariable); | 707 var completerGet = new VariableGet(controllerVariable); |
| 695 var returnStatement = new ReturnStatement( | 708 var returnStatement = new ReturnStatement( |
| 696 new PropertyGet(completerGet, new Name('stream', helper.asyncLibrary))); | 709 new PropertyGet(completerGet, new Name('stream', helper.asyncLibrary))); |
| 697 statements.add(returnStatement); | 710 statements.add(returnStatement); |
| 698 | 711 |
| 699 enclosingFunction.body = new Block(statements); | 712 enclosingFunction.body = new Block(statements); |
| 700 enclosingFunction.body.parent = enclosingFunction; | 713 enclosingFunction.body.parent = enclosingFunction; |
| 714 enclosingFunction.originalAsyncMarker = enclosingFunction.asyncMarker; | |
| 701 enclosingFunction.asyncMarker = AsyncMarker.Sync; | 715 enclosingFunction.asyncMarker = AsyncMarker.Sync; |
| 702 return enclosingFunction; | 716 return enclosingFunction; |
| 703 } | 717 } |
| 704 | 718 |
| 705 Statement buildWrappedBody() { | 719 Statement buildWrappedBody() { |
| 706 ++currentTryDepth; | 720 ++currentTryDepth; |
| 707 Statement body = super.buildWrappedBody(); | 721 Statement body = super.buildWrappedBody(); |
| 708 --currentTryDepth; | 722 --currentTryDepth; |
| 709 | 723 |
| 710 var finallyBody = new ExpressionStatement(new MethodInvocation( | 724 var finallyBody = new ExpressionStatement(new MethodInvocation( |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 813 statements.add(newMicrotaskStatement); | 827 statements.add(newMicrotaskStatement); |
| 814 | 828 |
| 815 // return :completer.future; | 829 // return :completer.future; |
| 816 var completerGet = new VariableGet(completerVariable); | 830 var completerGet = new VariableGet(completerVariable); |
| 817 var returnStatement = new ReturnStatement( | 831 var returnStatement = new ReturnStatement( |
| 818 new PropertyGet(completerGet, new Name('future', helper.asyncLibrary))); | 832 new PropertyGet(completerGet, new Name('future', helper.asyncLibrary))); |
| 819 statements.add(returnStatement); | 833 statements.add(returnStatement); |
| 820 | 834 |
| 821 enclosingFunction.body = new Block(statements); | 835 enclosingFunction.body = new Block(statements); |
| 822 enclosingFunction.body.parent = enclosingFunction; | 836 enclosingFunction.body.parent = enclosingFunction; |
| 837 enclosingFunction.originalAsyncMarker = enclosingFunction.asyncMarker; | |
| 823 enclosingFunction.asyncMarker = AsyncMarker.Sync; | 838 enclosingFunction.asyncMarker = AsyncMarker.Sync; |
| 824 enclosingFunction.debuggable = false; | |
| 825 return enclosingFunction; | 839 return enclosingFunction; |
| 826 } | 840 } |
| 827 | 841 |
| 828 Statement buildCatchBody(exceptionVariable, stackTraceVariable) { | 842 Statement buildCatchBody(exceptionVariable, stackTraceVariable) { |
| 829 return new ExpressionStatement(new MethodInvocation( | 843 return new ExpressionStatement(new MethodInvocation( |
| 830 new VariableGet(completerVariable), | 844 new VariableGet(completerVariable), |
| 831 new Name("completeError", helper.asyncLibrary), | 845 new Name("completeError", helper.asyncLibrary), |
| 832 new Arguments([ | 846 new Arguments([ |
| 833 new VariableGet(exceptionVariable), | 847 new VariableGet(exceptionVariable), |
| 834 new VariableGet(stackTraceVariable) | 848 new VariableGet(stackTraceVariable) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 867 final Class futureClass; | 881 final Class futureClass; |
| 868 final Class completerClass; | 882 final Class completerClass; |
| 869 final Procedure printProcedure; | 883 final Procedure printProcedure; |
| 870 final Procedure completerConstructor; | 884 final Procedure completerConstructor; |
| 871 final Procedure futureMicrotaskConstructor; | 885 final Procedure futureMicrotaskConstructor; |
| 872 final Constructor streamControllerConstructor; | 886 final Constructor streamControllerConstructor; |
| 873 final Constructor syncIterableConstructor; | 887 final Constructor syncIterableConstructor; |
| 874 final Constructor streamIteratorConstructor; | 888 final Constructor streamIteratorConstructor; |
| 875 final Procedure asyncThenWrapper; | 889 final Procedure asyncThenWrapper; |
| 876 final Procedure asyncErrorWrapper; | 890 final Procedure asyncErrorWrapper; |
| 891 final Procedure asyncStackTraceHelper; | |
| 877 final Procedure awaitHelper; | 892 final Procedure awaitHelper; |
| 878 final CoreTypes coreTypes; | 893 final CoreTypes coreTypes; |
| 879 | 894 |
| 880 HelperNodes( | 895 HelperNodes( |
| 881 this.asyncLibrary, | 896 this.asyncLibrary, |
| 882 this.coreLibrary, | 897 this.coreLibrary, |
| 883 this.iteratorClass, | 898 this.iteratorClass, |
| 884 this.futureClass, | 899 this.futureClass, |
| 885 this.completerClass, | 900 this.completerClass, |
| 886 this.printProcedure, | 901 this.printProcedure, |
| 887 this.completerConstructor, | 902 this.completerConstructor, |
| 888 this.syncIterableConstructor, | 903 this.syncIterableConstructor, |
| 889 this.streamIteratorConstructor, | 904 this.streamIteratorConstructor, |
| 890 this.futureMicrotaskConstructor, | 905 this.futureMicrotaskConstructor, |
| 891 this.streamControllerConstructor, | 906 this.streamControllerConstructor, |
| 892 this.asyncThenWrapper, | 907 this.asyncThenWrapper, |
| 893 this.asyncErrorWrapper, | 908 this.asyncErrorWrapper, |
| 909 this.asyncStackTraceHelper, | |
| 894 this.awaitHelper, | 910 this.awaitHelper, |
| 895 this.coreTypes); | 911 this.coreTypes); |
| 896 | 912 |
| 897 factory HelperNodes.fromProgram(Program program) { | 913 factory HelperNodes.fromProgram(Program program) { |
| 898 Library findLibrary(String name) { | 914 Library findLibrary(String name) { |
| 899 Uri uri = Uri.parse(name); | 915 Uri uri = Uri.parse(name); |
| 900 for (var library in program.libraries) { | 916 for (var library in program.libraries) { |
| 901 if (library.importUri == uri) return library; | 917 if (library.importUri == uri) return library; |
| 902 } | 918 } |
| 903 throw 'Library "$name" not found'; | 919 throw 'Library "$name" not found'; |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 966 futureClass, | 982 futureClass, |
| 967 completerClass, | 983 completerClass, |
| 968 findProcedure(coreLibrary, 'print'), | 984 findProcedure(coreLibrary, 'print'), |
| 969 findFactoryConstructor(completerClass, 'sync'), | 985 findFactoryConstructor(completerClass, 'sync'), |
| 970 findConstructor(syncIterableClass, ''), | 986 findConstructor(syncIterableClass, ''), |
| 971 findConstructor(streamIteratorClass, ''), | 987 findConstructor(streamIteratorClass, ''), |
| 972 findFactoryConstructor(futureClass, 'microtask'), | 988 findFactoryConstructor(futureClass, 'microtask'), |
| 973 findConstructor(streamControllerClass, ''), | 989 findConstructor(streamControllerClass, ''), |
| 974 findProcedure(asyncLibrary, '_asyncThenWrapperHelper'), | 990 findProcedure(asyncLibrary, '_asyncThenWrapperHelper'), |
| 975 findProcedure(asyncLibrary, '_asyncErrorWrapperHelper'), | 991 findProcedure(asyncLibrary, '_asyncErrorWrapperHelper'), |
| 992 findProcedure(asyncLibrary, '_asyncStackTraceHelper'), | |
| 976 findProcedure(asyncLibrary, '_awaitHelper'), | 993 findProcedure(asyncLibrary, '_awaitHelper'), |
| 977 new CoreTypes(program)); | 994 new CoreTypes(program)); |
| 978 } | 995 } |
| 979 } | 996 } |
| OLD | NEW |