Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(254)

Side by Side Diff: pkg/front_end/lib/src/fasta/source/source_loader.dart

Issue 2865843002: Use FileSystem to read files in SourceLoader and TranslateUri. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 fasta.source_loader; 5 library fasta.source_loader;
6 6
7 import 'dart:async' show Future; 7 import 'dart:async' show Future;
8 8
9 import 'dart:io';
9 import 'dart:typed_data' show Uint8List; 10 import 'dart:typed_data' show Uint8List;
10 11
12 import 'package:front_end/file_system.dart';
11 import 'package:front_end/src/base/instrumentation.dart' show Instrumentation; 13 import 'package:front_end/src/base/instrumentation.dart' show Instrumentation;
12 14
13 import 'package:front_end/src/fasta/builder/ast_factory.dart' show AstFactory; 15 import 'package:front_end/src/fasta/builder/ast_factory.dart' show AstFactory;
14 16
15 import 'package:front_end/src/fasta/kernel/kernel_ast_factory.dart' 17 import 'package:front_end/src/fasta/kernel/kernel_ast_factory.dart'
16 show KernelAstFactory; 18 show KernelAstFactory;
17 19
18 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart' 20 import 'package:front_end/src/fasta/kernel/kernel_shadow_ast.dart'
19 show KernelTypeInferenceEngine; 21 show KernelTypeInferenceEngine;
20 22
(...skipping 16 matching lines...) Expand all
37 import '../errors.dart' show inputError; 39 import '../errors.dart' show inputError;
38 40
39 import '../export.dart' show Export; 41 import '../export.dart' show Export;
40 42
41 import '../loader.dart' show Loader; 43 import '../loader.dart' show Loader;
42 44
43 import '../parser/class_member_parser.dart' show ClassMemberParser; 45 import '../parser/class_member_parser.dart' show ClassMemberParser;
44 46
45 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan; 47 import '../scanner.dart' show ErrorToken, ScannerResult, Token, scan;
46 48
47 import '../io.dart' show readBytesFromFile;
48
49 import 'diet_listener.dart' show DietListener; 49 import 'diet_listener.dart' show DietListener;
50 50
51 import 'diet_parser.dart' show DietParser; 51 import 'diet_parser.dart' show DietParser;
52 52
53 import 'outline_builder.dart' show OutlineBuilder; 53 import 'outline_builder.dart' show OutlineBuilder;
54 54
55 import 'source_class_builder.dart' show SourceClassBuilder; 55 import 'source_class_builder.dart' show SourceClassBuilder;
56 56
57 import 'source_library_builder.dart' show SourceLibraryBuilder; 57 import 'source_library_builder.dart' show SourceLibraryBuilder;
58 58
59 class SourceLoader<L> extends Loader<L> { 59 class SourceLoader<L> extends Loader<L> {
60 /// The [FileSystem] which should be used to access files.
61 final FileSystem fileSystem;
62
60 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{}; 63 final Map<Uri, List<int>> sourceBytes = <Uri, List<int>>{};
61 final bool excludeSource = CompilerContext.current.options.excludeSource; 64 final bool excludeSource = CompilerContext.current.options.excludeSource;
62 65
63 // Used when building directly to kernel. 66 // Used when building directly to kernel.
64 ClassHierarchy hierarchy; 67 ClassHierarchy hierarchy;
65 CoreTypes coreTypes; 68 CoreTypes coreTypes;
66 69
67 final AstFactory astFactory = new KernelAstFactory(); 70 final AstFactory astFactory = new KernelAstFactory();
68 71
69 TypeInferenceEngine typeInferenceEngine; 72 TypeInferenceEngine typeInferenceEngine;
70 73
71 Instrumentation instrumentation; 74 Instrumentation instrumentation;
72 75
73 SourceLoader(KernelTarget target) : super(target); 76 SourceLoader(this.fileSystem, KernelTarget target) : super(target);
74 77
75 Future<Token> tokenize(SourceLibraryBuilder library, 78 Future<Token> tokenize(SourceLibraryBuilder library,
76 {bool suppressLexicalErrors: false}) async { 79 {bool suppressLexicalErrors: false}) async {
77 Uri uri = library.fileUri; 80 Uri uri = library.fileUri;
78 if (uri == null || uri.scheme != "file") { 81 if (uri == null || uri.scheme != "file") {
79 return inputError(library.uri, -1, "Not found: ${library.uri}."); 82 return inputError(library.uri, -1, "Not found: ${library.uri}.");
80 } 83 }
84
85 // Get the library text from the cache, or read from the file system.
81 List<int> bytes = sourceBytes[uri]; 86 List<int> bytes = sourceBytes[uri];
82 if (bytes == null) { 87 if (bytes == null) {
83 bytes = sourceBytes[uri] = await readBytesFromFile(uri); 88 try {
89 List<int> rawBytes = await fileSystem.entityForUri(uri).readAsBytes();
90 Uint8List zeroTerminatedBytes = new Uint8List(rawBytes.length + 1);
91 zeroTerminatedBytes.setRange(0, rawBytes.length, rawBytes);
ahe 2017/05/08 10:06:14 I'd appreciate if we can roll this back until the
Siggi Cherem (dart-lang) 2017/05/08 16:38:22 Another option: instead of adding an API to read i
scheglov 2017/05/08 16:53:36 Well, we could do this. OTOH API bloating is not n
92 bytes = zeroTerminatedBytes;
93 sourceBytes[uri] = bytes;
94 } on FileSystemException catch (e) {
95 // TODO(scheglov) Throw and catch abstract file-system exceptions.
96 String message = e.message;
97 String osMessage = e.osError?.message;
98 if (osMessage != null && osMessage.isNotEmpty) {
99 message = osMessage;
100 }
101 return inputError(uri, -1, message);
102 }
84 } 103 }
104
85 byteCount += bytes.length - 1; 105 byteCount += bytes.length - 1;
86 ScannerResult result = scan(bytes); 106 ScannerResult result = scan(bytes);
87 Token token = result.tokens; 107 Token token = result.tokens;
88 if (!suppressLexicalErrors) { 108 if (!suppressLexicalErrors) {
89 List<int> source = getSource(bytes); 109 List<int> source = getSource(bytes);
90 target.addSourceInformation(library.fileUri, result.lineStarts, source); 110 target.addSourceInformation(library.fileUri, result.lineStarts, source);
91 } 111 }
92 while (token is ErrorToken) { 112 while (token is ErrorToken) {
93 if (!suppressLexicalErrors) { 113 if (!suppressLexicalErrors) {
94 ErrorToken error = token; 114 ErrorToken error = token;
(...skipping 305 matching lines...) Expand 10 before | Expand all | Expand 10 after
400 /// Performs the second phase of top level initializer inference, which is to 420 /// Performs the second phase of top level initializer inference, which is to
401 /// visit fields and top level variables in topologically-sorted order and 421 /// visit fields and top level variables in topologically-sorted order and
402 /// assign their types. 422 /// assign their types.
403 void performInitializerInference() { 423 void performInitializerInference() {
404 typeInferenceEngine.finishTopLevel(); 424 typeInferenceEngine.finishTopLevel();
405 ticker.logMs("Performed initializer inference"); 425 ticker.logMs("Performed initializer inference");
406 } 426 }
407 427
408 List<Uri> getDependencies() => sourceBytes.keys.toList(); 428 List<Uri> getDependencies() => sourceBytes.keys.toList();
409 } 429 }
OLDNEW
« no previous file with comments | « pkg/front_end/lib/src/fasta/kernel/kernel_target.dart ('k') | pkg/front_end/lib/src/fasta/translate_uri.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698