diff --git a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java index 5b29489851e..88595f38b90 100644 --- a/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java +++ b/ReactAndroid/src/main/java/com/facebook/yoga/YogaNative.java @@ -111,4 +111,8 @@ public class YogaNative { static native void jni_YGNodePrint(long nativePointer); static native void jni_YGNodeSetStyleInputs(long nativePointer, float[] styleInputsArray, int size); static native long jni_YGNodeClone(long nativePointer); + + + // JNI methods that use Vanilla JNI + public static native void jni_YGNodeStyleSetFlexJNI(long nativePointer, float flex); } diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk b/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk index 7fa2b376b37..77bdf57424d 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk +++ b/ReactAndroid/src/main/jni/first-party/yogajni/Android.mk @@ -11,7 +11,8 @@ LOCAL_MODULE := yoga LOCAL_SRC_FILES := \ jni/YGJNI.cpp \ - jni/YGJTypes.cpp + jni/YGJTypes.cpp \ + jni/YGJNIVanilla.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/jni diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp index f23d0f2b952..88e325d29b3 100644 --- a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNI.cpp @@ -15,6 +15,7 @@ #include #include "YGJTypes.h" +#include "YGJNIVanilla.h" using namespace facebook::jni; using namespace std; @@ -893,7 +894,7 @@ void jni_YGNodeStyleSetBorder(jlong nativePointer, jint edge, jfloat border) { makeCriticalNativeMethod_DO_NOT_USE_OR_YOU_WILL_BE_FIRED(#name, name) jint JNI_OnLoad(JavaVM* vm, void*) { - return initialize(vm, [] { + jint ret = initialize(vm, [] { registerNatives( "com/facebook/yoga/YogaNative", { @@ -993,4 +994,6 @@ jint JNI_OnLoad(JavaVM* vm, void*) { jni_YGConfigSetShouldDiffLayoutWithoutLegacyStretchBehaviour), }); }); + YGJNIVanilla::registerNatives(Environment::current()); + return ret; } diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp new file mode 100644 index 00000000000..d169d0e5073 --- /dev/null +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +#include "jni.h" +#include "YGJNIVanilla.h" +#include + +static inline YGNodeRef _jlong2YGNodeRef(jlong addr) { + return reinterpret_cast(static_cast(addr)); +} + +void jni_YGNodeStyleSetFlexJNI( + JNIEnv* env, + jobject obj, + jlong nativePointer, + jfloat value) { + YGNodeStyleSetFlex( + _jlong2YGNodeRef(nativePointer), static_cast(value)); +} + +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(); + } +} + +void registerNativeMethods( + JNIEnv* env, + const char* className, + JNINativeMethod methods[], + size_t numMethods) { + jclass clazz = env->FindClass(className); + + assertNoPendingJniException(env); + + env->RegisterNatives(clazz, methods, numMethods); + + assertNoPendingJniException(env); +} + +static JNINativeMethod methods[] = { + {"jni_YGNodeStyleSetFlexJNI", "(JF)V", (void*) jni_YGNodeStyleSetFlexJNI}}; + +void YGJNIVanilla::registerNatives(JNIEnv* env) { + registerNativeMethods( + env, + "com/facebook/yoga/YogaNative", + methods, + sizeof(methods) / sizeof(JNINativeMethod)); +} diff --git a/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h new file mode 100644 index 00000000000..7797a67857a --- /dev/null +++ b/ReactAndroid/src/main/jni/first-party/yogajni/jni/YGJNIVanilla.h @@ -0,0 +1,9 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the LICENSE + * file in the root directory of this source tree. + */ +namespace YGJNIVanilla { +void registerNatives(JNIEnv* env); +};