Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: pkg/compiler/lib/src/deferred_load.dart

Issue 785303003: Add option for outputting a deferred loading mapping. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: rebase Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/compiler/lib/src/dart2js.dart ('k') | pkg/compiler/lib/src/js_backend/backend.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, 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 deferred_load; 5 library deferred_load;
6 6
7 import 'constants/expressions.dart'; 7 import 'constants/expressions.dart';
8 import 'constants/values.dart' show 8 import 'constants/values.dart' show
9 ConstantValue, 9 ConstantValue,
10 ConstructedConstantValue, 10 ConstructedConstantValue,
(...skipping 22 matching lines...) Expand all
33 FunctionElement, 33 FunctionElement,
34 LibraryElement, 34 LibraryElement,
35 MetadataAnnotation, 35 MetadataAnnotation,
36 PrefixElement, 36 PrefixElement,
37 ScopeContainerElement, 37 ScopeContainerElement,
38 TypedefElement, 38 TypedefElement,
39 VoidElement; 39 VoidElement;
40 40
41 import 'util/util.dart' show 41 import 'util/util.dart' show
42 Link, makeUnique; 42 Link, makeUnique;
43 import 'util/uri_extras.dart' as uri_extras;
43 44
44 import 'util/setlet.dart' show 45 import 'util/setlet.dart' show
45 Setlet; 46 Setlet;
46 47
47 import 'tree/tree.dart' show 48 import 'tree/tree.dart' show
48 Import, 49 Import,
49 LibraryTag, 50 LibraryTag,
50 LibraryDependency, 51 LibraryDependency,
51 LiteralDartString, 52 LiteralDartString,
52 LiteralString, 53 LiteralString,
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
139 /// [outputUnitForConstant] 140 /// [outputUnitForConstant]
140 final Map<ConstantValue, OutputUnit> _constantToOutputUnit = 141 final Map<ConstantValue, OutputUnit> _constantToOutputUnit =
141 new Map<ConstantValue, OutputUnit>(); 142 new Map<ConstantValue, OutputUnit>();
142 143
143 /// All the imports with a [DeferredLibrary] annotation, mapped to the 144 /// All the imports with a [DeferredLibrary] annotation, mapped to the
144 /// [LibraryElement] they import. 145 /// [LibraryElement] they import.
145 /// The main library is included in this set for convenience. 146 /// The main library is included in this set for convenience.
146 final Map<Import, LibraryElement> _allDeferredImports = 147 final Map<Import, LibraryElement> _allDeferredImports =
147 new Map<Import, LibraryElement>(); 148 new Map<Import, LibraryElement>();
148 149
150 /// Because the token-stream is forgotten later in the program, we cache a
151 /// description of each deferred import.
152 final _deferredImportDescriptions = new Map<Import, ImportDescription>();
153
149 // For each deferred import we want to know exactly what elements have to 154 // For each deferred import we want to know exactly what elements have to
150 // be loaded. 155 // be loaded.
151 Map<Import, Set<Element>> _importedDeferredBy = null; 156 Map<Import, Set<Element>> _importedDeferredBy = null;
152 Map<Import, Set<ConstantValue>> _constantsDeferredBy = null; 157 Map<Import, Set<ConstantValue>> _constantsDeferredBy = null;
153 158
154 Set<Element> _mainElements = new Set<Element>(); 159 Set<Element> _mainElements = new Set<Element>();
155 160
156 DeferredLoadTask(Compiler compiler) : super(compiler) { 161 DeferredLoadTask(Compiler compiler) : super(compiler) {
157 mainOutputUnit.imports.add(_fakeMainImport); 162 mainOutputUnit.imports.add(_fakeMainImport);
158 } 163 }
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after
610 allOutputUnits.add(representative); 615 allOutputUnits.add(representative);
611 } 616 }
612 _constantToOutputUnit[constant] = representative; 617 _constantToOutputUnit[constant] = representative;
613 }); 618 });
614 619
615 // Generate a unique name for each OutputUnit. 620 // Generate a unique name for each OutputUnit.
616 _assignNamesToOutputUnits(allOutputUnits); 621 _assignNamesToOutputUnits(allOutputUnits);
617 }); 622 });
618 } 623 }
619 624
620 void ensureMetadataResolved(Compiler compiler) { 625 void beforeResolution(Compiler compiler) {
621 if (compiler.mainApp == null) return; 626 if (compiler.mainApp == null) return;
622 _allDeferredImports[_fakeMainImport] = compiler.mainApp; 627 _allDeferredImports[_fakeMainImport] = compiler.mainApp;
623 var lastDeferred; 628 var lastDeferred;
624 // When detecting duplicate prefixes of deferred libraries there are 4 629 // When detecting duplicate prefixes of deferred libraries there are 4
625 // cases of duplicate prefixes: 630 // cases of duplicate prefixes:
626 // 1. 631 // 1.
627 // import "lib.dart" deferred as a; 632 // import "lib.dart" deferred as a;
628 // import "lib2.dart" deferred as a; 633 // import "lib2.dart" deferred as a;
629 // 2. 634 // 2.
630 // import "lib.dart" deferred as a; 635 // import "lib.dart" deferred as a;
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
664 } 669 }
665 } 670 }
666 } 671 }
667 672
668 String prefix = (import.prefix != null) 673 String prefix = (import.prefix != null)
669 ? import.prefix.toString() 674 ? import.prefix.toString()
670 : null; 675 : null;
671 // The last import we saw with the same prefix. 676 // The last import we saw with the same prefix.
672 Import previousDeferredImport = prefixDeferredImport[prefix]; 677 Import previousDeferredImport = prefixDeferredImport[prefix];
673 if (import.isDeferred) { 678 if (import.isDeferred) {
674 _allDeferredImports[import] = library.getLibraryFromTag(import); 679 LibraryElement importedLibrary = library.getLibraryFromTag(import);
680 _allDeferredImports[import] = importedLibrary;
675 681
676 if (prefix == null) { 682 if (prefix == null) {
677 compiler.reportError(import, 683 compiler.reportError(import,
678 MessageKind.DEFERRED_LIBRARY_WITHOUT_PREFIX); 684 MessageKind.DEFERRED_LIBRARY_WITHOUT_PREFIX);
679 } else { 685 } else {
680 prefixDeferredImport[prefix] = import; 686 prefixDeferredImport[prefix] = import;
687 _deferredImportDescriptions[import] =
688 new ImportDescription(import, library, compiler);
681 } 689 }
682 isProgramSplit = true; 690 isProgramSplit = true;
683 lastDeferred = import; 691 lastDeferred = import;
684 } 692 }
685 if (prefix != null) { 693 if (prefix != null) {
686 if (previousDeferredImport != null || 694 if (previousDeferredImport != null ||
687 (import.isDeferred && usedPrefixes.contains(prefix))) { 695 (import.isDeferred && usedPrefixes.contains(prefix))) {
688 Import failingImport = (previousDeferredImport != null) 696 Import failingImport = (previousDeferredImport != null)
689 ? previousDeferredImport 697 ? previousDeferredImport
690 : import; 698 : import;
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after
757 if (identifier == null) return null; 765 if (identifier == null) return null;
758 Element maybePrefix = elements[identifier]; 766 Element maybePrefix = elements[identifier];
759 if (maybePrefix != null && maybePrefix.isPrefix) { 767 if (maybePrefix != null && maybePrefix.isPrefix) {
760 PrefixElement prefixElement = maybePrefix; 768 PrefixElement prefixElement = maybePrefix;
761 if (prefixElement.isDeferred) { 769 if (prefixElement.isDeferred) {
762 return prefixElement; 770 return prefixElement;
763 } 771 }
764 } 772 }
765 return null; 773 return null;
766 } 774 }
775
776 /// Returns a json-style map for describing what files that are loaded by a
777 /// given deferred import.
778 /// The mapping is structured as:
779 /// library uri -> {"name": library name, "files": (prefix -> list of files)}
780 /// Where
781 ///
782 /// - <library uri> is the relative uri of the library making a deferred
783 /// import
784 /// - <library name> is the name of the libary, and "<unnamed>" if it is
785 /// unnamed.
786 /// - <prefix> is the `as` prefix used for a given deferred import.
787 /// - <list of files> is a list of the filenames the must be loaded when that
788 /// import is loaded.
789 Map<String, Map<String, dynamic>> computeDeferredMap() {
790 JavaScriptBackend backend = compiler.backend;
791 Map<String, Map<String, dynamic>> mapping =
792 new Map<String, Map<String, dynamic>>();
793 _deferredImportDescriptions.keys.forEach((ast.Import import) {
794 List<OutputUnit> outputUnits = hunksToLoad[importDeferName[import]];
795 ImportDescription description = _deferredImportDescriptions[import];
796 Map<String, dynamic> libraryMap =
797 mapping.putIfAbsent(description.importingUri,
798 () => <String, dynamic>{"name": description.importingLibraryName,
799 "imports": <String, List<String>>{}});
800
801 libraryMap["imports"][description.prefix] = outputUnits.map(
802 (OutputUnit outputUnit) {
803 return backend.deferredPartFileName(outputUnit.name);
804 }).toList();
805 });
806 return mapping;
807 }
767 } 808 }
809
810 class ImportDescription {
811 /// Relative uri to the importing library.
812 final String importingUri;
813 /// The prefix this import is imported as.
814 final String prefix;
815 final LibraryElement _importingLibrary;
816
817 ImportDescription(Import import,
818 LibraryElement importingLibrary,
819 Compiler compiler)
820 : importingUri = uri_extras.relativize(
821 compiler.mainApp.canonicalUri,
822 importingLibrary.canonicalUri, false),
823 prefix = import.prefix.source,
824 _importingLibrary = importingLibrary;
825
826 String get importingLibraryName {
827 String libraryName = _importingLibrary.getLibraryName();
828 return libraryName == ""
829 ? "<unnamed>"
830 : libraryName;
831 }
832
833 }
OLDNEW
« no previous file with comments | « pkg/compiler/lib/src/dart2js.dart ('k') | pkg/compiler/lib/src/js_backend/backend.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698