OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
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. | |
4 | |
5 library deferred_load; | |
6 | |
7 import 'dart:uri'; | |
8 | |
9 import 'dart2jslib.dart' | |
10 show Compiler, | |
11 CompilerTask, | |
12 ConstructedConstant, | |
13 SourceString; | |
14 | |
15 import 'elements/elements.dart' | |
16 show ClassElement, | |
17 Element, | |
18 LibraryElement, | |
19 MetadataAnnotation; | |
20 | |
21 import 'util/util.dart' | |
22 show Link; | |
23 | |
24 import 'tree/tree.dart' | |
25 show LibraryTag; | |
26 | |
27 class DeferredLoadTask extends CompilerTask { | |
ngeoffray
2013/02/19 10:04:57
Is there some time you would like to measure for t
ahe
2013/02/19 11:10:36
Done.
| |
28 final Set<LibraryElement> deferredLibraries = new Set<LibraryElement>(); | |
29 | |
30 ClassElement cachedDeferredLibraryClass; | |
31 | |
32 DeferredLoadTask(Compiler compiler) : super(compiler); | |
33 | |
34 String get name => 'Lazy'; | |
35 | |
36 /// DeferredLibrary from dart:async | |
37 ClassElement get deferredLibraryClass { | |
38 if (cachedDeferredLibraryClass == null) { | |
39 cachedDeferredLibraryClass = findDeferredLibraryClass(); | |
40 } | |
41 return cachedDeferredLibraryClass; | |
42 } | |
43 | |
44 ClassElement findDeferredLibraryClass() { | |
45 var uri = new Uri.fromComponents(scheme: 'dart', path: 'async'); | |
46 LibraryElement asyncLibrary = | |
47 compiler.libraryLoader.loadLibrary(uri, null, uri); | |
48 var element = asyncLibrary.find(const SourceString('DeferredLibrary')); | |
49 if (element == null) { | |
50 internalErrorOnElement( | |
51 asyncLibrary, | |
52 'dart:async library does not contain required class: ' | |
53 'DeferredLibrary'); | |
54 } | |
55 return element; | |
56 } | |
57 | |
58 bool isDeferred(Element element) { | |
59 // TODO(ahe): This is really a graph coloring problem. We should | |
60 // make sure that libraries and elements only used by a deferred | |
61 // library are also deferred. | |
62 // Also, if something is deferred depends on your | |
63 // perspective. Inside a deferred library, other elements of the | |
64 // same library are not deferred. We should add an extra parameter | |
65 // to this method to indicate "from where". | |
66 return deferredLibraries.contains(element.getLibrary()); | |
67 } | |
68 | |
69 void registerMainApp(LibraryElement mainApp) { | |
70 if (mainApp == null) return; | |
71 compiler.withCurrentElement(mainApp, () { | |
72 deferredLibraries.addAll(findDeferredLibraries(mainApp)); | |
73 }); | |
74 } | |
75 | |
76 Link<Element> findDeferredLibraries(LibraryElement library) { | |
77 Link<LibraryElement> link = const Link<LibraryElement>(); | |
78 for (LibraryTag tag in library.tags) { | |
79 Link<MetadataAnnotation> metadata = tag.metadata; | |
80 if (metadata == null) continue; | |
81 for (MetadataAnnotation metadata in tag.metadata) { | |
82 metadata.ensureResolved(compiler); | |
83 Element element = metadata.value.computeType(compiler).element; | |
84 if (element == deferredLibraryClass) { | |
85 ConstructedConstant value = metadata.value; | |
86 SourceString expectedName = value.fields[0].toDartString().source; | |
87 LibraryElement deferredLibrary = library.getLibraryFromTag(tag); | |
88 link = link.prepend(deferredLibrary); | |
89 SourceString actualName = | |
90 new SourceString(deferredLibrary.getLibraryOrScriptName()); | |
91 if (expectedName != actualName) { | |
92 compiler.reportErrorCode( | |
93 metadata, | |
94 MessageKind.DEFERRED_LIBRARY_NAME_MISMATCH, | |
95 { 'expectedName': expectedName.slowToString(), | |
96 'actualName': actualName.slowToString()}); | |
97 } | |
98 } | |
99 } | |
100 } | |
101 return link; | |
102 } | |
103 } | |
OLD | NEW |