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

Unified Diff: compiler/java/com/google/dart/compiler/SystemLibraryManager.java

Issue 9702034: Removes dartc reliance on its own libraries, now can be targeted at any implementation's libraries (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: junit tests fixed Created 8 years, 9 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 side-by-side diff with in-line comments
Download patch
Index: compiler/java/com/google/dart/compiler/SystemLibraryManager.java
diff --git a/compiler/java/com/google/dart/compiler/SystemLibraryManager.java b/compiler/java/com/google/dart/compiler/SystemLibraryManager.java
index bab8b97444cda97da66d418f07705f9e0991a8b3..be9029a5ec8ac4f2d1b21c24588ae37c05945e91 100644
--- a/compiler/java/com/google/dart/compiler/SystemLibraryManager.java
+++ b/compiler/java/com/google/dart/compiler/SystemLibraryManager.java
@@ -5,56 +5,72 @@
package com.google.dart.compiler;
import java.io.File;
-import java.io.IOException;
-import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
-import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
+import java.util.List;
import java.util.Map;
-import java.util.jar.JarFile;
/**
* Manages the collection of {@link SystemLibrary}s.
*/
public class SystemLibraryManager {
- private enum SystemLibraryPath {
- CORE("core", "core", "com/google/dart/corelib/", "corelib.dart", "corelib.jar", true),
- COREIMPL("core", "coreimpl", "com/google/dart/corelib/", "corelib_impl.dart", "corelib.jar",
- CORE, true),
- DOM("dom", "dom", "dom/", "dom.dart", "domlib.jar"),
- HTML("html", "html", "html/", "html.dart", "htmllib.jar"),
- JSON("json", "json", "json/", "json.dart", "jsonlib.jar"),
- ISOLATE("isolate", "isolate", "isolate/", "isolate_compiler.dart", "isolatelib.jar");
-
- final String hostName;
- final SystemLibraryPath base;
- final String shortName;
- final String jar;
- final String lib;
- final boolean failIfMissing;
-
- SystemLibraryPath(String hostName, String shortName, String path, String file, String jar,
- boolean failIfMissing) {
- this(hostName, shortName, path, file, jar, null, failIfMissing);
- }
-
- SystemLibraryPath(String hostName, String shortName, String path, String file, String jar) {
- this(hostName, shortName, path, file, jar, null, false);
- }
-
- SystemLibraryPath(String hostName, String shortName, String path, String file, String jar,
- SystemLibraryPath base, boolean failIfMissing) {
- this.hostName = hostName;
+
+ private static class SystemLibraryPath {
+ String shortName;
+ String dartSourceFile;
+
+ SystemLibraryPath(String shortName, String dartSourceFile) {
this.shortName = shortName;
- this.jar = jar;
- this.lib = path + file;
- this.base = base;
- this.failIfMissing = failIfMissing;
+ this.dartSourceFile = dartSourceFile;
}
}
-
+
+ static final SystemLibraryPath[] frogImplementation = {
+ new SystemLibraryPath("core", "core/core_frog.dart"),
+ new SystemLibraryPath("coreimpl", "coreimpl/coreimpl_frog.dart"),
+ new SystemLibraryPath("dom", "dom/dom_frog.dart"),
+ new SystemLibraryPath("html", "html/html_frog.dart"),
+ new SystemLibraryPath("isolate", "isolate/isolate_frog.dart"),
+ new SystemLibraryPath("uri", "uri/uri.dart"),
+ new SystemLibraryPath("json", "json/json_frog.dart"),
+ new SystemLibraryPath("utf", "utf/utf.dart"),
+ };
+
+ static final SystemLibraryPath[] dart2JsImplentation = {
+ new SystemLibraryPath("core", "core/core_frog.dart"),
+ new SystemLibraryPath("coreimpl", "coreimpl/coreimpl_frog.dart"),
+ new SystemLibraryPath("dom", "dom/dom_frog.dart"),
+ new SystemLibraryPath("html", "html/html_frog.dart"),
+ new SystemLibraryPath("isolate", "isolate/isolate_leg.dart"),
+ new SystemLibraryPath("uri", "uri/uri.dart"),
+ new SystemLibraryPath("json", "json/json_frog.dart"),
+ new SystemLibraryPath("utf", "utf/utf.dart"),
+ };
+
+ static final SystemLibraryPath[] vmImplementation = {
+ new SystemLibraryPath("core", "core/core_runtime.dart"),
+ new SystemLibraryPath("coreimpl", "coreimpl/coreimpl_runtime.dart"),
+ new SystemLibraryPath("isolate", "isolate/isolate_runtime.dart"),
+ new SystemLibraryPath("uri", "uri/uri.dart"),
+ new SystemLibraryPath("json", "json/json.dart"),
+ new SystemLibraryPath("utf", "utf/utf_vm.dart"),
+ new SystemLibraryPath("utf", "io/io_runtime.dart"),
+ };
+
+ static final SystemLibraryPath[] dartiumImplementation = {
+ new SystemLibraryPath("core", "core/core_runtime.dart"),
+ new SystemLibraryPath("coreimpl", "coreimpl/coreimpl_runtime.dart"),
+ new SystemLibraryPath("isolate", "isolate/isolate_runtime.dart"),
+ // TODO(zundel): get real dartium bindings or interfaces for dom
+ new SystemLibraryPath("dom", "dom/dom_frog.dart"),
+ new SystemLibraryPath("html", "html/html_dartium.dart"),
+ new SystemLibraryPath("uri", "uri/uri.dart"),
+ new SystemLibraryPath("json", "json/json.dart"),
+ new SystemLibraryPath("utf", "utf/utf_vm.dart"),
+ };
+
private static final String DART_SCHEME = "dart";
private static final String DART_SCHEME_SPEC = "dart:";
@@ -63,20 +79,43 @@ public class SystemLibraryManager {
private static final File executionFile = new File(SystemLibraryManager.class
.getProtectionDomain().getCodeSource().getLocation().getPath());
+ public static final String DEFAULT_IMPLEMENTATION = "dartium";
+ public static final String DEFAULT_SDK_PATH = System.getProperty("com.google.dart.sdk", "../");
+
private HashMap<String, String> expansionMap;
private Map<String, SystemLibrary> hostMap;
+ private final String sdkLibPath;
private SystemLibrary[] libraries;
public SystemLibraryManager() {
- setLibraries(getDefaultLibraries());
+ this(DEFAULT_SDK_PATH, DEFAULT_IMPLEMENTATION);
+ }
+
+ public SystemLibraryManager(String dartSdkPath, String implementationName) {
+ this.sdkLibPath = dartSdkPath + "/lib";
+ SystemLibraryPath[] implementationPaths = null;
+ if (implementationName.equals("vm")) {
+ implementationPaths = vmImplementation;
+ } else if (implementationName.equals("frog")) {
+ implementationPaths = frogImplementation;
+ } else if (implementationName.equals("dart2js")) {
+ implementationPaths = dart2JsImplentation;
+ } else if (implementationName.equals("dartium")) {
+ implementationPaths = dartiumImplementation;
+ } else {
+ throw new RuntimeException("Invalid implentation name '" + implementationName +
+ "', expected one of vm, dart2js, frog, dartium");
+ }
+
+ setLibraries(getDefaultLibraries(implementationPaths));
}
/**
* Expand a relative or short URI (e.g. "dart:dom") which is implementation independent to its
* full URI (e.g. "dart://dom/com/google/dart/domlib/dom.dart") and then translate that URI to
- * either a "file:" or "jar:" URI (e.g.
- * "jar:file:/some/install/director/dom.jar!/com/google/dart/domlib/dom.dart").
+ * a "file:" URI (e.g.
+ * "file:/some/install/path/com/google/dart/domlib/dom.dart").
*
* @param uri the original URI
* @return the expanded and translated URI, which may be <code>null</code> and may not exist
@@ -84,12 +123,15 @@ public class SystemLibraryManager {
* library
*/
public URI resolveDartUri(URI uri) {
+ if (uri.isAbsolute()) {
+ return URI.create("file://" + uri.getPath());
+ }
return translateDartUri(expandRelativeDartUri(uri));
}
/**
* Translate the URI from dart://[host]/[pathToLib] (e.g. dart://dom/dom.dart)
- * to either a "file:" or "jar:" URI (e.g. "jar:file:/some/install/director/dom.jar!/dom.dart")
+ * to a "file:" URI (e.g. "file:/some/install/path/dom.dart")
*
* @param uri the original URI
* @return the translated URI, which may be <code>null</code> and may not exist
@@ -112,10 +154,10 @@ public class SystemLibraryManager {
/**
* Expand a relative or short URI (e.g. "dart:dom") which is implementation independent to its
* full URI (e.g. "dart://dom/com/google/dart/domlib/dom.dart").
- *
+ *
* @param uri the relative URI
- * @return the expanded URI
- * or the original URI if it could not be expanded
+ * @return the expanded URI
+ * or the original URI if it could not be expanded
* or null if the uri is of the form "dart:<libname>" but does not correspond to a system library
*/
public URI expandRelativeDartUri(URI uri) throws AssertionError {
@@ -168,148 +210,28 @@ public class SystemLibraryManager {
}
}
- private File getResource(String name, boolean failOnMissing) {
- URL baseUrl = SystemLibraryManager.class.getClassLoader().getResource(name);
- if (baseUrl == null) {
- if (!failOnMissing) {
- return null;
- }
- throw new RuntimeException("Failed to find the system library: " + name);
- }
- return resolveResource(baseUrl, name);
- }
-
- static private File resolveResource(URL baseUrl, String name) {
- if (baseUrl == null) {
- return null;
- }
- File coreDirOrZip = null;
- String protocol = baseUrl.getProtocol();
- String path = baseUrl.getPath();
- if ("file".equals(protocol)) {
- coreDirOrZip = new File(path.substring(0, path.lastIndexOf(name)));
- } else if ("jar".equals(protocol)) {
- // jar:file://www.foo.com/bar/baz.jar!/com/google/some.class
- if (path.startsWith("file:")) {
- int index = path.indexOf('!');
- coreDirOrZip = new File(path.substring(5, index > 0 ? index : path.length()));
- }
- }
- if (coreDirOrZip == null) {
- throw new RuntimeException("Failed to find system library in " + baseUrl);
- }
- if (!coreDirOrZip.exists()) {
- throw new RuntimeException("System library container does not exist " + coreDirOrZip
- + "\n from " + baseUrl);
- }
- return coreDirOrZip;
- }
-
- private File searchForResource(String searchPath, String libraryName, boolean failOnMissing) {
- URL urlPath = null;
- File sourcePath = new File(searchPath);
-
- /* The source can be a directory or a jar file. Search both for our library. */
- if (sourcePath.isDirectory()) {
- File foundLibrary = new File(sourcePath.getPath() + File.separator + libraryName);
- if (!foundLibrary.exists()) {
- if (failOnMissing) {
- throw new RuntimeException("Failed to find system library " + libraryName + " with "
- + sourcePath.toString());
- }
- return null;
- }
- try {
- urlPath = foundLibrary.toURI().toURL();
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- } else if (sourcePath.isFile() && sourcePath.toString().endsWith(".jar")) {
- // Support for jar only right now...
- JarFile jarFile = null;
- try {
- jarFile = new JarFile(sourcePath);
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- if (null != jarFile.getJarEntry(libraryName)) {
- String path = "jar:file:" + sourcePath.getPath() + "!/" + libraryName;
- try {
- urlPath = new URL(path);
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
- } else {
- if (failOnMissing) {
- throw new RuntimeException("Failed to find system library " + libraryName + " with "
- + sourcePath.getPath());
- }
- return null;
- }
- }
-
- File foundLibrary = resolveResource(urlPath, libraryName);
- if (foundLibrary == null && failOnMissing) {
- throw new RuntimeException("Failed to find system library " + libraryName + " with "
- + sourcePath.getPath());
- }
- return foundLibrary;
- }
-
protected SystemLibrary locateSystemLibrary(SystemLibraryPath path) {
- // First, check for jars on the class path
- File libraryDirOrZip = getResource(path.lib, path.failIfMissing);
-
- // TODO(codefu): This is a hack. To keep Eclipse happy and to find the
- // sources, we hard code this path. In the future, when the libraries are
- // all gathered into a common "lib/" path, we can search from there.
- if (libraryDirOrZip == null) {
- // Eclipse's executionPath should be a directory, unless a jar file was included.
- String executionPath;
- if (executionFile.isDirectory()) {
- // Universal location of eclipse workspace to the dart source tree is
- // 'dart/compiler/eclipse.workspace/dartc/output'
- // and we need 'dart/client'
- executionPath =
- executionFile.getParent() + File.separator + ".." + File.separator + ".."
- + File.separator + ".." + File.separator + "client";
- } else {
- executionPath = executionFile.getParent() + File.separator + path.jar;
- }
- libraryDirOrZip = searchForResource(executionPath, path.lib, false);
- if (libraryDirOrZip == null && executionFile.isFile()) {
- // Last ditch; are the artifacts just in a flat file...
- libraryDirOrZip = searchForResource(executionFile.getParent(), path.lib, false);
- }
- }
- if (libraryDirOrZip != null) {
- return new SystemLibrary(path.shortName, path.hostName, path.lib,
- libraryDirOrZip);
+ File lib = new File(sdkLibPath, path.dartSourceFile);
+ if (!lib.exists()) {
+ throw new RuntimeException("Failed to find system library dart:" + path.shortName + " in "
+ + lib);
}
- return null;
+ // TODO(zundel): why pass shortName twice?
+ return new SystemLibrary(path.shortName, path.shortName, lib.getAbsolutePath());
}
-
+
/**
- * Answer the libraries that are built into the compiler jar
+ * Answer the libraries that are built into the system
*/
- protected SystemLibrary[] getDefaultLibraries() {
- ArrayList<SystemLibrary> defaultLibraries = new ArrayList<SystemLibrary>();
- File[] baseFiles = new File[SystemLibraryPath.values().length];
+ protected SystemLibrary[] getDefaultLibraries(SystemLibraryPath[] paths) {
+ List<SystemLibrary> defaultLibraries = new ArrayList<SystemLibrary>();
- for (SystemLibraryPath path : SystemLibraryPath.values()) {
- if (path.base != null) {
- defaultLibraries.add(new SystemLibrary(path.shortName, path.hostName, path.lib,
- baseFiles[path.base.ordinal()]));
- baseFiles[path.ordinal()] = baseFiles[path.base.ordinal()];
- } else {
- SystemLibrary library = locateSystemLibrary(path);
- if (library != null) {
- defaultLibraries.add(library);
- baseFiles[path.ordinal()] = library.getFile();
- }
+ for (SystemLibraryPath path : paths) {
+ SystemLibrary library = locateSystemLibrary(path);
+ if (library != null) {
+ defaultLibraries.add(library);
}
}
-
return defaultLibraries.toArray(new SystemLibrary[defaultLibraries.size()]);
}
}

Powered by Google App Engine
This is Rietveld 408576698