diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index f379267dcaa..7b4f32de869 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -13,8 +13,16 @@
+
+
+
-
+
+
+
+
+
+
@@ -214,40 +222,49 @@
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
+
+
+
+
+
+
+
+
+
@@ -256,52 +273,49 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
-
+
+
-
-
+
+
+
+
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -341,7 +355,7 @@
-
+
@@ -380,22 +394,22 @@
@@ -514,7 +528,105 @@
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -583,10 +695,6 @@
-
-
-
-
@@ -610,7 +718,7 @@
-
+
@@ -620,23 +728,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -646,7 +738,7 @@
-
+
@@ -654,15 +746,7 @@
-
-
-
-
-
-
-
-
-
+
@@ -672,97 +756,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -821,35 +815,7 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
@@ -970,6 +936,29 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -1126,11 +1115,11 @@
-
-
-
-
-
+
+
+
+
+
localhost
@@ -1205,9 +1194,9 @@
-
+
-
+
@@ -1249,7 +1238,7 @@
-
+
@@ -1262,7 +1251,6 @@
-
@@ -1287,6 +1275,7 @@
+
@@ -1296,116 +1285,120 @@
-
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
+
-
+
-
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
+
+
+
+
+
+
+
+
+
+
-
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
diff --git a/translator/src/org/jetbrains/k2js/K2JSTranslator.java b/translator/src/org/jetbrains/k2js/K2JSTranslator.java
index 8e883730b9d..a2fd58fe234 100644
--- a/translator/src/org/jetbrains/k2js/K2JSTranslator.java
+++ b/translator/src/org/jetbrains/k2js/K2JSTranslator.java
@@ -65,7 +65,8 @@ public final class K2JSTranslator {
JetFile file = createPsiFile("test", programText);
String programCode = generateProgramCode(file);
String callToMain = generateCallToMain(file, argumentsString);
- return programCode + callToMain;
+ String programOutput = "Kotlin.System.output();";
+ return programCode + callToMain + programOutput;
}
private String generateProgramCode(JetFile psiFile) {
@@ -121,7 +122,8 @@ public final class K2JSTranslator {
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(environment.getProject())).trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
+ return ((PsiFileFactoryImpl) PsiFileFactory.getInstance(environment.getProject()))
+ .trySetupPsiForFile(virtualFile, JetLanguage.INSTANCE, true, false);
}
diff --git a/translator/src/org/jetbrains/k2js/K2JSTranslatorApplet.java b/translator/src/org/jetbrains/k2js/K2JSTranslatorApplet.java
index fe99f581c97..cdefad02ffb 100644
--- a/translator/src/org/jetbrains/k2js/K2JSTranslatorApplet.java
+++ b/translator/src/org/jetbrains/k2js/K2JSTranslatorApplet.java
@@ -1,20 +1,53 @@
package org.jetbrains.k2js;
import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+import org.jetbrains.k2js.utils.ErrorSender;
import java.applet.Applet;
+import java.io.PrintWriter;
+import java.io.StringWriter;
/**
* @author Pavel Talanov
*/
public final class K2JSTranslatorApplet extends Applet {
+ public static final int SESSION_ID = 100500;
+
+ @SuppressWarnings("FieldCanBeLocal")
+ private static String EXCEPTION = "exception=";
+
+ @Nullable
+ public String translateToJS(@NotNull String code, @NotNull String arguments) {
+ try {
+ return generateJSCode(code, arguments);
+ } catch (AssertionError e) {
+ reportException(e);
+ return EXCEPTION + " Translation error.";
+ } catch (UnsupportedOperationException e) {
+ reportException(e);
+ return EXCEPTION + " Unsupported feature.";
+ } catch (Throwable e) {
+ reportException(e);
+ return EXCEPTION + " Unexpected exception.";
+ }
+ }
+
@NotNull
- public String translate(@NotNull String code, @NotNull String arguments) {
+ private String generateJSCode(@NotNull String code, @NotNull String arguments) {
String generatedCode = (new K2JSTranslator()).translateStringWithCallToMain(code, arguments);
System.out.println("GENERATED JAVASCRIPT CODE:\n-----------------------------------\n");
System.out.println(generatedCode);
return generatedCode;
}
+ private void reportException(@NotNull Throwable e) {
+ System.out.println("Exception in translateToJS!!!");
+ e.printStackTrace();
+ StringWriter stringWriter = new StringWriter();
+ e.printStackTrace(new PrintWriter(stringWriter));
+ String request = getCodeBase().getProtocol() + "://" + getCodeBase().getHost();
+ ErrorSender.sendTextToServer(stringWriter.toString(), request);
+ }
}
diff --git a/translator/src/org/jetbrains/k2js/translate/context/NamingScope.java b/translator/src/org/jetbrains/k2js/translate/context/NamingScope.java
index 881b5d1fe63..f583f279108 100644
--- a/translator/src/org/jetbrains/k2js/translate/context/NamingScope.java
+++ b/translator/src/org/jetbrains/k2js/translate/context/NamingScope.java
@@ -59,11 +59,23 @@ public final class NamingScope {
@NotNull
public JsName declareVariable(@NotNull DeclarationDescriptor descriptor,
@NotNull String name) {
- JsName declaredName = scope.declareName(name);
+ JsName declaredName = scope.declareName(mayBeObfuscateName(name));
descriptorToNameMap.put(descriptor, declaredName);
return declaredName;
}
+ //TODO: temporary solution
+ @NotNull
+ private String mayBeObfuscateName(@NotNull String name) {
+ int obfuscate = 0;
+ String result = name;
+// while (scope.findExistingName(result) != null) {
+// result = name + "_" + obfuscate;
+// obfuscate++;
+// }
+ return result;
+ }
+
@NotNull
public JsName declareVariable(@NotNull DeclarationDescriptor descriptor) {
return declareVariable(descriptor, descriptor.getName());
diff --git a/translator/src/org/jetbrains/k2js/translate/context/StaticContext.java b/translator/src/org/jetbrains/k2js/translate/context/StaticContext.java
index 717957b8877..b992050c699 100644
--- a/translator/src/org/jetbrains/k2js/translate/context/StaticContext.java
+++ b/translator/src/org/jetbrains/k2js/translate/context/StaticContext.java
@@ -120,7 +120,7 @@ public class StaticContext {
@NotNull
public JsName getGlobalName(@NotNull DeclarationDescriptor descriptor) {
JsName nameToDeclare = declarations.getName(descriptor);
- nameToDeclare.setObfuscatable(false);
+ //nameToDeclare.setObfuscatable(false);
return nameToDeclare;
}
diff --git a/translator/src/org/jetbrains/k2js/utils/ErrorSender.java b/translator/src/org/jetbrains/k2js/utils/ErrorSender.java
new file mode 100644
index 00000000000..5a75ac5fbcb
--- /dev/null
+++ b/translator/src/org/jetbrains/k2js/utils/ErrorSender.java
@@ -0,0 +1,51 @@
+package org.jetbrains.k2js.utils;
+
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.k2js.K2JSTranslatorApplet;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.io.OutputStreamWriter;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+/**
+ * @author Pavel Talanov
+ */
+public final class ErrorSender {
+
+ public static void sendTextToServer(@NotNull String text, @NotNull String request) {
+ String urlPath = request + "/?sessionId=" + K2JSTranslatorApplet.SESSION_ID + "&writeLog=" + "error";
+
+ URL url;
+ try {
+ url = new URL(urlPath);
+
+ HttpURLConnection urlConnection;
+ urlConnection = (HttpURLConnection) url.openConnection();
+
+
+ urlConnection.setDoOutput(true);
+ OutputStreamWriter wr = new OutputStreamWriter(urlConnection.getOutputStream());
+ wr.write("text=" + text);
+ wr.flush();
+ wr.close();
+
+ urlConnection.connect();
+
+ BufferedReader in;
+
+ try {
+ in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
+ } catch (Exception e) {
+ in = new BufferedReader(new InputStreamReader(urlConnection.getErrorStream()));
+ }
+ in.close();
+
+ } catch (Exception e) {
+ // IGNORE
+ }
+ }
+
+
+}
diff --git a/translator/test/org/jetbrains/k2js/test/AppletTest.java b/translator/test/org/jetbrains/k2js/test/AppletTest.java
index 85086e80fc6..8d425965eed 100644
--- a/translator/test/org/jetbrains/k2js/test/AppletTest.java
+++ b/translator/test/org/jetbrains/k2js/test/AppletTest.java
@@ -1,7 +1,10 @@
package org.jetbrains.k2js.test;
import org.jetbrains.k2js.K2JSTranslatorApplet;
+import org.junit.Assert;
import org.junit.Test;
+import org.mozilla.javascript.Context;
+import org.mozilla.javascript.Scriptable;
/**
* @author Pavel Talanov
@@ -17,6 +20,30 @@ public final class AppletTest extends TranslationTest {
@Test
public void simpleTest() throws Exception {
- (new K2JSTranslatorApplet()).translate("fun main(args : Array) {}", " a 3 1 2134");
+ evaluateCodeWithParameters("fun main(args : Array) {}", " a 3 1 2134", "");
+ }
+
+
+ @Test
+ public void systemOutTest() throws Exception {
+ evaluateCodeWithParameters("class A() {} fun main(args : Array) { var a = A();}", "", "");
+ }
+
+ private void evaluateCodeWithParameters(String kotlinCode, String paramString, String expectedResult)
+ throws Exception {
+ String programCode = (new K2JSTranslatorApplet())
+ .translateToJS(kotlinCode, paramString);
+ Assert.assertEquals(evaluateStringWithRhino(programCode), expectedResult);
+ }
+
+
+ private String evaluateStringWithRhino(String programCode) throws Exception {
+ Context context = Context.enter();
+ Scriptable scope = context.initStandardObjects();
+ runFileWithRhino(kotlinLibraryPath(), context, scope);
+ Object result = context.evaluateString(scope, programCode, "program code", 1, null);
+ Context.exit();
+ assert result instanceof String : "Must evaluate to string! Evaluated to: " + result;
+ return (String) result;
}
}
diff --git a/translator/test/org/jetbrains/k2js/test/NameClashesTest.java b/translator/test/org/jetbrains/k2js/test/NameClashesTest.java
new file mode 100644
index 00000000000..fc466ffa0f1
--- /dev/null
+++ b/translator/test/org/jetbrains/k2js/test/NameClashesTest.java
@@ -0,0 +1,21 @@
+package org.jetbrains.k2js.test;
+
+import org.junit.Test;
+
+/**
+ * @author Pavel Talanov
+ */
+public final class NameClashesTest extends TranslationTest {
+
+ private static final String MAIN = "nameClashes/";
+
+ @Override
+ protected String mainDirectory() {
+ return MAIN;
+ }
+
+ @Test
+ public void methodOverload() throws Exception {
+ testFooBoxIsTrue("methodOverload.kt");
+ }
+}
\ No newline at end of file
diff --git a/translator/test/org/jetbrains/k2js/test/TranslationTest.java b/translator/test/org/jetbrains/k2js/test/TranslationTest.java
index e9571617f1a..a27a5112d3f 100644
--- a/translator/test/org/jetbrains/k2js/test/TranslationTest.java
+++ b/translator/test/org/jetbrains/k2js/test/TranslationTest.java
@@ -15,9 +15,9 @@ import java.util.List;
public abstract class TranslationTest {
private final static String TEST_FILES = "translator/testFiles/";
- final private static String CASES = "cases/";
- final private static String OUT = "out/";
- final private static String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js";
+ private static final String CASES = "cases/";
+ private static final String OUT = "out/";
+ private static final String KOTLIN_JS_LIB = TEST_FILES + "kotlin_lib.js";
protected abstract String mainDirectory();
@@ -88,12 +88,12 @@ public abstract class TranslationTest {
}
protected void runRhinoTest(List fileNames, RhinoResultChecker checker) throws Exception {
- Context cx = Context.enter();
- Scriptable scope = cx.initStandardObjects();
+ Context context = Context.enter();
+ Scriptable scope = context.initStandardObjects();
for (String filename : fileNames) {
- runFileWithRhino(filename, cx, scope);
+ runFileWithRhino(filename, context, scope);
}
- checker.runChecks(cx, scope);
+ checker.runChecks(context, scope);
Context.exit();
}
diff --git a/translator/testFiles/nameClashes/cases/methodOverload.kt b/translator/testFiles/nameClashes/cases/methodOverload.kt
new file mode 100644
index 00000000000..25c6670c196
--- /dev/null
+++ b/translator/testFiles/nameClashes/cases/methodOverload.kt
@@ -0,0 +1,20 @@
+namespace foo
+
+class A() {
+
+ fun eval() = 3
+ fun eval(a : Int) = 4
+ fun eval(a : String) = 5
+ fun eval(a : String, b : Int) = 6
+
+}
+
+fun box() : Boolean {
+
+ if (A().eval() != 3) return false;
+ if (A().eval(2) != 4) return false;
+ if (A().eval("3") != 5) return false;
+ if (A().eval("a", 3) != 6) return false;
+
+ return true;
+}
\ No newline at end of file