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 => null; |
| 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 |
163 abstract class _LocalObjectMirror extends _LocalMirror implements ObjectMirror { | 227 abstract class _LocalObjectMirror extends _LocalMirror implements ObjectMirror { |
164 final _reflectee; // May be a MirrorReference or an ordinary object. | 228 final _reflectee; // May be a MirrorReference or an ordinary object. |
165 | 229 |
166 _LocalObjectMirror(this._reflectee); | 230 _LocalObjectMirror(this._reflectee); |
167 | 231 |
168 InstanceMirror invoke(Symbol memberName, | 232 InstanceMirror invoke(Symbol memberName, |
169 List positionalArguments, | 233 List positionalArguments, |
170 [Map<Symbol, dynamic> namedArguments]) { | 234 [Map<Symbol, dynamic> namedArguments]) { |
171 int numPositionalArguments = positionalArguments.length; | 235 int numPositionalArguments = positionalArguments.length; |
172 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; | 236 int numNamedArguments = namedArguments != null ? namedArguments.length : 0; |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 // The reflectee is not a mixin application. | 549 // The reflectee is not a mixin application. |
486 _mixin = this; | 550 _mixin = this; |
487 } else { | 551 } else { |
488 _mixin = reflectType(mixinType); | 552 _mixin = reflectType(mixinType); |
489 } | 553 } |
490 } | 554 } |
491 } | 555 } |
492 return _mixin; | 556 return _mixin; |
493 } | 557 } |
494 | 558 |
| 559 var _cachedStaticMembers; |
| 560 Map<Symbol, MethodMirror> get staticMembers { |
| 561 if (_cachedStaticMembers == null) { |
| 562 var result = new Map<Symbol, MethodMirror>(); |
| 563 declarations.values.forEach((decl) { |
| 564 if (decl is MethodMirror && decl.isStatic && |
| 565 !decl.isConstructor && !decl.isAbstract) { |
| 566 result[decl.simpleName] = decl; |
| 567 } |
| 568 if (decl is VariableMirror && decl.isStatic) { |
| 569 var getterName = decl.simpleName; |
| 570 result[getterName] = |
| 571 new _SyntheticAccessor(this, getterName, true, true, false, decl); |
| 572 if (!decl.isFinal) { |
| 573 var setterName = _asSetter(decl.simpleName, this.owner); |
| 574 result[setterName] = new _SyntheticAccessor( |
| 575 this, setterName, false, true, false, decl); |
| 576 } |
| 577 } |
| 578 }); |
| 579 _cachedStaticMembers = result; |
| 580 } |
| 581 return _cachedStaticMembers; |
| 582 } |
| 583 |
| 584 var _cachedInstanceMembers; |
| 585 Map<Symbol, MethodMirror> get instanceMembers { |
| 586 if (_cachedInstanceMembers == null) { |
| 587 var result = new Map<Symbol, MethodMirror>(); |
| 588 if (superclass != null) { |
| 589 result.addAll(superclass.instanceMembers); |
| 590 } |
| 591 declarations.values.forEach((decl) { |
| 592 if (decl is MethodMirror && !decl.isStatic && |
| 593 !decl.isConstructor && !decl.isAbstract) { |
| 594 result[decl.simpleName] = decl; |
| 595 } |
| 596 if (decl is VariableMirror && !decl.isStatic) { |
| 597 var getterName = decl.simpleName; |
| 598 result[getterName] = |
| 599 new _SyntheticAccessor(this, getterName, true, false, false, decl)
; |
| 600 if (!decl.isFinal) { |
| 601 var setterName = _asSetter(decl.simpleName, this.owner); |
| 602 result[setterName] = new _SyntheticAccessor( |
| 603 this, setterName, false, false, false, decl); |
| 604 } |
| 605 } |
| 606 }); |
| 607 _cachedInstanceMembers = result; |
| 608 } |
| 609 return _cachedInstanceMembers; |
| 610 } |
| 611 |
495 Map<Symbol, DeclarationMirror> _declarations; | 612 Map<Symbol, DeclarationMirror> _declarations; |
496 Map<Symbol, DeclarationMirror> get declarations { | 613 Map<Symbol, DeclarationMirror> get declarations { |
497 if (_declarations != null) return _declarations; | 614 if (_declarations != null) return _declarations; |
498 var decls = new Map<Symbol, DeclarationMirror>(); | 615 var decls = new Map<Symbol, DeclarationMirror>(); |
499 decls.addAll(_members); | 616 decls.addAll(_members); |
500 decls.addAll(_constructors); | 617 decls.addAll(_constructors); |
501 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 618 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
502 return _declarations = | 619 return _declarations = |
503 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 620 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
504 } | 621 } |
(...skipping 406 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
911 | 1028 |
912 String toString() => "TypedefMirror on '${_n(simpleName)}'"; | 1029 String toString() => "TypedefMirror on '${_n(simpleName)}'"; |
913 | 1030 |
914 static _nativeReferent(reflectedType) | 1031 static _nativeReferent(reflectedType) |
915 native "TypedefMirror_referent"; | 1032 native "TypedefMirror_referent"; |
916 | 1033 |
917 static _nativeDeclaration(reflectedType) | 1034 static _nativeDeclaration(reflectedType) |
918 native "TypedefMirror_declaration"; | 1035 native "TypedefMirror_declaration"; |
919 } | 1036 } |
920 | 1037 |
921 class _LocalLibraryMirror extends _LocalObjectMirror | 1038 Symbol _asSetter(Symbol getter, LibraryMirror library) { |
922 implements LibraryMirror { | 1039 var unwrapped = MirrorSystem.getName(getter); |
| 1040 return MirrorSystem.getSymbol('${unwrapped}=', library); |
| 1041 } |
| 1042 |
| 1043 class _LocalLibraryMirror extends _LocalObjectMirror implements LibraryMirror { |
923 final Symbol simpleName; | 1044 final Symbol simpleName; |
924 final Uri uri; | 1045 final Uri uri; |
925 | 1046 |
926 _LocalLibraryMirror(reflectee, | 1047 _LocalLibraryMirror(reflectee, |
927 String simpleName, | 1048 String simpleName, |
928 String url) | 1049 String url) |
929 : this.simpleName = _s(simpleName), | 1050 : this.simpleName = _s(simpleName), |
930 this.uri = Uri.parse(url), | 1051 this.uri = Uri.parse(url), |
931 super(reflectee); | 1052 super(reflectee); |
932 | 1053 |
(...skipping 11 matching lines...) Expand all Loading... |
944 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1065 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
945 } | 1066 } |
946 | 1067 |
947 Map<Symbol, DeclarationMirror> _declarations; | 1068 Map<Symbol, DeclarationMirror> _declarations; |
948 Map<Symbol, DeclarationMirror> get declarations { | 1069 Map<Symbol, DeclarationMirror> get declarations { |
949 if (_declarations != null) return _declarations; | 1070 if (_declarations != null) return _declarations; |
950 return _declarations = | 1071 return _declarations = |
951 new _UnmodifiableMapView<Symbol, DeclarationMirror>(_members); | 1072 new _UnmodifiableMapView<Symbol, DeclarationMirror>(_members); |
952 } | 1073 } |
953 | 1074 |
| 1075 Map<Symbol, MethodMirror> get topLevelMembers { |
| 1076 throw new UnimplementedError( |
| 1077 'LibraryMirror.topLevelMembers is not implemented'); |
| 1078 } |
| 1079 |
954 Map<Symbol, Mirror> _cachedMembers; | 1080 Map<Symbol, Mirror> _cachedMembers; |
955 Map<Symbol, Mirror> get _members { | 1081 Map<Symbol, Mirror> get _members { |
956 if (_cachedMembers == null) { | 1082 if (_cachedMembers == null) { |
957 _cachedMembers = _makeMemberMap(_computeMembers(_reflectee)); | 1083 _cachedMembers = _makeMemberMap(_computeMembers(_reflectee)); |
958 } | 1084 } |
959 return _cachedMembers; | 1085 return _cachedMembers; |
960 } | 1086 } |
961 | 1087 |
962 Map<Symbol, MethodMirror> _cachedFunctions; | 1088 Map<Symbol, MethodMirror> _cachedFunctions; |
963 Map<Symbol, MethodMirror> get _functions { | 1089 Map<Symbol, MethodMirror> get _functions { |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
998 _invokeGetter(reflectee, getterName) | 1124 _invokeGetter(reflectee, getterName) |
999 native 'LibraryMirror_invokeGetter'; | 1125 native 'LibraryMirror_invokeGetter'; |
1000 | 1126 |
1001 _invokeSetter(reflectee, setterName, value) | 1127 _invokeSetter(reflectee, setterName, value) |
1002 native 'LibraryMirror_invokeSetter'; | 1128 native 'LibraryMirror_invokeSetter'; |
1003 | 1129 |
1004 _computeMembers(reflectee) | 1130 _computeMembers(reflectee) |
1005 native "LibraryMirror_members"; | 1131 native "LibraryMirror_members"; |
1006 } | 1132 } |
1007 | 1133 |
1008 class _LocalMethodMirror extends _LocalDeclarationMirror | 1134 class _LocalMethodMirror extends _LocalDeclarationMirror |
1009 implements MethodMirror { | 1135 implements MethodMirror { |
1010 final bool isStatic; | 1136 final bool isStatic; |
1011 final bool isAbstract; | 1137 final bool isAbstract; |
1012 final bool isGetter; | 1138 final bool isGetter; |
1013 final bool isSetter; | 1139 final bool isSetter; |
1014 final bool isConstructor; | 1140 final bool isConstructor; |
1015 final bool isConstConstructor; | 1141 final bool isConstConstructor; |
1016 final bool isGenerativeConstructor; | 1142 final bool isGenerativeConstructor; |
1017 final bool isRedirectingConstructor; | 1143 final bool isRedirectingConstructor; |
1018 final bool isFactoryConstructor; | 1144 final bool isFactoryConstructor; |
(...skipping 24 matching lines...) Expand all Loading... |
1043 if (_owner == null) { | 1169 if (_owner == null) { |
1044 _owner = _MethodMirror_owner(_reflectee); | 1170 _owner = _MethodMirror_owner(_reflectee); |
1045 } | 1171 } |
1046 return _owner; | 1172 return _owner; |
1047 } | 1173 } |
1048 | 1174 |
1049 bool get isPrivate => _n(simpleName).startsWith('_') || | 1175 bool get isPrivate => _n(simpleName).startsWith('_') || |
1050 _n(constructorName).startsWith('_'); | 1176 _n(constructorName).startsWith('_'); |
1051 | 1177 |
1052 bool get isTopLevel => owner is LibraryMirror; | 1178 bool get isTopLevel => owner is LibraryMirror; |
| 1179 bool get isSynthetic => false; |
1053 | 1180 |
1054 SourceLocation get location { | 1181 SourceLocation get location { |
1055 throw new UnimplementedError('MethodMirror.location is not implemented'); | 1182 throw new UnimplementedError('MethodMirror.location is not implemented'); |
1056 } | 1183 } |
1057 | 1184 |
1058 Type get _instantiator { | 1185 Type get _instantiator { |
1059 var o = owner; | 1186 var o = owner; |
1060 while (o is MethodMirror) o = o.owner; | 1187 while (o is MethodMirror) o = o.owner; |
1061 return o._instantiator; | 1188 return o._instantiator; |
1062 } | 1189 } |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1336 if (typeMirror == null) { | 1463 if (typeMirror == null) { |
1337 typeMirror = makeLocalTypeMirror(key); | 1464 typeMirror = makeLocalTypeMirror(key); |
1338 _instanitationCache[key] = typeMirror; | 1465 _instanitationCache[key] = typeMirror; |
1339 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1466 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1340 _declarationCache[key] = typeMirror; | 1467 _declarationCache[key] = typeMirror; |
1341 } | 1468 } |
1342 } | 1469 } |
1343 return typeMirror; | 1470 return typeMirror; |
1344 } | 1471 } |
1345 } | 1472 } |
OLD | NEW |