diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 20232350b50..be374de2dd3 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -10,14 +10,19 @@
-
+
-
+
+
+
+
+
-
+
+
@@ -275,7 +280,7 @@
-
+
@@ -338,7 +343,7 @@
-
+
@@ -375,74 +380,13 @@
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
@@ -450,16 +394,81 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -490,14 +499,6 @@
@@ -776,47 +785,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -838,29 +807,15 @@
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -878,43 +833,31 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1020,100 +963,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -1170,6 +1020,96 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1339,11 +1279,11 @@
-
-
-
-
-
+
+
+
+
+
localhost
@@ -1412,7 +1352,7 @@
-
+
@@ -1432,7 +1372,7 @@
-
+
@@ -1479,7 +1419,7 @@
-
+
@@ -1492,7 +1432,6 @@
-
@@ -1517,6 +1456,7 @@
+
@@ -1526,117 +1466,118 @@
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
-
+
-
+
-
+
-
-
-
-
-
-
-
-
-
-
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
diff --git a/translator/src/org/jetbrains/k2js/K2JSTranslator.java b/translator/src/org/jetbrains/k2js/K2JSTranslator.java
index e14c2c995f3..44141c4fe89 100644
--- a/translator/src/org/jetbrains/k2js/K2JSTranslator.java
+++ b/translator/src/org/jetbrains/k2js/K2JSTranslator.java
@@ -12,7 +12,6 @@ import com.intellij.psi.impl.PsiFileFactoryImpl;
import com.intellij.testFramework.LightVirtualFile;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
-import org.jetbrains.jet.compiler.CompileEnvironment;
import org.jetbrains.jet.compiler.JetCoreEnvironment;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.psi.JetFile;
@@ -34,6 +33,8 @@ import java.util.StringTokenizer;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getNamespaceDescriptor;
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getNameForNamespace;
+//TODO: clean up the code
+
/**
* @author Pavel Talanov
*/
@@ -72,13 +73,29 @@ public final class K2JSTranslator {
}
public void translateFile(@NotNull String inputFile, @NotNull String outputFile) throws Exception {
- JetFile PsiFile = loadPsiFile(inputFile);
+ JetFile psiFile = loadPsiFile(inputFile);
// includeRtJar();
- JsProgram program = generateProgram(PsiFile);
+ JsProgram program = generateProgram(Arrays.asList(psiFile));
CodeGenerator generator = new CodeGenerator();
generator.generateToFile(program, new File(outputFile));
}
+ public void translateFiles(@NotNull List inputFiles, @NotNull String outputFile) throws Exception {
+ List psiFiles = createPsiFileList(inputFiles);
+ JsProgram program = generateProgram(psiFiles);
+ CodeGenerator generator = new CodeGenerator();
+ generator.generateToFile(program, new File(outputFile));
+ }
+
+ @NotNull
+ private List createPsiFileList(@NotNull List inputFiles) {
+ List psiFiles = new ArrayList();
+ for (String inputFile : inputFiles) {
+ psiFiles.add(loadPsiFile(inputFile));
+ }
+ return psiFiles;
+ }
+
@NotNull
public String translateStringWithCallToMain(@NotNull String programText, @NotNull String argumentsString) {
JetFile file = createPsiFile("test", programText);
@@ -91,37 +108,34 @@ public final class K2JSTranslator {
@NotNull
private String generateProgramCode(@NotNull JetFile psiFile) {
- JsProgram program = generateProgram(psiFile);
+ JsProgram program = generateProgram(Arrays.asList(psiFile));
CodeGenerator generator = new CodeGenerator();
return generator.generateToString(program);
}
@NotNull
- private JsProgram generateProgram(@NotNull JetFile psiFile) {
+ private JsProgram generateProgram(@NotNull List psiFiles) {
List files = getJsSupportStdLib();
- files.add(psiFile);
- bindingContext = AnalyzerFacade.analyzeFilesWithJavaIntegration(psiFile.getProject(), files, new Predicate() {
- @Override
- public boolean apply(@Nullable PsiFile file) {
- for (String libFileName : LIB_FILE_NAMES) {
- if (libFileName.contains(file.getName().substring(0, file.getName().lastIndexOf('.')))) {
- return false;
+ files.addAll(psiFiles);
+ bindingContext = AnalyzerFacade.analyzeFilesWithJavaIntegration
+ (psiFiles.iterator().next().getProject(), files, new Predicate() {
+ @Override
+ public boolean apply(@Nullable PsiFile file) {
+ for (String libFileName : LIB_FILE_NAMES) {
+ if (libFileName.contains(file.getName().substring(0, file.getName().lastIndexOf('.')))) {
+ return false;
+ }
+ }
+ return true;
}
- }
- return true;
- }
- }, JetControlFlowDataTraceFactory.EMPTY);
+ }, JetControlFlowDataTraceFactory.EMPTY);
+ for (PsiFile file : psiFiles) {
+ AnalyzingUtils.checkForSyntacticErrors(file);
+ AnalyzingUtils.throwExceptionOnErrors(bindingContext);
+ }
assert bindingContext != null;
- AnalyzingUtils.checkForSyntacticErrors(psiFile);
- AnalyzingUtils.throwExceptionOnErrors(bindingContext);
-
- return Translation.generateAst(bindingContext, psiFile, environment.getProject());
- }
-
- private void includeRtJar() {
- final File rtJar = CompileEnvironment.findRtJar();
- environment.addToClasspath(rtJar);
+ return Translation.generateAst(bindingContext, psiFiles.iterator().next(), environment.getProject());
}
@NotNull
diff --git a/translator/test/org/jetbrains/k2js/test/MultiFileTest.java b/translator/test/org/jetbrains/k2js/test/MultiFileTest.java
new file mode 100644
index 00000000000..82453dc0a37
--- /dev/null
+++ b/translator/test/org/jetbrains/k2js/test/MultiFileTest.java
@@ -0,0 +1,31 @@
+package org.jetbrains.k2js.test;
+
+import org.junit.Test;
+
+/**
+ * @author Pavel Talanov
+ */
+public final class MultiFileTest extends TranslationTest {
+
+ final private static String MAIN = "multiFile/";
+
+ @Override
+ protected String mainDirectory() {
+ return MAIN;
+ }
+
+ @Test
+ public void functionsVisibleFromOtherFile() throws Exception {
+ testFooBoxIsTrue("functionsVisibleFromOtherFile");
+ }
+
+ @Test
+ public void classesInheritedFromOtherFile() throws Exception {
+ testFooBoxIsTrue("classesInheritedFromOtherFile");
+ }
+
+ @Override
+ protected void testFooBoxIsTrue(String dirName) throws Exception {
+ testMultiFile(dirName, "foo", "box", true);
+ }
+}
diff --git a/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java
index 27c5e631b17..a86499df84f 100644
--- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java
+++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java
@@ -11,9 +11,11 @@ import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
+//TODO: spread the test* methods amongst classes that actually use them
/**
* @author Pavel Talanov
@@ -71,10 +73,28 @@ public abstract class TranslationTest {
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
}
+ protected void testMultiFile(String dirName, String namespaceName,
+ String functionName, Object expectedResult) throws Exception {
+ translateFilesInDir(dirName);
+ runRhinoTest(generateFilenameList(getOutputFilePath(dirName + ".kt")),
+ new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
+ }
+
protected void translateFile(String filename) throws Exception {
(new K2JSTranslator()).translateFile(getInputFilePath(filename), getOutputFilePath(filename));
}
+ protected void translateFilesInDir(String dirName) throws Exception {
+ String dirPath = getInputFilePath(dirName);
+ File dir = new File(dirPath);
+ List fullFilePaths = new ArrayList();
+ for (String fileName : dir.list()) {
+ fullFilePaths.add(getInputFilePath(dirName) + "\\" + fileName);
+ }
+ assert dir.isDirectory();
+ (new K2JSTranslator()).translateFiles(fullFilePaths, getOutputFilePath(dirName + ".kt"));
+ }
+
protected List generateFilenameList(String inputFile) {
return Arrays.asList(kotlinLibraryPath(), inputFile);
}
diff --git a/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/a.kt b/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/a.kt
new file mode 100644
index 00000000000..17884a1a7a1
--- /dev/null
+++ b/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/a.kt
@@ -0,0 +1,9 @@
+package foo
+
+open class A() {
+ open fun f() = 3;
+}
+
+open class C() : B() {
+ override fun f() = 5
+}
\ No newline at end of file
diff --git a/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/b.kt b/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/b.kt
new file mode 100644
index 00000000000..e12163d316d
--- /dev/null
+++ b/translator/testFiles/multiFile/cases/classesInheritedFromOtherFile/b.kt
@@ -0,0 +1,7 @@
+package foo
+
+open class B() : A() {
+ override fun f() = 4
+}
+
+fun box() = (A().f() == 3) && (B().f() == 4) && (C().f() == 5)
\ No newline at end of file
diff --git a/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/a.kt b/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/a.kt
new file mode 100644
index 00000000000..343d72abff4
--- /dev/null
+++ b/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/a.kt
@@ -0,0 +1,3 @@
+package foo
+
+fun f() = 3;
\ No newline at end of file
diff --git a/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/b.kt b/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/b.kt
new file mode 100644
index 00000000000..4fd867b6a42
--- /dev/null
+++ b/translator/testFiles/multiFile/cases/functionsVisibleFromOtherFile/b.kt
@@ -0,0 +1,3 @@
+package foo
+
+fun box() = (f() == 3)
\ No newline at end of file