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

Unified Diff: dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart

Issue 12033003: Deferred (aka lazy) loading of static functions. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Address comments and use IsolateNatives.thisScript Created 7 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: dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
diff --git a/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart b/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
index 64c828963ccac5d3fba22b5aff259489f2a34a43..cd63a24d691ac2fb2dab85521dc25eb4623d38df 100644
--- a/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
+++ b/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
@@ -4,7 +4,8 @@
// Patch file for the dart:async library.
-import 'dart:_isolate_helper' show TimerImpl;
+import 'dart:_isolate_helper' show IsolateNatives, TimerImpl;
+import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS;
typedef void _TimerCallback0();
typedef void _TimerCallback1(Timer timer);
@@ -39,14 +40,58 @@ patch class Timer {
}
}
-final Set<String> _loadedLibraries = new Set<String>();
-
patch class DeferredLibrary {
patch Future<bool> load() {
- // TODO(ahe): Implement this.
- Future future =
- new Future<bool>.immediate(!_loadedLibraries.contains(libraryName));
- _loadedLibraries.add(libraryName);
- return future;
+ return _load(libraryName, uri);
+ }
+}
+
+// TODO(ahe): This should not only apply to this isolate.
+final _loadedLibraries = <String, Completer<bool>>{};
+
+Future<bool> _load(String libraryName, String uri) {
+ // 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.
+ Completer completer = new Completer<bool>();
+ Future<bool> future = _loadedLibraries[libraryName];
+ if (future != null) {
+ future.then((_) { completer.complete(false); });
+ return completer.future;
+ }
+ _loadedLibraries[libraryName] = completer.future;
+
+ if (uri == null) {
+ uri = IsolateNatives.thisScript;
+ int index = uri.lastIndexOf('/');
+ uri = '${uri.substring(0, index + 1)}part.js';
+ }
+
+ if (_hasDocument) {
+ // Inject a script tag.
+ var script = JS('', 'document.createElement("script")');
+ JS('', '#.type = "text/javascript"', script);
+ JS('', '#.async = "async"', script);
+ JS('', '#.src = #', script, uri);
+ var onLoad = JS('', '#.bind(null, #)',
+ DART_CLOSURE_TO_JS(_onDeferredLibraryLoad), completer);
+ JS('', '#.addEventListener("load", #, false)', script, onLoad);
+ JS('', 'document.body.appendChild(#)', script);
+ } else if (JS('String', 'typeof load') == 'function') {
+ new Timer(0, (_) {
+ JS('void', 'load(#)', uri);
+ completer.complete(true);
+ });
+ } else {
+ throw new UnsupportedError('load not supported');
}
+ return completer.future;
}
+
+/// Used to implement deferred loading. Used as callback on "load"
+/// event above in [load].
+_onDeferredLibraryLoad(Completer<bool> completer, event) {
+ completer.complete(true);
+}
+
+bool get _hasDocument => JS('String', 'typeof document') == 'object';

Powered by Google App Engine
This is Rietveld 408576698