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