Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library mirror_system_helper; | |
| 6 | |
| 7 import 'dart:async'; | |
| 8 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; | |
| 9 import '../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mirro r.dart'; | |
| 10 import 'mock_compiler.dart'; | |
| 11 | |
| 12 export '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors.dart' ; | |
| 13 export '../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_util. dart'; | |
| 14 | |
| 15 const String SOURCE = 'source'; | |
| 16 final Uri SOURCE_URI = new Uri(scheme: SOURCE, path: SOURCE); | |
| 17 | |
| 18 // TODO(johnniwinther): Move this to a mirrors helper library. | |
| 19 Future<MirrorSystem> createMirrorSystem(String source) { | |
| 20 MockCompiler compiler = new MockCompiler( | |
| 21 analyzeOnly: true, | |
| 22 analyzeAll: true, | |
| 23 preserveComments: true); | |
| 24 compiler.registerSource(SOURCE_URI, source); | |
| 25 compiler.librariesToAnalyzeWhenRun = <Uri>[SOURCE_URI]; | |
| 26 return compiler.runCompiler(null).then((_) { | |
| 27 return new Dart2JsMirrorSystem(compiler); | |
| 28 }); | |
| 29 } | |
| 30 | |
| 31 /** | |
| 32 * Returns [:true:] if [type] is an instance of [:decl:] with type arguments | |
| 33 * equal to [typeArgument]. | |
| 34 */ | |
| 35 bool isType(ClassMirror decl, List<TypeMirror> typeArguments, | |
|
karlklose
2013/11/05 10:34:18
'isInstance'?
Johnni Winther
2013/11/05 11:20:47
Done.
| |
| 36 ClassMirror type) { | |
| 37 if (type.isOriginalDeclaration) return false; | |
| 38 if (!sameDecl(decl, type)) return false; | |
| 39 return equalsTypes(typeArguments, type.typeArguments); | |
| 40 } | |
| 41 | |
| 42 /** | |
| 43 * Returns [:true:] if [type] is the same type as [expected]. This method | |
| 44 * equates a non-generic declaration with its instantiation. | |
| 45 */ | |
| 46 bool equalsType(TypeMirror expected, TypeMirror type) { | |
|
karlklose
2013/11/05 10:34:18
'isEqualType'?
Johnni Winther
2013/11/05 11:20:47
Done.
| |
| 47 if (expected == type) return true; | |
| 48 if (expected is ClassMirror && type is ClassMirror) { | |
| 49 if (!sameDecl(expected, type)) return false; | |
| 50 if (expected.isOriginalDeclaration || expected.typeArguments.isEmpty) { | |
| 51 return type.isOriginalDeclaration || type.typeArguments.isEmpty; | |
| 52 } | |
| 53 return equalsTypes(expected.typeArguments, type.typeArguments); | |
| 54 } | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 /** | |
| 59 * Returns [:true:] if [types] are equals to [expected] using the equalitry | |
| 60 * defined by [equalsType]. | |
| 61 */ | |
| 62 bool equalsTypes(List<TypeMirror> expected, List<TypeMirror> types) { | |
|
karlklose
2013/11/05 10:34:18
Ditto.
Johnni Winther
2013/11/05 11:20:47
Done.
| |
| 63 return checkSameList(expected, types, equalsType); | |
| 64 } | |
| 65 | |
| 66 /** | |
| 67 * Returns [:true:] if an instance of [type] with type arguments equal to | |
| 68 * [typeArguments] is found in [types]. | |
| 69 */ | |
| 70 bool containsType(ClassMirror decl, List<TypeMirror> typeArguments, | |
| 71 Iterable<TypeMirror> types) { | |
| 72 for (var type in types) { | |
|
karlklose
2013/11/05 10:34:18
types.any((type) => isType(decl, typeArguments, ty
Johnni Winther
2013/11/05 11:20:47
Done.
| |
| 73 if (isType(decl, typeArguments, type)) { | |
| 74 return true; | |
| 75 } | |
| 76 } | |
| 77 return false; | |
| 78 } | |
| 79 | |
| 80 /** | |
| 81 * Returns the declaration of [type]. | |
| 82 */ | |
| 83 TypeMirror toDecl(TypeMirror type) { | |
|
karlklose
2013/11/05 10:34:18
'Decl' -> 'Declaration'? (also multiple times belo
Johnni Winther
2013/11/05 11:20:47
Done.
| |
| 84 return type is ClassMirror ? type.originalDeclaration : type; | |
| 85 } | |
| 86 | |
| 87 /** | |
| 88 * Returns [:true:] if [type] is of the same declaration as [expected]. | |
| 89 */ | |
| 90 bool sameDecl(TypeMirror expected, TypeMirror type) { | |
| 91 return toDecl(expected) == toDecl(type); | |
| 92 } | |
| 93 | |
| 94 /** | |
| 95 * Returns [:true:] if a type of the declaration of [expected] is in [types]. | |
| 96 */ | |
| 97 bool containsDecl(TypeMirror expected, Iterable<TypeMirror> types) { | |
| 98 for (var type in types) { | |
| 99 if (sameDecl(expected, type)) { | |
| 100 return true; | |
| 101 } | |
| 102 } | |
| 103 return false; | |
| 104 } | |
| 105 | |
| 106 /** | |
| 107 * Returns [:true:] if declarations of [expected] are the same as those of | |
| 108 * [types], taking order into account. | |
| 109 */ | |
| 110 bool sameDeclList(Iterable<TypeMirror> expected, | |
| 111 Iterable<TypeMirror> types) { | |
| 112 return checkSameList(expected, types, sameDecl); | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Returns [:true:] if declarations of [expected] are the same as those of | |
| 117 * [iterable], not taking order into account. | |
| 118 */ | |
| 119 bool sameDeclSet(Iterable<TypeMirror> expected, | |
| 120 Iterable<TypeMirror> types) { | |
| 121 Set<TypeMirror> expectedSet = expected.map(toDecl).toSet(); | |
| 122 Set<TypeMirror> typesSet = types.map(toDecl).toSet(); | |
| 123 return expectedSet.length == typesSet.length && | |
| 124 expectedSet.containsAll(typesSet); | |
| 125 } | |
| 126 | |
| 127 /** | |
| 128 * Utility method for checking whether [expected] and [iterable] contains the | |
| 129 * same elements with respect to the checking function [check], takin order | |
| 130 * into account. | |
| 131 */ | |
| 132 bool checkSameList(Iterable<TypeMirror> expected, | |
| 133 Iterable<TypeMirror> types, | |
| 134 bool check(TypeMirror a, TypeMirror b)) { | |
| 135 if (expected.length != types.length) return false; | |
| 136 Iterator<TypeMirror> expectedIterator = expected.iterator; | |
| 137 Iterator<TypeMirror> typesIterator = types.iterator; | |
| 138 while (expectedIterator.moveNext() && typesIterator.moveNext()) { | |
| 139 if (!check(expectedIterator.current, typesIterator.current)) { | |
| 140 return false; | |
| 141 } | |
| 142 } | |
| 143 return true; | |
| 144 } | |
| OLD | NEW |