OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 library initialize.mirror_loader; | 4 library initialize.mirror_loader; |
5 | 5 |
6 import 'dart:collection' show Queue; | 6 import 'dart:collection' show Queue; |
7 import 'dart:mirrors'; | 7 import 'dart:mirrors'; |
8 import 'package:path/path.dart' as path; | 8 import 'package:path/path.dart' as path; |
9 import 'package:initialize/initialize.dart'; | 9 import 'package:initialize/initialize.dart'; |
10 | 10 |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
91 return Uri.parse('package:$packagePath'); | 91 return Uri.parse('package:$packagePath'); |
92 } | 92 } |
93 | 93 |
94 // Reads Initializer annotations on this library and all its dependencies in | 94 // Reads Initializer annotations on this library and all its dependencies in |
95 // post-order. | 95 // post-order. |
96 Queue<Function> _readLibraryDeclarations(LibraryMirror lib, | 96 Queue<Function> _readLibraryDeclarations(LibraryMirror lib, |
97 Set<LibraryMirror> librariesSeen, Queue<Function> queue) { | 97 Set<LibraryMirror> librariesSeen, Queue<Function> queue) { |
98 librariesSeen.add(lib); | 98 librariesSeen.add(lib); |
99 | 99 |
100 // First visit all our dependencies. | 100 // First visit all our dependencies. |
101 for (var dependency in _sortedLibraryDependencies(lib)) { | 101 for (var dependency in lib.libraryDependencies) { |
102 // Skip dart: imports, they never use this package. | 102 // Skip dart: imports, they never use this package. |
103 if (dependency.targetLibrary.uri.toString().startsWith('dart:')) continue; | 103 if (dependency.targetLibrary.uri.toString().startsWith('dart:')) continue; |
104 if (librariesSeen.contains(dependency.targetLibrary)) continue; | 104 if (librariesSeen.contains(dependency.targetLibrary)) continue; |
105 | 105 |
106 _readLibraryDeclarations(dependency.targetLibrary, librariesSeen, queue); | 106 _readLibraryDeclarations(dependency.targetLibrary, librariesSeen, queue); |
107 } | 107 } |
108 | 108 |
109 // Second parse the library directive annotations. | 109 // Second parse the library directive annotations. |
110 _readAnnotations(lib, queue); | 110 _readAnnotations(lib, queue); |
111 | 111 |
112 // Last, parse all class and method annotations. | 112 // Last, parse all class and method annotations. |
113 for (var declaration in _sortedLibraryDeclarations(lib)) { | 113 for (var declaration in _sortedLibraryDeclarations(lib)) { |
114 _readAnnotations(declaration, queue); | 114 _readAnnotations(declaration, queue); |
115 // Check classes for static annotations which are not supported | 115 // Check classes for static annotations which are not supported |
116 if (declaration is ClassMirror) { | 116 if (declaration is ClassMirror) { |
117 for (var classDeclaration in declaration.declarations.values) { | 117 for (var classDeclaration in declaration.declarations.values) { |
118 _readAnnotations(classDeclaration, queue); | 118 _readAnnotations(classDeclaration, queue); |
119 } | 119 } |
120 } | 120 } |
121 } | 121 } |
122 | 122 |
123 return queue; | 123 return queue; |
124 } | 124 } |
125 | 125 |
126 Iterable<LibraryDependencyMirror> _sortedLibraryDependencies( | |
127 LibraryMirror lib) => new List.from(lib.libraryDependencies) | |
128 ..sort((a, b) { | |
129 var aScheme = a.targetLibrary.uri.scheme; | |
130 var bScheme = b.targetLibrary.uri.scheme; | |
131 if (aScheme != 'file' && bScheme == 'file') return -1; | |
132 if (bScheme != 'file' && aScheme == 'file') return 1; | |
133 return _relativeLibraryUri(a).compareTo(_relativeLibraryUri(b)); | |
134 }); | |
135 | |
136 String _relativeLibraryUri(LibraryDependencyMirror lib) { | |
137 if (lib.targetLibrary.uri.scheme == 'file' && | |
138 lib.sourceLibrary.uri.scheme == 'file') { | |
139 return path.relative(lib.targetLibrary.uri.path, | |
140 from: path.dirname(lib.sourceLibrary.uri.path)); | |
141 } | |
142 return lib.targetLibrary.uri.toString(); | |
143 } | |
144 | |
145 Iterable<DeclarationMirror> _sortedLibraryDeclarations(LibraryMirror lib) => | 126 Iterable<DeclarationMirror> _sortedLibraryDeclarations(LibraryMirror lib) => |
146 lib.declarations.values | 127 lib.declarations.values |
147 .where((d) => d is ClassMirror || d is MethodMirror) | 128 .where((d) => d is ClassMirror || d is MethodMirror) |
148 .toList() | 129 .toList() |
149 ..sort((a, b) { | 130 ..sort((a, b) { |
150 if (a is MethodMirror && b is ClassMirror) return -1; | 131 if (a is MethodMirror && b is ClassMirror) return -1; |
151 if (a is ClassMirror && b is MethodMirror) return 1; | 132 if (a is ClassMirror && b is MethodMirror) return 1; |
152 return _declarationName(a).compareTo(_declarationName(b)); | 133 return 0; |
Siggi Cherem (dart-lang)
2015/03/17 19:50:29
this might be problematic depending on the size of
jakemac
2015/03/17 21:54:03
This now filters out all declarations that don't h
| |
153 }); | 134 }); |
154 | 135 |
155 String _declarationName(DeclarationMirror declaration) => | 136 String _declarationName(DeclarationMirror declaration) => |
156 MirrorSystem.getName(declaration.qualifiedName); | 137 MirrorSystem.getName(declaration.qualifiedName); |
157 | 138 |
158 // Reads annotations on declarations and adds them to `_initQueue` if they are | 139 // Reads annotations on declarations and adds them to `_initQueue` if they are |
159 // initializers. | 140 // initializers. |
160 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) { | 141 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) { |
161 var annotations = | 142 var annotations = |
162 declaration.metadata.where((m) => _filterMetadata(declaration, m)); | 143 declaration.metadata.where((m) => _filterMetadata(declaration, m)); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
238 return true; | 219 return true; |
239 } | 220 } |
240 } | 221 } |
241 | 222 |
242 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( | 223 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( |
243 'Only top level methods are supported for initializers'); | 224 'Only top level methods are supported for initializers'); |
244 | 225 |
245 final _UNSUPPORTED_DECLARATION = new UnsupportedError( | 226 final _UNSUPPORTED_DECLARATION = new UnsupportedError( |
246 'Initializers are only supported on libraries, classes, and top level ' | 227 'Initializers are only supported on libraries, classes, and top level ' |
247 'methods'); | 228 'methods'); |
OLD | NEW |