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 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 Symbol _selector; | 168 Symbol _selector; |
169 _InvocationTrampoline(this._receiver, this._selector); | 169 _InvocationTrampoline(this._receiver, this._selector); |
170 noSuchMethod(Invocation msg) { | 170 noSuchMethod(Invocation msg) { |
171 if (msg.memberName != #call) return super.noSuchMethod(msg); | 171 if (msg.memberName != #call) return super.noSuchMethod(msg); |
172 return _receiver.invoke(_selector, | 172 return _receiver.invoke(_selector, |
173 msg.positionalArguments, | 173 msg.positionalArguments, |
174 msg.namedArguments); | 174 msg.namedArguments); |
175 } | 175 } |
176 } | 176 } |
177 | 177 |
| 178 class _SyntheticAccessor implements MethodMirror { |
| 179 final DeclarationMirror owner; |
| 180 final Symbol simpleName; |
| 181 final bool isGetter; |
| 182 final bool isStatic; |
| 183 final bool isTopLevel; |
| 184 final _target; |
| 185 |
| 186 _SyntheticAccessor(this.owner, |
| 187 this.simpleName, |
| 188 this.isGetter, |
| 189 this.isStatic, |
| 190 this.isTopLevel, |
| 191 this._target); |
| 192 |
| 193 bool get isSynthetic => true; |
| 194 bool get isRegularMethod => false; |
| 195 bool get isOperator => false; |
| 196 bool get isConstructor => false; |
| 197 bool get isConstConstructor => false; |
| 198 bool get isGenerativeConstructor => false; |
| 199 bool get isFactoryConstructor => false; |
| 200 bool get isRedirectingConstructor => false; |
| 201 bool get isAbstract => false; |
| 202 |
| 203 |
| 204 bool get isSetter => !isGetter; |
| 205 bool get isPrivate => _n(simpleName).startsWith('_'); |
| 206 |
| 207 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); |
| 208 Symbol get constructorName => const Symbol(''); |
| 209 |
| 210 TypeMirror get returnType => _target.type; |
| 211 List<ParameterMirror> get parameters { |
| 212 if (isGetter) return emptyList; |
| 213 return new UnmodifiableListView( |
| 214 [new _SyntheticSetterParameter(this, this._target)]); |
| 215 } |
| 216 |
| 217 List<InstanceMirror> get metadata => emptyList; |
| 218 |
| 219 String get source => '<synthetic code>'; |
| 220 } |
| 221 |
| 222 class _SyntheticSetterParameter implements ParameterMirror { |
| 223 final DeclarationMirror owner; |
| 224 final VariableMirror _target; |
| 225 _SyntheticSetterParameter(this.owner, this._target); |
| 226 |
| 227 Symbol get simpleName => _target.simpleName; |
| 228 Symbol get qualifiedName => _computeQualifiedName(owner, simpleName); |
| 229 TypeMirror get type => _target.type; |
| 230 |
| 231 bool get isOptional => false; |
| 232 bool get isNamed => false; |
| 233 bool get isStatic => false; |
| 234 bool get isTopLevel => false; |
| 235 bool get isConst => false; |
| 236 bool get isFinal => true; |
| 237 bool get isPrivate => false; |
| 238 bool get hasDefaultValue => false; |
| 239 InstanceMirror get defaultValue => null; |
| 240 } |
| 241 |
| 242 class _SyntheticTypeGetter extends _SyntheticAccessor { |
| 243 _SyntheticTypeGetter(owner, simpleName, target) |
| 244 : super(owner, simpleName, true, true, true, target); |
| 245 TypeMirror get returnType => _target; |
| 246 } |
| 247 |
178 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl | 248 abstract class _LocalObjectMirrorImpl extends _LocalMirrorImpl |
179 implements ObjectMirror { | 249 implements ObjectMirror { |
180 _LocalObjectMirrorImpl(this._reflectee); | 250 _LocalObjectMirrorImpl(this._reflectee); |
181 | 251 |
182 final _reflectee; // May be a MirrorReference or an ordinary object. | 252 final _reflectee; // May be a MirrorReference or an ordinary object. |
183 | 253 |
184 InstanceMirror invoke(Symbol memberName, | 254 InstanceMirror invoke(Symbol memberName, |
185 List positionalArguments, | 255 List positionalArguments, |
186 [Map<Symbol, dynamic> namedArguments]) { | 256 [Map<Symbol, dynamic> namedArguments]) { |
187 | 257 |
(...skipping 330 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 // The reflectee is not a mixin application. | 588 // The reflectee is not a mixin application. |
519 _mixin = this; | 589 _mixin = this; |
520 } else { | 590 } else { |
521 _mixin = reflectType(mixinType); | 591 _mixin = reflectType(mixinType); |
522 } | 592 } |
523 } | 593 } |
524 } | 594 } |
525 return _mixin; | 595 return _mixin; |
526 } | 596 } |
527 | 597 |
| 598 var _cachedStaticMembers; |
| 599 Map<Symbol, MethodMirror> get staticMembers { |
| 600 if (_cachedStaticMembers != null) return _cachedStaticMembers; |
| 601 var result = new Map<Symbol, MethodMirror>(); |
| 602 declarations.values.forEach((decl) { |
| 603 if (decl is MethodMirror && decl.isStatic && |
| 604 !decl.isConstructor && !decl.isAbstract) { |
| 605 result[decl.simpleName] = decl; |
| 606 } |
| 607 if (decl is VariableMirror && decl.isStatic) { |
| 608 var getterName = decl.simpleName; |
| 609 result[getterName] = |
| 610 new _SyntheticAccessor(this, getterName, true, true, false, decl); |
| 611 if (!decl.isFinal) { |
| 612 var setterName = _asSetter(decl.simpleName, this.owner); |
| 613 result[setterName] = new _SyntheticAccessor( |
| 614 this, setterName, false, true, false, decl); |
| 615 } |
| 616 } |
| 617 }); |
| 618 return _cachedStaticMembers = result; |
| 619 } |
| 620 |
| 621 var _cachedInstanceMembers; |
| 622 Map<Symbol, MethodMirror> get instanceMembers { |
| 623 if (_cachedInstanceMembers != null) return _cachedInstanceMembers; |
| 624 var result = new Map<Symbol, MethodMirror>(); |
| 625 if (superclass != null) { |
| 626 result.addAll(superclass.instanceMembers); |
| 627 } |
| 628 declarations.values.forEach((decl) { |
| 629 if (decl is MethodMirror && !decl.isStatic && |
| 630 !decl.isConstructor && !decl.isAbstract) { |
| 631 result[decl.simpleName] = decl; |
| 632 } |
| 633 if (decl is VariableMirror && !decl.isStatic) { |
| 634 var getterName = decl.simpleName; |
| 635 result[getterName] = |
| 636 new _SyntheticAccessor(this, getterName, true, false, false, decl); |
| 637 if (!decl.isFinal) { |
| 638 var setterName = _asSetter(decl.simpleName, this.owner); |
| 639 result[setterName] = new _SyntheticAccessor( |
| 640 this, setterName, false, false, false, decl); |
| 641 } |
| 642 } |
| 643 }); |
| 644 return _cachedInstanceMembers = result; |
| 645 } |
| 646 |
528 Map<Symbol, DeclarationMirror> _declarations; | 647 Map<Symbol, DeclarationMirror> _declarations; |
529 Map<Symbol, DeclarationMirror> get declarations { | 648 Map<Symbol, DeclarationMirror> get declarations { |
530 if (_declarations != null) return _declarations; | 649 if (_declarations != null) return _declarations; |
531 var decls = new Map<Symbol, DeclarationMirror>(); | 650 var decls = new Map<Symbol, DeclarationMirror>(); |
532 decls.addAll(members); | 651 decls.addAll(members); |
533 decls.addAll(constructors); | 652 decls.addAll(constructors); |
534 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 653 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
535 return _declarations = | 654 return _declarations = |
536 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 655 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
537 } | 656 } |
(...skipping 447 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
985 | 1104 |
986 String toString() => "TypedefMirror on '${_n(simpleName)}'"; | 1105 String toString() => "TypedefMirror on '${_n(simpleName)}'"; |
987 | 1106 |
988 static _nativeReferent(reflectedType) | 1107 static _nativeReferent(reflectedType) |
989 native "TypedefMirror_referent"; | 1108 native "TypedefMirror_referent"; |
990 | 1109 |
991 static _nativeDeclaration(reflectedType) | 1110 static _nativeDeclaration(reflectedType) |
992 native "TypedefMirror_declaration"; | 1111 native "TypedefMirror_declaration"; |
993 } | 1112 } |
994 | 1113 |
| 1114 Symbol _asSetter(Symbol getter, LibraryMirror library) { |
| 1115 var unwrapped = MirrorSystem.getName(getter); |
| 1116 return MirrorSystem.getSymbol('${unwrapped}=', library); |
| 1117 } |
| 1118 |
995 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl | 1119 class _LocalLibraryMirrorImpl extends _LocalObjectMirrorImpl |
996 implements LibraryMirror { | 1120 implements LibraryMirror { |
997 _LocalLibraryMirrorImpl(reflectee, | 1121 _LocalLibraryMirrorImpl(reflectee, |
998 String simpleName, | 1122 String simpleName, |
999 String url) | 1123 String url) |
1000 : this.simpleName = _s(simpleName), | 1124 : this.simpleName = _s(simpleName), |
1001 this.uri = Uri.parse(url), | 1125 this.uri = Uri.parse(url), |
1002 super(reflectee); | 1126 super(reflectee); |
1003 | 1127 |
1004 final Symbol simpleName; | 1128 final Symbol simpleName; |
| 1129 final Uri uri; |
1005 | 1130 |
1006 // The simple name and the qualified name are the same for a library. | 1131 // The simple name and the qualified name are the same for a library. |
1007 Symbol get qualifiedName => simpleName; | 1132 Symbol get qualifiedName => simpleName; |
1008 | 1133 |
1009 // Always null for libraries. | 1134 // Always null for libraries. |
1010 final DeclarationMirror owner = null; | 1135 final DeclarationMirror owner = null; |
1011 | 1136 |
1012 // Always false for libraries. | 1137 // Always false for libraries. |
1013 final bool isPrivate = false; | 1138 final bool isPrivate = false; |
1014 | 1139 |
1015 // Always false for libraries. | 1140 // Always false for libraries. |
1016 final bool isTopLevel = false; | 1141 final bool isTopLevel = false; |
1017 | 1142 |
1018 Type get _instantiator => null; | 1143 Type get _instantiator => null; |
1019 | 1144 |
1020 SourceLocation get location { | 1145 SourceLocation get location { |
1021 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1146 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
1022 } | 1147 } |
1023 | 1148 |
1024 final Uri uri; | 1149 var _cachedTopLevelMembers; |
| 1150 Map<Symbol, MethodMirror> get topLevelMembers { |
| 1151 if (_cachedTopLevelMembers != null) return _cachedTopLevelMembers; |
| 1152 var result = new Map<Symbol, MethodMirror>(); |
| 1153 declarations.values.forEach((decl) { |
| 1154 if (decl is MethodMirror && !decl.isAbstract) { |
| 1155 result[decl.simpleName] = decl; |
| 1156 } |
| 1157 if (decl is VariableMirror) { |
| 1158 var getterName = decl.simpleName; |
| 1159 result[getterName] = |
| 1160 new _SyntheticAccessor(this, getterName, true, true, true, decl); |
| 1161 if (!decl.isFinal) { |
| 1162 var setterName = _asSetter(decl.simpleName, this); |
| 1163 result[setterName] = new _SyntheticAccessor( |
| 1164 this, setterName, false, true, true, decl); |
| 1165 } |
| 1166 } |
| 1167 if (decl is TypeMirror) { |
| 1168 var getterName = decl.simpleName; |
| 1169 result[getterName] = new _SyntheticTypeGetter(this, getterName, decl); |
| 1170 } |
| 1171 }); |
| 1172 return _cachedTopLevelMembers = result; |
| 1173 } |
1025 | 1174 |
1026 Map<Symbol, DeclarationMirror> _declarations; | 1175 Map<Symbol, DeclarationMirror> _declarations; |
1027 Map<Symbol, DeclarationMirror> get declarations { | 1176 Map<Symbol, DeclarationMirror> get declarations { |
1028 if (_declarations != null) return _declarations; | 1177 if (_declarations != null) return _declarations; |
1029 return _declarations = | 1178 return _declarations = |
1030 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); | 1179 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); |
1031 } | 1180 } |
1032 | 1181 |
1033 Map<Symbol, Mirror> _members; | 1182 Map<Symbol, Mirror> _members; |
1034 Map<Symbol, Mirror> get members { | 1183 Map<Symbol, Mirror> get members { |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1148 final bool isStatic; | 1297 final bool isStatic; |
1149 final bool isAbstract; | 1298 final bool isAbstract; |
1150 final bool isGetter; | 1299 final bool isGetter; |
1151 final bool isSetter; | 1300 final bool isSetter; |
1152 final bool isConstructor; | 1301 final bool isConstructor; |
1153 final bool isConstConstructor; | 1302 final bool isConstConstructor; |
1154 final bool isGenerativeConstructor; | 1303 final bool isGenerativeConstructor; |
1155 final bool isRedirectingConstructor; | 1304 final bool isRedirectingConstructor; |
1156 final bool isFactoryConstructor; | 1305 final bool isFactoryConstructor; |
1157 final bool isOperator; | 1306 final bool isOperator; |
| 1307 bool get isSynthetic => false; |
1158 | 1308 |
1159 DeclarationMirror _owner; | 1309 DeclarationMirror _owner; |
1160 DeclarationMirror get owner { | 1310 DeclarationMirror get owner { |
1161 // For nested closures it is possible, that the mirror for the owner has not | 1311 // For nested closures it is possible, that the mirror for the owner has not |
1162 // been created yet. | 1312 // been created yet. |
1163 if (_owner == null) { | 1313 if (_owner == null) { |
1164 _owner = _MethodMirror_owner(_reflectee); | 1314 _owner = _MethodMirror_owner(_reflectee); |
1165 } | 1315 } |
1166 return _owner; | 1316 return _owner; |
1167 } | 1317 } |
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1453 if (typeMirror == null) { | 1603 if (typeMirror == null) { |
1454 typeMirror = makeLocalTypeMirror(key); | 1604 typeMirror = makeLocalTypeMirror(key); |
1455 _instanitationCache[key] = typeMirror; | 1605 _instanitationCache[key] = typeMirror; |
1456 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1606 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1457 _declarationCache[key] = typeMirror; | 1607 _declarationCache[key] = typeMirror; |
1458 } | 1608 } |
1459 } | 1609 } |
1460 return typeMirror; | 1610 return typeMirror; |
1461 } | 1611 } |
1462 } | 1612 } |
OLD | NEW |