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

Commit

Permalink
Add exception related jni functions. Rework cpp exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
intrigus committed Oct 5, 2017
1 parent 3fa03ca commit 7040edd
Show file tree
Hide file tree
Showing 8 changed files with 1,688 additions and 314 deletions.
2 changes: 2 additions & 0 deletions jtransc-gen-common-tests/src/threading/ThreadingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

public class ThreadingTest {
static public void main(String[] args) {
System.out.println(Thread.currentThread() == null);
System.out.println(Thread.currentThread().getName());
ArrayList<String> logs = new ArrayList<String>();
long start = System.currentTimeMillis();
for (int n = 0; n < 3; n++) {
Expand Down
37 changes: 29 additions & 8 deletions jtransc-gen-cpp/src/com/jtransc/gen/cpp/CppTarget.kt
Original file line number Diff line number Diff line change
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() /*&& method.name.startsWith("dooFoo")*/) {
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__);""")
}
1 change: 1 addition & 0 deletions jtransc-rt-core/src/com/jtransc/io/JTranscConsole.java
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 7040edd

Please sign in to comment.