Chromium Code Reviews| 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"; | |
| 8 | |
| 7 // These values are allowed to be passed directly over the wire. | 9 // These values are allowed to be passed directly over the wire. |
| 8 bool _isSimpleValue(var value) { | 10 bool _isSimpleValue(var value) { |
| 9 return (value == null || value is num || value is String || value is bool); | 11 return (value == null || value is num || value is String || value is bool); |
| 10 } | 12 } |
| 11 | 13 |
| 12 Map _filterMap(Map<Symbol, dynamic> old_map, bool filter(Symbol key, value)) { | 14 Map _filterMap(Map<Symbol, dynamic> old_map, bool filter(Symbol key, value)) { |
| 13 Map new_map = new Map<Symbol, dynamic>(); | 15 Map new_map = new Map<Symbol, dynamic>(); |
| 14 old_map.forEach((key, value) { | 16 old_map.forEach((key, value) { |
| 15 if (filter(key, value)) { | 17 if (filter(key, value)) { |
| 16 new_map[key] = value; | 18 new_map[key] = value; |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 394 | 396 |
| 395 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl | 397 class _LocalClassMirrorImpl extends _LocalObjectMirrorImpl |
| 396 implements ClassMirror { | 398 implements ClassMirror { |
| 397 _LocalClassMirrorImpl(reflectee, | 399 _LocalClassMirrorImpl(reflectee, |
| 398 String simpleName, | 400 String simpleName, |
| 399 this.isClass, | 401 this.isClass, |
| 400 this._owner, | 402 this._owner, |
| 401 this._superclass, | 403 this._superclass, |
| 402 this._superinterfaces, | 404 this._superinterfaces, |
| 403 this._defaultFactory, | 405 this._defaultFactory, |
| 404 Map<String, Mirror> constructors, | 406 Map<String, Mirror> constructors) |
| 405 Map<String, Mirror> typeVariables) | |
| 406 : this._simpleName = _s(simpleName), | 407 : this._simpleName = _s(simpleName), |
| 407 this.constructors = _convertStringToSymbolMap(constructors), | 408 this.constructors = _convertStringToSymbolMap(constructors), |
| 408 this.typeVariables = _convertStringToSymbolMap(typeVariables), | |
| 409 super(reflectee); | 409 super(reflectee); |
| 410 | 410 |
| 411 Symbol _simpleName; | 411 Symbol _simpleName; |
| 412 Symbol get simpleName { | 412 Symbol get simpleName { |
| 413 // dynamic, void and the function types have their names set eagerly in the | 413 // dynamic, void and the function types have their names set eagerly in the |
| 414 // constructor. | 414 // constructor. |
| 415 if(_simpleName == null) { | 415 if(_simpleName == null) { |
| 416 _simpleName = _s(_name(_reflectee)); | 416 _simpleName = _s(_name(_reflectee)); |
| 417 } | 417 } |
| 418 return _simpleName; | 418 return _simpleName; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 521 Map<Symbol, VariableMirror> get variables { | 521 Map<Symbol, VariableMirror> get variables { |
| 522 if (_variables == null) { | 522 if (_variables == null) { |
| 523 _variables = _filterMap( | 523 _variables = _filterMap( |
| 524 members, | 524 members, |
| 525 (key, value) => (value is VariableMirror)); | 525 (key, value) => (value is VariableMirror)); |
| 526 } | 526 } |
| 527 return _variables; | 527 return _variables; |
| 528 } | 528 } |
| 529 | 529 |
| 530 Map<Symbol, MethodMirror> constructors; | 530 Map<Symbol, MethodMirror> constructors; |
| 531 Map<Symbol, TypeVariableMirror> typeVariables; | 531 |
| 532 Map<Symbol, TypeVariableMirror> _typeVariables = null; | |
| 533 Map<Symbol, TypeVariableMirror> get typeVariables { | |
| 534 if (_typeVariables == null) { | |
| 535 List params = _ClassMirror_type_variables(_reflectee); | |
| 536 _typeVariables = new LinkedHashMap<Symbol, TypeVariableMirror>(); | |
| 537 for (var i = 0; i < params.length; i += 2) { | |
| 538 _typeVariables[new Symbol(params[i])] = | |
| 539 new _LocalTypeVariableMirrorImpl(params[i + 1], params[0], this); | |
|
siva
2013/07/26 22:10:34
params[i] instead of params[0]
(looks like we are
Michael Lippautz (Google)
2013/07/26 23:30:07
Done. I also added some tests do cover these thing
| |
| 540 } | |
| 541 } | |
| 542 return _typeVariables; | |
| 543 } | |
| 532 | 544 |
| 533 Map<Symbol, TypeMirror> get typeArguments { | 545 Map<Symbol, TypeMirror> get typeArguments { |
| 534 throw new UnimplementedError( | 546 throw new UnimplementedError( |
| 535 'ClassMirror.typeArguments is not implemented'); | 547 'ClassMirror.typeArguments is not implemented'); |
| 536 } | 548 } |
| 537 | 549 |
| 538 bool get isOriginalDeclaration { | 550 bool get isOriginalDeclaration { |
| 539 throw new UnimplementedError( | 551 throw new UnimplementedError( |
| 540 'ClassMirror.isOriginalDeclaration is not implemented'); | 552 'ClassMirror.isOriginalDeclaration is not implemented'); |
| 541 } | 553 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 599 native 'ClassMirror_invoke'; | 611 native 'ClassMirror_invoke'; |
| 600 | 612 |
| 601 _invokeGetter(reflectee, getterName) | 613 _invokeGetter(reflectee, getterName) |
| 602 native 'ClassMirror_invokeGetter'; | 614 native 'ClassMirror_invokeGetter'; |
| 603 | 615 |
| 604 _invokeSetter(reflectee, setterName, value) | 616 _invokeSetter(reflectee, setterName, value) |
| 605 native 'ClassMirror_invokeSetter'; | 617 native 'ClassMirror_invokeSetter'; |
| 606 | 618 |
| 607 static _invokeConstructor(reflectee, constructorName, positionalArguments) | 619 static _invokeConstructor(reflectee, constructorName, positionalArguments) |
| 608 native 'ClassMirror_invokeConstructor'; | 620 native 'ClassMirror_invokeConstructor'; |
| 621 | |
| 622 static _ClassMirror_type_variables(reflectee) | |
| 623 native "ClassMirror_type_variables"; | |
| 609 } | 624 } |
| 610 | 625 |
| 611 class _LazyFunctionTypeMirror { | 626 class _LazyFunctionTypeMirror { |
| 612 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} | 627 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} |
| 613 | 628 |
| 614 ClassMirror resolve(MirrorSystem mirrors) { | 629 ClassMirror resolve(MirrorSystem mirrors) { |
| 615 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), | 630 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), |
| 616 parameters); | 631 parameters); |
| 617 } | 632 } |
| 618 | 633 |
| 619 final returnType; | 634 final returnType; |
| 620 final List<ParameterMirror> parameters; | 635 final List<ParameterMirror> parameters; |
| 621 } | 636 } |
| 622 | 637 |
| 623 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl | 638 class _LocalFunctionTypeMirrorImpl extends _LocalClassMirrorImpl |
| 624 implements FunctionTypeMirror { | 639 implements FunctionTypeMirror { |
| 625 _LocalFunctionTypeMirrorImpl(reflectee, | 640 _LocalFunctionTypeMirrorImpl(reflectee, |
| 626 simpleName, | 641 simpleName, |
| 627 this._returnType, | 642 this._returnType, |
| 628 this.parameters) | 643 this.parameters) |
| 629 : super(reflectee, | 644 : super(reflectee, |
| 630 simpleName, | 645 simpleName, |
| 631 true, | 646 true, |
| 632 null, | 647 null, |
| 633 new _LazyTypeMirror('dart:core', 'Object'), | 648 new _LazyTypeMirror('dart:core', 'Object'), |
| 634 [ new _LazyTypeMirror('dart:core', 'Function') ], | 649 [ new _LazyTypeMirror('dart:core', 'Function') ], |
| 635 null, | 650 null, |
| 636 const {}, | |
| 637 const {}); | 651 const {}); |
| 638 | 652 |
| 639 Map<Symbol, Mirror> get members => new Map<Symbol,Mirror>(); | 653 Map<Symbol, Mirror> get members => new Map<Symbol,Mirror>(); |
| 640 | 654 |
| 641 var _returnType; | 655 var _returnType; |
| 642 TypeMirror get returnType { | 656 TypeMirror get returnType { |
| 643 if (_returnType is! Mirror) { | 657 if (_returnType is! Mirror) { |
| 644 _returnType = _returnType.resolve(mirrors); | 658 _returnType = _returnType.resolve(mirrors); |
| 645 } | 659 } |
| 646 return _returnType; | 660 return _returnType; |
| 647 } | 661 } |
| 648 | 662 |
| 649 final List<ParameterMirror> parameters; | 663 final List<ParameterMirror> parameters; |
| 664 final Map<Symbol, TypeVariableMirror> typeVariables = const {}; | |
| 650 | 665 |
| 651 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'"; | 666 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'"; |
| 652 } | 667 } |
| 653 | 668 |
| 654 abstract class _LocalDeclarationMirrorImpl extends _LocalMirrorImpl | 669 abstract class _LocalDeclarationMirrorImpl extends _LocalMirrorImpl |
| 655 implements DeclarationMirror { | 670 implements DeclarationMirror { |
| 656 _LocalDeclarationMirrorImpl(this._reflectee); | 671 _LocalDeclarationMirrorImpl(this._reflectee); |
| 657 final _MirrorReference _reflectee; | 672 final _reflectee; |
| 658 | 673 |
| 659 List<InstanceMirror> get metadata { | 674 List<InstanceMirror> get metadata { |
| 660 // Get the metadata objects, convert them into InstanceMirrors using | 675 // Get the metadata objects, convert them into InstanceMirrors using |
| 661 // reflect() and then make them into a Dart list. | 676 // reflect() and then make them into a Dart list. |
| 662 return _metadata(_reflectee).map(reflect).toList(growable:false); | 677 return _metadata(_reflectee).map(reflect).toList(growable:false); |
| 663 } | 678 } |
| 664 } | 679 } |
| 665 | 680 |
| 666 class _LazyTypeVariableMirror { | 681 class _LazyTypeVariableMirror { |
| 667 _LazyTypeVariableMirror(String variableName, this._owner) | 682 _LazyTypeVariableMirror(String variableName, this._owner) |
| 668 : this._variableName = _s(variableName); | 683 : this._variableName = _s(variableName); |
| 669 | 684 |
| 670 TypeVariableMirror resolve(MirrorSystem mirrors) { | 685 TypeVariableMirror resolve(MirrorSystem mirrors) { |
| 671 ClassMirror owner = _owner.resolve(mirrors); | 686 ClassMirror owner = _owner.resolve(mirrors); |
| 672 return owner.typeVariables[_variableName]; | 687 return owner.typeVariables[_variableName]; |
| 673 } | 688 } |
| 674 | 689 |
| 675 final Symbol _variableName; | 690 final Symbol _variableName; |
| 676 final _LazyTypeMirror _owner; | 691 final _LazyTypeMirror _owner; |
| 677 } | 692 } |
| 678 | 693 |
| 679 class _LocalTypeVariableMirrorImpl extends _LocalDeclarationMirrorImpl | 694 class _LocalTypeVariableMirrorImpl extends _LocalDeclarationMirrorImpl |
| 680 implements TypeVariableMirror { | 695 implements TypeVariableMirror { |
| 681 _LocalTypeVariableMirrorImpl(reflectee, | 696 _LocalTypeVariableMirrorImpl(reflectee, |
| 682 String simpleName, | 697 String simpleName, |
| 683 this._owner, | 698 this._owner) |
| 684 this._upperBound) | |
| 685 : this.simpleName = _s(simpleName), | 699 : this.simpleName = _s(simpleName), |
| 686 super(reflectee); | 700 super(reflectee); |
| 687 | 701 |
| 688 final Symbol simpleName; | 702 final Symbol simpleName; |
| 689 | 703 |
| 690 Symbol _qualifiedName = null; | 704 Symbol _qualifiedName = null; |
| 691 Symbol get qualifiedName { | 705 Symbol get qualifiedName { |
| 692 if (_qualifiedName == null) { | 706 if (_qualifiedName == null) { |
| 693 _qualifiedName = _computeQualifiedName(owner, simpleName); | 707 _qualifiedName = _computeQualifiedName(owner, simpleName); |
| 694 } | 708 } |
| 695 return _qualifiedName; | 709 return _qualifiedName; |
| 696 } | 710 } |
| 697 | 711 |
| 698 var _owner; | 712 var _owner; |
| 699 DeclarationMirror get owner { | 713 DeclarationMirror get owner { |
| 714 if (_owner == null) { | |
| 715 _owner = _LocalTypeVariableMirror_owner(_reflectee); | |
| 716 } | |
| 717 // TODO(11897): This will go away, as soon as lazy mirrors go away. | |
| 700 if (_owner is! Mirror) { | 718 if (_owner is! Mirror) { |
| 701 _owner = _owner.resolve(mirrors); | 719 _owner = _owner.resolve(mirrors); |
| 702 } | 720 } |
| 703 return _owner; | 721 return _owner; |
| 704 } | 722 } |
| 705 | 723 |
| 706 bool get isPrivate => false; | 724 bool get isPrivate => false; |
| 707 | 725 |
| 708 final bool isTopLevel = false; | 726 final bool isTopLevel = false; |
| 709 | 727 |
| 710 SourceLocation get location { | 728 SourceLocation get location { |
| 711 throw new UnimplementedError( | 729 throw new UnimplementedError( |
| 712 'TypeVariableMirror.location is not implemented'); | 730 'TypeVariableMirror.location is not implemented'); |
| 713 } | 731 } |
| 714 | 732 |
| 715 var _upperBound; | 733 TypeMirror _upperBound = null; |
| 716 TypeMirror get upperBound { | 734 TypeMirror get upperBound { |
| 717 if (_upperBound is! Mirror) { | 735 if (_upperBound == null) { |
| 718 _upperBound = _upperBound.resolve(mirrors); | 736 _upperBound = _LocalTypeVariableMirror_upper_bound(_reflectee); |
| 719 } | 737 } |
| 720 return _upperBound; | 738 return _upperBound; |
| 721 } | 739 } |
| 722 | 740 |
| 723 List<InstanceMirror> get metadata { | 741 List<InstanceMirror> get metadata { |
| 724 throw new UnimplementedError( | 742 throw new UnimplementedError( |
| 725 'TypeVariableMirror.metadata is not implemented'); | 743 'TypeVariableMirror.metadata is not implemented'); |
| 726 } | 744 } |
| 727 | 745 |
| 728 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; | 746 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; |
| 747 | |
| 748 static DeclarationMirror _LocalTypeVariableMirror_owner(reflectee) | |
| 749 native "LocalTypeVariableMirror_owner"; | |
| 750 | |
| 751 static TypeMirror _LocalTypeVariableMirror_upper_bound(reflectee) | |
| 752 native "LocalTypeVariableMirror_upper_bound"; | |
| 729 } | 753 } |
| 730 | 754 |
| 731 | 755 |
| 732 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl | 756 class _LocalTypedefMirrorImpl extends _LocalDeclarationMirrorImpl |
| 733 implements TypedefMirror { | 757 implements TypedefMirror { |
| 734 _LocalTypedefMirrorImpl(reflectee, | 758 _LocalTypedefMirrorImpl(reflectee, |
| 735 String simpleName, | 759 String simpleName, |
| 736 this._owner, | 760 this._owner, |
| 737 this._referent) | 761 this._referent) |
| 738 : this.simpleName = _s(simpleName), | 762 : this.simpleName = _s(simpleName), |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1213 static Expando<ClassMirror> _classMirrorCache = new Expando("ClassMirror"); | 1237 static Expando<ClassMirror> _classMirrorCache = new Expando("ClassMirror"); |
| 1214 static ClassMirror reflectClass(Type key) { | 1238 static ClassMirror reflectClass(Type key) { |
| 1215 var classMirror = _classMirrorCache[key]; | 1239 var classMirror = _classMirrorCache[key]; |
| 1216 if (classMirror == null) { | 1240 if (classMirror == null) { |
| 1217 classMirror = makeLocalClassMirror(key); | 1241 classMirror = makeLocalClassMirror(key); |
| 1218 _classMirrorCache[key] = classMirror; | 1242 _classMirrorCache[key] = classMirror; |
| 1219 } | 1243 } |
| 1220 return classMirror; | 1244 return classMirror; |
| 1221 } | 1245 } |
| 1222 } | 1246 } |
| OLD | NEW |