modified the JS compiler so that it can have definitions and library sources specified or discovered on the classpath; so for example we can then discover the kotlin standard library kotlin that needs to be compiled; plus the JS library definitions (which map to the kotlin-lib.js file)

This commit is contained in:
James Strachan
2012-05-30 15:50:06 +01:00
parent ae12b4e5fc
commit 4cff5f9759
10 changed files with 206 additions and 116 deletions
@@ -17,26 +17,17 @@
package org.jetbrains.k2js.test.semantics;
import com.google.common.collect.Lists;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.cli.js.K2JSCompiler;
import org.jetbrains.jet.cli.js.K2JSCompilerArguments;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.facade.MainCallParameters;
import org.jetbrains.k2js.test.SingleFileTranslationTest;
import org.jetbrains.k2js.test.rhino.RhinoFunctionNativeObjectResultChecker;
import org.jetbrains.k2js.utils.JetFileUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.EnumSet;
import java.util.List;
/**
*/
@@ -60,23 +51,18 @@ public final class StdLibToJSTest extends SingleFileTranslationTest {
String... stdLibFiles) throws Exception {
List<String> files = Lists.newArrayList();
File stdlibDir = new File("libraries/stdlib/src");
assertTrue("Cannot find stdlib source: " + stdlibDir, stdlibDir.exists());
for (String file : stdLibFiles) {
files.add(new File(stdlibDir, file).getPath());
}
generateJavaScriptFiles(files, getTestName(false) + ".kt", mainCallParameters, ecmaVersions);
/*
runRhinoTests(getOutputFilePaths(kotlinFilename, ecmaVersions),
new RhinoFunctionNativeObjectResultChecker("test.browser", "foo", "Some Dynamically Created Content!!!"));
*/
// lets add the standard JS library files
for (String libFileName : Config.LIB_FILE_NAMES) {
System.out.println("Compiling " + libFileName);
files.add(Config.LIBRARIES_LOCATION + libFileName);
}
// now lets try invoke the compiler
for (EcmaVersion version : ecmaVersions) {
System.out.println("Compiling with version: " + version);
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.config;
import org.jetbrains.annotations.NotNull;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.psi.JetFile;
import java.util.*;
/**
* A Config implementation which is configured with a directory to find the standard library names from
*/
public class ClassPathLibraryDefintionsConfig extends Config {
public static final String META_INF_SERVICES_FILE = "META-INF/services/org.jetbrains.kotlin.js.libraryDefinitions";
public ClassPathLibraryDefintionsConfig(@NotNull Project project, @NotNull EcmaVersion version) {
super(project, version);
}
@NotNull
@Override
public List<JetFile> generateLibFiles() {
return MetaInfServices.loadServicesFiles(META_INF_SERVICES_FILE, getProject());
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2010-2012 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.k2js.config;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetFile;
import java.util.*;
/**
* A helper class to load the kotlin library sources to be compiled to JavaScript as part of a JavaScript build
*/
public class ClassPathLibrarySourcesLoader {
public static final String META_INF_SERVICES_FILE = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
@NotNull private final Project project;
public ClassPathLibrarySourcesLoader(@NotNull Project project) {
this.project = project;
}
@NotNull
public List<JetFile> findSourceFiles() {
return MetaInfServices.loadServicesFiles(META_INF_SERVICES_FILE, project);
}
}
@@ -16,13 +16,9 @@
package org.jetbrains.k2js.config;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.k2js.config.Config;
import org.jetbrains.k2js.config.EcmaVersion;
import org.jetbrains.k2js.utils.JetFileUtils;
import java.io.BufferedReader;
@@ -33,34 +29,24 @@ import java.net.URL;
import java.util.*;
/**
* A Config implementation which is configured with a directory to find the standard library names from
* A helper class to discover a META-INF/services file on the classpath and load the files referenced inside it
*/
public class ClassPathLibrarySourcesConfig extends Config {
public static final String LIBRARY_SOURCES_FILES = "META-INF/services/org.jetbrains.kotlin.js.librarySource";
@Nullable
private /*var*/ List<JetFile> jsLibFiles = null;
public ClassPathLibrarySourcesConfig(@NotNull Project project, @NotNull EcmaVersion version) {
super(project, version);
}
@NotNull
private List<JetFile> initLibFiles(@NotNull Project project) {
public class MetaInfServices {
public static List<JetFile> loadServicesFiles(String metaInfServicesFile, Project project) {
List<JetFile> libFiles = new ArrayList<JetFile>();
Set<URL> urlsLoaded = new HashSet<URL>();
try {
Enumeration<URL> resources = getClass().getClassLoader().getResources(LIBRARY_SOURCES_FILES);
loadLibFiles(resources, project, urlsLoaded, libFiles);
resources = Thread.currentThread().getContextClassLoader().getResources(LIBRARY_SOURCES_FILES);
loadLibFiles(resources, project, urlsLoaded, libFiles);
Enumeration<URL> resources = MetaInfServices.class.getClassLoader().getResources(metaInfServicesFile);
loadLibFiles(resources, urlsLoaded, libFiles, project);
resources = Thread.currentThread().getContextClassLoader().getResources(metaInfServicesFile);
loadLibFiles(resources, urlsLoaded, libFiles, project);
} catch (IOException e) {
throw new IllegalStateException(e);
}
return libFiles;
}
private void loadLibFiles(Enumeration<URL> resources, @NotNull Project project, Set<URL> urlsLoaded, List<JetFile> libFiles) throws IOException {
protected static void loadLibFiles(Enumeration<URL> resources, Set<URL> urlsLoaded, List<JetFile> libFiles, Project project) throws IOException {
while (resources != null && resources.hasMoreElements()) {
URL url = resources.nextElement();
if (url != null) {
@@ -84,6 +70,7 @@ public class ClassPathLibrarySourcesConfig extends Config {
String text = FileUtil.loadTextAndClose(stream);
JetFile file = JetFileUtils.createPsiFile(line, text, project);
if (file != null) {
//System.out.println("Parsing file: " + text);
libFiles.add(file);
}
}
@@ -102,19 +89,10 @@ public class ClassPathLibrarySourcesConfig extends Config {
* Tries to load the given resource name on the classpath
*/
public static InputStream loadClasspathResource(String resourceName) {
InputStream answer = ClassPathLibrarySourcesConfig.class.getClassLoader().getResourceAsStream(resourceName);
InputStream answer = ClassPathLibraryDefintionsConfig.class.getClassLoader().getResourceAsStream(resourceName);
if (answer == null) {
answer = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
}
return answer;
}
@Override
@NotNull
public List<JetFile> generateLibFiles() {
if (jsLibFiles == null) {
jsLibFiles = initLibFiles(getProject());
}
return jsLibFiles;
}
}
@@ -48,6 +48,7 @@ public class ZippedLibrarySourcesConfig extends Config {
@NotNull
@Override
public List<JetFile> generateLibFiles() {
System.out.println("Parsing JS library source zip: " + pathToLibZip);
if (pathToLibZip == null) {
return Collections.emptyList();
}
@@ -62,6 +63,8 @@ public class ZippedLibrarySourcesConfig extends Config {
}
}
catch (IOException e) {
System.out.println("Failed to process " + pathToLibZip + ". Reason: " + e);
e.printStackTrace();
return Collections.emptyList();
}
}
@@ -76,6 +79,7 @@ public class ZippedLibrarySourcesConfig extends Config {
InputStream stream = file.getInputStream(entry);
String text = FileUtil.loadTextAndClose(stream);
JetFile jetFile = JetFileUtils.createPsiFile(entry.getName(), text, getProject());
System.out.println("Parsing file: " + entry.getName());
result.add(jetFile);
}
}
@@ -189,7 +189,6 @@ public final class BindingUtils {
public static DeclarationDescriptor getDescriptorForReferenceExpression(@NotNull BindingContext context,
@NotNull JetReferenceExpression reference) {
DeclarationDescriptor referencedDescriptor = getNullableDescriptorForReferenceExpression(context, reference);
assert referencedDescriptor != null : "Reference expression must reference a descriptor.";
Preconditions.checkNotNull(referencedDescriptor, "Reference expression must reference a descriptor for reference %s at %s", reference.getText(),
DiagnosticUtils.atLocation(reference));
return referencedDescriptor;