OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 4 |
5 library dart2js.library_loader; | 5 library dart2js.library_loader; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart2jslib.dart' | 8 import 'dart2jslib.dart' |
9 show Compiler, | 9 show Compiler, |
10 CompilerTask, | 10 CompilerTask, |
(...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
116 * import 'package:foo.dart' as a; | 116 * import 'package:foo.dart' as a; |
117 * import 'packages/foo.dart' as b; | 117 * import 'packages/foo.dart' as b; |
118 * | 118 * |
119 * do _not_ resolve to the same library when the package root URI happens to | 119 * do _not_ resolve to the same library when the package root URI happens to |
120 * point to the 'packages' folder. | 120 * point to the 'packages' folder. |
121 * | 121 * |
122 */ | 122 */ |
123 abstract class LibraryLoaderTask implements CompilerTask { | 123 abstract class LibraryLoaderTask implements CompilerTask { |
124 factory LibraryLoaderTask(Compiler compiler) = _LibraryLoaderTask; | 124 factory LibraryLoaderTask(Compiler compiler) = _LibraryLoaderTask; |
125 | 125 |
126 /** | 126 /// Returns all libraries that have been loaded. |
127 * Loads the library specified by the [resolvedUri] and returns its | 127 Iterable<LibraryElement> get libraries; |
128 * [LibraryElement]. | 128 |
129 * | 129 /// Looks up the library with the [canonicalUri]. |
130 * If the library is not already loaded, the method creates the | 130 LibraryElement lookupLibrary(Uri canonicalUri); |
131 * [LibraryElement] for the library and computes the import/export scope, | 131 |
132 * loading and computing the import/export scopes of all required libraries in | 132 /// Loads the library specified by the [resolvedUri] and returns its |
133 * the process. The method handles cyclic dependency between libraries. | 133 /// [LibraryElement]. |
134 */ | 134 /// |
135 /// If the library is not already loaded, the method creates the | |
136 /// [LibraryElement] for the library and computes the import/export scope, | |
137 /// loading and computing the import/export scopes of all required libraries | |
138 /// in the process. The method handles cyclic dependency between libraries. | |
135 Future<LibraryElement> loadLibrary(Uri resolvedUri); | 139 Future<LibraryElement> loadLibrary(Uri resolvedUri); |
136 | 140 |
137 /// Reset the library loader task to prepare for compilation. This is used | 141 /// Reset the library loader task to prepare for compilation. If provided, |
138 /// for incremental compilation. | 142 /// libraries matching [reuseLibrary] are reused. |
139 void reset(); | 143 /// |
140 | 144 /// This method is used for incremental compilation. |
141 /// Reuse [library] from a previous compilation. This is used for incremental | 145 void reset([bool reuseLibrary(LibraryElement library)]); |
floitsch
2014/06/24 13:43:05
I think it would be easier to read (at use-sites)
Johnni Winther
2014/06/24 14:50:29
Changed to named parameter.
| |
142 /// compilation. | |
143 void reuseLibrary(LibraryElement library); | |
144 } | 146 } |
145 | 147 |
146 /// Handle for creating synthesized/patch libraries during library loading. | 148 /// Handle for creating synthesized/patch libraries during library loading. |
147 abstract class LibraryLoader { | 149 abstract class LibraryLoader { |
148 /// This method must be called when a new synthesized/patch library has been | 150 /// This method must be called when a new synthesized/patch library has been |
149 /// created to ensure that [library] will part of library dependency graph | 151 /// created to ensure that [library] will part of library dependency graph |
150 /// used for computing import/export scopes. | 152 /// used for computing import/export scopes. |
151 void registerNewLibrary(LibraryElement library); | 153 void registerNewLibrary(LibraryElement library); |
152 | 154 |
153 /// This method must be called when a new synthesized/patch library has been | 155 /// This method must be called when a new synthesized/patch library has been |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
239 | 241 |
240 /** | 242 /** |
241 * Implementation class for [LibraryLoader]. The distinction between | 243 * Implementation class for [LibraryLoader]. The distinction between |
242 * [LibraryLoader] and [LibraryLoaderTask] is made to hide internal members from | 244 * [LibraryLoader] and [LibraryLoaderTask] is made to hide internal members from |
243 * the [LibraryLoader] interface. | 245 * the [LibraryLoader] interface. |
244 */ | 246 */ |
245 class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { | 247 class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { |
246 _LibraryLoaderTask(Compiler compiler) : super(compiler); | 248 _LibraryLoaderTask(Compiler compiler) : super(compiler); |
247 String get name => 'LibraryLoader'; | 249 String get name => 'LibraryLoader'; |
248 | 250 |
251 final Map<Uri, LibraryElement> libraryCanonicalUriMap = | |
252 new Map<Uri, LibraryElement>(); | |
249 final Map<Uri, LibraryElement> libraryResourceUriMap = | 253 final Map<Uri, LibraryElement> libraryResourceUriMap = |
250 new Map<Uri, LibraryElement>(); | 254 new Map<Uri, LibraryElement>(); |
251 final Map<String, LibraryElement> libraryNames = | 255 final Map<String, LibraryElement> libraryNames = |
252 new Map<String, LibraryElement>(); | 256 new Map<String, LibraryElement>(); |
253 | 257 |
254 LibraryDependencyHandler currentHandler; | 258 LibraryDependencyHandler currentHandler; |
255 | 259 |
256 void reset() { | 260 Iterable<LibraryElement> get libraries => libraryCanonicalUriMap.values; |
261 | |
262 LibraryElement lookupLibrary(Uri canonicalUri) { | |
263 return libraryCanonicalUriMap[canonicalUri]; | |
264 } | |
265 | |
266 void reset([bool reuseLibrary(LibraryElement library)]) { | |
257 assert(currentHandler == null); | 267 assert(currentHandler == null); |
268 Iterable<LibraryElement> libraries = | |
269 new List.from(libraryCanonicalUriMap.values); | |
270 | |
271 libraryCanonicalUriMap.clear(); | |
258 libraryResourceUriMap.clear(); | 272 libraryResourceUriMap.clear(); |
259 libraryNames.clear(); | 273 libraryNames.clear(); |
274 | |
275 if (reuseLibrary == null) return; | |
276 | |
277 libraries.where(reuseLibrary).forEach(mapLibrary); | |
260 } | 278 } |
261 | 279 |
262 void reuseLibrary(LibraryElement library) { | 280 /// Insert [library] in the internal maps. Used for compiler reuse. |
263 String name = library.getLibraryOrScriptName(); | 281 void mapLibrary(LibraryElement library) { |
282 libraryCanonicalUriMap[library.canonicalUri] = library; | |
283 | |
264 Uri resourceUri = library.entryCompilationUnit.script.resourceUri; | 284 Uri resourceUri = library.entryCompilationUnit.script.resourceUri; |
265 libraryResourceUriMap[resourceUri] = library; | 285 libraryResourceUriMap[resourceUri] = library; |
286 | |
287 String name = library.getLibraryOrScriptName(); | |
266 libraryNames[name] = library; | 288 libraryNames[name] = library; |
267 } | 289 } |
268 | 290 |
269 Future<LibraryElement> loadLibrary(Uri resolvedUri) { | 291 Future<LibraryElement> loadLibrary(Uri resolvedUri) { |
270 return measure(() { | 292 return measure(() { |
271 assert(currentHandler == null); | 293 assert(currentHandler == null); |
272 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the | 294 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the |
273 // loading of a library cluster. | 295 // loading of a library cluster. |
274 currentHandler = new LibraryDependencyHandler(this); | 296 currentHandler = new LibraryDependencyHandler(this); |
275 return createLibrary(currentHandler, null, resolvedUri) | 297 return createLibrary(currentHandler, null, resolvedUri) |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
481 */ | 503 */ |
482 Future<LibraryElement> createLibrary(LibraryDependencyHandler handler, | 504 Future<LibraryElement> createLibrary(LibraryDependencyHandler handler, |
483 LibraryElement importingLibrary, | 505 LibraryElement importingLibrary, |
484 Uri resolvedUri, | 506 Uri resolvedUri, |
485 [Node node]) { | 507 [Node node]) { |
486 // TODO(johnniwinther): Create erroneous library elements for missing | 508 // TODO(johnniwinther): Create erroneous library elements for missing |
487 // libraries. | 509 // libraries. |
488 Uri readableUri = | 510 Uri readableUri = |
489 compiler.translateResolvedUri(importingLibrary, resolvedUri, node); | 511 compiler.translateResolvedUri(importingLibrary, resolvedUri, node); |
490 if (readableUri == null) return new Future.value(); | 512 if (readableUri == null) return new Future.value(); |
491 LibraryElement library = compiler.libraries[resolvedUri.toString()]; | 513 LibraryElement library = libraryCanonicalUriMap[resolvedUri]; |
492 if (library != null) { | 514 if (library != null) { |
493 return new Future.value(library); | 515 return new Future.value(library); |
494 } | 516 } |
495 return compiler.withCurrentElement(importingLibrary, () { | 517 return compiler.withCurrentElement(importingLibrary, () { |
496 return compiler.readScript(node, readableUri) | 518 return compiler.readScript(node, readableUri) |
497 .then((Script script) { | 519 .then((Script script) { |
498 if (script == null) return null; | 520 if (script == null) return null; |
499 LibraryElement element = new LibraryElementX(script, resolvedUri); | 521 LibraryElement element = new LibraryElementX(script, resolvedUri); |
500 compiler.withCurrentElement(element, () { | 522 compiler.withCurrentElement(element, () { |
501 handler.registerNewLibrary(element); | 523 handler.registerNewLibrary(element); |
502 native.maybeEnableNative(compiler, element); | 524 native.maybeEnableNative(compiler, element); |
503 compiler.libraries[resolvedUri.toString()] = element; | 525 libraryCanonicalUriMap[resolvedUri] = element; |
504 compiler.scanner.scanLibrary(element); | 526 compiler.scanner.scanLibrary(element); |
505 }); | 527 }); |
506 return processLibraryTags(handler, element).then((_) { | 528 return processLibraryTags(handler, element).then((_) { |
507 compiler.withCurrentElement(element, () { | 529 compiler.withCurrentElement(element, () { |
508 handler.registerLibraryExports(element); | 530 handler.registerLibraryExports(element); |
509 }); | 531 }); |
510 return element; | 532 return element; |
511 }); | 533 }); |
512 }); | 534 }); |
513 }); | 535 }); |
(...skipping 436 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
950 * fixed-point computation of the import/export scopes. | 972 * fixed-point computation of the import/export scopes. |
951 */ | 973 */ |
952 void registerLibraryExports(LibraryElement library) { | 974 void registerLibraryExports(LibraryElement library) { |
953 nodeMap[library].registerInitialExports(); | 975 nodeMap[library].registerInitialExports(); |
954 } | 976 } |
955 | 977 |
956 Future processLibraryTags(LibraryElement library) { | 978 Future processLibraryTags(LibraryElement library) { |
957 return task.processLibraryTags(this, library); | 979 return task.processLibraryTags(this, library); |
958 } | 980 } |
959 } | 981 } |
OLD | NEW |