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

Unified Diff: runtime/bin/builtin.dart

Issue 290713004: First step towards asynchronous loading of sources (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 7 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
« no previous file with comments | « no previous file | runtime/bin/builtin_natives.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/builtin.dart
===================================================================
--- runtime/bin/builtin.dart (revision 36574)
+++ runtime/bin/builtin.dart (working copy)
@@ -5,6 +5,7 @@
library builtin;
import 'dart:io';
import 'dart:async';
+import 'dart:convert';
// import 'root_library'; happens here from C Code
// The root library (aka the script) is imported into this library. The
@@ -107,6 +108,47 @@
}
+void _httpGet(Uri uri, loadCallback(List<int> data)) {
+ var httpClient = new HttpClient();
+ try {
+ httpClient.getUrl(uri)
+ .then((HttpClientRequest request) {
+ request.persistentConnection = false;
+ return request.close();
+ })
+ .then((HttpClientResponse response) {
+ // Only create a ByteBuilder if multiple chunks are received.
+ var builder = new BytesBuilder(copy: false);
+ response.listen(
+ builder.add,
+ onDone: () {
+ if (response.statusCode != 200) {
+ var msg = 'Failure getting $uri: '
+ '${response.statusCode} ${response.reasonPhrase}';
+ _asyncLoadError(uri.toString(), msg);
+ }
+
+ List<int> data = builder.takeBytes();
+ httpClient.close();
+ loadCallback(data);
+ },
+ onError: (error) {
+ _asyncLoadError(uri.toString(), error);
+ });
+ })
+ .catchError((error) {
+ _asyncLoadError(uri.toString(), error);
+ });
+ } catch (error) {
+ _asyncLoadError(uri.toString(), error);
+ }
+ // TODO(floitsch): remove this line. It's just here to push an event on the
+ // event loop so that we invoke the scheduled microtasks. Also remove the
+ // import of dart:async when this line is not needed anymore.
+ Timer.run(() {});
+}
+
+
// Are we running on Windows?
var _isWindows = false;
var _workingWindowsDrivePrefix;
@@ -266,6 +308,59 @@
}
+void _loadScript(String uri, List<int> data) native "Builtin_LoadScript";
+
+void _asyncLoadError(uri, error) native "Builtin_AsyncLoadError";
+
+
+// Asynchronously loads script data (source or snapshot) through
+// an http or file uri.
+_loadDataAsync(String uri) {
+ uri = _resolveScriptUri(uri);
+ Uri sourceUri = Uri.parse(uri);
+ if (sourceUri.scheme == 'http') {
+ _httpGet(sourceUri, (data) {
+ _loadScript(uri, data);
+ });
+ } else {
+ _loadDataFromFileAsync(uri);
+ }
+}
+
+_loadDataFromFileAsync(String uri) {
+ var sourceFile = new File(_filePathFromUri(uri));
+ sourceFile.readAsBytes().then((data) {
+ _loadScript(uri, data);
+ },
+ onError: (e) {
+ _asyncLoadError(uri, e);
+ });
+}
+
+
+void _loadLibrarySource(tag, uri, libraryUri, text)
+ native "Builtin_LoadLibrarySource";
+
+_loadSourceAsync(int tag, String uri, String libraryUri) {
+ var filePath = _filePathFromUri(uri);
+ Uri sourceUri = Uri.parse(filePath);
+ if (sourceUri.scheme == 'http') {
+ _httpGet(sourceUri, (data) {
+ var text = UTF8.decode(data);
+ _loadLibrarySource(tag, uri, libraryUri, text);
+ });
+ } else {
+ var sourceFile = new File(filePath);
+ sourceFile.readAsString().then((text) {
+ _loadLibrarySource(tag, uri, libraryUri, text);
+ },
+ onError: (e) {
+ _asyncLoadError(uri, e);
+ });
+ }
+}
+
+
// Returns the directory part, the filename part, and the name
// of a native extension URL as a list [directory, filename, name].
// The directory part is either a file system path or an HTTP(S) URL.
« no previous file with comments | « no previous file | runtime/bin/builtin_natives.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698