Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 library kernel.interpreter; | 4 library kernel.interpreter; |
| 5 | 5 |
| 6 import '../ast.dart'; | 6 import '../ast.dart'; |
| 7 import '../ast.dart' as ast show Class; | 7 import '../ast.dart' as ast show Class; |
| 8 | 8 |
| 9 import '../log.dart'; | 9 import '../log.dart'; |
| 10 export '../log.dart'; | 10 export '../log.dart'; |
| (...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 704 this.constructor, this.location, this.environment, this.continuation) | 704 this.constructor, this.location, this.environment, this.continuation) |
| 705 : _currentClass = new Class(constructor.enclosingClass.reference); | 705 : _currentClass = new Class(constructor.enclosingClass.reference); |
| 706 | 706 |
| 707 Configuration call(List<InterpreterValue> fieldValues) { | 707 Configuration call(List<InterpreterValue> fieldValues) { |
| 708 for (FieldInitializerValue f in fieldValues) { | 708 for (FieldInitializerValue f in fieldValues) { |
| 709 // Directly set the field with the corresponding implicit setter. | 709 // Directly set the field with the corresponding implicit setter. |
| 710 _currentClass.implicitSetters[f.field.name](location.value, f.value); | 710 _currentClass.implicitSetters[f.field.name](location.value, f.value); |
| 711 } | 711 } |
| 712 | 712 |
| 713 if (constructor.initializers.length == 0) { | 713 if (constructor.initializers.length == 0) { |
| 714 // This can happen when initializing fields of Ctr with empty initializer | |
|
Dmitry Stefantsov
2017/08/03 08:34:08
One of the following substitutions would be helpfu
zhivkag
2017/08/03 08:46:52
Done.
| |
| 715 // list. | |
| 714 return new ForwardConfiguration(continuation, environment); | 716 return new ForwardConfiguration(continuation, environment); |
| 715 } | 717 } |
| 716 | 718 |
| 717 if (constructor.initializers.first is SuperInitializer) { | 719 if (constructor.initializers.first is SuperInitializer) { |
| 718 // Target constructor is from the superclass `object`. | 720 // Target constructor is from the superclass `object`. |
| 719 if (_currentClass.superclass.superclass == null) { | 721 if (_currentClass.superclass.superclass == null) { |
| 720 // TODO(zhivkag): Execute the constructor when support for | 722 // TODO(zhivkag): Execute the constructor when support for |
| 721 // native/external functions is added. | 723 // native/external functions is added. |
| 722 _initializeNullFields(_currentClass, location.value); | 724 _initializeNullFields(_currentClass, location.value); |
| 723 return new ForwardConfiguration(continuation, environment); | 725 return new ForwardConfiguration(continuation, environment); |
| (...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1029 /// instance with running the constructor. | 1031 /// instance with running the constructor. |
| 1030 class InitializationEK extends ExpressionContinuation { | 1032 class InitializationEK extends ExpressionContinuation { |
| 1031 final Constructor constructor; | 1033 final Constructor constructor; |
| 1032 final Environment environment; | 1034 final Environment environment; |
| 1033 // TODO(zhivkag): Add components for exception handling support | 1035 // TODO(zhivkag): Add components for exception handling support |
| 1034 final StatementContinuation continuation; | 1036 final StatementContinuation continuation; |
| 1035 | 1037 |
| 1036 InitializationEK(this.constructor, this.environment, this.continuation); | 1038 InitializationEK(this.constructor, this.environment, this.continuation); |
| 1037 | 1039 |
| 1038 Configuration call(Value value) { | 1040 Configuration call(Value value) { |
| 1041 Location location = new Location(value); | |
| 1042 | |
| 1039 if (constructor.initializers.isNotEmpty && | 1043 if (constructor.initializers.isNotEmpty && |
| 1040 constructor.initializers.last is RedirectingInitializer) { | 1044 constructor.initializers.last is RedirectingInitializer) { |
| 1041 throw 'Support for redirecting initializers is not implemented.'; | 1045 return _createRedirectingInitializerConfig( |
| 1046 constructor.initializers.first, location); | |
| 1042 } | 1047 } |
| 1043 | |
| 1044 // The statement body is captured by the next statement continuation and | 1048 // The statement body is captured by the next statement continuation and |
| 1045 // expressions for field initialization are evaluated. | 1049 // expressions for field initialization are evaluated. |
| 1046 var ctrEnv = environment.extendWithThis(value); | 1050 var ctrEnv = environment.extendWithThis(value); |
| 1047 var bodyCont = | 1051 var bodyCont = |
| 1048 new ConstructorBodySK(constructor.function.body, ctrEnv, continuation); | 1052 new ConstructorBodySK(constructor.function.body, ctrEnv, continuation); |
| 1049 var initializers = _getFieldInitializers(constructor.enclosingClass); | 1053 var initializers = _getFieldInitializers(constructor.enclosingClass); |
| 1050 var fieldsCont = | 1054 var fieldsCont = |
| 1051 new InstanceFieldsA(constructor, new Location(value), ctrEnv, bodyCont); | 1055 new InstanceFieldsA(constructor, location, ctrEnv, bodyCont); |
| 1052 return new EvalListConfiguration( | 1056 return new EvalListConfiguration( |
| 1053 initializers, new Environment.empty(), fieldsCont); | 1057 initializers, new Environment.empty(), fieldsCont); |
| 1054 } | 1058 } |
| 1059 | |
| 1060 /// Creates the next configuration to further initializer the value for | |
| 1061 /// redirecting constructors. | |
| 1062 Configuration _createRedirectingInitializerConfig( | |
| 1063 Initializer initializer, Location location) { | |
| 1064 Initializer current = constructor.initializers.first; | |
| 1065 if (current is RedirectingInitializer) { | |
| 1066 // eval list of args with current env | |
|
Dmitry Stefantsov
2017/08/03 08:34:08
Please, make a full sentence out of this comment.
zhivkag
2017/08/03 08:46:52
Done.
| |
| 1067 List<InterpreterExpression> exprs = | |
| 1068 _getArgumentExpressions(current.arguments, current.target.function); | |
| 1069 var cont = | |
| 1070 new ConstructorInitializerA(current.target, location, continuation); | |
| 1071 return new EvalListConfiguration(exprs, environment, cont); | |
| 1072 } | |
| 1073 Expression expr = (current is FieldInitializer) | |
| 1074 ? current.value | |
| 1075 : (current as LocalInitializer).variable.initializer; | |
| 1076 | |
| 1077 var cont = new InitializerListEK(constructor, 0 /* initializerIndex*/, | |
|
Dmitry Stefantsov
2017/08/03 08:34:08
Ignore this comment if it was already done in a pr
zhivkag
2017/08/03 08:46:52
I probably lost it in the merge, added again :)
| |
| 1078 location, environment, continuation); | |
| 1079 return new EvalConfiguration(expr, environment, cont); | |
| 1080 } | |
| 1055 } | 1081 } |
| 1056 | 1082 |
| 1057 class InitializerListEK extends ExpressionContinuation { | 1083 class InitializerListEK extends ExpressionContinuation { |
| 1058 final Constructor constructor; | 1084 final Constructor constructor; |
| 1059 final int initializerIndex; | 1085 final int initializerIndex; |
| 1060 final Location location; | 1086 final Location location; |
| 1061 final Environment environment; | 1087 final Environment environment; |
| 1062 // TODO(zhivkag): Add componnents for exception handling. | 1088 // TODO(zhivkag): Add componnents for exception handling. |
| 1063 final ConstructorBodySK continuation; | 1089 final ConstructorBodySK continuation; |
| 1064 final Class _currentClass; | 1090 final Class _currentClass; |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 1091 Configuration _createNextConfiguration(Environment env) { | 1117 Configuration _createNextConfiguration(Environment env) { |
| 1092 assert(initializerIndex + 1 < constructor.initializers.length); | 1118 assert(initializerIndex + 1 < constructor.initializers.length); |
| 1093 Initializer next = constructor.initializers[initializerIndex + 1]; | 1119 Initializer next = constructor.initializers[initializerIndex + 1]; |
| 1094 if (next is SuperInitializer) { | 1120 if (next is SuperInitializer) { |
| 1095 // TODO(zhivkag): Execute constructor of "object" class when support for | 1121 // TODO(zhivkag): Execute constructor of "object" class when support for |
| 1096 // native/external functions is added. | 1122 // native/external functions is added. |
| 1097 if (_currentClass.superclass.superclass == null) { | 1123 if (_currentClass.superclass.superclass == null) { |
| 1098 _initializeNullFields(_currentClass, location.value); | 1124 _initializeNullFields(_currentClass, location.value); |
| 1099 return new ForwardConfiguration(continuation, environment); | 1125 return new ForwardConfiguration(continuation, environment); |
| 1100 } | 1126 } |
| 1101 return _createEvalListConfig(next); | 1127 return _createEvalListConfig(next.arguments, next.target, env); |
| 1102 } | 1128 } |
| 1103 | 1129 |
| 1104 if (next is RedirectingInitializer) { | 1130 if (next is RedirectingInitializer) { |
| 1105 // TODO(zhivkag): Produce the configuration according to | 1131 return _createEvalListConfig(next.arguments, next.target, env); |
| 1106 // specification. | |
| 1107 throw 'Support for RedirectingInitializers is not implemented.'; | |
| 1108 } | 1132 } |
| 1109 | 1133 |
| 1110 Expression nextExpr = (next is FieldInitializer) | 1134 Expression nextExpr = (next is FieldInitializer) |
| 1111 ? next.value | 1135 ? next.value |
| 1112 : (next as LocalInitializer).variable.initializer; | 1136 : (next as LocalInitializer).variable.initializer; |
| 1113 | 1137 |
| 1114 var cont = withInitializerIndex(initializerIndex + 1); | 1138 var cont = withInitializerIndex(initializerIndex + 1); |
| 1115 return new EvalConfiguration(nextExpr, env, cont); | 1139 return new EvalConfiguration(nextExpr, env, cont); |
| 1116 } | 1140 } |
| 1117 | 1141 |
| 1118 Configuration _createEvalListConfig(SuperInitializer initializer) { | 1142 Configuration _createEvalListConfig( |
| 1119 List<InterpreterExpression> args = _getArgumentExpressions( | 1143 Arguments args, Constructor ctr, Environment env) { |
| 1120 initializer.arguments, initializer.target.function); | 1144 List<InterpreterExpression> exprs = |
| 1121 var cont = | 1145 _getArgumentExpressions(args, ctr.function); |
| 1122 new ConstructorInitializerA(initializer.target, location, continuation); | 1146 var cont = new ConstructorInitializerA(ctr, location, continuation); |
| 1123 | 1147 return new EvalListConfiguration(exprs, env, cont); |
| 1124 return new EvalListConfiguration(args, environment, cont); | |
| 1125 } | 1148 } |
| 1126 } | 1149 } |
| 1127 | 1150 |
| 1128 /// Executes statements. | 1151 /// Executes statements. |
| 1129 /// | 1152 /// |
| 1130 /// Execution of a statement completes in one of the following ways: | 1153 /// Execution of a statement completes in one of the following ways: |
| 1131 /// - It completes normally, in which case the execution proceeds to applying | 1154 /// - It completes normally, in which case the execution proceeds to applying |
| 1132 /// the next continuation. | 1155 /// the next continuation. |
| 1133 /// - It breaks with a label, in which case the corresponding continuation is | 1156 /// - It breaks with a label, in which case the corresponding continuation is |
| 1134 /// returned and applied. | 1157 /// returned and applied. |
| (...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1538 /// Initializes all non initialized fields from the provided class to | 1561 /// Initializes all non initialized fields from the provided class to |
| 1539 /// `Value.nullInstance` in the provided value. | 1562 /// `Value.nullInstance` in the provided value. |
| 1540 void _initializeNullFields(Class class_, Value value) { | 1563 void _initializeNullFields(Class class_, Value value) { |
| 1541 int startIndex = class_.superclass?.instanceSize ?? 0; | 1564 int startIndex = class_.superclass?.instanceSize ?? 0; |
| 1542 for (int i = startIndex; i < class_.instanceSize; i++) { | 1565 for (int i = startIndex; i < class_.instanceSize; i++) { |
| 1543 if (value.fields[i].value == null) { | 1566 if (value.fields[i].value == null) { |
| 1544 value.fields[i].value = Value.nullInstance; | 1567 value.fields[i].value = Value.nullInstance; |
| 1545 } | 1568 } |
| 1546 } | 1569 } |
| 1547 } | 1570 } |
| OLD | NEW |