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 290 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
301 "'$right', not '$wrong'."; | 301 "'$right', not '$wrong'."; |
302 } | 302 } |
303 | 303 |
304 var packageRoot = _packageRoot == null ? | 304 var packageRoot = _packageRoot == null ? |
305 _entryPointScript.resolve('packages/') : | 305 _entryPointScript.resolve('packages/') : |
306 _packageRoot; | 306 _packageRoot; |
307 return _filePathFromUri(packageRoot.resolve(uri.path).toString()); | 307 return _filePathFromUri(packageRoot.resolve(uri.path).toString()); |
308 } | 308 } |
309 | 309 |
310 | 310 |
311 void _loadScript(String uri, List<int> data) native "Builtin_LoadScript"; | 311 int _numOutstandingLoadRequests = 0; |
312 | 312 |
313 void _asyncLoadError(uri, error) native "Builtin_AsyncLoadError"; | 313 void _signalDoneLoading() native "Builtin_DoneLoading"; |
| 314 |
| 315 void _loadScriptCallback(String uri, List<int> data) native "Builtin_LoadScript"
; |
| 316 |
| 317 void _loadScript(String uri, List<int> data) { |
| 318 // TODO: Currently a compilation error while loading the script is |
| 319 // fatal for the isolate. _loadScriptCallback() does not return and |
| 320 // the _numOutstandingLoadRequests counter remains out of sync. |
| 321 _loadScriptCallback(uri, data); |
| 322 assert(_numOutstandingLoadRequests > 0); |
| 323 _numOutstandingLoadRequests--; |
| 324 _logResolution("native Builtin_LoadScript($uri) completed, " |
| 325 "${_numOutstandingLoadRequests} requests remaining"); |
| 326 if (_numOutstandingLoadRequests == 0) { |
| 327 _signalDoneLoading(); |
| 328 } |
| 329 } |
| 330 |
| 331 |
| 332 void _asyncLoadErrorCallback(uri, error) native "Builtin_AsyncLoadError"; |
| 333 |
| 334 void _asyncLoadError(uri, error) { |
| 335 assert(_numOutstandingLoadRequests > 0); |
| 336 _numOutstandingLoadRequests--; |
| 337 _asyncLoadErrorCallback(uri, error); |
| 338 } |
314 | 339 |
315 | 340 |
316 // Asynchronously loads script data (source or snapshot) through | 341 // Asynchronously loads script data (source or snapshot) through |
317 // an http or file uri. | 342 // an http or file uri. |
318 _loadDataAsync(String uri) { | 343 _loadDataAsync(String uri) { |
319 uri = _resolveScriptUri(uri); | 344 uri = _resolveScriptUri(uri); |
320 Uri sourceUri = Uri.parse(uri); | 345 Uri sourceUri = Uri.parse(uri); |
| 346 _numOutstandingLoadRequests++; |
| 347 _logResolution("_loadDataAsync($uri), " |
| 348 "${_numOutstandingLoadRequests} requests outstanding"); |
321 if (sourceUri.scheme == 'http') { | 349 if (sourceUri.scheme == 'http') { |
322 _httpGet(sourceUri, (data) { | 350 _httpGet(sourceUri, (data) { |
323 _loadScript(uri, data); | 351 _loadScript(uri, data); |
324 }); | 352 }); |
325 } else { | 353 } else { |
326 _loadDataFromFileAsync(uri); | 354 var sourceFile = new File(_filePathFromUri(uri)); |
| 355 sourceFile.readAsBytes().then((data) { |
| 356 _loadScript(uri, data); |
| 357 }, |
| 358 onError: (e) { |
| 359 _asyncLoadError(uri, e); |
| 360 }); |
327 } | 361 } |
328 } | 362 } |
329 | 363 |
330 _loadDataFromFileAsync(String uri) { | 364 |
331 var sourceFile = new File(_filePathFromUri(uri)); | 365 void _loadLibrarySourceCallback(tag, uri, libraryUri, text) |
332 sourceFile.readAsBytes().then((data) { | 366 native "Builtin_LoadLibrarySource"; |
333 _loadScript(uri, data); | 367 |
334 }, | 368 void _loadLibrarySource(tag, uri, libraryUri, text) { |
335 onError: (e) { | 369 // TODO: Currently a compilation error while loading the library is |
336 _asyncLoadError(uri, e); | 370 // fatal for the isolate. _loadLibraryCallback() does not return and |
337 }); | 371 // the _numOutstandingLoadRequests counter remains out of sync. |
| 372 _loadLibrarySourceCallback(tag, uri, libraryUri, text); |
| 373 assert(_numOutstandingLoadRequests > 0); |
| 374 _numOutstandingLoadRequests--; |
| 375 _logResolution("native Builtin_LoadLibrarySource($uri) completed, " |
| 376 "${_numOutstandingLoadRequests} requests remaining"); |
| 377 if (_numOutstandingLoadRequests == 0) { |
| 378 _signalDoneLoading(); |
| 379 } |
338 } | 380 } |
339 | 381 |
340 | 382 |
341 void _loadLibrarySource(tag, uri, libraryUri, text) | 383 // Asynchronously loads source code through an http or file uri. |
342 native "Builtin_LoadLibrarySource"; | |
343 | |
344 _loadSourceAsync(int tag, String uri, String libraryUri) { | 384 _loadSourceAsync(int tag, String uri, String libraryUri) { |
345 var filePath = _filePathFromUri(uri); | 385 var filePath = _filePathFromUri(uri); |
346 Uri sourceUri = Uri.parse(filePath); | 386 Uri sourceUri = Uri.parse(filePath); |
| 387 _numOutstandingLoadRequests++; |
| 388 _logResolution("_loadLibrarySource($uri), " |
| 389 "${_numOutstandingLoadRequests} requests outstanding"); |
347 if (sourceUri.scheme == 'http') { | 390 if (sourceUri.scheme == 'http') { |
348 _httpGet(sourceUri, (data) { | 391 _httpGet(sourceUri, (data) { |
349 var text = UTF8.decode(data); | 392 var text = UTF8.decode(data); |
350 _loadLibrarySource(tag, uri, libraryUri, text); | 393 _loadLibrarySource(tag, uri, libraryUri, text); |
351 }); | 394 }); |
352 } else { | 395 } else { |
353 var sourceFile = new File(filePath); | 396 var sourceFile = new File(filePath); |
354 sourceFile.readAsString().then((text) { | 397 sourceFile.readAsString().then((text) { |
355 _loadLibrarySource(tag, uri, libraryUri, text); | 398 _loadLibrarySource(tag, uri, libraryUri, text); |
356 }, | 399 }, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
399 } else if (Platform.isWindows) { | 442 } else if (Platform.isWindows) { |
400 filename = '$name.dll'; | 443 filename = '$name.dll'; |
401 } else { | 444 } else { |
402 _logResolution( | 445 _logResolution( |
403 'Native extensions not supported on ${Platform.operatingSystem}'); | 446 'Native extensions not supported on ${Platform.operatingSystem}'); |
404 throw 'Native extensions not supported on ${Platform.operatingSystem}'; | 447 throw 'Native extensions not supported on ${Platform.operatingSystem}'; |
405 } | 448 } |
406 | 449 |
407 return [path, filename, name]; | 450 return [path, filename, name]; |
408 } | 451 } |
OLD | NEW |