From 07c0d539bdb3a248762d0a06fd3f622b278a7ecb Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Thu, 14 May 2020 06:32:13 -0700 Subject: [PATCH] throw std::logic_error instead of aborting the process and convert to java exception Summary: Changelog: [Internal][Yoga] throw std::logic_error instead of aborting the process and convert to java exception for jni layer Reviewed By: pasqualeanatriello Differential Revision: D21301235 fbshipit-source-id: 148b27920e62990a271e1d0df8c85a2cc42f4fd4 --- java/jni/YGJNIVanilla.cpp | 7 +++++++ tests/YGMeasureTest.cpp | 4 ++-- yoga/Utils.cpp | 4 ++++ yoga/Utils.h | 2 ++ yoga/Yoga.cpp | 6 +++--- yoga/log.cpp | 4 ---- 6 files changed, 18 insertions(+), 9 deletions(-) diff --git a/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp index 6e66c06e..698ab561 100644 --- a/java/jni/YGJNIVanilla.cpp +++ b/java/jni/YGJNIVanilla.cpp @@ -388,6 +388,13 @@ static void jni_YGNodeCalculateLayoutJNI( if (throwable.get()) { env->Throw(throwable.get()); } + } catch (const std::logic_error& ex) { + env->ExceptionClear(); + jclass cl = env->FindClass("Ljava/lang/IllegalStateException;"); + static const jmethodID methodId = facebook::yoga::vanillajni::getMethodId( + env, cl, "", "(Ljava/lang/String;)V"); + auto throwable = env->NewObject(cl, methodId, env->NewStringUTF(ex.what())); + env->Throw(static_cast(throwable)); } } diff --git a/tests/YGMeasureTest.cpp b/tests/YGMeasureTest.cpp index b5bbe5d4..e47607d4 100644 --- a/tests/YGMeasureTest.cpp +++ b/tests/YGMeasureTest.cpp @@ -580,7 +580,7 @@ TEST(YogaDeathTest, cannot_add_child_to_node_with_measure_func) { root->setMeasureFunc(_measure); const YGNodeRef root_child0 = YGNodeNew(); - ASSERT_DEATH(YGNodeInsertChild(root, root_child0, 0), "Cannot add child.*"); + ASSERT_THROW(YGNodeInsertChild(root, root_child0, 0), std::logic_error); YGNodeFree(root_child0); YGNodeFreeRecursive(root); } @@ -589,7 +589,7 @@ TEST(YogaDeathTest, cannot_add_nonnull_measure_func_to_non_leaf_node) { const YGNodeRef root = YGNodeNew(); const YGNodeRef root_child0 = YGNodeNew(); YGNodeInsertChild(root, root_child0, 0); - ASSERT_DEATH(root->setMeasureFunc(_measure), "Cannot set measure function.*"); + ASSERT_THROW(root->setMeasureFunc(_measure), std::logic_error); YGNodeFreeRecursive(root); } diff --git a/yoga/Utils.cpp b/yoga/Utils.cpp index 761f3515..f6e55d0d 100644 --- a/yoga/Utils.cpp +++ b/yoga/Utils.cpp @@ -65,3 +65,7 @@ YGFloatOptional YGFloatOptionalMax(YGFloatOptional op1, YGFloatOptional op2) { } return op1.isUndefined() ? op2 : op1; } + +void throwLogicalErrorWithMessage(const char* message) { + throw std::logic_error(message); +} diff --git a/yoga/Utils.h b/yoga/Utils.h index bce8dfca..e9edf2f9 100644 --- a/yoga/Utils.h +++ b/yoga/Utils.h @@ -141,3 +141,5 @@ inline YGFloatOptional YGResolveValueMargin( const float ownerSize) { return value.isAuto() ? YGFloatOptional{0} : YGResolveValue(value, ownerSize); } + +void throwLogicalErrorWithMessage(const char* message); diff --git a/yoga/Yoga.cpp b/yoga/Yoga.cpp index c2a5c286..91e09c15 100644 --- a/yoga/Yoga.cpp +++ b/yoga/Yoga.cpp @@ -249,9 +249,6 @@ YOGA_EXPORT YGNodeRef YGNodeClone(YGNodeRef oldNode) { static YGConfigRef YGConfigClone(const YGConfig& oldConfig) { const YGConfigRef config = new YGConfig(oldConfig); YGAssert(config != nullptr, "Could not allocate memory for config"); - if (config == nullptr) { - abort(); - } gConfigInstanceCount++; return config; } @@ -4341,6 +4338,7 @@ YOGA_EXPORT void YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour( void YGAssert(const bool condition, const char* message) { if (!condition) { Log::log(YGNodeRef{nullptr}, YGLogLevelFatal, nullptr, "%s\n", message); + throwLogicalErrorWithMessage(message); } } @@ -4350,6 +4348,7 @@ void YGAssertWithNode( const char* message) { if (!condition) { Log::log(node, YGLogLevelFatal, nullptr, "%s\n", message); + throwLogicalErrorWithMessage(message); } } @@ -4359,6 +4358,7 @@ void YGAssertWithConfig( const char* message) { if (!condition) { Log::log(config, YGLogLevelFatal, nullptr, "%s\n", message); + throwLogicalErrorWithMessage(message); } } diff --git a/yoga/log.cpp b/yoga/log.cpp index fe6fbbc6..eb3da039 100644 --- a/yoga/log.cpp +++ b/yoga/log.cpp @@ -26,10 +26,6 @@ void vlog( va_list args) { YGConfig* logConfig = config != nullptr ? config : YGConfigGetDefault(); logConfig->log(logConfig, node, level, context, format, args); - - if (level == YGLogLevelFatal) { - abort(); - } } } // namespace