Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit 7040edd

Browse files
committed
Add exception related jni functions. Rework cpp exceptions
1 parent 3fa03ca commit 7040edd

File tree

8 files changed

+1688
-314
lines changed

8 files changed

+1688
-314
lines changed

jtransc-gen-common-tests/src/threading/ThreadingTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
public class ThreadingTest {
77
static public void main(String[] args) {
8+
System.out.println(Thread.currentThread() == null);
9+
System.out.println(Thread.currentThread().getName());
810
ArrayList<String> logs = new ArrayList<String>();
911
long start = System.currentTimeMillis();
1012
for (int n = 0; n < 3; n++) {

jtransc-gen-cpp/src/com/jtransc/gen/cpp/CppTarget.kt

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,7 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
424424
line("try") {
425425
line("N::startup();")
426426
line(genStaticConstructorsSorted())
427+
line("initMainThread();")
427428
val callMain = buildMethod(program[AstMethodRef(program.entrypoint, "main", AstType.METHOD(AstType.VOID, listOf(ARRAY(AstType.STRING))))]!!, static = true)
428429

429430
line("$callMain(N::strEmptyArray());")
@@ -434,14 +435,14 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
434435
line("catch (std::wstring s)") {
435436
line("""std::wcout << L"ERROR std::wstring " << s << L"\n";""")
436437
}
437-
//line("catch (java_lang_Throwable *s)") {
438+
//line("catch (const java_lang_Throwable& s)") {
438439
// line("""std::wcout << L"${"java.lang.Throwable".fqname.targetName}:" << L"\n";""")
439-
// line("""printf("Exception: %p\n", (void*)s);""")
440-
//}
441-
//line("catch (p_java_lang_Object s)") {
442-
// val toStringMethod = program["java.lang.Object".fqname].getMethodWithoutOverrides("toString")!!.targetName
443-
// line("""std::wcout << L"ERROR p_java_lang_Object " << N::istr2(s->$toStringMethod()) << L"\n";""")
440+
//line("""printf("Exception: %p\n", (void*)s);""")
444441
//}
442+
line("catch (${"java.lang.Object".fqname.targetName}* s)") {
443+
val toStringMethod = program["java.lang.Object".fqname].getMethodWithoutOverrides("toString")!!.targetName
444+
line("""std::wcout << L"ERROR p_java_lang_Object " << N::istr2(s->$toStringMethod()) << L"\n";""")
445+
}
445446
//line("catch (...)") {
446447
// line("""std::wcout << L"ERROR unhandled unknown exception\n";""")
447448
//}
@@ -666,7 +667,7 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
666667
line(defaultBody)
667668
}
668669
line("#endif")
669-
} else if (method.isNative && bodies.isEmpty() && method.name.startsWith("dooFoo")) {
670+
} else if (method.isNative && bodies.isEmpty() /*&& method.name.startsWith("dooFoo")*/) {
670671
line(genJniMethod(method))
671672
} else {
672673
line(defaultBody)
@@ -721,7 +722,22 @@ class CppGenerator(injector: Injector) : CommonGenerator(injector) {
721722
val arg = method.methodType.args[i].type
722723
sb2.append(", ${genJavaToJniCast(arg)}p${i}")
723724
}
724-
line("return ${genJniToJavaCast(method.actualRetType)}fptr(N::getJniEnv(), NULL $sb2);")
725+
//line("N::enterNativeFunction(N::getThreadEnv()->currentThread)")
726+
if (method.actualRetType != AstType.VOID) line("${method.actualRetType.targetNameRef} result;")
727+
line("try") {
728+
if (method.actualRetType == AstType.VOID) {
729+
line("fptr((JNIEnv*)N::getThreadEnv(), NULL $sb2);")
730+
} else {
731+
line("result = ${genJniToJavaCast(method.actualRetType)}fptr((JNIEnv*)N::getThreadEnv(), NULL $sb2);")
732+
}
733+
}
734+
line("catch(${"java.lang.Object".fqname.targetName}* e)"){
735+
line("abort();")
736+
}
737+
line("N::exitNative(N::getThreadEnv());")
738+
if (method.actualRetType == AstType.VOID) line("return;")
739+
else line("return result;")
740+
725741
//line("JNI: \"Empty BODY : ${method.containingClass.name}::${method.name}::${method.desc}\";")
726742
}
727743

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

11851201
override fun genStmMonitorEnter(stm: AstStm.MONITOR_ENTER) = Indenter("N::monitorEnter(" + stm.expr.genExpr() + ");")
11861202
override fun genStmMonitorExit(stm: AstStm.MONITOR_EXIT) = Indenter("N::monitorExit(" + stm.expr.genExpr() + ");")
1203+
1204+
//override fun genStmThrow(stm: AstStm.THROW, last: Boolean) = Indenter("N::throwException(${stm.exception.genExpr()});")
1205+
//override fun genStmRethrow(stm: AstStm.RETHROW, last: Boolean) = Indenter("""N::throwException(J__i__exception__);""")
1206+
override open fun genStmThrow(stm: AstStm.THROW, last: Boolean) = Indenter("N::throwJavaException(${stm.exception.genExpr()});")
1207+
override open fun genStmRethrow(stm: AstStm.RETHROW, last: Boolean) = Indenter("""N::throwJavaException(J__i__exception__);""")
11871208
}

jtransc-rt-core/src/com/jtransc/io/JTranscConsole.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ static public synchronized void log(double v) {
170170
@JTranscMethodBodyList({
171171
@JTranscMethodBody(target = "php", value = "echo \"$p0\\n\";"),
172172
@JTranscMethodBody(target = "js", value = "console.error('' + p0);"),
173+
@JTranscMethodBody(target = "cpp", value = "std::wcerr << N::istr2({% SMETHOD java.lang.String:valueOf:(Ljava/lang/Object;)Ljava/lang/String; %}(p0)) << L\"\\n\";"),
173174
@JTranscMethodBody(target = "cs", value = "Console.Error.WriteLine(p0);"),
174175
@JTranscMethodBody(target = "as3", value = "trace(p0);"),
175176
@JTranscMethodBody(target = "dart", value = "print(p0);"),

0 commit comments

Comments
 (0)