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 // These values are allowed to be passed directly over the wire. | 7 // These values are allowed to be passed directly over the wire. |
| 8 bool _isSimpleValue(var value) { | 8 bool _isSimpleValue(var value) { |
| 9 return (value == null || value is num || value is String || value is bool); | 9 return (value == null || value is num || value is String || value is bool); |
| 10 } | 10 } |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 static _invoke(ref, memberName, positionalArguments, async) | 242 static _invoke(ref, memberName, positionalArguments, async) |
| 243 native 'LocalObjectMirrorImpl_invoke'; | 243 native 'LocalObjectMirrorImpl_invoke'; |
| 244 | 244 |
| 245 static _getField(ref, fieldName) // same for sync and async versions | 245 static _getField(ref, fieldName) // same for sync and async versions |
| 246 native 'LocalObjectMirrorImpl_getField'; | 246 native 'LocalObjectMirrorImpl_getField'; |
| 247 | 247 |
| 248 static _setField(ref, fieldName, value, async) | 248 static _setField(ref, fieldName, value, async) |
| 249 native 'LocalObjectMirrorImpl_setField'; | 249 native 'LocalObjectMirrorImpl_setField'; |
| 250 } | 250 } |
| 251 | 251 |
| 252 // Prints a string as it might appear in dart program text. | |
| 253 // TODO(turnidge): Consider truncating. | |
| 254 String _dartEscape(String str) { | |
|
ahe
2013/06/12 16:27:26
I have requested something like this gets integrat
| |
| 255 bool isNice(int code) => (code >= 32 && code <= 126); | |
| 256 | |
| 257 StringBuffer buf = new StringBuffer(); | |
| 258 for (int i = 0; i < str.length; i++) { | |
| 259 var input = str[i]; | |
| 260 String output; | |
| 261 switch (input) { | |
| 262 case '\\' : | |
| 263 output = r'\\'; | |
| 264 break; | |
| 265 case "\'" : | |
| 266 output = r"\'"; | |
| 267 break; | |
| 268 case '\n' : | |
| 269 output = r'\n'; | |
| 270 break; | |
| 271 case '\r' : | |
| 272 output = r'\r'; | |
| 273 break; | |
| 274 case '\f' : | |
| 275 output = r'\f'; | |
| 276 break; | |
| 277 case '\b' : | |
| 278 output = r'\b'; | |
| 279 break; | |
| 280 case '\t' : | |
| 281 output = r'\t'; | |
| 282 break; | |
| 283 case '\v' : | |
| 284 output = r'\v'; | |
| 285 break; | |
| 286 default: | |
| 287 // TODO(lrn): Someone decide if this should combine surrogate pairs. | |
| 288 int code = input.codeUnitAt(0); | |
| 289 if (isNice(code)) { | |
| 290 output = input; | |
| 291 } else { | |
| 292 output = '\\u{${code.toRadixString(16)}}'; | |
| 293 } | |
| 294 break; | |
| 295 } | |
| 296 buf.write(output); | |
| 297 } | |
| 298 return buf.toString(); | |
| 299 } | |
| 300 | |
| 301 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl | 252 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl |
| 302 implements InstanceMirror { | 253 implements InstanceMirror { |
| 303 // TODO(ahe): This is a hack, see delegate below. | 254 // TODO(ahe): This is a hack, see delegate below. |
| 304 static Function _invokeOnClosure; | 255 static Function _invokeOnClosure; |
| 305 | 256 |
| 306 _LocalInstanceMirrorImpl(ref, | 257 _LocalInstanceMirrorImpl(ref, |
| 307 this._type, | 258 this._type, |
| 308 this._reflectee) : super(ref) {} | 259 this._reflectee) : super(ref) {} |
| 309 | 260 |
| 310 var _type; | 261 var _type; |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 329 // private method does not work. | 280 // private method does not work. |
| 330 _LocalInstanceMirrorImpl mirror = | 281 _LocalInstanceMirrorImpl mirror = |
| 331 _Mirrors.makeLocalInstanceMirror(invocation); | 282 _Mirrors.makeLocalInstanceMirror(invocation); |
| 332 _invokeOnClosure = | 283 _invokeOnClosure = |
| 333 _LocalObjectMirrorImpl._getField(mirror.type, '_invokeOnClosure') | 284 _LocalObjectMirrorImpl._getField(mirror.type, '_invokeOnClosure') |
| 334 .reflectee; | 285 .reflectee; |
| 335 } | 286 } |
| 336 return _invokeOnClosure(reflectee, invocation); | 287 return _invokeOnClosure(reflectee, invocation); |
| 337 } | 288 } |
| 338 | 289 |
| 339 String toString() { | 290 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; |
| 340 if (_isSimpleValue(_reflectee)) { | |
| 341 if (_reflectee is String) { | |
| 342 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>"; | |
| 343 } else { | |
| 344 return "InstanceMirror on <$_reflectee>"; | |
| 345 } | |
| 346 } else { | |
| 347 return "InstanceMirror on instance of '${type.simpleName}'"; | |
| 348 } | |
| 349 } | |
| 350 } | 291 } |
| 351 | 292 |
| 352 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl | 293 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl |
| 353 implements ClosureMirror { | 294 implements ClosureMirror { |
| 354 _LocalClosureMirrorImpl(ref, | 295 _LocalClosureMirrorImpl(ref, |
| 355 type, | 296 type, |
| 356 reflectee, | 297 reflectee, |
| 357 this.function) : super(ref, type, reflectee) {} | 298 this.function) : super(ref, type, reflectee) {} |
| 358 | 299 |
| 359 final MethodMirror function; | 300 final MethodMirror function; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 391 } | 332 } |
| 392 } | 333 } |
| 393 | 334 |
| 394 Future<InstanceMirror> findInContext(Symbol name) { | 335 Future<InstanceMirror> findInContext(Symbol name) { |
| 395 throw new UnimplementedError( | 336 throw new UnimplementedError( |
| 396 'ClosureMirror.findInContext() is not implemented'); | 337 'ClosureMirror.findInContext() is not implemented'); |
| 397 } | 338 } |
| 398 | 339 |
| 399 static _apply(ref, positionalArguments, async) | 340 static _apply(ref, positionalArguments, async) |
| 400 native 'LocalClosureMirrorImpl_apply'; | 341 native 'LocalClosureMirrorImpl_apply'; |
| 342 | |
| 343 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; | |
| 401 } | 344 } |
| 402 | 345 |
| 403 class _LazyTypeMirror { | 346 class _LazyTypeMirror { |
| 404 _LazyTypeMirror(String this.libraryUrl, String typeName) | 347 _LazyTypeMirror(String this.libraryUrl, String typeName) |
| 405 : this.typeName = _s(typeName); | 348 : this.typeName = _s(typeName); |
| 406 | 349 |
| 407 TypeMirror resolve(MirrorSystem mirrors) { | 350 TypeMirror resolve(MirrorSystem mirrors) { |
| 408 if (libraryUrl == null) { | 351 if (libraryUrl == null) { |
| 409 if (typeName == const Symbol('dynamic')) { | 352 if (typeName == const Symbol('dynamic')) { |
| 410 return mirrors.dynamicType; | 353 return mirrors.dynamicType; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 557 bool get isOriginalDeclaration { | 500 bool get isOriginalDeclaration { |
| 558 throw new UnimplementedError( | 501 throw new UnimplementedError( |
| 559 'ClassMirror.isOriginalDeclaration is not implemented'); | 502 'ClassMirror.isOriginalDeclaration is not implemented'); |
| 560 } | 503 } |
| 561 | 504 |
| 562 ClassMirror get genericDeclaration { | 505 ClassMirror get genericDeclaration { |
| 563 throw new UnimplementedError( | 506 throw new UnimplementedError( |
| 564 'ClassMirror.originalDeclaration is not implemented'); | 507 'ClassMirror.originalDeclaration is not implemented'); |
| 565 } | 508 } |
| 566 | 509 |
| 567 String toString() => "ClassMirror on '$simpleName'"; | 510 String toString() { |
| 511 String prettyName = isClass ? 'ClassMirror' : 'TypeMirror'; | |
| 512 return "$prettyName on '${_n(simpleName)}'"; | |
| 513 } | |
| 568 | 514 |
| 569 InstanceMirror newInstance(Symbol constructorName, | 515 InstanceMirror newInstance(Symbol constructorName, |
| 570 List positionalArguments, | 516 List positionalArguments, |
| 571 [Map<Symbol, dynamic> namedArguments]) { | 517 [Map<Symbol, dynamic> namedArguments]) { |
| 572 if (namedArguments != null) { | 518 if (namedArguments != null) { |
| 573 throw new UnimplementedError( | 519 throw new UnimplementedError( |
| 574 'named argument support is not implemented'); | 520 'named argument support is not implemented'); |
| 575 } | 521 } |
| 576 return _invokeConstructor(this, | 522 return _invokeConstructor(this, |
| 577 _n(constructorName), | 523 _n(constructorName), |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 638 var _returnType; | 584 var _returnType; |
| 639 TypeMirror get returnType { | 585 TypeMirror get returnType { |
| 640 if (_returnType is! Mirror) { | 586 if (_returnType is! Mirror) { |
| 641 _returnType = _returnType.resolve(mirrors); | 587 _returnType = _returnType.resolve(mirrors); |
| 642 } | 588 } |
| 643 return _returnType; | 589 return _returnType; |
| 644 } | 590 } |
| 645 | 591 |
| 646 final List<ParameterMirror> parameters; | 592 final List<ParameterMirror> parameters; |
| 647 | 593 |
| 648 String toString() => "FunctionTypeMirror on '$simpleName'"; | 594 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'"; |
| 649 } | 595 } |
| 650 | 596 |
| 651 | 597 |
| 652 class _LazyTypeVariableMirror { | 598 class _LazyTypeVariableMirror { |
| 653 _LazyTypeVariableMirror(String variableName, this._owner) | 599 _LazyTypeVariableMirror(String variableName, this._owner) |
| 654 : this._variableName = _s(variableName); | 600 : this._variableName = _s(variableName); |
| 655 | 601 |
| 656 TypeVariableMirror resolve(MirrorSystem mirrors) { | 602 TypeVariableMirror resolve(MirrorSystem mirrors) { |
| 657 ClassMirror owner = _owner.resolve(mirrors); | 603 ClassMirror owner = _owner.resolve(mirrors); |
| 658 return owner.typeVariables[_variableName]; | 604 return owner.typeVariables[_variableName]; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 697 } | 643 } |
| 698 | 644 |
| 699 var _upperBound; | 645 var _upperBound; |
| 700 TypeMirror get upperBound { | 646 TypeMirror get upperBound { |
| 701 if (_upperBound is! Mirror) { | 647 if (_upperBound is! Mirror) { |
| 702 _upperBound = _upperBound.resolve(mirrors); | 648 _upperBound = _upperBound.resolve(mirrors); |
| 703 } | 649 } |
| 704 return _upperBound; | 650 return _upperBound; |
| 705 } | 651 } |
| 706 | 652 |
| 707 String toString() => "TypeVariableMirror on '$simpleName'"; | 653 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; |
| 708 } | 654 } |
| 709 | 655 |
| 710 | 656 |
| 711 class _LocalTypedefMirrorImpl extends _LocalMirrorImpl | 657 class _LocalTypedefMirrorImpl extends _LocalMirrorImpl |
| 712 implements TypedefMirror { | 658 implements TypedefMirror { |
| 713 _LocalTypedefMirrorImpl(String simpleName, | 659 _LocalTypedefMirrorImpl(String simpleName, |
| 714 this._owner, | 660 this._owner, |
| 715 this._referent) | 661 this._referent) |
| 716 : this.simpleName = _s(simpleName); | 662 : this.simpleName = _s(simpleName); |
| 717 | 663 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 743 } | 689 } |
| 744 | 690 |
| 745 var _referent; | 691 var _referent; |
| 746 TypeMirror get referent { | 692 TypeMirror get referent { |
| 747 if (_referent is! Mirror) { | 693 if (_referent is! Mirror) { |
| 748 _referent = _referent.resolve(mirrors); | 694 _referent = _referent.resolve(mirrors); |
| 749 } | 695 } |
| 750 return _referent; | 696 return _referent; |
| 751 } | 697 } |
| 752 | 698 |
| 753 String toString() => "TypedefMirror on '$simpleName'"; | 699 String toString() => "TypedefMirror on '${_n(simpleName)}'"; |
| 754 } | 700 } |
| 755 | 701 |
| 756 | 702 |
| 757 class _LazyLibraryMirror { | 703 class _LazyLibraryMirror { |
| 758 _LazyLibraryMirror(String this.libraryUrl); | 704 _LazyLibraryMirror(String this.libraryUrl); |
| 759 | 705 |
| 760 LibraryMirror resolve(MirrorSystem mirrors) { | 706 LibraryMirror resolve(MirrorSystem mirrors) { |
| 761 return mirrors.libraries[Uri.parse(libraryUrl)]; | 707 return mirrors.libraries[Uri.parse(libraryUrl)]; |
| 762 } | 708 } |
| 763 | 709 |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 836 } | 782 } |
| 837 | 783 |
| 838 Map<Symbol, VariableMirror> get variables { | 784 Map<Symbol, VariableMirror> get variables { |
| 839 if (_variables == null) { | 785 if (_variables == null) { |
| 840 _variables = _filterMap(members, | 786 _variables = _filterMap(members, |
| 841 (key, value) => (value is VariableMirror)); | 787 (key, value) => (value is VariableMirror)); |
| 842 } | 788 } |
| 843 return _variables; | 789 return _variables; |
| 844 } | 790 } |
| 845 | 791 |
| 846 String toString() => "LibraryMirror on '$simpleName'"; | 792 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
| 847 } | 793 } |
| 848 | 794 |
| 849 class _LocalMethodMirrorImpl extends _LocalMirrorImpl | 795 class _LocalMethodMirrorImpl extends _LocalMirrorImpl |
| 850 implements MethodMirror { | 796 implements MethodMirror { |
| 851 _LocalMethodMirrorImpl(String simpleName, | 797 _LocalMethodMirrorImpl(String simpleName, |
| 852 this._owner, | 798 this._owner, |
| 853 this.parameters, | 799 this.parameters, |
| 854 this._returnType, | 800 this._returnType, |
| 855 this.isStatic, | 801 this.isStatic, |
| 856 this.isAbstract, | 802 this.isAbstract, |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 936 } | 882 } |
| 937 } | 883 } |
| 938 return _constructorName; | 884 return _constructorName; |
| 939 } | 885 } |
| 940 | 886 |
| 941 final bool isConstConstructor; | 887 final bool isConstConstructor; |
| 942 final bool isGenerativeConstructor; | 888 final bool isGenerativeConstructor; |
| 943 final bool isRedirectingConstructor; | 889 final bool isRedirectingConstructor; |
| 944 final bool isFactoryConstructor; | 890 final bool isFactoryConstructor; |
| 945 | 891 |
| 946 String toString() => "MethodMirror on '$simpleName'"; | 892 String toString() => "MethodMirror on '${_n(simpleName)}'"; |
| 947 } | 893 } |
| 948 | 894 |
| 949 class _LocalVariableMirrorImpl extends _LocalMirrorImpl | 895 class _LocalVariableMirrorImpl extends _LocalMirrorImpl |
| 950 implements VariableMirror { | 896 implements VariableMirror { |
| 951 _LocalVariableMirrorImpl(String simpleName, | 897 _LocalVariableMirrorImpl(String simpleName, |
| 952 this._owner, | 898 this._owner, |
| 953 this._type, | 899 this._type, |
| 954 this.isStatic, | 900 this.isStatic, |
| 955 this.isFinal) | 901 this.isFinal) |
| 956 : this.simpleName = _s(simpleName); | 902 : this.simpleName = _s(simpleName); |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 990 TypeMirror get type { | 936 TypeMirror get type { |
| 991 if (_type is! Mirror) { | 937 if (_type is! Mirror) { |
| 992 _type = _type.resolve(mirrors); | 938 _type = _type.resolve(mirrors); |
| 993 } | 939 } |
| 994 return _type; | 940 return _type; |
| 995 } | 941 } |
| 996 | 942 |
| 997 final bool isStatic; | 943 final bool isStatic; |
| 998 final bool isFinal; | 944 final bool isFinal; |
| 999 | 945 |
| 1000 String toString() => "VariableMirror on '$simpleName'"; | 946 String toString() => "VariableMirror on '${_n(simpleName)}'"; |
| 1001 } | 947 } |
| 1002 | 948 |
| 1003 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl | 949 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl |
| 1004 implements ParameterMirror { | 950 implements ParameterMirror { |
| 1005 _LocalParameterMirrorImpl(type, this.isOptional) | 951 _LocalParameterMirrorImpl(type, this.isOptional) |
| 1006 : super('<TODO:unnamed>', null, type, false, false) {} | 952 : super('<TODO:unnamed>', null, type, false, false) {} |
| 1007 | 953 |
| 1008 final bool isOptional; | 954 final bool isOptional; |
| 1009 | 955 |
| 1010 String get defaultValue { | 956 String get defaultValue { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1061 } | 1007 } |
| 1062 | 1008 |
| 1063 // Creates a new local ClassMirror. | 1009 // Creates a new local ClassMirror. |
| 1064 static ClassMirror makeLocalClassMirror(Type key) | 1010 static ClassMirror makeLocalClassMirror(Type key) |
| 1065 native "Mirrors_makeLocalClassMirror"; | 1011 native "Mirrors_makeLocalClassMirror"; |
| 1066 | 1012 |
| 1067 static ClassMirror reflectClass(Type key) { | 1013 static ClassMirror reflectClass(Type key) { |
| 1068 return makeLocalClassMirror(key); | 1014 return makeLocalClassMirror(key); |
| 1069 } | 1015 } |
| 1070 } | 1016 } |
| OLD | NEW |