temporary commit.
This commit is contained in:
+1
@@ -3,5 +3,6 @@
|
||||
<option name="myName" value="Project Default" />
|
||||
<option name="myLocal" value="false" />
|
||||
<inspection_tool class="SimplifiableIfStatement" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
<inspection_tool class="TrivialIf" enabled="false" level="WARNING" enabled_by_default="false" />
|
||||
</profile>
|
||||
</component>
|
||||
Generated
+2
@@ -2,12 +2,14 @@
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/../../Kotlin/jet/compiler/backend/backend.iml" filepath="$PROJECT_DIR$/../../Kotlin/jet/compiler/backend/backend.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../../Kotlin/jet/compiler/frontend/frontend.iml" filepath="$PROJECT_DIR$/../../Kotlin/jet/compiler/frontend/frontend.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../Kotlin/jet/compiler/frontend/frontend.iml" filepath="$PROJECT_DIR$/../Kotlin/jet/compiler/frontend/frontend.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../../Kotlin/jet/compiler/frontend.java/frontend.java.iml" filepath="$PROJECT_DIR$/../../Kotlin/jet/compiler/frontend.java/frontend.java.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../Kotlin/jet/compiler/frontend.java/frontend.java.iml" filepath="$PROJECT_DIR$/../Kotlin/jet/compiler/frontend.java/frontend.java.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/examples/jetTests.iml" filepath="$PROJECT_DIR$/examples/jetTests.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/js/js.iml" filepath="$PROJECT_DIR$/js/js.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/../../Kotlin/jet/stdlib/stdlib.iml" filepath="$PROJECT_DIR$/../../Kotlin/jet/stdlib/stdlib.iml" />
|
||||
<module fileurl="file://$PROJECT_DIR$/translator/translator.iml" filepath="$PROJECT_DIR$/translator/translator.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
|
||||
Generated
+508
-824
File diff suppressed because it is too large
Load Diff
@@ -1,213 +1,111 @@
|
||||
package org.jetbrains.k2js;
|
||||
|
||||
import com.google.common.base.Predicates;
|
||||
import com.google.common.collect.LinkedHashMultimap;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Multimap;
|
||||
import com.google.dart.compiler.backend.js.ast.JsProgram;
|
||||
import com.intellij.core.JavaCoreEnvironment;
|
||||
import com.intellij.openapi.Disposable;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.vfs.VirtualFile;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.openapi.util.text.StringUtil;
|
||||
import com.intellij.openapi.vfs.CharsetToolkit;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.psi.PsiElementVisitor;
|
||||
import com.intellij.psi.PsiFile;
|
||||
import com.intellij.psi.PsiManager;
|
||||
import com.sampullara.cli.Argument;
|
||||
import com.intellij.psi.PsiFileFactory;
|
||||
import com.intellij.psi.impl.PsiFileFactoryImpl;
|
||||
import com.intellij.testFramework.LightVirtualFile;
|
||||
import org.jetbrains.annotations.NonNls;
|
||||
import org.jetbrains.jet.JetCoreEnvironment;
|
||||
import org.jetbrains.jet.compiler.CompileEnvironment;
|
||||
import org.jetbrains.jet.compiler.CompileEnvironmentException;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
|
||||
import org.jetbrains.jet.lang.diagnostics.DiagnosticWithTextRange;
|
||||
import org.jetbrains.jet.lang.diagnostics.Severity;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetFile;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports;
|
||||
import org.jetbrains.k2js.generate.CodeGenerator;
|
||||
import org.jetbrains.k2js.translate.general.GenerationState;
|
||||
import org.jetbrains.jet.plugin.JetLanguage;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
import org.jetbrains.k2js.utils.JetTestUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Pavel.Talanov
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
public final class K2JSTranslator {
|
||||
|
||||
public class K2JSTranslator {
|
||||
public static class Arguments {
|
||||
@Argument(value = "output", description = "output directory")
|
||||
public String outputDir;
|
||||
@Argument(value = "src", description = "source file or directory")
|
||||
public String src;
|
||||
private final JetCoreEnvironment myEnvironment = new JetCoreEnvironment(new Disposable() {
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
//To change body of implemented methods use File | Settings | File Templates.
|
||||
}
|
||||
});
|
||||
|
||||
public K2JSTranslator() {
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.setProperty("java.awt.headless", "true");
|
||||
Arguments arguments = new Arguments();
|
||||
arguments.src = "test_files\\test_cases\\test.kt";
|
||||
arguments.outputDir = null;
|
||||
translate(arguments);
|
||||
public void translate(String inputFile, String outputFile) {
|
||||
|
||||
try {
|
||||
|
||||
final File rtJar = CompileEnvironment.findRtJar(true);
|
||||
myEnvironment.addToClasspath(rtJar);
|
||||
|
||||
JetNamespace namespace = loadPsiFile(inputFile).getRootNamespace();
|
||||
|
||||
BindingContext bindingContext = JetTestUtils.analyzeNamespace(namespace,
|
||||
JetControlFlowDataTraceFactory.EMPTY);
|
||||
|
||||
NamespaceDescriptor namespaceDescriptor = BindingUtils.getNamespaceDescriptor(bindingContext, namespace);
|
||||
namespaceDescriptor.getMemberScope().getAllDescriptors();
|
||||
|
||||
} catch (CompileEnvironmentException e) {
|
||||
System.out.println(e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
public static void translate(Arguments arguments) {
|
||||
Disposable root = new Disposable() {
|
||||
protected String loadFile(String path) throws IOException {
|
||||
return doLoadFile(path);
|
||||
}
|
||||
|
||||
protected String doLoadFile(String path) throws IOException {
|
||||
String text = FileUtil.loadFile(new File(path), CharsetToolkit.UTF8).trim();
|
||||
text = StringUtil.convertLineSeparators(text);
|
||||
return text;
|
||||
}
|
||||
|
||||
protected JetFile createPsiFile(String name, String text) {
|
||||
return (JetFile) createFile(name + ".jet", text);
|
||||
}
|
||||
|
||||
protected JetFile loadPsiFile(String name) {
|
||||
try {
|
||||
return createPsiFile(name, loadFile(name));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
protected PsiFile createFile(@NonNls String name, String text) {
|
||||
LightVirtualFile virtualFile = new LightVirtualFile(name, JetLanguage.INSTANCE, text);
|
||||
virtualFile.setCharset(CharsetToolkit.UTF8_CHARSET);
|
||||
return ((PsiFileFactoryImpl) PsiFileFactory.getInstance(myEnvironment.getProject())).trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
|
||||
}
|
||||
|
||||
protected void ensureParsed(PsiFile file) {
|
||||
file.accept(new PsiElementVisitor() {
|
||||
@Override
|
||||
public void dispose() {
|
||||
public void visitElement(PsiElement element) {
|
||||
element.acceptChildren(this);
|
||||
}
|
||||
};
|
||||
|
||||
JetCoreEnvironment environment = new JetCoreEnvironment(root);
|
||||
|
||||
File rtJar = initJdk();
|
||||
if (rtJar == null) return;
|
||||
environment.addToClasspath(rtJar);
|
||||
|
||||
VirtualFile vFile = environment.getLocalFileSystem().findFileByPath(arguments.src);
|
||||
if (vFile == null) {
|
||||
System.out.print("File/directory not found: " + arguments.src);
|
||||
return;
|
||||
}
|
||||
|
||||
Project project = environment.getProject();
|
||||
GenerationState generationState = new GenerationState(project);
|
||||
List<JetNamespace> namespaces = Lists.newArrayList();
|
||||
if (vFile.isDirectory()) {
|
||||
File dir = new File(vFile.getPath());
|
||||
addFiles(environment, project, namespaces, dir);
|
||||
} else {
|
||||
PsiFile psiFile = PsiManager.getInstance(project).findFile(vFile);
|
||||
if (psiFile instanceof JetFile) {
|
||||
JetNamespace rootNamespace = ((JetFile) psiFile).getRootNamespace();
|
||||
AnalyzingUtils.checkForSyntacticErrors(rootNamespace);
|
||||
namespaces.add(rootNamespace);
|
||||
} else {
|
||||
System.out.print("Not a Kotlin file: " + vFile.getPath());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS)
|
||||
.analyzeNamespaces(project, namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
|
||||
|
||||
AnalyzingUtils.throwExceptionOnErrors(bindingContext);
|
||||
|
||||
ErrorCollector errorCollector = new ErrorCollector(bindingContext);
|
||||
errorCollector.report();
|
||||
|
||||
if (!errorCollector.hasErrors) {
|
||||
// Translate generated psi
|
||||
CodeGenerator generator = new CodeGenerator();
|
||||
JsProgram program = generationState.compileCorrectNamespaces(bindingContext, namespaces);
|
||||
if (arguments.outputDir == null) {
|
||||
generator.generateToConsole(program);
|
||||
} else {
|
||||
File outputFile = new File(arguments.outputDir);
|
||||
try {
|
||||
generator.generateToFile(program, outputFile);
|
||||
} catch (IOException e) {
|
||||
System.out.println("Failed to write to specified file");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
private static class ErrorCollector {
|
||||
Multimap<PsiFile, DiagnosticWithTextRange> maps = LinkedHashMultimap.<PsiFile, DiagnosticWithTextRange>create();
|
||||
|
||||
boolean hasErrors;
|
||||
|
||||
public ErrorCollector(BindingContext bindingContext) {
|
||||
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
|
||||
report(diagnostic);
|
||||
}
|
||||
}
|
||||
|
||||
private void report(Diagnostic diagnostic) {
|
||||
hasErrors |= diagnostic.getSeverity() == Severity.ERROR;
|
||||
if (diagnostic instanceof DiagnosticWithTextRange) {
|
||||
DiagnosticWithTextRange diagnosticWithTextRange = (DiagnosticWithTextRange) diagnostic;
|
||||
maps.put(diagnosticWithTextRange.getPsiFile(), diagnosticWithTextRange);
|
||||
} else {
|
||||
System.out.println(diagnostic.getSeverity().toString() + ": " + diagnostic.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
void report() {
|
||||
if (!maps.isEmpty()) {
|
||||
for (PsiFile psiFile : maps.keySet()) {
|
||||
System.out.println(psiFile.getVirtualFile().getPath());
|
||||
Collection<DiagnosticWithTextRange> diagnosticWithTextRanges = maps.get(psiFile);
|
||||
for (DiagnosticWithTextRange diagnosticWithTextRange : diagnosticWithTextRanges) {
|
||||
String position = DiagnosticUtils.formatPosition(diagnosticWithTextRange);
|
||||
System.out.println("\t" + diagnosticWithTextRange.getSeverity().toString() + ": " + position + " " + diagnosticWithTextRange.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
protected void prepareForTest(String name) throws IOException {
|
||||
// String text = loadFile(name + ".jet");
|
||||
// createAndCheckPsiFile(name, text);
|
||||
}
|
||||
|
||||
private static void addFiles(JavaCoreEnvironment environment, Project project, List<JetNamespace> namespaces, File dir) {
|
||||
for (File file : dir.listFiles()) {
|
||||
if (!file.isDirectory()) {
|
||||
VirtualFile virtualFile = environment.getLocalFileSystem().findFileByPath(file.getAbsolutePath());
|
||||
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
|
||||
if (psiFile instanceof JetFile) {
|
||||
namespaces.add(((JetFile) psiFile).getRootNamespace());
|
||||
}
|
||||
} else {
|
||||
addFiles(environment, project, namespaces, file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static File initJdk() {
|
||||
String javaHome = System.getenv("JAVA_HOME");
|
||||
File rtJar = null;
|
||||
if (javaHome == null) {
|
||||
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
|
||||
if (systemClassLoader instanceof URLClassLoader) {
|
||||
URLClassLoader loader = (URLClassLoader) systemClassLoader;
|
||||
for (URL url : loader.getURLs()) {
|
||||
if ("file".equals(url.getProtocol())) {
|
||||
if (url.getFile().endsWith("/lib/rt.jar")) {
|
||||
rtJar = new File(url.getFile());
|
||||
break;
|
||||
}
|
||||
if (url.getFile().endsWith("/Classes/classes.jar")) {
|
||||
rtJar = new File(url.getFile()).getAbsoluteFile();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (rtJar == null) {
|
||||
System.out.println("JAVA_HOME environment variable needs to be defined");
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
rtJar = findRtJar(javaHome);
|
||||
}
|
||||
|
||||
if (rtJar == null || !rtJar.exists()) {
|
||||
System.out.print("No rt.jar found under JAVA_HOME=" + javaHome);
|
||||
return null;
|
||||
}
|
||||
return rtJar;
|
||||
}
|
||||
|
||||
private static File findRtJar(String javaHome) {
|
||||
File rtJar = new File(javaHome, "jre/lib/rt.jar");
|
||||
if (rtJar.exists()) {
|
||||
return rtJar;
|
||||
}
|
||||
return null;
|
||||
protected void createAndCheckPsiFile(String name, String text) {
|
||||
//createCheckAndReturnPsiFile(name, text);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -7,12 +7,13 @@ import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyDescriptor;
|
||||
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
|
||||
import org.jetbrains.jet.lang.types.JetStandardLibrary;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.jetbrains.jet.resolve.DescriptorRenderer.getFQName;
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getFQName;
|
||||
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.*;
|
||||
|
||||
/**
|
||||
@@ -20,21 +21,28 @@ import static org.jetbrains.k2js.translate.utils.DescriptorUtils.*;
|
||||
*/
|
||||
public final class StandardClasses {
|
||||
|
||||
//TODO: move declaration code to some kind of builder
|
||||
@NotNull
|
||||
public static StandardClasses bindImplementations(@NotNull JetStandardLibrary standardLibrary,
|
||||
@NotNull JsScope kotlinObjectScope) {
|
||||
StandardClasses standardClasses = new StandardClasses(kotlinObjectScope);
|
||||
|
||||
bindArray(standardClasses, standardLibrary);
|
||||
ClassDescriptor iteratorClass = (ClassDescriptor)
|
||||
standardLibrary.getLibraryScope().getClassifier("Iterator");
|
||||
assert iteratorClass != null;
|
||||
bindIterator(standardClasses, iteratorClass);
|
||||
|
||||
declareArray(standardClasses, standardLibrary);
|
||||
declareIterator(standardClasses, standardLibrary);
|
||||
declareJavaArrayList(standardClasses);
|
||||
declareJavaSystem(standardClasses);
|
||||
return standardClasses;
|
||||
}
|
||||
|
||||
private static void declareJavaSystem(@NotNull StandardClasses standardClasses) {
|
||||
String systemFQName = "<java_root>.java.lang.System";
|
||||
standardClasses.declareStandardTopLevelObject(systemFQName, "System");
|
||||
declareMethods(standardClasses, systemFQName, "out");
|
||||
String printStreamFQName = "<java_root>.java.io.PrintStream";
|
||||
//TODO:
|
||||
standardClasses.declareStandardTopLevelObject(printStreamFQName, "ErrorName");
|
||||
declareMethods(standardClasses, printStreamFQName, "print", "println");
|
||||
}
|
||||
|
||||
private static void declareJavaArrayList(@NotNull StandardClasses standardClasses) {
|
||||
String arrayListFQName = "<java_root>.java.util.ArrayList";
|
||||
standardClasses.declareStandardTopLevelObject(arrayListFQName, "ArrayList");
|
||||
@@ -43,14 +51,17 @@ public final class StandardClasses {
|
||||
"isEmpty", "set", "remove", "addAll");
|
||||
}
|
||||
|
||||
private static void bindIterator(@NotNull StandardClasses standardClasses,
|
||||
@NotNull ClassDescriptor iteratorClass) {
|
||||
private static void declareIterator(@NotNull StandardClasses standardClasses,
|
||||
@NotNull JetStandardLibrary standardLibrary) {
|
||||
ClassDescriptor iteratorClass = (ClassDescriptor)
|
||||
standardLibrary.getLibraryScope().getClassifier("Iterator");
|
||||
assert iteratorClass != null;
|
||||
standardClasses.declareStandardTopLevelObject(iteratorClass, "ArrayIterator");
|
||||
declareMethods(standardClasses, getFQName(iteratorClass), "next", "hasNext");
|
||||
}
|
||||
|
||||
private static void bindArray(@NotNull StandardClasses standardClasses,
|
||||
@NotNull JetStandardLibrary standardLibrary) {
|
||||
private static void declareArray(@NotNull StandardClasses standardClasses,
|
||||
@NotNull JetStandardLibrary standardLibrary) {
|
||||
ClassDescriptor arrayClass = standardLibrary.getArray();
|
||||
standardClasses.declareStandardTopLevelObject(arrayClass, "Array");
|
||||
FunctionDescriptor nullConstructorFunction = getFunctionByName(standardLibrary.getLibraryScope(), "Array");
|
||||
@@ -85,7 +96,7 @@ public final class StandardClasses {
|
||||
|
||||
private void declareStandardTopLevelObject(@NotNull DeclarationDescriptor descriptor,
|
||||
@NotNull String kotlinLibName) {
|
||||
declareStandardTopLevelObject(getFQName(descriptor), kotlinLibName);
|
||||
declareStandardTopLevelObject(DescriptorUtils.getFQName(descriptor), kotlinLibName);
|
||||
}
|
||||
|
||||
private void declareStandardTopLevelObject(@NotNull String fullQualifiedName, @NotNull String kotlinLibName) {
|
||||
@@ -95,7 +106,7 @@ public final class StandardClasses {
|
||||
|
||||
private void declareStandardInnerDeclaration(@NotNull DeclarationDescriptor descriptor,
|
||||
@NotNull String kotlinLibName) {
|
||||
String containingFQName = getFQName(getContainingDeclaration(descriptor));
|
||||
String containingFQName = DescriptorUtils.getFQName(getContainingDeclaration(descriptor));
|
||||
declareStandardInnerDeclaration(containingFQName, descriptor.getName(), kotlinLibName);
|
||||
}
|
||||
|
||||
|
||||
@@ -6,10 +6,11 @@ import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
|
||||
import org.jetbrains.jet.lang.descriptors.PropertyAccessorDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.*;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.resolve.DescriptorRenderer;
|
||||
import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getFQName;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
@@ -105,7 +106,7 @@ public final class TranslationContext {
|
||||
if (staticContext.isDeclared(descriptor)) {
|
||||
return staticContext.getGlobalName(descriptor);
|
||||
}
|
||||
throw new AssertionError("Undefined descriptor: " + DescriptorRenderer.getFQName(descriptor));
|
||||
throw new AssertionError("Undefined descriptor: " + getFQName(descriptor));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
||||
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
package org.jetbrains.k2js.translate.general;
|
||||
|
||||
import com.google.dart.compiler.backend.js.ast.JsProgram;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.k2js.translate.utils.BindingUtils;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
//TODO: rework this class and the outer API
|
||||
public final class GenerationState {
|
||||
|
||||
@NotNull
|
||||
private final Project project;
|
||||
|
||||
public GenerationState(@NotNull Project project) {
|
||||
this.project = project;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public JsProgram compileCorrectNamespaces(@NotNull BindingContext bindingContext,
|
||||
@NotNull List<JetNamespace> namespaces) {
|
||||
//TODO hardcoded
|
||||
JetNamespace namespace = namespaces.get(0);
|
||||
NamespaceDescriptor descriptor = BindingUtils.getNamespaceDescriptor(bindingContext, namespace);
|
||||
return Translation.generateAst(bindingContext, namespace, project);
|
||||
}
|
||||
}
|
||||
@@ -36,7 +36,7 @@ public final class CallTranslator extends AbstractTranslator {
|
||||
JsExpression receiver = TranslationUtils.translateBaseExpression(context, unaryExpression);
|
||||
List<JsExpression> arguments = Collections.emptyList();
|
||||
DeclarationDescriptor descriptor = getDescriptorForReferenceExpression
|
||||
(context.bindingContext(), unaryExpression.getOperation());
|
||||
(context.bindingContext(), unaryExpression.getOperationReference());
|
||||
assert descriptor instanceof FunctionDescriptor;
|
||||
return (new CallTranslator(receiver, arguments, (FunctionDescriptor) descriptor, context)).translate();
|
||||
}
|
||||
@@ -71,7 +71,7 @@ public final class CallTranslator extends AbstractTranslator {
|
||||
List<JsExpression> arguments = Arrays.asList(translateRightExpression(context, binaryExpression));
|
||||
//TODO: use PSI util method to get operation reference
|
||||
DeclarationDescriptor descriptor = getDescriptorForReferenceExpression
|
||||
(context.bindingContext(), binaryExpression.getOperation());
|
||||
(context.bindingContext(), binaryExpression.getOperationReference());
|
||||
assert descriptor instanceof FunctionDescriptor;
|
||||
return (new CallTranslator(receiver, arguments, (FunctionDescriptor) descriptor, context)).translate();
|
||||
}
|
||||
|
||||
@@ -222,7 +222,7 @@ public final class BindingUtils {
|
||||
public static FunctionDescriptor getFunctionDescriptorForOperationExpression(@NotNull BindingContext context,
|
||||
@NotNull JetOperationExpression expression) {
|
||||
DeclarationDescriptor descriptorForReferenceExpression = getNullableDescriptorForReferenceExpression
|
||||
(context, expression.getOperation());
|
||||
(context, expression.getOperationReference());
|
||||
|
||||
if (descriptorForReferenceExpression == null) return null;
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ public final class PsiUtils {
|
||||
|
||||
@NotNull
|
||||
public static JetToken getOperationToken(@NotNull JetOperationExpression expression) {
|
||||
JetSimpleNameExpression operationExpression = expression.getOperation();
|
||||
JetSimpleNameExpression operationExpression = expression.getOperationReference();
|
||||
IElementType elementType = operationExpression.getReferencedNameElementType();
|
||||
assert elementType instanceof JetToken : "Unary expression should have operation token of type JetToken";
|
||||
return (JetToken) elementType;
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
package org.jetbrains.k2js.utils;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
|
||||
import org.jetbrains.jet.lang.diagnostics.Diagnostic;
|
||||
import org.jetbrains.jet.lang.diagnostics.Severity;
|
||||
import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic;
|
||||
import org.jetbrains.jet.lang.psi.JetNamespace;
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext;
|
||||
import org.jetbrains.jet.lang.resolve.BindingTrace;
|
||||
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
|
||||
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
|
||||
import org.jetbrains.jet.util.slicedmap.SlicedMap;
|
||||
import org.jetbrains.jet.util.slicedmap.WritableSlice;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* @author abreslav
|
||||
*/
|
||||
public class JetTestUtils {
|
||||
public static final BindingTrace DUMMY_TRACE = new BindingTrace() {
|
||||
|
||||
|
||||
@Override
|
||||
public BindingContext getBindingContext() {
|
||||
return new BindingContext() {
|
||||
|
||||
@Override
|
||||
public Collection<Diagnostic> getDiagnostics() {
|
||||
throw new UnsupportedOperationException(); // TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
|
||||
return DUMMY_TRACE.get(slice, key);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
|
||||
return DUMMY_TRACE.getKeys(slice);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> void record(WritableSlice<K, Boolean> slice, K key) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
|
||||
if (slice == BindingContext.PROCESSED) return (V) Boolean.FALSE;
|
||||
return SlicedMap.DO_NOTHING.get(slice, key);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
|
||||
assert slice.isCollective();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void report(@NotNull Diagnostic diagnostic) {
|
||||
if (diagnostic instanceof UnresolvedReferenceDiagnostic) {
|
||||
UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (UnresolvedReferenceDiagnostic) diagnostic;
|
||||
throw new IllegalStateException("Unresolved: " + unresolvedReferenceDiagnostic.getPsiElement().getText());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static BindingTrace DUMMY_EXCEPTION_ON_ERROR_TRACE = new BindingTrace() {
|
||||
@Override
|
||||
public BindingContext getBindingContext() {
|
||||
return new BindingContext() {
|
||||
@Override
|
||||
public Collection<Diagnostic> getDiagnostics() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
|
||||
return DUMMY_EXCEPTION_ON_ERROR_TRACE.get(slice, key);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
|
||||
return DUMMY_EXCEPTION_ON_ERROR_TRACE.getKeys(slice);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K> void record(WritableSlice<K, Boolean> slice, K key) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
|
||||
assert slice.isCollective();
|
||||
return Collections.emptySet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void report(@NotNull Diagnostic diagnostic) {
|
||||
if (diagnostic.getSeverity() == Severity.ERROR) {
|
||||
throw new IllegalStateException(diagnostic.getMessage());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public static BindingContext analyzeNamespace(@NotNull JetNamespace namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
|
||||
return AnalyzerFacade.analyzeOneNamespaceWithJavaIntegration(namespace, flowDataTraceFactory);
|
||||
}
|
||||
|
||||
|
||||
// public static JetCoreEnvironment createEnvironmentWithMockJdk(Disposable disposable) {
|
||||
// JetCoreEnvironment environment = new JetCoreEnvironment(disposable);
|
||||
// final File rtJar = new File(JetTestCaseBuilder.getHomeDirectory(), "compiler/testData/mockJDK-1.7/jre/lib/rt.jar");
|
||||
// environment.addToClasspath(rtJar);
|
||||
// environment.addToClasspath(new File(JetTestCaseBuilder.getHomeDirectory(), "compiler/testData/mockJDK-1.7/jre/lib/annotations.jar"));
|
||||
// return environment;
|
||||
// }
|
||||
|
||||
|
||||
public static void mkdirs(File file) throws IOException {
|
||||
if (file.isDirectory()) {
|
||||
return;
|
||||
}
|
||||
if (!file.mkdirs()) {
|
||||
throw new IOException();
|
||||
}
|
||||
}
|
||||
|
||||
public static void rmrf(File file) {
|
||||
if (file != null) {
|
||||
File[] children = file.listFiles();
|
||||
if (children != null) {
|
||||
for (File child : children) {
|
||||
rmrf(child);
|
||||
}
|
||||
}
|
||||
file.delete();
|
||||
}
|
||||
}
|
||||
|
||||
public static void recreateDirectory(File file) throws IOException {
|
||||
rmrf(file);
|
||||
mkdirs(file);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package org.jetbrains.k2js.test;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.mozilla.javascript.Context;
|
||||
import org.mozilla.javascript.Scriptable;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
public final class RhinoSystemOutputChecker implements RhinoResultChecker {
|
||||
|
||||
private final List<String> arguments;
|
||||
private final String expectedResult;
|
||||
|
||||
public RhinoSystemOutputChecker(String expectedResult, List<String> arguments) {
|
||||
this.expectedResult = expectedResult;
|
||||
this.arguments = arguments;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void runChecks(@NotNull Context context, @NotNull Scriptable scope)
|
||||
throws Exception {
|
||||
runMain(context, scope);
|
||||
String result = getSystemOutput(context, scope);
|
||||
assertTrue("Returned:\n" + result + "\n\nExpected:\n" + expectedResult, result.equals(expectedResult));
|
||||
}
|
||||
|
||||
private String getSystemOutput(@NotNull Context context, @NotNull Scriptable scope) {
|
||||
Object output = context.evaluateString(scope, "Kotlin.System.output()", "test", 0, null);
|
||||
assertTrue("Output should be a string.", output instanceof String);
|
||||
return (String) output;
|
||||
}
|
||||
|
||||
private void runMain(Context context, Scriptable scope) {
|
||||
context.evaluateString(scope, execMain(), "function call", 0, null);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String execMain() {
|
||||
String constructArguments = "var args = new Kotlin.Array(" + arguments.size() + ");\n";
|
||||
int index = 0;
|
||||
for (String argument : arguments) {
|
||||
constructArguments = constructArguments + "args[" + index + "] = \"" + argument + "\";\n";
|
||||
index++;
|
||||
}
|
||||
String callMain = "foo.main(args)\n";
|
||||
return constructArguments + callMain;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package org.jetbrains.k2js.test;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
* @author Talanov Pavel
|
||||
*/
|
||||
public final class SystemTest extends JavaClassesTest {
|
||||
|
||||
final private static String MAIN = "system/";
|
||||
|
||||
@Override
|
||||
protected String mainDirectory() {
|
||||
return MAIN;
|
||||
}
|
||||
|
||||
public void checkOutput(String filename, String expectedResult, String... args) throws Exception {
|
||||
translateFile(filename);
|
||||
runRhinoTest(generateFilenameList(getOutputFilePath(filename)),
|
||||
new RhinoSystemOutputChecker(expectedResult, Arrays.asList(args)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void systemPrint() throws Exception {
|
||||
checkOutput("systemPrint.kt", "Hello, world!");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -56,11 +56,8 @@ public abstract class TranslationTest {
|
||||
new RhinoFunctionResultChecker(namespaceName, functionName, expectedResult));
|
||||
}
|
||||
|
||||
private void translateFile(String filename) {
|
||||
K2JSTranslator.Arguments args = new K2JSTranslator.Arguments();
|
||||
args.src = getInputFilePath(filename);
|
||||
args.outputDir = getOutputFilePath(filename);
|
||||
K2JSTranslator.translate(args);
|
||||
protected void translateFile(String filename) {
|
||||
(new K2JSTranslator()).translate(getInputFilePath(filename), getOutputFilePath(filename));
|
||||
}
|
||||
|
||||
protected List<String> generateFilenameList(String inputFile) {
|
||||
@@ -68,7 +65,7 @@ public abstract class TranslationTest {
|
||||
}
|
||||
|
||||
//TODO: refactor filename generation logic
|
||||
private String getOutputFilePath(String filename) {
|
||||
protected String getOutputFilePath(String filename) {
|
||||
return getOutputPath() + convertToDotJsFile(filename);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
namespace foo
|
||||
|
||||
fun main(args : Array<String>) {
|
||||
System.out?.print("Hello, world!");
|
||||
}
|
||||
@@ -484,7 +484,7 @@ Kotlin.Array = Class.create({
|
||||
|
||||
|
||||
Kotlin.ArrayList = Class.create({
|
||||
initialize:function (len) {
|
||||
initialize:function () {
|
||||
this.array = [];
|
||||
this.$size = 0;
|
||||
},
|
||||
@@ -540,6 +540,30 @@ Kotlin.ArrayIterator = Class.create({
|
||||
}
|
||||
});
|
||||
|
||||
Kotlin.System = function () {
|
||||
var output = "";
|
||||
|
||||
var print = function (obj) {
|
||||
output += obj;
|
||||
};
|
||||
var println = function (obj) {
|
||||
output += obj;
|
||||
output += "\n";
|
||||
};
|
||||
|
||||
return {
|
||||
out:function () {
|
||||
return {
|
||||
print:print,
|
||||
println:println
|
||||
};
|
||||
},
|
||||
output:function () {
|
||||
return output;
|
||||
}
|
||||
};
|
||||
}();
|
||||
|
||||
Kotlin.Class = Class;
|
||||
Kotlin.Namespace = Namespace;
|
||||
Kotlin.Trait = Trait;
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
<orderEntry type="module" module-name="frontend" />
|
||||
<orderEntry type="module" module-name="frontend.java" />
|
||||
<orderEntry type="library" name="libs" level="project" />
|
||||
<orderEntry type="module" module-name="backend" />
|
||||
</component>
|
||||
</module>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user