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 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
242 bool exclude(Element element) => excludedNames.contains(element.name); | 242 bool exclude(Element element) => excludedNames.contains(element.name); |
243 } | 243 } |
244 | 244 |
245 /** | 245 /** |
246 * Implementation class for [LibraryLoader]. The distinction between | 246 * Implementation class for [LibraryLoader]. The distinction between |
247 * [LibraryLoader] and [LibraryLoaderTask] is made to hide internal members from | 247 * [LibraryLoader] and [LibraryLoaderTask] is made to hide internal members from |
248 * the [LibraryLoader] interface. | 248 * the [LibraryLoader] interface. |
249 */ | 249 */ |
250 class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { | 250 class _LibraryLoaderTask extends CompilerTask implements LibraryLoaderTask { |
251 _LibraryLoaderTask(Compiler compiler) : super(compiler); | 251 _LibraryLoaderTask(Compiler compiler) : super(compiler); |
252 | |
252 String get name => 'LibraryLoader'; | 253 String get name => 'LibraryLoader'; |
253 | 254 |
254 final Map<Uri, LibraryElement> libraryCanonicalUriMap = | 255 final Map<Uri, LibraryElement> libraryCanonicalUriMap = |
255 new Map<Uri, LibraryElement>(); | 256 new Map<Uri, LibraryElement>(); |
256 final Map<Uri, LibraryElement> libraryResourceUriMap = | 257 final Map<Uri, LibraryElement> libraryResourceUriMap = |
257 new Map<Uri, LibraryElement>(); | 258 new Map<Uri, LibraryElement>(); |
258 final Map<String, LibraryElement> libraryNames = | 259 final Map<String, LibraryElement> libraryNames = |
259 new Map<String, LibraryElement>(); | 260 new Map<String, LibraryElement>(); |
260 | 261 |
261 LibraryDependencyHandler currentHandler; | 262 LibraryDependencyHandler currentHandler; |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
335 return measure(() { | 336 return measure(() { |
336 assert(currentHandler == null); | 337 assert(currentHandler == null); |
337 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the | 338 // TODO(johnniwinther): Ensure that currentHandler correctly encloses the |
338 // loading of a library cluster. | 339 // loading of a library cluster. |
339 currentHandler = new LibraryDependencyHandler(this); | 340 currentHandler = new LibraryDependencyHandler(this); |
340 return createLibrary(currentHandler, null, resolvedUri) | 341 return createLibrary(currentHandler, null, resolvedUri) |
341 .then((LibraryElement library) { | 342 .then((LibraryElement library) { |
342 return compiler.withCurrentElement(library, () { | 343 return compiler.withCurrentElement(library, () { |
343 return measure(() { | 344 return measure(() { |
344 currentHandler.computeExports(); | 345 currentHandler.computeExports(); |
345 Map<Uri, LibraryElement> loadedLibraries = <Uri, LibraryElement>{}; | 346 LoadedLibraries loadedLibraries = |
346 currentHandler.loadedLibraries.forEach( | 347 new _LoadedLibraries(library, currentHandler.nodeMap); |
347 (LibraryElement loadedLibrary) { | |
348 loadedLibraries[loadedLibrary.canonicalUri] = loadedLibrary; | |
349 }); | |
350 currentHandler = null; | 348 currentHandler = null; |
351 return compiler.onLibrariesLoaded(loadedLibraries) | 349 return compiler.onLibrariesLoaded(loadedLibraries) |
352 .then((_) => library); | 350 .then((_) => library); |
353 }); | 351 }); |
354 }); | 352 }); |
355 }); | 353 }); |
356 }); | 354 }); |
357 } | 355 } |
358 | 356 |
359 /** | 357 /** |
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1025 * fixed-point computation of the import/export scopes. | 1023 * fixed-point computation of the import/export scopes. |
1026 */ | 1024 */ |
1027 void registerLibraryExports(LibraryElement library) { | 1025 void registerLibraryExports(LibraryElement library) { |
1028 nodeMap[library].registerInitialExports(); | 1026 nodeMap[library].registerInitialExports(); |
1029 } | 1027 } |
1030 | 1028 |
1031 Future processLibraryTags(LibraryElement library) { | 1029 Future processLibraryTags(LibraryElement library) { |
1032 return task.processLibraryTags(this, library); | 1030 return task.processLibraryTags(this, library); |
1033 } | 1031 } |
1034 } | 1032 } |
1033 | |
1034 /// Information on the bulk of newly loaded libraries through a call to | |
1035 /// [LibraryLoader.loadLibrary]. | |
1036 abstract class LoadedLibraries { | |
1037 /// The uri passed to [LibraryLoader.loadLibrary]. | |
1038 Uri get rootUri; | |
1039 | |
1040 /// Returns `true` if a library with canonical [uri] was loaded in this bulk. | |
1041 bool containsLibrary(Uri uri); | |
1042 | |
1043 /// Returns the library with canonical [uri] that was loaded in this bulk. | |
1044 LibraryElement getLibrary(Uri uri); | |
1045 | |
1046 /// Applies all libraries in this bulk to [f]. | |
1047 void forEachLibrary(f(LibraryElement library)); | |
1048 | |
1049 /// Applies all library imports in this bulk to [f]. | |
1050 /// | |
1051 /// The argument [importChainReversed] to [f] contains the chain of imports | |
1052 /// uris that lead to importing [library] starting in the canonical uri for | |
1053 /// [library] and ending in [rootUri]. | |
1054 /// | |
1055 /// [f] is called once for each chain of imports leading to [library]. | |
1056 void forEachImportChain(f(LibraryElement library, | |
1057 Link<Uri> importChainReversed)); | |
1058 } | |
1059 | |
1060 class _LoadedLibraries implements LoadedLibraries { | |
1061 final LibraryElement rootLibrary; | |
1062 final Map<Uri, LibraryElement> loadedLibraries = <Uri, LibraryElement>{}; | |
1063 final Map<LibraryElement, LibraryDependencyNode> nodeMap; | |
1064 | |
1065 _LoadedLibraries(this.rootLibrary, this.nodeMap) { | |
1066 nodeMap.keys.forEach((LibraryElement loadedLibrary) { | |
1067 loadedLibraries[loadedLibrary.canonicalUri] = loadedLibrary; | |
1068 }); | |
1069 } | |
1070 | |
1071 Uri get rootUri => rootLibrary.canonicalUri; | |
1072 | |
1073 bool containsLibrary(Uri uri) => loadedLibraries.containsKey(uri); | |
1074 | |
1075 LibraryElement getLibrary(Uri uri) => loadedLibraries[uri]; | |
1076 | |
1077 void forEachLibrary(f(LibraryElement library)) => nodeMap.keys.forEach(f); | |
1078 | |
1079 void forEachImportChain(f(LibraryElement library, Link<Uri> importChain)) { | |
1080 processImports(rootLibrary, const Link<Uri>(), f); | |
1081 } | |
1082 | |
1083 void processImports(LibraryElement library, | |
floitsch
2014/09/22 20:52:59
If the method is not in the public API I would mak
Johnni Winther
2014/11/12 13:06:26
Done.
| |
1084 Link<Uri> uris, | |
1085 f(LibraryElement library, Link<Uri> importChain)) { | |
1086 Uri uri = library.canonicalUri; | |
1087 if (uris.contains(uri)) { | |
1088 // Cyclic import. | |
1089 return; | |
1090 } | |
1091 uris = uris.prepend(uri); | |
1092 f(library, uris); | |
1093 LibraryDependencyNode node = nodeMap[library]; | |
1094 // TODO(johnniwinther): Reversing the imports every lookup. | |
1095 for (ImportLink import in node.imports.reverse()) { | |
1096 processImports(import.importedLibrary, uris, f); | |
1097 } | |
1098 } | |
1099 } | |
OLD | NEW |