Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ReactNativeMatrixSdk.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Pod::Spec.new do |s|

s.source_files = "ios/**/*.{h,m,mm,swift}", "ios/generated/**/*.{h,m,mm}", "cpp/**/*.{hpp,cpp,c,h}", "cpp/generated/**/*.{hpp,cpp,c,h}"
s.vendored_frameworks = "build/RnMatrixRustSdk.xcframework"
s.dependency "uniffi-bindgen-react-native", "0.29.3-1"
s.dependency "uniffi-bindgen-react-native", "0.31.0-2"

# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
Expand Down
3 changes: 3 additions & 0 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fstack-protector-all")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Werror")

# Set linker flags for 16KB page size alignment (required for Android 15+)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,-z,max-page-size=16384")

cmake_path(
SET MY_RUST_LIB
${CMAKE_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libmatrix_sdk_ffi.so
Expand Down
32 changes: 6 additions & 26 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,14 @@ Java_com_unomed_reactnativematrixsdk_ReactNativeMatrixSdkModule_nativeInstallRus
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();
using JCallInvokerHolder = facebook::react::CallInvokerHolder;

auto holderLocal = facebook::jni::make_local(callInvokerHolderJavaObj);
auto holderRef = facebook::jni::static_ref_cast<JCallInvokerHolder::javaobject>(holderLocal);
auto* holderCxx = holderRef->cthis();
auto jsCallInvoker = holderCxx->getCallInvoker();
auto runtime = reinterpret_cast<jsi::Runtime *>(rtPtr);

return unomed_reactnativematrixsdk::installRustCrate(*runtime, jsCallInvoker);
}

Expand Down
Loading
Loading