Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 Loading... | |
| 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 | |
|
floitsch
2015/01/08 13:41:07
Please check if this goes nicely in markdown. (may
sigurdm
2015/01/09 10:16:55
Done. Inserted a newline
| |
| 781 /// - <library uri> is the relative uri of the library making a deferred | |
| 782 /// import | |
| 783 /// - <library name> is the name of the libary, and "<unnamed>" if it is | |
| 784 /// unnamed. | |
| 785 /// - <prefix> is the `as` prefix used for a given deferred import. | |
| 786 /// - <list of files> is a list of the filenames the must be loaded when that | |
| 787 /// import is loaded. | |
| 788 Map<String, Map<String, dynamic>> computeDeferredMap() { | |
| 789 JavaScriptBackend backend = compiler.backend; | |
| 790 Map<String, Map<String, dynamic>> mapping = | |
| 791 new Map<String, Map<String, dynamic>>(); | |
| 792 _deferredImportDescriptions.keys.forEach((ast.Import import) { | |
| 793 List<OutputUnit> outputUnits = hunksToLoad[importDeferName[import]]; | |
| 794 ImportDescription description = _deferredImportDescriptions[import]; | |
| 795 Map<String, dynamic> libraryMap = | |
| 796 mapping.putIfAbsent(description.importingUri, | |
| 797 () => <String, dynamic>{"name": description.importingLibraryName, | |
| 798 "imports": <String, List<String>>{}}); | |
| 799 | |
| 800 libraryMap["imports"][description.prefix] = outputUnits.map( | |
| 801 (OutputUnit outputUnit) { | |
| 802 return backend.deferredPartFileName(outputUnit.name); | |
| 803 }).toList(); | |
| 804 }); | |
| 805 return mapping; | |
| 806 } | |
| 767 } | 807 } |
| 808 | |
| 809 class ImportDescription { | |
| 810 /// Relative uri to the importing library. | |
| 811 final String importingUri; | |
| 812 /// The prefix this import is imported as. | |
| 813 final String prefix; | |
| 814 final LibraryElement _importingLibrary; | |
| 815 | |
| 816 ImportDescription(Import import, | |
| 817 LibraryElement importingLibrary, | |
| 818 Compiler compiler) | |
| 819 : importingUri = uri_extras.relativize( | |
| 820 compiler.mainApp.canonicalUri, | |
| 821 importingLibrary.canonicalUri, false), | |
| 822 prefix = import.prefix.source, | |
| 823 _importingLibrary = importingLibrary; | |
| 824 | |
| 825 String get importingLibraryName { | |
| 826 String libraryName = _importingLibrary.getLibraryName(); | |
| 827 return libraryName == "" | |
| 828 ? "<unnamed>" | |
| 829 : libraryName; | |
| 830 } | |
| 831 | |
| 832 } | |
| OLD | NEW |