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 import 'dart:io'; | 6 import 'dart:io'; |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:convert'; | 8 import 'dart:convert'; |
9 // import 'root_library'; happens here from C Code | 9 // import 'root_library'; happens here from C Code |
10 | 10 |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 } catch (error) { | 101 } catch (error) { |
102 _requestFailed(error); | 102 _requestFailed(error); |
103 } | 103 } |
104 // 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 |
105 // 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 |
106 // import of dart:async when this line is not needed anymore. | 106 // import of dart:async when this line is not needed anymore. |
107 Timer.run(() {}); | 107 Timer.run(() {}); |
108 } | 108 } |
109 | 109 |
110 | 110 |
111 void _httpGet(Uri uri, loadCallback(List<int> data)) { | 111 void _httpGet(Uri uri, String libraryUri, loadCallback(List<int> data)) { |
112 var httpClient = new HttpClient(); | 112 var httpClient = new HttpClient(); |
113 try { | 113 try { |
114 httpClient.getUrl(uri) | 114 httpClient.getUrl(uri) |
115 .then((HttpClientRequest request) { | 115 .then((HttpClientRequest request) { |
116 request.persistentConnection = false; | 116 request.persistentConnection = false; |
117 return request.close(); | 117 return request.close(); |
118 }) | 118 }) |
119 .then((HttpClientResponse response) { | 119 .then((HttpClientResponse response) { |
120 // Only create a ByteBuilder if multiple chunks are received. | 120 // Only create a ByteBuilder if multiple chunks are received. |
121 var builder = new BytesBuilder(copy: false); | 121 var builder = new BytesBuilder(copy: false); |
122 response.listen( | 122 response.listen( |
123 builder.add, | 123 builder.add, |
124 onDone: () { | 124 onDone: () { |
125 if (response.statusCode != 200) { | 125 if (response.statusCode != 200) { |
126 var msg = 'Failure getting $uri: ' | 126 var msg = 'Failure getting $uri: ' |
127 '${response.statusCode} ${response.reasonPhrase}'; | 127 '${response.statusCode} ${response.reasonPhrase}'; |
128 _asyncLoadError(uri.toString(), msg); | 128 _asyncLoadError(uri.toString(), libraryUri, msg); |
129 } | 129 } |
130 | 130 |
131 List<int> data = builder.takeBytes(); | 131 List<int> data = builder.takeBytes(); |
132 httpClient.close(); | 132 httpClient.close(); |
133 loadCallback(data); | 133 loadCallback(data); |
134 }, | 134 }, |
135 onError: (error) { | 135 onError: (error) { |
136 _asyncLoadError(uri.toString(), error); | 136 _asyncLoadError(uri.toString(), libraryUri, error); |
137 }); | 137 }); |
138 }) | 138 }) |
139 .catchError((error) { | 139 .catchError((error) { |
140 _asyncLoadError(uri.toString(), error); | 140 _asyncLoadError(uri.toString(), libraryUri, error); |
141 }); | 141 }); |
142 } catch (error) { | 142 } catch (error) { |
143 _asyncLoadError(uri.toString(), error); | 143 _asyncLoadError(uri.toString(), libraryUri, error); |
144 } | 144 } |
145 // TODO(floitsch): remove this line. It's just here to push an event on the | 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 | 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. | 147 // import of dart:async when this line is not needed anymore. |
148 Timer.run(() {}); | 148 Timer.run(() {}); |
149 } | 149 } |
150 | 150 |
151 | 151 |
152 // Are we running on Windows? | 152 // Are we running on Windows? |
153 var _isWindows = false; | 153 var _isWindows = false; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
322 assert(_numOutstandingLoadRequests > 0); | 322 assert(_numOutstandingLoadRequests > 0); |
323 _numOutstandingLoadRequests--; | 323 _numOutstandingLoadRequests--; |
324 _logResolution("native Builtin_LoadScript($uri) completed, " | 324 _logResolution("native Builtin_LoadScript($uri) completed, " |
325 "${_numOutstandingLoadRequests} requests remaining"); | 325 "${_numOutstandingLoadRequests} requests remaining"); |
326 if (_numOutstandingLoadRequests == 0) { | 326 if (_numOutstandingLoadRequests == 0) { |
327 _signalDoneLoading(); | 327 _signalDoneLoading(); |
328 } | 328 } |
329 } | 329 } |
330 | 330 |
331 | 331 |
332 void _asyncLoadErrorCallback(uri, error) native "Builtin_AsyncLoadError"; | 332 void _asyncLoadErrorCallback(uri, libraryUri, error) |
| 333 native "Builtin_AsyncLoadError"; |
333 | 334 |
334 void _asyncLoadError(uri, error) { | 335 void _asyncLoadError(uri, libraryUri, error) { |
335 assert(_numOutstandingLoadRequests > 0); | 336 assert(_numOutstandingLoadRequests > 0); |
| 337 _logResolution("_asyncLoadError($uri), error: $error"); |
336 _numOutstandingLoadRequests--; | 338 _numOutstandingLoadRequests--; |
337 _asyncLoadErrorCallback(uri, error); | 339 _asyncLoadErrorCallback(uri, libraryUri, error); |
| 340 if (_numOutstandingLoadRequests == 0) { |
| 341 _signalDoneLoading(); |
| 342 } |
338 } | 343 } |
339 | 344 |
340 | 345 |
341 // Asynchronously loads script data (source or snapshot) through | 346 // Asynchronously loads script data (source or snapshot) through |
342 // an http or file uri. | 347 // an http or file uri. |
343 _loadDataAsync(String uri) { | 348 _loadDataAsync(String uri) { |
344 uri = _resolveScriptUri(uri); | 349 uri = _resolveScriptUri(uri); |
345 Uri sourceUri = Uri.parse(uri); | 350 Uri sourceUri = Uri.parse(uri); |
346 _numOutstandingLoadRequests++; | 351 _numOutstandingLoadRequests++; |
347 _logResolution("_loadDataAsync($uri), " | 352 _logResolution("_loadDataAsync($uri), " |
348 "${_numOutstandingLoadRequests} requests outstanding"); | 353 "${_numOutstandingLoadRequests} requests outstanding"); |
349 if (sourceUri.scheme == 'http') { | 354 if (sourceUri.scheme == 'http') { |
350 _httpGet(sourceUri, (data) { | 355 _httpGet(sourceUri, null, (data) { |
351 _loadScript(uri, data); | 356 _loadScript(uri, data); |
352 }); | 357 }); |
353 } else { | 358 } else { |
354 var sourceFile = new File(_filePathFromUri(uri)); | 359 var sourceFile = new File(_filePathFromUri(uri)); |
355 sourceFile.readAsBytes().then((data) { | 360 sourceFile.readAsBytes().then((data) { |
356 _loadScript(uri, data); | 361 _loadScript(uri, data); |
357 }, | 362 }, |
358 onError: (e) { | 363 onError: (e) { |
359 _asyncLoadError(uri, e); | 364 _asyncLoadError(uri, null, e); |
360 }); | 365 }); |
361 } | 366 } |
362 } | 367 } |
363 | 368 |
364 | 369 |
365 void _loadLibrarySourceCallback(tag, uri, libraryUri, text) | 370 void _loadLibrarySourceCallback(tag, uri, libraryUri, text) |
366 native "Builtin_LoadLibrarySource"; | 371 native "Builtin_LoadLibrarySource"; |
367 | 372 |
368 void _loadLibrarySource(tag, uri, libraryUri, text) { | 373 void _loadLibrarySource(tag, uri, libraryUri, text) { |
369 // TODO: Currently a compilation error while loading the library is | 374 // TODO: Currently a compilation error while loading the library is |
(...skipping 11 matching lines...) Expand all Loading... |
381 | 386 |
382 | 387 |
383 // Asynchronously loads source code through an http or file uri. | 388 // Asynchronously loads source code through an http or file uri. |
384 _loadSourceAsync(int tag, String uri, String libraryUri) { | 389 _loadSourceAsync(int tag, String uri, String libraryUri) { |
385 var filePath = _filePathFromUri(uri); | 390 var filePath = _filePathFromUri(uri); |
386 Uri sourceUri = Uri.parse(filePath); | 391 Uri sourceUri = Uri.parse(filePath); |
387 _numOutstandingLoadRequests++; | 392 _numOutstandingLoadRequests++; |
388 _logResolution("_loadLibrarySource($uri), " | 393 _logResolution("_loadLibrarySource($uri), " |
389 "${_numOutstandingLoadRequests} requests outstanding"); | 394 "${_numOutstandingLoadRequests} requests outstanding"); |
390 if (sourceUri.scheme == 'http') { | 395 if (sourceUri.scheme == 'http') { |
391 _httpGet(sourceUri, (data) { | 396 _httpGet(sourceUri, libraryUri, (data) { |
392 var text = UTF8.decode(data); | 397 var text = UTF8.decode(data); |
393 _loadLibrarySource(tag, uri, libraryUri, text); | 398 _loadLibrarySource(tag, uri, libraryUri, text); |
394 }); | 399 }); |
395 } else { | 400 } else { |
396 var sourceFile = new File(filePath); | 401 var sourceFile = new File(filePath); |
397 sourceFile.readAsString().then((text) { | 402 sourceFile.readAsString().then((text) { |
398 _loadLibrarySource(tag, uri, libraryUri, text); | 403 _loadLibrarySource(tag, uri, libraryUri, text); |
399 }, | 404 }, |
400 onError: (e) { | 405 onError: (e) { |
401 _asyncLoadError(uri, e); | 406 _asyncLoadError(uri, libraryUri, e); |
402 }); | 407 }); |
403 } | 408 } |
404 } | 409 } |
405 | 410 |
406 | 411 |
407 // Returns the directory part, the filename part, and the name | 412 // Returns the directory part, the filename part, and the name |
408 // of a native extension URL as a list [directory, filename, name]. | 413 // of a native extension URL as a list [directory, filename, name]. |
409 // The directory part is either a file system path or an HTTP(S) URL. | 414 // The directory part is either a file system path or an HTTP(S) URL. |
410 // The filename part is the extension name, with the platform-dependent | 415 // The filename part is the extension name, with the platform-dependent |
411 // prefixes and extensions added. | 416 // prefixes and extensions added. |
(...skipping 30 matching lines...) Expand all Loading... |
442 } else if (Platform.isWindows) { | 447 } else if (Platform.isWindows) { |
443 filename = '$name.dll'; | 448 filename = '$name.dll'; |
444 } else { | 449 } else { |
445 _logResolution( | 450 _logResolution( |
446 'Native extensions not supported on ${Platform.operatingSystem}'); | 451 'Native extensions not supported on ${Platform.operatingSystem}'); |
447 throw 'Native extensions not supported on ${Platform.operatingSystem}'; | 452 throw 'Native extensions not supported on ${Platform.operatingSystem}'; |
448 } | 453 } |
449 | 454 |
450 return [path, filename, name]; | 455 return [path, filename, name]; |
451 } | 456 } |
OLD | NEW |