From 66cf7ed4d5a7b90990ad8409a0154e515648c014 Mon Sep 17 00:00:00 2001 From: EthanShoeDev <13422990+EthanShoeDev@users.noreply.github.com> Date: Sat, 13 Sep 2025 03:07:26 -0400 Subject: [PATCH] working rust code --- docs/todos.md | 1 + patches/uniffi-bindgen-react-native.patch | 69 +++++++++++++++++++++++ pnpm-lock.yaml | 9 ++- pnpm-workspace.yaml | 3 + 4 files changed, 80 insertions(+), 2 deletions(-) create mode 100644 patches/uniffi-bindgen-react-native.patch diff --git a/docs/todos.md b/docs/todos.md index 3d417db..88fabe5 100644 --- a/docs/todos.md +++ b/docs/todos.md @@ -38,4 +38,5 @@ Uniffi is broken on RN 0.80 +- https://github.com/jhugman/uniffi-bindgen-react-native/issues/295 - https://github.com/realm/realm-js/issues/7011#issuecomment-3149613234 diff --git a/patches/uniffi-bindgen-react-native.patch b/patches/uniffi-bindgen-react-native.patch new file mode 100644 index 0000000..cdf7573 --- /dev/null +++ b/patches/uniffi-bindgen-react-native.patch @@ -0,0 +1,69 @@ +diff --git a/crates/ubrn_cli/templates/jsi/android/cpp-adapter.cpp b/crates/ubrn_cli/templates/jsi/android/cpp-adapter.cpp +index d8d1c1a1c1269ecee9467fd0ef809dcb1e375923..a031428d1eaecec2df94e7148e9d45d15642158d 100644 +--- a/crates/ubrn_cli/templates/jsi/android/cpp-adapter.cpp ++++ b/crates/ubrn_cli/templates/jsi/android/cpp-adapter.cpp +@@ -2,6 +2,7 @@ + #include + #include + #include ++#include + #include "{{ self.config.project.cpp_filename() }}.h" + {%- let package_name = self.config.project.android.package_name().replace(".", "_") %} + {%- let name = self.config.project.module_cpp() %} +@@ -29,35 +30,27 @@ JNIEXPORT jboolean JNICALL + jlong rtPtr, + jobject callInvokerHolderJavaObj + ) { +- // https://github.com/realm/realm-js/blob/main/packages/realm/binding/android/src/main/cpp/io_realm_react_RealmReactModule.cpp#L122-L145 +- // React Native uses the fbjni library for handling JNI, which has the concept of "hybrid objects", +- // which are Java objects containing a pointer to a C++ object. The CallInvokerHolder, which has the +- // invokeAsync method we want access to, is one such hybrid object. +- // Rather than reworking our code to use fbjni throughout, this code unpacks the C++ object from the Java +- // object `callInvokerHolderJavaObj` manually, based on reverse engineering the fbjni code. +- +- // 1. Get the Java object referred to by the mHybridData field of the Java holder object +- auto callInvokerHolderClass = env->GetObjectClass(callInvokerHolderJavaObj); +- auto hybridDataField = env->GetFieldID(callInvokerHolderClass, "mHybridData", "Lcom/facebook/jni/HybridData;"); +- auto hybridDataObj = env->GetObjectField(callInvokerHolderJavaObj, hybridDataField); +- +- // 2. Get the destructor Java object referred to by the mDestructor field from the myHybridData Java object +- auto hybridDataClass = env->FindClass("com/facebook/jni/HybridData"); +- auto destructorField = +- env->GetFieldID(hybridDataClass, "mDestructor", "Lcom/facebook/jni/HybridData$Destructor;"); +- auto destructorObj = env->GetObjectField(hybridDataObj, destructorField); +- +- // 3. Get the mNativePointer field from the mDestructor Java object +- auto destructorClass = env->FindClass("com/facebook/jni/HybridData$Destructor"); +- auto nativePointerField = env->GetFieldID(destructorClass, "mNativePointer", "J"); +- auto nativePointerValue = env->GetLongField(destructorObj, nativePointerField); +- +- // 4. Cast the mNativePointer back to its C++ type +- auto nativePointer = reinterpret_cast(nativePointerValue); +- auto jsCallInvoker = nativePointer->getCallInvoker(); +- +- auto runtime = reinterpret_cast(rtPtr); +- return {{ ns }}::installRustCrate(*runtime, jsCallInvoker); ++ try { ++ if (callInvokerHolderJavaObj == nullptr) { ++ return false; ++ } ++ ++ auto alias = facebook::jni::alias_ref(callInvokerHolderJavaObj); ++ auto holder = facebook::jni::static_ref_cast(alias); ++ if (!holder) { ++ return false; ++ } ++ ++ auto jsCallInvoker = holder->cthis()->getCallInvoker(); ++ if (!jsCallInvoker) { ++ return false; ++ } ++ ++ auto runtime = reinterpret_cast(rtPtr); ++ return {{ ns }}::installRustCrate(*runtime, jsCallInvoker); ++ } catch (...) { ++ return false; ++ } + } + + extern "C" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 6c8f48b..90cfc05 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -4,6 +4,11 @@ settings: autoInstallPeers: true excludeLinksFromLockfile: false +patchedDependencies: + uniffi-bindgen-react-native: + hash: 527b712c8fb029b29d9ac7caa72e593fa37a6dcebb63e15a56e21e75ffcb88ec + path: patches/uniffi-bindgen-react-native.patch + importers: .: @@ -239,7 +244,7 @@ importers: dependencies: uniffi-bindgen-react-native: specifier: 0.29.3-1 - version: 0.29.3-1 + version: 0.29.3-1(patch_hash=527b712c8fb029b29d9ac7caa72e593fa37a6dcebb63e15a56e21e75ffcb88ec) devDependencies: '@eslint/compat': specifier: ^1.3.2 @@ -18431,7 +18436,7 @@ snapshots: unicorn-magic@0.3.0: {} - uniffi-bindgen-react-native@0.29.3-1: {} + uniffi-bindgen-react-native@0.29.3-1(patch_hash=527b712c8fb029b29d9ac7caa72e593fa37a6dcebb63e15a56e21e75ffcb88ec): {} unified@11.0.5: dependencies: diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index c712e51..2a7eb3f 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -10,3 +10,6 @@ onlyBuiltDependencies: - oxc-resolver - sharp - unrs-resolver + +patchedDependencies: + uniffi-bindgen-react-native: patches/uniffi-bindgen-react-native.patch