| 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 part of dart2js.js_emitter.program_builder; | 5 part of dart2js.js_emitter.program_builder; |
| 6 | 6 |
| 7 /// A Fragment maps [LibraryElement]s to their [Element]s. | 7 /// Maps [LibraryElement]s to their [Element]s. |
| 8 /// | 8 /// |
| 9 /// Fundamentally, this class nicely encapsulates a | 9 /// Fundamentally, this class nicely encapsulates a |
| 10 /// `Map<LibraryElement, List<Element>>`. | 10 /// `Map<LibraryElement, List<Element>>`. |
| 11 class Fragment { | 11 /// |
| 12 /// There exists exactly one instance per [OutputUnit]. |
| 13 class LibrariesMap { |
| 12 final Map<LibraryElement, List<Element>> _mapping = | 14 final Map<LibraryElement, List<Element>> _mapping = |
| 13 <LibraryElement, List<Element>>{}; | 15 <LibraryElement, List<Element>>{}; |
| 14 | 16 |
| 15 // It is very common to access the same library multiple times in a row, so | 17 // It is very common to access the same library multiple times in a row, so |
| 16 // we cache the last access. | 18 // we cache the last access. |
| 17 LibraryElement _lastLibrary; | 19 LibraryElement _lastLibrary; |
| 18 List<Element> _lastElements; | 20 List<Element> _lastElements; |
| 19 | 21 |
| 20 /// A unique name representing this fragment. | 22 /// A unique name representing this instance. |
| 21 final String name; | 23 final String name; |
| 22 final OutputUnit outputUnit; | 24 final OutputUnit outputUnit; |
| 23 | 25 |
| 24 Fragment.main(this.outputUnit) : name = ""; | 26 LibrariesMap.main(this.outputUnit) : name = ""; |
| 25 | 27 |
| 26 Fragment.deferred(this.outputUnit, this.name) { | 28 LibrariesMap.deferred(this.outputUnit, this.name) { |
| 27 assert(name != ""); | 29 assert(name != ""); |
| 28 } | 30 } |
| 29 | 31 |
| 30 void add(LibraryElement library, Element element) { | 32 void add(LibraryElement library, Element element) { |
| 31 if (_lastLibrary != library) { | 33 if (_lastLibrary != library) { |
| 32 _lastLibrary = library; | 34 _lastLibrary = library; |
| 33 _lastElements = _mapping.putIfAbsent(library, () => <Element>[]); | 35 _lastElements = _mapping.putIfAbsent(library, () => <Element>[]); |
| 34 } | 36 } |
| 35 _lastElements.add(element); | 37 _lastElements.add(element); |
| 36 } | 38 } |
| 37 | 39 |
| 38 int get length => _mapping.length; | 40 int get length => _mapping.length; |
| 39 | 41 |
| 40 void forEach(void f(LibraryElement library, List<Element> elements)) { | 42 void forEach(void f(LibraryElement library, List<Element> elements)) { |
| 41 _mapping.forEach(f); | 43 _mapping.forEach(f); |
| 42 } | 44 } |
| 43 } | 45 } |
| 44 | 46 |
| 45 /// Keeps track of all elements and holders. | 47 /// Keeps track of all elements and holders. |
| 46 /// | 48 /// |
| 47 /// This class assigns each registered element to its [Fragment] (which are in | 49 /// This class assigns each registered element to its [LibrariesMap] (which are |
| 48 /// bijection with [OutputUnit]s). | 50 /// in bijection with [OutputUnit]s). |
| 49 /// | 51 /// |
| 50 /// Registered holders are assigned a name. | 52 /// Registered holders are assigned a name. |
| 51 class Registry { | 53 class Registry { |
| 52 final Compiler _compiler; | 54 final Compiler _compiler; |
| 53 final Map<String, Holder> _holdersMap = <String, Holder>{}; | 55 final Map<String, Holder> _holdersMap = <String, Holder>{}; |
| 54 final Map<OutputUnit, Fragment> _deferredFragmentsMap = | 56 final Map<OutputUnit, LibrariesMap> _deferredLibrariesMap = |
| 55 <OutputUnit, Fragment>{}; | 57 <OutputUnit, LibrariesMap>{}; |
| 56 | 58 |
| 57 /// Cache for the last seen output unit. | 59 /// Cache for the last seen output unit. |
| 58 OutputUnit _lastOutputUnit; | 60 OutputUnit _lastOutputUnit; |
| 59 Fragment _lastFragment; | 61 LibrariesMap _lastLibrariesMap; |
| 60 | 62 |
| 61 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask; | 63 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask; |
| 62 Iterable<Holder> get holders => _holdersMap.values; | 64 Iterable<Holder> get holders => _holdersMap.values; |
| 63 Iterable<Fragment> get deferredFragments => _deferredFragmentsMap.values; | 65 Iterable<LibrariesMap> get deferredLibrariesMap => |
| 64 // Add one for the main fragment. | 66 _deferredLibrariesMap.values; |
| 65 int get fragmentCount => _deferredFragmentsMap.length + 1; | |
| 66 | 67 |
| 67 Fragment mainFragment; | 68 // Add one for the main libraries map. |
| 69 int get librariesMapCount => _deferredLibrariesMap.length + 1; |
| 70 |
| 71 LibrariesMap mainLibrariesMap; |
| 68 | 72 |
| 69 Registry(this._compiler); | 73 Registry(this._compiler); |
| 70 | 74 |
| 71 bool get _isProgramSplit => _deferredLoadTask.isProgramSplit; | 75 bool get _isProgramSplit => _deferredLoadTask.isProgramSplit; |
| 72 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; | 76 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; |
| 73 | 77 |
| 74 Fragment _mapUnitToFragment(OutputUnit targetUnit) { | 78 LibrariesMap _mapUnitToLibrariesMap(OutputUnit targetUnit) { |
| 75 if (targetUnit == _lastOutputUnit) return _lastFragment; | 79 if (targetUnit == _lastOutputUnit) return _lastLibrariesMap; |
| 76 | 80 |
| 77 if (mainFragment == null) { | 81 if (mainLibrariesMap == null) { |
| 78 mainFragment = new Fragment.main(_deferredLoadTask.mainOutputUnit); | 82 mainLibrariesMap = |
| 83 new LibrariesMap.main(_deferredLoadTask.mainOutputUnit); |
| 79 } | 84 } |
| 80 | 85 |
| 81 Fragment result; | 86 LibrariesMap result; |
| 82 if (targetUnit == _mainOutputUnit) { | 87 if (targetUnit == _mainOutputUnit) { |
| 83 result = mainFragment; | 88 result = mainLibrariesMap; |
| 84 } else { | 89 } else { |
| 85 String name = targetUnit.name; | 90 String name = targetUnit.name; |
| 86 result = _deferredFragmentsMap.putIfAbsent( | 91 result = _deferredLibrariesMap.putIfAbsent( |
| 87 targetUnit, () => new Fragment.deferred(targetUnit, name)); | 92 targetUnit, () => new LibrariesMap.deferred(targetUnit, name)); |
| 88 } | 93 } |
| 89 _lastOutputUnit = targetUnit; | 94 _lastOutputUnit = targetUnit; |
| 90 _lastFragment = result; | 95 _lastLibrariesMap = result; |
| 91 return result; | 96 return result; |
| 92 } | 97 } |
| 93 | 98 |
| 94 /// Adds all elements to their respective libraries in the correct fragment. | 99 /// Adds all elements to their respective libraries in the correct |
| 100 /// libraries map. |
| 95 void registerElements(OutputUnit outputUnit, List<Element> elements) { | 101 void registerElements(OutputUnit outputUnit, List<Element> elements) { |
| 96 Fragment targetFragment = _mapUnitToFragment(outputUnit); | 102 LibrariesMap targetLibrariesMap = _mapUnitToLibrariesMap(outputUnit); |
| 97 for (Element element in Elements.sortedByPosition(elements)) { | 103 for (Element element in Elements.sortedByPosition(elements)) { |
| 98 targetFragment.add(element.library, element); | 104 targetLibrariesMap.add(element.library, element); |
| 99 } | 105 } |
| 100 } | 106 } |
| 101 | 107 |
| 102 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) { | 108 void registerConstant(OutputUnit outputUnit, ConstantValue constantValue) { |
| 103 // We just need to make sure that the target fragment is registered. | 109 // We just need to make sure that the target library map is registered. |
| 104 // Otherwise a fragment that contains only constants is not built. | 110 // Otherwise a library map that contains only constants is not built. |
| 105 _mapUnitToFragment(outputUnit); | 111 _mapUnitToLibrariesMap(outputUnit); |
| 106 } | 112 } |
| 107 | 113 |
| 108 Holder registerHolder(String name) { | 114 Holder registerHolder(String name) { |
| 109 return _holdersMap.putIfAbsent( | 115 return _holdersMap.putIfAbsent( |
| 110 name, | 116 name, |
| 111 () => new Holder(name, _holdersMap.length)); | 117 () => new Holder(name, _holdersMap.length)); |
| 112 } | 118 } |
| 113 } | 119 } |
| OLD | NEW |