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

Unified Diff: sdk/lib/_internal/compiler/js_lib/js_helper.dart

Issue 956953002: Support for a dart2js `dartDeferredLoader` hook. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix text in pkg/compiler/lib/src/js_emitter/old_emitter/declarations.dart Created 5 years, 10 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: sdk/lib/_internal/compiler/js_lib/js_helper.dart
diff --git a/sdk/lib/_internal/compiler/js_lib/js_helper.dart b/sdk/lib/_internal/compiler/js_lib/js_helper.dart
index 1b33108bd3b0a86bad1e98c4c6d815962d61dfd8..49f53ed6c08d37b3bfc8d554261455cfba3c253e 100644
--- a/sdk/lib/_internal/compiler/js_lib/js_helper.dart
+++ b/sdk/lib/_internal/compiler/js_lib/js_helper.dart
@@ -3451,9 +3451,6 @@ Future<Null> loadDeferredLibrary(String loadId) {
}
Future<Null> _loadHunk(String hunkName) {
- // TODO(ahe): Validate libraryName. Kasper points out that you want
- // to be able to experiment with the effect of toggling @DeferLoad,
- // so perhaps we should silently ignore "bad" library names.
Future<Null> future = _loadingLibraries[hunkName];
if (future != null) {
return future.then((_) => null);
@@ -3464,87 +3461,74 @@ Future<Null> _loadHunk(String hunkName) {
int index = uri.lastIndexOf('/');
uri = '${uri.substring(0, index + 1)}$hunkName';
- if (Primitives.isJsshell || Primitives.isD8) {
- // TODO(ahe): Move this code to a JavaScript command helper script that is
- // not included in generated output.
- return _loadingLibraries[hunkName] = new Future<Null>(() {
+ var deferredLibraryLoader = JS('', 'self.dartDeferredLibraryLoader');
+ Completer<Null> completer = new Completer<Null>();
+
+ void success() {
+ completer.complete(null);
+ }
+
+ void failure([error, StackTrace stackTrace]) {
+ _loadingLibraries[hunkName] = null;
+ completer.completeError(
+ new DeferredLoadException("Loading $uri failed: $error"),
+ stackTrace);
+ }
+
+ var jsSuccess = convertDartClosureToJS(success, 0);
+ var jsFailure = convertDartClosureToJS((error) {
+ failure(unwrapException(error), getTraceFromException(error));
+ }, 1);
+
+ if (JS('bool', 'typeof # === "function"', deferredLibraryLoader)) {
+ try {
+ JS('void', '#(#, #, #)', deferredLibraryLoader, uri,
+ jsSuccess, jsFailure);
+ } catch (error, stackTrace) {
+ failure(error, stackTrace);
+ }
+ } else if (isWorker()) {
+ // We are in a web worker. Load the code with an XMLHttpRequest.
+ enterJsAsync();
+ Future<Null> leavingFuture = completer.future.whenComplete(() {
+ leaveJsAsync();
+ });
+
+ int index = uri.lastIndexOf('/');
+ uri = '${uri.substring(0, index + 1)}$hunkName';
+ var xhr = JS('dynamic', 'new XMLHttpRequest()');
sra1 2015/04/16 17:23:04 Don't use dynamic here. Use '' or 'var'. 'dynamic
sigurdm 2015/04/17 08:37:37 Acknowledged. It seems you already made the change
+ JS('void', '#.open("GET", #)', xhr, uri);
+ JS('void', '#.addEventListener("load", #, false)',
+ xhr, convertDartClosureToJS((event) {
+ if (JS('int', '#.status', xhr) != 200) {
+ failure("");
+ }
+ String code = JS('String', '#.responseText', xhr);
try {
// Create a new function to avoid getting access to current function
// context.
- JS('void', '(new Function(#))()', 'load("$uri")');
+ JS('void', '(new Function(#))()', code);
+ success();
} catch (error, stackTrace) {
- _loadingLibraries[hunkName] = null;
- throw new DeferredLoadException("Loading $uri failed.");
+ failure(error, stackTrace);
}
- return null;
- });
- } else if (isWorker()) {
- // We are in a web worker. Load the code with an XMLHttpRequest.
- return _loadingLibraries[hunkName] = new Future<Null>(() {
- Completer completer = new Completer<Null>();
- enterJsAsync();
- Future<Null> leavingFuture = completer.future.whenComplete(() {
- leaveJsAsync();
- });
+ }, 1));
- int index = uri.lastIndexOf('/');
- uri = '${uri.substring(0, index + 1)}$hunkName';
- var xhr = JS('dynamic', 'new XMLHttpRequest()');
- JS('void', '#.open("GET", #)', xhr, uri);
- JS('void', '#.addEventListener("load", #, false)',
- xhr, convertDartClosureToJS((event) {
- if (JS('int', '#.status', xhr) != 200) {
- _loadingLibraries[hunkName] = null;
- completer.completeError(
- new DeferredLoadException("Loading $uri failed."));
- return;
- }
- String code = JS('String', '#.responseText', xhr);
- try {
- // Create a new function to avoid getting access to current function
- // context.
- JS('void', '(new Function(#))()', code);
- } catch (error, stackTrace) {
- _loadingLibraries[hunkName] = null;
- completer.completeError(
- new DeferredLoadException("Evaluating $uri failed."));
- return;
- }
- completer.complete(null);
- }, 1));
-
- var fail = convertDartClosureToJS((event) {
- _loadingLibraries[hunkName] = null;
- new DeferredLoadException("Loading $uri failed.");
- }, 1);
- JS('void', '#.addEventListener("error", #, false)', xhr, fail);
- JS('void', '#.addEventListener("abort", #, false)', xhr, fail);
-
- JS('void', '#.send()', xhr);
- return leavingFuture;
- });
- }
- // We are in a dom-context.
- return _loadingLibraries[hunkName] = new Future<Null>(() {
- Completer completer = new Completer<Null>();
+ JS('void', '#.addEventListener("error", #, false)', xhr, failure);
+ JS('void', '#.addEventListener("abort", #, false)', xhr, failure);
+ JS('void', '#.send()', xhr);
+ } else {
+ // We are in a dom-context.
// Inject a script tag.
var script = JS('', 'document.createElement("script")');
JS('', '#.type = "text/javascript"', script);
JS('', '#.src = #', script, uri);
- JS('', '#.addEventListener("load", #, false)',
- script, convertDartClosureToJS((event) {
- completer.complete(null);
- }, 1));
- JS('', '#.addEventListener("error", #, false)',
- script, convertDartClosureToJS((event) {
- _loadingLibraries[hunkName] = null;
- completer.completeError(
- new DeferredLoadException("Loading $uri failed."));
- }, 1));
+ JS('', '#.addEventListener("load", #, false)', script, jsSuccess);
+ JS('', '#.addEventListener("error", #, false)', script, jsFailure);
JS('', 'document.body.appendChild(#)', script);
-
- return completer.future;
- });
+ }
+ _loadingLibraries[hunkName] = completer.future;
+ return completer.future;
}
class MainError extends Error implements NoSuchMethodError {
« no previous file with comments | « pkg/compiler/lib/src/js_emitter/old_emitter/declarations.dart ('k') | sdk/lib/_internal/compiler/js_lib/preambles/d8.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698