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 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
66 var result = new Map<Uri, LibraryMirror>(); | 66 var result = new Map<Uri, LibraryMirror>(); |
67 map.forEach((String url, LibraryMirror mirror) { | 67 map.forEach((String url, LibraryMirror mirror) { |
68 result[Uri.parse(url)] = mirror; | 68 result[Uri.parse(url)] = mirror; |
69 }); | 69 }); |
70 return result; | 70 return result; |
71 } | 71 } |
72 | 72 |
73 List<InstanceMirror> _metadata(mirror) | 73 List<InstanceMirror> _metadata(mirror) |
74 native 'Mirrors_metadata'; | 74 native 'Mirrors_metadata'; |
75 | 75 |
| 76 // This will verify the argument types, unwrap them, and ensure we have a fixed |
| 77 // array. |
| 78 List _unwarpAsyncArguments(wrappedArgs){ |
| 79 List unwrappedArgs = new List(wrappedArgs.length); |
| 80 for(int i = 0; i < wrappedArgs.length; i++){ |
| 81 var wrappedArg = wrappedArgs[i]; |
| 82 if(_isSimpleValue(wrappedArg)) { |
| 83 unwrappedArgs[i] = wrappedArg; |
| 84 } else if(wrappedArg is InstanceMirror) { |
| 85 unwrappedArgs[i] = wrappedArg._reflectee; |
| 86 } else { |
| 87 throw "positional argument $i ($arg) was not a simple value or InstanceMir
ror"; |
| 88 } |
| 89 } |
| 90 return unwrappedArgs; |
| 91 } |
76 | 92 |
77 class _LocalMirrorSystemImpl extends MirrorSystem { | 93 class _LocalMirrorSystemImpl extends MirrorSystem { |
78 // Change parameter back to "this.libraries" when native code is changed. | 94 // Change parameter back to "this.libraries" when native code is changed. |
79 _LocalMirrorSystemImpl(Map<String, LibraryMirror> libraries, this.isolate) | 95 _LocalMirrorSystemImpl(Map<String, LibraryMirror> libraries, this.isolate) |
80 : this.libraries = _createLibrariesMap(libraries), | 96 : this.libraries = _createLibrariesMap(libraries), |
81 _functionTypes = new Map<String, FunctionTypeMirror>(); | 97 _functionTypes = new Map<String, FunctionTypeMirror>(); |
82 | 98 |
83 final Map<Uri, LibraryMirror> libraries; | 99 final Map<Uri, LibraryMirror> libraries; |
84 final IsolateMirror isolate; | 100 final IsolateMirror isolate; |
85 | 101 |
(...skipping 311 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 this.typeVariables = _convertStringToSymbolMap(typeVariables), | 413 this.typeVariables = _convertStringToSymbolMap(typeVariables), |
398 super(ref); | 414 super(ref); |
399 | 415 |
400 final _MirrorReference _reflectee; | 416 final _MirrorReference _reflectee; |
401 | 417 |
402 Symbol _simpleName; | 418 Symbol _simpleName; |
403 Symbol get simpleName { | 419 Symbol get simpleName { |
404 // dynamic, void and the function types have their names set eagerly in the | 420 // dynamic, void and the function types have their names set eagerly in the |
405 // constructor. | 421 // constructor. |
406 if(_simpleName == null) { | 422 if(_simpleName == null) { |
407 _simpleName = _s(_ClassMirror_name(_reflectee)); | 423 _simpleName = _s(_name(_reflectee)); |
408 } | 424 } |
409 return _simpleName; | 425 return _simpleName; |
410 } | 426 } |
411 | 427 |
412 Symbol _qualifiedName = null; | 428 Symbol _qualifiedName = null; |
413 Symbol get qualifiedName { | 429 Symbol get qualifiedName { |
414 if (_qualifiedName == null) { | 430 if (_qualifiedName == null) { |
415 _qualifiedName = _computeQualifiedName(owner, simpleName); | 431 _qualifiedName = _computeQualifiedName(owner, simpleName); |
416 } | 432 } |
417 return _qualifiedName; | 433 return _qualifiedName; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
524 ClassMirror get genericDeclaration { | 540 ClassMirror get genericDeclaration { |
525 throw new UnimplementedError( | 541 throw new UnimplementedError( |
526 'ClassMirror.originalDeclaration is not implemented'); | 542 'ClassMirror.originalDeclaration is not implemented'); |
527 } | 543 } |
528 | 544 |
529 String toString() { | 545 String toString() { |
530 String prettyName = isClass ? 'ClassMirror' : 'TypeMirror'; | 546 String prettyName = isClass ? 'ClassMirror' : 'TypeMirror'; |
531 return "$prettyName on '${_n(simpleName)}'"; | 547 return "$prettyName on '${_n(simpleName)}'"; |
532 } | 548 } |
533 | 549 |
| 550 InstanceMirror invoke(Symbol memberName, |
| 551 List positionalArguments, |
| 552 [Map<Symbol, dynamic> namedArguments]) { |
| 553 if (namedArguments != null) { |
| 554 throw new UnimplementedError( |
| 555 'named argument support is not implemented'); |
| 556 } |
| 557 return reflect(_invoke(_reflectee, |
| 558 _n(memberName), |
| 559 positionalArguments.toList(growable:false))); |
| 560 } |
| 561 |
| 562 Future<InstanceMirror> invokeAsync(Symbol memberName, |
| 563 List positionalArguments, |
| 564 [Map<Symbol, dynamic> namedArguments]) { |
| 565 if (namedArguments != null) { |
| 566 throw new UnimplementedError( |
| 567 'named argument support is not implemented'); |
| 568 } |
| 569 |
| 570 try { |
| 571 var result = _invoke(_reflectee, |
| 572 _n(memberName), |
| 573 _unwarpAsyncArguments(positionalArguments)); |
| 574 return new Future.value(reflect(result)); |
| 575 } on MirroredError catch(e) { |
| 576 return new Future.error(e); |
| 577 } |
| 578 } |
| 579 |
| 580 InstanceMirror getField(Symbol memberName) { |
| 581 return reflect(_invokeGetter(_reflectee, |
| 582 _n(memberName))); |
| 583 } |
| 584 |
| 585 Future<InstanceMirror> getFieldAsync(Symbol memberName) { |
| 586 try { |
| 587 var result = _invokeGetter(_reflectee, |
| 588 _n(memberName)); |
| 589 return new Future.value(reflect(result)); |
| 590 } on MirroredError catch(e) { |
| 591 return new Future.error(e); |
| 592 } |
| 593 } |
| 594 |
| 595 InstanceMirror setField(Symbol memberName, Object value) { |
| 596 return reflect(_invokeSetter(_reflectee, |
| 597 _n(memberName), |
| 598 value)); |
| 599 } |
| 600 |
| 601 Future<InstanceMirror> setFieldAsync(Symbol memberName, Object value) { |
| 602 try { |
| 603 var unwrappedValue; |
| 604 if(_isSimpleValue(value)) { |
| 605 unwrappedValue = value; |
| 606 } else if(wrappedArg is InstanceMirror) { |
| 607 unwrappedValue = value._reflectee; |
| 608 } else { |
| 609 throw "setter argument ($value) must be a simple value or InstanceMirror
"; |
| 610 } |
| 611 |
| 612 var result = _invokeSetter(_reflectee, |
| 613 _n(memberName), |
| 614 unwrappedValue); |
| 615 return new Future.value(reflect(result)); |
| 616 } on MirroredError catch(e) { |
| 617 return new Future.error(e); |
| 618 } |
| 619 } |
| 620 |
534 InstanceMirror newInstance(Symbol constructorName, | 621 InstanceMirror newInstance(Symbol constructorName, |
535 List positionalArguments, | 622 List positionalArguments, |
536 [Map<Symbol, dynamic> namedArguments]) { | 623 [Map<Symbol, dynamic> namedArguments]) { |
537 if (namedArguments != null) { | 624 if (namedArguments != null) { |
538 throw new UnimplementedError( | 625 throw new UnimplementedError( |
539 'named argument support is not implemented'); | 626 'named argument support is not implemented'); |
540 } | 627 } |
541 return _invokeConstructor(this, | 628 return reflect(_invokeConstructor(_reflectee, |
542 _n(constructorName), | 629 _n(constructorName), |
543 positionalArguments, | 630 positionalArguments.toList(growable:false)
)); |
544 false); | |
545 } | 631 } |
546 | 632 |
547 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, | 633 Future<InstanceMirror> newInstanceAsync(Symbol constructorName, |
548 List positionalArguments, | 634 List positionalArguments, |
549 [Map<Symbol, dynamic> namedArguments])
{ | 635 [Map<Symbol, dynamic> namedArguments])
{ |
550 if (namedArguments != null) { | 636 if (namedArguments != null) { |
551 throw new UnimplementedError( | 637 throw new UnimplementedError( |
552 'named argument support is not implemented'); | 638 'named argument support is not implemented'); |
553 } | 639 } |
554 // Walk the arguments and make sure they are legal. | 640 |
555 for (int i = 0; i < positionalArguments.length; i++) { | |
556 var arg = positionalArguments[i]; | |
557 _LocalObjectMirrorImpl._validateArgument(i, arg); | |
558 } | |
559 try { | 641 try { |
560 return new Future<InstanceMirror>.value( | 642 var result = _invokeConstructor(_reflectee, |
561 _invokeConstructor(this, | 643 _n(constructorName), |
562 _n(constructorName), | 644 _unwarpAsyncArguments(positionalArguments)
); |
563 positionalArguments, | 645 return new Future.value(reflect(result)); |
564 true)); | 646 } on MirroredError catch(e) { |
565 } catch (exception) { | 647 return new Future.error(e); |
566 return new Future<InstanceMirror>.error(exception); | |
567 } | 648 } |
568 } | 649 } |
569 | 650 |
570 // get the metadata objects, convert them into InstanceMirrors using | 651 // get the metadata objects, convert them into InstanceMirrors using |
571 // reflect() and then make them into a Dart list | 652 // reflect() and then make them into a Dart list |
572 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); | 653 List<InstanceMirror> get metadata => _metadata(this).map(reflect).toList(); |
573 | 654 |
574 static _invokeConstructor(ref, constructorName, positionalArguments, async) | |
575 native 'LocalClassMirrorImpl_invokeConstructor'; | |
576 | 655 |
577 static String _ClassMirror_name(reflectee) | 656 static _name(reflectee) |
578 native "ClassMirror_name"; | 657 native "ClassMirror_name"; |
| 658 |
| 659 static _invoke(reflectee, constructorName, positionalArguments) |
| 660 native 'ClassMirror_invoke'; |
| 661 |
| 662 static _invokeGetter(reflectee, getterName) |
| 663 native 'ClassMirror_invokeGetter'; |
| 664 |
| 665 static _invokeSetter(reflectee, setterName, value) |
| 666 native 'ClassMirror_invokeSetter'; |
| 667 |
| 668 static _invokeConstructor(reflectee, constructorName, positionalArguments) |
| 669 native 'ClassMirror_invokeConstructor'; |
579 } | 670 } |
580 | 671 |
581 class _LazyFunctionTypeMirror { | 672 class _LazyFunctionTypeMirror { |
582 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} | 673 _LazyFunctionTypeMirror(this.returnType, this.parameters) {} |
583 | 674 |
584 ClassMirror resolve(MirrorSystem mirrors) { | 675 ClassMirror resolve(MirrorSystem mirrors) { |
585 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), | 676 return mirrors._lookupFunctionTypeMirror(returnType.resolve(mirrors), |
586 parameters); | 677 parameters); |
587 } | 678 } |
588 | 679 |
(...skipping 467 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1056 } | 1147 } |
1057 | 1148 |
1058 // Creates a new local ClassMirror. | 1149 // Creates a new local ClassMirror. |
1059 static ClassMirror makeLocalClassMirror(Type key) | 1150 static ClassMirror makeLocalClassMirror(Type key) |
1060 native "Mirrors_makeLocalClassMirror"; | 1151 native "Mirrors_makeLocalClassMirror"; |
1061 | 1152 |
1062 static ClassMirror reflectClass(Type key) { | 1153 static ClassMirror reflectClass(Type key) { |
1063 return makeLocalClassMirror(key); | 1154 return makeLocalClassMirror(key); |
1064 } | 1155 } |
1065 } | 1156 } |
OLD | NEW |