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:collection'; | 7 import 'dart:collection'; |
8 import 'dart:isolate'; | 8 import 'dart:isolate'; |
9 import 'dart:typed_data'; | 9 import 'dart:typed_data'; |
10 | 10 |
(...skipping 30 matching lines...) Expand all Loading... | |
41 return new Uri.file("$result/"); | 41 return new Uri.file("$result/"); |
42 } | 42 } |
43 | 43 |
44 | 44 |
45 _getUriBaseClosure() => _uriBase; | 45 _getUriBaseClosure() => _uriBase; |
46 | 46 |
47 | 47 |
48 // Asynchronous loading of resources. | 48 // Asynchronous loading of resources. |
49 // The embedder forwards most loading requests to this library. | 49 // The embedder forwards most loading requests to this library. |
50 | 50 |
51 _log(msg) { | |
52 _print("* $msg"); | |
53 } | |
54 | |
51 // See Dart_LibraryTag in dart_api.h | 55 // See Dart_LibraryTag in dart_api.h |
52 const Dart_kScriptTag = null; | 56 const Dart_kScriptTag = null; |
53 const Dart_kImportTag = 0; | 57 const Dart_kImportTag = 0; |
54 const Dart_kSourceTag = 1; | 58 const Dart_kSourceTag = 1; |
55 const Dart_kCanonicalizeUrl = 2; | 59 const Dart_kCanonicalizeUrl = 2; |
56 | 60 |
57 // Embedder sets this to true if the --trace-loading flag was passed on the | 61 // Embedder sets this to true if the --trace-loading flag was passed on the |
58 // command line. | 62 // command line. |
59 bool _traceLoading = false; | 63 bool _traceLoading = false; |
60 | 64 |
(...skipping 10 matching lines...) Expand all Loading... | |
71 // An unordered hash map mapping from request id to a particular load request. | 75 // An unordered hash map mapping from request id to a particular load request. |
72 // Once there are no outstanding load requests the current load has finished. | 76 // Once there are no outstanding load requests the current load has finished. |
73 HashMap _reqMap = new HashMap(); | 77 HashMap _reqMap = new HashMap(); |
74 | 78 |
75 // The current working directory when the embedder was launched. | 79 // The current working directory when the embedder was launched. |
76 Uri _workingDirectory; | 80 Uri _workingDirectory; |
77 // The URI that the root script was loaded from. Remembered so that | 81 // The URI that the root script was loaded from. Remembered so that |
78 // package imports can be resolved relative to it. The root script is the basis | 82 // package imports can be resolved relative to it. The root script is the basis |
79 // for the root library in the VM. | 83 // for the root library in the VM. |
80 Uri _rootScript; | 84 Uri _rootScript; |
85 | |
86 // Packages are either resolved looking up in a map or resolved from within a | |
87 // package root. | |
88 bool _packagesReady() => (_packageRoot != null) || (_packageMap != null); | |
81 // The directory to look in to resolve "package:" scheme URIs. By detault it is | 89 // The directory to look in to resolve "package:" scheme URIs. By detault it is |
82 // the 'packages' directory right next to the script. | 90 // the 'packages' directory right next to the script. |
83 Uri _packageRoot = _rootScript.resolve('packages/'); | 91 Uri _packageRoot = null; // Used to be _rootScript.resolve('packages/'); |
92 // The map describing how certain package names are mapped to Uris. | |
93 Map<String, Uri> _packageMap = null; | |
94 // A list of pending packags which have been requested while resolving the | |
95 // location of the package root or the contents of the package map. | |
96 List<_LoadRequest> _pendingPackageLoads = []; | |
97 | |
98 // If we have outstanding loads or pending package loads waiting for resolution, | |
99 // then we do have pending loads. | |
100 bool _pendingLoads() => !_reqMap.isEmpty || !_pendingPackageLoads.isEmpty; | |
84 | 101 |
85 // Special handling for Windows paths so that they are compatible with URI | 102 // Special handling for Windows paths so that they are compatible with URI |
86 // handling. | 103 // handling. |
87 // Embedder sets this to true if we are running on Windows. | 104 // Embedder sets this to true if we are running on Windows. |
88 bool _isWindows = false; | 105 bool _isWindows = false; |
89 | 106 |
90 | 107 |
siva
2015/07/28 23:50:09
Can you move the _log(msg) function down here, it
Ivan Posva
2015/07/29 18:22:18
Done.
| |
91 // A class wrapping the load error message in an Error object. | 108 // A class wrapping the load error message in an Error object. |
92 class _LoadError extends Error { | 109 class _LoadError extends Error { |
93 final String message; | 110 final String message; |
94 final String uri; | 111 final String uri; |
95 _LoadError(this.uri, this.message); | 112 _LoadError(this.uri, this.message); |
96 | 113 |
97 String toString() => 'Load Error for "$uri": $message'; | 114 String toString() => 'Load Error for "$uri": $message'; |
98 } | 115 } |
99 | 116 |
100 // Class collecting all of the information about a particular load request. | 117 // Class collecting all of the information about a particular load request. |
101 class _LoadRequest { | 118 class _LoadRequest { |
102 final int _id; | 119 final int _id; |
103 final int _tag; | 120 final int _tag; |
104 final String _uri; | 121 final String _uri; |
105 final String _libraryUri; | 122 final String _libraryUri; |
123 final Uri _resourceUri; | |
106 | 124 |
107 _LoadRequest(this._id, this._tag, this._uri, this._libraryUri); | 125 _LoadRequest(this._id, |
126 this._tag, | |
127 this._uri, | |
128 this._libraryUri, | |
129 this._resourceUri); | |
130 | |
131 toString() => "LoadRequest($_id, $_tag, $_uri, $_libraryUri, $_resourceUri)"; | |
108 } | 132 } |
109 | 133 |
110 | 134 |
111 // Native calls provided by the embedder. | 135 // Native calls provided by the embedder. |
112 void _signalDoneLoading() native "Builtin_DoneLoading"; | 136 void _signalDoneLoading() native "Builtin_DoneLoading"; |
113 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) | 137 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) |
114 native "Builtin_LoadSource"; | 138 native "Builtin_LoadSource"; |
115 void _asyncLoadErrorCallback(uri, libraryUri, error) | 139 void _asyncLoadErrorCallback(uri, libraryUri, error) |
116 native "Builtin_AsyncLoadError"; | 140 native "Builtin_AsyncLoadError"; |
117 | 141 |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
165 return '$uri/'; | 189 return '$uri/'; |
166 } | 190 } |
167 return uri; | 191 return uri; |
168 } | 192 } |
169 | 193 |
170 | 194 |
171 // Embedder Entrypoint: | 195 // Embedder Entrypoint: |
172 // The embedder calls this method with the current working directory. | 196 // The embedder calls this method with the current working directory. |
173 void _setWorkingDirectory(cwd) { | 197 void _setWorkingDirectory(cwd) { |
174 if (_traceLoading) { | 198 if (_traceLoading) { |
175 _print('# Setting working directory: $cwd'); | 199 _log('Setting working directory: $cwd'); |
176 } | 200 } |
177 _workingDirectory = new Uri.directory(cwd); | 201 _workingDirectory = new Uri.directory(cwd); |
178 if (_traceLoading) { | 202 if (_traceLoading) { |
179 _print('# Working directory URI: $_workingDirectory'); | 203 _log('Working directory URI: $_workingDirectory'); |
180 } | 204 } |
181 } | 205 } |
182 | 206 |
183 | 207 |
184 // Embedder Entrypoint: | 208 // Embedder Entrypoint: |
185 // The embedder calls this method with a custom package root. | 209 // The embedder calls this method with a custom package root. |
186 _setPackageRoot(String packageRoot) { | 210 _setPackageRoot(String packageRoot) { |
187 if (_traceLoading) { | 211 if (_traceLoading) { |
188 _print('# Setting package root: $packageRoot'); | 212 _log('Setting package root: $packageRoot'); |
189 } | 213 } |
190 packageRoot = _enforceTrailingSlash(packageRoot); | 214 packageRoot = _enforceTrailingSlash(packageRoot); |
191 if (packageRoot.startsWith('file:') || | 215 if (packageRoot.startsWith('file:') || |
192 packageRoot.startsWith('http:') || | 216 packageRoot.startsWith('http:') || |
193 packageRoot.startsWith('https:')) { | 217 packageRoot.startsWith('https:')) { |
194 _packageRoot = _workingDirectory.resolve(packageRoot); | 218 _packageRoot = _workingDirectory.resolve(packageRoot); |
195 } else { | 219 } else { |
196 packageRoot = _sanitizeWindowsPath(packageRoot); | 220 packageRoot = _sanitizeWindowsPath(packageRoot); |
197 packageRoot = _trimWindowsPath(packageRoot); | 221 packageRoot = _trimWindowsPath(packageRoot); |
198 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); | 222 _packageRoot = _workingDirectory.resolveUri(new Uri.file(packageRoot)); |
199 } | 223 } |
200 if (_traceLoading) { | 224 if (_traceLoading) { |
201 _print('# Package root URI: $_packageRoot'); | 225 _log('Package root URI: $_packageRoot'); |
202 } | 226 } |
203 } | 227 } |
204 | 228 |
205 | 229 |
206 // Given a uri with a 'package' scheme, return a Uri that is prefixed with | 230 // Given a uri with a 'package' scheme, return a Uri that is prefixed with |
207 // the package root. | 231 // the package root. |
208 Uri _resolvePackageUri(Uri uri) { | 232 Uri _resolvePackageUri(Uri uri) { |
209 if (!uri.host.isEmpty) { | 233 if (!uri.host.isEmpty) { |
210 var path = '${uri.host}${uri.path}'; | 234 var path = '${uri.host}${uri.path}'; |
211 var right = 'package:$path'; | 235 var right = 'package:$path'; |
212 var wrong = 'package://$path'; | 236 var wrong = 'package://$path'; |
213 | 237 |
214 throw "URIs using the 'package:' scheme should look like " | 238 throw "URIs using the 'package:' scheme should look like " |
215 "'$right', not '$wrong'."; | 239 "'$right', not '$wrong'."; |
216 } | 240 } |
217 | 241 |
218 if (_traceLoading) { | 242 if (_traceLoading) { |
219 _print('# Package root: $_packageRoot'); | 243 _log('Resolving package with uri path: ${uri.path}'); |
220 _print('# uri path: ${uri.path}'); | |
221 } | 244 } |
222 return _packageRoot.resolve(uri.path); | 245 var resolvedUri; |
246 if (_packageRoot != null) { | |
247 resolvedUri = _packageRoot.resolve(uri.path); | |
248 } else { | |
249 var packageName = uri.pathSegments[0]; | |
250 var mapping = _packageMap[packageName]; | |
251 if (_traceLoading) { | |
252 _log("Mapped '$packageName' package to '$mapping'"); | |
253 } | |
254 if (mapping == null) { | |
255 throw "No mapping for '$packageName' package when resolving '$uri'."; | |
256 } | |
257 var path = uri.path.substring(packageName.length + 1); | |
258 resolvedUri = mapping.resolve(path); | |
259 } | |
260 if (_traceLoading) { | |
261 _log("Resolved '$uri' to '$resolvedUri'."); | |
262 } | |
263 return resolvedUri; | |
223 } | 264 } |
224 | 265 |
225 | 266 |
226 // Resolves the script uri in the current working directory iff the given uri | 267 // Resolves the script uri in the current working directory iff the given uri |
227 // did not specify a scheme (e.g. a path to a script file on the command line). | 268 // did not specify a scheme (e.g. a path to a script file on the command line). |
228 Uri _resolveScriptUri(String scriptName) { | 269 Uri _resolveScriptUri(String scriptName) { |
270 if (_traceLoading) { | |
271 _log("Resolving script: $scriptName"); | |
272 } | |
229 if (_workingDirectory == null) { | 273 if (_workingDirectory == null) { |
230 throw 'No current working directory set.'; | 274 throw 'No current working directory set.'; |
231 } | 275 } |
232 scriptName = _sanitizeWindowsPath(scriptName); | 276 scriptName = _sanitizeWindowsPath(scriptName); |
233 | 277 |
234 var scriptUri = Uri.parse(scriptName); | 278 var scriptUri = Uri.parse(scriptName); |
235 if (scriptUri.scheme == '') { | 279 if (scriptUri.scheme == '') { |
236 // Script does not have a scheme, assume that it is a path, | 280 // Script does not have a scheme, assume that it is a path, |
237 // resolve it against the working directory. | 281 // resolve it against the working directory. |
238 scriptUri = _workingDirectory.resolveUri(scriptUri); | 282 scriptUri = _workingDirectory.resolveUri(scriptUri); |
239 } | 283 } |
240 | 284 |
241 // Remember the root script URI so that we can resolve packages based on | 285 // Remember the root script URI so that we can resolve packages based on |
242 // this location. | 286 // this location. |
243 _rootScript = scriptUri; | 287 _rootScript = scriptUri; |
244 | 288 |
245 if (_traceLoading) { | 289 if (_traceLoading) { |
246 _print('# Resolved entry point to: $_rootScript'); | 290 _log('Resolved entry point to: $_rootScript'); |
247 } | 291 } |
248 return scriptUri; | 292 return scriptUri; |
249 } | 293 } |
250 | 294 |
251 | 295 |
252 void _finishLoadRequest(_LoadRequest req) { | 296 void _finishLoadRequest(_LoadRequest req) { |
253 // Now that we are done with loading remove the request from the map. | 297 // Now that we are done with loading remove the request from the map. |
254 var tmp = _reqMap.remove(req._id); | 298 var tmp = _reqMap.remove(req._id); |
255 assert(tmp == req); | 299 assert(tmp == req); |
256 if (_traceLoading) { | 300 if (_traceLoading) { |
257 _print("Loading of ${req._uri} finished, " | 301 _log("Loading of ${req._uri} finished: " |
258 "${_reqMap.length} requests remaining"); | 302 "${_reqMap.length} requests remaining, " |
303 "${_pendingPackageLoads.length} packages pending."); | |
259 } | 304 } |
260 | 305 |
261 if (_reqMap.isEmpty) { | 306 if (!_pendingLoads()) { |
262 if (_traceLoading) { | 307 if (_traceLoading) { |
263 _print("Closing loading port."); | 308 _log("Closing loading port."); |
264 } | 309 } |
265 _receivePort.close(); | 310 _receivePort.close(); |
266 _receivePort = null; | 311 _receivePort = null; |
267 _sendPort = null; | 312 _sendPort = null; |
268 _reqId = 0; | 313 _reqId = 0; |
269 _signalDoneLoading(); | 314 _signalDoneLoading(); |
270 } | 315 } |
271 } | 316 } |
272 | 317 |
273 | 318 |
(...skipping 17 matching lines...) Expand all Loading... | |
291 assert(req != null); | 336 assert(req != null); |
292 _asyncLoadError(req, error); | 337 _asyncLoadError(req, error); |
293 } | 338 } |
294 } | 339 } |
295 | 340 |
296 | 341 |
297 void _startLoadRequest(int tag, | 342 void _startLoadRequest(int tag, |
298 String uri, | 343 String uri, |
299 String libraryUri, | 344 String libraryUri, |
300 Uri resourceUri) { | 345 Uri resourceUri) { |
301 if (_reqMap.isEmpty) { | 346 if (_receivePort == null) { |
302 if (_traceLoading) { | 347 if (_traceLoading) { |
303 _print("Initializing load port."); | 348 _log("Initializing load port."); |
304 } | 349 } |
305 assert(_receivePort == null); | 350 assert(_receivePort == null); |
306 assert(_sendPort == null); | 351 assert(_sendPort == null); |
307 _receivePort = new RawReceivePort(_handleLoaderReply); | 352 _receivePort = new RawReceivePort(_handleLoaderReply); |
308 _sendPort = _receivePort.sendPort; | 353 _sendPort = _receivePort.sendPort; |
309 } | 354 } |
310 // Register the load request and send it to the VM service isolate. | 355 // Register the load request and send it to the VM service isolate. |
311 var curId = _reqId++; | 356 var curId = _reqId++; |
312 | 357 |
313 assert(_reqMap[curId] == null); | 358 assert(_reqMap[curId] == null); |
314 _reqMap[curId] = new _LoadRequest(curId, tag, uri, libraryUri); | 359 _reqMap[curId] = new _LoadRequest(curId, tag, uri, libraryUri, resourceUri); |
315 | 360 |
316 var msg = new List(3); | 361 assert(_receivePort != null); |
362 assert(_sendPort != null); | |
363 | |
364 var msg = new List(4); | |
317 msg[0] = _sendPort; | 365 msg[0] = _sendPort; |
318 msg[1] = curId; | 366 msg[1] = _traceLoading; |
319 msg[2] = resourceUri.toString(); | 367 msg[2] = curId; |
368 msg[3] = resourceUri.toString(); | |
320 _loadPort.send(msg); | 369 _loadPort.send(msg); |
321 | 370 |
322 if (_traceLoading) { | 371 if (_traceLoading) { |
323 _print("Loading of $resourceUri for $uri started with id: $curId, " | 372 _log("Loading of $resourceUri for $uri started with id: $curId, " |
324 "${_reqMap.length} requests outstanding"); | 373 "${_reqMap.length} requests outstanding"); |
325 } | 374 } |
326 } | 375 } |
327 | 376 |
377 | |
378 RawReceivePort _packagesPort; | |
379 | |
380 void _handlePackagesReply(msg) { | |
381 // Make sure to close the _packagePort before any other action. | |
382 _packagesPort.close(); | |
383 | |
384 if (_traceLoading) { | |
385 _log("Got packages reply: $msg"); | |
386 } | |
387 if (msg is String) { | |
388 if (_traceLoading) { | |
389 _log("Got failure response on package port: '$msg'"); | |
390 } | |
391 throw msg; | |
392 } | |
393 if (msg.length == 1) { | |
394 if (_traceLoading) { | |
395 _log("Received package root: '${msg[0]}'"); | |
396 } | |
397 _packageRoot = Uri.parse(msg[0]); | |
398 } else { | |
399 assert((msg.length % 2) == 0); | |
400 _packageMap = new Map<String, Uri>(); | |
401 for (var i = 0; i < msg.length; i+=2) { | |
402 _packageMap[msg[i]] = Uri.parse(msg[i+1]); | |
403 } | |
404 if (_traceLoading) { | |
405 _log("Setup package map: $_packageMap"); | |
406 } | |
407 } | |
408 | |
409 // Resolve all pending package loads now that we know how to resolve them. | |
410 for (var i = 0; i < _pendingPackageLoads.length; i++) { | |
411 var req = _pendingPackageLoads[i]; | |
412 if (req != null) { | |
413 if (_traceLoading) { | |
414 _log("Handling deferred load request: $req"); | |
415 } | |
416 _loadPackage(req._tag, req._uri, req._libraryUri, req._resourceUri); | |
417 } | |
418 } | |
419 // Reset the pending package loads to empty. So that we eventually can | |
420 // finish loading. | |
421 _pendingPackageLoads = []; | |
422 } | |
423 | |
424 | |
425 void _requestPackagesMap() { | |
426 assert(_packagesPort == null); | |
427 assert(_rootScript != null); | |
428 // Create a port to receive the packages map on. | |
429 _packagesPort = new RawReceivePort(_handlePackagesReply); | |
430 var sp = _packagesPort.sendPort; | |
431 | |
432 var msg = new List(4); | |
433 msg[0] = sp; | |
434 msg[1] = _traceLoading; | |
435 msg[2] = -1; | |
436 msg[3] = _rootScript.toString(); | |
437 _loadPort.send(msg); | |
438 | |
439 if (_traceLoading) { | |
440 _log("Requested packages map for '$_rootScript'."); | |
441 } | |
442 } | |
443 | |
444 | |
445 // Embedder Entrypoint: | |
446 // Request the load of a particular packages map. | |
447 void _loadPackagesMap(String packagesParam) { | |
448 // First convert the packages parameter from the command line to a URI which | |
449 // can be handled by the loader code. | |
450 // TODO(iposva): Consider refactoring the common code below which is almost | |
451 // shared with resolution of the root script. | |
452 if (_traceLoading) { | |
453 _log("Resolving packages map: $packagesParam"); | |
454 } | |
455 if (_workingDirectory == null) { | |
456 throw 'No current working directory set.'; | |
457 } | |
458 var packagesName = _sanitizeWindowsPath(packagesParam); | |
459 var packagesUri = Uri.parse(packagesName); | |
460 if (packagesUri.scheme == '') { | |
461 // Script does not have a scheme, assume that it is a path, | |
462 // resolve it against the working directory. | |
463 packagesUri = _workingDirectory.resolveUri(packagesUri); | |
464 } | |
465 if (_traceLoading) { | |
466 _log('Resolved packages map to: $packagesUri'); | |
467 } | |
468 | |
469 // Request the loading and parsing of the packages map at the specified URI. | |
470 // Create a port to receive the packages map on. | |
471 assert(_packagesPort == null); | |
472 _packagesPort = new RawReceivePort(_handlePackagesReply); | |
473 var sp = _packagesPort.sendPort; | |
474 | |
475 var msg = new List(4); | |
476 msg[0] = sp; | |
477 msg[1] = _traceLoading; | |
478 msg[2] = -2; | |
479 msg[3] = packagesUri.toString(); | |
480 _loadPort.send(msg); | |
481 | |
482 // Signal that the resolution of the packages map has started. But in this | |
483 // case it is not tied to a particular request. | |
484 _pendingPackageLoads.add(null); | |
485 | |
486 if (_traceLoading) { | |
487 _log("Requested packages map at '$packagesUri'."); | |
488 } | |
489 } | |
490 | |
328 | 491 |
329 void _loadScript(_LoadRequest req, Uint8List data) { | 492 void _loadScript(_LoadRequest req, Uint8List data) { |
330 // TODO: Currently a compilation error while loading the script is | 493 // TODO: Currently a compilation error while loading the script is |
331 // fatal for the isolate. _loadScriptCallback() does not return and | 494 // fatal for the isolate. _loadScriptCallback() does not return and |
332 // the number of requests remains out of sync. | 495 // the number of requests remains out of sync. |
333 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data); | 496 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data); |
334 _finishLoadRequest(req); | 497 _finishLoadRequest(req); |
335 } | 498 } |
336 | 499 |
337 | 500 |
338 void _asyncLoadError(_LoadRequest req, _LoadError error) { | 501 void _asyncLoadError(_LoadRequest req, _LoadError error) { |
339 if (_traceLoading) { | 502 if (_traceLoading) { |
340 _print("_asyncLoadError(${req._uri}), error: $error"); | 503 _log("_asyncLoadError(${req._uri}), error: $error"); |
341 } | 504 } |
342 var libraryUri = req._libraryUri; | 505 var libraryUri = req._libraryUri; |
343 if (req._tag == Dart_kImportTag) { | 506 if (req._tag == Dart_kImportTag) { |
344 // When importing a library, the libraryUri is the imported | 507 // When importing a library, the libraryUri is the imported |
345 // uri. | 508 // uri. |
346 libraryUri = req._uri; | 509 libraryUri = req._uri; |
347 } | 510 } |
348 _asyncLoadErrorCallback(req._uri, libraryUri, error); | 511 _asyncLoadErrorCallback(req._uri, libraryUri, error); |
349 _finishLoadRequest(req); | 512 _finishLoadRequest(req); |
350 } | 513 } |
351 | 514 |
352 | 515 |
353 _loadDataFromLoadPort(int tag, | 516 _loadDataFromLoadPort(int tag, |
354 String uri, | 517 String uri, |
355 String libraryUri, | 518 String libraryUri, |
356 Uri resourceUri) { | 519 Uri resourceUri) { |
357 try { | 520 try { |
358 _startLoadRequest(tag, uri, libraryUri, resourceUri); | 521 _startLoadRequest(tag, uri, libraryUri, resourceUri); |
359 } catch (e) { | 522 } catch (e) { |
360 if (_traceLoading) { | 523 if (_traceLoading) { |
361 _print("Exception when communicating with service isolate: $e"); | 524 _log("Exception when communicating with service isolate: $e"); |
362 } | 525 } |
363 // Wrap inside a _LoadError unless we are already propagating a previously | 526 // Wrap inside a _LoadError unless we are already propagating a previously |
364 // seen _LoadError. | 527 // seen _LoadError. |
365 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); | 528 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); |
366 _asyncLoadError(tag, uri, libraryUri, error); | 529 _asyncLoadError(tag, uri, libraryUri, error); |
367 } | 530 } |
368 } | 531 } |
369 | 532 |
370 | 533 |
534 // Loading a package URI needs to first map the package name to a loadable | |
535 // URI. | |
536 _loadPackage(int tag, String uri, String libraryUri, Uri resourceUri) { | |
537 if (_packagesReady()) { | |
538 _loadData(tag, uri, libraryUri, _resolvePackageUri(resourceUri)); | |
539 } else { | |
540 if (_pendingPackageLoads.isEmpty) { | |
541 // Package resolution has not been setup yet, and this is the first | |
542 // request for package resolution & loading. | |
543 _requestPackagesMap(); | |
544 } | |
545 var req = new _LoadRequest(-1, tag, uri, libraryUri, resourceUri); | |
546 _pendingPackageLoads.add(req); | |
547 if (_traceLoading) { | |
548 _log("Pending package load of '$uri': " | |
549 "${_pendingPackageLoads.length} pending"); | |
550 } | |
551 } | |
552 } | |
553 | |
554 | |
555 // Load the data associated with the resourceUri. | |
556 _loadData(int tag, String uri, String libraryUri, Uri resourceUri) { | |
557 if (resourceUri.scheme == 'package') { | |
558 // package based uris need to be resolved to the correct loadable location. | |
559 // The logic of which is handled seperately, and then _loadData is called | |
560 // recursively. | |
561 _loadPackage(tag, uri, libraryUri, resourceUri); | |
562 } else { | |
563 _loadDataFromLoadPort(tag, uri, libraryUri, resourceUri); | |
564 } | |
565 } | |
566 | |
567 | |
371 // Embedder Entrypoint: | 568 // Embedder Entrypoint: |
372 // Asynchronously loads script data through a http[s] or file uri. | 569 // Asynchronously loads script data through a http[s] or file uri. |
373 _loadDataAsync(int tag, String uri, String libraryUri) { | 570 _loadDataAsync(int tag, String uri, String libraryUri) { |
374 var resourceUri; | 571 var resourceUri; |
375 if (tag == Dart_kScriptTag) { | 572 if (tag == Dart_kScriptTag) { |
376 resourceUri = _resolveScriptUri(uri); | 573 resourceUri = _resolveScriptUri(uri); |
377 uri = resourceUri.toString(); | 574 uri = resourceUri.toString(); |
378 } else { | 575 } else { |
379 resourceUri = Uri.parse(uri); | 576 resourceUri = Uri.parse(uri); |
380 } | 577 } |
381 | 578 _loadData(tag, uri, libraryUri, resourceUri); |
382 // package based uris need to be resolved to the correct loadable location. | |
383 if (resourceUri.scheme == 'package') { | |
384 resourceUri = _resolvePackageUri(resourceUri); | |
385 } | |
386 | |
387 _loadDataFromLoadPort(tag, uri, libraryUri, resourceUri); | |
388 } | 579 } |
389 | 580 |
390 | 581 |
391 // Embedder Entrypoint: | 582 // Embedder Entrypoint: |
392 // Function called by standalone embedder to resolve uris when the VM requests | 583 // Function called by standalone embedder to resolve uris when the VM requests |
393 // Dart_kCanonicalizeUrl from the tag handler. | 584 // Dart_kCanonicalizeUrl from the tag handler. |
394 String _resolveUri(String base, String userString) { | 585 String _resolveUri(String base, String userString) { |
395 if (_traceLoading) { | 586 if (_traceLoading) { |
396 _print('# Resolving: $userString from $base'); | 587 _log('Resolving: $userString from $base'); |
397 } | 588 } |
398 var baseUri = Uri.parse(base); | 589 var baseUri = Uri.parse(base); |
399 var result; | 590 var result; |
400 if (userString.startsWith(_DART_EXT)) { | 591 if (userString.startsWith(_DART_EXT)) { |
401 var uri = userString.substring(_DART_EXT.length); | 592 var uri = userString.substring(_DART_EXT.length); |
402 result = '$_DART_EXT${baseUri.resolve(uri)}'; | 593 result = '$_DART_EXT${baseUri.resolve(uri)}'; |
403 } else { | 594 } else { |
404 result = baseUri.resolve(userString).toString(); | 595 result = baseUri.resolve(userString).toString(); |
405 } | 596 } |
406 if (_traceLoading) { | 597 if (_traceLoading) { |
407 _print('Resolved $userString in $base to $result'); | 598 _log('Resolved $userString in $base to $result'); |
408 } | 599 } |
409 return result; | 600 return result; |
410 } | 601 } |
411 | 602 |
412 | 603 |
413 // Embedder Entrypoint (gen_snapshot): | 604 // Embedder Entrypoint (gen_snapshot): |
414 // Resolve relative paths relative to working directory. | 605 // Resolve relative paths relative to working directory. |
415 String _resolveInWorkingDirectory(String fileName) { | 606 String _resolveInWorkingDirectory(String fileName) { |
416 if (_workingDirectory == null) { | 607 if (_workingDirectory == null) { |
417 throw 'No current working directory set.'; | 608 throw 'No current working directory set.'; |
418 } | 609 } |
419 var name = _sanitizeWindowsPath(fileName); | 610 var name = _sanitizeWindowsPath(fileName); |
420 | 611 |
421 var uri = Uri.parse(name); | 612 var uri = Uri.parse(name); |
422 if (uri.scheme != '') { | 613 if (uri.scheme != '') { |
423 throw 'Schemes are not supported when resolving filenames.'; | 614 throw 'Schemes are not supported when resolving filenames.'; |
424 } | 615 } |
425 uri = _workingDirectory.resolveUri(uri); | 616 uri = _workingDirectory.resolveUri(uri); |
426 | 617 |
427 if (_traceLoading) { | 618 if (_traceLoading) { |
428 _print('# Resolved in working directory: $fileName -> $uri'); | 619 _log('Resolved in working directory: $fileName -> $uri'); |
429 } | 620 } |
430 return uri.toString(); | 621 return uri.toString(); |
431 } | 622 } |
432 | 623 |
433 | 624 |
434 // Handling of dart-ext loading. | 625 // Handling of dart-ext loading. |
435 // Dart native extension scheme. | 626 // Dart native extension scheme. |
436 const _DART_EXT = 'dart-ext:'; | 627 const _DART_EXT = 'dart-ext:'; |
437 | 628 |
438 String _nativeLibraryExtension() native "Builtin_NativeLibraryExtension"; | 629 String _nativeLibraryExtension() native "Builtin_NativeLibraryExtension"; |
439 | 630 |
440 | 631 |
441 String _platformExtensionFileName(String name) { | 632 String _platformExtensionFileName(String name) { |
442 var extension = _nativeLibraryExtension(); | 633 var extension = _nativeLibraryExtension(); |
443 | 634 |
444 if (_isWindows) { | 635 if (_isWindows) { |
445 return '$name.$extension'; | 636 return '$name.$extension'; |
446 } else { | 637 } else { |
447 return 'lib$name.$extension'; | 638 return 'lib$name.$extension'; |
448 } | 639 } |
449 } | 640 } |
450 | 641 |
451 | 642 |
452 // Returns either a file path or a URI starting with http[s]:, as a String. | 643 // Returns either a file path or a URI starting with http[s]:, as a String. |
453 String _filePathFromUri(String userUri) { | 644 String _filePathFromUri(String userUri) { |
454 var uri = Uri.parse(userUri); | 645 var uri = Uri.parse(userUri); |
455 if (_traceLoading) { | 646 if (_traceLoading) { |
456 _print('# Getting file path from: $uri'); | 647 _log('Getting file path from: $uri'); |
457 } | 648 } |
458 | 649 |
459 var path; | 650 var path; |
460 switch (uri.scheme) { | 651 switch (uri.scheme) { |
461 case '': | 652 case '': |
462 case 'file': | 653 case 'file': |
463 return uri.toFilePath(); | 654 return uri.toFilePath(); |
464 case 'package': | 655 case 'package': |
465 return _filePathFromUri(_resolvePackageUri(uri).toString()); | 656 return _filePathFromUri(_resolvePackageUri(uri).toString()); |
466 case 'data': | 657 case 'data': |
467 case 'http': | 658 case 'http': |
468 case 'https': | 659 case 'https': |
469 return uri.toString(); | 660 return uri.toString(); |
470 default: | 661 default: |
471 // Only handling file, http, and package URIs | 662 // Only handling file, http, and package URIs |
472 // in standalone binary. | 663 // in standalone binary. |
473 if (_traceLoading) { | 664 if (_traceLoading) { |
474 _print('# Unknown scheme (${uri.scheme}) in $uri.'); | 665 _log('Unknown scheme (${uri.scheme}) in $uri.'); |
475 } | 666 } |
476 throw 'Not a known scheme: $uri'; | 667 throw 'Not a known scheme: $uri'; |
477 } | 668 } |
478 } | 669 } |
479 | 670 |
480 | 671 |
481 // Embedder Entrypoint: | 672 // Embedder Entrypoint: |
482 // When loading an extension the embedder calls this method to get the | 673 // When loading an extension the embedder calls this method to get the |
483 // different components. | 674 // different components. |
484 // Returns the directory part, the filename part, and the name | 675 // Returns the directory part, the filename part, and the name |
(...skipping 22 matching lines...) Expand all Loading... | |
507 } else { | 698 } else { |
508 name = userUri.substring(index + 1); | 699 name = userUri.substring(index + 1); |
509 path = userUri.substring(0, index + 1); | 700 path = userUri.substring(0, index + 1); |
510 } | 701 } |
511 | 702 |
512 path = _filePathFromUri(path); | 703 path = _filePathFromUri(path); |
513 var filename = _platformExtensionFileName(name); | 704 var filename = _platformExtensionFileName(name); |
514 | 705 |
515 return [path, filename, name]; | 706 return [path, filename, name]; |
516 } | 707 } |
OLD | NEW |