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 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 | 265 |
266 int get hashCode { | 266 int get hashCode { |
267 // Avoid hash collisions with the reflectee. This constant is in Smi range | 267 // Avoid hash collisions with the reflectee. This constant is in Smi range |
268 // and happens to be the inner padding from RFC 2104. | 268 // and happens to be the inner padding from RFC 2104. |
269 return identityHashCode(_reflectee) ^ 0x36363636; | 269 return identityHashCode(_reflectee) ^ 0x36363636; |
270 } | 270 } |
271 | 271 |
272 Function operator [](Symbol selector) { | 272 Function operator [](Symbol selector) { |
273 bool found = false; | 273 bool found = false; |
274 for (ClassMirror c = type; c != null; c = c.superclass) { | 274 for (ClassMirror c = type; c != null; c = c.superclass) { |
275 var target = c.methods[selector]; | 275 var target = c._methods[selector]; |
276 if (target != null && !target.isStatic && target.isRegularMethod) { | 276 if (target != null && !target.isStatic && target.isRegularMethod) { |
277 found = true; | 277 found = true; |
278 break; | 278 break; |
279 } | 279 } |
280 } | 280 } |
281 if (!found) { | 281 if (!found) { |
282 throw new ArgumentError( | 282 throw new ArgumentError( |
283 "${MirrorSystem.getName(type.simpleName)} has no instance method " | 283 "${MirrorSystem.getName(type.simpleName)} has no instance method " |
284 "${MirrorSystem.getName(selector)}"); | 284 "${MirrorSystem.getName(selector)}"); |
285 } | 285 } |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
357 arguments[argumentIndex++] = value; | 357 arguments[argumentIndex++] = value; |
358 names[nameIndex++] = _n(name); | 358 names[nameIndex++] = _n(name); |
359 }); | 359 }); |
360 } | 360 } |
361 | 361 |
362 // It is tempting to implement this in terms of Function.apply, but then | 362 // It is tempting to implement this in terms of Function.apply, but then |
363 // lazy compilation errors would be fatal. | 363 // lazy compilation errors would be fatal. |
364 return reflect(_apply(arguments, names)); | 364 return reflect(_apply(arguments, names)); |
365 } | 365 } |
366 | 366 |
367 Future<InstanceMirror> applyAsync(List positionalArguments, | |
368 [Map<Symbol, dynamic> namedArguments]) { | |
369 return new Future(() { | |
370 return this.apply(_unwrapAsyncPositionals(positionalArguments), | |
371 _unwrapAsyncNamed(namedArguments)); | |
372 }); | |
373 } | |
374 | |
375 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { | 367 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { |
376 List<String> parts = _n(name).split(".").toList(growable: false); | 368 List<String> parts = _n(name).split(".").toList(growable: false); |
377 if (parts.length > 3) { | 369 if (parts.length > 3) { |
378 throw new ArgumentError("Invalid symbol: ${name}"); | 370 throw new ArgumentError("Invalid symbol: ${name}"); |
379 } | 371 } |
380 List tuple = _computeFindInContext(_reflectee, parts); | 372 List tuple = _computeFindInContext(_reflectee, parts); |
381 if (tuple.length == 0) { | 373 if (tuple.length == 0) { |
382 throw new UnsupportedError( | 374 throw new UnsupportedError( |
383 "ClosureMirror.findInContext not yet supported"); | 375 "ClosureMirror.findInContext not yet supported"); |
384 } | 376 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
520 // The reflectee is not a mixin application. | 512 // The reflectee is not a mixin application. |
521 _mixin = this; | 513 _mixin = this; |
522 } else { | 514 } else { |
523 _mixin = reflectType(mixinType); | 515 _mixin = reflectType(mixinType); |
524 } | 516 } |
525 } | 517 } |
526 } | 518 } |
527 return _mixin; | 519 return _mixin; |
528 } | 520 } |
529 | 521 |
530 Map<Symbol, DeclarationMirror> _declarations; | 522 Map<Symbol, DeclarationMirror> _cachedDeclarations; |
531 Map<Symbol, DeclarationMirror> get declarations { | 523 Map<Symbol, DeclarationMirror> get declarations { |
532 if (_declarations != null) return _declarations; | 524 if (_cachedDeclarations != null) return _cachedDeclarations; |
533 var decls = new Map<Symbol, DeclarationMirror>(); | 525 var decls = new Map<Symbol, DeclarationMirror>(); |
534 decls.addAll(members); | 526 decls.addAll(_members); |
535 decls.addAll(constructors); | 527 decls.addAll(_constructors); |
536 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 528 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
537 return _declarations = | 529 return _cachedDeclarations = |
538 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 530 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
539 } | 531 } |
540 | 532 |
541 Map<Symbol, Mirror> _members; | 533 Map<Symbol, Mirror> _cachedMembers; |
542 Map<Symbol, Mirror> get members { | 534 Map<Symbol, Mirror> get _members { |
543 if (_members == null) { | 535 if (_cachedMembers == null) { |
544 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; | 536 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; |
545 _members = _makeMemberMap(mixin._computeMembers(whoseMembers._reflectee)); | 537 _cachedMembers = _makeMemberMap(mixin._computeMembers(whoseMembers._reflec
tee)); |
546 } | 538 } |
547 return _members; | 539 return _cachedMembers; |
548 } | 540 } |
549 | 541 |
550 Map<Symbol, MethodMirror> _methods; | 542 Map<Symbol, MethodMirror> _cachedConstructors; |
551 Map<Symbol, MethodMirror> get methods { | 543 Map<Symbol, MethodMirror> get _constructors { |
552 if (_methods == null) { | 544 if (_cachedConstructors == null) { |
553 _methods = _filterMap( | |
554 members, | |
555 (key, value) => (value is MethodMirror && value.isRegularMethod)); | |
556 } | |
557 return _methods; | |
558 } | |
559 | |
560 Map<Symbol, MethodMirror> _getters; | |
561 Map<Symbol, MethodMirror> get getters { | |
562 if (_getters == null) { | |
563 _getters = _filterMap( | |
564 members, | |
565 (key, value) => (value is MethodMirror && value.isGetter)); | |
566 } | |
567 return _getters; | |
568 } | |
569 | |
570 Map<Symbol, MethodMirror> _setters; | |
571 Map<Symbol, MethodMirror> get setters { | |
572 if (_setters == null) { | |
573 _setters = _filterMap( | |
574 members, | |
575 (key, value) => (value is MethodMirror && value.isSetter)); | |
576 } | |
577 return _setters; | |
578 } | |
579 | |
580 Map<Symbol, VariableMirror> _variables; | |
581 Map<Symbol, VariableMirror> get variables { | |
582 if (_variables == null) { | |
583 _variables = _filterMap( | |
584 members, | |
585 (key, value) => (value is VariableMirror)); | |
586 } | |
587 return _variables; | |
588 } | |
589 | |
590 Map<Symbol, MethodMirror> _constructors; | |
591 Map<Symbol, MethodMirror> get constructors { | |
592 if (_constructors == null) { | |
593 var constructorsList = _computeConstructors(_reflectee); | 545 var constructorsList = _computeConstructors(_reflectee); |
594 var stringName = _n(simpleName); | 546 var stringName = _n(simpleName); |
595 constructorsList.forEach((c) => c._patchConstructorName(stringName)); | 547 constructorsList.forEach((c) => c._patchConstructorName(stringName)); |
596 _constructors = _makeMemberMap(constructorsList); | 548 _cachedConstructors = _makeMemberMap(constructorsList); |
597 } | 549 } |
598 return _constructors; | 550 return _cachedConstructors; |
| 551 } |
| 552 |
| 553 get _methods { |
| 554 var result = new Map(); |
| 555 declarations.forEach((k, v) { |
| 556 if (v is MethodMirror && !v.isConstructor) result[k] = v; |
| 557 }); |
| 558 return result; |
599 } | 559 } |
600 | 560 |
601 bool get _isAnonymousMixinApplication { | 561 bool get _isAnonymousMixinApplication { |
602 if (_isMixinTypedef) return false; // Named mixin application. | 562 if (_isMixinTypedef) return false; // Named mixin application. |
603 if (mixin == this) return false; // Not a mixin application. | 563 if (mixin == this) return false; // Not a mixin application. |
604 return true; | 564 return true; |
605 } | 565 } |
606 | 566 |
607 List<TypeVariableMirror> _typeVariables = null; | 567 List<TypeVariableMirror> _typeVariables; |
608 List<TypeVariableMirror> get typeVariables { | 568 List<TypeVariableMirror> get typeVariables { |
609 if (_typeVariables == null) { | 569 if (_typeVariables == null) { |
610 if (_isAnonymousMixinApplication) return _typeVariables = emptyList; | 570 if (_isAnonymousMixinApplication) return _typeVariables = emptyList; |
611 _typeVariables = new List<TypeVariableMirror>(); | 571 _typeVariables = new List<TypeVariableMirror>(); |
612 | 572 |
613 List params = _ClassMirror_type_variables(_reflectee); | 573 List params = _ClassMirror_type_variables(_reflectee); |
614 ClassMirror owner = originalDeclaration; | 574 ClassMirror owner = originalDeclaration; |
615 var mirror; | 575 var mirror; |
616 for (var i = 0; i < params.length; i += 2) { | 576 for (var i = 0; i < params.length; i += 2) { |
617 mirror = new _LocalTypeVariableMirrorImpl( | 577 mirror = new _LocalTypeVariableMirrorImpl( |
618 params[i + 1], params[i], owner); | 578 params[i + 1], params[i], owner); |
619 _typeVariables.add(mirror); | 579 _typeVariables.add(mirror); |
620 } | 580 } |
621 _typeVariables = new UnmodifiableListView(_typeVariables); | 581 _typeVariables = new UnmodifiableListView(_typeVariables); |
622 } | 582 } |
623 return _typeVariables; | 583 return _typeVariables; |
624 } | 584 } |
625 | 585 |
626 List<TypeMirror> _typeArguments = null; | 586 List<TypeMirror> _typeArguments; |
627 List<TypeMirror> get typeArguments { | 587 List<TypeMirror> get typeArguments { |
628 if(_typeArguments == null) { | 588 if(_typeArguments == null) { |
629 if(_isGenericDeclaration || _isAnonymousMixinApplication) { | 589 if(_isGenericDeclaration || _isAnonymousMixinApplication) { |
630 _typeArguments = emptyList; | 590 _typeArguments = emptyList; |
631 } else { | 591 } else { |
632 _typeArguments = | 592 _typeArguments = |
633 new UnmodifiableListView(_computeTypeArguments(_reflectedType)); | 593 new UnmodifiableListView(_computeTypeArguments(_reflectedType)); |
634 } | 594 } |
635 } | 595 } |
636 return _typeArguments; | 596 return _typeArguments; |
637 } | 597 } |
638 | 598 |
639 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; | 599 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; |
640 | 600 |
641 ClassMirror get originalDeclaration { | 601 ClassMirror get originalDeclaration { |
642 if (isOriginalDeclaration) { | 602 if (isOriginalDeclaration) { |
643 return this; | 603 return this; |
644 } else { | 604 } else { |
645 return reflectClass(_reflectedType); | 605 return reflectClass(_reflectedType); |
646 } | 606 } |
647 } | 607 } |
648 | 608 |
649 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; | 609 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; |
650 | 610 |
651 Function operator [](Symbol selector) { | 611 Function operator [](Symbol selector) { |
652 var target = methods[selector]; | 612 var target = _methods[selector]; |
653 if (target == null || !target.isStatic || !target.isRegularMethod) { | 613 if (target == null || !target.isStatic || !target.isRegularMethod) { |
654 throw new ArgumentError( | 614 throw new ArgumentError( |
655 "${MirrorSystem.getName(simpleName)} has no static method " | 615 "${MirrorSystem.getName(simpleName)} has no static method " |
656 "${MirrorSystem.getName(selector)}"); | 616 "${MirrorSystem.getName(selector)}"); |
657 } | 617 } |
658 return new _InvocationTrampoline(this, selector); | 618 return new _InvocationTrampoline(this, selector); |
659 } | 619 } |
660 | 620 |
661 InstanceMirror newInstance(Symbol constructorName, | 621 InstanceMirror newInstance(Symbol constructorName, |
662 List positionalArguments, | 622 List positionalArguments, |
(...skipping 15 matching lines...) Expand all Loading... |
678 }); | 638 }); |
679 } | 639 } |
680 | 640 |
681 return reflect(_invokeConstructor(_reflectee, | 641 return reflect(_invokeConstructor(_reflectee, |
682 _reflectedType, | 642 _reflectedType, |
683 _n(constructorName), | 643 _n(constructorName), |
684 arguments, | 644 arguments, |
685 names)); | 645 names)); |
686 } | 646 } |
687 | 647 |
688 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, | |
689 List positionalArguments, | |
690 [Map<Symbol, dynamic> namedArguments])
{ | |
691 return new Future(() { | |
692 return this.newInstance(constructorName, | |
693 _unwrapAsyncPositionals(positionalArguments), | |
694 _unwrapAsyncNamed(namedArguments)); | |
695 }); | |
696 } | |
697 | |
698 List<InstanceMirror> get metadata { | 648 List<InstanceMirror> get metadata { |
699 // Get the metadata objects, convert them into InstanceMirrors using | 649 // Get the metadata objects, convert them into InstanceMirrors using |
700 // reflect() and then make them into a Dart list. | 650 // reflect() and then make them into a Dart list. |
701 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); | 651 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); |
702 } | 652 } |
703 | 653 |
704 bool operator ==(other) { | 654 bool operator ==(other) { |
705 return this.runtimeType == other.runtimeType && | 655 return this.runtimeType == other.runtimeType && |
706 this._reflectee == other._reflectee && | 656 this._reflectee == other._reflectee && |
707 this._reflectedType == other._reflectedType && | 657 this._reflectedType == other._reflectedType && |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 | 999 |
1050 // Always false for libraries. | 1000 // Always false for libraries. |
1051 final bool isTopLevel = false; | 1001 final bool isTopLevel = false; |
1052 | 1002 |
1053 SourceLocation get location { | 1003 SourceLocation get location { |
1054 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1004 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
1055 } | 1005 } |
1056 | 1006 |
1057 final Uri uri; | 1007 final Uri uri; |
1058 | 1008 |
1059 Map<Symbol, DeclarationMirror> _declarations; | 1009 Map<Symbol, DeclarationMirror> _cachedDeclarations; |
1060 Map<Symbol, DeclarationMirror> get declarations { | 1010 Map<Symbol, DeclarationMirror> get declarations { |
1061 if (_declarations != null) return _declarations; | 1011 if (_cachedDeclarations != null) return _cachedDeclarations; |
1062 return _declarations = | 1012 return _cachedDeclarations = |
1063 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); | 1013 new _UnmodifiableMapView<Symbol, DeclarationMirror>( |
| 1014 _makeMemberMap(_computeMembers(_reflectee))); |
1064 } | 1015 } |
1065 | 1016 |
1066 Map<Symbol, Mirror> _members; | 1017 get _functions { |
1067 Map<Symbol, Mirror> get members { | 1018 var result = new Map(); |
1068 if (_members == null) { | 1019 declarations.forEach((k, v) { |
1069 _members = _makeMemberMap(_computeMembers(_reflectee)); | 1020 if (v is MethodMirror) result[k] = v; |
1070 } | 1021 }); |
1071 return _members; | 1022 return result; |
1072 } | |
1073 | |
1074 Map<Symbol, ClassMirror> _types; | |
1075 Map<Symbol, TypeMirror> get types { | |
1076 if (_types == null) { | |
1077 _types = _filterMap(members, (key, value) => (value is TypeMirror)); | |
1078 } | |
1079 return _types; | |
1080 } | |
1081 | |
1082 Map<Symbol, ClassMirror> _classes; | |
1083 Map<Symbol, ClassMirror> get classes { | |
1084 if (_classes == null) { | |
1085 _classes = _filterMap(members, (key, value) => (value is ClassMirror)); | |
1086 } | |
1087 return _classes; | |
1088 } | |
1089 | |
1090 Map<Symbol, MethodMirror> _functions; | |
1091 Map<Symbol, MethodMirror> get functions { | |
1092 if (_functions == null) { | |
1093 _functions = _filterMap(members, (key, value) => (value is MethodMirror)); | |
1094 } | |
1095 return _functions; | |
1096 } | |
1097 | |
1098 Map<Symbol, MethodMirror> _getters; | |
1099 Map<Symbol, MethodMirror> get getters { | |
1100 if (_getters == null) { | |
1101 _getters = _filterMap(functions, (key, value) => (value.isGetter)); | |
1102 } | |
1103 return _getters; | |
1104 } | |
1105 | |
1106 Map<Symbol, MethodMirror> _setters; | |
1107 Map<Symbol, MethodMirror> get setters { | |
1108 if (_setters == null) { | |
1109 _setters = _filterMap(functions, (key, value) => (value.isSetter)); | |
1110 } | |
1111 return _setters; | |
1112 } | |
1113 | |
1114 Map<Symbol, VariableMirror> _variables; | |
1115 Map<Symbol, VariableMirror> get variables { | |
1116 if (_variables == null) { | |
1117 _variables = _filterMap(members, | |
1118 (key, value) => (value is VariableMirror)); | |
1119 } | |
1120 return _variables; | |
1121 } | 1023 } |
1122 | 1024 |
1123 List<InstanceMirror> get metadata { | 1025 List<InstanceMirror> get metadata { |
1124 // Get the metadata objects, convert them into InstanceMirrors using | 1026 // Get the metadata objects, convert them into InstanceMirrors using |
1125 // reflect() and then make them into a Dart list. | 1027 // reflect() and then make them into a Dart list. |
1126 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); | 1028 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); |
1127 } | 1029 } |
1128 | 1030 |
1129 bool operator ==(other) { | 1031 bool operator ==(other) { |
1130 return this.runtimeType == other.runtimeType && | 1032 return this.runtimeType == other.runtimeType && |
1131 this._reflectee == other._reflectee; | 1033 this._reflectee == other._reflectee; |
1132 } | 1034 } |
1133 | 1035 |
1134 int get hashCode => simpleName.hashCode; | 1036 int get hashCode => simpleName.hashCode; |
1135 | 1037 |
1136 String toString() => "LibraryMirror on '${_n(simpleName)}'"; | 1038 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
1137 | 1039 |
1138 Function operator [](Symbol selector) { | 1040 Function operator [](Symbol selector) { |
1139 var target = functions[selector]; | 1041 var target = _functions[selector]; |
1140 if (target == null || !target.isRegularMethod) { | 1042 if (target == null || !target.isRegularMethod) { |
1141 throw new ArgumentError( | 1043 throw new ArgumentError( |
1142 "${MirrorSystem.getName(simpleName)} has no top-level method " | 1044 "${MirrorSystem.getName(simpleName)} has no top-level method " |
1143 "${MirrorSystem.getName(selector)}"); | 1045 "${MirrorSystem.getName(selector)}"); |
1144 } | 1046 } |
1145 return new _InvocationTrampoline(this, selector); | 1047 return new _InvocationTrampoline(this, selector); |
1146 } | 1048 } |
1147 | 1049 |
1148 _invoke(reflectee, memberName, arguments, argumentNames) | 1050 _invoke(reflectee, memberName, arguments, argumentNames) |
1149 native 'LibraryMirror_invoke'; | 1051 native 'LibraryMirror_invoke'; |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1468 if (typeMirror == null) { | 1370 if (typeMirror == null) { |
1469 typeMirror = makeLocalTypeMirror(key); | 1371 typeMirror = makeLocalTypeMirror(key); |
1470 _instanitationCache[key] = typeMirror; | 1372 _instanitationCache[key] = typeMirror; |
1471 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1373 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1472 _declarationCache[key] = typeMirror; | 1374 _declarationCache[key] = typeMirror; |
1473 } | 1375 } |
1474 } | 1376 } |
1475 return typeMirror; | 1377 return typeMirror; |
1476 } | 1378 } |
1477 } | 1379 } |
OLD | NEW |