Skip to content
Draft
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
37 changes: 21 additions & 16 deletions android/CMakeLists.txt
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This patch is pulled out from #72, it makes this library compile for RN 76 on Android, but it still does not work for the new architecure.

Seems like the library is not linked properly when fabric is enabled.

Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
project("react-native-bignumber")
cmake_minimum_required(VERSION 3.9.0)

set(PACKAGE_NAME "reactnativeBigNumber")
set(PACKAGE_NAME "react-native-bignumber")
set(BUILD_DIR ${CMAKE_SOURCE_DIR}/build)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(CMAKE_CXX_STANDARD 20)

# Consume shared libraries and headers from prefabs
find_package(fbjni REQUIRED CONFIG)
find_package(ReactAndroid REQUIRED CONFIG)
find_package(openssl REQUIRED CONFIG)
find_library(LOG_LIB log)

include_directories(
"../cpp"
Expand Down Expand Up @@ -40,29 +43,31 @@ add_library(
set_target_properties(
${PACKAGE_NAME}
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD 20
CXX_EXTENSIONS OFF
POSITION_INDEPENDENT_CODE ON
)

file(GLOB LIBRN_DIR "${BUILD_DIR}/react-native-0*/jni/${ANDROID_ABI}")

find_library(
log-lib
log
)

find_package(openssl REQUIRED CONFIG)

target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::turbomodulejsijni
${LOG_LIB}
fbjni::fbjni
${log-lib}
ReactAndroid::jsi
ReactAndroid::reactnativejni
ReactAndroid::react_nativemodule_core
android
openssl::crypto
openssl::ssl
)

if (ReactAndroid_VERSION_MINOR GREATER_EQUAL 76)
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::reactnative
)
else ()
target_link_libraries(
${PACKAGE_NAME}
ReactAndroid::reactnativejni
ReactAndroid::turbomodulejsijni
ReactAndroid::react_nativemodule_core
)
endif ()
4 changes: 3 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,14 @@ android {
'**/libreactnativejni.so',
'**/libjsi.so',
'**/libreact_nativemodule_core.so',
'**/libturbomodulejsijni.so'
'**/libturbomodulejsijni.so',
'**/libreactnative.so'
]
}
resources {
excludes += ['**/MANIFEST.MF']
}
exclude 'META-INF/LICENSE.md'
}


Expand Down
66 changes: 61 additions & 5 deletions android/src/main/cpp/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,74 @@ explicit BigNumberCppAdapter() {
}

void install(jsi::Runtime& runtime, std::shared_ptr<facebook::react::CallInvoker> jsCallInvoker) {
auto workerQueue = std::make_shared<margelo::DispatchQueue::dispatch_queue>("Margelo MGBigNumber Thread");
auto hostObject = std::make_shared<margelo::MGBigNumberHostObject>(jsCallInvoker, workerQueue);
auto object = jsi::Object::createFromHostObject(runtime, hostObject);
runtime.global().setProperty(runtime, "__BigNumberProxy", std::move(object));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's migrate it to Nitro after we made the RN 76 upgrade. WIth Nitro, we no longer need any of this install(..) f*ckery.

try {
if (!jsCallInvoker) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "jsCallInvoker is null in install");
return;
}

auto workerQueue = std::make_shared<margelo::DispatchQueue::dispatch_queue>("Margelo MGBigNumber Thread");
if (!workerQueue) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Failed to create worker queue");
return;
}

auto hostObject = std::make_shared<margelo::MGBigNumberHostObject>(jsCallInvoker, workerQueue);

if (!hostObject) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Host object creation failed");
return;
}

auto object = jsi::Object::createFromHostObject(runtime, hostObject);

// try {
// auto propNames = object.getPropertyNames(runtime);
// __android_log_print(ANDROID_LOG_INFO, "BigNumber", "Host object has %d properties", propNames.size(runtime));
// } catch (const jsi::JSError& e) {
// __android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Error inspecting host object: %s", e.what());
// return;
// }

