From 25e4265fc7dcbd3b7f84e7cc36db32c3e2f31992 Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Tue, 22 Oct 2019 10:45:20 -0700 Subject: [PATCH] Add exception handling in vanilla jni Summary: Exception handling in vanilla jni ## Changelog: [Internal] [Added] Added exception handling for vanilla jni implementation in yoga Reviewed By: amir-shalem Differential Revision: D18036134 fbshipit-source-id: 965eaa2fddbc00b9ac0120b79678608e280d03db --- .../first-party/yogajni/jni/YGJNIVanilla.cpp | 38 ++++++++++--------- .../first-party/yogajni/jni/corefunctions.cpp | 14 ++++--- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp index 9835758f8b5..9a38c5755c0 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -352,25 +352,29 @@ static void jni_YGNodeCalculateLayoutJNI( jlongArray nativePointers, jobjectArray javaNodes) { - void* layoutContext = nullptr; - auto map = PtrJNodeMapVanilla{}; - if (nativePointers) { - size_t nativePointersSize = env->GetArrayLength(nativePointers); - jlong result[nativePointersSize]; - env->GetLongArrayRegion(nativePointers, 0, nativePointersSize, result); + try { + void* layoutContext = nullptr; + auto map = PtrJNodeMapVanilla{}; + if (nativePointers) { + size_t nativePointersSize = env->GetArrayLength(nativePointers); + jlong result[nativePointersSize]; + env->GetLongArrayRegion(nativePointers, 0, nativePointersSize, result); - map = PtrJNodeMapVanilla{result, nativePointersSize, javaNodes}; - layoutContext = ↦ + map = PtrJNodeMapVanilla{result, nativePointersSize, javaNodes}; + layoutContext = ↦ + } + + const YGNodeRef root = _jlong2YGNodeRef(nativePointer); + YGNodeCalculateLayoutWithContext( + root, + static_cast(width), + static_cast(height), + YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)), + layoutContext); + YGTransferLayoutOutputsRecursive(env, obj, root, layoutContext); + } catch (jthrowable throwable) { + env->Throw(throwable); } - - const YGNodeRef root = _jlong2YGNodeRef(nativePointer); - YGNodeCalculateLayoutWithContext( - root, - static_cast(width), - static_cast(height), - YGNodeStyleGetDirection(_jlong2YGNodeRef(nativePointer)), - layoutContext); - YGTransferLayoutOutputsRecursive(env, obj, root, layoutContext); } static void jni_YGNodeMarkDirtyJNI( diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp index 37dc12d429f..6a250b1dc21 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/corefunctions.cpp @@ -62,12 +62,16 @@ void logErrorMessageAndDie(const char* message) { } void assertNoPendingJniException(JNIEnv* env) { - // This method cannot call any other method of the library, since other - // methods of the library use it to check for exceptions too - if (env->ExceptionCheck()) { - env->ExceptionDescribe(); - logErrorMessageAndDie("Aborting due to pending Java exception in JNI"); + if (env->ExceptionCheck() == JNI_FALSE) { + return; } + + auto throwable = env->ExceptionOccurred(); + if (!throwable) { + logErrorMessageAndDie("Unable to get pending JNI exception."); + } + env->ExceptionClear(); + throw throwable; } } // namespace vanillajni