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 library mirrors_dart2js; | 5 library mirrors_dart2js; |
6 | 6 |
7 import 'dart:io'; | 7 import 'dart:io'; |
8 import 'dart:uri'; | 8 import 'dart:uri'; |
9 | 9 |
10 import '../../compiler.dart' as diagnostics; | 10 import '../../compiler.dart' as diagnostics; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
106 | 106 |
107 MethodMirror _convertElementMethodToMethodMirror(Dart2JsContainerMirror library, | 107 MethodMirror _convertElementMethodToMethodMirror(Dart2JsContainerMirror library, |
108 Element element) { | 108 Element element) { |
109 if (element is FunctionElement) { | 109 if (element is FunctionElement) { |
110 return new Dart2JsMethodMirror(library, element); | 110 return new Dart2JsMethodMirror(library, element); |
111 } else { | 111 } else { |
112 return null; | 112 return null; |
113 } | 113 } |
114 } | 114 } |
115 | 115 |
116 InstanceMirror _convertConstantToInstanceMirror(Dart2JsMirrorSystem mirrors, | |
117 Constant constant) { | |
118 if (constant is BoolConstant) { | |
119 return new Dart2JsBoolConstantMirror(mirrors, constant); | |
120 } else if (constant is NumConstant) { | |
121 return new Dart2JsNumConstantMirror(mirrors, constant); | |
122 } else if (constant is StringConstant) { | |
123 return new Dart2JsStringConstantMirror(mirrors, constant); | |
124 } else if (constant is ListConstant) { | |
125 return new Dart2JsListConstantMirror(mirrors, constant); | |
126 } else if (constant is MapConstant) { | |
127 return new Dart2JsMapConstantMirror(mirrors, constant); | |
128 } else if (constant is TypeConstant) { | |
129 return new Dart2JsTypeConstantMirror(mirrors, constant); | |
130 } else if (constant is FunctionConstant) { | |
131 return new Dart2JsConstantMirror(mirrors, constant); | |
132 } else if (constant is NullConstant) { | |
133 // TODO(johnniwinther): Introduce a special ClassMirror for the null type? | |
ahe
2012/12/19 15:44:03
Why? null is an instance of Null.
Johnni Winther
2012/12/20 11:30:35
Null is currently an internal class in js_helper,
| |
134 return new Dart2JsNullConstantMirror(mirrors, constant); | |
135 } else if (constant is ConstructedConstant) { | |
136 return new Dart2JsConstructedConstantMirror(mirrors, constant); | |
137 } | |
138 throw new ArgumentError( | |
ahe
2012/12/19 15:44:03
This is an internal error as _convertConstantToIns
Johnni Winther
2012/12/20 11:30:35
Changed to InternalError (+ some more places).
| |
139 "Unexpected constant $constant"); | |
140 } | |
141 | |
116 class Dart2JsMethodKind { | 142 class Dart2JsMethodKind { |
117 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular"); | 143 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular"); |
118 static const Dart2JsMethodKind GENERATIVE = | 144 static const Dart2JsMethodKind GENERATIVE = |
119 const Dart2JsMethodKind("generative"); | 145 const Dart2JsMethodKind("generative"); |
120 static const Dart2JsMethodKind REDIRECTING = | 146 static const Dart2JsMethodKind REDIRECTING = |
121 const Dart2JsMethodKind("redirecting"); | 147 const Dart2JsMethodKind("redirecting"); |
122 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const"); | 148 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const"); |
123 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory"); | 149 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory"); |
124 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter"); | 150 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter"); |
125 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter"); | 151 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter"); |
(...skipping 324 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 | 476 |
451 String get displayName => simpleName; | 477 String get displayName => simpleName; |
452 | 478 |
453 SourceLocation get location => new Dart2JsSourceLocation( | 479 SourceLocation get location => new Dart2JsSourceLocation( |
454 _element.getCompilationUnit().script, | 480 _element.getCompilationUnit().script, |
455 mirrors.compiler.spanFromElement(_element)); | 481 mirrors.compiler.spanFromElement(_element)); |
456 | 482 |
457 String toString() => _element.toString(); | 483 String toString() => _element.toString(); |
458 | 484 |
459 int get hashCode => qualifiedName.hashCode; | 485 int get hashCode => qualifiedName.hashCode; |
486 | |
487 List<InstanceMirror> get metadata { | |
488 var list = <InstanceMirror>[]; | |
489 for (MetadataAnnotation metadata in _element.metadata) { | |
490 metadata.ensureResolved(mirrors.compiler); | |
491 list.add(_convertConstantToInstanceMirror(mirrors, metadata.value)); | |
492 } | |
493 return list; | |
494 } | |
460 } | 495 } |
461 | 496 |
462 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror { | 497 abstract class Dart2JsProxyMirror extends Dart2JsDeclarationMirror { |
463 final Dart2JsMirrorSystem mirrors; | 498 final Dart2JsMirrorSystem mirrors; |
464 | 499 |
465 Dart2JsProxyMirror(this.mirrors); | 500 Dart2JsProxyMirror(this.mirrors); |
466 | 501 |
467 String get displayName => simpleName; | 502 String get displayName => simpleName; |
468 | 503 |
469 int get hashCode => qualifiedName.hashCode; | 504 int get hashCode => qualifiedName.hashCode; |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
642 SourceLocation get location { | 677 SourceLocation get location { |
643 var script = _library.getCompilationUnit().script; | 678 var script = _library.getCompilationUnit().script; |
644 SourceSpan span; | 679 SourceSpan span; |
645 if (_library.libraryTag != null) { | 680 if (_library.libraryTag != null) { |
646 span = mirrors.compiler.spanFromNode(_library.libraryTag, script.uri); | 681 span = mirrors.compiler.spanFromNode(_library.libraryTag, script.uri); |
647 } else { | 682 } else { |
648 span = new SourceSpan(script.uri, 0, 0); | 683 span = new SourceSpan(script.uri, 0, 0); |
649 } | 684 } |
650 return new Dart2JsSourceLocation(script, span); | 685 return new Dart2JsSourceLocation(script, span); |
651 } | 686 } |
687 | |
688 | |
ahe
2012/12/19 15:44:03
Remove extra lines.
Johnni Winther
2012/12/20 11:30:35
Done.
| |
652 } | 689 } |
653 | 690 |
654 class Dart2JsSourceLocation implements SourceLocation { | 691 class Dart2JsSourceLocation implements SourceLocation { |
655 final Script _script; | 692 final Script _script; |
656 final SourceSpan _span; | 693 final SourceSpan _span; |
657 int _line; | 694 int _line; |
658 int _column; | 695 int _column; |
659 | 696 |
660 Dart2JsSourceLocation(this._script, this._span); | 697 Dart2JsSourceLocation(this._script, this._span); |
661 | 698 |
(...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1124 | 1161 |
1125 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; | 1162 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; |
1126 | 1163 |
1127 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; | 1164 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; |
1128 | 1165 |
1129 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; | 1166 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; |
1130 | 1167 |
1131 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; | 1168 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; |
1132 | 1169 |
1133 ClassMirror get defaultFactory => null; | 1170 ClassMirror get defaultFactory => null; |
1171 | |
1172 // TODO(johnniwinther): Should a type show the metadata of its declaration? | |
1173 List<InstanceMirror> get metadata { | |
ahe
2012/12/19 15:44:03
My personal opinion on style: I don't like getters
Johnni Winther
2012/12/20 11:30:35
I don't either. Currently dart2js_mirror generally
Johnni Winther
2012/12/20 11:30:35
I don't either. Currently dart2js_mirror generally
| |
1174 var list = <InstanceMirror>[]; | |
1175 for (MetadataAnnotation metadata in _type.element.metadata) { | |
1176 metadata.ensureResolved(mirrors.compiler); | |
1177 list.add(_convertConstantToInstanceMirror(mirrors, metadata.value)); | |
1178 } | |
1179 return list; | |
1180 } | |
1134 } | 1181 } |
1135 | 1182 |
1136 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror | 1183 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror |
1137 implements ClassMirror { | 1184 implements ClassMirror { |
1138 List<TypeMirror> _typeArguments; | 1185 List<TypeMirror> _typeArguments; |
1139 | 1186 |
1140 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, | 1187 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, |
1141 InterfaceType interfaceType) | 1188 InterfaceType interfaceType) |
1142 : super(system, interfaceType); | 1189 : super(system, interfaceType); |
1143 | 1190 |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1558 var node = _variable.variables.parseNode(_diagnosticListener); | 1605 var node = _variable.variables.parseNode(_diagnosticListener); |
1559 if (node != null) { | 1606 if (node != null) { |
1560 var span = mirrors.compiler.spanFromNode(node, script.uri); | 1607 var span = mirrors.compiler.spanFromNode(node, script.uri); |
1561 return new Dart2JsSourceLocation(script, span); | 1608 return new Dart2JsSourceLocation(script, span); |
1562 } else { | 1609 } else { |
1563 var span = mirrors.compiler.spanFromElement(_variable); | 1610 var span = mirrors.compiler.spanFromElement(_variable); |
1564 return new Dart2JsSourceLocation(script, span); | 1611 return new Dart2JsSourceLocation(script, span); |
1565 } | 1612 } |
1566 } | 1613 } |
1567 } | 1614 } |
1615 | |
1616 //////////////////////////////////////////////////////////////////////////////// | |
1617 // Mirrors on constant values used for metadata. | |
ahe
2012/12/19 15:44:03
Section comments don't work. A separate file or l
Johnni Winther
2012/12/20 11:30:35
Will do a split into multiple files at some point.
| |
1618 //////////////////////////////////////////////////////////////////////////////// | |
1619 | |
1620 class Dart2JsConstantMirror extends InstanceMirror { | |
1621 final Dart2JsMirrorSystem mirrors; | |
ahe
2012/12/19 15:44:03
If you store the type in a final, you don't need m
Johnni Winther
2012/12/20 11:30:35
It is required by the implemented Mirror interface
ahe
2013/01/03 12:03:05
The Mirror interface needs to be changed, then. A
Johnni Winther
2013/01/04 09:14:39
Added a TODO.
gbracha
2013/01/04 19:34:30
This is interesting collateral damage from the cha
| |
1622 final Constant _constant; | |
1623 | |
1624 Dart2JsConstantMirror(this.mirrors, this._constant); | |
1625 | |
1626 ClassMirror get type => new Dart2JsClassMirror( | |
ahe
2012/12/19 15:44:03
If it doesn't fit on one line, my personal prefere
Johnni Winther
2012/12/20 11:30:35
Changed to { }
| |
1627 mirrors, _constant.computeType(mirrors.compiler).element); | |
1628 | |
1629 bool get hasReflectee => false; | |
1630 | |
1631 get reflectee { | |
1632 // TODO(johnniwinther): Which exception/error should be thrown here? | |
1633 throw new UnsupportedError('InstanceMirror does not have a reflectee'); | |
1634 } | |
1635 | |
1636 Future<InstanceMirror> getField(String fieldName) { | |
1637 throw new NoSuchMethodError(this, fieldName, [], null); | |
ahe
2012/12/19 15:44:03
Why are you throwing NoSuchMethod?
Johnni Winther
2012/12/20 11:30:35
Changed to UnsupportedError and added a TODO. I'm
| |
1638 } | |
1639 } | |
1640 | |
1641 class Dart2JsNullConstantMirror extends Dart2JsConstantMirror { | |
1642 Dart2JsNullConstantMirror(Dart2JsMirrorSystem mirrors, NullConstant constant) | |
1643 : super(mirrors, constant); | |
1644 | |
1645 NullConstant get _constant => super._constant; | |
1646 | |
1647 bool get hasReflectee => true; | |
1648 | |
1649 get reflectee => null; | |
1650 } | |
1651 | |
1652 class Dart2JsBoolConstantMirror extends Dart2JsConstantMirror { | |
1653 Dart2JsBoolConstantMirror(Dart2JsMirrorSystem mirrors, BoolConstant constant) | |
1654 : super(mirrors, constant); | |
1655 | |
1656 BoolConstant get _constant => super._constant; | |
1657 | |
1658 bool get hasReflectee => true; | |
1659 | |
1660 get reflectee => _constant is TrueConstant; | |
1661 } | |
1662 | |
1663 class Dart2JsStringConstantMirror extends Dart2JsConstantMirror { | |
1664 Dart2JsStringConstantMirror(Dart2JsMirrorSystem mirrors, | |
1665 StringConstant constant) | |
ahe
2012/12/19 15:44:03
Indentation.
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1666 : super(mirrors, constant); | |
1667 | |
1668 StringConstant get _constant => super._constant; | |
1669 | |
1670 bool get hasReflectee => true; | |
1671 | |
1672 get reflectee => _constant.value.slowToString(); | |
1673 } | |
1674 | |
1675 class Dart2JsNumConstantMirror extends Dart2JsConstantMirror { | |
1676 Dart2JsNumConstantMirror(Dart2JsMirrorSystem mirrors, | |
1677 NumConstant constant) | |
1678 : super(mirrors, constant); | |
1679 | |
1680 NumConstant get _constant => super._constant; | |
1681 | |
1682 bool get hasReflectee => true; | |
1683 | |
1684 get reflectee => _constant.value; | |
1685 } | |
1686 | |
1687 class Dart2JsListConstantMirror extends Dart2JsConstantMirror | |
1688 implements ListInstanceMirror { | |
1689 Dart2JsListConstantMirror(Dart2JsMirrorSystem mirrors, | |
1690 ListConstant constant) | |
1691 : super(mirrors, constant); | |
1692 | |
1693 ListConstant get _constant => super._constant; | |
1694 | |
1695 int get length => _constant.length; | |
1696 | |
1697 Future<InstanceMirror> operator[](int index) { | |
1698 if (index < 0) throw new RangeError('Negative index'); | |
1699 if (index >= _constant.length) throw new RangeError('Index out of bounds'); | |
1700 var completer = new Completer<InstanceMirror>(); | |
ahe
2012/12/19 15:44:03
return new Future.immediate(_convertConstantToInst
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1701 completer.complete( | |
1702 _convertConstantToInstanceMirror(mirrors, _constant.entries[index])); | |
1703 return completer.future; | |
1704 } | |
1705 } | |
1706 | |
1707 class Dart2JsMapConstantMirror extends Dart2JsConstantMirror | |
1708 implements MapInstanceMirror { | |
1709 List<String> _list; | |
1710 | |
1711 Dart2JsMapConstantMirror(Dart2JsMirrorSystem mirrors, | |
1712 MapConstant constant) | |
1713 : super(mirrors, constant); | |
1714 | |
1715 MapConstant get _constant => super._constant; | |
1716 | |
1717 void _ensureKeyList() { | |
1718 if (_list == null) { | |
1719 _list = new List<String>(); | |
ahe
2012/12/19 15:44:03
Could this be a fixed-size list?
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1720 for (StringConstant keyConstant in _constant.keys.entries) { | |
1721 _list.add(keyConstant.value.slowToString()); | |
1722 } | |
1723 } | |
1724 } | |
1725 | |
1726 int get length => _constant.length; | |
1727 | |
1728 Collection<String> get keys { | |
1729 _ensureKeyList(); | |
1730 // TODO(johnniwinther): Return an unmodifiable list. | |
1731 return _list; | |
ahe
2012/12/19 15:44:03
return new List<String>.from(_list)
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1732 } | |
1733 | |
1734 Future<InstanceMirror> operator[](String key) { | |
1735 _ensureKeyList(); | |
1736 int index = _list.indexOf(key); | |
ahe
2012/12/19 15:44:03
How about using "keys" instead. Then you don't ne
Johnni Winther
2012/12/20 11:30:35
I don't want to copy the keys list as is now done
ahe
2013/01/03 12:23:02
Sounds like you need a todo for updating this code
Johnni Winther
2013/01/04 09:14:39
Changed to _ensureKeyList to _getKeyList which ret
| |
1737 if (index == -1) return null; | |
1738 var completer = new Completer<InstanceMirror>(); | |
ahe
2012/12/19 15:44:03
Future.immediate
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1739 completer.complete( | |
1740 _convertConstantToInstanceMirror(mirrors, _constant.values[index])); | |
1741 return completer.future; | |
1742 } | |
1743 } | |
1744 | |
1745 class Dart2JsTypeConstantMirror extends Dart2JsConstantMirror | |
1746 implements TypeInstanceMirror { | |
1747 | |
1748 Dart2JsTypeConstantMirror(Dart2JsMirrorSystem mirrors, | |
1749 TypeConstant constant) | |
1750 : super(mirrors, constant); | |
1751 | |
1752 TypeConstant get _constant => super._constant; | |
1753 | |
1754 TypeMirror get representedType => _convertTypeToTypeMirror( | |
1755 mirrors, _constant.representedType, mirrors.compiler.types.dynamicType); | |
1756 | |
ahe
2012/12/19 15:44:03
Extra line.
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1757 } | |
1758 | |
1759 class Dart2JsConstructedConstantMirror extends Dart2JsConstantMirror { | |
1760 | |
1761 Dart2JsConstructedConstantMirror(Dart2JsMirrorSystem mirrors, | |
1762 ConstructedConstant constant) | |
1763 : super(mirrors, constant); | |
1764 | |
1765 ConstructedConstant get _constant => super._constant; | |
1766 | |
1767 Future<InstanceMirror> getField(String fieldName) { | |
1768 if (identical(_constant.type.element.kind, ElementKind.CLASS)) { | |
1769 ClassElement element = _constant.type.element; | |
1770 var fieldConstant = null; | |
1771 var index = 0; | |
1772 element.forEachInstanceField((_, Element field) { | |
1773 if (field.name.slowToString() == fieldName) { | |
1774 fieldConstant = _constant.fields[index]; | |
ahe
2012/12/19 15:44:03
You need to stop iterating when a match is found:
Johnni Winther
2012/12/20 11:30:35
Done.
Johnni Winther
2012/12/20 11:30:35
Changed to compute a field map instead.
| |
1775 } | |
1776 index++; | |
1777 }, includeBackendMembers: true, includeSuperMembers: true); | |
1778 if (fieldConstant != null) { | |
1779 var completer = new Completer<InstanceMirror>(); | |
ahe
2012/12/19 15:44:03
Future.immediate.
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1780 completer.complete( | |
1781 _convertConstantToInstanceMirror(mirrors, fieldConstant)); | |
1782 return completer.future; | |
1783 } | |
1784 } | |
1785 return super.getField(fieldName); | |
1786 } | |
1787 | |
ahe
2012/12/19 15:44:03
Extra line.
Johnni Winther
2012/12/20 11:30:35
Done.
| |
1788 } | |
OLD | NEW |