| OLD | NEW |
| 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 // NOTE: Do not import 'dart:io' in builtin. |
| 6 import 'dart:async'; | 7 import 'dart:async'; |
| 7 import 'dart:convert'; | 8 import 'dart:convert'; |
| 8 import 'dart:isolate'; | 9 import 'dart:isolate'; |
| 9 | 10 |
| 10 | 11 |
| 11 ////////////////////// | |
| 12 /* Support for loading within the isolate via dart:io */ | |
| 13 import 'dart:io'; | |
| 14 | |
| 15 // Enable by setting the #define LOAD_VIA_SERVICE_ISOLATE (see dartutils.cc) | |
| 16 bool _load_via_service_isolate = false; | |
| 17 | |
| 18 var _httpClient; | |
| 19 void _httpGet(int tag, | |
| 20 Uri uri, | |
| 21 String libraryUri, | |
| 22 loadCallback(List<int> data)) { | |
| 23 if (_httpClient == null) { | |
| 24 _httpClient = new HttpClient()..maxConnectionsPerHost = 6; | |
| 25 } | |
| 26 _httpClient.getUrl(uri) | |
| 27 .then((HttpClientRequest request) => request.close()) | |
| 28 .then((HttpClientResponse response) { | |
| 29 var builder = new BytesBuilder(copy: false); | |
| 30 response.listen( | |
| 31 builder.add, | |
| 32 onDone: () { | |
| 33 if (response.statusCode != 200) { | |
| 34 var msg = 'Failure getting $uri: ' | |
| 35 '${response.statusCode} ${response.reasonPhrase}'; | |
| 36 _asyncLoadError(tag, uri.toString(), libraryUri, msg); | |
| 37 } | |
| 38 loadCallback(builder.takeBytes()); | |
| 39 }, | |
| 40 onError: (error) { | |
| 41 _asyncLoadError(tag, uri.toString(), libraryUri, error); | |
| 42 }); | |
| 43 }) | |
| 44 .catchError((error) { | |
| 45 _asyncLoadError(tag, uri.toString(), libraryUri, error); | |
| 46 }); | |
| 47 // TODO(floitsch): remove this line. It's just here to push an event on the | |
| 48 // event loop so that we invoke the scheduled microtasks. Also remove the | |
| 49 // import of dart:async when this line is not needed anymore. | |
| 50 Timer.run(() {}); | |
| 51 } | |
| 52 | |
| 53 | |
| 54 void _cleanup() { | |
| 55 if (_httpClient != null) { | |
| 56 _httpClient.close(); | |
| 57 _httpClient = null; | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 _loadDataAsyncDartIO(int tag, | |
| 62 String uri, | |
| 63 String libraryUri, | |
| 64 Uri resourceUri) { | |
| 65 _startingOneLoadRequest(uri); | |
| 66 if ((resourceUri.scheme == 'http') || (resourceUri.scheme == 'https')) { | |
| 67 _httpGet(tag, resourceUri, libraryUri, (data) { | |
| 68 _loadScript(tag, uri, libraryUri, data); | |
| 69 }); | |
| 70 } else { | |
| 71 var sourceFile = new File(resourceUri.toFilePath()); | |
| 72 sourceFile.readAsBytes().then((data) { | |
| 73 _loadScript(tag, uri, libraryUri, data); | |
| 74 }, | |
| 75 onError: (e) { | |
| 76 _asyncLoadError(tag, uri, libraryUri, e); | |
| 77 }); | |
| 78 } | |
| 79 } | |
| 80 | |
| 81 ////////////////////// | |
| 82 | |
| 83 /* See Dart_LibraryTag in dart_api.h */ | 12 /* See Dart_LibraryTag in dart_api.h */ |
| 84 const Dart_kScriptTag = null; | 13 const Dart_kScriptTag = null; |
| 85 const Dart_kImportTag = 0; | 14 const Dart_kImportTag = 0; |
| 86 const Dart_kSourceTag = 1; | 15 const Dart_kSourceTag = 1; |
| 87 const Dart_kCanonicalizeUrl = 2; | 16 const Dart_kCanonicalizeUrl = 2; |
| 88 | 17 |
| 89 // Dart native extension scheme. | 18 // Dart native extension scheme. |
| 90 const _DART_EXT = 'dart-ext:'; | 19 const _DART_EXT = 'dart-ext:'; |
| 91 | 20 |
| 92 // import 'root_library'; happens here from C Code | 21 // import 'root_library'; happens here from C Code |
| (...skipping 12 matching lines...) Expand all Loading... |
| 105 void _print(arg) { | 34 void _print(arg) { |
| 106 _Logger._printString(arg.toString()); | 35 _Logger._printString(arg.toString()); |
| 107 } | 36 } |
| 108 | 37 |
| 109 class _Logger { | 38 class _Logger { |
| 110 static void _printString(String s) native "Logger_PrintString"; | 39 static void _printString(String s) native "Logger_PrintString"; |
| 111 } | 40 } |
| 112 | 41 |
| 113 _getPrintClosure() => _print; | 42 _getPrintClosure() => _print; |
| 114 | 43 |
| 115 _getCurrentDirectoryPath() native "Directory_Current"; | 44 _getCurrentDirectoryPath() native "Builtin_GetCurrentDirectory"; |
| 116 | 45 |
| 117 // Corelib 'Uri.base' implementation. | 46 // Corelib 'Uri.base' implementation. |
| 118 Uri _uriBase() { | 47 Uri _uriBase() { |
| 119 // We are not using Dircetory.current here to limit the dependency | 48 // We are not using Dircetory.current here to limit the dependency |
| 120 // on dart:io. This code is the same as: | 49 // on dart:io. This code is the same as: |
| 121 // return new Uri.file(Directory.current.path + "/"); | 50 // return new Uri.file(Directory.current.path + "/"); |
| 122 var result = _getCurrentDirectoryPath(); | 51 var result = _getCurrentDirectoryPath(); |
| 123 if (result is OSError) { | |
| 124 throw new FileSystemException( | |
| 125 "Getting current working directory failed", "", result); | |
| 126 } | |
| 127 return new Uri.file(result + "/"); | 52 return new Uri.file(result + "/"); |
| 128 } | 53 } |
| 129 | 54 |
| 130 _getUriBaseClosure() => _uriBase; | 55 _getUriBaseClosure() => _uriBase; |
| 131 | 56 |
| 132 | 57 |
| 133 // Are we running on Windows? | 58 // Are we running on Windows? |
| 134 var _isWindows; | 59 var _isWindows; |
| 135 var _workingWindowsDrivePrefix; | 60 var _workingWindowsDrivePrefix; |
| 136 // The current working directory | 61 // The current working directory |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 309 int _numOutstandingLoadRequests = 0; | 234 int _numOutstandingLoadRequests = 0; |
| 310 void _finishedOneLoadRequest(String uri) { | 235 void _finishedOneLoadRequest(String uri) { |
| 311 assert(_numOutstandingLoadRequests > 0); | 236 assert(_numOutstandingLoadRequests > 0); |
| 312 _numOutstandingLoadRequests--; | 237 _numOutstandingLoadRequests--; |
| 313 if (_logBuiltin) { | 238 if (_logBuiltin) { |
| 314 _print("Loading of $uri finished, " | 239 _print("Loading of $uri finished, " |
| 315 "${_numOutstandingLoadRequests} requests remaining"); | 240 "${_numOutstandingLoadRequests} requests remaining"); |
| 316 } | 241 } |
| 317 if (_numOutstandingLoadRequests == 0) { | 242 if (_numOutstandingLoadRequests == 0) { |
| 318 _signalDoneLoading(); | 243 _signalDoneLoading(); |
| 319 _cleanup(); | |
| 320 } | 244 } |
| 321 } | 245 } |
| 322 | 246 |
| 323 void _startingOneLoadRequest(String uri) { | 247 void _startingOneLoadRequest(String uri) { |
| 324 assert(_numOutstandingLoadRequests >= 0); | 248 assert(_numOutstandingLoadRequests >= 0); |
| 325 _numOutstandingLoadRequests++; | 249 _numOutstandingLoadRequests++; |
| 326 if (_logBuiltin) { | 250 if (_logBuiltin) { |
| 327 _print("Loading of $uri started, " | 251 _print("Loading of $uri started, " |
| 328 "${_numOutstandingLoadRequests} requests outstanding"); | 252 "${_numOutstandingLoadRequests} requests outstanding"); |
| 329 } | 253 } |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 } | 317 } |
| 394 | 318 |
| 395 // Asynchronously loads script data through a http[s] or file uri. | 319 // Asynchronously loads script data through a http[s] or file uri. |
| 396 _loadDataAsync(int tag, String uri, String libraryUri) { | 320 _loadDataAsync(int tag, String uri, String libraryUri) { |
| 397 if (tag == Dart_kScriptTag) { | 321 if (tag == Dart_kScriptTag) { |
| 398 uri = _resolveScriptUri(uri); | 322 uri = _resolveScriptUri(uri); |
| 399 } | 323 } |
| 400 | 324 |
| 401 Uri resourceUri = _createUri(uri); | 325 Uri resourceUri = _createUri(uri); |
| 402 | 326 |
| 403 if (_load_via_service_isolate) { | 327 _loadDataAsyncLoadPort(tag, uri, libraryUri, resourceUri); |
| 404 _loadDataAsyncLoadPort(tag, uri, libraryUri, resourceUri); | |
| 405 } else { | |
| 406 _loadDataAsyncDartIO(tag, uri, libraryUri, resourceUri); | |
| 407 } | |
| 408 | |
| 409 } | 328 } |
| 410 | 329 |
| 411 // Returns either a file path or a URI starting with http[s]:, as a String. | 330 // Returns either a file path or a URI starting with http[s]:, as a String. |
| 412 String _filePathFromUri(String userUri) { | 331 String _filePathFromUri(String userUri) { |
| 413 var uri = Uri.parse(userUri); | 332 var uri = Uri.parse(userUri); |
| 414 if (_logBuiltin) { | 333 if (_logBuiltin) { |
| 415 _print('# Getting file path from: $uri'); | 334 _print('# Getting file path from: $uri'); |
| 416 } | 335 } |
| 417 | 336 |
| 418 var path; | 337 var path; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 474 } else { | 393 } else { |
| 475 name = userUri.substring(index + 1); | 394 name = userUri.substring(index + 1); |
| 476 path = userUri.substring(0, index + 1); | 395 path = userUri.substring(0, index + 1); |
| 477 } | 396 } |
| 478 | 397 |
| 479 path = _filePathFromUri(path); | 398 path = _filePathFromUri(path); |
| 480 var filename = _platformExtensionFileName(name); | 399 var filename = _platformExtensionFileName(name); |
| 481 | 400 |
| 482 return [path, filename, name]; | 401 return [path, filename, name]; |
| 483 } | 402 } |
| OLD | NEW |