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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | runtime/bin/builtin_natives.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 library builtin; 5 library builtin;
6 import 'dart:io'; 6 import 'dart:io';
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert';
8 // import 'root_library'; happens here from C Code 9 // import 'root_library'; happens here from C Code
9 10
10 // The root library (aka the script) is imported into this library. The 11 // The root library (aka the script) is imported into this library. The
11 // standalone embedder uses this to lookup the main entrypoint in the 12 // standalone embedder uses this to lookup the main entrypoint in the
12 // root library's namespace. 13 // root library's namespace.
13 Function _getMainClosure() => main; 14 Function _getMainClosure() => main;
14 15
15 16
16 // Corelib 'print' implementation. 17 // Corelib 'print' implementation.
17 void _print(arg) { 18 void _print(arg) {
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 } catch (error) { 101 } catch (error) {
101 _requestFailed(error); 102 _requestFailed(error);
102 } 103 }
103 // TODO(floitsch): remove this line. It's just here to push an event on the 104 // TODO(floitsch): remove this line. It's just here to push an event on the
104 // event loop so that we invoke the scheduled microtasks. Also remove the 105 // event loop so that we invoke the scheduled microtasks. Also remove the
105 // import of dart:async when this line is not needed anymore. 106 // import of dart:async when this line is not needed anymore.
106 Timer.run(() {}); 107 Timer.run(() {});
107 } 108 }
108 109
109 110
111 void _httpGet(Uri uri, loadCallback(List<int> data)) {
112 var httpClient = new HttpClient();
113 try {
114 httpClient.getUrl(uri)
115 .then((HttpClientRequest request) {
116 request.persistentConnection = false;
117 return request.close();
118 })
119 .then((HttpClientResponse response) {
120 // Only create a ByteBuilder if multiple chunks are received.
121 var builder = new BytesBuilder(copy: false);
122 response.listen(
123 builder.add,
124 onDone: () {
125 if (response.statusCode != 200) {
126 var msg = 'Failure getting $uri: '
127 '${response.statusCode} ${response.reasonPhrase}';
128 _asyncLoadError(uri.toString(), msg);
129 }
130
131 List<int> data = builder.takeBytes();
132 httpClient.close();
133 loadCallback(data);
134 },
135 onError: (error) {
136 _asyncLoadError(uri.toString(), error);
137 });
138 })
139 .catchError((error) {
140 _asyncLoadError(uri.toString(), error);
141 });
142 } catch (error) {
143 _asyncLoadError(uri.toString(), error);
144 }
145 // TODO(floitsch): remove this line. It's just here to push an event on the
146 // event loop so that we invoke the scheduled microtasks. Also remove the
147 // import of dart:async when this line is not needed anymore.
148 Timer.run(() {});
149 }
150
151
110 // Are we running on Windows? 152 // Are we running on Windows?
111 var _isWindows = false; 153 var _isWindows = false;
112 var _workingWindowsDrivePrefix; 154 var _workingWindowsDrivePrefix;
113 // The current working directory 155 // The current working directory
114 var _workingDirectoryUri; 156 var _workingDirectoryUri;
115 // The URI that the entry point script was loaded from. Remembered so that 157 // The URI that the entry point script was loaded from. Remembered so that
116 // package imports can be resolved relative to it. 158 // package imports can be resolved relative to it.
117 var _entryPointScript; 159 var _entryPointScript;
118 // The directory to look in to resolve "package:" scheme URIs. 160 // The directory to look in to resolve "package:" scheme URIs.
119 var _packageRoot; 161 var _packageRoot;
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 "'$right', not '$wrong'."; 301 "'$right', not '$wrong'.";
260 } 302 }
261 303
262 var packageRoot = _packageRoot == null ? 304 var packageRoot = _packageRoot == null ?
263 _entryPointScript.resolve('packages/') : 305 _entryPointScript.resolve('packages/') :
264 _packageRoot; 306 _packageRoot;
265 return _filePathFromUri(packageRoot.resolve(uri.path).toString()); 307 return _filePathFromUri(packageRoot.resolve(uri.path).toString());
266 } 308 }
267 309
268 310
311 void _loadScript(String uri, List<int> data) native "Builtin_LoadScript";
312
313 void _asyncLoadError(uri, error) native "Builtin_AsyncLoadError";
314
315
316 // Asynchronously loads script data (source or snapshot) through
317 // an http or file uri.
318 _loadDataAsync(String uri) {
319 uri = _resolveScriptUri(uri);
320 Uri sourceUri = Uri.parse(uri);
321 if (sourceUri.scheme == 'http') {
322 _httpGet(sourceUri, (data) {
323 _loadScript(uri, data);
324 });
325 } else {
326 _loadDataFromFileAsync(uri);
327 }
328 }
329
330 _loadDataFromFileAsync(String uri) {
331 var sourceFile = new File(_filePathFromUri(uri));
332 sourceFile.readAsBytes().then((data) {
333 _loadScript(uri, data);
334 },
335 onError: (e) {
336 _asyncLoadError(uri, e);
337 });
338 }
339
340
341 void _loadLibrarySource(tag, uri, libraryUri, text)
342 native "Builtin_LoadLibrarySource";
343
344 _loadSourceAsync(int tag, String uri, String libraryUri) {
345 var filePath = _filePathFromUri(uri);
346 Uri sourceUri = Uri.parse(filePath);
347 if (sourceUri.scheme == 'http') {
348 _httpGet(sourceUri, (data) {
349 var text = UTF8.decode(data);
350 _loadLibrarySource(tag, uri, libraryUri, text);
351 });
352 } else {
353 var sourceFile = new File(filePath);
354 sourceFile.readAsString().then((text) {
355 _loadLibrarySource(tag, uri, libraryUri, text);
356 },
357 onError: (e) {
358 _asyncLoadError(uri, e);
359 });
360 }
361 }
362
363
269 // Returns the directory part, the filename part, and the name 364 // Returns the directory part, the filename part, and the name
270 // of a native extension URL as a list [directory, filename, name]. 365 // of a native extension URL as a list [directory, filename, name].
271 // The directory part is either a file system path or an HTTP(S) URL. 366 // The directory part is either a file system path or an HTTP(S) URL.
272 // The filename part is the extension name, with the platform-dependent 367 // The filename part is the extension name, with the platform-dependent
273 // prefixes and extensions added. 368 // prefixes and extensions added.
274 _extensionPathFromUri(String userUri) { 369 _extensionPathFromUri(String userUri) {
275 if (!userUri.startsWith(_DART_EXT)) { 370 if (!userUri.startsWith(_DART_EXT)) {
276 throw 'Unexpected internal error: Extension URI $userUri missing dart-ext:'; 371 throw 'Unexpected internal error: Extension URI $userUri missing dart-ext:';
277 } 372 }
278 userUri = userUri.substring(_DART_EXT.length); 373 userUri = userUri.substring(_DART_EXT.length);
(...skipping 25 matching lines...) Expand all
304 } else if (Platform.isWindows) { 399 } else if (Platform.isWindows) {
305 filename = '$name.dll'; 400 filename = '$name.dll';
306 } else { 401 } else {
307 _logResolution( 402 _logResolution(
308 'Native extensions not supported on ${Platform.operatingSystem}'); 403 'Native extensions not supported on ${Platform.operatingSystem}');
309 throw 'Native extensions not supported on ${Platform.operatingSystem}'; 404 throw 'Native extensions not supported on ${Platform.operatingSystem}';
310 } 405 }
311 406
312 return [path, filename, name]; 407 return [path, filename, name];
313 } 408 }
OLDNEW
« 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