try {
runtime.global().setProperty(runtime, "__BigNumberProxy", std::move(object));
__android_log_print(ANDROID_LOG_INFO, "BigNumber", "Property set successfully");
} catch (const jsi::JSError& e) {
jni::throwNewJavaException("java/lang/IllegalStateException", "JSI Error setting property: %s", e.what());
} catch (const std::exception& e) {
jni::throwNewJavaException("java/lang/IllegalStateException", "Exception setting property: %s", e.what());
} catch (...) {
jni::throwNewJavaException("java/lang/IllegalStateException", "Unknown exception setting property");
}
} catch (const std::exception& e) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Exception in install: %s", e.what());
} catch (...) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Unknown exception in install");
}
}

void nativeInstall(jlong jsiPtr, jni::alias_ref<facebook::react::CallInvokerHolder::javaobject>
jsCallInvokerHolder) {
__android_log_print(ANDROID_LOG_INFO, "BigNumber", "nativeInstall called");

if (!jsCallInvokerHolder) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "jsCallInvokerHolder is null");
return;
}

auto jsCallInvoker = jsCallInvokerHolder->cthis()->getCallInvoker();
if (!jsCallInvoker) {
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "jsCallInvoker is null");
return;
}

auto runtime = reinterpret_cast<jsi::Runtime*>(jsiPtr);
if (runtime) {
install(*runtime, jsCallInvoker);
} else {
// if runtime was nullptr, MGBigNumber will not be installed. This should only happen while Remote Debugging (Chrome), but will be weird either way.
__android_log_print(ANDROID_LOG_ERROR, "BigNumber", "Runtime pointer is null");
}
// if runtime was nullptr, MGBigNumber will not be installed. This should only happen while Remote Debugging (Chrome), but will be weird either way.
}

static void registerNatives() {
Expand All @@ -47,7 +101,9 @@ friend HybridBase;
};

JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *) {
__android_log_print(ANDROID_LOG_INFO, "BigNumber", "JNI_OnLoad called");
return facebook::jni::initialize(vm, [] {
__android_log_print(ANDROID_LOG_INFO, "BigNumber", "Registering natives");
BigNumberCppAdapter::registerNatives();
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public boolean install() {
return false;
}
Log.i(NAME, "Loading C++ library...");
System.loadLibrary("reactnativeBigNumber");
System.loadLibrary("react-native-bignumber");

JavaScriptContextHolder jsContext = getReactApplicationContext().getJavaScriptContextHolder();
CallInvokerHolderImpl jsCallInvokerHolder = (CallInvokerHolderImpl) getReactApplicationContext()
Expand Down
25 changes: 23 additions & 2 deletions cpp/MGBigNumberHostObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,35 @@ namespace margelo
{
len = std::max(1, numberLen);
}
else
{
len = std::min(len, numberLen);
}

unsigned char *to = new unsigned char[len];
if (le)
{
BN_bn2lebinpad(thiz->bign, to, len);
if (len == numberLen) {
BN_bn2lebinpad(thiz->bign, to, len);
} else {
// For smaller lengths, we need to get the full number and truncate
unsigned char *full = new unsigned char[numberLen];
BN_bn2lebinpad(thiz->bign, full, numberLen);
memcpy(to, full, len);
delete[] full;
}
}
else
{
BN_bn2binpad(thiz->bign, to, len);
if (len == numberLen) {
BN_bn2binpad(thiz->bign, to, len);
} else {
// For smaller lengths, we need to get the full number and truncate
unsigned char *full = new unsigned char[numberLen];
BN_bn2binpad(thiz->bign, full, numberLen);
memcpy(to, full, len);
delete[] full;
}
}

jsi::Array res(runtime, len);
Expand Down
4 changes: 4 additions & 0 deletions example/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
root: true,
extends: '@react-native',
};
15 changes: 13 additions & 2 deletions example/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ DerivedData
*.hmap
*.ipa
*.xcuserstate
ios/.xcode.env.local
**/.xcode.env.local

# Android/IntelliJ
#
Expand Down Expand Up @@ -56,8 +56,19 @@ yarn-error.log
*.jsbundle

# Ruby / CocoaPods
/ios/Pods/
**/Pods/
/vendor/bundle/

# Temporary files created by Metro to check the health of the file watcher
.metro-health-check*

# testing
/coverage

# Yarn
.yarn/*
!.yarn/patches
!.yarn/plugins
!.yarn/releases
!.yarn/sdks
!.yarn/versions
9 changes: 6 additions & 3 deletions example/Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
source 'https://rubygems.org'

# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
ruby '>= 2.6.10'

gem 'cocoapods', '>= 1.11.3'
ruby ">= 2.6.10"

# Exclude problematic versions of cocoapods and activesupport that causes build failures.
gem 'cocoapods', '>= 1.13', '!= 1.15.0', '!= 1.15.1'
gem 'activesupport', '>= 6.1.7.5', '!= 7.1.0'
gem 'xcodeproj', '< 1.26.0'
63 changes: 41 additions & 22 deletions example/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,26 +1,38 @@
GEM
remote: https://rubygems.org/
specs:
CFPropertyList (3.0.6)
CFPropertyList (3.0.7)
base64
nkf
rexml
activesupport (7.0.4.3)
concurrent-ruby (~> 1.0, >= 1.0.2)
activesupport (7.2.2.1)
base64
benchmark (>= 0.3)
bigdecimal
concurrent-ruby (~> 1.0, >= 1.3.1)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
logger (>= 1.4.2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.3)
public_suffix (>= 2.0.2, < 6.0)
securerandom (>= 0.3)
tzinfo (~> 2.0, >= 2.0.5)
addressable (2.8.7)
public_suffix (>= 2.0.2, < 7.0)
algoliasearch (1.27.5)
httpclient (~> 2.8, >= 2.8.3)
json (>= 1.5.1)
atomos (0.1.3)
base64 (0.2.0)
benchmark (0.4.0)
bigdecimal (3.1.9)
claide (1.1.0)
cocoapods (1.12.0)
cocoapods (1.15.2)
addressable (~> 2.8)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.12.0)
cocoapods-core (= 1.15.2)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 1.6.0, < 2.0)
cocoapods-downloader (>= 2.1, < 3.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.6.0, < 2.0)
Expand All @@ -32,8 +44,8 @@ GEM
molinillo (~> 0.8.0)
nap (~> 1.0)
ruby-macho (>= 2.3.0, < 3.0)
xcodeproj (>= 1.21.0, < 2.0)
cocoapods-core (1.12.0)
xcodeproj (>= 1.23.0, < 2.0)
cocoapods-core (1.15.2)
activesupport (>= 5.0, < 8)
addressable (~> 2.8)
algoliasearch (~> 1.0)
Expand All @@ -44,7 +56,7 @@ GEM
public_suffix (~> 4.0)
typhoeus (~> 1.0)
cocoapods-deintegrate (1.0.5)
cocoapods-downloader (1.6.3)
cocoapods-downloader (2.1)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.1)
Expand All @@ -53,43 +65,50 @@ GEM
netrc (~> 0.11)
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.2.2)
concurrent-ruby (1.3.5)
connection_pool (2.5.0)
drb (2.2.1)
escape (0.0.4)
ethon (0.16.0)
ffi (>= 1.15.0)
ffi (1.15.5)
ffi (1.17.1)
fourflusher (2.3.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
httpclient (2.8.3)
i18n (1.12.0)
i18n (1.14.7)
concurrent-ruby (~> 1.0)
json (2.6.3)
minitest (5.18.0)
json (2.9.1)
logger (1.6.5)
minitest (5.25.4)
molinillo (0.8.0)
nanaimo (0.3.0)
nap (1.1.0)
netrc (0.11.0)
nkf (0.2.0)
public_suffix (4.0.7)
rexml (3.2.5)
rexml (3.4.0)
ruby-macho (2.5.1)
typhoeus (1.4.0)
securerandom (0.4.1)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
xcodeproj (1.22.0)
xcodeproj (1.25.1)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (~> 3.2.4)
rexml (>= 3.3.6, < 4.0)

PLATFORMS
ruby

DEPENDENCIES
cocoapods (>= 1.11.3)
activesupport (>= 6.1.7.5, != 7.1.0)
cocoapods (>= 1.13, != 1.15.1, != 1.15.0)
xcodeproj (< 1.26.0)

RUBY VERSION
ruby 2.7.6p219
Expand Down
1 change: 0 additions & 1 deletion example/_node-version

This file was deleted.

Loading
Loading