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. | |
|
floitsch
2015/01/05 15:46:19
Add comment what the keys and values are.
sigurdm
2015/01/06 09:02:20
Done.
| |
| 778 Map<String, Map<String, List<String>>> getDeferredMap() { | |
|
floitsch
2015/01/05 15:46:19
'getX' doesn't say why this is not a getter.
compu
sigurdm
2015/01/06 09:02:20
Done.
| |
| 779 JavaScriptBackend backend = compiler.backend; | |
| 780 Map<String, Map<String, List<String>>> mapping = | |
|
floitsch
2015/01/05 15:46:19
Now I understand why you didn't add the types...
sigurdm
2015/01/06 09:02:20
First the map had a different structure that could
| |
| 781 new Map<String, Map<String, List<String>>>(); | |
| 782 _deferredImportDescriptions.keys.forEach((ast.Import import) { | |
| 783 List<OutputUnit> outputUnits = hunksToLoad[importDeferName[import]]; | |
| 784 ImportDescription description = _deferredImportDescriptions[import]; | |
| 785 Map<String, List<String>> fileLists = | |
| 786 mapping.putIfAbsent(description.importingUri, | |
| 787 () => new Map<String, List<String>>()); | |
| 788 | |
| 789 fileLists[description.prefix] = outputUnits.map( | |
| 790 (OutputUnit outputUnit) { | |
| 791 return backend.outputFileName("${outputUnit.name}.part.js"); | |
|
floitsch
2015/01/05 15:46:19
Don't add the extension here.
Ask for the deferred
sigurdm
2015/01/06 09:02:20
Done.
| |
| 792 }).toList(); | |
| 793 }); | |
| 794 return mapping; | |
| 795 } | |
| 767 } | 796 } |
| 797 | |
| 798 class ImportDescription { | |
| 799 /// Relative uri to the importing library. | |
| 800 final String importingUri; | |
| 801 /// The prefix this import is imported as. | |
| 802 final String prefix; | |
| 803 | |
| 804 ImportDescription(Import import, | |
| 805 LibraryElement importingLibrary, | |
| 806 Compiler compiler) | |
| 807 : importingUri = uri_extras.relativize( | |
| 808 compiler.mainApp.canonicalUri, | |
| 809 importingLibrary.canonicalUri, false), | |
| 810 prefix = import.prefix.source; | |
| 811 } | |
| OLD | NEW |