| 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 19 matching lines...) Expand all Loading... |
| 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 InitializationCrawler(this.typeFilter, this.customFilter); |
| 34 | 34 |
| 35 // The primary function in this class, invoke it to crawl and collect all the | 35 // The primary function in this class, invoke it to crawl and collect all the |
| 36 // annotations into a queue of init functions. | 36 // annotations into a queue of init functions. |
| 37 Queue<Function> run() { | 37 Queue<Function> run() { |
| 38 var librariesSeen = new Set<LibraryMirror>(); | 38 var librariesSeen = new Set<LibraryMirror>(); |
| 39 var queue = new Queue<Function>(); | 39 var queue = new Queue<Function>(); |
| 40 var libraries = currentMirrorSystem().libraries; |
| 40 | 41 |
| 41 var libraries = currentMirrorSystem().libraries; | 42 var trampolineUri = Uri.parse('${_root.uri}\$trampoline'); |
| 42 var nonDartOrPackageImports = new List.from(libraries.keys.where( | 43 if (libraries.containsKey(trampolineUri)) { |
| 43 (uri) => uri.scheme != 'package' && uri.scheme != 'dart')); | 44 // In dartium, process all relative libraries in reverse order of when |
| 45 // they were seen. |
| 46 // TODO(jakemac): This is an approximation of what we actually want. |
| 47 // https://github.com/dart-lang/initialize/issues/25 |
| 48 var relativeLibraryUris = new List.from(libraries.keys.where( |
| 49 (uri) => uri.scheme != 'package' && uri.scheme != 'dart')); |
| 44 | 50 |
| 45 for (var import in nonDartOrPackageImports.reversed) { | 51 for (var import in relativeLibraryUris.reversed) { |
| 46 // Always load the package: version of a library if available. | 52 // Always load the package: version of a library if available for |
| 47 var libToRun; | 53 // canonicalization purposes. |
| 48 if (_isHttpStylePackageUrl(import)) { | 54 var libToRun; |
| 49 var packageUri = _packageUriFor(import); | 55 if (_isHttpStylePackageUrl(import)) { |
| 50 libToRun = libraries[packageUri]; | 56 var packageUri = _packageUriFor(import); |
| 57 libToRun = libraries[packageUri]; |
| 58 } |
| 59 if (libToRun == null) libToRun = libraries[import]; |
| 60 |
| 61 // Dartium creates an extra trampoline lib that loads the main dart scri
pt |
| 62 // and breaks our ordering, we should skip it. |
| 63 if (librariesSeen.contains(libToRun) || |
| 64 libToRun.uri.path.endsWith('\$trampoline')) { |
| 65 continue; |
| 66 } |
| 67 _readLibraryDeclarations(libToRun, librariesSeen, queue); |
| 51 } | 68 } |
| 52 if (libToRun == null) libToRun = libraries[import]; | 69 } else { |
| 53 | 70 // Not in dartium, just process from the root library. |
| 54 // Dartium creates an extra trampoline lib that loads the main dart script | 71 _readLibraryDeclarations(_root, librariesSeen, queue); |
| 55 // and breaks our ordering. | |
| 56 if (librariesSeen.contains(libToRun) || | |
| 57 libToRun.uri.path.endsWith('\$trampoline')) { | |
| 58 continue; | |
| 59 } | |
| 60 _readLibraryDeclarations(libToRun, librariesSeen, queue); | |
| 61 } | 72 } |
| 62 | 73 |
| 63 return queue; | 74 return queue; |
| 64 } | 75 } |
| 65 | 76 |
| 77 |
| 66 /// Whether [uri] is an http URI that contains a 'packages' segment, and | 78 /// Whether [uri] is an http URI that contains a 'packages' segment, and |
| 67 /// therefore could be converted into a 'package:' URI. | 79 /// therefore could be converted into a 'package:' URI. |
| 68 bool _isHttpStylePackageUrl(Uri uri) { | 80 bool _isHttpStylePackageUrl(Uri uri) { |
| 69 var uriPath = uri.path; | 81 var uriPath = uri.path; |
| 70 return uri.scheme == _root.uri.scheme && | 82 return uri.scheme == _root.uri.scheme && |
| 71 // Don't process cross-domain uris. | 83 // Don't process cross-domain uris. |
| 72 uri.authority == _root.uri.authority && | 84 uri.authority == _root.uri.authority && |
| 73 uriPath.endsWith('.dart') && | 85 uriPath.endsWith('.dart') && |
| 74 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); | 86 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
| 75 } | 87 } |
| (...skipping 151 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 return true; | 239 return true; |
| 228 } | 240 } |
| 229 } | 241 } |
| 230 | 242 |
| 231 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( | 243 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( |
| 232 'Only top level methods are supported for initializers'); | 244 'Only top level methods are supported for initializers'); |
| 233 | 245 |
| 234 final _UNSUPPORTED_DECLARATION = new UnsupportedError( | 246 final _UNSUPPORTED_DECLARATION = new UnsupportedError( |
| 235 'Initializers are only supported on libraries, classes, and top level ' | 247 'Initializers are only supported on libraries, classes, and top level ' |
| 236 'methods'); | 248 'methods'); |
| OLD | NEW |