Reduce dependency on library spec from dart2js

Change-Id: I0d55b380f135d2e45a0dd112336bd87085e38c76
Reviewed-on: https://dart-review.googlesource.com/c/85471
Reviewed-by: Peter von der Ahé <ahe@google.com>
Commit-Queue: Sigmund Cherem <sigmund@google.com>
diff --git a/pkg/compiler/lib/src/apiimpl.dart b/pkg/compiler/lib/src/apiimpl.dart
index 4f13e9b..8ac8b86 100644
--- a/pkg/compiler/lib/src/apiimpl.dart
+++ b/pkg/compiler/lib/src/apiimpl.dart
@@ -8,7 +8,7 @@
 import 'dart:convert' show utf8;
 
 import 'package:front_end/src/api_unstable/dart2js.dart'
-    show LibrariesSpecification, TargetLibrariesSpecification, LibraryInfo;
+    show getSupportedLibraryNames;
 
 import '../compiler_new.dart' as api;
 import 'common/tasks.dart' show GenericTask, Measurer;
@@ -54,7 +54,7 @@
   Future setupSdk() {
     var future = new Future.value(null);
     _Environment env = environment;
-    if (env.librariesSpecification == null) {
+    if (env.supportedLibraries == null) {
       future = future.then((_) {
         Uri specificationUri = options.librariesSpecificationUri;
         return provider.readFromUri(specificationUri).then((api.Input spec) {
@@ -69,10 +69,9 @@
 
           // TODO(sigmund): would be nice to front-load some of the CFE option
           // processing and parse this .json file only once.
-          env.librariesSpecification =
-              LibrariesSpecification.parse(specificationUri, json)
-                  .specificationFor(
-                      options.compileForServer ? "dart2js_server" : "dart2js");
+          env.supportedLibraries = getSupportedLibraryNames(specificationUri,
+                  json, options.compileForServer ? "dart2js_server" : "dart2js")
+              .toSet();
         });
       });
     }
@@ -181,8 +180,7 @@
 
 class _Environment implements Environment {
   final Map<String, String> definitions;
-
-  TargetLibrariesSpecification librariesSpecification;
+  Set<String> supportedLibraries;
 
   _Environment(this.definitions);
 
@@ -195,10 +193,7 @@
 
     // Private libraries are not exposed to the users.
     if (libraryName.startsWith("_")) return null;
-    LibraryInfo info = librariesSpecification.libraryInfoFor(libraryName);
-    if (info != null && info.isSupported) {
-      return "true";
-    }
+    if (supportedLibraries.contains(libraryName)) return "true";
     return null;
   }
 }
diff --git a/pkg/front_end/lib/src/api_unstable/dart2js.dart b/pkg/front_end/lib/src/api_unstable/dart2js.dart
index 24624f1..82225f3 100644
--- a/pkg/front_end/lib/src/api_unstable/dart2js.dart
+++ b/pkg/front_end/lib/src/api_unstable/dart2js.dart
@@ -16,6 +16,8 @@
 
 import '../base/processed_options.dart' show ProcessedOptions;
 
+import '../base/libraries_specification.dart' show LibrariesSpecification;
+
 import '../fasta/compiler_context.dart' show CompilerContext;
 
 import '../fasta/fasta_codes.dart' show messageMissingMain;
@@ -39,8 +41,6 @@
 
 export '../api_prototype/standard_file_system.dart' show DataFileSystemEntity;
 
-export '../base/libraries_specification.dart';
-
 export '../compute_platform_binaries_location.dart'
     show computePlatformBinariesLocation;
 
@@ -167,3 +167,23 @@
     return value;
   }
 }
+
+/// Retrieve the name of the libraries that are supported by [target] according
+/// to the libraries specification [json] file.
+///
+/// Dart2js uses these names to determine the value of library environment
+/// constants, such as `const bool.fromEnvironment("dart.library.io")`.
+// TODO(sigmund): refactor dart2js so that we can retrieve this data later in
+// the compilation pipeline. At that point we can get it from the CFE
+// results directly and completely hide the libraries specification file from
+// dart2js.
+// TODO(sigmund): delete after all constant evaluation is done in the CFE, as
+// this data will no longer be needed on the dart2js side.
+Iterable<String> getSupportedLibraryNames(
+    Uri librariesSpecificationUri, String json, String target) {
+  return LibrariesSpecification.parse(librariesSpecificationUri, json)
+      .specificationFor(target)
+      .allLibraries
+      .where((l) => l.isSupported)
+      .map((l) => l.name);
+}
diff --git a/pkg/front_end/lib/src/base/libraries_specification.dart b/pkg/front_end/lib/src/base/libraries_specification.dart
index 2a25b30..7838eda 100644
--- a/pkg/front_end/lib/src/base/libraries_specification.dart
+++ b/pkg/front_end/lib/src/base/libraries_specification.dart
@@ -232,6 +232,8 @@
 
   /// Details about a library whose import is `dart:$name`.
   LibraryInfo libraryInfoFor(String name) => _libraries[name];
+
+  Iterable<LibraryInfo> get allLibraries => _libraries.values;
 }
 
 /// Information about a `dart:` library in a specific target platform.