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; | |
| 681 _deferredImportDescriptions[import] = | |
| 682 new ImportDescription(import, library, compiler); | |
| 675 | 683 |
| 676 if (prefix == null) { | 684 if (prefix == null) { |
| 677 compiler.reportError(import, | 685 compiler.reportError(import, |
| 678 MessageKind.DEFERRED_LIBRARY_WITHOUT_PREFIX); | 686 MessageKind.DEFERRED_LIBRARY_WITHOUT_PREFIX); |
| 679 } else { | 687 } else { |
| 680 prefixDeferredImport[prefix] = import; | 688 prefixDeferredImport[prefix] = import; |
| 681 } | 689 } |
| 682 isProgramSplit = true; | 690 isProgramSplit = true; |
| 683 lastDeferred = import; | 691 lastDeferred = import; |
| 684 } | 692 } |
| (...skipping 72 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 Map deferredMap() { | |
|
floitsch
2015/01/05 14:10:38
has a getter name.
Either make it a getter, or cha
sigurdm
2015/01/05 15:23:08
Done.
| |
| 779 JavaScriptBackend backend = compiler.backend; | |
| 780 Map mapping = {}; | |
| 781 _deferredImportDescriptions.keys.forEach((ast.Import import) { | |
| 782 List<OutputUnit> outputUnits = hunksToLoad[importDeferName[import]]; | |
| 783 ImportDescription description = _deferredImportDescriptions[import]; | |
| 784 Map a = mapping.putIfAbsent(description.importingUri, () => {}); | |
|
floitsch
2015/01/05 14:10:38
what's "a" ?
Type the map (both static and dynamic
sigurdm
2015/01/05 15:23:08
Fixed the name.
| |
| 785 | |
| 786 a[description.prefix] = outputUnits.map( | |
| 787 (OutputUnit outputUnit) { | |
| 788 return backend.emitter.oldEmitter.deferredPartFileName(outputUnit); | |
|
floitsch
2015/01/05 14:10:38
Don't go through "oldEmitter".
The best is probabl
sigurdm
2015/01/05 15:23:08
Moved it to backend.
| |
| 789 }).toList(); | |
| 790 }); | |
| 791 return mapping; | |
| 792 } | |
| 767 } | 793 } |
| 794 | |
| 795 class ImportDescription { | |
| 796 /// Relative uri to the importing library. | |
| 797 final String importingUri; | |
| 798 /// The prefix this import is imported as. | |
| 799 final String prefix; | |
| 800 | |
| 801 ImportDescription(Import import, | |
| 802 LibraryElement importingLibrary, | |
| 803 Compiler compiler) | |
| 804 : importingUri = uri_extras.relativize( | |
| 805 compiler.mainApp.canonicalUri, | |
| 806 importingLibrary.canonicalUri, false), | |
| 807 prefix = import.prefix.source; | |
| 808 } | |
| OLD | NEW |