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

Unified Diff: pkg/compiler/lib/src/source_file_provider.dart

Issue 2897903003: Support loading binary data in dart2js (Closed)
Patch Set: Updated cf. comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/compiler/lib/src/serialization/equivalence.dart ('k') | tests/compiler/dart2js/bad_loop_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/compiler/lib/src/source_file_provider.dart
diff --git a/pkg/compiler/lib/src/source_file_provider.dart b/pkg/compiler/lib/src/source_file_provider.dart
index 3923829acda0b8173653ac5dd19fbeedb16635fc..ba766cda7e8a00d24c45cc177da8c17719bf6851 100644
--- a/pkg/compiler/lib/src/source_file_provider.dart
+++ b/pkg/compiler/lib/src/source_file_provider.dart
@@ -21,24 +21,25 @@ import 'util/uri_extras.dart';
abstract class SourceFileProvider implements CompilerInput {
bool isWindows = (Platform.operatingSystem == 'windows');
Uri cwd = currentDirectory;
- Map<Uri, SourceFile> sourceFiles = <Uri, SourceFile>{};
+ Map<Uri, api.Input> sourceFiles = <Uri, api.Input>{};
int dartCharactersRead = 0;
- Future<List<int>> readUtf8BytesFromUri(Uri resourceUri) {
+ Future<api.Input> readBytesFromUri(Uri resourceUri, api.InputKind inputKind) {
if (resourceUri.scheme == 'file') {
- return _readFromFile(resourceUri);
+ return _readFromFile(resourceUri, inputKind);
} else if (resourceUri.scheme == 'http' || resourceUri.scheme == 'https') {
- return _readFromHttp(resourceUri);
+ return _readFromHttp(resourceUri, inputKind);
} else {
throw new ArgumentError("Unknown scheme in uri '$resourceUri'");
}
}
- Future<List<int>> _readFromFile(Uri resourceUri) {
+ Future<api.Input> _readFromFile(Uri resourceUri, api.InputKind inputKind) {
assert(resourceUri.scheme == 'file');
List<int> source;
try {
- source = readAll(resourceUri.toFilePath());
+ source = readAll(resourceUri.toFilePath(),
+ zeroTerminated: inputKind == api.InputKind.utf8);
} on FileSystemException catch (ex) {
String message = ex.osError?.message;
String detail = message != null ? ' ($message)' : '';
@@ -46,12 +47,21 @@ abstract class SourceFileProvider implements CompilerInput {
"Error reading '${relativizeUri(resourceUri)}' $detail");
}
dartCharactersRead += source.length;
- sourceFiles[resourceUri] = new CachingUtf8BytesSourceFile(
- resourceUri, relativizeUri(resourceUri), source);
- return new Future.value(source);
+ api.Input input;
+ switch (inputKind) {
+ case api.InputKind.utf8:
+ input = new CachingUtf8BytesSourceFile(
+ resourceUri, relativizeUri(resourceUri), source);
+ break;
+ case api.InputKind.binary:
+ input = new Binary(resourceUri, source);
+ break;
+ }
+ sourceFiles[resourceUri] = input;
+ return new Future.value(input);
}
- Future<List<int>> _readFromHttp(Uri resourceUri) {
+ Future<api.Input> _readFromHttp(Uri resourceUri, api.InputKind inputKind) {
assert(resourceUri.scheme == 'http');
HttpClient client = new HttpClient();
return client
@@ -75,9 +85,18 @@ abstract class SourceFileProvider implements CompilerInput {
offset += contentPart.length;
}
dartCharactersRead += totalLength;
- sourceFiles[resourceUri] = new CachingUtf8BytesSourceFile(
- resourceUri, resourceUri.toString(), result);
- return result;
+ api.Input input;
+ switch (inputKind) {
+ case api.InputKind.utf8:
+ input = new CachingUtf8BytesSourceFile(
+ resourceUri, resourceUri.toString(), result);
+ break;
+ case api.InputKind.binary:
+ input = new Binary(resourceUri, result);
+ break;
+ }
+ sourceFiles[resourceUri] = input;
+ return input;
});
}
@@ -94,11 +113,15 @@ abstract class SourceFileProvider implements CompilerInput {
}
}
-List<int> readAll(String filename) {
- var file = (new File(filename)).openSync();
- var length = file.lengthSync();
- // +1 to have a 0 terminated list, see [Scanner].
- var buffer = new Uint8List(length + 1);
+List<int> readAll(String filename, {bool zeroTerminated: true}) {
+ RandomAccessFile file = (new File(filename)).openSync();
+ int length = file.lengthSync();
+ int bufferLength = length;
+ if (zeroTerminated) {
+ // +1 to have a 0 terminated list, see [Scanner].
+ bufferLength++;
+ }
+ var buffer = new Uint8List(bufferLength);
file.readIntoSync(buffer, 0, length);
file.closeSync();
return buffer;
@@ -110,7 +133,8 @@ class CompilerSourceFileProvider extends SourceFileProvider {
Future<List<int>> call(Uri resourceUri) => readFromUri(resourceUri);
@override
- Future readFromUri(Uri uri) => readUtf8BytesFromUri(uri);
+ Future readFromUri(Uri uri, {InputKind inputKind: InputKind.utf8}) =>
+ readBytesFromUri(uri, inputKind);
}
class FormattingDiagnosticHandler implements CompilerDiagnostics {
@@ -205,10 +229,8 @@ class FormattingDiagnosticHandler implements CompilerDiagnostics {
if (uri == null) {
print('${color(message)}');
} else {
- SourceFile file = provider.sourceFiles[uri];
- // TODO(johnniwinther): Remove the '.dill' hack; add support for binary
- // files to avoid crashes on trying to decode .dill binaries as utf8.
- if (file != null && !file.filename.endsWith('.dill')) {
+ api.Input file = provider.sourceFiles[uri];
+ if (file is SourceFile) {
print(file.getLocationMessage(color(message), begin, end,
colorize: color));
} else {
@@ -407,7 +429,8 @@ class BazelInputProvider extends SourceFileProvider {
static _resolve(String path) => currentDirectory.resolve(path);
@override
- Future readFromUri(Uri uri) async {
+ Future<api.Input> readFromUri(Uri uri,
+ {InputKind inputKind: InputKind.utf8}) async {
var resolvedUri = uri;
var path = uri.path;
if (path.startsWith('/bazel-root')) {
@@ -420,7 +443,7 @@ class BazelInputProvider extends SourceFileProvider {
}
}
}
- List<int> result = await readUtf8BytesFromUri(resolvedUri);
+ api.Input result = await readBytesFromUri(resolvedUri, inputKind);
sourceFiles[uri] = sourceFiles[resolvedUri];
return result;
}
« no previous file with comments | « pkg/compiler/lib/src/serialization/equivalence.dart ('k') | tests/compiler/dart2js/bad_loop_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698