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 27 matching lines...) Expand all Loading... |
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 var libraries = currentMirrorSystem().libraries; |
41 | 41 |
42 var trampolineUri = Uri.parse('${_root.uri}\$trampoline'); | 42 var trampolineUri = Uri.parse('${_root.uri}\$trampoline'); |
43 if (libraries.containsKey(trampolineUri)) { | 43 if (libraries.containsKey(trampolineUri)) { |
44 // In dartium, process all relative libraries in reverse order of when | 44 // In dartium, process all relative libraries in reverse order of when |
45 // they were seen. | 45 // they were seen. |
46 // TODO(jakemac): This is an approximation of what we actually want. | 46 // TODO(jakemac): This is an approximation of what we actually want. |
47 // https://github.com/dart-lang/initialize/issues/25 | 47 // https://github.com/dart-lang/initialize/issues/25 |
48 var relativeLibraryUris = new List.from(libraries.keys.where( | 48 var relativeLibraryUris = new List.from(libraries.keys |
49 (uri) => uri.scheme != 'package' && uri.scheme != 'dart')); | 49 .where((uri) => uri.scheme != 'package' && uri.scheme != 'dart')); |
50 | 50 |
51 for (var import in relativeLibraryUris.reversed) { | 51 for (var import in relativeLibraryUris.reversed) { |
52 // Always load the package: version of a library if available for | 52 // Always load the package: version of a library if available for |
53 // canonicalization purposes. | 53 // canonicalization purposes. |
54 var libToRun; | 54 var libToRun; |
55 if (_isHttpStylePackageUrl(import)) { | 55 if (_isHttpStylePackageUrl(import)) { |
56 var packageUri = _packageUriFor(import); | 56 var packageUri = _packageUriFor(import); |
57 libToRun = libraries[packageUri]; | 57 libToRun = libraries[packageUri]; |
58 } | 58 } |
59 if (libToRun == null) libToRun = libraries[import]; | 59 if (libToRun == null) libToRun = libraries[import]; |
60 | 60 |
61 // Dartium creates an extra trampoline lib that loads the main dart scri
pt | 61 // Dartium creates an extra trampoline lib that loads the main dart scri
pt |
62 // and breaks our ordering, we should skip it. | 62 // and breaks our ordering, we should skip it. |
63 if (librariesSeen.contains(libToRun) || | 63 if (librariesSeen.contains(libToRun) || |
64 libToRun.uri.path.endsWith('\$trampoline')) { | 64 libToRun.uri.path.endsWith('\$trampoline')) { |
65 continue; | 65 continue; |
66 } | 66 } |
67 _readLibraryDeclarations(libToRun, librariesSeen, queue); | 67 _readLibraryDeclarations(libToRun, librariesSeen, queue); |
68 } | 68 } |
69 } else { | 69 } else { |
70 // Not in dartium, just process from the root library. | 70 // Not in dartium, just process from the root library. |
71 _readLibraryDeclarations(_root, librariesSeen, queue); | 71 _readLibraryDeclarations(_root, librariesSeen, queue); |
72 } | 72 } |
73 | 73 |
74 return queue; | 74 return queue; |
75 } | 75 } |
76 | 76 |
77 | |
78 /// Whether [uri] is an http URI that contains a 'packages' segment, and | 77 /// Whether [uri] is an http URI that contains a 'packages' segment, and |
79 /// therefore could be converted into a 'package:' URI. | 78 /// therefore could be converted into a 'package:' URI. |
80 bool _isHttpStylePackageUrl(Uri uri) { | 79 bool _isHttpStylePackageUrl(Uri uri) { |
81 var uriPath = uri.path; | 80 var uriPath = uri.path; |
82 return uri.scheme == _root.uri.scheme && | 81 return uri.scheme == _root.uri.scheme && |
83 // Don't process cross-domain uris. | 82 // Don't process cross-domain uris. |
84 uri.authority == _root.uri.authority && | 83 uri.authority == _root.uri.authority && |
85 uriPath.endsWith('.dart') && | 84 uriPath.endsWith('.dart') && |
86 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); | 85 (uriPath.contains('/packages/') || uriPath.startsWith('packages/')); |
87 } | 86 } |
88 | 87 |
89 Uri _packageUriFor(Uri httpUri) { | 88 Uri _packageUriFor(Uri httpUri) { |
90 var packagePath = httpUri.path.substring( | 89 var packagePath = httpUri.path |
91 httpUri.path.lastIndexOf('packages/') + 'packages/'.length); | 90 .substring(httpUri.path.lastIndexOf('packages/') + 'packages/'.length); |
92 return Uri.parse('package:$packagePath'); | 91 return Uri.parse('package:$packagePath'); |
93 } | 92 } |
94 | 93 |
95 // Reads Initializer annotations on this library and all its dependencies in | 94 // Reads Initializer annotations on this library and all its dependencies in |
96 // post-order. | 95 // post-order. |
97 Queue<Function> _readLibraryDeclarations(LibraryMirror lib, | 96 Queue<Function> _readLibraryDeclarations(LibraryMirror lib, |
98 Set<LibraryMirror> librariesSeen, Queue<Function> queue) { | 97 Set<LibraryMirror> librariesSeen, Queue<Function> queue) { |
99 librariesSeen.add(lib); | 98 librariesSeen.add(lib); |
100 | 99 |
101 // First visit all our dependencies. | 100 // First visit all our dependencies. |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 return true; | 238 return true; |
240 } | 239 } |
241 } | 240 } |
242 | 241 |
243 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( | 242 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( |
244 'Only top level methods are supported for initializers'); | 243 'Only top level methods are supported for initializers'); |
245 | 244 |
246 final _UNSUPPORTED_DECLARATION = new UnsupportedError( | 245 final _UNSUPPORTED_DECLARATION = new UnsupportedError( |
247 'Initializers are only supported on libraries, classes, and top level ' | 246 'Initializers are only supported on libraries, classes, and top level ' |
248 'methods'); | 247 'methods'); |
OLD | NEW |