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 0fccce2a432b6eb93bf2af79f0d317545e165f69..8a7dd5f6fb70caa084e884af30fbe5eba745a1e0 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; |
patch class Timer { |
patch factory Timer(int milliseconds, void callback(Timer timer)) { |
@@ -19,3 +20,85 @@ patch class Timer { |
return new TimerImpl.repeating(milliseconds, callback); |
} |
} |
+ |
+// TODO(ahe): This should not only apply to this isolate. |
+final _loadedLibraries = <String, Completer<bool>>{}; |
+ |
+patch Future<bool> load(String libraryName, {String uri}) { |
kasperl
2013/02/05 08:30:53
How should this behave if the specified library na
|
+ // TODO(ahe): Validate libraryName. |
+ 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); |
+ uri = JS('String', '#.substring(0, # + 1) + #', uri, index, "part.js"); |
+ } |
+ |
+ if (JS('String', 'typeof document') == 'object') { |
kasperl
2013/02/05 08:30:53
Use _hasDocument?
ahe
2013/02/05 13:54:22
Done.
|
+ // 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, #)', Primitives.onDeferredLibraryLoad(), completer); |
kasperl
2013/02/05 08:30:53
4 space indent.
ahe
2013/02/05 13:54:22
Done.
|
+ JS('', '#.addEventListener("load", #, false)', script, onLoad); |
+ JS('', 'document.body.appendChild(#)', script); |
+ } else if (JS('String', 'typeof read') == 'function') { |
kasperl
2013/02/05 08:30:53
Not sure I understand this. If the function 'read'
ahe
2013/02/05 13:54:22
It's a mistake. I got the name wrong first :-)
|
+ new Timer(0, (_) { |
+ JS('void', 'load(#)', uri); |
+ completer.complete(true); |
+ }); |
+ } else { |
+ throw new UnsupportedError('load not supported'); |
+ } |
+ return completer.future; |
+} |
+ |
+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', 'throw new Error().stack'); |
kasperl
2013/02/05 08:30:53
Why doesn't this throw an uncaught exception? Did
ahe
2013/02/05 13:54:22
Yes. This was caught by my tests, but after having
|
+ 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"'); |
+} |