create global ref properly in YogaJNIException

Summary:
##Changelog:
[Internal][Yoga] create global ref properly in YogaJNIException

Reviewed By: astreet

Differential Revision: D18775982

fbshipit-source-id: ee529d6178d40b5f887fa1327fe156fa466f154f
This commit is contained in:
Sidharth Guglani
2019-12-03 15:57:51 -08:00
committed by Facebook Github Bot
parent ac8eb111a9
commit 089095f532
3 changed files with 19 additions and 4 deletions

View File

@@ -19,18 +19,19 @@ YogaJniException::YogaJniException() {
static const jmethodID methodId = facebook::yoga::vanillajni::getMethodId( static const jmethodID methodId = facebook::yoga::vanillajni::getMethodId(
getCurrentEnv(), cl, "<init>", "()V"); getCurrentEnv(), cl, "<init>", "()V");
auto throwable = getCurrentEnv()->NewObject(cl, methodId); auto throwable = getCurrentEnv()->NewObject(cl, methodId);
throwable_ = make_global_ref(static_cast<jthrowable>(throwable)); throwable_ =
newGlobalRef(getCurrentEnv(), static_cast<jthrowable>(throwable));
} }
YogaJniException::YogaJniException(jthrowable throwable) { YogaJniException::YogaJniException(jthrowable throwable) {
throwable_ = make_global_ref(throwable); throwable_ = newGlobalRef(getCurrentEnv(), throwable);
} }
YogaJniException::YogaJniException(YogaJniException&& rhs) YogaJniException::YogaJniException(YogaJniException&& rhs)
: throwable_(std::move(rhs.throwable_)) {} : throwable_(std::move(rhs.throwable_)) {}
YogaJniException::YogaJniException(const YogaJniException& rhs) { YogaJniException::YogaJniException(const YogaJniException& rhs) {
throwable_ = make_global_ref(rhs.throwable_.get()); throwable_ = newGlobalRef(getCurrentEnv(), rhs.throwable_.get());
} }
YogaJniException::~YogaJniException() { YogaJniException::~YogaJniException() {
@@ -42,7 +43,9 @@ YogaJniException::~YogaJniException() {
} }
ScopedLocalRef<jthrowable> YogaJniException::getThrowable() const noexcept { ScopedLocalRef<jthrowable> YogaJniException::getThrowable() const noexcept {
return make_local_ref(getCurrentEnv(), throwable_.get()); return make_local_ref(
getCurrentEnv(),
static_cast<jthrowable>(getCurrentEnv()->NewLocalRef(throwable_.get())));
} }
} // namespace vanillajni } // namespace vanillajni
} // namespace yoga } // namespace yoga

View File

@@ -96,6 +96,16 @@ ScopedGlobalRef<jobject> newGlobalRef(JNIEnv* env, jobject obj) {
return make_global_ref(result); return make_global_ref(result);
} }
ScopedGlobalRef<jthrowable> newGlobalRef(JNIEnv* env, jthrowable obj) {
jthrowable result = static_cast<jthrowable>(env->NewGlobalRef(obj));
if (!result) {
logErrorMessageAndDie("Could not obtain global reference from object");
}
return make_global_ref(result);
}
} // namespace vanillajni } // namespace vanillajni
} // namespace yoga } // namespace yoga
} // namespace facebook } // namespace facebook

View File

@@ -69,6 +69,8 @@ callStaticObjectMethod(JNIEnv* env, jclass clazz, jmethodID methodId, ...);
* reference out of it. If any error happens, aborts the process. * reference out of it. If any error happens, aborts the process.
*/ */
ScopedGlobalRef<jobject> newGlobalRef(JNIEnv* env, jobject obj); ScopedGlobalRef<jobject> newGlobalRef(JNIEnv* env, jobject obj);
ScopedGlobalRef<jthrowable> newGlobalRef(JNIEnv* env, jthrowable obj);
} // namespace vanillajni } // namespace vanillajni
} // namespace yoga } // namespace yoga
} // namespace facebook } // namespace facebook