2019-10-08 17:48:32 -07:00
|
|
|
/*
|
2021-12-30 15:08:43 -08:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2019-10-08 17:48:32 -07:00
|
|
|
*
|
2019-10-15 10:30:08 -07:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2019-10-08 17:48:32 -07:00
|
|
|
*/
|
2019-10-15 10:30:08 -07:00
|
|
|
|
2019-10-08 17:48:32 -07:00
|
|
|
#include "corefunctions.h"
|
2019-12-02 05:21:56 -08:00
|
|
|
#include "YogaJniException.h"
|
2023-09-13 20:12:55 -07:00
|
|
|
#include "macros.h"
|
2019-10-08 17:48:32 -07:00
|
|
|
|
2023-07-12 09:38:40 -07:00
|
|
|
namespace facebook::yoga::vanillajni {
|
2019-10-08 17:48:32 -07:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
JavaVM* globalVm = NULL;
|
|
|
|
struct JavaVMInitializer {
|
|
|
|
JavaVMInitializer(JavaVM* vm) {
|
|
|
|
if (!vm) {
|
|
|
|
logErrorMessageAndDie(
|
|
|
|
"You cannot pass a NULL JavaVM to ensureInitialized");
|
|
|
|
}
|
|
|
|
globalVm = vm;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
jint ensureInitialized(JNIEnv** env, JavaVM* vm) {
|
|
|
|
static JavaVMInitializer init(vm);
|
|
|
|
|
|
|
|
if (!env) {
|
|
|
|
logErrorMessageAndDie(
|
|
|
|
"Need to pass a valid JNIEnv pointer to vanillajni initialization "
|
|
|
|
"routine");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (vm->GetEnv(reinterpret_cast<void**>(env), JNI_VERSION_1_6) != JNI_OK) {
|
|
|
|
logErrorMessageAndDie(
|
|
|
|
"Error retrieving JNIEnv during initialization of vanillajni");
|
|
|
|
}
|
|
|
|
|
|
|
|
return JNI_VERSION_1_6;
|
|
|
|
}
|
|
|
|
|
2019-11-01 11:45:19 -07:00
|
|
|
// TODO why we need JNIEXPORT for getCurrentEnv ?
|
|
|
|
JNIEXPORT JNIEnv* getCurrentEnv() {
|
2019-10-08 17:48:32 -07:00
|
|
|
JNIEnv* env;
|
2023-09-13 20:12:55 -07:00
|
|
|
jint ret = globalVm->GetEnv((void**)&env, JNI_VERSION_1_6);
|
2019-10-08 17:48:32 -07:00
|
|
|
if (ret != JNI_OK) {
|
|
|
|
logErrorMessageAndDie(
|
|
|
|
"There was an error retrieving the current JNIEnv. Make sure the "
|
|
|
|
"current thread is attached");
|
|
|
|
}
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logErrorMessageAndDie(const char* message) {
|
2023-09-13 20:12:55 -07:00
|
|
|
(void)message;
|
2019-10-08 17:48:32 -07:00
|
|
|
VANILLAJNI_LOG_ERROR(
|
|
|
|
"VanillaJni",
|
|
|
|
"Aborting due to error detected in native code: %s",
|
|
|
|
message);
|
|
|
|
VANILLAJNI_DIE();
|
|
|
|
}
|
|
|
|
|
|
|
|
void assertNoPendingJniException(JNIEnv* env) {
|
2019-10-22 10:45:20 -07:00
|
|
|
if (env->ExceptionCheck() == JNI_FALSE) {
|
|
|
|
return;
|
2019-10-08 17:48:32 -07:00
|
|
|
}
|
2019-10-22 10:45:20 -07:00
|
|
|
|
|
|
|
auto throwable = env->ExceptionOccurred();
|
|
|
|
if (!throwable) {
|
|
|
|
logErrorMessageAndDie("Unable to get pending JNI exception.");
|
|
|
|
}
|
|
|
|
env->ExceptionClear();
|
2019-12-02 05:21:56 -08:00
|
|
|
throw YogaJniException(throwable);
|
2019-10-08 17:48:32 -07:00
|
|
|
}
|
|
|
|
|
2019-12-02 05:21:56 -08:00
|
|
|
void assertNoPendingJniExceptionIf(JNIEnv* env, bool condition) {
|
|
|
|
if (!condition) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (env->ExceptionCheck() == JNI_TRUE) {
|
|
|
|
assertNoPendingJniException(env);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw YogaJniException();
|
|
|
|
}
|
|
|
|
|
2023-07-12 09:38:40 -07:00
|
|
|
} // namespace facebook::yoga::vanillajni
|