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

Side by Side 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 unified diff | Download patch
OLDNEW
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 leg_apiimpl; 5 library leg_apiimpl;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 8
9 import 'package:package_config/packages.dart'; 9 import 'package:package_config/packages.dart';
10 import 'package:package_config/packages_file.dart' as pkgs; 10 import 'package:package_config/packages_file.dart' as pkgs;
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 reporter.withCurrentElement(element, () { 108 reporter.withCurrentElement(element, () {
109 reporter.reportErrorMessage(node, MessageKind.DART_EXT_NOT_SUPPORTED); 109 reporter.reportErrorMessage(node, MessageKind.DART_EXT_NOT_SUPPORTED);
110 }); 110 });
111 } 111 }
112 return _synthesizeScript(readableUri); 112 return _synthesizeScript(readableUri);
113 } 113 }
114 114
115 // TODO(johnniwinther): Wrap the result from [provider] in a specialized 115 // TODO(johnniwinther): Wrap the result from [provider] in a specialized
116 // [Future] to ensure that we never execute an asynchronous action without 116 // [Future] to ensure that we never execute an asynchronous action without
117 // setting up the current element of the compiler. 117 // setting up the current element of the compiler.
118 return new Future.sync(() => callUserProvider(resourceUri)) 118 return new Future.sync(
119 () => callUserProvider(resourceUri, api.InputKind.utf8))
119 .then((SourceFile sourceFile) { 120 .then((SourceFile sourceFile) {
120 // We use [readableUri] as the URI for the script since need to preserve 121 // We use [readableUri] as the URI for the script since need to preserve
121 // the scheme in the script because [Script.uri] is used for resolving 122 // the scheme in the script because [Script.uri] is used for resolving
122 // relative URIs mentioned in the script. See the comment on 123 // relative URIs mentioned in the script. See the comment on
123 // [LibraryLoader] for more details. 124 // [LibraryLoader] for more details.
124 return new Script(readableUri, resourceUri, sourceFile); 125 return new Script(readableUri, resourceUri, sourceFile);
125 }).catchError((error) { 126 }).catchError((error) {
126 reportReadError(error); 127 reportReadError(error);
127 return _synthesizeScript(readableUri); 128 return _synthesizeScript(readableUri);
128 }); 129 });
129 } 130 }
130 131
131 Future<Script> _synthesizeScript(Uri readableUri) { 132 Future<Script> _synthesizeScript(Uri readableUri) {
132 return new Future.value(new Script.synthetic(readableUri)); 133 return new Future.value(new Script.synthetic(readableUri));
133 } 134 }
134 135
136 Future<Binary> readBinary(Uri resourceUri, [Spannable node]) {
137 if (!resourceUri.isAbsolute) {
138 if (node == null) node = NO_LOCATION_SPANNABLE;
139 reporter.internalError(
140 node, 'Relative uri $resourceUri provided to readScript(Uri).');
141 }
142
143 // We need to store the current element since we are reporting read errors
144 // asynchronously and therefore need to restore the current element for
145 // [node] to be valid.
146 elements.Element element = currentElement;
147 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.
148 if (element == null || node == null) {
149 reporter.reportErrorMessage(
150 new SourceSpan(resourceUri, 0, 0),
151 MessageKind.READ_SELF_ERROR,
152 {'uri': resourceUri, 'exception': exception});
153 } else {
154 reporter.withCurrentElement(element, () {
155 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.
156 {'uri': resourceUri, 'exception': exception});
157 });
158 }
159 }
160
161 return new Future.sync(
162 () => callUserProvider(resourceUri, api.InputKind.binary))
163 .catchError((error) {
164 reportReadError(error);
165 return new Binary(resourceUri, null);
166 });
167 }
168
135 /** 169 /**
136 * Translates a readable URI into a resource URI. 170 * Translates a readable URI into a resource URI.
137 * 171 *
138 * See [LibraryLoader] for terminology on URIs. 172 * See [LibraryLoader] for terminology on URIs.
139 */ 173 */
140 Uri translateUri(Spannable node, Uri uri) => 174 Uri translateUri(Spannable node, Uri uri) =>
141 uri.scheme == 'package' ? translatePackageUri(node, uri) : uri; 175 uri.scheme == 'package' ? translatePackageUri(node, uri) : uri;
142 176
143 Uri translatePackageUri(Spannable node, Uri uri) { 177 Uri translatePackageUri(Spannable node, Uri uri) {
144 try { 178 try {
(...skipping 24 matching lines...) Expand all
169 .analyzeUri(uri, skipLibraryWithPartOfTag: skipLibraryWithPartOfTag); 203 .analyzeUri(uri, skipLibraryWithPartOfTag: skipLibraryWithPartOfTag);
170 }); 204 });
171 } 205 }
172 206
173 Future setupPackages(Uri uri) { 207 Future setupPackages(Uri uri) {
174 if (options.packageRoot != null) { 208 if (options.packageRoot != null) {
175 // Use "non-file" packages because the file version requires a [Directory] 209 // Use "non-file" packages because the file version requires a [Directory]
176 // and we can't depend on 'dart:io' classes. 210 // and we can't depend on 'dart:io' classes.
177 packages = new NonFilePackagesDirectoryPackages(options.packageRoot); 211 packages = new NonFilePackagesDirectoryPackages(options.packageRoot);
178 } else if (options.packageConfig != null) { 212 } else if (options.packageConfig != null) {
179 return callUserProvider(options.packageConfig) 213 return callUserProvider(options.packageConfig, api.InputKind.binary)
180 .then((SourceFile sourceFile) { 214 .then((Binary binary) {
181 List<int> configContents = sourceFile.slowUtf8ZeroTerminatedBytes();
182 // The input provider may put a trailing 0 byte when it reads a source
183 // file, which confuses the package config parser.
184 if (configContents.length > 0 && configContents.last == 0) {
185 configContents = configContents.sublist(0, configContents.length - 1);
186 }
187 packages = 215 packages =
188 new MapPackages(pkgs.parse(configContents, options.packageConfig)); 216 new MapPackages(pkgs.parse(binary.data, options.packageConfig));
189 }).catchError((error) { 217 }).catchError((error) {
190 reporter.reportErrorMessage( 218 reporter.reportErrorMessage(
191 NO_LOCATION_SPANNABLE, 219 NO_LOCATION_SPANNABLE,
192 MessageKind.INVALID_PACKAGE_CONFIG, 220 MessageKind.INVALID_PACKAGE_CONFIG,
193 {'uri': options.packageConfig, 'exception': error}); 221 {'uri': options.packageConfig, 'exception': error});
194 packages = Packages.noPackages; 222 packages = Packages.noPackages;
195 }); 223 });
196 } else { 224 } else {
197 if (options.packagesDiscoveryProvider == null) { 225 if (options.packagesDiscoveryProvider == null) {
198 packages = Packages.noPackages; 226 packages = Packages.noPackages;
199 } else { 227 } else {
200 return callUserPackagesDiscovery(uri).then((p) { 228 return callUserPackagesDiscovery(uri).then((p) {
201 packages = p; 229 packages = p;
202 }); 230 });
203 } 231 }
204 } 232 }
205 return new Future.value(); 233 return new Future.value();
206 } 234 }
207 235
208 Future<Null> setupSdk() { 236 Future<Null> setupSdk() {
209 Future future = new Future.value(null); 237 Future future = new Future.value(null);
210 if (options.resolutionInputs != null) { 238 if (options.resolutionInputs != null) {
211 future = Future.forEach(options.resolutionInputs, (Uri resolutionInput) { 239 future = Future.forEach(options.resolutionInputs, (Uri resolutionInput) {
212 reporter.log('Reading serialized data from ${resolutionInput}'); 240 reporter.log('Reading serialized data from ${resolutionInput}');
213 return callUserProvider(resolutionInput).then((SourceFile sourceFile) { 241 return callUserProvider(resolutionInput, api.InputKind.utf8)
242 .then((SourceFile sourceFile) {
214 serialization.deserializeFromText( 243 serialization.deserializeFromText(
215 resolutionInput, sourceFile.slowText()); 244 resolutionInput, sourceFile.slowText());
216 }); 245 });
217 }); 246 });
218 } 247 }
219 if (resolvedUriTranslator.isNotSet) { 248 if (resolvedUriTranslator.isNotSet) {
220 future = future.then((_) { 249 future = future.then((_) {
221 return platform_configuration 250 return platform_configuration
222 .load(options.platformConfigUri, provider) 251 .load(options.platformConfigUri, provider)
223 .then((Map<String, Uri> mapping) { 252 .then((Map<String, Uri> mapping) {
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 try { 344 try {
316 userHandlerTask.measure(() { 345 userHandlerTask.measure(() {
317 handler.report(message, uri, begin, end, text, kind); 346 handler.report(message, uri, begin, end, text, kind);
318 }); 347 });
319 } catch (ex, s) { 348 } catch (ex, s) {
320 reportCrashInUserCode('Uncaught exception in diagnostic handler', ex, s); 349 reportCrashInUserCode('Uncaught exception in diagnostic handler', ex, s);
321 rethrow; 350 rethrow;
322 } 351 }
323 } 352 }
324 353
325 Future<SourceFile> callUserProvider(Uri uri) { 354 Future<api.Input> callUserProvider(Uri uri, api.InputKind inputKind) {
326 try { 355 try {
327 return userProviderTask 356 return userProviderTask
328 .measureIo(() => provider.readFromUri(uri)) 357 .measureIo(() => provider.readFromUri(uri, inputKind: inputKind));
329 .then((data) {
330 SourceFile sourceFile;
331 if (data is List<int>) {
332 sourceFile = new Utf8BytesSourceFile(uri, data);
333 } else if (data is String) {
334 sourceFile = new StringSourceFile.fromUri(uri, data);
335 } else {
336 throw "Expected a 'String' or a 'List<int>' from the input "
337 "provider, but got: ${Error.safeToString(data)}.";
338 }
339 return sourceFile;
340 });
341 } catch (ex, s) { 358 } catch (ex, s) {
342 reportCrashInUserCode('Uncaught exception in input provider', ex, s); 359 reportCrashInUserCode('Uncaught exception in input provider', ex, s);
343 rethrow; 360 rethrow;
344 } 361 }
345 } 362 }
346 363
347 Future<Packages> callUserPackagesDiscovery(Uri uri) { 364 Future<Packages> callUserPackagesDiscovery(Uri uri) {
348 try { 365 try {
349 return userPackagesDiscoveryTask 366 return userPackagesDiscoveryTask
350 .measureIo(() => options.packagesDiscoveryProvider(uri)); 367 .measureIo(() => options.packagesDiscoveryProvider(uri));
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 } 428 }
412 } 429 }
413 430
414 /// For every 'dart:' library, a corresponding environment variable is set 431 /// For every 'dart:' library, a corresponding environment variable is set
415 /// to "true". The environment variable's name is the concatenation of 432 /// to "true". The environment variable's name is the concatenation of
416 /// this prefix and the name (without the 'dart:'. 433 /// this prefix and the name (without the 'dart:'.
417 /// 434 ///
418 /// For example 'dart:html' has the environment variable 'dart.library.html' set 435 /// For example 'dart:html' has the environment variable 'dart.library.html' set
419 /// to "true". 436 /// to "true".
420 const String _dartLibraryEnvironmentPrefix = 'dart.library.'; 437 const String _dartLibraryEnvironmentPrefix = 'dart.library.';
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698