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 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 // An unordered hash map mapping from request id to a particular load request. | 71 // 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. | 72 // Once there are no outstanding load requests the current load has finished. |
73 HashMap _reqMap = new HashMap(); | 73 HashMap _reqMap = new HashMap(); |
74 | 74 |
75 // The current working directory when the embedder was launched. | 75 // The current working directory when the embedder was launched. |
76 Uri _workingDirectory; | 76 Uri _workingDirectory; |
77 // The URI that the root script was loaded from. Remembered so that | 77 // 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 | 78 // package imports can be resolved relative to it. The root script is the basis |
79 // for the root library in the VM. | 79 // for the root library in the VM. |
80 Uri _rootScript; | 80 Uri _rootScript; |
| 81 |
| 82 // Packages are either resolved looking up in a map or resolved from within a |
| 83 // package root. |
| 84 bool _packagesReady() => (_packageRoot != null) || (_packageMap != null); |
81 // The directory to look in to resolve "package:" scheme URIs. By detault it is | 85 // The directory to look in to resolve "package:" scheme URIs. By detault it is |
82 // the 'packages' directory right next to the script. | 86 // the 'packages' directory right next to the script. |
83 Uri _packageRoot = _rootScript.resolve('packages/'); | 87 Uri _packageRoot = null; // Used to be _rootScript.resolve('packages/'); |
| 88 // The map describing how certain package names are mapped to Uris. |
| 89 Map<String, Uri> _packageMap = null; |
| 90 // A list of pending packags which have been requested while resolving the |
| 91 // location of the package root or the contents of the package map. |
| 92 List<_LoadRequest> _pendingPackageLoads = []; |
| 93 |
| 94 // If we have outstanding loads or pending package loads waiting for resolution, |
| 95 // then we do have pending loads. |
| 96 bool _pendingLoads() => !_reqMap.isEmpty || !_pendingPackageLoads.isEmpty; |
84 | 97 |
85 // Special handling for Windows paths so that they are compatible with URI | 98 // Special handling for Windows paths so that they are compatible with URI |
86 // handling. | 99 // handling. |
87 // Embedder sets this to true if we are running on Windows. | 100 // Embedder sets this to true if we are running on Windows. |
88 bool _isWindows = false; | 101 bool _isWindows = false; |
89 | 102 |
| 103 // Logging from builtin.dart is prefixed with a '*'. |
| 104 _log(msg) { |
| 105 _print("* $msg"); |
| 106 } |
90 | 107 |
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 // TODO(iposva): Complain about duplicate entries. |
| 403 _packageMap[msg[i]] = Uri.parse(msg[i+1]); |
| 404 } |
| 405 if (_traceLoading) { |
| 406 _log("Setup package map: $_packageMap"); |
| 407 } |
| 408 } |
| 409 |
| 410 // Resolve all pending package loads now that we know how to resolve them. |
| 411 for (var i = 0; i < _pendingPackageLoads.length; i++) { |
| 412 var req = _pendingPackageLoads[i]; |
| 413 if (req != null) { |
| 414 if (_traceLoading) { |
| 415 _log("Handling deferred load request: $req"); |
| 416 } |
| 417 _loadPackage(req._tag, req._uri, req._libraryUri, req._resourceUri); |
| 418 } |
| 419 } |
| 420 // Reset the pending package loads to empty. So that we eventually can |
| 421 // finish loading. |
| 422 _pendingPackageLoads = []; |
| 423 } |
| 424 |
| 425 |
| 426 void _requestPackagesMap() { |
| 427 assert(_packagesPort == null); |
| 428 assert(_rootScript != null); |
| 429 // Create a port to receive the packages map on. |
| 430 _packagesPort = new RawReceivePort(_handlePackagesReply); |
| 431 var sp = _packagesPort.sendPort; |
| 432 |
| 433 var msg = new List(4); |
| 434 msg[0] = sp; |
| 435 msg[1] = _traceLoading; |
| 436 msg[2] = -1; |
| 437 msg[3] = _rootScript.toString(); |
| 438 _loadPort.send(msg); |
| 439 |
| 440 if (_traceLoading) { |
| 441 _log("Requested packages map for '$_rootScript'."); |
| 442 } |
| 443 } |
| 444 |
| 445 |
| 446 // Embedder Entrypoint: |
| 447 // Request the load of a particular packages map. |
| 448 void _loadPackagesMap(String packagesParam) { |
| 449 // First convert the packages parameter from the command line to a URI which |
| 450 // can be handled by the loader code. |
| 451 // TODO(iposva): Consider refactoring the common code below which is almost |
| 452 // shared with resolution of the root script. |
| 453 if (_traceLoading) { |
| 454 _log("Resolving packages map: $packagesParam"); |
| 455 } |
| 456 if (_workingDirectory == null) { |
| 457 throw 'No current working directory set.'; |
| 458 } |
| 459 var packagesName = _sanitizeWindowsPath(packagesParam); |
| 460 var packagesUri = Uri.parse(packagesName); |
| 461 if (packagesUri.scheme == '') { |
| 462 // Script does not have a scheme, assume that it is a path, |
| 463 // resolve it against the working directory. |
| 464 packagesUri = _workingDirectory.resolveUri(packagesUri); |
| 465 } |
| 466 if (_traceLoading) { |
| 467 _log('Resolved packages map to: $packagesUri'); |
| 468 } |
| 469 |
| 470 // Request the loading and parsing of the packages map at the specified URI. |
| 471 // Create a port to receive the packages map on. |
| 472 assert(_packagesPort == null); |
| 473 _packagesPort = new RawReceivePort(_handlePackagesReply); |
| 474 var sp = _packagesPort.sendPort; |
| 475 |
| 476 var msg = new List(4); |
| 477 msg[0] = sp; |
| 478 msg[1] = _traceLoading; |
| 479 msg[2] = -2; |
| 480 msg[3] = packagesUri.toString(); |
| 481 _loadPort.send(msg); |
| 482 |
| 483 // Signal that the resolution of the packages map has started. But in this |
| 484 // case it is not tied to a particular request. |
| 485 _pendingPackageLoads.add(null); |
| 486 |
| 487 if (_traceLoading) { |
| 488 _log("Requested packages map at '$packagesUri'."); |
| 489 } |
| 490 } |
| 491 |
328 | 492 |
329 void _loadScript(_LoadRequest req, Uint8List data) { | 493 void _loadScript(_LoadRequest req, Uint8List data) { |
330 // TODO: Currently a compilation error while loading the script is | 494 // TODO: Currently a compilation error while loading the script is |
331 // fatal for the isolate. _loadScriptCallback() does not return and | 495 // fatal for the isolate. _loadScriptCallback() does not return and |
332 // the number of requests remains out of sync. | 496 // the number of requests remains out of sync. |
333 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data); | 497 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data); |
334 _finishLoadRequest(req); | 498 _finishLoadRequest(req); |
335 } | 499 } |
336 | 500 |
337 | 501 |
338 void _asyncLoadError(_LoadRequest req, _LoadError error) { | 502 void _asyncLoadError(_LoadRequest req, _LoadError error) { |
339 if (_traceLoading) { | 503 if (_traceLoading) { |
340 _print("_asyncLoadError(${req._uri}), error: $error"); | 504 _log("_asyncLoadError(${req._uri}), error: $error"); |
341 } | 505 } |
342 var libraryUri = req._libraryUri; | 506 var libraryUri = req._libraryUri; |
343 if (req._tag == Dart_kImportTag) { | 507 if (req._tag == Dart_kImportTag) { |
344 // When importing a library, the libraryUri is the imported | 508 // When importing a library, the libraryUri is the imported |
345 // uri. | 509 // uri. |
346 libraryUri = req._uri; | 510 libraryUri = req._uri; |
347 } | 511 } |
348 _asyncLoadErrorCallback(req._uri, libraryUri, error); | 512 _asyncLoadErrorCallback(req._uri, libraryUri, error); |
349 _finishLoadRequest(req); | 513 _finishLoadRequest(req); |
350 } | 514 } |
351 | 515 |
352 | 516 |
353 _loadDataFromLoadPort(int tag, | 517 _loadDataFromLoadPort(int tag, |
354 String uri, | 518 String uri, |
355 String libraryUri, | 519 String libraryUri, |
356 Uri resourceUri) { | 520 Uri resourceUri) { |
357 try { | 521 try { |
358 _startLoadRequest(tag, uri, libraryUri, resourceUri); | 522 _startLoadRequest(tag, uri, libraryUri, resourceUri); |
359 } catch (e) { | 523 } catch (e) { |
360 if (_traceLoading) { | 524 if (_traceLoading) { |
361 _print("Exception when communicating with service isolate: $e"); | 525 _log("Exception when communicating with service isolate: $e"); |
362 } | 526 } |
363 // Wrap inside a _LoadError unless we are already propagating a previously | 527 // Wrap inside a _LoadError unless we are already propagating a previously |
364 // seen _LoadError. | 528 // seen _LoadError. |
365 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); | 529 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); |
366 _asyncLoadError(tag, uri, libraryUri, error); | 530 _asyncLoadError(tag, uri, libraryUri, error); |
367 } | 531 } |
368 } | 532 } |
369 | 533 |
370 | 534 |
| 535 // Loading a package URI needs to first map the package name to a loadable |
| 536 // URI. |
| 537 _loadPackage(int tag, String uri, String libraryUri, Uri resourceUri) { |
| 538 if (_packagesReady()) { |
| 539 _loadData(tag, uri, libraryUri, _resolvePackageUri(resourceUri)); |
| 540 } else { |
| 541 if (_pendingPackageLoads.isEmpty) { |
| 542 // Package resolution has not been setup yet, and this is the first |
| 543 // request for package resolution & loading. |
| 544 _requestPackagesMap(); |
| 545 } |
| 546 var req = new _LoadRequest(-1, tag, uri, libraryUri, resourceUri); |
| 547 _pendingPackageLoads.add(req); |
| 548 if (_traceLoading) { |
| 549 _log("Pending package load of '$uri': " |
| 550 "${_pendingPackageLoads.length} pending"); |
| 551 } |
| 552 } |
| 553 } |
| 554 |
| 555 |
| 556 // Load the data associated with the resourceUri. |
| 557 _loadData(int tag, String uri, String libraryUri, Uri resourceUri) { |
| 558 if (resourceUri.scheme == 'package') { |
| 559 // package based uris need to be resolved to the correct loadable location. |
| 560 // The logic of which is handled seperately, and then _loadData is called |
| 561 // recursively. |
| 562 _loadPackage(tag, uri, libraryUri, resourceUri); |
| 563 } else { |
| 564 _loadDataFromLoadPort(tag, uri, libraryUri, resourceUri); |
| 565 } |
| 566 } |
| 567 |
| 568 |
371 // Embedder Entrypoint: | 569 // Embedder Entrypoint: |
372 // Asynchronously loads script data through a http[s] or file uri. | 570 // Asynchronously loads script data through a http[s] or file uri. |
373 _loadDataAsync(int tag, String uri, String libraryUri) { | 571 _loadDataAsync(int tag, String uri, String libraryUri) { |
374 var resourceUri; | 572 var resourceUri; |
375 if (tag == Dart_kScriptTag) { | 573 if (tag == Dart_kScriptTag) { |
376 resourceUri = _resolveScriptUri(uri); | 574 resourceUri = _resolveScriptUri(uri); |
377 uri = resourceUri.toString(); | 575 uri = resourceUri.toString(); |
378 } else { | 576 } else { |
379 resourceUri = Uri.parse(uri); | 577 resourceUri = Uri.parse(uri); |
380 } | 578 } |
381 | 579 _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 } | 580 } |
389 | 581 |
390 | 582 |
391 // Embedder Entrypoint: | 583 // Embedder Entrypoint: |
392 // Function called by standalone embedder to resolve uris when the VM requests | 584 // Function called by standalone embedder to resolve uris when the VM requests |
393 // Dart_kCanonicalizeUrl from the tag handler. | 585 // Dart_kCanonicalizeUrl from the tag handler. |
394 String _resolveUri(String base, String userString) { | 586 String _resolveUri(String base, String userString) { |
395 if (_traceLoading) { | 587 if (_traceLoading) { |
396 _print('# Resolving: $userString from $base'); | 588 _log('Resolving: $userString from $base'); |
397 } | 589 } |
398 var baseUri = Uri.parse(base); | 590 var baseUri = Uri.parse(base); |
399 var result; | 591 var result; |
400 if (userString.startsWith(_DART_EXT)) { | 592 if (userString.startsWith(_DART_EXT)) { |
401 var uri = userString.substring(_DART_EXT.length); | 593 var uri = userString.substring(_DART_EXT.length); |
402 result = '$_DART_EXT${baseUri.resolve(uri)}'; | 594 result = '$_DART_EXT${baseUri.resolve(uri)}'; |
403 } else { | 595 } else { |
404 result = baseUri.resolve(userString).toString(); | 596 result = baseUri.resolve(userString).toString(); |
405 } | 597 } |
406 if (_traceLoading) { | 598 if (_traceLoading) { |
407 _print('Resolved $userString in $base to $result'); | 599 _log('Resolved $userString in $base to $result'); |
408 } | 600 } |
409 return result; | 601 return result; |
410 } | 602 } |
411 | 603 |
412 | 604 |
413 // Embedder Entrypoint (gen_snapshot): | 605 // Embedder Entrypoint (gen_snapshot): |
414 // Resolve relative paths relative to working directory. | 606 // Resolve relative paths relative to working directory. |
415 String _resolveInWorkingDirectory(String fileName) { | 607 String _resolveInWorkingDirectory(String fileName) { |
416 if (_workingDirectory == null) { | 608 if (_workingDirectory == null) { |
417 throw 'No current working directory set.'; | 609 throw 'No current working directory set.'; |
418 } | 610 } |
419 var name = _sanitizeWindowsPath(fileName); | 611 var name = _sanitizeWindowsPath(fileName); |
420 | 612 |
421 var uri = Uri.parse(name); | 613 var uri = Uri.parse(name); |
422 if (uri.scheme != '') { | 614 if (uri.scheme != '') { |
423 throw 'Schemes are not supported when resolving filenames.'; | 615 throw 'Schemes are not supported when resolving filenames.'; |
424 } | 616 } |
425 uri = _workingDirectory.resolveUri(uri); | 617 uri = _workingDirectory.resolveUri(uri); |
426 | 618 |
427 if (_traceLoading) { | 619 if (_traceLoading) { |
428 _print('# Resolved in working directory: $fileName -> $uri'); | 620 _log('Resolved in working directory: $fileName -> $uri'); |
429 } | 621 } |
430 return uri.toString(); | 622 return uri.toString(); |
431 } | 623 } |
432 | 624 |
433 | 625 |
434 // Handling of dart-ext loading. | 626 // Handling of dart-ext loading. |
435 // Dart native extension scheme. | 627 // Dart native extension scheme. |
436 const _DART_EXT = 'dart-ext:'; | 628 const _DART_EXT = 'dart-ext:'; |
437 | 629 |
438 String _nativeLibraryExtension() native "Builtin_NativeLibraryExtension"; | 630 String _nativeLibraryExtension() native "Builtin_NativeLibraryExtension"; |
439 | 631 |
440 | 632 |
441 String _platformExtensionFileName(String name) { | 633 String _platformExtensionFileName(String name) { |
442 var extension = _nativeLibraryExtension(); | 634 var extension = _nativeLibraryExtension(); |
443 | 635 |
444 if (_isWindows) { | 636 if (_isWindows) { |
445 return '$name.$extension'; | 637 return '$name.$extension'; |
446 } else { | 638 } else { |
447 return 'lib$name.$extension'; | 639 return 'lib$name.$extension'; |
448 } | 640 } |
449 } | 641 } |
450 | 642 |
451 | 643 |
452 // Returns either a file path or a URI starting with http[s]:, as a String. | 644 // Returns either a file path or a URI starting with http[s]:, as a String. |
453 String _filePathFromUri(String userUri) { | 645 String _filePathFromUri(String userUri) { |
454 var uri = Uri.parse(userUri); | 646 var uri = Uri.parse(userUri); |
455 if (_traceLoading) { | 647 if (_traceLoading) { |
456 _print('# Getting file path from: $uri'); | 648 _log('Getting file path from: $uri'); |
457 } | 649 } |
458 | 650 |
459 var path; | 651 var path; |
460 switch (uri.scheme) { | 652 switch (uri.scheme) { |
461 case '': | 653 case '': |
462 case 'file': | 654 case 'file': |
463 return uri.toFilePath(); | 655 return uri.toFilePath(); |
464 case 'package': | 656 case 'package': |
465 return _filePathFromUri(_resolvePackageUri(uri).toString()); | 657 return _filePathFromUri(_resolvePackageUri(uri).toString()); |
466 case 'data': | 658 case 'data': |
467 case 'http': | 659 case 'http': |
468 case 'https': | 660 case 'https': |
469 return uri.toString(); | 661 return uri.toString(); |
470 default: | 662 default: |
471 // Only handling file, http, and package URIs | 663 // Only handling file, http, and package URIs |
472 // in standalone binary. | 664 // in standalone binary. |
473 if (_traceLoading) { | 665 if (_traceLoading) { |
474 _print('# Unknown scheme (${uri.scheme}) in $uri.'); | 666 _log('Unknown scheme (${uri.scheme}) in $uri.'); |
475 } | 667 } |
476 throw 'Not a known scheme: $uri'; | 668 throw 'Not a known scheme: $uri'; |
477 } | 669 } |
478 } | 670 } |
479 | 671 |
480 | 672 |
481 // Embedder Entrypoint: | 673 // Embedder Entrypoint: |
482 // When loading an extension the embedder calls this method to get the | 674 // When loading an extension the embedder calls this method to get the |
483 // different components. | 675 // different components. |
484 // Returns the directory part, the filename part, and the name | 676 // Returns the directory part, the filename part, and the name |
(...skipping 22 matching lines...) Expand all Loading... |
507 } else { | 699 } else { |
508 name = userUri.substring(index + 1); | 700 name = userUri.substring(index + 1); |
509 path = userUri.substring(0, index + 1); | 701 path = userUri.substring(0, index + 1); |
510 } | 702 } |
511 | 703 |
512 path = _filePathFromUri(path); | 704 path = _filePathFromUri(path); |
513 var filename = _platformExtensionFileName(name); | 705 var filename = _platformExtensionFileName(name); |
514 | 706 |
515 return [path, filename, name]; | 707 return [path, filename, name]; |
516 } | 708 } |
OLD | NEW |