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 // NOTE: Do not import 'dart:io' in builtin. |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:collection'; | 8 import 'dart:collection'; |
9 import 'dart:_internal'; | 9 import 'dart:_internal'; |
10 import 'dart:isolate'; | 10 import 'dart:isolate'; |
11 import 'dart:typed_data'; | 11 import 'dart:typed_data'; |
12 | 12 |
13 // Embedder sets this to true if the --trace-loading flag was passed on the | |
14 // command line. | |
15 bool _traceLoading = false; | |
16 | |
13 | 17 |
14 // Before handling an embedder entrypoint we finalize the setup of the | 18 // Before handling an embedder entrypoint we finalize the setup of the |
15 // dart:_builtin library. | 19 // dart:_builtin library. |
16 bool _setupCompleted = false; | 20 bool _setupCompleted = false; |
17 | 21 |
18 | 22 |
19 // 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 |
20 // standalone embedder uses this to lookup the main entrypoint in the | 24 // standalone embedder uses this to lookup the main entrypoint in the |
21 // root library's namespace. | 25 // root library's namespace. |
22 Function _getMainClosure() => main; | 26 Function _getMainClosure() => main; |
(...skipping 22 matching lines...) Expand all Loading... | |
45 // We are not using Dircetory.current here to limit the dependency | 49 // We are not using Dircetory.current here to limit the dependency |
46 // on dart:io. This code is the same as: | 50 // on dart:io. This code is the same as: |
47 // return new Uri.directory(Directory.current.path); | 51 // return new Uri.directory(Directory.current.path); |
48 var path = _getCurrentDirectoryPath(); | 52 var path = _getCurrentDirectoryPath(); |
49 return new Uri.directory(path); | 53 return new Uri.directory(path); |
50 } | 54 } |
51 | 55 |
52 | 56 |
53 _getUriBaseClosure() => _uriBase; | 57 _getUriBaseClosure() => _uriBase; |
54 | 58 |
55 | |
56 // Asynchronous loading of resources. | 59 // Asynchronous loading of resources. |
57 // The embedder forwards most loading requests to this library. | 60 // The embedder forwards loading requests to the service isolate. |
58 | |
59 // See Dart_LibraryTag in dart_api.h | |
60 const _Dart_kScriptTag = null; | |
61 const _Dart_kImportTag = 0; | |
62 const _Dart_kSourceTag = 1; | |
63 const _Dart_kCanonicalizeUrl = 2; | |
64 const _Dart_kResourceLoad = 3; | |
65 | |
66 // Embedder sets this to true if the --trace-loading flag was passed on the | |
67 // command line. | |
68 bool _traceLoading = false; | |
69 | |
70 // This is currently a build time flag only. We measure the time from the first | |
71 // load request (opening the receive port) to completing the last load | |
72 // request (closing the receive port). Future, deferred load operations will | |
73 // add to this time. | |
74 bool _timeLoading = false; | |
75 Stopwatch _stopwatch; | |
76 | 61 |
77 // A port for communicating with the service isolate for I/O. | 62 // A port for communicating with the service isolate for I/O. |
78 SendPort _loadPort; | 63 SendPort _loadPort; |
79 // The receive port for a load request. Multiple sources can be fetched in | 64 |
80 // a single load request. | 65 // The isolateId used to communicate with the service isolate for I/O. |
81 RawReceivePort _dataPort; | 66 int _isolateId; |
82 // A request id valid only for the current load cycle (while the number of | 67 |
83 // outstanding load requests is greater than 0). Can be reset when loading is | 68 // Requests made to the service isolate over the load port. |
84 // completed. | 69 |
85 int _reqId = 0; | 70 // Extra requests. Keep these in sync between loader.dart and builtin.dart. |
86 // An unordered hash map mapping from request id to a particular load request. | 71 const _Dart_kInitLoader = 4; // Initialize the loader. |
87 // Once there are no outstanding load requests the current load has finished. | 72 const _Dart_kResourceLoad = 5; // Resource class support. |
88 HashMap _reqMap = new HashMap(); | 73 const _Dart_kGetPackageRootUri = 6; // Uri of the packages/ directory. |
74 const _Dart_kGetPackageConfigUri = 7; // Uri of the .packages file. | |
75 const _Dart_kResolvePackageUri = 8; // Resolve a package: uri. | |
76 | |
77 // Make a request to the loader. Future will complete with result which is | |
78 // either a Uri or a List<int>. | |
79 Future _makeLoaderRequest(int tag, String uri) { | |
80 assert(_isolateId != null); | |
81 assert(_loadPort != null); | |
82 Completer completer = new Completer(); | |
83 RawReceivePort port = new RawReceivePort(); | |
84 port.handler = (msg) { | |
85 // Close the port. | |
86 port.close(); | |
87 completer.complete(msg); | |
88 }; | |
89 _loadPort.send([_traceLoading, _isolateId, tag, port.sendPort, uri]); | |
90 return completer.future; | |
91 } | |
92 | |
89 | 93 |
90 // The current working directory when the embedder was launched. | 94 // The current working directory when the embedder was launched. |
91 Uri _workingDirectory; | 95 Uri _workingDirectory; |
92 // 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 |
93 // 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 |
94 // for the root library in the VM. | 98 // for the root library in the VM. |
95 Uri _rootScript; | 99 Uri _rootScript; |
96 | 100 // The package root set on the command line. |
97 // Packages are either resolved looking up in a map or resolved from within a | 101 Uri _packageRoot; |
98 // package root. | |
99 bool get _packagesReady => | |
100 (_packageRoot != null) || (_packageMap != null) || (_packageError != null); | |
101 // Error string set if there was an error resolving package configuration. | |
102 // For example not finding a .packages file or packages/ directory, malformed | |
103 // .packages file or any other related error. | |
104 String _packageError = null; | |
105 // The directory to look in to resolve "package:" scheme URIs. By detault it is | |
106 // the 'packages' directory right next to the script. | |
107 Uri _packageRoot = null; // Used to be _rootScript.resolve('packages/'); | |
108 // The map describing how certain package names are mapped to Uris. | |
109 Uri _packageConfig = null; | |
110 Map<String, Uri> _packageMap = null; | |
111 | |
112 // A list of pending packags which have been requested while resolving the | |
113 // location of the package root or the contents of the package map. | |
114 List<_LoadRequest> _pendingPackageLoads = []; | |
115 | |
116 // If we have outstanding loads or pending package loads waiting for resolution, | |
117 // then we do have pending loads. | |
118 bool _pendingLoads() => !_reqMap.isEmpty || !_pendingPackageLoads.isEmpty; | |
119 | 102 |
120 // 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 |
121 // handling. | 104 // handling. |
122 // Embedder sets this to true if we are running on Windows. | 105 // Embedder sets this to true if we are running on Windows. |
123 bool _isWindows = false; | 106 bool _isWindows = false; |
124 | 107 |
125 // Logging from builtin.dart is prefixed with a '*'. | 108 // Logging from builtin.dart is prefixed with a '*'. |
126 String _logId = (Isolate.current.hashCode % 0x100000).toRadixString(16); | 109 String _logId = (Isolate.current.hashCode % 0x100000).toRadixString(16); |
127 _log(msg) { | 110 _log(msg) { |
128 _print("* $_logId $msg"); | 111 _print("* $_logId $msg"); |
129 } | 112 } |
130 | 113 |
131 // A class wrapping the load error message in an Error object. | |
132 class _LoadError extends Error { | |
133 final _LoadRequest request; | |
134 final String message; | |
135 _LoadError(this.request, this.message); | |
136 | |
137 String toString() { | |
138 var context = request._context; | |
139 if (context == null || context is! String) { | |
140 return 'Could not load "${request._uri}": $message'; | |
141 } else { | |
142 return 'Could not import "${request._uri}" from "$context": $message'; | |
143 } | |
144 } | |
145 } | |
146 | |
147 // Class collecting all of the information about a particular load request. | |
148 class _LoadRequest { | |
149 final int _id = _reqId++; | |
150 final int _tag; | |
151 final String _uri; | |
152 final Uri _resourceUri; | |
153 final _context; | |
154 | |
155 _LoadRequest(this._tag, this._uri, this._resourceUri, this._context) { | |
156 assert(_reqMap[_id] == null); | |
157 _reqMap[_id] = this; | |
158 } | |
159 | |
160 toString() => "LoadRequest($_id, $_tag, $_uri, $_resourceUri, $_context)"; | |
161 } | |
162 | |
163 | |
164 // Native calls provided by the embedder. | |
165 void _signalDoneLoading() native "Builtin_DoneLoading"; | |
166 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) | |
167 native "Builtin_LoadSource"; | |
168 void _asyncLoadErrorCallback(uri, libraryUri, error) | |
169 native "Builtin_AsyncLoadError"; | |
170 | |
171 | |
172 _sanitizeWindowsPath(path) { | 114 _sanitizeWindowsPath(path) { |
173 // For Windows we need to massage the paths a bit according to | 115 // For Windows we need to massage the paths a bit according to |
174 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx | 116 // http://blogs.msdn.com/b/ie/archive/2006/12/06/file-uris-in-windows.aspx |
175 // | 117 // |
176 // Convert | 118 // Convert |
177 // C:\one\two\three | 119 // C:\one\two\three |
178 // to | 120 // to |
179 // /C:/one/two/three | 121 // /C:/one/two/three |
180 | 122 |
181 if (_isWindows == false) { | 123 if (_isWindows == false) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
213 | 155 |
214 | 156 |
215 // Ensure we have a trailing slash character. | 157 // Ensure we have a trailing slash character. |
216 _enforceTrailingSlash(uri) { | 158 _enforceTrailingSlash(uri) { |
217 if (!uri.endsWith('/')) { | 159 if (!uri.endsWith('/')) { |
218 return '$uri/'; | 160 return '$uri/'; |
219 } | 161 } |
220 return uri; | 162 return uri; |
221 } | 163 } |
222 | 164 |
223 | |
224 // Embedder Entrypoint: | 165 // Embedder Entrypoint: |
225 // The embedder calls this method with the current working directory. | 166 // The embedder calls this method with the current working directory. |
226 void _setWorkingDirectory(cwd) { | 167 void _setWorkingDirectory(cwd) { |
227 if (!_setupCompleted) { | 168 if (!_setupCompleted) { |
228 _setupHooks(); | 169 _setupHooks(); |
229 } | 170 } |
230 if (_traceLoading) { | 171 if (_traceLoading) { |
231 _log('Setting working directory: $cwd'); | 172 _log('Setting working directory: $cwd'); |
232 } | 173 } |
233 _workingDirectory = new Uri.directory(cwd); | 174 _workingDirectory = new Uri.directory(cwd); |
(...skipping 23 matching lines...) Expand all Loading... | |
257 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); | 198 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); |
258 } | 199 } |
259 // Now that we have determined the packageRoot value being used, set it | 200 // Now that we have determined the packageRoot value being used, set it |
260 // up for use in Platform.packageRoot. This is only set when the embedder | 201 // up for use in Platform.packageRoot. This is only set when the embedder |
261 // sets up the package root. Automatically discovered package root will | 202 // sets up the package root. Automatically discovered package root will |
262 // not update the VMLibraryHooks value. | 203 // not update the VMLibraryHooks value. |
263 VMLibraryHooks.packageRootString = _packageRoot.toString(); | 204 VMLibraryHooks.packageRootString = _packageRoot.toString(); |
264 if (_traceLoading) { | 205 if (_traceLoading) { |
265 _log('Package root URI: $_packageRoot'); | 206 _log('Package root URI: $_packageRoot'); |
266 } | 207 } |
267 } | 208 return VMLibraryHooks.packageRootString; |
turnidge
2016/06/03 17:50:24
Is this return value used?
Cutch
2016/06/03 22:07:32
Removed this.
| |
268 | |
269 | |
270 // Given a uri with a 'package' scheme, return a Uri that is prefixed with | |
271 // the package root. | |
272 Uri _resolvePackageUri(Uri uri) { | |
273 assert(uri.scheme == "package"); | |
274 assert(_packagesReady); | |
275 | |
276 if (!uri.host.isEmpty) { | |
277 var path = '${uri.host}${uri.path}'; | |
278 var right = 'package:$path'; | |
279 var wrong = 'package://$path'; | |
280 | |
281 throw "URIs using the 'package:' scheme should look like " | |
282 "'$right', not '$wrong'."; | |
283 } | |
284 | |
285 if (_traceLoading) { | |
286 _log('Resolving package with uri path: ${uri.path}'); | |
287 } | |
288 var resolvedUri; | |
289 if (_packageError != null) { | |
290 if (_traceLoading) { | |
291 _log("Resolving package with pending resolution error: $_packageError"); | |
292 } | |
293 throw _packageError; | |
294 } else if (_packageRoot != null) { | |
295 resolvedUri = _packageRoot.resolve(uri.path); | |
296 } else { | |
297 var packageName = uri.pathSegments[0]; | |
298 var mapping = _packageMap[packageName]; | |
299 if (_traceLoading) { | |
300 _log("Mapped '$packageName' package to '$mapping'"); | |
301 } | |
302 if (mapping == null) { | |
303 throw "No mapping for '$packageName' package when resolving '$uri'."; | |
304 } | |
305 var path; | |
306 if (uri.path.length > packageName.length) { | |
307 path = uri.path.substring(packageName.length + 1); | |
308 } else { | |
309 // Handle naked package resolution to the default package name: | |
310 // package:foo is equivalent to package:foo/foo.dart | |
311 assert(uri.path.length == packageName.length); | |
312 path = "$packageName.dart"; | |
313 } | |
314 if (_traceLoading) { | |
315 _log("Path to be resolved in package: $path"); | |
316 } | |
317 resolvedUri = mapping.resolve(path); | |
318 } | |
319 if (_traceLoading) { | |
320 _log("Resolved '$uri' to '$resolvedUri'."); | |
321 } | |
322 return resolvedUri; | |
323 } | |
324 | |
325 | |
326 // Resolves the script uri in the current working directory iff the given uri | |
327 // did not specify a scheme (e.g. a path to a script file on the command line). | |
328 Uri _resolveScriptUri(String scriptName) { | |
329 if (_traceLoading) { | |
330 _log("Resolving script: $scriptName"); | |
331 } | |
332 if (_workingDirectory == null) { | |
333 throw 'No current working directory set.'; | |
334 } | |
335 scriptName = _sanitizeWindowsPath(scriptName); | |
336 | |
337 var scriptUri = Uri.parse(scriptName); | |
338 if (scriptUri.scheme == '') { | |
339 // Script does not have a scheme, assume that it is a path, | |
340 // resolve it against the working directory. | |
341 scriptUri = _workingDirectory.resolveUri(scriptUri); | |
342 } | |
343 | |
344 // Remember the root script URI so that we can resolve packages based on | |
345 // this location. | |
346 _rootScript = scriptUri; | |
347 | |
348 if (_traceLoading) { | |
349 _log('Resolved entry point to: $_rootScript'); | |
350 } | |
351 return scriptUri; | |
352 } | |
353 | |
354 | |
355 void _finishLoadRequest(_LoadRequest req) { | |
356 if (req != null) { | |
357 // Now that we are done with loading remove the request from the map. | |
358 var tmp = _reqMap.remove(req._id); | |
359 assert(tmp == req); | |
360 if (_traceLoading) { | |
361 _log("Loading of ${req._uri} finished: " | |
362 "${_reqMap.length} requests remaining, " | |
363 "${_pendingPackageLoads.length} packages pending."); | |
364 } | |
365 } | |
366 | |
367 if (!_pendingLoads() && (_dataPort != null)) { | |
368 _stopwatch.stop(); | |
369 // Close the _dataPort now that there are no more requests outstanding. | |
370 if (_traceLoading || _timeLoading) { | |
371 _log("Closing loading port: ${_stopwatch.elapsedMilliseconds} ms"); | |
372 } | |
373 _dataPort.close(); | |
374 _dataPort = null; | |
375 _reqId = 0; | |
376 _signalDoneLoading(); | |
377 } | |
378 } | |
379 | |
380 | |
381 void _handleLoaderReply(msg) { | |
382 int id = msg[0]; | |
383 var dataOrError = msg[1]; | |
384 assert((id >= 0) && (id < _reqId)); | |
385 var req = _reqMap[id]; | |
386 try { | |
387 if (dataOrError is Uint8List) { | |
388 // Successfully loaded the data. | |
389 if (req._tag == _Dart_kResourceLoad) { | |
390 Completer c = req._context; | |
391 c.complete(dataOrError); | |
392 } else { | |
393 // TODO: Currently a compilation error while loading the script is | |
394 // fatal for the isolate. _loadScriptCallback() does not return and | |
395 // the number of requests remains out of sync. | |
396 _loadScriptCallback(req._tag, req._uri, req._context, dataOrError); | |
397 } | |
398 _finishLoadRequest(req); | |
399 } else { | |
400 assert(dataOrError is String); | |
401 var error = new _LoadError(req, dataOrError.toString()); | |
402 _asyncLoadError(req, error, null); | |
403 } | |
404 } catch(e, s) { | |
405 // Wrap inside a _LoadError unless we are already propagating a | |
406 // previous _LoadError. | |
407 var error = (e is _LoadError) ? e : new _LoadError(req, e.toString()); | |
408 assert(req != null); | |
409 _asyncLoadError(req, error, s); | |
410 } | |
411 } | |
412 | |
413 | |
414 void _startLoadRequest(int tag, String uri, Uri resourceUri, context) { | |
415 if (_dataPort == null) { | |
416 if (_traceLoading) { | |
417 _log("Initializing load port."); | |
418 } | |
419 // Allocate the Stopwatch if necessary. | |
420 if (_stopwatch == null) { | |
421 _stopwatch = new Stopwatch(); | |
422 } | |
423 assert(_dataPort == null); | |
424 _dataPort = new RawReceivePort(_handleLoaderReply); | |
425 _stopwatch.start(); | |
426 } | |
427 // Register the load request and send it to the VM service isolate. | |
428 var req = new _LoadRequest(tag, uri, resourceUri, context); | |
429 | |
430 assert(_dataPort != null); | |
431 var msg = new List(4); | |
432 msg[0] = _dataPort.sendPort; | |
433 msg[1] = _traceLoading; | |
434 msg[2] = req._id; | |
435 msg[3] = resourceUri.toString(); | |
436 _loadPort.send(msg); | |
437 | |
438 if (_traceLoading) { | |
439 _log("Loading of $resourceUri for $uri started with id: ${req._id}. " | |
440 "${_reqMap.length} requests remaining, " | |
441 "${_pendingPackageLoads.length} packages pending."); | |
442 } | |
443 } | |
444 | |
445 | |
446 RawReceivePort _packagesPort; | |
447 | |
448 void _handlePackagesReply(msg) { | |
449 // Make sure to close the _packagePort before any other action. | |
450 _packagesPort.close(); | |
451 _packagesPort = null; | |
452 | |
453 if (_traceLoading) { | |
454 _log("Got packages reply: $msg"); | |
455 } | |
456 if (msg is String) { | |
457 if (_traceLoading) { | |
458 _log("Got failure response on package port: '$msg'"); | |
459 } | |
460 // Remember the error message. | |
461 _packageError = msg; | |
462 } else if (msg is List) { | |
463 if (msg.length == 1) { | |
464 if (_traceLoading) { | |
465 _log("Received package root: '${msg[0]}'"); | |
466 } | |
467 _packageRoot = Uri.parse(msg[0]); | |
468 } else { | |
469 // First entry contains the location of the loaded .packages file. | |
470 assert((msg.length % 2) == 0); | |
471 assert(msg.length >= 2); | |
472 assert(msg[1] == null); | |
473 _packageConfig = Uri.parse(msg[0]); | |
474 _packageMap = new Map<String, Uri>(); | |
475 for (var i = 2; i < msg.length; i+=2) { | |
476 // TODO(iposva): Complain about duplicate entries. | |
477 _packageMap[msg[i]] = Uri.parse(msg[i+1]); | |
478 } | |
479 if (_traceLoading) { | |
480 _log("Setup package map: $_packageMap"); | |
481 } | |
482 } | |
483 } else { | |
484 _packageError = "Bad type of packages reply: ${msg.runtimeType}"; | |
485 if (_traceLoading) { | |
486 _log(_packageError); | |
487 } | |
488 } | |
489 | |
490 // Resolve all pending package loads now that we know how to resolve them. | |
491 while (_pendingPackageLoads.length > 0) { | |
492 // Order does not matter as we queue all of the requests up right now. | |
493 var req = _pendingPackageLoads.removeLast(); | |
494 // Call the registered closure, to handle the delayed action. | |
495 req(); | |
496 } | |
497 // Reset the pending package loads to empty. So that we eventually can | |
498 // finish loading. | |
499 _pendingPackageLoads = []; | |
500 // Make sure that the receive port is closed if no other loads are pending. | |
501 _finishLoadRequest(null); | |
502 } | |
503 | |
504 | |
505 void _requestPackagesMap() { | |
506 assert(_packagesPort == null); | |
507 assert(_rootScript != null); | |
508 // Create a port to receive the packages map on. | |
509 _packagesPort = new RawReceivePort(_handlePackagesReply); | |
510 var sp = _packagesPort.sendPort; | |
511 | |
512 var msg = new List(4); | |
513 msg[0] = sp; | |
514 msg[1] = _traceLoading; | |
515 msg[2] = -1; | |
516 msg[3] = _rootScript.toString(); | |
517 _loadPort.send(msg); | |
518 | |
519 if (_traceLoading) { | |
520 _log("Requested packages map for '$_rootScript'."); | |
521 } | |
522 } | 209 } |
523 | 210 |
524 | 211 |
525 // Embedder Entrypoint: | 212 // Embedder Entrypoint: |
526 // Request the load of a particular packages map. | 213 void _setPackagesMap(String packagesParam) { |
527 void _loadPackagesMap(String packagesParam) { | |
528 if (!_setupCompleted) { | 214 if (!_setupCompleted) { |
529 _setupHooks(); | 215 _setupHooks(); |
530 } | 216 } |
531 // First convert the packages parameter from the command line to a URI which | 217 // First convert the packages parameter from the command line to a URI which |
532 // can be handled by the loader code. | 218 // can be handled by the loader code. |
533 // TODO(iposva): Consider refactoring the common code below which is almost | 219 // TODO(iposva): Consider refactoring the common code below which is almost |
534 // shared with resolution of the root script. | 220 // shared with resolution of the root script. |
535 if (_traceLoading) { | 221 if (_traceLoading) { |
536 _log("Resolving packages map: $packagesParam"); | 222 _log("Resolving packages map: $packagesParam"); |
537 } | 223 } |
538 if (_workingDirectory == null) { | 224 if (_workingDirectory == null) { |
539 throw 'No current working directory set.'; | 225 throw 'No current working directory set.'; |
540 } | 226 } |
541 var packagesName = _sanitizeWindowsPath(packagesParam); | 227 var packagesName = _sanitizeWindowsPath(packagesParam); |
542 var packagesUri = Uri.parse(packagesName); | 228 var packagesUri = Uri.parse(packagesName); |
543 if (packagesUri.scheme == '') { | 229 if (packagesUri.scheme == '') { |
544 // Script does not have a scheme, assume that it is a path, | 230 // Script does not have a scheme, assume that it is a path, |
545 // resolve it against the working directory. | 231 // resolve it against the working directory. |
546 packagesUri = _workingDirectory.resolveUri(packagesUri); | 232 packagesUri = _workingDirectory.resolveUri(packagesUri); |
547 } | 233 } |
548 var packagesUriStr = packagesUri.toString(); | 234 var packagesUriStr = packagesUri.toString(); |
549 VMLibraryHooks.packageConfigString = packagesUriStr; | 235 VMLibraryHooks.packageConfigString = packagesUriStr; |
550 if (_traceLoading) { | 236 if (_traceLoading) { |
551 _log('Resolved packages map to: $packagesUri'); | 237 _log('Resolved packages map to: $packagesUri'); |
552 } | 238 } |
553 | |
554 // Request the loading and parsing of the packages map at the specified URI. | |
555 // Create a port to receive the packages map on. | |
556 assert(_packagesPort == null); | |
557 _packagesPort = new RawReceivePort(_handlePackagesReply); | |
558 var sp = _packagesPort.sendPort; | |
559 | |
560 var msg = new List(4); | |
561 msg[0] = sp; | |
562 msg[1] = _traceLoading; | |
563 msg[2] = -2; | |
564 msg[3] = packagesUriStr; | |
565 _loadPort.send(msg); | |
566 | |
567 // Signal that the resolution of the packages map has started. But in this | |
568 // case it is not tied to a particular request. | |
569 _pendingPackageLoads.add(() { | |
570 // Nothing to be done beyond registering that there is pending package | |
571 // resolution requested by having an empty entry. | |
572 if (_traceLoading) { | |
573 _log("Skipping dummy deferred request."); | |
574 } | |
575 }); | |
576 | |
577 if (_traceLoading) { | |
578 _log("Requested packages map at '$packagesUri'."); | |
579 } | |
580 } | 239 } |
581 | 240 |
582 | 241 |
583 void _asyncLoadError(_LoadRequest req, _LoadError error, StackTrace stack) { | 242 // Resolves the script uri in the current working directory iff the given uri |
243 // did not specify a scheme (e.g. a path to a script file on the command line). | |
244 String _resolveScriptUri(String scriptName) { | |
584 if (_traceLoading) { | 245 if (_traceLoading) { |
585 _log("_asyncLoadError(${req._uri}), error: $error\nstack: $stack"); | 246 _log("Resolving script: $scriptName"); |
586 } | 247 } |
587 if (req._tag == _Dart_kResourceLoad) { | 248 if (_workingDirectory == null) { |
588 Completer c = req._context; | 249 throw 'No current working directory set.'; |
589 c.completeError(error, stack); | |
590 } else { | |
591 String libraryUri = req._context; | |
592 if (req._tag == _Dart_kImportTag) { | |
593 // When importing a library, the libraryUri is the imported | |
594 // uri. | |
595 libraryUri = req._uri; | |
596 } | |
597 _asyncLoadErrorCallback(req._uri, libraryUri, error); | |
598 } | 250 } |
599 _finishLoadRequest(req); | 251 scriptName = _sanitizeWindowsPath(scriptName); |
252 | |
253 var scriptUri = Uri.parse(scriptName); | |
254 if (scriptUri.scheme == '') { | |
255 // Script does not have a scheme, assume that it is a path, | |
256 // resolve it against the working directory. | |
257 scriptUri = _workingDirectory.resolveUri(scriptUri); | |
258 } | |
259 | |
260 // Remember the root script URI so that we can resolve packages based on | |
261 // this location. | |
262 _rootScript = scriptUri; | |
263 | |
264 if (_traceLoading) { | |
265 _log('Resolved entry point to: $_rootScript'); | |
266 } | |
267 return scriptUri.toString(); | |
600 } | 268 } |
601 | 269 |
602 | 270 |
603 _loadDataFromLoadPort(int tag, String uri, Uri resourceUri, context) { | |
604 try { | |
605 _startLoadRequest(tag, uri, resourceUri, context); | |
606 } catch (e, s) { | |
607 if (_traceLoading) { | |
608 _log("Exception when communicating with service isolate: $e"); | |
609 } | |
610 // Register a dummy load request so we can fail to load it. | |
611 var req = new _LoadRequest(tag, uri, resourceUri, context); | |
612 | |
613 // Wrap inside a _LoadError unless we are already propagating a previously | |
614 // seen _LoadError. | |
615 var error = (e is _LoadError) ? e : new _LoadError(req, e.toString()); | |
616 _asyncLoadError(req, error, s); | |
617 } | |
618 } | |
619 | |
620 | |
621 // Loading a package URI needs to first map the package name to a loadable | |
622 // URI. | |
623 _loadPackage(int tag, String uri, Uri resourceUri, context) { | |
624 if (_packagesReady) { | |
625 var resolvedUri; | |
626 try { | |
627 resolvedUri = _resolvePackageUri(resourceUri); | |
628 } catch (e, s) { | |
629 if (_traceLoading) { | |
630 _log("Exception ($e) when resolving package URI: $resourceUri"); | |
631 } | |
632 // Register a dummy load request so we can fail to load it. | |
633 var req = new _LoadRequest(tag, uri, resourceUri, context); | |
634 | |
635 // Wrap inside a _LoadError unless we are already propagating a previously | |
636 // seen _LoadError. | |
637 var error = (e is _LoadError) ? e : new _LoadError(req, e.toString()); | |
638 _asyncLoadError(req, error, s); | |
639 } | |
640 _loadData(tag, uri, resolvedUri, context); | |
641 } else { | |
642 if (_pendingPackageLoads.isEmpty) { | |
643 // Package resolution has not been setup yet, and this is the first | |
644 // request for package resolution & loading. | |
645 _requestPackagesMap(); | |
646 } | |
647 // Register the action of loading this package once the package resolution | |
648 // is ready. | |
649 _pendingPackageLoads.add(() { | |
650 if (_traceLoading) { | |
651 _log("Handling deferred package request: " | |
652 "$tag, $uri, $resourceUri, $context"); | |
653 } | |
654 _loadPackage(tag, uri, resourceUri, context); | |
655 }); | |
656 if (_traceLoading) { | |
657 _log("Pending package load of '$uri': " | |
658 "${_pendingPackageLoads.length} pending"); | |
659 } | |
660 } | |
661 } | |
662 | |
663 | |
664 // Load the data associated with the resourceUri. | |
665 _loadData(int tag, String uri, Uri resourceUri, context) { | |
666 if (resourceUri.scheme == 'package') { | |
667 // package based uris need to be resolved to the correct loadable location. | |
668 // The logic of which is handled seperately, and then _loadData is called | |
669 // recursively. | |
670 _loadPackage(tag, uri, resourceUri, context); | |
671 } else { | |
672 _loadDataFromLoadPort(tag, uri, resourceUri, context); | |
673 } | |
674 } | |
675 | |
676 | |
677 // Embedder Entrypoint: | |
678 // Asynchronously loads script data through a http[s] or file uri. | |
679 _loadDataAsync(int tag, String uri, String libraryUri) { | |
680 if (!_setupCompleted) { | |
681 _setupHooks(); | |
682 } | |
683 var resourceUri; | |
684 if (tag == _Dart_kScriptTag) { | |
685 resourceUri = _resolveScriptUri(uri); | |
686 uri = resourceUri.toString(); | |
687 } else { | |
688 resourceUri = Uri.parse(uri); | |
689 } | |
690 _loadData(tag, uri, resourceUri, libraryUri); | |
691 } | |
692 | |
693 | |
694 // Embedder Entrypoint: | 271 // Embedder Entrypoint: |
695 // Function called by standalone embedder to resolve uris when the VM requests | 272 // Function called by standalone embedder to resolve uris when the VM requests |
696 // Dart_kCanonicalizeUrl from the tag handler. | 273 // Dart_kCanonicalizeUrl from the tag handler. |
697 String _resolveUri(String base, String userString) { | 274 String _resolveUri(String base, String userString) { |
698 if (!_setupCompleted) { | 275 if (!_setupCompleted) { |
699 _setupHooks(); | 276 _setupHooks(); |
700 } | 277 } |
701 if (_traceLoading) { | 278 if (_traceLoading) { |
702 _log('Resolving: $userString from $base'); | 279 _log('Resolving: $userString from $base'); |
703 } | 280 } |
704 var baseUri = Uri.parse(base); | 281 var baseUri = Uri.parse(base); |
705 var result = baseUri.resolve(userString).toString(); | 282 var result = baseUri.resolve(userString).toString(); |
706 if (_traceLoading) { | 283 if (_traceLoading) { |
707 _log('Resolved $userString in $base to $result'); | 284 _log('Resolved $userString in $base to $result'); |
708 } | 285 } |
709 return result; | 286 return result; |
710 } | 287 } |
711 | 288 |
712 | 289 |
713 // Handling of access to the package root or package map from user code. | |
714 _triggerPackageResolution(action) { | |
715 if (_packagesReady) { | |
716 // Packages are ready. Execute the action now. | |
717 action(); | |
718 } else { | |
719 if (_pendingPackageLoads.isEmpty) { | |
720 // Package resolution has not been setup yet, and this is the first | |
721 // request for package resolution & loading. | |
722 _requestPackagesMap(); | |
723 } | |
724 // Register the action for when the package resolution is ready. | |
725 _pendingPackageLoads.add(action); | |
726 } | |
727 } | |
728 | |
729 | |
730 Future<Uri> _getPackageRootFuture() { | |
731 if (_traceLoading) { | |
732 _log("Request for package root from user code."); | |
733 } | |
734 var completer = new Completer<Uri>(); | |
735 _triggerPackageResolution(() { | |
736 completer.complete(_packageRoot); | |
737 }); | |
738 return completer.future; | |
739 } | |
740 | |
741 | |
742 Future<Uri> _getPackageConfigFuture() { | |
743 if (_traceLoading) { | |
744 _log("Request for package config from user code."); | |
745 } | |
746 var completer = new Completer<Uri>(); | |
747 _triggerPackageResolution(() { | |
748 completer.complete(_packageConfig); | |
749 }); | |
750 return completer.future; | |
751 } | |
752 | |
753 | |
754 Future<Uri> _resolvePackageUriFuture(Uri packageUri) async { | |
755 if (_traceLoading) { | |
756 _log("Request for package Uri resolution from user code: $packageUri"); | |
757 } | |
758 if (packageUri.scheme != "package") { | |
759 if (_traceLoading) { | |
760 _log("Non-package Uri, returning unmodified: $packageUri"); | |
761 } | |
762 // Return the incoming parameter if not passed a package: URI. | |
763 return packageUri; | |
764 } | |
765 | |
766 if (!_packagesReady) { | |
767 if (_traceLoading) { | |
768 _log("Trigger loading by requesting the package config."); | |
769 } | |
770 // Make sure to trigger package resolution. | |
771 var dummy = await _getPackageConfigFuture(); | |
772 } | |
773 assert(_packagesReady); | |
774 | |
775 var result; | |
776 try { | |
777 result = _resolvePackageUri(packageUri); | |
778 } catch (e, s) { | |
779 // Any error during resolution will resolve this package as not mapped, | |
780 // which is indicated by a null return. | |
781 if (_traceLoading) { | |
782 _log("Exception ($e) when resolving package URI: $packageUri"); | |
783 } | |
784 result = null; | |
785 } | |
786 if (_traceLoading) { | |
787 _log("Resolved '$packageUri' to '$result'"); | |
788 } | |
789 return result; | |
790 } | |
791 | |
792 | |
793 // Handling of Resource class by dispatching to the load port. | |
794 Future<List<int>> _resourceReadAsBytes(Uri uri) { | |
795 var completer = new Completer<List<int>>(); | |
796 // Request the load of the resource associating the completer as the context | |
797 // for the load. | |
798 _loadData(_Dart_kResourceLoad, uri.toString(), uri, completer); | |
799 // Return the future that will be triggered once the resource has been loaded. | |
800 return completer.future; | |
801 } | |
802 | |
803 | |
804 // Embedder Entrypoint (gen_snapshot): | 290 // Embedder Entrypoint (gen_snapshot): |
805 // Resolve relative paths relative to working directory. | 291 // Resolve relative paths relative to working directory. |
806 String _resolveInWorkingDirectory(String fileName) { | 292 String _resolveInWorkingDirectory(String fileName) { |
807 if (!_setupCompleted) { | 293 if (!_setupCompleted) { |
808 _setupHooks(); | 294 _setupHooks(); |
809 } | 295 } |
810 if (_workingDirectory == null) { | 296 if (_workingDirectory == null) { |
811 throw 'No current working directory set.'; | 297 throw 'No current working directory set.'; |
812 } | 298 } |
813 var name = _sanitizeWindowsPath(fileName); | 299 var name = _sanitizeWindowsPath(fileName); |
814 | 300 |
815 var uri = Uri.parse(name); | 301 var uri = Uri.parse(name); |
816 if (uri.scheme != '') { | 302 if (uri.scheme != '') { |
817 throw 'Schemes are not supported when resolving filenames.'; | 303 throw 'Schemes are not supported when resolving filenames.'; |
818 } | 304 } |
819 uri = _workingDirectory.resolveUri(uri); | 305 uri = _workingDirectory.resolveUri(uri); |
820 | 306 |
821 if (_traceLoading) { | 307 if (_traceLoading) { |
822 _log('Resolved in working directory: $fileName -> $uri'); | 308 _log('Resolved in working directory: $fileName -> $uri'); |
823 } | 309 } |
824 return uri.toString(); | 310 return uri.toString(); |
825 } | 311 } |
826 | 312 |
313 // Only used by vm/cc unit tests. | |
314 Uri _resolvePackageUri(Uri uri) { | |
315 assert(_packageRoot != null); | |
316 return _packageRoot.resolve(uri.path); | |
317 } | |
318 | |
827 | 319 |
828 // Returns either a file path or a URI starting with http[s]:, as a String. | 320 // Returns either a file path or a URI starting with http[s]:, as a String. |
829 String _filePathFromUri(String userUri) { | 321 String _filePathFromUri(String userUri) { |
830 var uri = Uri.parse(userUri); | 322 var uri = Uri.parse(userUri); |
831 if (_traceLoading) { | 323 if (_traceLoading) { |
832 _log('Getting file path from: $uri'); | 324 _log('Getting file path from: $uri'); |
833 } | 325 } |
834 | 326 |
835 var path; | 327 var path; |
836 switch (uri.scheme) { | 328 switch (uri.scheme) { |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
872 | 364 |
873 // Register callbacks and hooks with the rest of the core libraries. | 365 // Register callbacks and hooks with the rest of the core libraries. |
874 _setupHooks() { | 366 _setupHooks() { |
875 _setupCompleted = true; | 367 _setupCompleted = true; |
876 VMLibraryHooks.resourceReadAsBytes = _resourceReadAsBytes; | 368 VMLibraryHooks.resourceReadAsBytes = _resourceReadAsBytes; |
877 | 369 |
878 VMLibraryHooks.packageRootUriFuture = _getPackageRootFuture; | 370 VMLibraryHooks.packageRootUriFuture = _getPackageRootFuture; |
879 VMLibraryHooks.packageConfigUriFuture = _getPackageConfigFuture; | 371 VMLibraryHooks.packageConfigUriFuture = _getPackageConfigFuture; |
880 VMLibraryHooks.resolvePackageUriFuture = _resolvePackageUriFuture; | 372 VMLibraryHooks.resolvePackageUriFuture = _resolvePackageUriFuture; |
881 } | 373 } |
374 | |
375 | |
376 // Handling of Resource class by dispatching to the load port. | |
377 Future<List<int>> _resourceReadAsBytes(Uri uri) async { | |
378 List response = await _makeLoaderRequest(_Dart_kResourceLoad, uri.toString()); | |
379 if (response[3] is String) { | |
380 // Throw the error. | |
381 throw response[3]; | |
382 } else { | |
383 return response[3]; | |
384 } | |
385 } | |
386 | |
387 | |
388 Future<Uri> _getPackageRootFuture() { | |
389 if (_traceLoading) { | |
390 _log("Request for package root from user code."); | |
391 } | |
392 return _makeLoaderRequest(_Dart_kGetPackageRootUri, null); | |
393 } | |
394 | |
395 | |
396 Future<Uri> _getPackageConfigFuture() { | |
397 if (_traceLoading) { | |
398 _log("Request for package config from user code."); | |
399 } | |
400 assert(_loadPort != null); | |
401 return _makeLoaderRequest(_Dart_kGetPackageConfigUri, null); | |
402 } | |
403 | |
404 | |
405 Future<Uri> _resolvePackageUriFuture(Uri packageUri) async { | |
406 if (_traceLoading) { | |
407 _log("Request for package Uri resolution from user code: $packageUri"); | |
408 } | |
409 if (packageUri.scheme != "package") { | |
410 if (_traceLoading) { | |
411 _log("Non-package Uri, returning unmodified: $packageUri"); | |
412 } | |
413 // Return the incoming parameter if not passed a package: URI. | |
414 return packageUri; | |
415 } | |
416 var result = await _makeLoaderRequest(_Dart_kResolvePackageUri, | |
417 packageUri.toString()); | |
418 if (result is! Uri) { | |
419 if (_traceLoading) { | |
420 _log("Exception when resolving package URI: $packageUri"); | |
421 } | |
422 result = null; | |
423 } | |
424 if (_traceLoading) { | |
425 _log("Resolved '$packageUri' to '$result'"); | |
426 } | |
427 return result; | |
428 } | |
OLD | NEW |