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 30 matching lines...) Expand all Loading... |
41 ? _root | 41 ? _root |
42 : _libs[from] { | 42 : _libs[from] { |
43 if (_rootLibrary == null) throw 'Unable to find library at $from.'; | 43 if (_rootLibrary == null) throw 'Unable to find library at $from.'; |
44 } | 44 } |
45 | 45 |
46 // The primary function in this class, invoke it to crawl and collect all the | 46 // The primary function in this class, invoke it to crawl and collect all the |
47 // annotations into a queue of init functions. | 47 // annotations into a queue of init functions. |
48 Queue<Function> run() { | 48 Queue<Function> run() { |
49 var librariesSeen = new Set<LibraryMirror>(); | 49 var librariesSeen = new Set<LibraryMirror>(); |
50 var queue = new Queue<Function>(); | 50 var queue = new Queue<Function>(); |
51 var libraries = currentMirrorSystem().libraries; | |
52 | 51 |
53 _readLibraryDeclarations(_rootLibrary, librariesSeen, queue); | 52 _readLibraryDeclarations(_rootLibrary, librariesSeen, queue); |
54 return queue; | 53 return queue; |
55 } | 54 } |
56 | 55 |
57 /// Returns the canonical [LibraryMirror] for a given [LibraryMirror]. This | 56 /// Returns the canonical [LibraryMirror] for a given [LibraryMirror]. This |
58 /// is defined as the one loaded from a `package:` url if available, otherwise | 57 /// is defined as the one loaded from a `package:` url if available, otherwise |
59 /// it is just [lib]. | 58 /// it is just [lib]. |
60 LibraryMirror _canonicalLib(LibraryMirror lib) { | 59 LibraryMirror _canonicalLib(LibraryMirror lib) { |
61 var uri = lib.uri; | 60 var uri = lib.uri; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 // Run parts first if one is from the original library. | 144 // Run parts first if one is from the original library. |
146 if (aSourceUri == sourceLib.uri) return 1; | 145 if (aSourceUri == sourceLib.uri) return 1; |
147 if (bSourceUri == sourceLib.uri) return -1; | 146 if (bSourceUri == sourceLib.uri) return -1; |
148 | 147 |
149 // Sort parts alphabetically. | 148 // Sort parts alphabetically. |
150 return aSourceUri.path.compareTo(bSourceUri.path); | 149 return aSourceUri.path.compareTo(bSourceUri.path); |
151 }); | 150 }); |
152 return declarationList; | 151 return declarationList; |
153 } | 152 } |
154 | 153 |
155 String _declarationName(DeclarationMirror declaration) => | |
156 MirrorSystem.getName(declaration.qualifiedName); | |
157 | |
158 /// Reads annotations on a [DeclarationMirror] and adds them to [_initQueue] | 154 /// Reads annotations on a [DeclarationMirror] and adds them to [_initQueue] |
159 /// if they are [Initializer]s. | 155 /// if they are [Initializer]s. |
160 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) { | 156 void _readAnnotations(DeclarationMirror declaration, Queue<Function> queue) { |
161 var annotations = | 157 var annotations = |
162 declaration.metadata.where((m) => _filterMetadata(declaration, m)); | 158 declaration.metadata.where((m) => _filterMetadata(declaration, m)); |
163 for (var meta in annotations) { | 159 for (var meta in annotations) { |
164 _annotationsFound.putIfAbsent( | 160 _annotationsFound.putIfAbsent( |
165 declaration, () => new Set<InstanceMirror>()); | 161 declaration, () => new Set<InstanceMirror>()); |
166 _annotationsFound[declaration].add(meta); | 162 _annotationsFound[declaration].add(meta); |
167 | 163 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 return true; | 248 return true; |
253 } | 249 } |
254 } | 250 } |
255 | 251 |
256 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( | 252 final _TOP_LEVEL_FUNCTIONS_ONLY = new UnsupportedError( |
257 'Only top level methods are supported for initializers'); | 253 'Only top level methods are supported for initializers'); |
258 | 254 |
259 final _UNSUPPORTED_DECLARATION = new UnsupportedError( | 255 final _UNSUPPORTED_DECLARATION = new UnsupportedError( |
260 'Initializers are only supported on libraries, classes, and top level ' | 256 'Initializers are only supported on libraries, classes, and top level ' |
261 'methods'); | 257 'methods'); |
OLD | NEW |