| 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()');
|
| + 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 {
|
|
|