|
| 1 | +// Generated by uniffi-bindgen-react-native |
1 | 2 | #include <jni.h>
|
2 |
| -#include "nostr-sdk-react-native.h" |
| 3 | +#include <jsi/jsi.h> |
| 4 | +#include <ReactCommon/CallInvokerHolder.h> |
| 5 | +#include "rust-nostr-nostr-sdk-react-native.h" |
3 | 6 |
|
4 |
| -extern "C" |
5 |
| -JNIEXPORT jdouble JNICALL |
| 7 | +namespace jsi = facebook::jsi; |
| 8 | +namespace react = facebook::react; |
| 9 | + |
| 10 | +// Automated testing checks Java_com_nostrsdkreactnative_NostrSdkReactNativeModule and rustnostr_nostrsdkreactnative |
| 11 | +// by comparing the whole line here. |
| 12 | +/* |
6 | 13 | Java_com_nostrsdkreactnative_NostrSdkReactNativeModule_nativeMultiply(JNIEnv *env, jclass type, jdouble a, jdouble b) {
|
7 |
| - return nostrsdkreactnative::multiply(a, b); |
| 14 | + return rustnostr_nostrsdkreactnative::multiply(a, b); |
| 15 | +} |
| 16 | +*/ |
| 17 | + |
| 18 | +// Installer coming from NostrSdkReactNativeModule |
| 19 | +extern "C" |
| 20 | +JNIEXPORT jboolean JNICALL |
| 21 | +Java_com_nostrsdkreactnative_NostrSdkReactNativeModule_nativeInstallRustCrate( |
| 22 | + JNIEnv *env, |
| 23 | + jclass type, |
| 24 | + jlong rtPtr, |
| 25 | + jobject callInvokerHolderJavaObj |
| 26 | +) { |
| 27 | + // https://github.com/realm/realm-js/blob/main/packages/realm/binding/android/src/main/cpp/io_realm_react_RealmReactModule.cpp#L122-L145 |
| 28 | + // React Native uses the fbjni library for handling JNI, which has the concept of "hybrid objects", |
| 29 | + // which are Java objects containing a pointer to a C++ object. The CallInvokerHolder, which has the |
| 30 | + // invokeAsync method we want access to, is one such hybrid object. |
| 31 | + // Rather than reworking our code to use fbjni throughout, this code unpacks the C++ object from the Java |
| 32 | + // object `callInvokerHolderJavaObj` manually, based on reverse engineering the fbjni code. |
| 33 | + |
| 34 | + // 1. Get the Java object referred to by the mHybridData field of the Java holder object |
| 35 | + auto callInvokerHolderClass = env->GetObjectClass(callInvokerHolderJavaObj); |
| 36 | + auto hybridDataField = env->GetFieldID(callInvokerHolderClass, "mHybridData", "Lcom/facebook/jni/HybridData;"); |
| 37 | + auto hybridDataObj = env->GetObjectField(callInvokerHolderJavaObj, hybridDataField); |
| 38 | + |
| 39 | + // 2. Get the destructor Java object referred to by the mDestructor field from the myHybridData Java object |
| 40 | + auto hybridDataClass = env->FindClass("com/facebook/jni/HybridData"); |
| 41 | + auto destructorField = |
| 42 | + env->GetFieldID(hybridDataClass, "mDestructor", "Lcom/facebook/jni/HybridData$Destructor;"); |
| 43 | + auto destructorObj = env->GetObjectField(hybridDataObj, destructorField); |
| 44 | + |
| 45 | + // 3. Get the mNativePointer field from the mDestructor Java object |
| 46 | + auto destructorClass = env->FindClass("com/facebook/jni/HybridData$Destructor"); |
| 47 | + auto nativePointerField = env->GetFieldID(destructorClass, "mNativePointer", "J"); |
| 48 | + auto nativePointerValue = env->GetLongField(destructorObj, nativePointerField); |
| 49 | + |
| 50 | + // 4. Cast the mNativePointer back to its C++ type |
| 51 | + auto nativePointer = reinterpret_cast<facebook::react::CallInvokerHolder*>(nativePointerValue); |
| 52 | + auto jsCallInvoker = nativePointer->getCallInvoker(); |
| 53 | + |
| 54 | + auto runtime = reinterpret_cast<jsi::Runtime *>(rtPtr); |
| 55 | + return rustnostr_nostrsdkreactnative::installRustCrate(*runtime, jsCallInvoker); |
| 56 | +} |
| 57 | + |
| 58 | +extern "C" |
| 59 | +JNIEXPORT jboolean JNICALL |
| 60 | +Java_com_nostrsdkreactnative_NostrSdkReactNativeModule_nativeCleanupRustCrate(JNIEnv *env, jclass type, jlong rtPtr) { |
| 61 | + auto runtime = reinterpret_cast<jsi::Runtime *>(rtPtr); |
| 62 | + return rustnostr_nostrsdkreactnative::cleanupRustCrate(*runtime); |
8 | 63 | }
|
0 commit comments