OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 // VM-specific implementation of the dart:mirrors library. | 5 // VM-specific implementation of the dart:mirrors library. |
6 | 6 |
7 import "dart:collection"; | 7 import "dart:collection"; |
8 | 8 |
9 final emptyList = new UnmodifiableListView([]); | 9 final emptyList = new UnmodifiableListView([]); |
10 final emptyMap = new _UnmodifiableMapView({}); | 10 final emptyMap = new _UnmodifiableMapView({}); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 final Symbol _selector; | 153 final Symbol _selector; |
154 _InvocationTrampoline(this._receiver, this._selector); | 154 _InvocationTrampoline(this._receiver, this._selector); |
155 noSuchMethod(Invocation msg) { | 155 noSuchMethod(Invocation msg) { |
156 if (msg.memberName != #call) return super.noSuchMethod(msg); | 156 if (msg.memberName != #call) return super.noSuchMethod(msg); |
157 return _receiver.invoke(_selector, | 157 return _receiver.invoke(_selector, |
158 msg.positionalArguments, | 158 msg.positionalArguments, |
159 msg.namedArguments); | 159 msg.namedArguments); |
160 } | 160 } |
161 } | 161 } |
162 | 162 |
| 163 class _SyntheticAccessor implements MethodMirror { |
| 164 final DeclarationMirror owner; |
| 165 final Symbol simpleName; |
| 166 final bool isGetter; |
| 167 final bool isStatic; |
| 168 final bool isTopLevel; |
| 169 final _target; |
| 170 |
| 171 _SyntheticAccessor(this.owner, |
| 172 this.simpleName, |
| 173 this.isGetter, |
| 174 this.isStatic, |
| 175 this.isTopLevel, |
| 176 this._target); |
| 177 |
| 178 bool get isSynthetic => true; |
| 179 bool get isRegularMethod => false; |
| 180 bool get isOperator => false; |
| 181 bool get isConstructor => false; |
| 182 bool get isConstConstructor => false; |
| 183 bool get isGenerativeConstructor => false; |
| 184 bool get isFactoryConstructor => false; |
| 185 bool get isRedirectingConstructor => false; |
| 186 bool get isAbstract => false; |
| 187 |
| 188 bool get isSetter => !isGetter; |
| 189 bool get isPrivate => _n(simpleName).startsWith('_'); |
| 190 |
| 191 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); |
| 192 Symbol get constructorName => const Symbol(''); |
| 193 |
| 194 TypeMirror get returnType => _target.type; |
| 195 List<ParameterMirror> get parameters { |
| 196 if (isGetter) return emptyList; |
| 197 return new UnmodifiableListView( |
| 198 [new _SyntheticSetterParameter(this, this._target)]); |
| 199 } |
| 200 |
| 201 List<InstanceMirror> get metadata => emptyList; |
| 202 |
| 203 String get source => '<synthetic code>'; |
| 204 } |
| 205 |
| 206 class _SyntheticSetterParameter implements ParameterMirror { |
| 207 final DeclarationMirror owner; |
| 208 final VariableMirror _target; |
| 209 |
| 210 _SyntheticSetterParameter(this.owner, this._target); |
| 211 |
| 212 Symbol get simpleName => _target.simpleName; |
| 213 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); |
| 214 TypeMirror get type => _target.type; |
| 215 |
| 216 bool get isOptional => false; |
| 217 bool get isNamed => false; |
| 218 bool get isStatic => false; |
| 219 bool get isTopLevel => false; |
| 220 bool get isConst => false; |
| 221 bool get isFinal => true; |
| 222 bool get isPrivate => false; |
| 223 bool get hasDefaultValue => false; |
| 224 InstanceMirror get defaultValue => null; |
| 225 } |
| 226 |
| 227 class _SyntheticTypeGetter extends _SyntheticAccessor { |
| 228 _SyntheticTypeGetter(owner, simpleName, target) |
| 229 : super(owner, simpleName, true, true, true, target); |
| 230 TypeMirror get returnType => reflectClass(Type); |
| 231 } |
| 232 |
163 abstract class _LocalObjectMirror extends _LocalMirror implements ObjectMirror { | 233 abstract class _LocalObjectMirror extends _LocalMirror implements ObjectMirror { |
164 final _reflectee; // May be a MirrorReference or an ordinary object. | 234 final _reflectee; // May be a MirrorReference or an ordinary object. |
165 | 235 |
166 _LocalObjectMirror(this._reflectee); | 236 _LocalObjectMirror(this._reflectee); |
167 | 237 |
168 InstanceMirror invoke(Symbol memberName, | 238 InstanceMirror invoke(Symbol memberName, |
169 List positionalArguments, | 239 List positionalArguments, |
170 [Map<Symbol, dynamic> namedArguments]) { | 240 [Map<Symbol, dynamic> namedArguments]) { |
171 int numPositionalArguments = positionalArguments.length; | 241 int numPositionalArguments = positionalArguments.length; |
172 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 242 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
(...skipping 320 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
493 // The reflectee is not a mixin application. | 563 // The reflectee is not a mixin application. |
494 _mixin = this; | 564 _mixin = this; |
495 } else { | 565 } else { |
496 _mixin = reflectType(mixinType); | 566 _mixin = reflectType(mixinType); |
497 } | 567 } |
498 } | 568 } |
499 } | 569 } |
500 return _mixin; | 570 return _mixin; |
501 } | 571 } |
502 | 572 |
| 573 var _cachedStaticMembers; |
| 574 Map<Symbol, MethodMirror> get staticMembers { |
| 575 if (_cachedStaticMembers != null) return _cachedStaticMembers; |
| 576 var result = new Map<Symbol, MethodMirror>(); |
| 577 declarations.values.forEach((decl) { |
| 578 if (decl is MethodMirror && decl.isStatic && |
| 579 !decl.isConstructor && !decl.isAbstract) { |
| 580 result[decl.simpleName] = decl; |
| 581 } |
| 582 if (decl is VariableMirror && decl.isStatic) { |
| 583 var getterName = decl.simpleName; |
| 584 result[getterName] = |
| 585 new _SyntheticAccessor(this, getterName, true, true, false, decl); |
| 586 if (!decl.isFinal) { |
| 587 var setterName = _asSetter(decl.simpleName, this.owner); |
| 588 result[setterName] = new _SyntheticAccessor( |
| 589 this, setterName, false, true, false, decl); |
| 590 } |
| 591 } |
| 592 }); |
| 593 return _cachedStaticMembers = result; |
| 594 } |
| 595 |
| 596 var _cachedInstanceMembers; |
| 597 Map<Symbol, MethodMirror> get instanceMembers { |
| 598 if (_cachedInstanceMembers != null) return _cachedInstanceMembers; |
| 599 var result = new Map<Symbol, MethodMirror>(); |
| 600 if (superclass != null) { |
| 601 result.addAll(superclass.instanceMembers); |
| 602 } |
| 603 declarations.values.forEach((decl) { |
| 604 if (decl is MethodMirror && !decl.isStatic && |
| 605 !decl.isConstructor && !decl.isAbstract) { |
| 606 result[decl.simpleName] = decl; |
| 607 } |
| 608 if (decl is VariableMirror && !decl.isStatic) { |
| 609 var getterName = decl.simpleName; |
| 610 result[getterName] = |
| 611 new _SyntheticAccessor(this, getterName, true, false, false, decl); |
| 612 if (!decl.isFinal) { |
| 613 var setterName = _asSetter(decl.simpleName, this.owner); |
| 614 result[setterName] = new _SyntheticAccessor( |
| 615 this, setterName, false, false, false, decl); |
| 616 } |
| 617 } |
| 618 }); |
| 619 return _cachedInstanceMembers = result; |
| 620 } |
| 621 |
503 Map<Symbol, DeclarationMirror> _declarations; | 622 Map<Symbol, DeclarationMirror> _declarations; |
504 Map<Symbol, DeclarationMirror> get declarations { | 623 Map<Symbol, DeclarationMirror> get declarations { |
505 if (_declarations != null) return _declarations; | 624 if (_declarations != null) return _declarations; |
506 var decls = new Map<Symbol, DeclarationMirror>(); | 625 var decls = new Map<Symbol, DeclarationMirror>(); |
507 decls.addAll(members); | 626 decls.addAll(members); |
508 decls.addAll(constructors); | 627 decls.addAll(constructors); |
509 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 628 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
510 return _declarations = | 629 return _declarations = |
511 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 630 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
512 } | 631 } |
(...skipping 446 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
959 | 1078 |
960 String toString() => "TypedefMirror on '${_n(simpleName)}'"; | 1079 String toString() => "TypedefMirror on '${_n(simpleName)}'"; |
961 | 1080 |
962 static _nativeReferent(reflectedType) | 1081 static _nativeReferent(reflectedType) |
963 native "TypedefMirror_referent"; | 1082 native "TypedefMirror_referent"; |
964 | 1083 |
965 static _nativeDeclaration(reflectedType) | 1084 static _nativeDeclaration(reflectedType) |
966 native "TypedefMirror_declaration"; | 1085 native "TypedefMirror_declaration"; |
967 } | 1086 } |
968 | 1087 |
969 class _LocalLibraryMirror extends _LocalObjectMirror | 1088 Symbol _asSetter(Symbol getter, LibraryMirror library) { |
970 implements LibraryMirror { | 1089 var unwrapped = MirrorSystem.getName(getter); |
| 1090 return MirrorSystem.getSymbol('${unwrapped}=', library); |
| 1091 } |
| 1092 |
| 1093 class _LocalLibraryMirror extends _LocalObjectMirror implements LibraryMirror { |
971 final Symbol simpleName; | 1094 final Symbol simpleName; |
972 final Uri uri; | 1095 final Uri uri; |
973 | 1096 |
974 _LocalLibraryMirror(reflectee, | 1097 _LocalLibraryMirror(reflectee, |
975 String simpleName, | 1098 String simpleName, |
976 String url) | 1099 String url) |
977 : this.simpleName = _s(simpleName), | 1100 : this.simpleName = _s(simpleName), |
978 this.uri = Uri.parse(url), | 1101 this.uri = Uri.parse(url), |
979 super(reflectee); | 1102 super(reflectee); |
980 | 1103 |
981 // The simple name and the qualified name are the same for a library. | 1104 // The simple name and the qualified name are the same for a library. |
982 Symbol get qualifiedName => simpleName; | 1105 Symbol get qualifiedName => simpleName; |
983 | 1106 |
984 DeclarationMirror get owner => null; | 1107 DeclarationMirror get owner => null; |
985 | 1108 |
986 bool get isPrivate => false; | 1109 bool get isPrivate => false; |
987 bool get isTopLevel => false; | 1110 bool get isTopLevel => false; |
988 | 1111 |
989 Type get _instantiator => null; | 1112 Type get _instantiator => null; |
990 | 1113 |
991 SourceLocation get location { | 1114 SourceLocation get location { |
992 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1115 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
993 } | 1116 } |
994 | 1117 |
| 1118 var _cachedTopLevelMembers; |
| 1119 Map<Symbol, MethodMirror> get topLevelMembers { |
| 1120 if (_cachedTopLevelMembers != null) return _cachedTopLevelMembers; |
| 1121 var result = new Map<Symbol, MethodMirror>(); |
| 1122 declarations.values.forEach((decl) { |
| 1123 if (decl is MethodMirror && !decl.isAbstract) { |
| 1124 result[decl.simpleName] = decl; |
| 1125 } |
| 1126 if (decl is VariableMirror) { |
| 1127 var getterName = decl.simpleName; |
| 1128 result[getterName] = |
| 1129 new _SyntheticAccessor(this, getterName, true, true, true, decl); |
| 1130 if (!decl.isFinal) { |
| 1131 var setterName = _asSetter(decl.simpleName, this); |
| 1132 result[setterName] = new _SyntheticAccessor( |
| 1133 this, setterName, false, true, true, decl); |
| 1134 } |
| 1135 } |
| 1136 if (decl is TypeMirror) { |
| 1137 var getterName = decl.simpleName; |
| 1138 result[getterName] = new _SyntheticTypeGetter(this, getterName, decl); |
| 1139 } |
| 1140 }); |
| 1141 return _cachedTopLevelMembers = result; |
| 1142 } |
| 1143 |
995 Map<Symbol, DeclarationMirror> _declarations; | 1144 Map<Symbol, DeclarationMirror> _declarations; |
996 Map<Symbol, DeclarationMirror> get declarations { | 1145 Map<Symbol, DeclarationMirror> get declarations { |
997 if (_declarations != null) return _declarations; | 1146 if (_declarations != null) return _declarations; |
998 return _declarations = | 1147 return _declarations = |
999 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); | 1148 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); |
1000 } | 1149 } |
1001 | 1150 |
1002 Map<Symbol, Mirror> _members; | 1151 Map<Symbol, Mirror> _members; |
1003 Map<Symbol, Mirror> get members { | 1152 Map<Symbol, Mirror> get members { |
1004 if (_members == null) { | 1153 if (_members == null) { |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1087 _invokeGetter(reflectee, getterName) | 1236 _invokeGetter(reflectee, getterName) |
1088 native 'LibraryMirror_invokeGetter'; | 1237 native 'LibraryMirror_invokeGetter'; |
1089 | 1238 |
1090 _invokeSetter(reflectee, setterName, value) | 1239 _invokeSetter(reflectee, setterName, value) |
1091 native 'LibraryMirror_invokeSetter'; | 1240 native 'LibraryMirror_invokeSetter'; |
1092 | 1241 |
1093 _computeMembers(reflectee) | 1242 _computeMembers(reflectee) |
1094 native "LibraryMirror_members"; | 1243 native "LibraryMirror_members"; |
1095 } | 1244 } |
1096 | 1245 |
1097 class _LocalMethodMirror extends _LocalDeclarationMirror | 1246 class _LocalMethodMirror extends _LocalDeclarationMirror |
1098 implements MethodMirror { | 1247 implements MethodMirror { |
1099 final bool isStatic; | 1248 final bool isStatic; |
1100 final bool isAbstract; | 1249 final bool isAbstract; |
1101 final bool isGetter; | 1250 final bool isGetter; |
1102 final bool isSetter; | 1251 final bool isSetter; |
1103 final bool isConstructor; | 1252 final bool isConstructor; |
1104 final bool isConstConstructor; | 1253 final bool isConstConstructor; |
1105 final bool isGenerativeConstructor; | 1254 final bool isGenerativeConstructor; |
1106 final bool isRedirectingConstructor; | 1255 final bool isRedirectingConstructor; |
1107 final bool isFactoryConstructor; | 1256 final bool isFactoryConstructor; |
(...skipping 24 matching lines...) Expand all Loading... |
1132 if (_owner == null) { | 1281 if (_owner == null) { |
1133 _owner = _MethodMirror_owner(_reflectee); | 1282 _owner = _MethodMirror_owner(_reflectee); |
1134 } | 1283 } |
1135 return _owner; | 1284 return _owner; |
1136 } | 1285 } |
1137 | 1286 |
1138 bool get isPrivate => _n(simpleName).startsWith('_') || | 1287 bool get isPrivate => _n(simpleName).startsWith('_') || |
1139 _n(constructorName).startsWith('_'); | 1288 _n(constructorName).startsWith('_'); |
1140 | 1289 |
1141 bool get isTopLevel => owner is LibraryMirror; | 1290 bool get isTopLevel => owner is LibraryMirror; |
| 1291 bool get isSynthetic => false; |
1142 | 1292 |
1143 SourceLocation get location { | 1293 SourceLocation get location { |
1144 throw new UnimplementedError('MethodMirror.location is not implemented'); | 1294 throw new UnimplementedError('MethodMirror.location is not implemented'); |
1145 } | 1295 } |
1146 | 1296 |
1147 Type get _instantiator { | 1297 Type get _instantiator { |
1148 var o = owner; | 1298 var o = owner; |
1149 while (o is MethodMirror) o = o.owner; | 1299 while (o is MethodMirror) o = o.owner; |
1150 return o._instantiator; | 1300 return o._instantiator; |
1151 } | 1301 } |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1425 if (typeMirror == null) { | 1575 if (typeMirror == null) { |
1426 typeMirror = makeLocalTypeMirror(key); | 1576 typeMirror = makeLocalTypeMirror(key); |
1427 _instanitationCache[key] = typeMirror; | 1577 _instanitationCache[key] = typeMirror; |
1428 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1578 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1429 _declarationCache[key] = typeMirror; | 1579 _declarationCache[key] = typeMirror; |
1430 } | 1580 } |
1431 } | 1581 } |
1432 return typeMirror; | 1582 return typeMirror; |
1433 } | 1583 } |
1434 } | 1584 } |
OLD | NEW |