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 | |
7 // NOTE: Do not import 'dart:io' in builtin. | 6 // NOTE: Do not import 'dart:io' in builtin. |
8 import 'dart:async'; | 7 import 'dart:async'; |
9 import 'dart:collection'; | 8 import 'dart:collection'; |
10 import 'dart:_internal' hide Symbol; | 9 import 'dart:_internal' hide Symbol; |
11 import 'dart:isolate'; | 10 import 'dart:isolate'; |
12 import 'dart:typed_data'; | 11 import 'dart:typed_data'; |
13 | 12 |
14 // Embedder sets this to true if the --trace-loading flag was passed on the | 13 // Embedder sets this to true if the --trace-loading flag was passed on the |
15 // command line. | 14 // command line. |
16 bool _traceLoading = false; | 15 bool _traceLoading = false; |
17 | 16 |
| 17 |
18 // Before handling an embedder entrypoint we finalize the setup of the | 18 // Before handling an embedder entrypoint we finalize the setup of the |
19 // dart:_builtin library. | 19 // dart:_builtin library. |
20 bool _setupCompleted = false; | 20 bool _setupCompleted = false; |
21 | 21 |
| 22 |
22 // The root library (aka the script) is imported into this library. The | 23 // The root library (aka the script) is imported into this library. The |
23 // standalone embedder uses this to lookup the main entrypoint in the | 24 // standalone embedder uses this to lookup the main entrypoint in the |
24 // root library's namespace. | 25 // root library's namespace. |
25 Function _getMainClosure() => main; | 26 Function _getMainClosure() => main; |
26 | 27 |
| 28 |
27 // 'print' implementation. | 29 // 'print' implementation. |
28 // The standalone embedder registers the closurized _print function with the | 30 // The standalone embedder registers the closurized _print function with the |
29 // dart:core library. | 31 // dart:core library. |
30 void _printString(String s) native "Builtin_PrintString"; | 32 void _printString(String s) native "Builtin_PrintString"; |
31 | 33 |
| 34 |
32 void _print(arg) { | 35 void _print(arg) { |
33 _printString(arg.toString()); | 36 _printString(arg.toString()); |
34 } | 37 } |
35 | 38 |
| 39 |
36 _getPrintClosure() => _print; | 40 _getPrintClosure() => _print; |
37 | 41 |
| 42 |
38 // Corelib 'Uri.base' implementation. | 43 // Corelib 'Uri.base' implementation. |
39 // Uri.base is susceptible to changes in the current working directory. | 44 // Uri.base is susceptible to changes in the current working directory. |
40 _getCurrentDirectoryPath() native "Builtin_GetCurrentDirectory"; | 45 _getCurrentDirectoryPath() native "Builtin_GetCurrentDirectory"; |
41 | 46 |
| 47 |
42 Uri _uriBase() { | 48 Uri _uriBase() { |
43 // We are not using Dircetory.current here to limit the dependency | 49 // We are not using Dircetory.current here to limit the dependency |
44 // on dart:io. This code is the same as: | 50 // on dart:io. This code is the same as: |
45 // return new Uri.directory(Directory.current.path); | 51 // return new Uri.directory(Directory.current.path); |
46 var path = _getCurrentDirectoryPath(); | 52 var path = _getCurrentDirectoryPath(); |
47 return new Uri.directory(path); | 53 return new Uri.directory(path); |
48 } | 54 } |
49 | 55 |
| 56 |
50 _getUriBaseClosure() => _uriBase; | 57 _getUriBaseClosure() => _uriBase; |
51 | 58 |
52 // Asynchronous loading of resources. | 59 // Asynchronous loading of resources. |
53 // The embedder forwards loading requests to the service isolate. | 60 // The embedder forwards loading requests to the service isolate. |
54 | 61 |
55 // A port for communicating with the service isolate for I/O. | 62 // A port for communicating with the service isolate for I/O. |
56 SendPort _loadPort; | 63 SendPort _loadPort; |
57 | 64 |
58 // The isolateId used to communicate with the service isolate for I/O. | 65 // The isolateId used to communicate with the service isolate for I/O. |
59 int _isolateId; | 66 int _isolateId; |
60 | 67 |
61 // Requests made to the service isolate over the load port. | 68 // Requests made to the service isolate over the load port. |
62 | 69 |
63 // Extra requests. Keep these in sync between loader.dart and builtin.dart. | 70 // Extra requests. Keep these in sync between loader.dart and builtin.dart. |
64 const _Dart_kInitLoader = 4; // Initialize the loader. | 71 const _Dart_kInitLoader = 4; // Initialize the loader. |
65 const _Dart_kResourceLoad = 5; // Resource class support. | 72 const _Dart_kResourceLoad = 5; // Resource class support. |
66 const _Dart_kGetPackageRootUri = 6; // Uri of the packages/ directory. | 73 const _Dart_kGetPackageRootUri = 6; // Uri of the packages/ directory. |
67 const _Dart_kGetPackageConfigUri = 7; // Uri of the .packages file. | 74 const _Dart_kGetPackageConfigUri = 7; // Uri of the .packages file. |
68 const _Dart_kResolvePackageUri = 8; // Resolve a package: uri. | 75 const _Dart_kResolvePackageUri = 8; // Resolve a package: uri. |
69 | 76 |
70 // Make a request to the loader. Future will complete with result which is | 77 // Make a request to the loader. Future will complete with result which is |
71 // either a Uri or a List<int>. | 78 // either a Uri or a List<int>. |
72 Future _makeLoaderRequest(int tag, String uri) { | 79 Future _makeLoaderRequest(int tag, String uri) { |
73 assert(_isolateId != null); | 80 assert(_isolateId != null); |
74 assert(_loadPort != null); | 81 assert(_loadPort != null); |
75 Completer completer = new Completer(); | 82 Completer completer = new Completer(); |
76 RawReceivePort port = new RawReceivePort(); | 83 RawReceivePort port = new RawReceivePort(); |
77 port.handler = (msg) { | 84 port.handler = (msg) { |
78 // Close the port. | 85 // Close the port. |
79 port.close(); | 86 port.close(); |
80 completer.complete(msg); | 87 completer.complete(msg); |
81 }; | 88 }; |
82 _loadPort.send([_traceLoading, _isolateId, tag, port.sendPort, uri]); | 89 _loadPort.send([_traceLoading, _isolateId, tag, port.sendPort, uri]); |
83 return completer.future; | 90 return completer.future; |
84 } | 91 } |
85 | 92 |
| 93 |
86 // The current working directory when the embedder was launched. | 94 // The current working directory when the embedder was launched. |
87 Uri _workingDirectory; | 95 Uri _workingDirectory; |
88 // The URI that the root script was loaded from. Remembered so that | 96 // The URI that the root script was loaded from. Remembered so that |
89 // package imports can be resolved relative to it. The root script is the basis | 97 // package imports can be resolved relative to it. The root script is the basis |
90 // for the root library in the VM. | 98 // for the root library in the VM. |
91 Uri _rootScript; | 99 Uri _rootScript; |
92 // The package root set on the command line. | 100 // The package root set on the command line. |
93 Uri _packageRoot; | 101 Uri _packageRoot; |
94 | 102 |
95 // Special handling for Windows paths so that they are compatible with URI | 103 // Special handling for Windows paths so that they are compatible with URI |
(...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 String _filePathFromUri(String userUri) { | 294 String _filePathFromUri(String userUri) { |
287 var uri = Uri.parse(userUri); | 295 var uri = Uri.parse(userUri); |
288 if (_traceLoading) { | 296 if (_traceLoading) { |
289 _log('Getting file path from: $uri'); | 297 _log('Getting file path from: $uri'); |
290 } | 298 } |
291 | 299 |
292 var path; | 300 var path; |
293 switch (uri.scheme) { | 301 switch (uri.scheme) { |
294 case '': | 302 case '': |
295 case 'file': | 303 case 'file': |
296 return uri.toFilePath(); | 304 return uri.toFilePath(); |
297 case 'package': | 305 case 'package': |
298 return _filePathFromUri(_resolvePackageUri(uri).toString()); | 306 return _filePathFromUri(_resolvePackageUri(uri).toString()); |
299 case 'data': | 307 case 'data': |
300 case 'http': | 308 case 'http': |
301 case 'https': | 309 case 'https': |
302 return uri.toString(); | 310 return uri.toString(); |
303 default: | 311 default: |
304 // Only handling file, http, and package URIs | 312 // Only handling file, http, and package URIs |
305 // in standalone binary. | 313 // in standalone binary. |
306 if (_traceLoading) { | 314 if (_traceLoading) { |
307 _log('Unknown scheme (${uri.scheme}) in $uri.'); | 315 _log('Unknown scheme (${uri.scheme}) in $uri.'); |
308 } | 316 } |
309 throw 'Not a known scheme: $uri'; | 317 throw 'Not a known scheme: $uri'; |
310 } | 318 } |
311 } | 319 } |
312 | 320 |
313 // Embedder Entrypoint. | 321 // Embedder Entrypoint. |
314 _libraryFilePath(String libraryUri) { | 322 _libraryFilePath(String libraryUri) { |
315 if (!_setupCompleted) { | 323 if (!_setupCompleted) { |
316 _setupHooks(); | 324 _setupHooks(); |
317 } | 325 } |
318 int index = libraryUri.lastIndexOf('/'); | 326 int index = libraryUri.lastIndexOf('/'); |
319 var path; | 327 var path; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
365 if (_traceLoading) { | 373 if (_traceLoading) { |
366 _log("Request for package Uri resolution from user code: $packageUri"); | 374 _log("Request for package Uri resolution from user code: $packageUri"); |
367 } | 375 } |
368 if (packageUri.scheme != "package") { | 376 if (packageUri.scheme != "package") { |
369 if (_traceLoading) { | 377 if (_traceLoading) { |
370 _log("Non-package Uri, returning unmodified: $packageUri"); | 378 _log("Non-package Uri, returning unmodified: $packageUri"); |
371 } | 379 } |
372 // Return the incoming parameter if not passed a package: URI. | 380 // Return the incoming parameter if not passed a package: URI. |
373 return packageUri; | 381 return packageUri; |
374 } | 382 } |
375 var result = | 383 var result = await _makeLoaderRequest(_Dart_kResolvePackageUri, |
376 await _makeLoaderRequest(_Dart_kResolvePackageUri, packageUri.toString()); | 384 packageUri.toString()); |
377 if (result is! Uri) { | 385 if (result is! Uri) { |
378 if (_traceLoading) { | 386 if (_traceLoading) { |
379 _log("Exception when resolving package URI: $packageUri"); | 387 _log("Exception when resolving package URI: $packageUri"); |
380 } | 388 } |
381 result = null; | 389 result = null; |
382 } | 390 } |
383 if (_traceLoading) { | 391 if (_traceLoading) { |
384 _log("Resolved '$packageUri' to '$result'"); | 392 _log("Resolved '$packageUri' to '$result'"); |
385 } | 393 } |
386 return result; | 394 return result; |
387 } | 395 } |
OLD | NEW |