Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(89)

Side by Side Diff: runtime/lib/mirrors_impl.dart

Issue 1080393006: Mirrors memory usage tweaks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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" show UnmodifiableListView, UnmodifiableMapView; 7 import "dart:collection" show UnmodifiableListView, UnmodifiableMapView;
8 8
9 final emptyList = new UnmodifiableListView([]); 9 final emptyList = new UnmodifiableListView([]);
10 final emptyMap = new UnmodifiableMapView({}); 10 final emptyMap = new UnmodifiableMapView({});
11 11
12 class _InternalMirrorError { 12 class _InternalMirrorError {
13 final String _msg; 13 final String _msg;
14 const _InternalMirrorError(String this._msg); 14 const _InternalMirrorError(String this._msg);
15 String toString() => _msg; 15 String toString() => _msg;
16 } 16 }
17 17
18 Map _makeMemberMap(List mirrors) {
19 return new UnmodifiableMapView<Symbol, DeclarationMirror>(
20 new Map<Symbol, DeclarationMirror>.fromIterable(
21 mirrors, key: (e) => e.simpleName));
22 }
23
24 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol); 18 String _n(Symbol symbol) => _symbol_dev.Symbol.getName(symbol);
25 19
26 Symbol _s(String name) { 20 Symbol _s(String name) {
27 if (name == null) return null; 21 if (name == null) return null;
28 return new _symbol_dev.Symbol.unvalidated(name); 22 return new _symbol_dev.Symbol.unvalidated(name);
29 } 23 }
30 24
31 Symbol _computeQualifiedName(DeclarationMirror owner, Symbol simpleName) { 25 Symbol _computeQualifiedName(DeclarationMirror owner, Symbol simpleName) {
32 if (owner == null) return simpleName; 26 if (owner == null) return simpleName;
33 return _s('${_n(owner.qualifiedName)}.${_n(simpleName)}'); 27 return _s('${_n(owner.qualifiedName)}.${_n(simpleName)}');
(...skipping 581 matching lines...) Expand 10 before | Expand all | Expand 10 after
615 Symbol _qualifiedName = null; 609 Symbol _qualifiedName = null;
616 Symbol get qualifiedName { 610 Symbol get qualifiedName {
617 if (_qualifiedName == null) { 611 if (_qualifiedName == null) {
618 _qualifiedName = _computeQualifiedName(owner, simpleName); 612 _qualifiedName = _computeQualifiedName(owner, simpleName);
619 } 613 }
620 return _qualifiedName; 614 return _qualifiedName;
621 } 615 }
622 616
623 DeclarationMirror get owner { 617 DeclarationMirror get owner {
624 if (_owner == null) { 618 if (_owner == null) {
625 _owner = _library(_reflectee); 619 var uri = _LocalClassMirror._libraryUri(_reflectee);
620 _owner = currentMirrorSystem().libraries[Uri.parse(uri)];
626 } 621 }
627 return _owner; 622 return _owner;
628 } 623 }
629 624
630 bool get isPrivate => _n(simpleName).startsWith('_'); 625 bool get isPrivate => _n(simpleName).startsWith('_');
631 626
632 final bool isTopLevel = true; 627 bool get isTopLevel => true;
633 628
634 SourceLocation get location { 629 SourceLocation get location {
635 return _location(_reflectee); 630 return _location(_reflectee);
636 } 631 }
637 632
638 ClassMirror _trueSuperclassField; 633 ClassMirror _trueSuperclassField;
639 ClassMirror get _trueSuperclass { 634 ClassMirror get _trueSuperclass {
640 if (_trueSuperclassField == null) { 635 if (_trueSuperclassField == null) {
641 Type supertype = isOriginalDeclaration 636 Type supertype = isOriginalDeclaration
642 ? _supertype(_reflectedType) 637 ? _supertype(_reflectedType)
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after
750 }); 745 });
751 _cachedInstanceMembers = 746 _cachedInstanceMembers =
752 new UnmodifiableMapView<Symbol, MethodMirror>(result); 747 new UnmodifiableMapView<Symbol, MethodMirror>(result);
753 } 748 }
754 return _cachedInstanceMembers; 749 return _cachedInstanceMembers;
755 } 750 }
756 751
757 Map<Symbol, DeclarationMirror> _declarations; 752 Map<Symbol, DeclarationMirror> _declarations;
758 Map<Symbol, DeclarationMirror> get declarations { 753 Map<Symbol, DeclarationMirror> get declarations {
759 if (_declarations != null) return _declarations; 754 if (_declarations != null) return _declarations;
755
760 var decls = new Map<Symbol, DeclarationMirror>(); 756 var decls = new Map<Symbol, DeclarationMirror>();
761 decls.addAll(_members); 757
762 decls.addAll(_constructors); 758 var whoseMembers = _isMixinAlias ? _trueSuperclass : this;
763 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); 759 var members = mixin._computeMembers(_instantiator,
760 whoseMembers.mixin._reflectee);
761 for (var member in members) {
762 decls[member.simpleName] = member;
763 }
764
765 var constructors = _computeConstructors(_instantiator, _reflectee);
766 var stringName = _n(simpleName);
767 for (var constructor in constructors) {
768 constructor._patchConstructorName(stringName);
769 decls[constructor.simpleName] = constructor;
770 }
771
772 for (var typeVariable in typeVariables) {
773 decls[typeVariable.simpleName] = typeVariable;
774 }
775
764 return _declarations = 776 return _declarations =
765 new UnmodifiableMapView<Symbol, DeclarationMirror>(decls); 777 new UnmodifiableMapView<Symbol, DeclarationMirror>(decls);
766 } 778 }
767 779
768 Map<Symbol, Mirror> _cachedMembers;
769 Map<Symbol, Mirror> get _members {
770 if (_cachedMembers == null) {
771 var whoseMembers = _isMixinAlias ? _trueSuperclass : this;
772 _cachedMembers = _makeMemberMap(mixin._computeMembers(
773 _instantiator, whoseMembers.mixin._reflectee));
774 }
775 return _cachedMembers;
776 }
777
778 Map<Symbol, MethodMirror> _cachedConstructors;
779 Map<Symbol, MethodMirror> get _constructors {
780 if (_cachedConstructors == null) {
781 var constructorsList = _computeConstructors(_instantiator, _reflectee);
782 var stringName = _n(simpleName);
783 constructorsList.forEach((c) => c._patchConstructorName(stringName));
784 _cachedConstructors =
785 new Map.fromIterable(constructorsList, key: (e) => e.simpleName);
786 }
787 return _cachedConstructors;
788 }
789
790 bool get _isAnonymousMixinApplication { 780 bool get _isAnonymousMixinApplication {
791 if (_isMixinAlias) return false; // Named mixin application. 781 if (_isMixinAlias) return false; // Named mixin application.
792 if (mixin == this) return false; // Not a mixin application. 782 if (mixin == this) return false; // Not a mixin application.
793 return true; 783 return true;
794 } 784 }
795 785
796 List<TypeVariableMirror> _typeVariables = null; 786 List<TypeVariableMirror> _typeVariables = null;
797 List<TypeVariableMirror> get typeVariables { 787 List<TypeVariableMirror> get typeVariables {
798 if (_typeVariables == null) { 788 if (_typeVariables == null) {
799 if (_isAnonymousMixinApplication) return _typeVariables = emptyList; 789 if (_isAnonymousMixinApplication) return _typeVariables = emptyList;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
897 ClassMirror otherDeclaration = other.originalDeclaration; 887 ClassMirror otherDeclaration = other.originalDeclaration;
898 ClassMirror c = this; 888 ClassMirror c = this;
899 while (c != null) { 889 while (c != null) {
900 c = c.originalDeclaration; 890 c = c.originalDeclaration;
901 if (c == otherDeclaration) return true; 891 if (c == otherDeclaration) return true;
902 c = c.superclass; 892 c = c.superclass;
903 } 893 }
904 return false; 894 return false;
905 } 895 }
906 896
907 static _library(reflectee) 897 static _libraryUri(reflectee)
908 native "ClassMirror_library"; 898 native "ClassMirror_libraryUri";
909 899
910 static _supertype(reflectedType) 900 static _supertype(reflectedType)
911 native "ClassMirror_supertype"; 901 native "ClassMirror_supertype";
912 902
913 static _supertypeInstantiated(reflectedType) 903 static _supertypeInstantiated(reflectedType)
914 native "ClassMirror_supertype_instantiated"; 904 native "ClassMirror_supertype_instantiated";
915 905
916 static _nativeInterfaces(reflectedType) 906 static _nativeInterfaces(reflectedType)
917 native "ClassMirror_interfaces"; 907 native "ClassMirror_interfaces";
918 908
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1127 this._isGeneric, 1117 this._isGeneric,
1128 this._isGenericDeclaration, 1118 this._isGenericDeclaration,
1129 this._owner) 1119 this._owner)
1130 : super(reflectee, _s(simpleName)); 1120 : super(reflectee, _s(simpleName));
1131 1121
1132 bool get isTopLevel => true; 1122 bool get isTopLevel => true;
1133 1123
1134 DeclarationMirror _owner; 1124 DeclarationMirror _owner;
1135 DeclarationMirror get owner { 1125 DeclarationMirror get owner {
1136 if (_owner == null) { 1126 if (_owner == null) {
1137 _owner = _LocalClassMirror._library(_reflectee); 1127 var uri = _LocalClassMirror._libraryUri(_reflectee);
1128 _owner = currentMirrorSystem().libraries[Uri.parse(uri)];
1138 } 1129 }
1139 return _owner; 1130 return _owner;
1140 } 1131 }
1141 1132
1142 TypeMirror _referent = null; 1133 TypeMirror _referent = null;
1143 TypeMirror get referent { 1134 TypeMirror get referent {
1144 if (_referent == null) { 1135 if (_referent == null) {
1145 _referent = _nativeReferent(_reflectedType); 1136 _referent = _nativeReferent(_reflectedType);
1146 _referent._instantiator = _reflectedType; 1137 _referent._instantiator = _reflectedType;
1147 } 1138 }
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after
1240 DeclarationMirror get owner => null; 1231 DeclarationMirror get owner => null;
1241 1232
1242 bool get isPrivate => false; 1233 bool get isPrivate => false;
1243 bool get isTopLevel => false; 1234 bool get isTopLevel => false;
1244 1235
1245 Type get _instantiator => null; 1236 Type get _instantiator => null;
1246 1237
1247 Map<Symbol, DeclarationMirror> _declarations; 1238 Map<Symbol, DeclarationMirror> _declarations;
1248 Map<Symbol, DeclarationMirror> get declarations { 1239 Map<Symbol, DeclarationMirror> get declarations {
1249 if (_declarations != null) return _declarations; 1240 if (_declarations != null) return _declarations;
1241
1242 var decls = new Map<Symbol, DeclarationMirror>();
1243 var members = _computeMembers(_reflectee);
1244 for (var member in members) {
1245 decls[member.simpleName] = member;
1246 }
1247
1250 return _declarations = 1248 return _declarations =
1251 new UnmodifiableMapView<Symbol, DeclarationMirror>(_members); 1249 new UnmodifiableMapView<Symbol, DeclarationMirror>(decls);
1252 }
1253
1254 Map<Symbol, Mirror> _cachedMembers;
1255 Map<Symbol, Mirror> get _members {
1256 if (_cachedMembers == null) {
1257 _cachedMembers = _makeMemberMap(_computeMembers(_reflectee));
1258 }
1259 return _cachedMembers;
1260 } 1250 }
1261 1251
1262 SourceLocation get location { 1252 SourceLocation get location {
1263 return _location(_reflectee); 1253 return _location(_reflectee);
1264 } 1254 }
1265 1255
1266 List<InstanceMirror> get metadata { 1256 List<InstanceMirror> get metadata {
1267 // Get the metadata objects, convert them into InstanceMirrors using 1257 // Get the metadata objects, convert them into InstanceMirrors using
1268 // reflect() and then make them into a Dart list. 1258 // reflect() and then make them into a Dart list.
1269 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); 1259 return new UnmodifiableListView(_metadata(_reflectee).map(reflect));
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
1332 _LocalCombinatorMirror(identifierString, this.isShow) 1322 _LocalCombinatorMirror(identifierString, this.isShow)
1333 : this.identifiers = [_s(identifierString)]; 1323 : this.identifiers = [_s(identifierString)];
1334 1324
1335 bool get isHide => !isShow; 1325 bool get isHide => !isShow;
1336 } 1326 }
1337 1327
1338 class _LocalMethodMirror extends _LocalDeclarationMirror 1328 class _LocalMethodMirror extends _LocalDeclarationMirror
1339 implements MethodMirror { 1329 implements MethodMirror {
1340 final Type _instantiator; 1330 final Type _instantiator;
1341 final bool isStatic; 1331 final bool isStatic;
1342 final bool isAbstract; 1332 final int _kindFlags;
1343 final bool isGetter;
1344 final bool isSetter;
1345 final bool isConstructor;
1346 final bool isConstConstructor;
1347 final bool isGenerativeConstructor;
1348 final bool isRedirectingConstructor;
1349 final bool isFactoryConstructor;
1350 final bool isOperator;
1351
1352 static const _operators = const ["%", "&", "*", "+", "-", "/", "<", "<<",
1353 "<=", "==", ">", ">=", ">>", "[]", "[]=", "^", "|", "~", "unary-", "~/"];
1354 1333
1355 _LocalMethodMirror(reflectee, 1334 _LocalMethodMirror(reflectee,
1356 String simpleName, 1335 String simpleName,
1357 this._owner, 1336 this._owner,
1358 this._instantiator, 1337 this._instantiator,
1359 this.isStatic, 1338 this.isStatic,
1360 this.isAbstract, 1339 this._kindFlags)
1361 this.isGetter, 1340 : super(reflectee, _s(simpleName));
1362 this.isSetter, 1341
1363 this.isConstructor, 1342 static const kAbstract = 0;
1364 this.isConstConstructor, 1343 static const kGetter = 1;
1365 this.isGenerativeConstructor, 1344 static const kSetter = 2;
1366 this.isRedirectingConstructor, 1345 static const kConstructor = 3;
1367 this.isFactoryConstructor) 1346 static const kConstCtor = 4;
1368 : this.isOperator = _operators.contains(simpleName), 1347 static const kGenerativeCtor = 5;
1369 super(reflectee, _s(simpleName)); 1348 static const kRedirectingCtor = 6;
1349 static const kFactoryCtor = 7;
1350
1351 // These offsets much be kept in sync with those in mirrors.h.
1352 bool get isAbstract => 0 != (_kindFlags & (1 << kAbstract));
1353 bool get isGetter => 0 != (_kindFlags & (1 << kGetter));
1354 bool get isSetter => 0 != (_kindFlags & (1 << kSetter));
1355 bool get isConstructor => 0 != (_kindFlags & (1 << kConstructor));
1356 bool get isConstConstructor => 0 != (_kindFlags & (1 << kConstCtor));
1357 bool get isGenerativeConstructor => 0 != (_kindFlags & (1 << kGenerativeCtor) );
1358 bool get isRedirectingConstructor => 0 != (_kindFlags & (1 << kRedirectingCtor ));
1359 bool get isFactoryConstructor => 0 != (_kindFlags & (1 << kFactoryCtor));
1360
1361 static const _operators = const ["%", "&", "*", "+", "-", "/", "<", "<<",
1362 "<=", "==", ">", ">=", ">>", "[]", "[]=", "^", "|", "~", "unary-", "~/"];
1363 bool get isOperator => _operators.contains(_n(simpleName));
1370 1364
1371 DeclarationMirror _owner; 1365 DeclarationMirror _owner;
1372 DeclarationMirror get owner { 1366 DeclarationMirror get owner {
1373 // For nested closures it is possible, that the mirror for the owner has not 1367 // For nested closures it is possible, that the mirror for the owner has not
1374 // been created yet. 1368 // been created yet.
1375 if (_owner == null) { 1369 if (_owner == null) {
1376 _owner = _MethodMirror_owner(_reflectee, _instantiator); 1370 _owner = _MethodMirror_owner(_reflectee, _instantiator);
1377 } 1371 }
1378 return _owner; 1372 return _owner;
1379 } 1373 }
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
1423 LibraryMirror definingLibrary = owner.owner; 1417 LibraryMirror definingLibrary = owner.owner;
1424 _constructorName = MirrorSystem.getSymbol(parts[1], definingLibrary); 1418 _constructorName = MirrorSystem.getSymbol(parts[1], definingLibrary);
1425 } else { 1419 } else {
1426 _constructorName = _s(''); 1420 _constructorName = _s('');
1427 } 1421 }
1428 } 1422 }
1429 } 1423 }
1430 return _constructorName; 1424 return _constructorName;
1431 } 1425 }
1432 1426
1433 String _source = null; 1427 String get source => _MethodMirror_source(_reflectee);
1434 String get source {
1435 if (_source == null) {
1436 _source = _MethodMirror_source(_reflectee);
1437 }
1438 return _source;
1439 }
1440 1428
1441 void _patchConstructorName(ownerName) { 1429 void _patchConstructorName(ownerName) {
1442 var cn = _n(constructorName); 1430 var cn = _n(constructorName);
1443 if(cn == ''){ 1431 if (cn == '') {
1444 _simpleName = _s(ownerName); 1432 _simpleName = _s(ownerName);
1445 } else { 1433 } else {
1446 _simpleName = _s(ownerName + "." + cn); 1434 _simpleName = _s(ownerName + "." + cn);
1447 } 1435 }
1448 } 1436 }
1449 1437
1450 String toString() => "MethodMirror on '${MirrorSystem.getName(simpleName)}'"; 1438 String toString() => "MethodMirror on '${MirrorSystem.getName(simpleName)}'";
1451 1439
1452 static dynamic _MethodMirror_owner(reflectee, instantiator) 1440 static dynamic _MethodMirror_owner(reflectee, instantiator)
1453 native "MethodMirror_owner"; 1441 native "MethodMirror_owner";
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
1662 if (typeMirror == null) { 1650 if (typeMirror == null) {
1663 typeMirror = makeLocalTypeMirror(key); 1651 typeMirror = makeLocalTypeMirror(key);
1664 _instanitationCache[key] = typeMirror; 1652 _instanitationCache[key] = typeMirror;
1665 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { 1653 if (typeMirror is ClassMirror && !typeMirror._isGeneric) {
1666 _declarationCache[key] = typeMirror; 1654 _declarationCache[key] = typeMirror;
1667 } 1655 }
1668 } 1656 }
1669 return typeMirror; 1657 return typeMirror;
1670 } 1658 }
1671 } 1659 }
OLDNEW
« no previous file with comments | « runtime/lib/mirrors.cc ('k') | runtime/vm/bootstrap_natives.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698