| 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 // These values are allowed to be passed directly over the wire. | 9 // These values are allowed to be passed directly over the wire. |
| 10 bool _isSimpleValue(var value) { | 10 bool _isSimpleValue(var value) { |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 262 // TODO(ahe): This is a hack, see delegate below. | 262 // TODO(ahe): This is a hack, see delegate below. |
| 263 static Function _invokeOnClosure; | 263 static Function _invokeOnClosure; |
| 264 | 264 |
| 265 _LocalInstanceMirrorImpl(reflectee) : super(reflectee); | 265 _LocalInstanceMirrorImpl(reflectee) : super(reflectee); |
| 266 | 266 |
| 267 ClassMirror _type; | 267 ClassMirror _type; |
| 268 ClassMirror get type { | 268 ClassMirror get type { |
| 269 if (_type == null) { | 269 if (_type == null) { |
| 270 // Note it not safe to use reflectee.runtimeType because runtimeType may | 270 // Note it not safe to use reflectee.runtimeType because runtimeType may |
| 271 // be overridden. | 271 // be overridden. |
| 272 _type = _Mirrors._reflectType(_computeType(reflectee)); | 272 _type = reflectType(_computeType(reflectee)); |
| 273 } | 273 } |
| 274 return _type; | 274 return _type; |
| 275 } | 275 } |
| 276 | 276 |
| 277 // LocalInstanceMirrors always reflect local instances | 277 // LocalInstanceMirrors always reflect local instances |
| 278 bool hasReflectee = true; | 278 bool hasReflectee = true; |
| 279 | 279 |
| 280 get reflectee => _reflectee; | 280 get reflectee => _reflectee; |
| 281 | 281 |
| 282 delegate(Invocation invocation) { | 282 delegate(Invocation invocation) { |
| (...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 ClassMirror _trueSuperclassField; | 488 ClassMirror _trueSuperclassField; |
| 489 ClassMirror get _trueSuperclass { | 489 ClassMirror get _trueSuperclass { |
| 490 if (_trueSuperclassField == null) { | 490 if (_trueSuperclassField == null) { |
| 491 Type supertype = isOriginalDeclaration | 491 Type supertype = isOriginalDeclaration |
| 492 ? _supertype(_reflectedType) | 492 ? _supertype(_reflectedType) |
| 493 : _supertypeInstantiated(_reflectedType); | 493 : _supertypeInstantiated(_reflectedType); |
| 494 if (supertype == null) { | 494 if (supertype == null) { |
| 495 // Object has no superclass. | 495 // Object has no superclass. |
| 496 return null; | 496 return null; |
| 497 } | 497 } |
| 498 _trueSuperclassField = _Mirrors._reflectType(supertype); | 498 _trueSuperclassField = reflectType(supertype); |
| 499 _trueSuperclassField._instantiator = _instantiator; | 499 _trueSuperclassField._instantiator = _instantiator; |
| 500 } | 500 } |
| 501 return _trueSuperclassField; | 501 return _trueSuperclassField; |
| 502 } | 502 } |
| 503 ClassMirror get superclass { | 503 ClassMirror get superclass { |
| 504 return _isMixinTypedef ? _trueSuperclass._trueSuperclass : _trueSuperclass; | 504 return _isMixinTypedef ? _trueSuperclass._trueSuperclass : _trueSuperclass; |
| 505 } | 505 } |
| 506 | 506 |
| 507 var _superinterfaces; | 507 var _superinterfaces; |
| 508 List<ClassMirror> get superinterfaces { | 508 List<ClassMirror> get superinterfaces { |
| 509 if (_superinterfaces == null) { | 509 if (_superinterfaces == null) { |
| 510 _superinterfaces = isOriginalDeclaration | 510 _superinterfaces = isOriginalDeclaration |
| 511 ? _nativeInterfaces(_reflectedType) | 511 ? _nativeInterfaces(_reflectedType) |
| 512 : _nativeInterfacesInstantiated(_reflectedType); | 512 : _nativeInterfacesInstantiated(_reflectedType); |
| 513 _superinterfaces = _superinterfaces | 513 _superinterfaces = _superinterfaces |
| 514 .map((i) => _Mirrors._reflectType(i)).toList(growable:false); | 514 .map((i) => reflectType(i)).toList(growable:false); |
| 515 } | 515 } |
| 516 return _superinterfaces; | 516 return _superinterfaces; |
| 517 } | 517 } |
| 518 | 518 |
| 519 get _mixinApplicationName { | 519 get _mixinApplicationName { |
| 520 var mixins = new List<ClassMirror>(); | 520 var mixins = new List<ClassMirror>(); |
| 521 var klass = this; | 521 var klass = this; |
| 522 while (_nativeMixin(klass._reflectedType) != null) { | 522 while (_nativeMixin(klass._reflectedType) != null) { |
| 523 mixins.add(klass.mixin); | 523 mixins.add(klass.mixin); |
| 524 klass = klass.superclass; | 524 klass = klass.superclass; |
| 525 } | 525 } |
| 526 return _s( | 526 return _s( |
| 527 _n(klass.qualifiedName) | 527 _n(klass.qualifiedName) |
| 528 + ' with ' | 528 + ' with ' |
| 529 + mixins.reversed.map((m)=>_n(m.qualifiedName)).join(', ')); | 529 + mixins.reversed.map((m)=>_n(m.qualifiedName)).join(', ')); |
| 530 } | 530 } |
| 531 | 531 |
| 532 var _mixin; | 532 var _mixin; |
| 533 ClassMirror get mixin { | 533 ClassMirror get mixin { |
| 534 if (_mixin == null) { | 534 if (_mixin == null) { |
| 535 if (_isMixinTypedef) { | 535 if (_isMixinTypedef) { |
| 536 Type mixinType = isOriginalDeclaration | 536 Type mixinType = isOriginalDeclaration |
| 537 ? _nativeMixin(_trueSuperclass._reflectedType) | 537 ? _nativeMixin(_trueSuperclass._reflectedType) |
| 538 : _nativeMixinInstantiated(_trueSuperclass._reflectedType, _instanti
ator); | 538 : _nativeMixinInstantiated(_trueSuperclass._reflectedType, _instanti
ator); |
| 539 _mixin = _Mirrors._reflectType(mixinType); | 539 _mixin = reflectType(mixinType); |
| 540 } else { | 540 } else { |
| 541 Type mixinType = isOriginalDeclaration | 541 Type mixinType = isOriginalDeclaration |
| 542 ? _nativeMixin(_reflectedType) | 542 ? _nativeMixin(_reflectedType) |
| 543 : _nativeMixinInstantiated(_reflectedType, _instantiator); | 543 : _nativeMixinInstantiated(_reflectedType, _instantiator); |
| 544 if (mixinType == null) { | 544 if (mixinType == null) { |
| 545 // The reflectee is not a mixin application. | 545 // The reflectee is not a mixin application. |
| 546 _mixin = this; | 546 _mixin = this; |
| 547 } else { | 547 } else { |
| 548 _mixin = _Mirrors._reflectType(mixinType); | 548 _mixin = reflectType(mixinType); |
| 549 } | 549 } |
| 550 } | 550 } |
| 551 } | 551 } |
| 552 return _mixin; | 552 return _mixin; |
| 553 } | 553 } |
| 554 | 554 |
| 555 Map<Symbol, Mirror> _members; | 555 Map<Symbol, Mirror> _members; |
| 556 Map<Symbol, Mirror> get members { | 556 Map<Symbol, Mirror> get members { |
| 557 if (_members == null) { | 557 if (_members == null) { |
| 558 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; | 558 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; |
| (...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 779 if (_callMethod == null) { | 779 if (_callMethod == null) { |
| 780 _callMethod = this._FunctionTypeMirror_call_method(_reflectee); | 780 _callMethod = this._FunctionTypeMirror_call_method(_reflectee); |
| 781 } | 781 } |
| 782 return _callMethod; | 782 return _callMethod; |
| 783 } | 783 } |
| 784 | 784 |
| 785 TypeMirror _returnType = null; | 785 TypeMirror _returnType = null; |
| 786 TypeMirror get returnType { | 786 TypeMirror get returnType { |
| 787 if (_returnType == null) { | 787 if (_returnType == null) { |
| 788 _returnType = | 788 _returnType = |
| 789 _Mirrors._reflectType(_FunctionTypeMirror_return_type(_reflectee)); | 789 reflectType(_FunctionTypeMirror_return_type(_reflectee)); |
| 790 } | 790 } |
| 791 return _returnType; | 791 return _returnType; |
| 792 } | 792 } |
| 793 | 793 |
| 794 List<ParameterMirror> _parameters = null; | 794 List<ParameterMirror> _parameters = null; |
| 795 List<ParameterMirror> get parameters { | 795 List<ParameterMirror> get parameters { |
| 796 if (_parameters == null) { | 796 if (_parameters == null) { |
| 797 _parameters = _FunctionTypeMirror_parameters(_reflectee); | 797 _parameters = _FunctionTypeMirror_parameters(_reflectee); |
| 798 } | 798 } |
| 799 return _parameters; | 799 return _parameters; |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 865 final bool isTopLevel = false; | 865 final bool isTopLevel = false; |
| 866 | 866 |
| 867 SourceLocation get location { | 867 SourceLocation get location { |
| 868 throw new UnimplementedError( | 868 throw new UnimplementedError( |
| 869 'TypeVariableMirror.location is not implemented'); | 869 'TypeVariableMirror.location is not implemented'); |
| 870 } | 870 } |
| 871 | 871 |
| 872 TypeMirror _upperBound = null; | 872 TypeMirror _upperBound = null; |
| 873 TypeMirror get upperBound { | 873 TypeMirror get upperBound { |
| 874 if (_upperBound == null) { | 874 if (_upperBound == null) { |
| 875 _upperBound = | 875 _upperBound = reflectType(_TypeVariableMirror_upper_bound(_reflectee)); |
| 876 _Mirrors._reflectType(_TypeVariableMirror_upper_bound(_reflectee)); | |
| 877 } | 876 } |
| 878 return _upperBound; | 877 return _upperBound; |
| 879 } | 878 } |
| 880 | 879 |
| 881 bool get hasReflectedType => false; | 880 bool get hasReflectedType => false; |
| 882 Type get reflectedType => throw new UnsupportedError() ; | 881 Type get reflectedType => throw new UnsupportedError() ; |
| 883 | 882 |
| 884 List<TypeVariableMirror> get typeVariables => new UnmodifiableListView<TypeVar
iableMirror>(); | 883 List<TypeVariableMirror> get typeVariables => new UnmodifiableListView<TypeVar
iableMirror>(); |
| 885 List<TypeMirror> get typeArguments => new UnmodifiableListView<TypeMirror>(); | 884 List<TypeMirror> get typeArguments => new UnmodifiableListView<TypeMirror>(); |
| 886 | 885 |
| (...skipping 18 matching lines...) Expand all Loading... |
| 905 static Type _TypeVariableMirror_instantiate_from(reflectee, instantiator) | 904 static Type _TypeVariableMirror_instantiate_from(reflectee, instantiator) |
| 906 native "TypeVariableMirror_instantiate_from"; | 905 native "TypeVariableMirror_instantiate_from"; |
| 907 | 906 |
| 908 TypeMirror _instantiateInContextOf(declaration) { | 907 TypeMirror _instantiateInContextOf(declaration) { |
| 909 var instantiator = declaration; | 908 var instantiator = declaration; |
| 910 while (instantiator is MethodMirror) instantiator = instantiator.owner; | 909 while (instantiator is MethodMirror) instantiator = instantiator.owner; |
| 911 if (instantiator is LibraryMirror) return this; | 910 if (instantiator is LibraryMirror) return this; |
| 912 if (instantiator is! ClassMirror) throw "UNREACHABLE"; | 911 if (instantiator is! ClassMirror) throw "UNREACHABLE"; |
| 913 if (instantiator.isOriginalDeclaration) return this; | 912 if (instantiator.isOriginalDeclaration) return this; |
| 914 | 913 |
| 915 return _Mirrors._reflectType( | 914 return reflectType( |
| 916 _TypeVariableMirror_instantiate_from( | 915 _TypeVariableMirror_instantiate_from(_reflectee, |
| 917 _reflectee, | 916 instantiator._reflectedType)); |
| 918 instantiator._reflectedType)); | |
| 919 } | 917 } |
| 920 } | 918 } |
| 921 | 919 |
| 922 | 920 |
| 923 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl | 921 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl |
| 924 implements TypedefMirror { | 922 implements TypedefMirror { |
| 925 _LocalTypedefMirrorImpl(reflectee, | 923 _LocalTypedefMirrorImpl(reflectee, |
| 926 String simpleName, | 924 String simpleName, |
| 927 this._owner) | 925 this._owner) |
| 928 : super(reflectee, _s(simpleName)); | 926 : super(reflectee, _s(simpleName)); |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 995 final Uri uri; | 993 final Uri uri; |
| 996 | 994 |
| 997 Map<Symbol, Mirror> _members; | 995 Map<Symbol, Mirror> _members; |
| 998 Map<Symbol, Mirror> get members { | 996 Map<Symbol, Mirror> get members { |
| 999 if (_members == null) { | 997 if (_members == null) { |
| 1000 _members = _makeMemberMap(_computeMembers(_reflectee)); | 998 _members = _makeMemberMap(_computeMembers(_reflectee)); |
| 1001 } | 999 } |
| 1002 return _members; | 1000 return _members; |
| 1003 } | 1001 } |
| 1004 | 1002 |
| 1003 Map<Symbol, ClassMirror> _types; |
| 1004 Map<Symbol, TypeMirror> get types { |
| 1005 if (_types == null) { |
| 1006 _types = _filterMap(members, (key, value) => (value is TypeMirror)); |
| 1007 } |
| 1008 return _types; |
| 1009 } |
| 1010 |
| 1005 Map<Symbol, ClassMirror> _classes; | 1011 Map<Symbol, ClassMirror> _classes; |
| 1006 Map<Symbol, ClassMirror> get classes { | 1012 Map<Symbol, ClassMirror> get classes { |
| 1007 if (_classes == null) { | 1013 if (_classes == null) { |
| 1008 _classes = _filterMap(members, | 1014 _classes = _filterMap(members, |
| 1009 (key, value) => (value is ClassMirror)); | 1015 (key, value) => (value is ClassMirror)); |
| 1010 } | 1016 } |
| 1011 return _classes; | 1017 return _classes; |
| 1012 } | 1018 } |
| 1013 | 1019 |
| 1014 Map<Symbol, MethodMirror> _functions; | 1020 Map<Symbol, MethodMirror> _functions; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1121 SourceLocation get location { | 1127 SourceLocation get location { |
| 1122 throw new UnimplementedError('MethodMirror.location is not implemented'); | 1128 throw new UnimplementedError('MethodMirror.location is not implemented'); |
| 1123 } | 1129 } |
| 1124 | 1130 |
| 1125 TypeMirror _returnType = null; | 1131 TypeMirror _returnType = null; |
| 1126 TypeMirror get returnType { | 1132 TypeMirror get returnType { |
| 1127 if (_returnType == null) { | 1133 if (_returnType == null) { |
| 1128 if (isConstructor) { | 1134 if (isConstructor) { |
| 1129 _returnType = owner; | 1135 _returnType = owner; |
| 1130 } else { | 1136 } else { |
| 1131 _returnType = | 1137 _returnType = reflectType(_MethodMirror_return_type(_reflectee)); |
| 1132 _Mirrors._reflectType(_MethodMirror_return_type(_reflectee)); | |
| 1133 } | 1138 } |
| 1134 _returnType = _returnType._instantiateInContextOf(owner); | 1139 _returnType = _returnType._instantiateInContextOf(owner); |
| 1135 } | 1140 } |
| 1136 return _returnType; | 1141 return _returnType; |
| 1137 } | 1142 } |
| 1138 | 1143 |
| 1139 List<ParameterMirror> _parameters = null; | 1144 List<ParameterMirror> _parameters = null; |
| 1140 List<ParameterMirror> get parameters { | 1145 List<ParameterMirror> get parameters { |
| 1141 if (_parameters == null) { | 1146 if (_parameters == null) { |
| 1142 _parameters = _MethodMirror_parameters(_reflectee); | 1147 _parameters = _MethodMirror_parameters(_reflectee); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1218 | 1223 |
| 1219 bool get isTopLevel => owner is LibraryMirror; | 1224 bool get isTopLevel => owner is LibraryMirror; |
| 1220 | 1225 |
| 1221 SourceLocation get location { | 1226 SourceLocation get location { |
| 1222 throw new UnimplementedError('VariableMirror.location is not implemented'); | 1227 throw new UnimplementedError('VariableMirror.location is not implemented'); |
| 1223 } | 1228 } |
| 1224 | 1229 |
| 1225 TypeMirror _type; | 1230 TypeMirror _type; |
| 1226 TypeMirror get type { | 1231 TypeMirror get type { |
| 1227 if (_type == null) { | 1232 if (_type == null) { |
| 1228 _type = _Mirrors._reflectType(_VariableMirror_type(_reflectee)); | 1233 _type = reflectType(_VariableMirror_type(_reflectee)); |
| 1229 _type = _type._instantiateInContextOf(owner); | 1234 _type = _type._instantiateInContextOf(owner); |
| 1230 } | 1235 } |
| 1231 return _type; | 1236 return _type; |
| 1232 } | 1237 } |
| 1233 | 1238 |
| 1234 String toString() => "VariableMirror on '${MirrorSystem.getName(simpleName)}'"
; | 1239 String toString() => "VariableMirror on '${MirrorSystem.getName(simpleName)}'"
; |
| 1235 | 1240 |
| 1236 static _VariableMirror_type(reflectee) | 1241 static _VariableMirror_type(reflectee) |
| 1237 native "VariableMirror_type"; | 1242 native "VariableMirror_type"; |
| 1238 } | 1243 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1275 bool get hasDefaultValue => _defaultValueReflectee != null; | 1280 bool get hasDefaultValue => _defaultValueReflectee != null; |
| 1276 | 1281 |
| 1277 List<InstanceMirror> get metadata { | 1282 List<InstanceMirror> get metadata { |
| 1278 if ( _unmirroredMetadata == null) return const []; | 1283 if ( _unmirroredMetadata == null) return const []; |
| 1279 return _unmirroredMetadata.map(reflect).toList(growable:false); | 1284 return _unmirroredMetadata.map(reflect).toList(growable:false); |
| 1280 } | 1285 } |
| 1281 | 1286 |
| 1282 TypeMirror _type = null; | 1287 TypeMirror _type = null; |
| 1283 TypeMirror get type { | 1288 TypeMirror get type { |
| 1284 if (_type == null) { | 1289 if (_type == null) { |
| 1285 _type = | 1290 _type = reflectType(_ParameterMirror_type(_reflectee, _position)); |
| 1286 _Mirrors._reflectType(_ParameterMirror_type(_reflectee, _position)); | |
| 1287 _type = _type._instantiateInContextOf(owner); | 1291 _type = _type._instantiateInContextOf(owner); |
| 1288 } | 1292 } |
| 1289 return _type; | 1293 return _type; |
| 1290 } | 1294 } |
| 1291 | 1295 |
| 1292 String toString() => "ParameterMirror on '${_n(simpleName)}'"; | 1296 String toString() => "ParameterMirror on '${_n(simpleName)}'"; |
| 1293 | 1297 |
| 1294 static Type _ParameterMirror_type(_reflectee, _position) | 1298 static Type _ParameterMirror_type(_reflectee, _position) |
| 1295 native "ParameterMirror_type"; | 1299 native "ParameterMirror_type"; |
| 1296 } | 1300 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1367 ? new _LocalClosureMirrorImpl(reflectee) | 1371 ? new _LocalClosureMirrorImpl(reflectee) |
| 1368 : new _LocalInstanceMirrorImpl(reflectee); | 1372 : new _LocalInstanceMirrorImpl(reflectee); |
| 1369 } | 1373 } |
| 1370 | 1374 |
| 1371 static ClassMirror makeLocalClassMirror(Type key) | 1375 static ClassMirror makeLocalClassMirror(Type key) |
| 1372 native "Mirrors_makeLocalClassMirror"; | 1376 native "Mirrors_makeLocalClassMirror"; |
| 1373 static TypeMirror makeLocalTypeMirror(Type key) | 1377 static TypeMirror makeLocalTypeMirror(Type key) |
| 1374 native "Mirrors_makeLocalTypeMirror"; | 1378 native "Mirrors_makeLocalTypeMirror"; |
| 1375 | 1379 |
| 1376 static Expando<ClassMirror> _declarationCache = new Expando("ClassMirror"); | 1380 static Expando<ClassMirror> _declarationCache = new Expando("ClassMirror"); |
| 1377 static Expando<ClassMirror> _instanitationCache = new Expando("TypeMirror"); | 1381 static Expando<TypeMirror> _instanitationCache = new Expando("TypeMirror"); |
| 1378 | 1382 |
| 1379 static ClassMirror reflectClass(Type key) { | 1383 static ClassMirror reflectClass(Type key) { |
| 1380 var classMirror = _declarationCache[key]; | 1384 var classMirror = _declarationCache[key]; |
| 1381 if (classMirror == null) { | 1385 if (classMirror == null) { |
| 1382 classMirror = makeLocalClassMirror(key); | 1386 classMirror = makeLocalClassMirror(key); |
| 1383 _declarationCache[key] = classMirror; | 1387 _declarationCache[key] = classMirror; |
| 1384 if (!classMirror._isGeneric) { | 1388 if (!classMirror._isGeneric) { |
| 1385 _instanitationCache[key] = classMirror; | 1389 _instanitationCache[key] = classMirror; |
| 1386 } | 1390 } |
| 1387 } | 1391 } |
| 1388 return classMirror; | 1392 return classMirror; |
| 1389 } | 1393 } |
| 1390 | 1394 |
| 1391 static TypeMirror _reflectType(Type key) { | 1395 static TypeMirror reflectType(Type key) { |
| 1392 var typeMirror = _instanitationCache[key]; | 1396 var typeMirror = _instanitationCache[key]; |
| 1393 if (typeMirror == null) { | 1397 if (typeMirror == null) { |
| 1394 typeMirror = makeLocalTypeMirror(key); | 1398 typeMirror = makeLocalTypeMirror(key); |
| 1395 _instanitationCache[key] = typeMirror; | 1399 _instanitationCache[key] = typeMirror; |
| 1396 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1400 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
| 1397 _declarationCache[key] = typeMirror; | 1401 _declarationCache[key] = typeMirror; |
| 1398 } | 1402 } |
| 1399 } | 1403 } |
| 1400 return typeMirror; | 1404 return typeMirror; |
| 1401 } | 1405 } |
| 1402 } | 1406 } |
| OLD | NEW |