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:collection'; | 8 import 'dart:collection'; |
9 import 'dart:_internal'; | |
8 import 'dart:isolate'; | 10 import 'dart:isolate'; |
9 import 'dart:typed_data'; | 11 import 'dart:typed_data'; |
10 | 12 |
13 | |
14 // Before handling an embedder entrypoint we finalize the setup of the | |
15 // dart:_builtin library. | |
16 bool _setupCompleted = false; | |
17 | |
18 | |
11 // The root library (aka the script) is imported into this library. The | 19 // The root library (aka the script) is imported into this library. The |
12 // standalone embedder uses this to lookup the main entrypoint in the | 20 // standalone embedder uses this to lookup the main entrypoint in the |
13 // root library's namespace. | 21 // root library's namespace. |
14 Function _getMainClosure() => main; | 22 Function _getMainClosure() => main; |
15 | 23 |
16 | 24 |
17 // 'print' implementation. | 25 // 'print' implementation. |
18 // The standalone embedder registers the closurized _print function with the | 26 // The standalone embedder registers the closurized _print function with the |
19 // dart:core library. | 27 // dart:core library. |
20 void _printString(String s) native "Builtin_PrintString"; | 28 void _printString(String s) native "Builtin_PrintString"; |
(...skipping 21 matching lines...) Expand all Loading... | |
42 } | 50 } |
43 | 51 |
44 | 52 |
45 _getUriBaseClosure() => _uriBase; | 53 _getUriBaseClosure() => _uriBase; |
46 | 54 |
47 | 55 |
48 // Asynchronous loading of resources. | 56 // Asynchronous loading of resources. |
49 // The embedder forwards most loading requests to this library. | 57 // The embedder forwards most loading requests to this library. |
50 | 58 |
51 // See Dart_LibraryTag in dart_api.h | 59 // See Dart_LibraryTag in dart_api.h |
52 const Dart_kScriptTag = null; | 60 const _Dart_kScriptTag = null; |
53 const Dart_kImportTag = 0; | 61 const _Dart_kImportTag = 0; |
54 const Dart_kSourceTag = 1; | 62 const _Dart_kSourceTag = 1; |
55 const Dart_kCanonicalizeUrl = 2; | 63 const _Dart_kCanonicalizeUrl = 2; |
64 const _Dart_kResourceLoad = 3; | |
56 | 65 |
57 // Embedder sets this to true if the --trace-loading flag was passed on the | 66 // Embedder sets this to true if the --trace-loading flag was passed on the |
58 // command line. | 67 // command line. |
59 bool _traceLoading = false; | 68 bool _traceLoading = false; |
60 | 69 |
61 // A port for communicating with the service isolate for I/O. | 70 // A port for communicating with the service isolate for I/O. |
62 SendPort _loadPort; | 71 SendPort _loadPort; |
63 // The receive port for a load request. Multiple sources can be fetched in | 72 // The receive port for a load request. Multiple sources can be fetched in |
64 // a single load request. | 73 // a single load request. |
65 RawReceivePort _receivePort; | 74 RawReceivePort _receivePort; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 _LoadError(this.uri, this.message); | 121 _LoadError(this.uri, this.message); |
113 | 122 |
114 String toString() => 'Load Error for "$uri": $message'; | 123 String toString() => 'Load Error for "$uri": $message'; |
115 } | 124 } |
116 | 125 |
117 // Class collecting all of the information about a particular load request. | 126 // Class collecting all of the information about a particular load request. |
118 class _LoadRequest { | 127 class _LoadRequest { |
119 final int _id; | 128 final int _id; |
120 final int _tag; | 129 final int _tag; |
121 final String _uri; | 130 final String _uri; |
122 final String _libraryUri; | |
123 final Uri _resourceUri; | 131 final Uri _resourceUri; |
132 final _context; | |
124 | 133 |
125 _LoadRequest(this._id, | 134 _LoadRequest(this._id, |
126 this._tag, | 135 this._tag, |
127 this._uri, | 136 this._uri, |
128 this._libraryUri, | 137 this._resourceUri, |
129 this._resourceUri); | 138 this._context); |
130 | 139 |
131 toString() => "LoadRequest($_id, $_tag, $_uri, $_libraryUri, $_resourceUri)"; | 140 toString() => "LoadRequest($_id, $_tag, $_uri, $_resourceUri, $_context)"; |
132 } | 141 } |
133 | 142 |
134 | 143 |
135 // Native calls provided by the embedder. | 144 // Native calls provided by the embedder. |
136 void _signalDoneLoading() native "Builtin_DoneLoading"; | 145 void _signalDoneLoading() native "Builtin_DoneLoading"; |
137 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) | 146 void _loadScriptCallback(int tag, String uri, String libraryUri, Uint8List data) |
138 native "Builtin_LoadSource"; | 147 native "Builtin_LoadSource"; |
139 void _asyncLoadErrorCallback(uri, libraryUri, error) | 148 void _asyncLoadErrorCallback(uri, libraryUri, error) |
140 native "Builtin_AsyncLoadError"; | 149 native "Builtin_AsyncLoadError"; |
141 | 150 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
188 if (!uri.endsWith('/')) { | 197 if (!uri.endsWith('/')) { |
189 return '$uri/'; | 198 return '$uri/'; |
190 } | 199 } |
191 return uri; | 200 return uri; |
192 } | 201 } |
193 | 202 |
194 | 203 |
195 // Embedder Entrypoint: | 204 // Embedder Entrypoint: |
196 // The embedder calls this method with the current working directory. | 205 // The embedder calls this method with the current working directory. |
197 void _setWorkingDirectory(cwd) { | 206 void _setWorkingDirectory(cwd) { |
207 if (!_setupCompleted) { | |
208 _setupHooks(); | |
209 } | |
198 if (_traceLoading) { | 210 if (_traceLoading) { |
199 _log('Setting working directory: $cwd'); | 211 _log('Setting working directory: $cwd'); |
200 } | 212 } |
201 _workingDirectory = new Uri.directory(cwd); | 213 _workingDirectory = new Uri.directory(cwd); |
202 if (_traceLoading) { | 214 if (_traceLoading) { |
203 _log('Working directory URI: $_workingDirectory'); | 215 _log('Working directory URI: $_workingDirectory'); |
204 } | 216 } |
205 } | 217 } |
206 | 218 |
207 | 219 |
208 // Embedder Entrypoint: | 220 // Embedder Entrypoint: |
209 // The embedder calls this method with a custom package root. | 221 // The embedder calls this method with a custom package root. |
210 _setPackageRoot(String packageRoot) { | 222 _setPackageRoot(String packageRoot) { |
223 if (!_setupCompleted) { | |
224 _setupHooks(); | |
225 } | |
211 if (_traceLoading) { | 226 if (_traceLoading) { |
212 _log('Setting package root: $packageRoot'); | 227 _log('Setting package root: $packageRoot'); |
213 } | 228 } |
214 packageRoot = _enforceTrailingSlash(packageRoot); | 229 packageRoot = _enforceTrailingSlash(packageRoot); |
215 if (packageRoot.startsWith('file:') || | 230 if (packageRoot.startsWith('file:') || |
216 packageRoot.startsWith('http:') || | 231 packageRoot.startsWith('http:') || |
217 packageRoot.startsWith('https:')) { | 232 packageRoot.startsWith('https:')) { |
218 _packageRoot = _workingDirectory.resolve(packageRoot); | 233 _packageRoot = _workingDirectory.resolve(packageRoot); |
219 } else { | 234 } else { |
220 packageRoot = _sanitizeWindowsPath(packageRoot); | 235 packageRoot = _sanitizeWindowsPath(packageRoot); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
316 } | 331 } |
317 | 332 |
318 | 333 |
319 void _handleLoaderReply(msg) { | 334 void _handleLoaderReply(msg) { |
320 int id = msg[0]; | 335 int id = msg[0]; |
321 var dataOrError = msg[1]; | 336 var dataOrError = msg[1]; |
322 assert((id >= 0) && (id < _reqId)); | 337 assert((id >= 0) && (id < _reqId)); |
323 var req = _reqMap[id]; | 338 var req = _reqMap[id]; |
324 try { | 339 try { |
325 if (dataOrError is Uint8List) { | 340 if (dataOrError is Uint8List) { |
326 _loadScript(req, dataOrError); | 341 // Successfully loaded the data. |
342 if (req._tag == _Dart_kResourceLoad) { | |
343 Completer c = req._context; | |
344 c.complete(dataOrError); | |
345 } else { | |
346 // TODO: Currently a compilation error while loading the script is | |
347 // fatal for the isolate. _loadScriptCallback() does not return and | |
348 // the number of requests remains out of sync. | |
349 _loadScriptCallback(req._tag, req._uri, req._context, dataOrError); | |
350 } | |
351 _finishLoadRequest(req); | |
327 } else { | 352 } else { |
328 assert(dataOrError is String); | 353 assert(dataOrError is String); |
329 var error = new _LoadError(req._uri, dataOrError.toString()); | 354 var error = new _LoadError(req._uri, dataOrError.toString()); |
330 _asyncLoadError(req, error); | 355 _asyncLoadError(req, error); |
331 } | 356 } |
332 } catch(e, s) { | 357 } catch(e, s) { |
333 // Wrap inside a _LoadError unless we are already propagating a | 358 // Wrap inside a _LoadError unless we are already propagating a |
334 // previous _LoadError. | 359 // previous _LoadError. |
335 var error = (e is _LoadError) ? e : new _LoadError(req._uri, e.toString()); | 360 var error = (e is _LoadError) ? e : new _LoadError(req._uri, e.toString()); |
336 assert(req != null); | 361 assert(req != null); |
337 _asyncLoadError(req, error); | 362 _asyncLoadError(req, error); |
338 } | 363 } |
339 } | 364 } |
340 | 365 |
341 | 366 |
342 void _startLoadRequest(int tag, | 367 void _startLoadRequest(int tag, String uri, Uri resourceUri, context) { |
343 String uri, | |
344 String libraryUri, | |
345 Uri resourceUri) { | |
346 if (_receivePort == null) { | 368 if (_receivePort == null) { |
347 if (_traceLoading) { | 369 if (_traceLoading) { |
348 _log("Initializing load port."); | 370 _log("Initializing load port."); |
349 } | 371 } |
350 assert(_receivePort == null); | 372 assert(_receivePort == null); |
351 assert(_sendPort == null); | 373 assert(_sendPort == null); |
352 _receivePort = new RawReceivePort(_handleLoaderReply); | 374 _receivePort = new RawReceivePort(_handleLoaderReply); |
353 _sendPort = _receivePort.sendPort; | 375 _sendPort = _receivePort.sendPort; |
354 } | 376 } |
355 // Register the load request and send it to the VM service isolate. | 377 // Register the load request and send it to the VM service isolate. |
356 var curId = _reqId++; | 378 var curId = _reqId++; |
357 | 379 |
358 assert(_reqMap[curId] == null); | 380 assert(_reqMap[curId] == null); |
359 _reqMap[curId] = new _LoadRequest(curId, tag, uri, libraryUri, resourceUri); | 381 _reqMap[curId] = new _LoadRequest(curId, tag, uri, resourceUri, context); |
360 | 382 |
361 assert(_receivePort != null); | 383 assert(_receivePort != null); |
362 assert(_sendPort != null); | 384 assert(_sendPort != null); |
363 | 385 |
364 var msg = new List(4); | 386 var msg = new List(4); |
365 msg[0] = _sendPort; | 387 msg[0] = _sendPort; |
366 msg[1] = _traceLoading; | 388 msg[1] = _traceLoading; |
367 msg[2] = curId; | 389 msg[2] = curId; |
368 msg[3] = resourceUri.toString(); | 390 msg[3] = resourceUri.toString(); |
369 _loadPort.send(msg); | 391 _loadPort.send(msg); |
370 | 392 |
371 if (_traceLoading) { | 393 if (_traceLoading) { |
372 _log("Loading of $resourceUri for $uri started with id: $curId, " | 394 _log("Loading of $resourceUri for $uri started with id: $curId. " |
373 "${_reqMap.length} requests outstanding"); | 395 "${_reqMap.length} requests remaining, " |
396 "${_pendingPackageLoads.length} packages pending."); | |
374 } | 397 } |
375 } | 398 } |
376 | 399 |
377 | 400 |
378 RawReceivePort _packagesPort; | 401 RawReceivePort _packagesPort; |
379 | 402 |
380 void _handlePackagesReply(msg) { | 403 void _handlePackagesReply(msg) { |
381 // Make sure to close the _packagePort before any other action. | 404 // Make sure to close the _packagePort before any other action. |
382 _packagesPort.close(); | 405 _packagesPort.close(); |
383 | 406 |
(...skipping 17 matching lines...) Expand all Loading... | |
401 for (var i = 0; i < msg.length; i+=2) { | 424 for (var i = 0; i < msg.length; i+=2) { |
402 // TODO(iposva): Complain about duplicate entries. | 425 // TODO(iposva): Complain about duplicate entries. |
403 _packageMap[msg[i]] = Uri.parse(msg[i+1]); | 426 _packageMap[msg[i]] = Uri.parse(msg[i+1]); |
404 } | 427 } |
405 if (_traceLoading) { | 428 if (_traceLoading) { |
406 _log("Setup package map: $_packageMap"); | 429 _log("Setup package map: $_packageMap"); |
407 } | 430 } |
408 } | 431 } |
409 | 432 |
410 // Resolve all pending package loads now that we know how to resolve them. | 433 // Resolve all pending package loads now that we know how to resolve them. |
411 for (var i = 0; i < _pendingPackageLoads.length; i++) { | 434 while (_pendingPackageLoads.length > 0) { |
412 var req = _pendingPackageLoads[i]; | 435 var req = _pendingPackageLoads.removeLast(); |
413 if (req != null) { | 436 if (req != null) { |
414 if (_traceLoading) { | 437 if (_traceLoading) { |
415 _log("Handling deferred load request: $req"); | 438 _log("Handling deferred load request: $req"); |
416 } | 439 } |
417 _loadPackage(req._tag, req._uri, req._libraryUri, req._resourceUri); | 440 _loadPackage(req._tag, req._uri, req._resourceUri, req._context); |
441 } else { | |
442 if (_traceLoading) { | |
443 _log("Skipping dummy deferred request."); | |
444 } | |
418 } | 445 } |
419 } | 446 } |
420 // Reset the pending package loads to empty. So that we eventually can | 447 // Reset the pending package loads to empty. So that we eventually can |
421 // finish loading. | 448 // finish loading. |
422 _pendingPackageLoads = []; | 449 _pendingPackageLoads = []; |
423 } | 450 } |
424 | 451 |
425 | 452 |
426 void _requestPackagesMap() { | 453 void _requestPackagesMap() { |
427 assert(_packagesPort == null); | 454 assert(_packagesPort == null); |
(...skipping 11 matching lines...) Expand all Loading... | |
439 | 466 |
440 if (_traceLoading) { | 467 if (_traceLoading) { |
441 _log("Requested packages map for '$_rootScript'."); | 468 _log("Requested packages map for '$_rootScript'."); |
442 } | 469 } |
443 } | 470 } |
444 | 471 |
445 | 472 |
446 // Embedder Entrypoint: | 473 // Embedder Entrypoint: |
447 // Request the load of a particular packages map. | 474 // Request the load of a particular packages map. |
448 void _loadPackagesMap(String packagesParam) { | 475 void _loadPackagesMap(String packagesParam) { |
476 if (!_setupCompleted) { | |
477 _setupHooks(); | |
478 } | |
449 // First convert the packages parameter from the command line to a URI which | 479 // First convert the packages parameter from the command line to a URI which |
450 // can be handled by the loader code. | 480 // can be handled by the loader code. |
451 // TODO(iposva): Consider refactoring the common code below which is almost | 481 // TODO(iposva): Consider refactoring the common code below which is almost |
452 // shared with resolution of the root script. | 482 // shared with resolution of the root script. |
453 if (_traceLoading) { | 483 if (_traceLoading) { |
454 _log("Resolving packages map: $packagesParam"); | 484 _log("Resolving packages map: $packagesParam"); |
455 } | 485 } |
456 if (_workingDirectory == null) { | 486 if (_workingDirectory == null) { |
457 throw 'No current working directory set.'; | 487 throw 'No current working directory set.'; |
458 } | 488 } |
(...skipping 24 matching lines...) Expand all Loading... | |
483 // Signal that the resolution of the packages map has started. But in this | 513 // Signal that the resolution of the packages map has started. But in this |
484 // case it is not tied to a particular request. | 514 // case it is not tied to a particular request. |
485 _pendingPackageLoads.add(null); | 515 _pendingPackageLoads.add(null); |
486 | 516 |
487 if (_traceLoading) { | 517 if (_traceLoading) { |
488 _log("Requested packages map at '$packagesUri'."); | 518 _log("Requested packages map at '$packagesUri'."); |
489 } | 519 } |
490 } | 520 } |
491 | 521 |
492 | 522 |
493 void _loadScript(_LoadRequest req, Uint8List data) { | |
494 // TODO: Currently a compilation error while loading the script is | |
495 // fatal for the isolate. _loadScriptCallback() does not return and | |
496 // the number of requests remains out of sync. | |
497 _loadScriptCallback(req._tag, req._uri, req._libraryUri, data); | |
498 _finishLoadRequest(req); | |
499 } | |
500 | |
501 | |
502 void _asyncLoadError(_LoadRequest req, _LoadError error) { | 523 void _asyncLoadError(_LoadRequest req, _LoadError error) { |
503 if (_traceLoading) { | 524 if (_traceLoading) { |
504 _log("_asyncLoadError(${req._uri}), error: $error"); | 525 _log("_asyncLoadError(${req._uri}), error: $error"); |
505 } | 526 } |
506 var libraryUri = req._libraryUri; | 527 var libraryUri = req._context; |
507 if (req._tag == Dart_kImportTag) { | 528 if (req._tag == _Dart_kImportTag) { |
508 // When importing a library, the libraryUri is the imported | 529 // When importing a library, the libraryUri is the imported |
509 // uri. | 530 // uri. |
510 libraryUri = req._uri; | 531 libraryUri = req._uri; |
511 } | 532 } |
512 _asyncLoadErrorCallback(req._uri, libraryUri, error); | 533 _asyncLoadErrorCallback(req._uri, libraryUri, error); |
513 _finishLoadRequest(req); | 534 _finishLoadRequest(req); |
514 } | 535 } |
515 | 536 |
516 | 537 |
517 _loadDataFromLoadPort(int tag, | 538 _loadDataFromLoadPort(int tag, String uri, Uri resourceUri, context) { |
518 String uri, | |
519 String libraryUri, | |
520 Uri resourceUri) { | |
521 try { | 539 try { |
522 _startLoadRequest(tag, uri, libraryUri, resourceUri); | 540 _startLoadRequest(tag, uri, resourceUri, context); |
523 } catch (e) { | 541 } catch (e, s) { |
542 // For resource loads we need to complete with an error. | |
543 if (tag == _Dart_kResourceLoad) { | |
544 assert(context is Completer); | |
545 context.completeError(e, s); | |
546 } | |
547 | |
524 if (_traceLoading) { | 548 if (_traceLoading) { |
525 _log("Exception when communicating with service isolate: $e"); | 549 _log("Exception when communicating with service isolate: $e"); |
526 } | 550 } |
527 // Wrap inside a _LoadError unless we are already propagating a previously | 551 // Wrap inside a _LoadError unless we are already propagating a previously |
528 // seen _LoadError. | 552 // seen _LoadError. |
529 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); | 553 var error = (e is _LoadError) ? e : new _LoadError(e.toString()); |
530 _asyncLoadError(tag, uri, libraryUri, error); | 554 _asyncLoadError(tag, uri, context, error); |
531 } | 555 } |
532 } | 556 } |
533 | 557 |
534 | 558 |
535 // Loading a package URI needs to first map the package name to a loadable | 559 // Loading a package URI needs to first map the package name to a loadable |
536 // URI. | 560 // URI. |
537 _loadPackage(int tag, String uri, String libraryUri, Uri resourceUri) { | 561 _loadPackage(int tag, String uri, Uri resourceUri, context) { |
538 if (_packagesReady()) { | 562 if (_packagesReady()) { |
539 _loadData(tag, uri, libraryUri, _resolvePackageUri(resourceUri)); | 563 _loadData(tag, uri, _resolvePackageUri(resourceUri), context); |
540 } else { | 564 } else { |
541 if (_pendingPackageLoads.isEmpty) { | 565 if (_pendingPackageLoads.isEmpty) { |
542 // Package resolution has not been setup yet, and this is the first | 566 // Package resolution has not been setup yet, and this is the first |
543 // request for package resolution & loading. | 567 // request for package resolution & loading. |
544 _requestPackagesMap(); | 568 _requestPackagesMap(); |
545 } | 569 } |
546 var req = new _LoadRequest(-1, tag, uri, libraryUri, resourceUri); | 570 var req = new _LoadRequest(-1, tag, uri, resourceUri, context); |
547 _pendingPackageLoads.add(req); | 571 _pendingPackageLoads.add(req); |
548 if (_traceLoading) { | 572 if (_traceLoading) { |
549 _log("Pending package load of '$uri': " | 573 _log("Pending package load of '$uri': " |
550 "${_pendingPackageLoads.length} pending"); | 574 "${_pendingPackageLoads.length} pending"); |
551 } | 575 } |
552 } | 576 } |
553 } | 577 } |
554 | 578 |
555 | 579 |
556 // Load the data associated with the resourceUri. | 580 // Load the data associated with the resourceUri. |
557 _loadData(int tag, String uri, String libraryUri, Uri resourceUri) { | 581 _loadData(int tag, String uri, Uri resourceUri, context) { |
558 if (resourceUri.scheme == 'package') { | 582 if (resourceUri.scheme == 'package') { |
559 // package based uris need to be resolved to the correct loadable location. | 583 // 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 | 584 // The logic of which is handled seperately, and then _loadData is called |
561 // recursively. | 585 // recursively. |
562 _loadPackage(tag, uri, libraryUri, resourceUri); | 586 _loadPackage(tag, uri, resourceUri, context); |
563 } else { | 587 } else { |
564 _loadDataFromLoadPort(tag, uri, libraryUri, resourceUri); | 588 _loadDataFromLoadPort(tag, uri, resourceUri, context); |
565 } | 589 } |
566 } | 590 } |
567 | 591 |
568 | 592 |
569 // Embedder Entrypoint: | 593 // Embedder Entrypoint: |
570 // Asynchronously loads script data through a http[s] or file uri. | 594 // Asynchronously loads script data through a http[s] or file uri. |
571 _loadDataAsync(int tag, String uri, String libraryUri) { | 595 _loadDataAsync(int tag, String uri, String libraryUri) { |
596 if (!_setupCompleted) { | |
597 _setupHooks(); | |
598 } | |
572 var resourceUri; | 599 var resourceUri; |
573 if (tag == Dart_kScriptTag) { | 600 if (tag == _Dart_kScriptTag) { |
574 resourceUri = _resolveScriptUri(uri); | 601 resourceUri = _resolveScriptUri(uri); |
575 uri = resourceUri.toString(); | 602 uri = resourceUri.toString(); |
576 } else { | 603 } else { |
577 resourceUri = Uri.parse(uri); | 604 resourceUri = Uri.parse(uri); |
578 } | 605 } |
579 _loadData(tag, uri, libraryUri, resourceUri); | 606 _loadData(tag, uri, resourceUri, libraryUri); |
580 } | 607 } |
581 | 608 |
582 | 609 |
583 // Embedder Entrypoint: | 610 // Embedder Entrypoint: |
584 // Function called by standalone embedder to resolve uris when the VM requests | 611 // Function called by standalone embedder to resolve uris when the VM requests |
585 // Dart_kCanonicalizeUrl from the tag handler. | 612 // Dart_kCanonicalizeUrl from the tag handler. |
586 String _resolveUri(String base, String userString) { | 613 String _resolveUri(String base, String userString) { |
614 if (!_setupCompleted) { | |
615 _setupHooks(); | |
616 } | |
587 if (_traceLoading) { | 617 if (_traceLoading) { |
588 _log('Resolving: $userString from $base'); | 618 _log('Resolving: $userString from $base'); |
589 } | 619 } |
590 var baseUri = Uri.parse(base); | 620 var baseUri = Uri.parse(base); |
591 var result; | 621 var result; |
592 if (userString.startsWith(_DART_EXT)) { | 622 if (userString.startsWith(_DART_EXT)) { |
593 var uri = userString.substring(_DART_EXT.length); | 623 var uri = userString.substring(_DART_EXT.length); |
594 result = '$_DART_EXT${baseUri.resolve(uri)}'; | 624 result = '$_DART_EXT${baseUri.resolve(uri)}'; |
595 } else { | 625 } else { |
596 result = baseUri.resolve(userString).toString(); | 626 result = baseUri.resolve(userString).toString(); |
597 } | 627 } |
598 if (_traceLoading) { | 628 if (_traceLoading) { |
599 _log('Resolved $userString in $base to $result'); | 629 _log('Resolved $userString in $base to $result'); |
600 } | 630 } |
601 return result; | 631 return result; |
602 } | 632 } |
603 | 633 |
604 | 634 |
635 // Handling of Resource class by dispatching to the load port. | |
636 Future<List<int>> _resourceReadAsBytes(Uri uri) { | |
637 var completer = new Completer<List<int>>(); | |
638 // Request the load of the resource associating the completer as the context | |
639 // for the load. | |
640 _loadData(_Dart_kResourceLoad, uri.toString(), uri, completer); | |
641 // Return the future that will be triggered once the resource has been loaded. | |
642 return completer.future; | |
643 } | |
644 | |
645 | |
605 // Embedder Entrypoint (gen_snapshot): | 646 // Embedder Entrypoint (gen_snapshot): |
606 // Resolve relative paths relative to working directory. | 647 // Resolve relative paths relative to working directory. |
607 String _resolveInWorkingDirectory(String fileName) { | 648 String _resolveInWorkingDirectory(String fileName) { |
649 if (!_setupCompleted) { | |
650 _setupHooks(); | |
651 } | |
608 if (_workingDirectory == null) { | 652 if (_workingDirectory == null) { |
609 throw 'No current working directory set.'; | 653 throw 'No current working directory set.'; |
610 } | 654 } |
611 var name = _sanitizeWindowsPath(fileName); | 655 var name = _sanitizeWindowsPath(fileName); |
612 | 656 |
613 var uri = Uri.parse(name); | 657 var uri = Uri.parse(name); |
614 if (uri.scheme != '') { | 658 if (uri.scheme != '') { |
615 throw 'Schemes are not supported when resolving filenames.'; | 659 throw 'Schemes are not supported when resolving filenames.'; |
616 } | 660 } |
617 uri = _workingDirectory.resolveUri(uri); | 661 uri = _workingDirectory.resolveUri(uri); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
672 | 716 |
673 // Embedder Entrypoint: | 717 // Embedder Entrypoint: |
674 // When loading an extension the embedder calls this method to get the | 718 // When loading an extension the embedder calls this method to get the |
675 // different components. | 719 // different components. |
676 // Returns the directory part, the filename part, and the name | 720 // Returns the directory part, the filename part, and the name |
677 // of a native extension URL as a list [directory, filename, name]. | 721 // of a native extension URL as a list [directory, filename, name]. |
678 // The directory part is either a file system path or an HTTP(S) URL. | 722 // The directory part is either a file system path or an HTTP(S) URL. |
679 // The filename part is the extension name, with the platform-dependent | 723 // The filename part is the extension name, with the platform-dependent |
680 // prefixes and extensions added. | 724 // prefixes and extensions added. |
681 _extensionPathFromUri(String userUri) { | 725 _extensionPathFromUri(String userUri) { |
726 if (!_setupCompleted) { | |
727 _setupHooks(); | |
728 } | |
682 if (!userUri.startsWith(_DART_EXT)) { | 729 if (!userUri.startsWith(_DART_EXT)) { |
683 throw 'Unexpected internal error: Extension URI $userUri missing dart-ext:'; | 730 throw 'Unexpected internal error: Extension URI $userUri missing dart-ext:'; |
684 } | 731 } |
685 userUri = userUri.substring(_DART_EXT.length); | 732 userUri = userUri.substring(_DART_EXT.length); |
686 | 733 |
687 if (userUri.contains('\\')) { | 734 if (userUri.contains('\\')) { |
688 throw 'Unexpected internal error: Extension URI $userUri contains \\'; | 735 throw 'Unexpected internal error: Extension URI $userUri contains \\'; |
689 } | 736 } |
690 | 737 |
691 String name; | 738 String name; |
692 String path; // Will end in '/'. | 739 String path; // Will end in '/'. |
693 int index = userUri.lastIndexOf('/'); | 740 int index = userUri.lastIndexOf('/'); |
694 if (index == -1) { | 741 if (index == -1) { |
695 name = userUri; | 742 name = userUri; |
696 path = './'; | 743 path = './'; |
697 } else if (index == userUri.length - 1) { | 744 } else if (index == userUri.length - 1) { |
698 throw 'Extension name missing in $extensionUri'; | 745 throw 'Extension name missing in $extensionUri'; |
699 } else { | 746 } else { |
700 name = userUri.substring(index + 1); | 747 name = userUri.substring(index + 1); |
701 path = userUri.substring(0, index + 1); | 748 path = userUri.substring(0, index + 1); |
702 } | 749 } |
703 | 750 |
704 path = _filePathFromUri(path); | 751 path = _filePathFromUri(path); |
705 var filename = _platformExtensionFileName(name); | 752 var filename = _platformExtensionFileName(name); |
706 | 753 |
707 return [path, filename, name]; | 754 return [path, filename, name]; |
708 } | 755 } |
756 | |
757 | |
758 // | |
siva
2015/08/04 21:48:23
Dangling comment start ?
| |
759 _setupHooks() { | |
760 _setupCompleted = true; | |
761 VMLibraryHooks.resourceReadAsBytes = _resourceReadAsBytes; | |
762 } | |
OLD | NEW |