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
10 changes: 6 additions & 4 deletions android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ execute_process(
OUTPUT_VARIABLE UNIFFI_BINDGEN_PATH
OUTPUT_STRIP_TRAILING_WHITESPACE
)
string(REGEX
REPLACE "/package\\.json$" ""
UNIFFI_BINDGEN_PATH ${UNIFFI_BINDGEN_PATH}
)
# Get the directory; get_filename_component and cmake_path will normalize
# paths with Windows path separators.
get_filename_component(UNIFFI_BINDGEN_PATH "${UNIFFI_BINDGEN_PATH}" DIRECTORY)

# Specifies a path to native header files.
include_directories(
Expand All @@ -38,6 +37,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}/libloro_rs.a
Expand Down
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ android {
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
consumerProguardFiles 'proguard-rules.pro'

buildFeatures {
prefab true
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_loro_lororeactnative_LoroReactNativeModule_nativeInstallRustCrate(
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 lororeactnative::installRustCrate(*runtime, jsCallInvoker);
}

Expand Down
10 changes: 5 additions & 5 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ PODS:
- hermes-engine (0.79.2):
- hermes-engine/Pre-built (= 0.79.2)
- hermes-engine/Pre-built (0.79.2)
- loro-react-native (1.10.3):
- loro-react-native (1.13.1):
- DoubleConversion
- glog
- hermes-engine
Expand All @@ -31,7 +31,7 @@ PODS:
- ReactCodegen
- ReactCommon/turbomodule/bridging
- ReactCommon/turbomodule/core
- uniffi-bindgen-react-native (= 0.28.3-5)
- uniffi-bindgen-react-native (= 0.31.0-2)
- Yoga
- RCT-Folly (2024.11.18.00):
- boost
Expand Down Expand Up @@ -1680,7 +1680,7 @@ PODS:
- React-perflogger (= 0.79.2)
- React-utils (= 0.79.2)
- SocketRocket (0.7.1)
- uniffi-bindgen-react-native (0.28.3-5):
- uniffi-bindgen-react-native (0.31.0-2):
- React-Core
- Yoga (0.0.0)

Expand Down Expand Up @@ -1917,7 +1917,7 @@ SPEC CHECKSUMS:
fmt: a40bb5bd0294ea969aaaba240a927bd33d878cdd
glog: 5683914934d5b6e4240e497e0f4a3b42d1854183
hermes-engine: 314be5250afa5692b57b4dd1705959e1973a8ebe
loro-react-native: ef58f3d1b3bf19efb88e422080ed52552bf5c693
loro-react-native: 20b44780910281515017a631b3f6003185366cef
RCT-Folly: 36fe2295e44b10d831836cc0d1daec5f8abcf809
RCTDeprecation: 83ffb90c23ee5cea353bd32008a7bca100908f8c
RCTRequired: eb7c0aba998009f47a540bec9e9d69a54f68136e
Expand Down Expand Up @@ -1981,7 +1981,7 @@ SPEC CHECKSUMS:
ReactCodegen: b9c6802762046e8000655cee5ba22f6422620f03
ReactCommon: 4d0da92a5eb8da86c08e3ec34bd23ab439fb2461
SocketRocket: d4aabe649be1e368d1318fdf28a022d714d65748
uniffi-bindgen-react-native: 018090e6c342b31e1189eb4d098860e9d44caa86
uniffi-bindgen-react-native: 205a15613d97a49fe17d5bda57f7fa46a7bbafd0
Yoga: c758bfb934100bb4bf9cbaccb52557cee35e8bdf

PODFILE CHECKSUM: a5ca260180a96f18394d1d29176689cad6469ad8
Expand Down
4 changes: 2 additions & 2 deletions loro-react-native.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/LoroFfiFramework.xcframework"
s.dependency "uniffi-bindgen-react-native", "0.28.3-5"
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 All @@ -41,4 +41,4 @@ Pod::Spec.new do |s|
s.dependency "ReactCommon/turbomodule/core"
end
end
end
end
Loading
Loading