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

Side by Side Diff: dart/sdk/lib/_internal/lib/async_patch.dart

Issue 23523003: Address a number of issues related to computation of currentScript and deferred loading. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Add tests Created 7 years, 3 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Patch file for the dart:async library. 5 // Patch file for the dart:async library.
6 6
7 import 'dart:_isolate_helper' show IsolateNatives, TimerImpl; 7 import 'dart:_isolate_helper' show IsolateNatives, TimerImpl;
8 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS; 8 import 'dart:_foreign_helper' show JS, DART_CLOSURE_TO_JS;
9 9
10 patch Timer _createTimer(Duration duration, void callback()) { 10 patch Timer _createTimer(Duration duration, void callback()) {
(...skipping 22 matching lines...) Expand all
33 } 33 }
34 } 34 }
35 35
36 // TODO(ahe): This should not only apply to this isolate. 36 // TODO(ahe): This should not only apply to this isolate.
37 final _loadedLibraries = <String, Completer<bool>>{}; 37 final _loadedLibraries = <String, Completer<bool>>{};
38 38
39 Future<bool> _load(String libraryName, String uri) { 39 Future<bool> _load(String libraryName, String uri) {
40 // TODO(ahe): Validate libraryName. Kasper points out that you want 40 // TODO(ahe): Validate libraryName. Kasper points out that you want
41 // to be able to experiment with the effect of toggling @DeferLoad, 41 // to be able to experiment with the effect of toggling @DeferLoad,
42 // so perhaps we should silently ignore "bad" library names. 42 // so perhaps we should silently ignore "bad" library names.
43 Completer completer = new Completer<bool>();
44 Future<bool> future = _loadedLibraries[libraryName]; 43 Future<bool> future = _loadedLibraries[libraryName];
45 if (future != null) { 44 if (future != null) {
46 future.then((_) { completer.complete(false); }); 45 return future.then((_) => false);
47 return completer.future;
48 }
49 _loadedLibraries[libraryName] = completer.future;
50
51 if (uri == null) {
52 uri = IsolateNatives.thisScript;
53 int index = uri.lastIndexOf('/');
54 uri = '${uri.substring(0, index + 1)}part.js';
55 } 46 }
56 47
57 if (_hasDocument) { 48 if (IsolateNatives.isJsshell) {
49 // TODO(ahe): Move this code to a JavaScript command helper script that is
50 // not included in generated output.
51 return _loadedLibraries[libraryName] = new Future<bool>(() {
52 if (uri == null) uri = 'part.js';
53 // Create a new function to avoid getting access to current function
54 // context.
55 JS('void', '(new Function(#))()', 'loadRelativeToScript("$uri")');
56 return true;
57 });
58 } else if (IsolateNatives.isD8) {
59 // TODO(ahe): Move this code to a JavaScript command helper script that is
60 // not included in generated output.
61 return _loadedLibraries[libraryName] = new Future<bool>(() {
62 if (uri == null) {
63 uri = IsolateNatives.computeThisScriptD8();
64 int index = uri.lastIndexOf('/');
65 uri = '${uri.substring(0, index + 1)}part.js';
66 }
67 // Create a new function to avoid getting access to current function
68 // context.
69 JS('void', '(new Function(#))()', 'load("$uri")');
70 return true;
71 });
72 }
73
74 Completer completer = new Completer<bool>();
75 _loadedLibraries[libraryName] = completer.future;
76 try {
77 if (uri == null) {
78 uri = IsolateNatives.thisScript;
79 int index = uri.lastIndexOf('/');
80 uri = '${uri.substring(0, index + 1)}part.js';
81 }
82
58 // Inject a script tag. 83 // Inject a script tag.
59 var script = JS('', 'document.createElement("script")'); 84 var script = JS('', 'document.createElement("script")');
60 JS('', '#.type = "text/javascript"', script); 85 JS('', '#.type = "text/javascript"', script);
61 JS('', '#.async = "async"', script); 86 JS('', '#.async = "async"', script);
62 JS('', '#.src = #', script, uri); 87 JS('', '#.src = #', script, uri);
63 var onLoad = JS('', '#.bind(null, #)', 88 var onLoad = JS('', '#.bind(null, #)',
64 DART_CLOSURE_TO_JS(_onDeferredLibraryLoad), completer); 89 DART_CLOSURE_TO_JS(_onDeferredLibraryLoad), completer);
65 JS('', '#.addEventListener("load", #, false)', script, onLoad); 90 JS('', '#.addEventListener("load", #, false)', script, onLoad);
66 JS('', 'document.body.appendChild(#)', script); 91 JS('', 'document.body.appendChild(#)', script);
67 } else if (JS('String', 'typeof load') == 'function') { 92 } catch (e, trace) {
68 Timer.run(() { 93 completer.completeError(e, trace);
69 JS('void', 'load(#)', uri);
70 completer.complete(true);
71 });
72 } else {
73 throw new UnsupportedError('load not supported');
74 } 94 }
75 return completer.future; 95 return completer.future;
76 } 96 }
77 97
78 /// Used to implement deferred loading. Used as callback on "load" 98 /// Used to implement deferred loading. Used as callback on "load"
79 /// event above in [load]. 99 /// event above in [load].
80 _onDeferredLibraryLoad(Completer<bool> completer, event) { 100 _onDeferredLibraryLoad(Completer<bool> completer, event) {
81 completer.complete(true); 101 completer.complete(true);
82 } 102 }
83 103
84 bool get _hasDocument => JS('String', 'typeof document') == 'object'; 104 bool get _hasDocument => JS('String', 'typeof document') == 'object';
OLDNEW
« no previous file with comments | « dart/sdk/lib/_internal/compiler/implementation/js_backend/emitter.dart ('k') | dart/sdk/lib/_internal/lib/isolate_helper.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698