| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 dart2js.library_loader; | 5 library dart2js.library_loader; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'common/names.dart' show Uris; | 9 import 'common/names.dart' show Uris; |
| 10 import 'common/tasks.dart' show CompilerTask, Measurer; | 10 import 'common/tasks.dart' show CompilerTask, Measurer; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 DeferredLoaderGetterElementX, | 23 DeferredLoaderGetterElementX, |
| 24 ErroneousElementX, | 24 ErroneousElementX, |
| 25 ExportElementX, | 25 ExportElementX, |
| 26 ImportElementX, | 26 ImportElementX, |
| 27 LibraryElementX, | 27 LibraryElementX, |
| 28 LibraryDependencyElementX, | 28 LibraryDependencyElementX, |
| 29 PrefixElementX, | 29 PrefixElementX, |
| 30 SyntheticImportElement; | 30 SyntheticImportElement; |
| 31 import 'enqueue.dart' show DeferredAction; | 31 import 'enqueue.dart' show DeferredAction; |
| 32 import 'environment.dart'; | 32 import 'environment.dart'; |
| 33 import 'kernel/world_builder.dart' show KernelWorldBuilder; | 33 import 'kernel/element_map.dart' show KernelToElementMap; |
| 34 import 'patch_parser.dart' show PatchParserTask; | 34 import 'patch_parser.dart' show PatchParserTask; |
| 35 import 'resolved_uri_translator.dart'; | 35 import 'resolved_uri_translator.dart'; |
| 36 import 'script.dart'; | 36 import 'script.dart'; |
| 37 import 'serialization/serialization.dart' show LibraryDeserializer; | 37 import 'serialization/serialization.dart' show LibraryDeserializer; |
| 38 import 'tree/tree.dart'; | 38 import 'tree/tree.dart'; |
| 39 import 'util/util.dart' show Link, LinkBuilder; | 39 import 'util/util.dart' show Link, LinkBuilder; |
| 40 | 40 |
| 41 import 'package:kernel/ast.dart' as ir; | 41 import 'package:kernel/ast.dart' as ir; |
| 42 import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder; | 42 import 'package:kernel/binary/ast_from_binary.dart' show BinaryBuilder; |
| 43 | 43 |
| (...skipping 793 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 837 final DiagnosticReporter reporter; | 837 final DiagnosticReporter reporter; |
| 838 | 838 |
| 839 final ResolvedUriTranslator uriTranslator; | 839 final ResolvedUriTranslator uriTranslator; |
| 840 | 840 |
| 841 /// Loads the contents of a script file (a .dart file). Used when loading | 841 /// Loads the contents of a script file (a .dart file). Used when loading |
| 842 /// libraries from source. | 842 /// libraries from source. |
| 843 final ScriptLoader scriptLoader; | 843 final ScriptLoader scriptLoader; |
| 844 | 844 |
| 845 /// Holds the mapping of Kernel IR to KElements that is constructed as a | 845 /// Holds the mapping of Kernel IR to KElements that is constructed as a |
| 846 /// result of loading a program. | 846 /// result of loading a program. |
| 847 KernelWorldBuilder _worldBuilder; | 847 KernelToElementMap _elementMap; |
| 848 | 848 |
| 849 List<LibraryEntity> _allLoadedLibraries; | 849 List<LibraryEntity> _allLoadedLibraries; |
| 850 | 850 |
| 851 _DillLibraryLoaderTask( | 851 _DillLibraryLoaderTask( |
| 852 this.uriTranslator, this.scriptLoader, this.reporter, Measurer measurer) | 852 this.uriTranslator, this.scriptLoader, this.reporter, Measurer measurer) |
| 853 : _allLoadedLibraries = new List<LibraryEntity>(), | 853 : _allLoadedLibraries = new List<LibraryEntity>(), |
| 854 super(measurer); | 854 super(measurer); |
| 855 | 855 |
| 856 /// Loads an entire Kernel [Program] from a file on disk (note, not just a | 856 /// Loads an entire Kernel [Program] from a file on disk (note, not just a |
| 857 /// library, so this name is actually a bit of a misnomer). | 857 /// library, so this name is actually a bit of a misnomer). |
| 858 // TODO(efortuna): Rename this once the Element library loader class goes | 858 // TODO(efortuna): Rename this once the Element library loader class goes |
| 859 // away. | 859 // away. |
| 860 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, | 860 Future<LoadedLibraries> loadLibrary(Uri resolvedUri, |
| 861 {bool skipFileWithPartOfTag: false}) { | 861 {bool skipFileWithPartOfTag: false}) { |
| 862 assert(resolvedUri.pathSegments.last.endsWith('.dill')); | 862 assert(resolvedUri.pathSegments.last.endsWith('.dill')); |
| 863 Uri readableUri = uriTranslator.translate(null, resolvedUri, null); | 863 Uri readableUri = uriTranslator.translate(null, resolvedUri, null); |
| 864 return measure(() async { | 864 return measure(() async { |
| 865 Script script = await scriptLoader.readScript(readableUri, null); | 865 Script script = await scriptLoader.readScript(readableUri, null); |
| 866 ir.Program program = new ir.Program(); | 866 ir.Program program = new ir.Program(); |
| 867 // Hack because the existing file has a terminating 0 and the | 867 // Hack because the existing file has a terminating 0 and the |
| 868 // BinaryBuilder doesn't expect that. | 868 // BinaryBuilder doesn't expect that. |
| 869 var bytes = new List<int>.from(script.file.slowUtf8ZeroTerminatedBytes()); | 869 var bytes = new List<int>.from(script.file.slowUtf8ZeroTerminatedBytes()); |
| 870 bytes.removeLast(); | 870 bytes.removeLast(); |
| 871 new BinaryBuilder(bytes).readProgram(program); | 871 new BinaryBuilder(bytes).readProgram(program); |
| 872 return measure(() { | 872 return measure(() { |
| 873 _worldBuilder = new KernelWorldBuilder(reporter, program); | 873 _elementMap = new KernelToElementMap(reporter, program); |
| 874 program.libraries.forEach((ir.Library library) => _allLoadedLibraries | 874 program.libraries.forEach((ir.Library library) => _allLoadedLibraries |
| 875 .add(_worldBuilder.lookupLibrary(library.importUri))); | 875 .add(_elementMap.lookupLibrary(library.importUri))); |
| 876 LibraryEntity rootLibrary = null; | 876 LibraryEntity rootLibrary = null; |
| 877 if (program.mainMethod != null) { | 877 if (program.mainMethod != null) { |
| 878 rootLibrary = _worldBuilder | 878 rootLibrary = _elementMap |
| 879 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); | 879 .lookupLibrary(program.mainMethod.enclosingLibrary.importUri); |
| 880 } | 880 } |
| 881 return new _LoadedLibrariesAdapter( | 881 return new _LoadedLibrariesAdapter( |
| 882 rootLibrary, _allLoadedLibraries, _worldBuilder); | 882 rootLibrary, _allLoadedLibraries, _elementMap); |
| 883 }); | 883 }); |
| 884 }); | 884 }); |
| 885 } | 885 } |
| 886 | 886 |
| 887 KernelWorldBuilder get worldBuilder => _worldBuilder; | 887 KernelToElementMap get elementMap => _elementMap; |
| 888 | 888 |
| 889 void reset({bool reuseLibrary(LibraryElement library)}) { | 889 void reset({bool reuseLibrary(LibraryElement library)}) { |
| 890 throw new UnimplementedError('_DillLibraryLoaderTask.reset'); | 890 throw new UnimplementedError('_DillLibraryLoaderTask.reset'); |
| 891 } | 891 } |
| 892 | 892 |
| 893 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { | 893 Future resetAsync(Future<bool> reuseLibrary(LibraryElement library)) { |
| 894 throw new UnimplementedError('_DillLibraryLoaderTask.resetAsync'); | 894 throw new UnimplementedError('_DillLibraryLoaderTask.resetAsync'); |
| 895 } | 895 } |
| 896 | 896 |
| 897 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; | 897 Iterable<LibraryEntity> get libraries => _allLoadedLibraries; |
| 898 | 898 |
| 899 LibraryEntity lookupLibrary(Uri canonicalUri) { | 899 LibraryEntity lookupLibrary(Uri canonicalUri) { |
| 900 return _worldBuilder?.lookupLibrary(canonicalUri); | 900 return _elementMap?.lookupLibrary(canonicalUri); |
| 901 } | 901 } |
| 902 | 902 |
| 903 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { | 903 Future<Null> resetLibraries(ReuseLibrariesFunction reuseLibraries) { |
| 904 throw new UnimplementedError('_DillLibraryLoaderTask.reuseLibraries'); | 904 throw new UnimplementedError('_DillLibraryLoaderTask.reuseLibraries'); |
| 905 } | 905 } |
| 906 | 906 |
| 907 void registerDeferredAction(DeferredAction action) { | 907 void registerDeferredAction(DeferredAction action) { |
| 908 throw new UnimplementedError( | 908 throw new UnimplementedError( |
| 909 '_DillLibraryLoaderTask.registerDeferredAction'); | 909 '_DillLibraryLoaderTask.registerDeferredAction'); |
| 910 } | 910 } |
| (...skipping 693 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1604 } | 1604 } |
| 1605 | 1605 |
| 1606 String toString() => 'root=$rootLibrary,libraries=${_newLibraries}'; | 1606 String toString() => 'root=$rootLibrary,libraries=${_newLibraries}'; |
| 1607 } | 1607 } |
| 1608 | 1608 |
| 1609 /// Adapter class to mimic the behavior of LoadedLibraries for Kernel element | 1609 /// Adapter class to mimic the behavior of LoadedLibraries for Kernel element |
| 1610 /// behavior. Ultimately we'll just access worldBuilder instead. | 1610 /// behavior. Ultimately we'll just access worldBuilder instead. |
| 1611 class _LoadedLibrariesAdapter implements LoadedLibraries { | 1611 class _LoadedLibrariesAdapter implements LoadedLibraries { |
| 1612 final LibraryEntity rootLibrary; | 1612 final LibraryEntity rootLibrary; |
| 1613 final List<LibraryEntity> _newLibraries; | 1613 final List<LibraryEntity> _newLibraries; |
| 1614 final KernelWorldBuilder worldBuilder; | 1614 final KernelToElementMap worldBuilder; |
| 1615 | 1615 |
| 1616 _LoadedLibrariesAdapter( | 1616 _LoadedLibrariesAdapter( |
| 1617 this.rootLibrary, this._newLibraries, this.worldBuilder) { | 1617 this.rootLibrary, this._newLibraries, this.worldBuilder) { |
| 1618 assert(rootLibrary != null); | 1618 assert(rootLibrary != null); |
| 1619 } | 1619 } |
| 1620 | 1620 |
| 1621 bool containsLibrary(Uri uri) => getLibrary(uri) != null; | 1621 bool containsLibrary(Uri uri) => getLibrary(uri) != null; |
| 1622 | 1622 |
| 1623 LibraryEntity getLibrary(Uri uri) => worldBuilder.lookupLibrary(uri); | 1623 LibraryEntity getLibrary(Uri uri) => worldBuilder.lookupLibrary(uri); |
| 1624 | 1624 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1678 } | 1678 } |
| 1679 | 1679 |
| 1680 /// API used by the library loader to synchronously scan a library or | 1680 /// API used by the library loader to synchronously scan a library or |
| 1681 /// compilation unit and ensure that their library tags are computed. | 1681 /// compilation unit and ensure that their library tags are computed. |
| 1682 abstract class ElementScanner { | 1682 abstract class ElementScanner { |
| 1683 void scanLibrary(LibraryElement library); | 1683 void scanLibrary(LibraryElement library); |
| 1684 void scanUnit(CompilationUnitElement unit); | 1684 void scanUnit(CompilationUnitElement unit); |
| 1685 } | 1685 } |
| 1686 | 1686 |
| 1687 const _reuseLibrarySubtaskName = "Reuse library"; | 1687 const _reuseLibrarySubtaskName = "Reuse library"; |
| OLD | NEW |