@@ -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}
0 commit comments