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 a constructor with an empty |
| 715 // initializer 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 // Evaluate the list of arguments for invoking the target constructor in |
| 1067 // the current environment. |
| 1068 List<InterpreterExpression> exprs = |
| 1069 _getArgumentExpressions(current.arguments, current.target.function); |
| 1070 var cont = |
| 1071 new ConstructorInitializerA(current.target, location, continuation); |
| 1072 return new EvalListConfiguration(exprs, environment, cont); |
| 1073 } |
| 1074 Expression expr = (current is FieldInitializer) |
| 1075 ? current.value |
| 1076 : (current as LocalInitializer).variable.initializer; |
| 1077 |
| 1078 // The index is set to 0 since we are evaluating the expression for the |
| 1079 // first initializer in the initializer list. |
| 1080 var cont = new InitializerListEK( |
| 1081 constructor, 0, location, environment, continuation); |
| 1082 return new EvalConfiguration(expr, environment, cont); |
| 1083 } |
1055 } | 1084 } |
1056 | 1085 |
1057 class InitializerListEK extends ExpressionContinuation { | 1086 class InitializerListEK extends ExpressionContinuation { |
1058 final Constructor constructor; | 1087 final Constructor constructor; |
1059 final int initializerIndex; | 1088 final int initializerIndex; |
1060 final Location location; | 1089 final Location location; |
1061 final Environment environment; | 1090 final Environment environment; |
1062 // TODO(zhivkag): Add componnents for exception handling. | 1091 // TODO(zhivkag): Add componnents for exception handling. |
1063 final ConstructorBodySK continuation; | 1092 final ConstructorBodySK continuation; |
1064 final Class _currentClass; | 1093 final Class _currentClass; |
(...skipping 26 matching lines...) Expand all Loading... |
1091 Configuration _createNextConfiguration(Environment env) { | 1120 Configuration _createNextConfiguration(Environment env) { |
1092 assert(initializerIndex + 1 < constructor.initializers.length); | 1121 assert(initializerIndex + 1 < constructor.initializers.length); |
1093 Initializer next = constructor.initializers[initializerIndex + 1]; | 1122 Initializer next = constructor.initializers[initializerIndex + 1]; |
1094 if (next is SuperInitializer) { | 1123 if (next is SuperInitializer) { |
1095 // TODO(zhivkag): Execute constructor of "object" class when support for | 1124 // TODO(zhivkag): Execute constructor of "object" class when support for |
1096 // native/external functions is added. | 1125 // native/external functions is added. |
1097 if (_currentClass.superclass.superclass == null) { | 1126 if (_currentClass.superclass.superclass == null) { |
1098 _initializeNullFields(_currentClass, location.value); | 1127 _initializeNullFields(_currentClass, location.value); |
1099 return new ForwardConfiguration(continuation, environment); | 1128 return new ForwardConfiguration(continuation, environment); |
1100 } | 1129 } |
1101 return _createEvalListConfig(next); | 1130 return _createEvalListConfig(next.arguments, next.target, env); |
1102 } | 1131 } |
1103 | 1132 |
1104 if (next is RedirectingInitializer) { | 1133 if (next is RedirectingInitializer) { |
1105 // TODO(zhivkag): Produce the configuration according to | 1134 return _createEvalListConfig(next.arguments, next.target, env); |
1106 // specification. | |
1107 throw 'Support for RedirectingInitializers is not implemented.'; | |
1108 } | 1135 } |
1109 | 1136 |
1110 Expression nextExpr = (next is FieldInitializer) | 1137 Expression nextExpr = (next is FieldInitializer) |
1111 ? next.value | 1138 ? next.value |
1112 : (next as LocalInitializer).variable.initializer; | 1139 : (next as LocalInitializer).variable.initializer; |
1113 | 1140 |
1114 var cont = withInitializerIndex(initializerIndex + 1); | 1141 var cont = withInitializerIndex(initializerIndex + 1); |
1115 return new EvalConfiguration(nextExpr, env, cont); | 1142 return new EvalConfiguration(nextExpr, env, cont); |
1116 } | 1143 } |
1117 | 1144 |
1118 Configuration _createEvalListConfig(SuperInitializer initializer) { | 1145 Configuration _createEvalListConfig( |
1119 List<InterpreterExpression> args = _getArgumentExpressions( | 1146 Arguments args, Constructor ctr, Environment env) { |
1120 initializer.arguments, initializer.target.function); | 1147 List<InterpreterExpression> exprs = |
1121 var cont = | 1148 _getArgumentExpressions(args, ctr.function); |
1122 new ConstructorInitializerA(initializer.target, location, continuation); | 1149 var cont = new ConstructorInitializerA(ctr, location, continuation); |
1123 | 1150 return new EvalListConfiguration(exprs, env, cont); |
1124 return new EvalListConfiguration(args, environment, cont); | |
1125 } | 1151 } |
1126 } | 1152 } |
1127 | 1153 |
1128 /// Executes statements. | 1154 /// Executes statements. |
1129 /// | 1155 /// |
1130 /// Execution of a statement completes in one of the following ways: | 1156 /// Execution of a statement completes in one of the following ways: |
1131 /// - It completes normally, in which case the execution proceeds to applying | 1157 /// - It completes normally, in which case the execution proceeds to applying |
1132 /// the next continuation. | 1158 /// the next continuation. |
1133 /// - It breaks with a label, in which case the corresponding continuation is | 1159 /// - It breaks with a label, in which case the corresponding continuation is |
1134 /// returned and applied. | 1160 /// 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 | 1564 /// Initializes all non initialized fields from the provided class to |
1539 /// `Value.nullInstance` in the provided value. | 1565 /// `Value.nullInstance` in the provided value. |
1540 void _initializeNullFields(Class class_, Value value) { | 1566 void _initializeNullFields(Class class_, Value value) { |
1541 int startIndex = class_.superclass?.instanceSize ?? 0; | 1567 int startIndex = class_.superclass?.instanceSize ?? 0; |
1542 for (int i = startIndex; i < class_.instanceSize; i++) { | 1568 for (int i = startIndex; i < class_.instanceSize; i++) { |
1543 if (value.fields[i].value == null) { | 1569 if (value.fields[i].value == null) { |
1544 value.fields[i].value = Value.nullInstance; | 1570 value.fields[i].value = Value.nullInstance; |
1545 } | 1571 } |
1546 } | 1572 } |
1547 } | 1573 } |
OLD | NEW |