| 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 '../../../../../lib/compiler/compiler.dart' as diagnostics; | 10 import '../../../../../lib/compiler/compiler.dart' as diagnostics; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 return new Dart2JsFunctionTypeMirror(system, type, functionSignature); | 74 return new Dart2JsFunctionTypeMirror(system, type, functionSignature); |
| 75 } else if (type is VoidType) { | 75 } else if (type is VoidType) { |
| 76 return new Dart2JsVoidMirror(system, type); | 76 return new Dart2JsVoidMirror(system, type); |
| 77 } else if (type is TypedefType) { | 77 } else if (type is TypedefType) { |
| 78 return new Dart2JsTypedefMirror(system, type); | 78 return new Dart2JsTypedefMirror(system, type); |
| 79 } | 79 } |
| 80 throw new ArgumentError("Unexpected interface type $type"); | 80 throw new ArgumentError("Unexpected interface type $type"); |
| 81 } | 81 } |
| 82 | 82 |
| 83 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors( | 83 Collection<Dart2JsMemberMirror> _convertElementMemberToMemberMirrors( |
| 84 Dart2JsObjectMirror library, Element element) { | 84 Dart2JsContainerMirror library, Element element) { |
| 85 if (element is SynthesizedConstructorElement) { | 85 if (element is SynthesizedConstructorElement) { |
| 86 return const <Dart2JsMemberMirror>[]; | 86 return const <Dart2JsMemberMirror>[]; |
| 87 } else if (element is VariableElement) { | 87 } else if (element is VariableElement) { |
| 88 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)]; | 88 return <Dart2JsMemberMirror>[new Dart2JsFieldMirror(library, element)]; |
| 89 } else if (element is FunctionElement) { | 89 } else if (element is FunctionElement) { |
| 90 return <Dart2JsMemberMirror>[new Dart2JsMethodMirror(library, element)]; | 90 return <Dart2JsMemberMirror>[new Dart2JsMethodMirror(library, element)]; |
| 91 } else if (element is AbstractFieldElement) { | 91 } else if (element is AbstractFieldElement) { |
| 92 var members = <Dart2JsMemberMirror>[]; | 92 var members = <Dart2JsMemberMirror>[]; |
| 93 if (element.getter !== null) { | 93 if (element.getter !== null) { |
| 94 members.add(new Dart2JsMethodMirror(library, element.getter)); | 94 members.add(new Dart2JsMethodMirror(library, element.getter)); |
| 95 } | 95 } |
| 96 if (element.setter !== null) { | 96 if (element.setter !== null) { |
| 97 members.add(new Dart2JsMethodMirror(library, element.setter)); | 97 members.add(new Dart2JsMethodMirror(library, element.setter)); |
| 98 } | 98 } |
| 99 return members; | 99 return members; |
| 100 } | 100 } |
| 101 throw new ArgumentError( | 101 throw new ArgumentError( |
| 102 "Unexpected member type $element ${element.kind}"); | 102 "Unexpected member type $element ${element.kind}"); |
| 103 } | 103 } |
| 104 | 104 |
| 105 MethodMirror _convertElementMethodToMethodMirror(Dart2JsObjectMirror library, | 105 MethodMirror _convertElementMethodToMethodMirror(Dart2JsContainerMirror library, |
| 106 Element element) { | 106 Element element) { |
| 107 if (element is FunctionElement) { | 107 if (element is FunctionElement) { |
| 108 return new Dart2JsMethodMirror(library, element); | 108 return new Dart2JsMethodMirror(library, element); |
| 109 } else { | 109 } else { |
| 110 return null; | 110 return null; |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 | 113 |
| 114 class Dart2JsMethodKind { | 114 class Dart2JsMethodKind { |
| 115 static const Dart2JsMethodKind NORMAL = const Dart2JsMethodKind("normal"); | 115 static const Dart2JsMethodKind NORMAL = const Dart2JsMethodKind("normal"); |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 480 Dart2JsMirrorSystem get system => this; | 480 Dart2JsMirrorSystem get system => this; |
| 481 | 481 |
| 482 String get simpleName => "mirror"; | 482 String get simpleName => "mirror"; |
| 483 String get displayName => simpleName; | 483 String get displayName => simpleName; |
| 484 String get qualifiedName => simpleName; | 484 String get qualifiedName => simpleName; |
| 485 | 485 |
| 486 // TODO(johnniwinther): Hack! Dart2JsMirrorSystem need not be a Mirror. | 486 // TODO(johnniwinther): Hack! Dart2JsMirrorSystem need not be a Mirror. |
| 487 int get hashCode => qualifiedName.hashCode; | 487 int get hashCode => qualifiedName.hashCode; |
| 488 } | 488 } |
| 489 | 489 |
| 490 abstract class Dart2JsObjectMirror extends Dart2JsElementMirror | 490 abstract class Dart2JsContainerMirror extends Dart2JsElementMirror |
| 491 implements ObjectMirror { | 491 implements ContainerMirror { |
| 492 Map<String, MemberMirror> _members; | 492 Map<String, MemberMirror> _members; |
| 493 | 493 |
| 494 Dart2JsObjectMirror(Dart2JsMirrorSystem system, Element element) | 494 Dart2JsContainerMirror(Dart2JsMirrorSystem system, Element element) |
| 495 : super(system, element); | 495 : super(system, element); |
| 496 | 496 |
| 497 abstract void _ensureMembers(); | 497 abstract void _ensureMembers(); |
| 498 | 498 |
| 499 Map<String, MemberMirror> get declaredMembers { | 499 Map<String, MemberMirror> get members { |
| 500 _ensureMembers(); | 500 _ensureMembers(); |
| 501 return new ImmutableMapWrapper<String, MemberMirror>(_members); | 501 return new ImmutableMapWrapper<String, MemberMirror>(_members); |
| 502 } | 502 } |
| 503 | 503 |
| 504 Map<String, Mirror> get members => declaredMembers; | |
| 505 | |
| 506 Map<String, MethodMirror> get functions { | 504 Map<String, MethodMirror> get functions { |
| 507 _ensureMembers(); | 505 _ensureMembers(); |
| 508 return new AsFilteredImmutableMap<String, MemberMirror, MethodMirror>( | 506 return new AsFilteredImmutableMap<String, MemberMirror, MethodMirror>( |
| 509 _members, | 507 _members, |
| 510 (MemberMirror member) => member is MethodMirror); | 508 (MemberMirror member) => member is MethodMirror); |
| 511 } | 509 } |
| 512 | 510 |
| 513 Map<String, MethodMirror> get getters { | 511 Map<String, MethodMirror> get getters { |
| 514 _ensureMembers(); | 512 _ensureMembers(); |
| 515 return new AsFilteredImmutableMap<String, MemberMirror, MethodMirror>( | 513 return new AsFilteredImmutableMap<String, MemberMirror, MethodMirror>( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 527 } | 525 } |
| 528 | 526 |
| 529 Map<String, VariableMirror> get variables { | 527 Map<String, VariableMirror> get variables { |
| 530 _ensureMembers(); | 528 _ensureMembers(); |
| 531 return new AsFilteredImmutableMap<String, MemberMirror, VariableMirror>( | 529 return new AsFilteredImmutableMap<String, MemberMirror, VariableMirror>( |
| 532 _members, | 530 _members, |
| 533 (MemberMirror member) => member is VariableMirror); | 531 (MemberMirror member) => member is VariableMirror); |
| 534 } | 532 } |
| 535 } | 533 } |
| 536 | 534 |
| 537 class Dart2JsLibraryMirror extends Dart2JsObjectMirror | 535 class Dart2JsLibraryMirror extends Dart2JsContainerMirror |
| 538 implements LibraryMirror { | 536 implements LibraryMirror { |
| 539 Map<String, ClassMirror> _classes; | 537 Map<String, ClassMirror> _classes; |
| 540 | 538 |
| 541 Dart2JsLibraryMirror(Dart2JsMirrorSystem system, LibraryElement library) | 539 Dart2JsLibraryMirror(Dart2JsMirrorSystem system, LibraryElement library) |
| 542 : super(system, library); | 540 : super(system, library); |
| 543 | 541 |
| 544 LibraryElement get _library => _element; | 542 LibraryElement get _library => _element; |
| 545 | 543 |
| 546 Uri get uri => _library.uri; | 544 Uri get uri => _library.uri; |
| 547 | 545 |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 733 | 731 |
| 734 bool get isInitializingFormal => true; | 732 bool get isInitializingFormal => true; |
| 735 | 733 |
| 736 VariableMirror get initializedField => new Dart2JsFieldMirror( | 734 VariableMirror get initializedField => new Dart2JsFieldMirror( |
| 737 _method.owner, _fieldParameterElement.fieldElement); | 735 _method.owner, _fieldParameterElement.fieldElement); |
| 738 } | 736 } |
| 739 | 737 |
| 740 //------------------------------------------------------------------------------ | 738 //------------------------------------------------------------------------------ |
| 741 // Declarations | 739 // Declarations |
| 742 //------------------------------------------------------------------------------ | 740 //------------------------------------------------------------------------------ |
| 743 class Dart2JsClassMirror extends Dart2JsObjectMirror | 741 class Dart2JsClassMirror extends Dart2JsContainerMirror |
| 744 implements Dart2JsTypeMirror, ClassMirror { | 742 implements Dart2JsTypeMirror, ClassMirror { |
| 745 final Dart2JsLibraryMirror library; | 743 final Dart2JsLibraryMirror library; |
| 746 List<TypeVariableMirror> _typeVariables; | 744 List<TypeVariableMirror> _typeVariables; |
| 747 | 745 |
| 748 Dart2JsClassMirror(Dart2JsMirrorSystem system, ClassElement _class) | 746 Dart2JsClassMirror(Dart2JsMirrorSystem system, ClassElement _class) |
| 749 : this.library = system.getLibrary(_class.getLibrary()), | 747 : this.library = system.getLibrary(_class.getLibrary()), |
| 750 super(system, _class); | 748 super(system, _class); |
| 751 | 749 |
| 752 ClassElement get _class => _element; | 750 ClassElement get _class => _element; |
| 753 | 751 |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1042 bool get isDynamic => false; | 1040 bool get isDynamic => false; |
| 1043 | 1041 |
| 1044 bool get isTypeVariable => false; | 1042 bool get isTypeVariable => false; |
| 1045 | 1043 |
| 1046 bool get isTypedef => false; | 1044 bool get isTypedef => false; |
| 1047 | 1045 |
| 1048 bool get isFunction => false; | 1046 bool get isFunction => false; |
| 1049 | 1047 |
| 1050 String toString() => _type.element.toString(); | 1048 String toString() => _type.element.toString(); |
| 1051 | 1049 |
| 1052 Map<String, MemberMirror> get declaredMembers => | |
| 1053 const <String, MemberMirror>{}; | |
| 1054 | |
| 1055 Map<String, MemberMirror> get members => const <String, MemberMirror>{}; | 1050 Map<String, MemberMirror> get members => const <String, MemberMirror>{}; |
| 1056 | 1051 |
| 1057 Map<String, MethodMirror> get constructors => const <String, MethodMirror>{}; | 1052 Map<String, MethodMirror> get constructors => const <String, MethodMirror>{}; |
| 1058 | 1053 |
| 1059 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; | 1054 Map<String, MethodMirror> get methods => const <String, MethodMirror>{}; |
| 1060 | 1055 |
| 1061 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; | 1056 Map<String, MethodMirror> get getters => const <String, MethodMirror>{}; |
| 1062 | 1057 |
| 1063 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; | 1058 Map<String, MethodMirror> get setters => const <String, MethodMirror>{}; |
| 1064 | 1059 |
| 1065 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; | 1060 Map<String, VariableMirror> get variables => const <String, VariableMirror>{}; |
| 1066 | 1061 |
| 1067 ClassMirror get defaultFactory => null; | 1062 ClassMirror get defaultFactory => null; |
| 1068 } | 1063 } |
| 1069 | 1064 |
| 1070 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror | 1065 class Dart2JsInterfaceTypeMirror extends Dart2JsTypeElementMirror |
| 1071 implements ClassMirror { | 1066 implements ClassMirror { |
| 1072 List<TypeMirror> _typeArguments; | 1067 List<TypeMirror> _typeArguments; |
| 1073 | 1068 |
| 1074 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, | 1069 Dart2JsInterfaceTypeMirror(Dart2JsMirrorSystem system, |
| 1075 InterfaceType interfaceType) | 1070 InterfaceType interfaceType) |
| 1076 : super(system, interfaceType); | 1071 : super(system, interfaceType); |
| 1077 | 1072 |
| 1078 InterfaceType get _interfaceType => _type; | 1073 InterfaceType get _interfaceType => _type; |
| 1079 | 1074 |
| 1080 String get qualifiedName => originalDeclaration.qualifiedName; | 1075 String get qualifiedName => originalDeclaration.qualifiedName; |
| 1081 | 1076 |
| 1082 // TODO(johnniwinther): Substitute type arguments for type variables. | 1077 // TODO(johnniwinther): Substitute type arguments for type variables. |
| 1083 Map<String, MemberMirror> get declaredMembers => | |
| 1084 originalDeclaration.declaredMembers; | |
| 1085 | |
| 1086 // TODO(johnniwinther): Substitute type arguments for type variables. | |
| 1087 Map<String, MemberMirror> get members => originalDeclaration.members; | 1078 Map<String, MemberMirror> get members => originalDeclaration.members; |
| 1088 | 1079 |
| 1089 bool get isObject => system.compiler.objectClass == _type.element; | 1080 bool get isObject => system.compiler.objectClass == _type.element; |
| 1090 | 1081 |
| 1091 bool get isDynamic => system.compiler.dynamicClass == _type.element; | 1082 bool get isDynamic => system.compiler.dynamicClass == _type.element; |
| 1092 | 1083 |
| 1093 ClassMirror get originalDeclaration | 1084 ClassMirror get originalDeclaration |
| 1094 => new Dart2JsClassMirror(system, _type.element); | 1085 => new Dart2JsClassMirror(system, _type.element); |
| 1095 | 1086 |
| 1096 // TODO(johnniwinther): Substitute type arguments for type variables. | 1087 // TODO(johnniwinther): Substitute type arguments for type variables. |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1179 : super(system, functionType) { | 1170 : super(system, functionType) { |
| 1180 assert (_functionSignature !== null); | 1171 assert (_functionSignature !== null); |
| 1181 } | 1172 } |
| 1182 | 1173 |
| 1183 FunctionType get _functionType => _type; | 1174 FunctionType get _functionType => _type; |
| 1184 | 1175 |
| 1185 // TODO(johnniwinther): Is this the qualified name of a function type? | 1176 // TODO(johnniwinther): Is this the qualified name of a function type? |
| 1186 String get qualifiedName => originalDeclaration.qualifiedName; | 1177 String get qualifiedName => originalDeclaration.qualifiedName; |
| 1187 | 1178 |
| 1188 // TODO(johnniwinther): Substitute type arguments for type variables. | 1179 // TODO(johnniwinther): Substitute type arguments for type variables. |
| 1189 Map<String, MemberMirror> get declaredMembers { | 1180 Map<String, MemberMirror> get members { |
| 1190 var method = callMethod; | 1181 var method = callMethod; |
| 1191 if (method !== null) { | 1182 if (method !== null) { |
| 1192 var map = new Map<String, MemberMirror>.from( | 1183 var map = new Map<String, MemberMirror>.from( |
| 1193 originalDeclaration.declaredMembers); | 1184 originalDeclaration.members); |
| 1194 var name = method.qualifiedName; | 1185 var name = method.qualifiedName; |
| 1195 assert(!map.containsKey(name)); | 1186 assert(!map.containsKey(name)); |
| 1196 map[name] = method; | 1187 map[name] = method; |
| 1197 return new ImmutableMapWrapper<String, MemberMirror>(map); | 1188 return new ImmutableMapWrapper<String, MemberMirror>(map); |
| 1198 } | 1189 } |
| 1199 return originalDeclaration.declaredMembers; | 1190 return originalDeclaration.members; |
| 1200 } | 1191 } |
| 1201 | 1192 |
| 1202 bool get isFunction => true; | 1193 bool get isFunction => true; |
| 1203 | 1194 |
| 1204 MethodMirror get callMethod => _convertElementMethodToMethodMirror( | 1195 MethodMirror get callMethod => _convertElementMethodToMethodMirror( |
| 1205 system.getLibrary(_functionType.element.getLibrary()), | 1196 system.getLibrary(_functionType.element.getLibrary()), |
| 1206 _functionType.element); | 1197 _functionType.element); |
| 1207 | 1198 |
| 1208 ClassMirror get originalDeclaration | 1199 ClassMirror get originalDeclaration |
| 1209 => new Dart2JsClassMirror(system, system.compiler.functionClass); | 1200 => new Dart2JsClassMirror(system, system.compiler.functionClass); |
| (...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1306 return other.isDynamic; | 1297 return other.isDynamic; |
| 1307 } | 1298 } |
| 1308 } | 1299 } |
| 1309 | 1300 |
| 1310 //------------------------------------------------------------------------------ | 1301 //------------------------------------------------------------------------------ |
| 1311 // Member mirrors implementation. | 1302 // Member mirrors implementation. |
| 1312 //------------------------------------------------------------------------------ | 1303 //------------------------------------------------------------------------------ |
| 1313 | 1304 |
| 1314 class Dart2JsMethodMirror extends Dart2JsMemberMirror | 1305 class Dart2JsMethodMirror extends Dart2JsMemberMirror |
| 1315 implements MethodMirror { | 1306 implements MethodMirror { |
| 1316 final Dart2JsObjectMirror _objectMirror; | 1307 final Dart2JsContainerMirror _objectMirror; |
| 1317 String _simpleName; | 1308 String _simpleName; |
| 1318 String _displayName; | 1309 String _displayName; |
| 1319 String _constructorName; | 1310 String _constructorName; |
| 1320 String _operatorName; | 1311 String _operatorName; |
| 1321 Dart2JsMethodKind _kind; | 1312 Dart2JsMethodKind _kind; |
| 1322 | 1313 |
| 1323 Dart2JsMethodMirror(Dart2JsObjectMirror objectMirror, | 1314 Dart2JsMethodMirror(Dart2JsContainerMirror objectMirror, |
| 1324 FunctionElement function) | 1315 FunctionElement function) |
| 1325 : this._objectMirror = objectMirror, | 1316 : this._objectMirror = objectMirror, |
| 1326 super(objectMirror.system, function) { | 1317 super(objectMirror.system, function) { |
| 1327 _simpleName = _element.name.slowToString(); | 1318 _simpleName = _element.name.slowToString(); |
| 1328 if (_function.kind == ElementKind.GETTER) { | 1319 if (_function.kind == ElementKind.GETTER) { |
| 1329 _kind = Dart2JsMethodKind.GETTER; | 1320 _kind = Dart2JsMethodKind.GETTER; |
| 1330 _displayName = _simpleName; | 1321 _displayName = _simpleName; |
| 1331 } else if (_function.kind == ElementKind.SETTER) { | 1322 } else if (_function.kind == ElementKind.SETTER) { |
| 1332 _kind = Dart2JsMethodKind.SETTER; | 1323 _kind = Dart2JsMethodKind.SETTER; |
| 1333 _displayName = _simpleName; | 1324 _displayName = _simpleName; |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1436 var script = _function.getCompilationUnit().script; | 1427 var script = _function.getCompilationUnit().script; |
| 1437 var span = system.compiler.spanFromNode(node, script.uri); | 1428 var span = system.compiler.spanFromNode(node, script.uri); |
| 1438 return new Dart2JsLocation(script, span); | 1429 return new Dart2JsLocation(script, span); |
| 1439 } | 1430 } |
| 1440 return super.location; | 1431 return super.location; |
| 1441 } | 1432 } |
| 1442 | 1433 |
| 1443 } | 1434 } |
| 1444 | 1435 |
| 1445 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { | 1436 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { |
| 1446 Dart2JsObjectMirror _objectMirror; | 1437 Dart2JsContainerMirror _objectMirror; |
| 1447 VariableElement _variable; | 1438 VariableElement _variable; |
| 1448 | 1439 |
| 1449 Dart2JsFieldMirror(Dart2JsObjectMirror objectMirror, | 1440 Dart2JsFieldMirror(Dart2JsContainerMirror objectMirror, |
| 1450 VariableElement variable) | 1441 VariableElement variable) |
| 1451 : this._objectMirror = objectMirror, | 1442 : this._objectMirror = objectMirror, |
| 1452 this._variable = variable, | 1443 this._variable = variable, |
| 1453 super(objectMirror.system, variable); | 1444 super(objectMirror.system, variable); |
| 1454 | 1445 |
| 1455 String get qualifiedName | 1446 String get qualifiedName |
| 1456 => '${owner.qualifiedName}.$simpleName'; | 1447 => '${owner.qualifiedName}.$simpleName'; |
| 1457 | 1448 |
| 1458 DeclarationMirror get owner => _objectMirror; | 1449 DeclarationMirror get owner => _objectMirror; |
| 1459 | 1450 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 1477 if (node !== null) { | 1468 if (node !== null) { |
| 1478 var span = system.compiler.spanFromNode(node, script.uri); | 1469 var span = system.compiler.spanFromNode(node, script.uri); |
| 1479 return new Dart2JsLocation(script, span); | 1470 return new Dart2JsLocation(script, span); |
| 1480 } else { | 1471 } else { |
| 1481 var span = system.compiler.spanFromElement(_variable); | 1472 var span = system.compiler.spanFromElement(_variable); |
| 1482 return new Dart2JsLocation(script, span); | 1473 return new Dart2JsLocation(script, span); |
| 1483 } | 1474 } |
| 1484 } | 1475 } |
| 1485 } | 1476 } |
| 1486 | 1477 |
| OLD | NEW |