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

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: Missing imports 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..044c4236d0d36d796f160b7bf31ff0241cbb4a85 100644
--- a/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
+++ b/dart/sdk/lib/_internal/compiler/implementation/lib/async_patch.dart
@@ -5,6 +5,7 @@
// Patch file for the dart:async library.
import 'dart:_isolate_helper' show TimerImpl;
+import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS;
typedef void _TimerCallback0();
typedef void _TimerCallback1(Timer timer);
@@ -39,14 +40,98 @@ 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 = _currentScriptUri;
+ int index = JS('int', '#.lastIndexOf("/")', uri);
ngeoffray 2013/02/19 10:04:57 It says it's a URI, but did you check it worked on
ahe 2013/02/19 11:10:36 Yes. It is a URL.
+ uri = JS('String', '#.substring(0, # + 1) + #', uri, index, "part.js");
ngeoffray 2013/02/19 10:04:57 Why don't you use Dart String methods for substrin
ahe 2013/02/19 11:10:36 I was trying to avoid bringing unnecessary "junk"
+ }
+
+ 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';
+
+/// Returns the URI of the current script (as a string).
+// TODO(ahe): Share with IsolateNatives.computeThisScript.
+String get _currentScriptUri() {
+ // TODO(ahe): The following works in Firefox during loading of the
+ // script, and is being considered for the standard.
+ // if (_hasDocument) {
+ // var currentScript = JS('', 'document.currentScript');
+ // if (JS('String', 'typeof #', currentScript) == 'object') {
+ // return JS('String', '#.src', currentScript);
+ // }
+ // }
+
+ var stack = JS('String|Null', 'new Error().stack');
+ if (stack == null) {
+ // According to Internet Explorer documentation, the stack
+ // property is not set until the exception is thrown.
+ stack = JS('String',
+ 'function() {try{throw new Error()}catch(e){return e.stack}}');
+ }
+ var pattern, matches;
+
+ // This pattern matches V8, Chrome, and Internet Explorer stack
+ // traces that look like this:
+ // Error
+ // at methodName (URI:LINE:COLUMN)
+ pattern = JS('', r'new RegExp("^ *at (.*):[0-9]*:[0-9]*$", "m")');
+
+ matches = JS('', '#.match(#)', stack, pattern);
+ if (matches != null) return matches[1];
+
+ // This pattern matches Firefox stack traces that look like this:
+ // methodName@URI:LINE
+ pattern = JS('', r'new RegExp("^[^@]*@(.*):[0-9]*$", "m")');
+
+ matches = JS('', '#.match(#)', stack, pattern);
+ if (matches != null) return matches[1];
+
+ throw new UnsupportedError('Cannot extract URI from "$stack"');
}

Powered by Google App Engine
This is Rietveld 408576698