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

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

Issue 2897903003: Support loading binary data in dart2js (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 side-by-side diff with in-line comments
Download patch
Index: pkg/compiler/lib/src/apiimpl.dart
diff --git a/pkg/compiler/lib/src/apiimpl.dart b/pkg/compiler/lib/src/apiimpl.dart
index 465c5c582802d0a60531daa224cf3d727b0b3176..81ea7a8346fbce702ddbd06e3a66d4540cb60956 100644
--- a/pkg/compiler/lib/src/apiimpl.dart
+++ b/pkg/compiler/lib/src/apiimpl.dart
@@ -115,7 +115,8 @@ class CompilerImpl extends Compiler {
// TODO(johnniwinther): Wrap the result from [provider] in a specialized
// [Future] to ensure that we never execute an asynchronous action without
// setting up the current element of the compiler.
- return new Future.sync(() => callUserProvider(resourceUri))
+ return new Future.sync(
+ () => callUserProvider(resourceUri, api.InputKind.utf8))
.then((SourceFile sourceFile) {
// We use [readableUri] as the URI for the script since need to preserve
// the scheme in the script because [Script.uri] is used for resolving
@@ -132,6 +133,39 @@ class CompilerImpl extends Compiler {
return new Future.value(new Script.synthetic(readableUri));
}
+ Future<Binary> readBinary(Uri resourceUri, [Spannable node]) {
+ if (!resourceUri.isAbsolute) {
+ if (node == null) node = NO_LOCATION_SPANNABLE;
+ reporter.internalError(
+ node, 'Relative uri $resourceUri provided to readScript(Uri).');
+ }
+
+ // We need to store the current element since we are reporting read errors
+ // asynchronously and therefore need to restore the current element for
+ // [node] to be valid.
+ elements.Element element = currentElement;
+ void reportReadError(exception) {
Siggi Cherem (dart-lang) 2017/05/22 22:21:36 should we pull out this helper to reuse it with th
Johnni Winther 2017/05/23 08:43:15 Done.
+ if (element == null || node == null) {
+ reporter.reportErrorMessage(
+ new SourceSpan(resourceUri, 0, 0),
+ MessageKind.READ_SELF_ERROR,
+ {'uri': resourceUri, 'exception': exception});
+ } else {
+ reporter.withCurrentElement(element, () {
+ reporter.reportErrorMessage(node, MessageKind.READ_SCRIPT_ERROR,
Siggi Cherem (dart-lang) 2017/05/22 22:21:36 maybe "SCRIPT" is the wrong kind of word for the e
Johnni Winther 2017/05/23 08:43:15 Renamed to READ_URI_ERROR.
+ {'uri': resourceUri, 'exception': exception});
+ });
+ }
+ }
+
+ return new Future.sync(
+ () => callUserProvider(resourceUri, api.InputKind.binary))
+ .catchError((error) {
+ reportReadError(error);
+ return new Binary(resourceUri, null);
+ });
+ }
+
/**
* Translates a readable URI into a resource URI.
*
@@ -176,16 +210,10 @@ class CompilerImpl extends Compiler {
// and we can't depend on 'dart:io' classes.
packages = new NonFilePackagesDirectoryPackages(options.packageRoot);
} else if (options.packageConfig != null) {
- return callUserProvider(options.packageConfig)
- .then((SourceFile sourceFile) {
- List<int> configContents = sourceFile.slowUtf8ZeroTerminatedBytes();
- // The input provider may put a trailing 0 byte when it reads a source
- // file, which confuses the package config parser.
- if (configContents.length > 0 && configContents.last == 0) {
- configContents = configContents.sublist(0, configContents.length - 1);
- }
+ return callUserProvider(options.packageConfig, api.InputKind.binary)
+ .then((Binary binary) {
packages =
- new MapPackages(pkgs.parse(configContents, options.packageConfig));
+ new MapPackages(pkgs.parse(binary.data, options.packageConfig));
}).catchError((error) {
reporter.reportErrorMessage(
NO_LOCATION_SPANNABLE,
@@ -210,7 +238,8 @@ class CompilerImpl extends Compiler {
if (options.resolutionInputs != null) {
future = Future.forEach(options.resolutionInputs, (Uri resolutionInput) {
reporter.log('Reading serialized data from ${resolutionInput}');
- return callUserProvider(resolutionInput).then((SourceFile sourceFile) {
+ return callUserProvider(resolutionInput, api.InputKind.utf8)
+ .then((SourceFile sourceFile) {
serialization.deserializeFromText(
resolutionInput, sourceFile.slowText());
});
@@ -322,22 +351,10 @@ class CompilerImpl extends Compiler {
}
}
- Future<SourceFile> callUserProvider(Uri uri) {
+ Future<api.Input> callUserProvider(Uri uri, api.InputKind inputKind) {
try {
return userProviderTask
- .measureIo(() => provider.readFromUri(uri))
- .then((data) {
- SourceFile sourceFile;
- if (data is List<int>) {
- sourceFile = new Utf8BytesSourceFile(uri, data);
- } else if (data is String) {
- sourceFile = new StringSourceFile.fromUri(uri, data);
- } else {
- throw "Expected a 'String' or a 'List<int>' from the input "
- "provider, but got: ${Error.safeToString(data)}.";
- }
- return sourceFile;
- });
+ .measureIo(() => provider.readFromUri(uri, inputKind: inputKind));
} catch (ex, s) {
reportCrashInUserCode('Uncaught exception in input provider', ex, s);
rethrow;

Powered by Google App Engine
This is Rietveld 408576698