| 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 235 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 static _invoke(ref, memberName, positionalArguments, async) | 246 static _invoke(ref, memberName, positionalArguments, async) |
| 247 native 'LocalObjectMirrorImpl_invoke'; | 247 native 'LocalObjectMirrorImpl_invoke'; |
| 248 | 248 |
| 249 static _getField(ref, fieldName) // same for sync and async versions | 249 static _getField(ref, fieldName) // same for sync and async versions |
| 250 native 'LocalObjectMirrorImpl_getField'; | 250 native 'LocalObjectMirrorImpl_getField'; |
| 251 | 251 |
| 252 static _setField(ref, fieldName, value, async) | 252 static _setField(ref, fieldName, value, async) |
| 253 native 'LocalObjectMirrorImpl_setField'; | 253 native 'LocalObjectMirrorImpl_setField'; |
| 254 } | 254 } |
| 255 | 255 |
| 256 // Prints a string as it might appear in dart program text. | |
| 257 // TODO(turnidge): Consider truncating. | |
| 258 String _dartEscape(String str) { | |
| 259 bool isNice(int code) => (code >= 32 && code <= 126); | |
| 260 | |
| 261 StringBuffer buf = new StringBuffer(); | |
| 262 for (int i = 0; i < str.length; i++) { | |
| 263 var input = str[i]; | |
| 264 String output; | |
| 265 switch (input) { | |
| 266 case '\\' : | |
| 267 output = r'\\'; | |
| 268 break; | |
| 269 case "\'" : | |
| 270 output = r"\'"; | |
| 271 break; | |
| 272 case '\n' : | |
| 273 output = r'\n'; | |
| 274 break; | |
| 275 case '\r' : | |
| 276 output = r'\r'; | |
| 277 break; | |
| 278 case '\f' : | |
| 279 output = r'\f'; | |
| 280 break; | |
| 281 case '\b' : | |
| 282 output = r'\b'; | |
| 283 break; | |
| 284 case '\t' : | |
| 285 output = r'\t'; | |
| 286 break; | |
| 287 case '\v' : | |
| 288 output = r'\v'; | |
| 289 break; | |
| 290 default: | |
| 291 // TODO(lrn): Someone decide if this should combine surrogate pairs. | |
| 292 int code = input.codeUnitAt(0); | |
| 293 if (isNice(code)) { | |
| 294 output = input; | |
| 295 } else { | |
| 296 output = '\\u{${code.toRadixString(16)}}'; | |
| 297 } | |
| 298 break; | |
| 299 } | |
| 300 buf.write(output); | |
| 301 } | |
| 302 return buf.toString(); | |
| 303 } | |
| 304 | |
| 305 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl | 256 class _LocalInstanceMirrorImpl extends _LocalObjectMirrorImpl |
| 306 implements InstanceMirror { | 257 implements InstanceMirror { |
| 307 // TODO(ahe): This is a hack, see delegate below. | 258 // TODO(ahe): This is a hack, see delegate below. |
| 308 static Function _invokeOnClosure; | 259 static Function _invokeOnClosure; |
| 309 | 260 |
| 310 _LocalInstanceMirrorImpl(ref, | 261 _LocalInstanceMirrorImpl(ref, |
| 311 this._type, | 262 this._type, |
| 312 this._reflectee) : super(ref) {} | 263 this._reflectee) : super(ref) {} |
| 313 | 264 |
| 314 var _type; | 265 var _type; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 333 // private method does not work. | 284 // private method does not work. |
| 334 _LocalInstanceMirrorImpl mirror = | 285 _LocalInstanceMirrorImpl mirror = |
| 335 _Mirrors.makeLocalInstanceMirror(invocation); | 286 _Mirrors.makeLocalInstanceMirror(invocation); |
| 336 _invokeOnClosure = | 287 _invokeOnClosure = |
| 337 _LocalObjectMirrorImpl._getField(mirror.type, '_invokeOnClosure') | 288 _LocalObjectMirrorImpl._getField(mirror.type, '_invokeOnClosure') |
| 338 .reflectee; | 289 .reflectee; |
| 339 } | 290 } |
| 340 return _invokeOnClosure(reflectee, invocation); | 291 return _invokeOnClosure(reflectee, invocation); |
| 341 } | 292 } |
| 342 | 293 |
| 343 String toString() { | 294 String toString() => 'InstanceMirror on ${Error.safeToString(_reflectee)}'; |
| 344 if (_isSimpleValue(_reflectee)) { | |
| 345 if (_reflectee is String) { | |
| 346 return "InstanceMirror on <'${_dartEscape(_reflectee)}'>"; | |
| 347 } else { | |
| 348 return "InstanceMirror on <$_reflectee>"; | |
| 349 } | |
| 350 } else { | |
| 351 return "InstanceMirror on instance of '${type.simpleName}'"; | |
| 352 } | |
| 353 } | |
| 354 } | 295 } |
| 355 | 296 |
| 356 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl | 297 class _LocalClosureMirrorImpl extends _LocalInstanceMirrorImpl |
| 357 implements ClosureMirror { | 298 implements ClosureMirror { |
| 358 _LocalClosureMirrorImpl(ref, | 299 _LocalClosureMirrorImpl(ref, |
| 359 type, | 300 type, |
| 360 reflectee, | 301 reflectee, |
| 361 this.function) : super(ref, type, reflectee) {} | 302 this.function) : super(ref, type, reflectee) {} |
| 362 | 303 |
| 363 final MethodMirror function; | 304 final MethodMirror function; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 395 } | 336 } |
| 396 } | 337 } |
| 397 | 338 |
| 398 Future<InstanceMirror> findInContext(Symbol name) { | 339 Future<InstanceMirror> findInContext(Symbol name) { |
| 399 throw new UnimplementedError( | 340 throw new UnimplementedError( |
| 400 'ClosureMirror.findInContext() is not implemented'); | 341 'ClosureMirror.findInContext() is not implemented'); |
| 401 } | 342 } |
| 402 | 343 |
| 403 static _apply(ref, positionalArguments, async) | 344 static _apply(ref, positionalArguments, async) |
| 404 native 'LocalClosureMirrorImpl_apply'; | 345 native 'LocalClosureMirrorImpl_apply'; |
| 346 |
| 347 String toString() => "ClosureMirror on '${Error.safeToString(_reflectee)}'"; |
| 405 } | 348 } |
| 406 | 349 |
| 407 class _LazyTypeMirror { | 350 class _LazyTypeMirror { |
| 408 _LazyTypeMirror(String this.libraryUrl, String typeName) | 351 _LazyTypeMirror(String this.libraryUrl, String typeName) |
| 409 : this.typeName = _s(typeName); | 352 : this.typeName = _s(typeName); |
| 410 | 353 |
| 411 TypeMirror resolve(MirrorSystem mirrors) { | 354 TypeMirror resolve(MirrorSystem mirrors) { |
| 412 if (libraryUrl == null) { | 355 if (libraryUrl == null) { |
| 413 if (typeName == const Symbol('dynamic')) { | 356 if (typeName == const Symbol('dynamic')) { |
| 414 return mirrors.dynamicType; | 357 return mirrors.dynamicType; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 561 bool get isOriginalDeclaration { | 504 bool get isOriginalDeclaration { |
| 562 throw new UnimplementedError( | 505 throw new UnimplementedError( |
| 563 'ClassMirror.isOriginalDeclaration is not implemented'); | 506 'ClassMirror.isOriginalDeclaration is not implemented'); |
| 564 } | 507 } |
| 565 | 508 |
| 566 ClassMirror get genericDeclaration { | 509 ClassMirror get genericDeclaration { |
| 567 throw new UnimplementedError( | 510 throw new UnimplementedError( |
| 568 'ClassMirror.originalDeclaration is not implemented'); | 511 'ClassMirror.originalDeclaration is not implemented'); |
| 569 } | 512 } |
| 570 | 513 |
| 571 String toString() => "ClassMirror on '$simpleName'"; | 514 String toString() { |
| 515 String prettyName = isClass ? 'ClassMirror' : 'TypeMirror'; |
| 516 return "$prettyName on '${_n(simpleName)}'"; |
| 517 } |
| 572 | 518 |
| 573 InstanceMirror newInstance(Symbol constructorName, | 519 InstanceMirror newInstance(Symbol constructorName, |
| 574 List positionalArguments, | 520 List positionalArguments, |
| 575 [Map<Symbol, dynamic> namedArguments]) { | 521 [Map<Symbol, dynamic> namedArguments]) { |
| 576 if (namedArguments != null) { | 522 if (namedArguments != null) { |
| 577 throw new UnimplementedError( | 523 throw new UnimplementedError( |
| 578 'named argument support is not implemented'); | 524 'named argument support is not implemented'); |
| 579 } | 525 } |
| 580 return _invokeConstructor(this, | 526 return _invokeConstructor(this, |
| 581 _n(constructorName), | 527 _n(constructorName), |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 646 var _returnType; | 592 var _returnType; |
| 647 TypeMirror get returnType { | 593 TypeMirror get returnType { |
| 648 if (_returnType is! Mirror) { | 594 if (_returnType is! Mirror) { |
| 649 _returnType = _returnType.resolve(mirrors); | 595 _returnType = _returnType.resolve(mirrors); |
| 650 } | 596 } |
| 651 return _returnType; | 597 return _returnType; |
| 652 } | 598 } |
| 653 | 599 |
| 654 final List<ParameterMirror> parameters; | 600 final List<ParameterMirror> parameters; |
| 655 | 601 |
| 656 String toString() => "FunctionTypeMirror on '$simpleName'"; | 602 String toString() => "FunctionTypeMirror on '${_n(simpleName)}'"; |
| 657 } | 603 } |
| 658 | 604 |
| 659 | 605 |
| 660 class _LazyTypeVariableMirror { | 606 class _LazyTypeVariableMirror { |
| 661 _LazyTypeVariableMirror(String variableName, this._owner) | 607 _LazyTypeVariableMirror(String variableName, this._owner) |
| 662 : this._variableName = _s(variableName); | 608 : this._variableName = _s(variableName); |
| 663 | 609 |
| 664 TypeVariableMirror resolve(MirrorSystem mirrors) { | 610 TypeVariableMirror resolve(MirrorSystem mirrors) { |
| 665 ClassMirror owner = _owner.resolve(mirrors); | 611 ClassMirror owner = _owner.resolve(mirrors); |
| 666 return owner.typeVariables[_variableName]; | 612 return owner.typeVariables[_variableName]; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 709 if (_upperBound is! Mirror) { | 655 if (_upperBound is! Mirror) { |
| 710 _upperBound = _upperBound.resolve(mirrors); | 656 _upperBound = _upperBound.resolve(mirrors); |
| 711 } | 657 } |
| 712 return _upperBound; | 658 return _upperBound; |
| 713 } | 659 } |
| 714 | 660 |
| 715 // get the metadata objects, convert them into InstanceMirrors using | 661 // get the metadata objects, convert them into InstanceMirrors using |
| 716 // reflect() and then make them into a Dart list | 662 // reflect() and then make them into a Dart list |
| 717 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); | 663 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); |
| 718 | 664 |
| 719 String toString() => "TypeVariableMirror on '$simpleName'"; | 665 String toString() => "TypeVariableMirror on '${_n(simpleName)}'"; |
| 720 } | 666 } |
| 721 | 667 |
| 722 | 668 |
| 723 class _LocalTypedefMirrorImpl extends _LocalMirrorImpl | 669 class _LocalTypedefMirrorImpl extends _LocalMirrorImpl |
| 724 implements TypedefMirror { | 670 implements TypedefMirror { |
| 725 _LocalTypedefMirrorImpl(String simpleName, | 671 _LocalTypedefMirrorImpl(String simpleName, |
| 726 this._owner, | 672 this._owner, |
| 727 this._referent) | 673 this._referent) |
| 728 : this.simpleName = _s(simpleName); | 674 : this.simpleName = _s(simpleName); |
| 729 | 675 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 755 } | 701 } |
| 756 | 702 |
| 757 var _referent; | 703 var _referent; |
| 758 TypeMirror get referent { | 704 TypeMirror get referent { |
| 759 if (_referent is! Mirror) { | 705 if (_referent is! Mirror) { |
| 760 _referent = _referent.resolve(mirrors); | 706 _referent = _referent.resolve(mirrors); |
| 761 } | 707 } |
| 762 return _referent; | 708 return _referent; |
| 763 } | 709 } |
| 764 | 710 |
| 765 String toString() => "TypedefMirror on '$simpleName'"; | 711 String toString() => "TypedefMirror on '${_n(simpleName)}'"; |
| 766 } | 712 } |
| 767 | 713 |
| 768 | 714 |
| 769 class _LazyLibraryMirror { | 715 class _LazyLibraryMirror { |
| 770 _LazyLibraryMirror(String this.libraryUrl); | 716 _LazyLibraryMirror(String this.libraryUrl); |
| 771 | 717 |
| 772 LibraryMirror resolve(MirrorSystem mirrors) { | 718 LibraryMirror resolve(MirrorSystem mirrors) { |
| 773 return mirrors.libraries[Uri.parse(libraryUrl)]; | 719 return mirrors.libraries[Uri.parse(libraryUrl)]; |
| 774 } | 720 } |
| 775 | 721 |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 852 _variables = _filterMap(members, | 798 _variables = _filterMap(members, |
| 853 (key, value) => (value is VariableMirror)); | 799 (key, value) => (value is VariableMirror)); |
| 854 } | 800 } |
| 855 return _variables; | 801 return _variables; |
| 856 } | 802 } |
| 857 | 803 |
| 858 // get the metadata objects, convert them into InstanceMirrors using | 804 // get the metadata objects, convert them into InstanceMirrors using |
| 859 // reflect() and then make them into a Dart list | 805 // reflect() and then make them into a Dart list |
| 860 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); | 806 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); |
| 861 | 807 |
| 862 String toString() => "LibraryMirror on '$simpleName'"; | 808 String toString() => "LibraryMirror on '${_n(simpleName)}'"; |
| 863 } | 809 } |
| 864 | 810 |
| 865 class _LocalMethodMirrorImpl extends _LocalMirrorImpl | 811 class _LocalMethodMirrorImpl extends _LocalMirrorImpl |
| 866 implements MethodMirror { | 812 implements MethodMirror { |
| 867 _LocalMethodMirrorImpl(String simpleName, | 813 _LocalMethodMirrorImpl(String simpleName, |
| 868 this._owner, | 814 this._owner, |
| 869 this.parameters, | 815 this.parameters, |
| 870 this._returnType, | 816 this._returnType, |
| 871 this.isStatic, | 817 this.isStatic, |
| 872 this.isAbstract, | 818 this.isAbstract, |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 959 final bool isRedirectingConstructor; | 905 final bool isRedirectingConstructor; |
| 960 final bool isFactoryConstructor; | 906 final bool isFactoryConstructor; |
| 961 | 907 |
| 962 List<InstanceMirror> get metadata { | 908 List<InstanceMirror> get metadata { |
| 963 owner; // ensure owner is computed | 909 owner; // ensure owner is computed |
| 964 // get the metadata objects, convert them into InstanceMirrors using | 910 // get the metadata objects, convert them into InstanceMirrors using |
| 965 // reflect() and then make them into a Dart list | 911 // reflect() and then make them into a Dart list |
| 966 return _metadata(this).map(reflect).toList(); | 912 return _metadata(this).map(reflect).toList(); |
| 967 } | 913 } |
| 968 | 914 |
| 969 String toString() => "MethodMirror on '$simpleName'"; | 915 String toString() => "MethodMirror on '${_n(simpleName)}'"; |
| 970 } | 916 } |
| 971 | 917 |
| 972 class _LocalVariableMirrorImpl extends _LocalMirrorImpl | 918 class _LocalVariableMirrorImpl extends _LocalMirrorImpl |
| 973 implements VariableMirror { | 919 implements VariableMirror { |
| 974 _LocalVariableMirrorImpl(String simpleName, | 920 _LocalVariableMirrorImpl(String simpleName, |
| 975 this._owner, | 921 this._owner, |
| 976 this._type, | 922 this._type, |
| 977 this.isStatic, | 923 this.isStatic, |
| 978 this.isFinal) | 924 this.isFinal) |
| 979 : this.simpleName = _s(simpleName); | 925 : this.simpleName = _s(simpleName); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1020 final bool isStatic; | 966 final bool isStatic; |
| 1021 final bool isFinal; | 967 final bool isFinal; |
| 1022 | 968 |
| 1023 List<InstanceMirror> get metadata { | 969 List<InstanceMirror> get metadata { |
| 1024 owner; // ensure owner is computed | 970 owner; // ensure owner is computed |
| 1025 // get the metadata objects, convert them into InstanceMirrors using | 971 // get the metadata objects, convert them into InstanceMirrors using |
| 1026 // reflect() and then make them into a Dart list | 972 // reflect() and then make them into a Dart list |
| 1027 return _metadata(this).map(reflect).toList(); | 973 return _metadata(this).map(reflect).toList(); |
| 1028 } | 974 } |
| 1029 | 975 |
| 1030 String toString() => "VariableMirror on '$simpleName'"; | 976 String toString() => "VariableMirror on '${_n(simpleName)}'"; |
| 1031 } | 977 } |
| 1032 | 978 |
| 1033 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl | 979 class _LocalParameterMirrorImpl extends _LocalVariableMirrorImpl |
| 1034 implements ParameterMirror { | 980 implements ParameterMirror { |
| 1035 _LocalParameterMirrorImpl(type, this.isOptional) | 981 _LocalParameterMirrorImpl(type, this.isOptional) |
| 1036 : super('<TODO:unnamed>', null, type, false, false) {} | 982 : super('<TODO:unnamed>', null, type, false, false) {} |
| 1037 | 983 |
| 1038 final bool isOptional; | 984 final bool isOptional; |
| 1039 | 985 |
| 1040 String get defaultValue { | 986 String get defaultValue { |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1091 } | 1037 } |
| 1092 | 1038 |
| 1093 // Creates a new local ClassMirror. | 1039 // Creates a new local ClassMirror. |
| 1094 static ClassMirror makeLocalClassMirror(Type key) | 1040 static ClassMirror makeLocalClassMirror(Type key) |
| 1095 native "Mirrors_makeLocalClassMirror"; | 1041 native "Mirrors_makeLocalClassMirror"; |
| 1096 | 1042 |
| 1097 static ClassMirror reflectClass(Type key) { | 1043 static ClassMirror reflectClass(Type key) { |
| 1098 return makeLocalClassMirror(key); | 1044 return makeLocalClassMirror(key); |
| 1099 } | 1045 } |
| 1100 } | 1046 } |
| OLD | NEW |