| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart2js.js_emitter.program_builder; | |
| 6 | |
| 7 /// A Fragment maps [LibraryElement]s to their [Element]s. | |
| 8 /// | |
| 9 /// Fundamentally, this class nicely encapsulates a | |
| 10 /// `Map<LibraryElement, List<Element>>`. | |
| 11 class Fragment { | |
| 12 final Map<LibraryElement, List<Element>> _mapping = | |
| 13 <LibraryElement, List<Element>>{}; | |
| 14 | |
| 15 // It is very common to access the same library multiple times in a row, so | |
| 16 // we cache the last access. | |
| 17 LibraryElement _lastLibrary; | |
| 18 List<Element> _lastElements; | |
| 19 | |
| 20 /// A unique name representing this fragment. | |
| 21 final String name; | |
| 22 final OutputUnit outputUnit; | |
| 23 | |
| 24 Fragment.main(this.outputUnit) : name = ""; | |
| 25 | |
| 26 Fragment.deferred(this.outputUnit, this.name) { | |
| 27 assert(name != ""); | |
| 28 } | |
| 29 | |
| 30 void add(LibraryElement library, Element element) { | |
| 31 if (_lastLibrary != library) { | |
| 32 _lastLibrary = library; | |
| 33 _lastElements = _mapping.putIfAbsent(library, () => <Element>[]); | |
| 34 } | |
| 35 _lastElements.add(element); | |
| 36 } | |
| 37 | |
| 38 int get length => _mapping.length; | |
| 39 | |
| 40 void forEach(void f(LibraryElement library, List<Element> elements)) { | |
| 41 _mapping.forEach(f); | |
| 42 } | |
| 43 } | |
| 44 | |
| 45 /// Keeps track of all elements and holders. | |
| 46 /// | |
| 47 /// This class assigns each registered element to its [Fragment] (which are in | |
| 48 /// bijection with [OutputUnit]s). | |
| 49 /// | |
| 50 /// Registered holders are assigned a name. | |
| 51 class Registry { | |
| 52 final Compiler _compiler; | |
| 53 final Map<String, Holder> _holdersMap = <String, Holder>{}; | |
| 54 final Map<OutputUnit, Fragment> _deferredFragmentsMap = | |
| 55 <OutputUnit, Fragment>{}; | |
| 56 | |
| 57 DeferredLoadTask get _deferredLoadTask => _compiler.deferredLoadTask; | |
| 58 Iterable<Holder> get holders => _holdersMap.values; | |
| 59 Iterable<Fragment> get deferredFragments => _deferredFragmentsMap.values; | |
| 60 // Add one for the main fragment. | |
| 61 int get fragmentCount => _deferredFragmentsMap.length + 1; | |
| 62 | |
| 63 Fragment mainFragment; | |
| 64 | |
| 65 Registry(this._compiler); | |
| 66 | |
| 67 bool get _isProgramSplit => _deferredLoadTask.isProgramSplit; | |
| 68 OutputUnit get _mainOutputUnit => _deferredLoadTask.mainOutputUnit; | |
| 69 | |
| 70 Fragment _mapUnitToFragment(OutputUnit targetUnit) { | |
| 71 if (mainFragment == null) { | |
| 72 mainFragment = new Fragment.main(_deferredLoadTask.mainOutputUnit); | |
| 73 } | |
| 74 if (!_isProgramSplit) { | |
| 75 assert(targetUnit == _deferredLoadTask.mainOutputUnit); | |
| 76 return mainFragment; | |
| 77 } | |
| 78 if (targetUnit == _mainOutputUnit) return mainFragment; | |
| 79 String name = targetUnit.name; | |
| 80 return _deferredFragmentsMap.putIfAbsent( | |
| 81 targetUnit, () => new Fragment.deferred(targetUnit, name)); | |
| 82 } | |
| 83 | |
| 84 /// Adds all elements to their respective libraries in the correct fragment. | |
| 85 void registerElements(OutputUnit outputUnit, List<Element> elements) { | |
| 86 Fragment targetFragment = _mapUnitToFragment(outputUnit); | |
| 87 for (Element element in Elements.sortedByPosition(elements)) { | |
| 88 targetFragment.add(element.library, element); | |
| 89 } | |
| 90 } | |
| 91 | |
| 92 Holder registerHolder(String name) { | |
| 93 return _holdersMap.putIfAbsent( | |
| 94 name, | |
| 95 () => new Holder(name, _holdersMap.length)); | |
| 96 } | |
| 97 } | |
| OLD | NEW |