Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: pkg/compiler/lib/src/resolved_uri_translator.dart

Issue 1884793002: dart2js: remove references to compiler in ResolvedUriTranslator (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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.
4
5 import 'common.dart';
6 import 'elements/elements.dart' show LibraryElement;
7 import 'util/emptyset.dart';
8
9 /// API used by the library loader to translate internal SDK URIs into file
10 /// system readable URIs.
11 abstract class ResolvedUriTranslator {
12 factory ResolvedUriTranslator(
13 Map<String, Uri> sdkLibraries, DiagnosticReporter reporter) =
14 _ResolvedUriTranslator;
15
16 /// The set of platform libraries reported as unsupported.
17 ///
18 /// For instance when importing 'dart:io' without '--categories=Server'.
19 Set<Uri> get disallowedLibraryUris;
20
21 /// Whether or not a mockable library has been translated.
22 bool get mockableLibraryUsed;
23
24 /// A mapping from dart: library names to their location.
25 Map<String, Uri> get sdkLibraries;
26
27 /// Translates the resolved [uri] into a readable URI.
28 ///
29 /// The [importingLibrary] holds the library importing [uri] or `null` if
30 /// [uri] is loaded as the main library. The [importingLibrary] is used to
31 /// grant access to internal libraries from platform libraries and patch
32 /// libraries.
33 ///
34 /// If the [uri] is not accessible from [importingLibrary], this method is
35 /// responsible for reporting errors.
36 ///
37 /// See [LibraryLoader] for terminology on URIs.
38 Uri translate(LibraryElement importingLibrary, Uri uri,
39 [Spannable spannable]);
40 }
41
42 /// A translator that forwards all methods to an internal
43 /// [ResolvedUriTranslator].
44 ///
45 /// The translator to forward to may be set after the instance is constructed.
46 /// This is useful for the compiler because some tasks that are instantiated at
47 /// compiler construction time need a [ResolvedUriTranslator], but the data
48 /// required to instantiate it cannot be obtained at construction time. So a
49 /// [ForwardingResolvedUriTranslator] may be passed instead, and the translator
50 /// to forward to can be set once the required data has been retrieved.
51 class ForwardingResolvedUriTranslator implements ResolvedUriTranslator {
52 ResolvedUriTranslator resolvedUriTranslator;
53
54 /// Returns `true` if [resolvedUriTranslator] is not `null`.
55 bool get isSet => resolvedUriTranslator != null;
56
57 /// The opposite of [isSet].
58 bool get isNotSet => resolvedUriTranslator == null;
59
60 @override
61 Uri translate(LibraryElement importingLibrary, Uri resolvedUri,
62 [Spannable spannable]) =>
63 resolvedUriTranslator.translate(importingLibrary, resolvedUri, spannable);
64
65 @override
66 Set<Uri> get disallowedLibraryUris =>
67 resolvedUriTranslator?.disallowedLibraryUris ??
68 const ImmutableEmptySet<Uri>();
69
70 @override
71 bool get mockableLibraryUsed => resolvedUriTranslator.mockableLibraryUsed;
72
73 @override
74 Map<String, Uri> get sdkLibraries => resolvedUriTranslator.sdkLibraries;
75 }
76
77 class _ResolvedUriTranslator implements ResolvedUriTranslator {
78 final Map<String, Uri> _sdkLibraries;
79 final DiagnosticReporter _reporter;
80
81 Set<Uri> disallowedLibraryUris = new Set<Uri>();
82 bool mockableLibraryUsed = false;
83
84 _ResolvedUriTranslator(this._sdkLibraries, this._reporter);
85
86 Map<String, Uri> get sdkLibraries => _sdkLibraries;
87
88 @override
89 Uri translate(LibraryElement importingLibrary, Uri uri,
90 [Spannable spannable]) {
91 if (uri.scheme == 'dart') {
92 return translateDartUri(importingLibrary, uri, spannable);
93 }
94 return uri;
95 }
96
97 /// Translates "resolvedUri" with scheme "dart" to a [uri] resolved relative
98 /// to `options.platformConfigUri` according to the information in the file at
99 /// `options.platformConfigUri`.
100 ///
101 /// Returns `null` and emits an error if the library could not be found or
102 /// imported into [importingLibrary].
103 ///
104 /// Internal libraries (whose name starts with '_') can be only resolved if
105 /// [importingLibrary] is a platform or patch library.
106 Uri translateDartUri(
107 LibraryElement importingLibrary, Uri resolvedUri, Spannable spannable) {
108 Uri location = lookupLibraryUri(resolvedUri.path);
109
110 if (location == null) {
111 _reporter.reportErrorMessage(spannable, MessageKind.LIBRARY_NOT_FOUND,
112 {'resolvedUri': resolvedUri});
113 return null;
114 }
115
116 if (resolvedUri.path.startsWith('_')) {
117 bool allowInternalLibraryAccess = importingLibrary != null &&
118 (importingLibrary.isPlatformLibrary ||
119 importingLibrary.isPatch ||
120 importingLibrary.canonicalUri.path
121 .contains('sdk/tests/compiler/dart2js_native'));
122
123 if (!allowInternalLibraryAccess) {
124 if (importingLibrary != null) {
125 _reporter.reportErrorMessage(
126 spannable, MessageKind.INTERNAL_LIBRARY_FROM, {
127 'resolvedUri': resolvedUri,
128 'importingUri': importingLibrary.canonicalUri
129 });
130 } else {
131 _reporter.reportErrorMessage(spannable, MessageKind.INTERNAL_LIBRARY,
132 {'resolvedUri': resolvedUri});
133 registerDisallowedLibraryUse(resolvedUri);
134 }
135 return null;
136 }
137 }
138
139 if (location.scheme == "unsupported") {
140 _reporter.reportErrorMessage(spannable, MessageKind.LIBRARY_NOT_SUPPORTED,
141 {'resolvedUri': resolvedUri});
142 registerDisallowedLibraryUse(resolvedUri);
143 return null;
144 }
145
146 if (resolvedUri.path == 'html' || resolvedUri.path == 'io') {
147 // TODO(ahe): Get rid of mockableLibraryUsed when test.dart
148 // supports this use case better.
149 mockableLibraryUsed = true;
150 }
151 return location;
152 }
153
154 void registerDisallowedLibraryUse(Uri uri) {
155 disallowedLibraryUris.add(uri);
156 }
157
158 Uri lookupLibraryUri(String libraryName) {
159 return _sdkLibraries[libraryName];
160 }
161 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698