dart-sdk/runtime/lib/lib_prefix.dart
hausner@google.com 4258c115b4 Handle load errors in deferred code
IO errors on the URL of a deferred library are forwarded to the Future
that handles the deferred load. Syntax errors and finalization errors
are lethal, killing the isolate.

Review URL: https://codereview.chromium.org//419103003

git-svn-id: https://dart.googlecode.com/svn/branches/bleeding_edge/dart@38800 260f80e4-7a28-3924-810f-c04153c831b5
2014-07-31 21:09:33 +00:00

58 lines
1.7 KiB
Dart

// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:async";
import "dart:isolate";
// This type corresponds to the VM-internal class LibraryPrefix.
class _LibraryPrefix {
bool _load() native "LibraryPrefix_load";
Error _loadError() native "LibraryPrefix_loadError";
bool _invalidateDependentCode()
native "LibraryPrefix_invalidateDependentCode";
loadLibrary() {
var completer = _outstandingLoadRequests[this];
if (completer != null) {
return completer.future;
}
completer = new Completer<bool>();
_outstandingLoadRequests[this] = completer;
Timer.run(() {
var hasCompleted = this._load();
// Loading can complete immediately, for example when the same
// library has been loaded eagerly or through another deferred
// prefix. If that is the case, we must invalidate the dependent
// code and complete the future now since there will be no callback
// from the VM.
if (hasCompleted) {
_invalidateDependentCode();
completer.complete(true);
_outstandingLoadRequests.remove(this);
}
});
return completer.future;
}
}
var _outstandingLoadRequests = new Map<_LibraryPrefix, Completer>();
// Called from the VM when all outstanding load requests have
// finished.
_completeDeferredLoads() {
_outstandingLoadRequests.forEach((prefix, completer) {
var error = prefix._loadError();
if (error != null) {
completer.completeError(error);
} else {
prefix._invalidateDependentCode();
completer.complete(true);
}
});
_outstandingLoadRequests.clear();
}