@@ -30,44 +30,8 @@ _sanitizeWindowsPath(path) {
3030 return fixedPath;
3131}
3232
33- _trimWindowsPath (path) {
34- // Convert /X:/ to X:/.
35- if (_isWindows == false ) {
36- // Do nothing when not running Windows.
37- return path;
38- }
39- if (! path.startsWith ('/' ) || (path.length < 3 )) {
40- return path;
41- }
42- // Match '/?:'.
43- if ((path[0 ] == '/' ) && (path[2 ] == ':' )) {
44- // Remove leading '/'.
45- return path.substring (1 );
46- }
47- return path;
48- }
49-
50- // Ensure we have a trailing slash character.
51- _enforceTrailingSlash (uri) {
52- if (! uri.endsWith ('/' )) {
53- return '$uri /' ;
54- }
55- return uri;
56- }
57-
58- class FileRequest {
59- final SendPort sp;
60- final int tag;
61- final Uri uri;
62- final Uri resolvedUri;
63- final String libraryUrl;
64- FileRequest (this .sp, this .tag, this .uri, this .resolvedUri, this .libraryUrl);
65- }
66-
6733@pragma ("vm:entry-point" )
6834bool _traceLoading = false ;
69- @pragma ("vm:entry-point" )
70- bool _deterministic = false ;
7135
7236// State associated with the isolate that is used for loading.
7337class IsolateLoaderState extends IsolateEmbedderData {
@@ -87,10 +51,6 @@ class IsolateLoaderState extends IsolateEmbedderData {
8751 if (rootScript != null ) {
8852 _rootScript = Uri .parse (rootScript);
8953 }
90- // If the --package-root flag was passed.
91- if (packageRootFlag != null ) {
92- _setPackageRoot (packageRootFlag);
93- }
9454 // If the --packages flag was passed.
9555 if (packagesConfigFlag != null ) {
9656 _setPackagesConfig (packagesConfigFlag);
@@ -139,40 +99,6 @@ class IsolateLoaderState extends IsolateEmbedderData {
13999 Uri _packageConfig = null ;
140100 Map <String , Uri > _packageMap = null ;
141101
142- // We issue only 16 concurrent calls to File.readAsBytes() to stay within
143- // platform-specific resource limits (e.g. max open files). The rest go on
144- // _fileRequestQueue and are processed when we can safely issue them.
145- static final int _maxFileRequests = _deterministic ? 1 : 16 ;
146- int currentFileRequests = 0 ;
147- final List <FileRequest > _fileRequestQueue = new List <FileRequest >();
148-
149- bool get shouldIssueFileRequest => currentFileRequests < _maxFileRequests;
150- void enqueueFileRequest (FileRequest fr) {
151- _fileRequestQueue.add (fr);
152- }
153-
154- FileRequest dequeueFileRequest () {
155- if (_fileRequestQueue.length == 0 ) {
156- return null ;
157- }
158- return _fileRequestQueue.removeAt (0 );
159- }
160-
161- _setPackageRoot (String packageRoot) {
162- packageRoot = _sanitizeWindowsPath (packageRoot);
163- if (packageRoot.startsWith ('file:' ) ||
164- packageRoot.startsWith ('http:' ) ||
165- packageRoot.startsWith ('https:' )) {
166- packageRoot = _enforceTrailingSlash (packageRoot);
167- _packageRoot = _workingDirectory.resolve (packageRoot);
168- } else {
169- packageRoot = _sanitizeWindowsPath (packageRoot);
170- packageRoot = _trimWindowsPath (packageRoot);
171- _packageRoot =
172- _workingDirectory.resolveUri (new Uri .directory (packageRoot));
173- }
174- }
175-
176102 _setPackagesConfig (String packagesParam) {
177103 var packagesName = _sanitizeWindowsPath (packagesParam);
178104 var packagesUri = Uri .parse (packagesName);
@@ -396,150 +322,6 @@ void _sendExtensionImportResponse(
396322 sp.send (msg);
397323}
398324
399- void _loadHttp (
400- SendPort sp, int tag, Uri uri, Uri resolvedUri, String libraryUrl) {
401- if (_httpClient == null ) {
402- _httpClient = new HttpClient ()..maxConnectionsPerHost = 6 ;
403- }
404- _httpClient
405- .getUrl (resolvedUri)
406- .then ((HttpClientRequest request) => request.close ())
407- .then ((HttpClientResponse response) {
408- var builder = new BytesBuilder (copy: false );
409- response.listen (builder.add, onDone: () {
410- if (response.statusCode != 200 ) {
411- var msg = "Failure getting $resolvedUri :\n "
412- " ${response .statusCode } ${response .reasonPhrase }" ;
413- _sendResourceResponse (sp, tag, uri, resolvedUri, libraryUrl, msg);
414- } else {
415- _sendResourceResponse (
416- sp, tag, uri, resolvedUri, libraryUrl, builder.takeBytes ());
417- }
418- }, onError: (e) {
419- _sendResourceResponse (
420- sp, tag, uri, resolvedUri, libraryUrl, e.toString ());
421- });
422- }).catchError ((e) {
423- _sendResourceResponse (sp, tag, uri, resolvedUri, libraryUrl, e.toString ());
424- });
425- // It's just here to push an event on the event loop so that we invoke the
426- // scheduled microtasks.
427- Timer .run (() {});
428- }
429-
430- void _loadFile (IsolateLoaderState loaderState, SendPort sp, int tag, Uri uri,
431- Uri resolvedUri, String libraryUrl) {
432- var path = resolvedUri.toFilePath ();
433- var sourceFile = new File (path);
434- sourceFile.readAsBytes ().then ((data) {
435- _sendResourceResponse (sp, tag, uri, resolvedUri, libraryUrl, data);
436- }, onError: (e) {
437- _sendResourceResponse (sp, tag, uri, resolvedUri, libraryUrl, e.toString ());
438- }).whenComplete (() {
439- loaderState.currentFileRequests-- ;
440- while (loaderState.shouldIssueFileRequest) {
441- FileRequest fr = loaderState.dequeueFileRequest ();
442- if (fr == null ) {
443- break ;
444- }
445- _loadFile (
446- loaderState, fr.sp, fr.tag, fr.uri, fr.resolvedUri, fr.libraryUrl);
447- loaderState.currentFileRequests++ ;
448- }
449- });
450- }
451-
452- void _loadDataUri (
453- SendPort sp, int tag, Uri uri, Uri resolvedUri, String libraryUrl) {
454- try {
455- var mime = uri.data.mimeType;
456- if ((mime != "application/dart" ) && (mime != "text/plain" )) {
457- throw "MIME-type must be application/dart or text/plain: $mime given." ;
458- }
459- var charset = uri.data.charset;
460- if ((charset != "utf-8" ) && (charset != "US-ASCII" )) {
461- // The C++ portion of the embedder assumes UTF-8.
462- throw "Only utf-8 or US-ASCII encodings are supported: $charset given." ;
463- }
464- _sendResourceResponse (
465- sp, tag, uri, resolvedUri, libraryUrl, uri.data.contentAsBytes ());
466- } catch (e) {
467- _sendResourceResponse (sp, tag, uri, resolvedUri, libraryUrl,
468- "Invalid data uri ($uri ):\n $e " );
469- }
470- }
471-
472- // Loading a package URI needs to first map the package name to a loadable
473- // URI.
474- _loadPackage (IsolateLoaderState loaderState, SendPort sp, bool traceLoading,
475- int tag, Uri uri, Uri resolvedUri, String libraryUrl) {
476- if (loaderState._packagesReady) {
477- var resolvedUri;
478- try {
479- resolvedUri = loaderState._resolvePackageUri (uri);
480- } catch (e, s) {
481- if (traceLoading) {
482- _log ("Exception ($e ) when resolving package URI: $uri " );
483- }
484- // Report error.
485- _sendResourceResponse (
486- sp, tag, uri, resolvedUri, libraryUrl, e.toString ());
487- return ;
488- }
489- // Recursively call with the new resolved uri.
490- _handleResourceRequest (
491- loaderState, sp, traceLoading, tag, uri, resolvedUri, libraryUrl);
492- } else {
493- if (loaderState._pendingPackageLoads.isEmpty) {
494- // Package resolution has not been setup yet, and this is the first
495- // request for package resolution & loading.
496- loaderState._requestPackagesMap ();
497- }
498- // Register the action of loading this package once the package resolution
499- // is ready.
500- loaderState._pendingPackageLoads.add (() {
501- _handleResourceRequest (
502- loaderState, sp, traceLoading, tag, uri, uri, libraryUrl);
503- });
504- if (traceLoading) {
505- _log ("Pending package load of '$uri ': "
506- "${loaderState ._pendingPackageLoads .length } pending" );
507- }
508- }
509- }
510-
511- // TODO(johnmccutchan): This and most other top level functions in this file
512- // should be turned into methods on the IsolateLoaderState class.
513- _handleResourceRequest (IsolateLoaderState loaderState, SendPort sp,
514- bool traceLoading, int tag, Uri uri, Uri resolvedUri, String libraryUrl) {
515- if (resolvedUri.scheme == '' || resolvedUri.scheme == 'file' ) {
516- if (loaderState.shouldIssueFileRequest) {
517- _loadFile (loaderState, sp, tag, uri, resolvedUri, libraryUrl);
518- loaderState.currentFileRequests++ ;
519- } else {
520- FileRequest fr = new FileRequest (sp, tag, uri, resolvedUri, libraryUrl);
521- loaderState.enqueueFileRequest (fr);
522- }
523- } else if ((resolvedUri.scheme == 'http' ) ||
524- (resolvedUri.scheme == 'https' )) {
525- _loadHttp (sp, tag, uri, resolvedUri, libraryUrl);
526- } else if ((resolvedUri.scheme == 'data' )) {
527- _loadDataUri (sp, tag, uri, resolvedUri, libraryUrl);
528- } else if ((resolvedUri.scheme == 'package' )) {
529- _loadPackage (
530- loaderState, sp, traceLoading, tag, uri, resolvedUri, libraryUrl);
531- } else {
532- _sendResourceResponse (
533- sp,
534- tag,
535- uri,
536- resolvedUri,
537- libraryUrl,
538- 'Unknown scheme (${resolvedUri .scheme }) for '
539- '$resolvedUri ' );
540- }
541- }
542-
543325// Handling of packages requests. Finding and parsing of .packages file or
544326// packages/ directories.
545327const _LF = 0x0A ;
0 commit comments