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 |
367 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { | 375 InstanceMirror findInContext(Symbol name, {ifAbsent: null}) { |
368 List<String> parts = _n(name).split(".").toList(growable: false); | 376 List<String> parts = _n(name).split(".").toList(growable: false); |
369 if (parts.length > 3) { | 377 if (parts.length > 3) { |
370 throw new ArgumentError("Invalid symbol: ${name}"); | 378 throw new ArgumentError("Invalid symbol: ${name}"); |
371 } | 379 } |
372 List tuple = _computeFindInContext(_reflectee, parts); | 380 List tuple = _computeFindInContext(_reflectee, parts); |
373 if (tuple.length == 0) { | 381 if (tuple.length == 0) { |
374 throw new UnsupportedError( | 382 throw new UnsupportedError( |
375 "ClosureMirror.findInContext not yet supported"); | 383 "ClosureMirror.findInContext not yet supported"); |
376 } | 384 } |
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
512 // The reflectee is not a mixin application. | 520 // The reflectee is not a mixin application. |
513 _mixin = this; | 521 _mixin = this; |
514 } else { | 522 } else { |
515 _mixin = reflectType(mixinType); | 523 _mixin = reflectType(mixinType); |
516 } | 524 } |
517 } | 525 } |
518 } | 526 } |
519 return _mixin; | 527 return _mixin; |
520 } | 528 } |
521 | 529 |
522 Map<Symbol, DeclarationMirror> _cachedDeclarations; | 530 Map<Symbol, DeclarationMirror> _declarations; |
523 Map<Symbol, DeclarationMirror> get declarations { | 531 Map<Symbol, DeclarationMirror> get declarations { |
524 if (_cachedDeclarations != null) return _cachedDeclarations; | 532 if (_declarations != null) return _declarations; |
525 var decls = new Map<Symbol, DeclarationMirror>(); | 533 var decls = new Map<Symbol, DeclarationMirror>(); |
526 decls.addAll(_members); | 534 decls.addAll(members); |
527 decls.addAll(_constructors); | 535 decls.addAll(constructors); |
528 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); | 536 typeVariables.forEach((tv) => decls[tv.simpleName] = tv); |
529 return _cachedDeclarations = | 537 return _declarations = |
530 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); | 538 new _UnmodifiableMapView<Symbol, DeclarationMirror>(decls); |
531 } | 539 } |
532 | 540 |
533 Map<Symbol, Mirror> _cachedMembers; | 541 Map<Symbol, Mirror> _members; |
534 Map<Symbol, Mirror> get _members { | 542 Map<Symbol, Mirror> get members { |
535 if (_cachedMembers == null) { | 543 if (_members == null) { |
536 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; | 544 var whoseMembers = _isMixinTypedef ? _trueSuperclass : this; |
537 _cachedMembers = _makeMemberMap(mixin._computeMembers(whoseMembers._reflec
tee)); | 545 _members = _makeMemberMap(mixin._computeMembers(whoseMembers._reflectee)); |
538 } | 546 } |
539 return _cachedMembers; | 547 return _members; |
540 } | 548 } |
541 | 549 |
542 Map<Symbol, MethodMirror> _cachedConstructors; | 550 Map<Symbol, MethodMirror> _methods; |
543 Map<Symbol, MethodMirror> get _constructors { | 551 Map<Symbol, MethodMirror> get methods { |
544 if (_cachedConstructors == null) { | 552 if (_methods == 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) { |
545 var constructorsList = _computeConstructors(_reflectee); | 593 var constructorsList = _computeConstructors(_reflectee); |
546 var stringName = _n(simpleName); | 594 var stringName = _n(simpleName); |
547 constructorsList.forEach((c) => c._patchConstructorName(stringName)); | 595 constructorsList.forEach((c) => c._patchConstructorName(stringName)); |
548 _cachedConstructors = _makeMemberMap(constructorsList); | 596 _constructors = _makeMemberMap(constructorsList); |
549 } | 597 } |
550 return _cachedConstructors; | 598 return _constructors; |
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; | |
559 } | 599 } |
560 | 600 |
561 bool get _isAnonymousMixinApplication { | 601 bool get _isAnonymousMixinApplication { |
562 if (_isMixinTypedef) return false; // Named mixin application. | 602 if (_isMixinTypedef) return false; // Named mixin application. |
563 if (mixin == this) return false; // Not a mixin application. | 603 if (mixin == this) return false; // Not a mixin application. |
564 return true; | 604 return true; |
565 } | 605 } |
566 | 606 |
567 List<TypeVariableMirror> _typeVariables; | 607 List<TypeVariableMirror> _typeVariables = null; |
568 List<TypeVariableMirror> get typeVariables { | 608 List<TypeVariableMirror> get typeVariables { |
569 if (_typeVariables == null) { | 609 if (_typeVariables == null) { |
570 if (_isAnonymousMixinApplication) return _typeVariables = emptyList; | 610 if (_isAnonymousMixinApplication) return _typeVariables = emptyList; |
571 _typeVariables = new List<TypeVariableMirror>(); | 611 _typeVariables = new List<TypeVariableMirror>(); |
572 | 612 |
573 List params = _ClassMirror_type_variables(_reflectee); | 613 List params = _ClassMirror_type_variables(_reflectee); |
574 ClassMirror owner = originalDeclaration; | 614 ClassMirror owner = originalDeclaration; |
575 var mirror; | 615 var mirror; |
576 for (var i = 0; i < params.length; i += 2) { | 616 for (var i = 0; i < params.length; i += 2) { |
577 mirror = new _LocalTypeVariableMirrorImpl( | 617 mirror = new _LocalTypeVariableMirrorImpl( |
578 params[i + 1], params[i], owner); | 618 params[i + 1], params[i], owner); |
579 _typeVariables.add(mirror); | 619 _typeVariables.add(mirror); |
580 } | 620 } |
581 _typeVariables = new UnmodifiableListView(_typeVariables); | 621 _typeVariables = new UnmodifiableListView(_typeVariables); |
582 } | 622 } |
583 return _typeVariables; | 623 return _typeVariables; |
584 } | 624 } |
585 | 625 |
586 List<TypeMirror> _typeArguments; | 626 List<TypeMirror> _typeArguments = null; |
587 List<TypeMirror> get typeArguments { | 627 List<TypeMirror> get typeArguments { |
588 if(_typeArguments == null) { | 628 if(_typeArguments == null) { |
589 if(_isGenericDeclaration || _isAnonymousMixinApplication) { | 629 if(_isGenericDeclaration || _isAnonymousMixinApplication) { |
590 _typeArguments = emptyList; | 630 _typeArguments = emptyList; |
591 } else { | 631 } else { |
592 _typeArguments = | 632 _typeArguments = |
593 new UnmodifiableListView(_computeTypeArguments(_reflectedType)); | 633 new UnmodifiableListView(_computeTypeArguments(_reflectedType)); |
594 } | 634 } |
595 } | 635 } |
596 return _typeArguments; | 636 return _typeArguments; |
597 } | 637 } |
598 | 638 |
599 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; | 639 bool get isOriginalDeclaration => !_isGeneric || _isGenericDeclaration; |
600 | 640 |
601 ClassMirror get originalDeclaration { | 641 ClassMirror get originalDeclaration { |
602 if (isOriginalDeclaration) { | 642 if (isOriginalDeclaration) { |
603 return this; | 643 return this; |
604 } else { | 644 } else { |
605 return reflectClass(_reflectedType); | 645 return reflectClass(_reflectedType); |
606 } | 646 } |
607 } | 647 } |
608 | 648 |
609 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; | 649 String toString() => "ClassMirror on '${MirrorSystem.getName(simpleName)}'"; |
610 | 650 |
611 Function operator [](Symbol selector) { | 651 Function operator [](Symbol selector) { |
612 var target = _methods[selector]; | 652 var target = methods[selector]; |
613 if (target == null || !target.isStatic || !target.isRegularMethod) { | 653 if (target == null || !target.isStatic || !target.isRegularMethod) { |
614 throw new ArgumentError( | 654 throw new ArgumentError( |
615 "${MirrorSystem.getName(simpleName)} has no static method " | 655 "${MirrorSystem.getName(simpleName)} has no static method " |
616 "${MirrorSystem.getName(selector)}"); | 656 "${MirrorSystem.getName(selector)}"); |
617 } | 657 } |
618 return new _InvocationTrampoline(this, selector); | 658 return new _InvocationTrampoline(this, selector); |
619 } | 659 } |
620 | 660 |
621 InstanceMirror newInstance(Symbol constructorName, | 661 InstanceMirror newInstance(Symbol constructorName, |
622 List positionalArguments, | 662 List positionalArguments, |
(...skipping 15 matching lines...) Expand all Loading... |
638 }); | 678 }); |
639 } | 679 } |
640 | 680 |
641 return reflect(_invokeConstructor(_reflectee, | 681 return reflect(_invokeConstructor(_reflectee, |
642 _reflectedType, | 682 _reflectedType, |
643 _n(constructorName), | 683 _n(constructorName), |
644 arguments, | 684 arguments, |
645 names)); | 685 names)); |
646 } | 686 } |
647 | 687 |
| 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 |
648 List<InstanceMirror> get metadata { | 698 List<InstanceMirror> get metadata { |
649 // Get the metadata objects, convert them into InstanceMirrors using | 699 // Get the metadata objects, convert them into InstanceMirrors using |
650 // reflect() and then make them into a Dart list. | 700 // reflect() and then make them into a Dart list. |
651 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); | 701 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); |
652 } | 702 } |
653 | 703 |
654 bool operator ==(other) { | 704 bool operator ==(other) { |
655 return this.runtimeType == other.runtimeType && | 705 return this.runtimeType == other.runtimeType && |
656 this._reflectee == other._reflectee && | 706 this._reflectee == other._reflectee && |
657 this._reflectedType == other._reflectedType && | 707 this._reflectedType == other._reflectedType && |
(...skipping 341 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
999 | 1049 |
1000 // Always false for libraries. | 1050 // Always false for libraries. |
1001 final bool isTopLevel = false; | 1051 final bool isTopLevel = false; |
1002 | 1052 |
1003 SourceLocation get location { | 1053 SourceLocation get location { |
1004 throw new UnimplementedError('LibraryMirror.location is not implemented'); | 1054 throw new UnimplementedError('LibraryMirror.location is not implemented'); |
1005 } | 1055 } |
1006 | 1056 |
1007 final Uri uri; | 1057 final Uri uri; |
1008 | 1058 |
1009 Map<Symbol, DeclarationMirror> _cachedDeclarations; | 1059 Map<Symbol, DeclarationMirror> _declarations; |
1010 Map<Symbol, DeclarationMirror> get declarations { | 1060 Map<Symbol, DeclarationMirror> get declarations { |
1011 if (_cachedDeclarations != null) return _cachedDeclarations; | 1061 if (_declarations != null) return _declarations; |
1012 return _cachedDeclarations = | 1062 return _declarations = |
1013 new _UnmodifiableMapView<Symbol, DeclarationMirror>( | 1063 new _UnmodifiableMapView<Symbol, DeclarationMirror>(members); |
1014 _makeMemberMap(_computeMembers(_reflectee))); | |
1015 } | 1064 } |
1016 | 1065 |
1017 get _functions { | 1066 Map<Symbol, Mirror> _members; |
1018 var result = new Map(); | 1067 Map<Symbol, Mirror> get members { |
1019 declarations.forEach((k, v) { | 1068 if (_members == null) { |
1020 if (v is MethodMirror) result[k] = v; | 1069 _members = _makeMemberMap(_computeMembers(_reflectee)); |
1021 }); | 1070 } |
1022 return result; | 1071 return _members; |
| 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; |
1023 } | 1121 } |
1024 | 1122 |
1025 List<InstanceMirror> get metadata { | 1123 List<InstanceMirror> get metadata { |
1026 // Get the metadata objects, convert them into InstanceMirrors using | 1124 // Get the metadata objects, convert them into InstanceMirrors using |
1027 // reflect() and then make them into a Dart list. | 1125 // reflect() and then make them into a Dart list. |
1028 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); | 1126 return new UnmodifiableListView(_metadata(_reflectee).map(reflect)); |
1029 } | 1127 } |
1030 | 1128 |
1031 bool operator ==(other) { | 1129 bool operator ==(other) { |
1032 return this.runtimeType == other.runtimeType && | 1130 return this.runtimeType == other.runtimeType && |
1033 this._reflectee == other._reflectee; | 1131 this._reflectee == other._reflectee; |
1034 } | 1132 } |
1035 | 1133 |
1036 int get hashCode => simpleName.hashCode; | 1134 int get hashCode => simpleName.hashCode; |
1037 | 1135 |
1038 String toString() => "LibraryMirror on '${_n(simpleName)}'"; | 1136 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
1039 | 1137 |
1040 Function operator [](Symbol selector) { | 1138 Function operator [](Symbol selector) { |
1041 var target = _functions[selector]; | 1139 var target = functions[selector]; |
1042 if (target == null || !target.isRegularMethod) { | 1140 if (target == null || !target.isRegularMethod) { |
1043 throw new ArgumentError( | 1141 throw new ArgumentError( |
1044 "${MirrorSystem.getName(simpleName)} has no top-level method " | 1142 "${MirrorSystem.getName(simpleName)} has no top-level method " |
1045 "${MirrorSystem.getName(selector)}"); | 1143 "${MirrorSystem.getName(selector)}"); |
1046 } | 1144 } |
1047 return new _InvocationTrampoline(this, selector); | 1145 return new _InvocationTrampoline(this, selector); |
1048 } | 1146 } |
1049 | 1147 |
1050 _invoke(reflectee, memberName, arguments, argumentNames) | 1148 _invoke(reflectee, memberName, arguments, argumentNames) |
1051 native 'LibraryMirror_invoke'; | 1149 native 'LibraryMirror_invoke'; |
(...skipping 318 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1370 if (typeMirror == null) { | 1468 if (typeMirror == null) { |
1371 typeMirror = makeLocalTypeMirror(key); | 1469 typeMirror = makeLocalTypeMirror(key); |
1372 _instanitationCache[key] = typeMirror; | 1470 _instanitationCache[key] = typeMirror; |
1373 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { | 1471 if (typeMirror is ClassMirror && !typeMirror._isGeneric) { |
1374 _declarationCache[key] = typeMirror; | 1472 _declarationCache[key] = typeMirror; |
1375 } | 1473 } |
1376 } | 1474 } |
1377 return typeMirror; | 1475 return typeMirror; |
1378 } | 1476 } |
1379 } | 1477 } |
OLD | NEW |