working rust code

This commit is contained in:
EthanShoeDev
2025-09-13 03:07:26 -04:00
parent bbc1b8a9a0
commit 66cf7ed4d5
4 changed files with 80 additions and 2 deletions

View File

@@ -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

View File

@@ -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 <jni.h>
#include <jsi/jsi.h>
#include <ReactCommon/CallInvokerHolder.h>
+#include <fbjni/fbjni.h>
#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<facebook::react::CallInvokerHolder*>(nativePointerValue);
- auto jsCallInvoker = nativePointer->getCallInvoker();
-
- auto runtime = reinterpret_cast<jsi::Runtime *>(rtPtr);
- return {{ ns }}::installRustCrate(*runtime, jsCallInvoker);
+ try {
+ if (callInvokerHolderJavaObj == nullptr) {
+ return false;
+ }
+
+ auto alias = facebook::jni::alias_ref<jobject>(callInvokerHolderJavaObj);
+ auto holder = facebook::jni::static_ref_cast<facebook::react::CallInvokerHolder::javaobject>(alias);
+ if (!holder) {
+ return false;
+ }
+
+ auto jsCallInvoker = holder->cthis()->getCallInvoker();
+ if (!jsCallInvoker) {
+ return false;
+ }
+
+ auto runtime = reinterpret_cast<jsi::Runtime *>(rtPtr);
+ return {{ ns }}::installRustCrate(*runtime, jsCallInvoker);
+ } catch (...) {
+ return false;
+ }
}
extern "C"

9
pnpm-lock.yaml generated
View File

@@ -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:

View File

@@ -10,3 +10,6 @@ onlyBuiltDependencies:
- oxc-resolver
- sharp
- unrs-resolver
patchedDependencies:
uniffi-bindgen-react-native: patches/uniffi-bindgen-react-native.patch