Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Add exception related jni functions. Rework cpp exceptions #273

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 29 additions & 0 deletions jtransc-gen-common-tests/src/threading/ThreadingTest.java
Expand Up @@ -5,6 +5,7 @@

public class ThreadingTest {
static public void main(String[] args) {
testCurrentThread();
ArrayList<String> logs = new ArrayList<String>();
long start = System.currentTimeMillis();
for (int n = 0; n < 3; n++) {
Expand Down Expand Up @@ -34,4 +35,32 @@ public void run() {
System.out.println(log);
}
}

private static void testCurrentThread() {
System.out.println(Thread.currentThread() == null);
System.out.println(Thread.currentThread().getName() == null);
System.out.println(Thread.currentThread().isDaemon());
Thread t1 = new Thread() {
public void run() {
System.out.println(Thread.currentThread().getName());
Thread t2 = new Thread() {
public void run() {
System.out.println(Thread.currentThread().getName());
}
};
t2.start();
try {
t2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t1.start();
try {
t1.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
37 changes: 29 additions & 8 deletions jtransc-gen-cpp/src/com/jtransc/gen/cpp/CppTarget.kt
Expand Up @@ -424,6 +424,7 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
line("try") {
line("N::startup();")
line(genStaticConstructorsSorted())
line("initMainThread();")
val callMain = buildMethod(program[AstMethodRef(program.entrypoint, "main", AstType.METHOD(AstType.VOID, listOf(ARRAY(AstType.STRING))))]!!, static = true)

line("$callMain(N::strEmptyArray());")
Expand All @@ -434,14 +435,14 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
line("catch (std::wstring s)") {
line("""std::wcout << L"ERROR std::wstring " << s << L"\n";""")
}
//line("catch (java_lang_Throwable *s)") {
//line("catch (const java_lang_Throwable& s)") {
// line("""std::wcout << L"${"java.lang.Throwable".fqname.targetName}:" << L"\n";""")
// line("""printf("Exception: %p\n", (void*)s);""")
//}
//line("catch (p_java_lang_Object s)") {
// val toStringMethod = program["java.lang.Object".fqname].getMethodWithoutOverrides("toString")!!.targetName
// line("""std::wcout << L"ERROR p_java_lang_Object " << N::istr2(s->$toStringMethod()) << L"\n";""")
//line("""printf("Exception: %p\n", (void*)s);""")
//}
line("catch (${"java.lang.Object".fqname.targetName}* s)") {
val toStringMethod = program["java.lang.Object".fqname].getMethodWithoutOverrides("toString")!!.targetName
line("""std::wcout << L"ERROR p_java_lang_Object " << N::istr2(s->$toStringMethod()) << L"\n";""")
}
//line("catch (...)") {
// line("""std::wcout << L"ERROR unhandled unknown exception\n";""")
//}
Expand Down Expand Up @@ -666,7 +667,7 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
line(defaultBody)
}
line("#endif")
} else if (method.isNative && bodies.isEmpty() && method.name.startsWith("dooFoo")) {
} else if (method.isNative && bodies.isEmpty()) {
line(genJniMethod(method))
} else {
line(defaultBody)
Expand Down Expand Up @@ -721,7 +722,22 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
val arg = method.methodType.args[i].type
sb2.append(", ${genJavaToJniCast(arg)}p${i}")
}
line("return ${genJniToJavaCast(method.actualRetType)}fptr(N::getJniEnv(), NULL $sb2);")
//line("N::enterNativeFunction(N::getThreadEnv()->currentThread)")
if (method.actualRetType != AstType.VOID) line("${method.actualRetType.targetNameRef} result;")
line("try") {
if (method.actualRetType == AstType.VOID) {
line("fptr((JNIEnv*)N::getThreadEnv(), NULL $sb2);")
} else {
line("result = ${genJniToJavaCast(method.actualRetType)}fptr((JNIEnv*)N::getThreadEnv(), NULL $sb2);")
}
}
line("catch(${"java.lang.Object".fqname.targetName}* e)"){
line("abort();")
}
line("N::exitNative(N::getThreadEnv());")
if (method.actualRetType == AstType.VOID) line("return;")
else line("return result;")

//line("JNI: \"Empty BODY : ${method.containingClass.name}::${method.name}::${method.desc}\";")
}

Expand Down Expand Up @@ -1184,4 +1200,9 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {

override fun genStmMonitorEnter(stm: AstStm.MONITOR_ENTER) = Indenter("N::monitorEnter(" + stm.expr.genExpr() + ");")
override fun genStmMonitorExit(stm: AstStm.MONITOR_EXIT) = Indenter("N::monitorExit(" + stm.expr.genExpr() + ");")

//override fun genStmThrow(stm: AstStm.THROW, last: Boolean) = Indenter("N::throwException(${stm.exception.genExpr()});")
//override fun genStmRethrow(stm: AstStm.RETHROW, last: Boolean) = Indenter("""N::throwException(J__i__exception__);""")
override open fun genStmThrow(stm: AstStm.THROW, last: Boolean) = Indenter("N::throwJavaException(${stm.exception.genExpr()});")
override open fun genStmRethrow(stm: AstStm.RETHROW, last: Boolean) = Indenter("""N::throwJavaException(J__i__exception__);""")
}
3 changes: 3 additions & 0 deletions jtransc-gen-haxe/test/HaxeTest.kt
Expand Up @@ -32,6 +32,7 @@ import jtransc.jtransc.nativ.JTranscHaxeNativeMixedTest
import jtransc.micro.MicroHelloWorld
import org.junit.Ignore
import org.junit.Test
import threading.ThreadingTest

class HaxeTest : _Base() {
override val DEFAULT_TARGET = HaxeTarget()
Expand Down Expand Up @@ -59,6 +60,8 @@ class HaxeTest : _Base() {

@Test fun testThreadCpp() = testClass(Params(clazz = ThreadTest::class.java, minimize = false, lang = "cpp", log = null, debug = true))

@Test fun testThreading() = testClass(Params(clazz = ThreadingTest::class.java, minimize = false, lang = "cpp", log = false, debug = true))

@Test fun haxeNativeCallTest() = testNativeClass("""
true
true
Expand Down
5 changes: 5 additions & 0 deletions jtransc-rt-core/src/com/jtransc/JTranscSystem.java
Expand Up @@ -165,6 +165,11 @@ public static boolean isSys() {
return TRUE;
}

@HaxeMethodBody(target = "cpp", value = "return true;")
public static boolean isHaxeCpp() {
return FALSE;
}

@JTranscMethodBody(target = "cpp", value = "return true;")
public static boolean isCpp() {
return FALSE;
Expand Down
1 change: 1 addition & 0 deletions jtransc-rt-core/src/com/jtransc/io/JTranscConsole.java
Expand Up @@ -170,6 +170,7 @@ static public synchronized void log(double v) {
@JTranscMethodBodyList({
@JTranscMethodBody(target = "php", value = "echo \"$p0\\n\";"),
@JTranscMethodBody(target = "js", value = "console.error('' + p0);"),
@JTranscMethodBody(target = "cpp", value = "std::wcerr << N::istr2({% SMETHOD java.lang.String:valueOf:(Ljava/lang/Object;)Ljava/lang/String; %}(p0)) << L\"\\n\";"),
@JTranscMethodBody(target = "cs", value = "Console.Error.WriteLine(p0);"),
@JTranscMethodBody(target = "as3", value = "trace(p0);"),
@JTranscMethodBody(target = "dart", value = "print(p0);"),
Expand Down
6 changes: 5 additions & 1 deletion jtransc-rt-core/src/j/ProgramReflection.java
@@ -1,5 +1,6 @@
package j;

import com.jtransc.JTranscSystem;
import com.jtransc.ds.FastIntMap;
import com.jtransc.ds.FastStringMap;

Expand Down Expand Up @@ -327,7 +328,10 @@ static public MemberInfo getMethodInfo(int classId, int methodId) {

//native static public Class<?> getClassByInfo(ClassInfo info);

native static public Method getMethodByInfo(Class<?> clazz, MemberInfo info);
static public Method getMethodByInfo(Class<?> clazz, MemberInfo info){
JTranscSystem.checkInJVM("ProgramReflection::getMethodByInfo should've been replaced by plugin!");
return null;
}

static public Class<?> getClassById(int classId) {
ProgramReflection._ensure();
Expand Down