Index: pkg/compiler/lib/src/apiimpl.dart |
diff --git a/pkg/compiler/lib/src/apiimpl.dart b/pkg/compiler/lib/src/apiimpl.dart |
index e515631bdb9163d8f06ebf1a796f860c2402b6ad..ac5670047699cd92a91b2e0499802ad24ea42889 100644 |
--- a/pkg/compiler/lib/src/apiimpl.dart |
+++ b/pkg/compiler/lib/src/apiimpl.dart |
@@ -14,9 +14,7 @@ import 'package:package_config/src/packages_impl.dart' show |
NonFilePackagesDirectoryPackages; |
import 'package:package_config/src/util.dart' show |
checkValidPackageUri; |
-import 'package:sdk_library_metadata/libraries.dart' hide LIBRARIES; |
-import 'package:sdk_library_metadata/libraries.dart' as library_info show |
- LIBRARIES; |
+import 'package:sdk_library_metadata/libraries.dart' as library_info; |
import '../compiler_new.dart' as api; |
import 'commandline_options.dart'; |
@@ -50,7 +48,7 @@ class Compiler extends leg.Compiler { |
List<String> options; |
Map<String, dynamic> environment; |
bool mockableLibraryUsed = false; |
- final Set<String> allowedLibraryCategories; |
+ final Set<library_info.Category> allowedLibraryCategories; |
GenericTask userHandlerTask; |
GenericTask userProviderTask; |
@@ -179,38 +177,24 @@ class Compiler extends leg.Compiler { |
return const <String>[]; |
} |
- static Set<String> getAllowedLibraryCategories(List<String> options) { |
- var result = extractCsvOption(options, '--categories='); |
- if (result.isEmpty) { |
- result = ['Client']; |
+ static Set<library_info.Category> getAllowedLibraryCategories( |
+ List<String> options) { |
+ Iterable<library_info.Category> categories = |
+ extractCsvOption(options, '--categories=') |
+ .map(library_info.parseCategory) |
+ .where((x) => x != null); |
+ if (categories.isEmpty) { |
+ return new Set.from([library_info.Category.client]); |
} |
- result.add('Shared'); |
- result.add('Internal'); |
- return new Set<String>.from(result); |
+ return new Set.from(categories); |
} |
static bool hasOption(List<String> options, String option) { |
return options.indexOf(option) >= 0; |
} |
- // TODO(johnniwinther): Merge better with [translateDartUri] when |
- // [scanBuiltinLibrary] is removed. |
- String lookupLibraryPath(Uri uri, LibraryInfo info) { |
- if (info == null) return null; |
- if (!info.isDart2jsLibrary) return null; |
- if (!allowedLibraryCategories.contains(info.category)) { |
- registerDisallowedLibraryUse(uri); |
- return null; |
- } |
- String path = info.dart2jsPath; |
- if (path == null) { |
- path = info.path; |
- } |
- return "lib/$path"; |
- } |
- |
String lookupPatchPath(String dartLibraryName) { |
- LibraryInfo info = lookupLibraryInfo(dartLibraryName); |
+ library_info.LibraryInfo info = lookupLibraryInfo(dartLibraryName); |
if (info == null) return null; |
if (!info.isDart2jsLibrary) return null; |
String path = info.dart2jsPatchPath; |
@@ -321,36 +305,68 @@ class Compiler extends leg.Compiler { |
} |
} |
+ /// Translates "resolvedUri" with scheme "dart" to a [uri] resolved relative |
+ /// to [libraryRoot] according to the information in [library_info.libraries]. |
+ /// |
+ /// Returns null and emits an error if the library could not be found or |
+ /// imported into [importingLibrary]. |
+ /// |
+ /// If [importingLibrary] is a platform or patch library all dart2js libraries |
+ /// can be resolved. Otherwise only libraries with categories in |
+ /// [allowedLibraryCategories] can be resolved. |
Uri translateDartUri(elements.LibraryElement importingLibrary, |
Uri resolvedUri, Spannable spannable) { |
- LibraryInfo libraryInfo = lookupLibraryInfo(resolvedUri.path); |
- String path = lookupLibraryPath(resolvedUri, libraryInfo); |
- if (libraryInfo != null && |
- libraryInfo.category == "Internal") { |
- bool allowInternalLibraryAccess = false; |
- if (importingLibrary != null) { |
- if (importingLibrary.isPlatformLibrary || importingLibrary.isPatch) { |
- allowInternalLibraryAccess = true; |
- } else if (importingLibrary.canonicalUri.path.contains( |
- 'sdk/tests/compiler/dart2js_native')) { |
- allowInternalLibraryAccess = true; |
- } |
+ |
+ library_info.LibraryInfo libraryInfo = lookupLibraryInfo(resolvedUri.path); |
+ |
+ bool allowInternalLibraryAccess = false; |
+ if (importingLibrary != null) { |
+ if (importingLibrary.isPlatformLibrary || importingLibrary.isPatch) { |
+ allowInternalLibraryAccess = true; |
+ } else if (importingLibrary.canonicalUri.path.contains( |
+ 'sdk/tests/compiler/dart2js_native')) { |
+ allowInternalLibraryAccess = true; |
} |
- if (!allowInternalLibraryAccess) { |
- if (importingLibrary != null) { |
- reporter.reportErrorMessage( |
- spannable, |
- MessageKind.INTERNAL_LIBRARY_FROM, |
- {'resolvedUri': resolvedUri, |
- 'importingUri': importingLibrary.canonicalUri}); |
+ } |
+ |
+ String computePath() { |
+ if (libraryInfo == null) { |
+ return null; |
+ } else if (!libraryInfo.isDart2jsLibrary) { |
+ return null; |
+ } else { |
+ if (libraryInfo.isInternal && |
+ !allowInternalLibraryAccess) { |
+ if (importingLibrary != null) { |
+ reporter.reportErrorMessage( |
+ spannable, |
+ MessageKind.INTERNAL_LIBRARY_FROM, |
+ {'resolvedUri': resolvedUri, |
+ 'importingUri': importingLibrary.canonicalUri}); |
+ } else { |
+ reporter.reportErrorMessage( |
+ spannable, |
+ MessageKind.INTERNAL_LIBRARY, |
+ {'resolvedUri': resolvedUri}); |
+ registerDisallowedLibraryUse(resolvedUri); |
+ } |
+ return null; |
+ } else if (!allowInternalLibraryAccess && |
+ !allowedLibraryCategories.any(libraryInfo.categories.contains)) { |
+ registerDisallowedLibraryUse(resolvedUri); |
+ // TODO(sigurdm): Currently we allow the sdk libraries to import |
+ // libraries from any category. We might want to revisit this. |
+ return null; |
} else { |
- reporter.reportErrorMessage( |
- spannable, |
- MessageKind.INTERNAL_LIBRARY, |
- {'resolvedUri': resolvedUri}); |
+ return (libraryInfo.dart2jsPath != null) |
+ ? libraryInfo.dart2jsPath |
+ : libraryInfo.path; |
} |
} |
} |
+ |
+ String path = computePath(); |
+ |
if (path == null) { |
if (libraryInfo == null) { |
reporter.reportErrorMessage( |
@@ -367,13 +383,14 @@ class Compiler extends leg.Compiler { |
// value. |
return null; |
} |
+ |
if (resolvedUri.path == 'html' || |
resolvedUri.path == 'io') { |
// TODO(ahe): Get rid of mockableLibraryUsed when test.dart |
// supports this use case better. |
mockableLibraryUsed = true; |
} |
- return libraryRoot.resolve(path); |
+ return libraryRoot.resolve("lib/$path"); |
} |
Uri resolvePatchUri(String dartLibraryPath) { |
@@ -547,7 +564,7 @@ class Compiler extends leg.Compiler { |
fromEnvironment(String name) => environment[name]; |
- LibraryInfo lookupLibraryInfo(String libraryName) { |
- return library_info.LIBRARIES[libraryName]; |
+ library_info.LibraryInfo lookupLibraryInfo(String libraryName) { |
+ return library_info.libraries[libraryName]; |
} |
} |