diff --git a/java/CMakeLists.txt b/java/CMakeLists.txt index 12e69eb0..c55c6bd9 100644 --- a/java/CMakeLists.txt +++ b/java/CMakeLists.txt @@ -29,7 +29,7 @@ add_compile_options( -Wall -std=c++11) -add_library(yoga SHARED jni/YGJNI.cpp jni/YGJTypes.cpp) +add_library(yoga SHARED jni/YGJNI.cpp jni/YGJTypes.cpp jni/YGJNIVanilla.cpp) target_include_directories(yoga PRIVATE ${libfb_DIR}/include diff --git a/java/com/facebook/yoga/YogaNative.java b/java/com/facebook/yoga/YogaNative.java index 5b294898..88595f38 100644 --- a/java/com/facebook/yoga/YogaNative.java +++ b/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/java/jni/YGJNI.cpp b/java/jni/YGJNI.cpp index f23d0f2b..88e325d2 100644 --- a/java/jni/YGJNI.cpp +++ b/java/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/java/jni/YGJNIVanilla.cpp b/java/jni/YGJNIVanilla.cpp new file mode 100644 index 00000000..d169d0e5 --- /dev/null +++ b/java/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/java/jni/YGJNIVanilla.h b/java/jni/YGJNIVanilla.h new file mode 100644 index 00000000..7797a678 --- /dev/null +++ b/java/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); +};