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

Side by Side Diff: pkg/front_end/lib/src/incremental/file_state.dart

Issue 2867303002: FileState needs only bytes of the file. (Closed)
Patch Set: Add a comment. 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2017, 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 import 'dart:async'; 5 import 'dart:async';
6 import 'dart:convert';
7 import 'dart:typed_data'; 6 import 'dart:typed_data';
8 7
9 import 'package:front_end/file_system.dart'; 8 import 'package:front_end/file_system.dart';
10 import 'package:front_end/src/fasta/parser/top_level_parser.dart'; 9 import 'package:front_end/src/fasta/parser/top_level_parser.dart';
11 import 'package:front_end/src/fasta/scanner.dart'; 10 import 'package:front_end/src/fasta/scanner.dart';
12 import 'package:front_end/src/fasta/source/directive_listener.dart'; 11 import 'package:front_end/src/fasta/source/directive_listener.dart';
13 import 'package:front_end/src/fasta/translate_uri.dart'; 12 import 'package:front_end/src/fasta/translate_uri.dart';
14 13
15 /// Information about a file being compiled, explicitly or implicitly. 14 /// Information about a file being compiled, explicitly or implicitly.
16 /// 15 ///
17 /// It provides a consistent view on its properties. 16 /// It provides a consistent view on its properties.
18 /// 17 ///
19 /// The properties are not guaranteed to represent the most recent state 18 /// The properties are not guaranteed to represent the most recent state
20 /// of the file system. To update the file to the most recent state, [refresh] 19 /// of the file system. To update the file to the most recent state, [refresh]
21 /// should be called. 20 /// should be called.
22 class FileState { 21 class FileState {
23 final FileSystemState _fsState; 22 final FileSystemState _fsState;
24 23
25 /// The resolved URI of the file in the file system. 24 /// The resolved URI of the file in the file system.
26 final Uri fileUri; 25 final Uri fileUri;
27 26
28 bool _exists; 27 bool _exists;
29 List<int> _contentBytes; 28 List<int> _content;
30 String _content;
31 29
32 List<FileState> _importedFiles; 30 List<FileState> _importedFiles;
33 List<FileState> _exportedFiles; 31 List<FileState> _exportedFiles;
34 List<FileState> _partFiles; 32 List<FileState> _partFiles;
35 33
36 Set<FileState> _directReferencedFiles = new Set<FileState>(); 34 Set<FileState> _directReferencedFiles = new Set<FileState>();
37 35
38 FileState._(this._fsState, this.fileUri); 36 FileState._(this._fsState, this.fileUri);
39 37
40 /// The content of the file. 38 /// The content of the file.
41 String get content => _content; 39 List<int> get content => _content;
42
43 /// The content bytes of the file.
44 List<int> get contentBytes => _contentBytes;
45 40
46 /// Whether the file exists. 41 /// Whether the file exists.
47 bool get exists => _exists; 42 bool get exists => _exists;
48 43
49 @override 44 @override
50 int get hashCode => fileUri.hashCode; 45 int get hashCode => fileUri.hashCode;
51 46
52 /// Return the set of transitive files - the file itself and all of the 47 /// Return the set of transitive files - the file itself and all of the
53 /// directly or indirectly referenced files. 48 /// directly or indirectly referenced files.
54 Set<FileState> get transitiveFiles { 49 Set<FileState> get transitiveFiles {
(...skipping 14 matching lines...) Expand all
69 bool operator ==(Object other) { 64 bool operator ==(Object other) {
70 return other is FileState && other.fileUri == fileUri; 65 return other is FileState && other.fileUri == fileUri;
71 } 66 }
72 67
73 /// Read the file content and ensure that all of the file properties are 68 /// Read the file content and ensure that all of the file properties are
74 /// consistent with the read content, including all its dependencies. 69 /// consistent with the read content, including all its dependencies.
75 Future<Null> refresh() async { 70 Future<Null> refresh() async {
76 // Read the content. 71 // Read the content.
77 try { 72 try {
78 FileSystemEntity entry = _fsState.fileSystem.entityForUri(fileUri); 73 FileSystemEntity entry = _fsState.fileSystem.entityForUri(fileUri);
79 _contentBytes = await entry.readAsBytes(); 74 _content = await entry.readAsBytes();
80 _content = UTF8.decode(_contentBytes);
81 _exists = true; 75 _exists = true;
82 } catch (_) { 76 } catch (_) {
83 _contentBytes = new Uint8List(0); 77 _content = new Uint8List(0);
84 _content = '';
85 _exists = false; 78 _exists = false;
86 } 79 }
87 80
88 // Parse directives. 81 // Parse directives.
89 ScannerResult scannerResults = scanString(_content); 82 ScannerResult scannerResults = _scan();
90 var listener = new DirectiveListener(); 83 var listener = new DirectiveListener();
91 new TopLevelParser(listener).parseUnit(scannerResults.tokens); 84 new TopLevelParser(listener).parseUnit(scannerResults.tokens);
92 85
93 // Build the graph. 86 // Build the graph.
94 _importedFiles = <FileState>[]; 87 _importedFiles = <FileState>[];
95 _exportedFiles = <FileState>[]; 88 _exportedFiles = <FileState>[];
96 _partFiles = <FileState>[]; 89 _partFiles = <FileState>[];
97 await _addFileForRelativeUri(_importedFiles, 'dart:core'); 90 await _addFileForRelativeUri(_importedFiles, 'dart:core');
98 for (String uri in listener.imports) { 91 for (String uri in listener.imports) {
99 await _addFileForRelativeUri(_importedFiles, uri); 92 await _addFileForRelativeUri(_importedFiles, uri);
(...skipping 29 matching lines...) Expand all
129 return; 122 return;
130 } 123 }
131 124
132 // Resolve the absolute URI into the absolute file URI. 125 // Resolve the absolute URI into the absolute file URI.
133 Uri resolvedUri = _fsState.uriTranslator.translate(absoluteUri); 126 Uri resolvedUri = _fsState.uriTranslator.translate(absoluteUri);
134 if (resolvedUri == null) return; 127 if (resolvedUri == null) return;
135 128
136 FileState file = await _fsState.getFile(resolvedUri); 129 FileState file = await _fsState.getFile(resolvedUri);
137 files.add(file); 130 files.add(file);
138 } 131 }
132
133 /// Scan the content of the file.
134 ScannerResult _scan() {
135 var zeroTerminatedBytes = new Uint8List(_content.length + 1);
136 zeroTerminatedBytes.setRange(0, _content.length, _content);
137 return scan(zeroTerminatedBytes);
138 }
139 } 139 }
140 140
141 /// Information about known file system state. 141 /// Information about known file system state.
142 class FileSystemState { 142 class FileSystemState {
143 final FileSystem fileSystem; 143 final FileSystem fileSystem;
144 final TranslateUri uriTranslator; 144 final TranslateUri uriTranslator;
145 145
146 _FileSystemView _fileSystemView; 146 _FileSystemView _fileSystemView;
147 147
148 /// Mapping from file URIs to corresponding [FileState]s. 148 /// Mapping from file URIs to corresponding [FileState]s.
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 /// [FileSystemState] based implementation of [FileSystemEntity]. 190 /// [FileSystemState] based implementation of [FileSystemEntity].
191 class _FileSystemViewEntry implements FileSystemEntity { 191 class _FileSystemViewEntry implements FileSystemEntity {
192 @override 192 @override
193 final Uri uri; 193 final Uri uri;
194 194
195 final FileState file; 195 final FileState file;
196 196
197 _FileSystemViewEntry(this.uri, this.file); 197 _FileSystemViewEntry(this.uri, this.file);
198 198
199 @override 199 @override
200 Future<bool> exists() async => file?.exists ?? false; 200 Future<bool> exists() async => _shouldNotBeQueried();
201 201
202 @override 202 @override
203 Future<DateTime> lastModified() async { 203 Future<DateTime> lastModified() async => _shouldNotBeQueried();
204 throw new StateError( 204
205 'FileSystemViewEntry modification stamp should not be queried'); 205 @override
206 Future<List<int>> readAsBytes() async {
207 if (file == null) {
208 throw new FileSystemException(uri, 'File $uri does not exist.');
209 }
210 return file.content;
206 } 211 }
207 212
208 @override 213 @override
209 Future<List<int>> readAsBytes() async { 214 Future<String> readAsString() async => _shouldNotBeQueried();
210 _throwIfDoesNotExist();
211 return file.contentBytes;
212 }
213 215
214 @override 216 /// _FileSystemViewEntry is used by the incremental kernel generator to
215 Future<String> readAsString() async { 217 /// provide Fasta with a consistent, race condition free view of the files
216 _throwIfDoesNotExist(); 218 /// constituting the project. It should only need to be used for reading
217 return file.content; 219 /// file contents.
218 } 220 dynamic _shouldNotBeQueried() {
219 221 throw new StateError('The method should not be invoked.');
220 void _throwIfDoesNotExist() {
221 if (file == null) {
222 throw new FileSystemException(uri, 'File $uri does not exist.');
223 }
224 } 222 }
225 } 223 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698