| 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..8c186458c37ab084c7a22da211e95273b9561649 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,93 @@ 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}) { | 
| +  // 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); | 
| +    uri = JS('String', '#.substring(0, # + 1) + #', uri, index, "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'; | 
| + | 
| +/// 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"'); | 
| +} | 
|  |