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 1014 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1025 // ------------------------------------------------------------------------ | 1025 // ------------------------------------------------------------------------ |
1026 // VALUES | 1026 // VALUES |
1027 // ------------------------------------------------------------------------ | 1027 // ------------------------------------------------------------------------ |
1028 | 1028 |
1029 typedef Value Getter(Value receiver); | 1029 typedef Value Getter(Value receiver); |
1030 typedef void Setter(Value receiver, Value value); | 1030 typedef void Setter(Value receiver, Value value); |
1031 | 1031 |
1032 class Class { | 1032 class Class { |
1033 static final Map<Reference, Class> _classes = <Reference, Class>{}; | 1033 static final Map<Reference, Class> _classes = <Reference, Class>{}; |
1034 | 1034 |
| 1035 /// The immediate super class, or `null` if this is the root class object. |
1035 Class superclass; | 1036 Class superclass; |
1036 List<Field> instanceFields = <Field>[]; | |
1037 // Implicit getters and setters for instance Fields. | |
1038 Map<Name, Getter> getters = <Name, Getter>{}; | |
1039 Map<Name, Setter> setters = <Name, Setter>{}; | |
1040 // The initializers of static fields are evaluated the first time the field | |
1041 // is accessed. | |
1042 List<Procedure> methods = <Procedure>[]; | |
1043 | 1037 |
1044 int get instanceSize => instanceFields.length; | 1038 /// The class definitions from the `implements` clause. |
| 1039 final List<Supertype> interfaces = <Supertype>[]; |
| 1040 |
| 1041 /// Implicit getters for instance fields. |
| 1042 Map<Name, Getter> implicitGetters = <Name, Getter>{}; |
| 1043 |
| 1044 /// Implicit setters for non final instance fields. |
| 1045 Map<Name, Setter> implicitSetters = <Name, Setter>{}; |
| 1046 |
| 1047 /// Instance methods, explicit getters and setters. |
| 1048 Map<Name, Procedure> methods = <Name, Procedure>{}; |
| 1049 |
| 1050 int get instanceSize => implicitGetters.length; |
1045 | 1051 |
1046 factory Class(Reference classRef) { | 1052 factory Class(Reference classRef) { |
1047 return _classes.putIfAbsent( | 1053 return _classes.putIfAbsent( |
1048 classRef, () => new Class._internal(classRef.asClass)); | 1054 classRef, () => new Class._internal(classRef.asClass)); |
1049 } | 1055 } |
1050 | 1056 |
1051 Class._internal(ast.Class currentClass) { | 1057 Class._internal(ast.Class currentClass) { |
1052 if (currentClass.superclass != null) { | 1058 if (currentClass.superclass != null) { |
1053 superclass = new Class(currentClass.superclass.reference); | 1059 superclass = new Class(currentClass.superclass.reference); |
1054 } | 1060 } |
1055 | 1061 |
1056 _populateInstanceFields(currentClass); | 1062 _populateImplicitGettersAndSetters(currentClass); |
1057 // TODO: Populate methods. | 1063 _populateInstanceMethods(currentClass); |
1058 } | 1064 } |
1059 | 1065 |
1060 Getter lookupImplicitGetter(Name name) { | 1066 Getter lookupImplicitGetter(Name name) { |
1061 Getter getter = getters[name]; | 1067 Getter getter = implicitGetters[name]; |
1062 if (getter != null) return getter; | 1068 if (getter != null) return getter; |
1063 if (superclass != null) return superclass.lookupImplicitGetter(name); | 1069 if (superclass != null) return superclass.lookupImplicitGetter(name); |
1064 return (Value receiver) => notImplemented(obj: name); | 1070 return (Value receiver) => notImplemented(obj: name); |
1065 } | 1071 } |
1066 | 1072 |
1067 Setter lookupImplicitSetter(Name name) { | 1073 Setter lookupImplicitSetter(Name name) { |
1068 Setter setter = setters[name]; | 1074 Setter setter = implicitSetters[name]; |
1069 if (setter != null) return setter; | 1075 if (setter != null) return setter; |
1070 if (superclass != null) return superclass.lookupImplicitSetter(name); | 1076 if (superclass != null) return superclass.lookupImplicitSetter(name); |
1071 return (Value receiver, Value value) => notImplemented(obj: name); | 1077 return (Value receiver, Value value) => notImplemented(obj: name); |
1072 } | 1078 } |
1073 | 1079 |
1074 Value getProperty(ObjectValue object, Member member) { | 1080 /// Populates implicit getters and setters for the current class and its |
1075 if (member is Field) { | 1081 /// superclass recursively. |
1076 int index = instanceFields.indexOf(member); | 1082 _populateImplicitGettersAndSetters(ast.Class class_) { |
1077 // TODO: throw NoSuchMethodError instead. | |
1078 if (index < 0) return notImplemented(m: 'NoSuchMethod: ${member}'); | |
1079 return object.fields[index].value; | |
1080 } | |
1081 return notImplemented(obj: member); | |
1082 } | |
1083 | |
1084 Value setProperty(ObjectValue object, Member member, Value value) { | |
1085 if (member is Field) { | |
1086 int index = instanceFields.indexOf(member); | |
1087 // TODO: throw NoSuchMethodError instead. | |
1088 if (index < 0) return notImplemented(m: 'NoSuchMethod: ${member}'); | |
1089 object.fields[index] = new Location(value); | |
1090 return Value.nullInstance; | |
1091 } | |
1092 return notImplemented(obj: member); | |
1093 } | |
1094 | |
1095 /// Populates instance variables and the corresponding implicit getters and | |
1096 /// setters for the current class and its superclass recursively. | |
1097 _populateInstanceFields(ast.Class class_) { | |
1098 if (class_.superclass != null) { | 1083 if (class_.superclass != null) { |
1099 _populateInstanceFields(class_.superclass); | 1084 _populateImplicitGettersAndSetters(class_.superclass); |
1100 } | 1085 } |
1101 | 1086 |
1102 for (Field f in class_.fields) { | 1087 for (Field f in class_.fields) { |
1103 if (f.isStatic) continue; | 1088 if (f.isStatic) continue; |
1104 instanceFields.add(f); | |
1105 assert(f.hasImplicitGetter); | 1089 assert(f.hasImplicitGetter); |
1106 | 1090 |
1107 int currentFieldIndex = instanceFields.length - 1; | 1091 int currentFieldIndex = implicitGetters.length; |
1108 | |
1109 // Shadowing an inherited getter with the same name. | 1092 // Shadowing an inherited getter with the same name. |
1110 getters[f.name] = | 1093 implicitGetters[f.name] = |
1111 (Value receiver) => receiver.fields[currentFieldIndex].value; | 1094 (Value receiver) => receiver.fields[currentFieldIndex].value; |
1112 if (f.hasImplicitSetter) { | 1095 if (f.hasImplicitSetter) { |
1113 // Shadowing an inherited setter with the same name. | 1096 // Shadowing an inherited setter with the same name. |
1114 setters[f.name] = (Value receiver, Value value) => | 1097 implicitSetters[f.name] = (Value receiver, Value value) => |
1115 receiver.fields[currentFieldIndex] = new Location(value); | 1098 receiver.fields[currentFieldIndex] = new Location(value); |
1116 } | 1099 } |
1117 } | 1100 } |
1118 } | 1101 } |
| 1102 |
| 1103 /// Populates instance methods, getters and setters for the current class and |
| 1104 /// its super class recursively. |
| 1105 _populateInstanceMethods(ast.Class class_) { |
| 1106 if (class_.superclass != null) { |
| 1107 _populateInstanceMethods(class_.superclass); |
| 1108 } |
| 1109 |
| 1110 for (Procedure p in class_.members) { |
| 1111 if (p.isStatic) continue; |
| 1112 // Shadowing an inherited method, getter or setter with the same name. |
| 1113 methods[p.name] = p; |
| 1114 } |
| 1115 } |
1119 } | 1116 } |
1120 | 1117 |
1121 abstract class Value { | 1118 abstract class Value { |
1122 Class get class_; | 1119 Class get class_; |
1123 List<Location> get fields; | 1120 List<Location> get fields; |
1124 Object get value; | 1121 Object get value; |
1125 | 1122 |
1126 static final NullValue nullInstance = const NullValue(); | 1123 static final NullValue nullInstance = const NullValue(); |
1127 static final BoolValue trueInstance = const BoolValue(true); | 1124 static final BoolValue trueInstance = const BoolValue(true); |
1128 static final BoolValue falseInstance = const BoolValue(false); | 1125 static final BoolValue falseInstance = const BoolValue(false); |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1283 var current = providedArgs.named[i]; | 1280 var current = providedArgs.named[i]; |
1284 args.add(new NamedExpression(current.name, current.value)); | 1281 args.add(new NamedExpression(current.name, current.value)); |
1285 namedFormals.remove(current.name); | 1282 namedFormals.remove(current.name); |
1286 } | 1283 } |
1287 | 1284 |
1288 // Add missing optional named initializers. | 1285 // Add missing optional named initializers. |
1289 args.addAll(namedFormals.values); | 1286 args.addAll(namedFormals.values); |
1290 | 1287 |
1291 return args; | 1288 return args; |
1292 } | 1289 } |
OLD | NEW |