Chromium Code Reviews| 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..ed4a62307300397f2588a490d56b9a12469a411f 100644 |
| --- a/compiler/java/com/google/dart/compiler/SystemLibraryManager.java |
| +++ b/compiler/java/com/google/dart/compiler/SystemLibraryManager.java |
| @@ -4,57 +4,75 @@ |
| package com.google.dart.compiler; |
| +import com.google.dart.compiler.util.Lists; |
| + |
| 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[] legImplementation = { |
| + new SystemLibraryPath("core", "core/core_frog.dart"), |
|
ahe
2012/03/20 21:34:09
Leg doesn't use the same libraries as Frog. You ca
zundel
2012/03/25 16:39:02
We wukk need to update create_sdk.py to copy the l
|
| + 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:"; |
| @@ -65,11 +83,26 @@ public class SystemLibraryManager { |
| private HashMap<String, String> expansionMap; |
| private Map<String, SystemLibrary> hostMap; |
| + private final String sdkLibPath; |
| private SystemLibrary[] libraries; |
| - public SystemLibraryManager() { |
| - setLibraries(getDefaultLibraries()); |
| + 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("leg")) { |
|
ahe
2012/03/20 21:34:09
Would you mind renaming this to dart2js?
zundel
2012/03/25 16:39:02
Done.
|
| + implementationPaths = legImplementation; |
| + } else if (implementationName.equals("dartium")) { |
| + implementationPaths = dartiumImplementation; |
| + } else { |
| + throw new RuntimeException("Invalid implentation name '" + implementationName + "', expected one of vm, frog, leg, dartium"); |
|
ahe
2012/03/20 21:34:09
Ditto.
zundel
2012/03/25 16:39:02
Done.
|
| + } |
| + |
| + setLibraries(getDefaultLibraries(implementationPaths)); |
| } |
| /** |
| @@ -84,6 +117,9 @@ public class SystemLibraryManager { |
| * library |
| */ |
| public URI resolveDartUri(URI uri) { |
| + if (uri.isAbsolute()) { |
| + return URI.create("file://" + uri.getPath()); |
|
ahe
2012/03/20 21:34:09
Indentation.
zundel
2012/03/25 16:39:02
Done.
|
| + } |
| return translateDartUri(expandRelativeDartUri(uri)); |
| } |
| @@ -112,10 +148,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 +204,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(String.format("%s/%s", sdkLibPath, path.dartSourceFile)); |
|
ahe
2012/03/20 21:34:09
Probably better to write:
new File(sdkLibPath, pa
zundel
2012/03/25 16:39:02
Done.
|
| + 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()]); |
| } |
| } |