Chromium Code Reviews| 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 |
| 11 final _root = currentMirrorSystem().isolate.rootLibrary; | 11 final _root = currentMirrorSystem().isolate.rootLibrary; |
| 12 | 12 |
| 13 Queue<Function> loadInitializers( | 13 Queue<Function> loadInitializers( |
| 14 {List<Type> typeFilter, InitializerFilter customFilter}) { | 14 {List<Type> typeFilter, InitializerFilter customFilter, Uri from}) { |
| 15 return new InitializationCrawler(typeFilter, customFilter).run(); | 15 return new InitializationCrawler(typeFilter, customFilter, from: from).run(); |
| 16 } | 16 } |
| 17 | 17 |
| 18 // Crawls a library and all its dependencies for `Initializer` annotations using | 18 // Crawls a library and all its dependencies for `Initializer` annotations using |
| 19 // mirrors | 19 // mirrors |
| 20 class InitializationCrawler { | 20 class InitializationCrawler { |
| 21 // Set of all visited annotations, keys are the declarations that were | 21 // Set of all visited annotations, keys are the declarations that were |
| 22 // annotated, values are the annotations that have been processed. | 22 // annotated, values are the annotations that have been processed. |
| 23 static final _annotationsFound = | 23 static final _annotationsFound = |
| 24 new Map<DeclarationMirror, Set<InstanceMirror>>(); | 24 new Map<DeclarationMirror, Set<InstanceMirror>>(); |
| 25 | 25 |
| 26 // If non-null, then only these annotations should be processed. | 26 // If non-null, then only these annotations should be processed. |
| 27 final List<Type> typeFilter; | 27 final List<Type> typeFilter; |
| 28 | 28 |
| 29 // If non-null, then only annotations which return true when passed to this | 29 // If non-null, then only annotations which return true when passed to this |
| 30 // function will be processed. | 30 // function will be processed. |
| 31 final InitializerFilter customFilter; | 31 final InitializerFilter customFilter; |
| 32 | 32 |
| 33 InitializationCrawler(this.typeFilter, this.customFilter); | 33 /// Optional property, if supplied then crawling will run from this library |
| 34 /// only. | |
| 35 /// | |
| 36 /// Note: This is only supported in the mirror_loader.dart. It is not | |
| 37 /// supported statically. | |
| 38 final LibraryMirror _rootLibrary; | |
| 39 | |
| 40 InitializationCrawler(this.typeFilter, this.customFilter, {Uri from}) | |
| 41 : _rootLibrary = currentMirrorSystem().libraries[from] { | |
| 42 if (from != null && _rootLibrary == null) { | |
| 43 throw 'Unable to find library at $from.'; | |
| 44 } | |
| 45 } | |
| 34 | 46 |
| 35 // The primary function in this class, invoke it to crawl and collect all the | 47 // The primary function in this class, invoke it to crawl and collect all the |
| 36 // annotations into a queue of init functions. | 48 // annotations into a queue of init functions. |
| 37 Queue<Function> run() { | 49 Queue<Function> run() { |
| 38 var librariesSeen = new Set<LibraryMirror>(); | 50 var librariesSeen = new Set<LibraryMirror>(); |
| 39 var queue = new Queue<Function>(); | 51 var queue = new Queue<Function>(); |
| 40 var libraries = currentMirrorSystem().libraries; | 52 var libraries = currentMirrorSystem().libraries; |
| 41 | 53 |
| 42 var trampolineUri = Uri.parse('${_root.uri}\$trampoline'); | 54 if (_rootLibrary != null) { |
| 43 if (libraries.containsKey(trampolineUri)) { | 55 _readLibraryDeclarations(_rootLibrary, librariesSeen, queue); |
| 56 } else if (libraries.containsKey(Uri.parse('${_root.uri}\$trampoline'))) { | |
| 44 // In dartium, process all relative libraries in reverse order of when | 57 // In dartium, process all relative libraries in reverse order of when |
| 45 // they were seen. | 58 // they were seen. |
| 46 // TODO(jakemac): This is an approximation of what we actually want. | 59 // TODO(jakemac): This is an approximation of what we actually want. |
| 47 // https://github.com/dart-lang/initialize/issues/25 | 60 // https://github.com/dart-lang/initialize/issues/25 |
|
Siggi Cherem (dart-lang)
2015/03/23 20:25:55
Now that we have `from:`, we can go ahead and dele
jakemac
2015/03/23 20:58:04
done, and bumped version to a breaking change
| |
| 48 var relativeLibraryUris = new List.from(libraries.keys | 61 var relativeLibraryUris = new List.from(libraries.keys |
| 49 .where((uri) => uri.scheme != 'package' && uri.scheme != 'dart')); | 62 .where((uri) => uri.scheme != 'package' && uri.scheme != 'dart')); |
| 50 | 63 |
| 51 for (var import in relativeLibraryUris.reversed) { | 64 for (var import in relativeLibraryUris.reversed) { |
| 52 // Always load the package: version of a library if available for | 65 // Always load the package: version of a library if available for |
| 53 // canonicalization purposes. | 66 // canonicalization purposes. |
| 54 var libToRun; | 67 var libToRun; |
| 55 if (_isHttpStylePackageUrl(import)) { | 68 if (_isHttpStylePackageUrl(import)) { |
| 56 var packageUri = _packageUriFor(import); | 69 var packageUri = _packageUriFor(import); |
| 57 libToRun = libraries[packageUri]; | 70 libToRun = libraries[packageUri]; |
| (...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 239 return true; | 252 return true; |
| 240 } | 253 } |
| 241 } | 254 } |
| 242 | 255 |
| 243 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( | 256 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( |
| 244 'Only top level methods are supported for initializers'); | 257 'Only top level methods are supported for initializers'); |
| 245 | 258 |
| 246 final _UNSUPPORTED_DECLARATION = new UnsupportedError( | 259 final _UNSUPPORTED_DECLARATION = new UnsupportedError( |
| 247 'Initializers are only supported on libraries, classes, and top level ' | 260 'Initializers are only supported on libraries, classes, and top level ' |
| 248 'methods'); | 261 'methods'); |
| OLD | NEW |