From cd01be302945819feb38deb3c16f6244c3f3cf43 Mon Sep 17 00:00:00 2001 From: Teodor Ciuraru Date: Sun, 9 Jun 2024 18:19:40 +0300 Subject: [PATCH] refactor: add code review recommendations --- android/cpp-adapter.cpp | 30 +++++++++++++++------------- cpp/react-native-jsi-module.cpp | 10 +++++----- ios/JsiModule.h | 1 - rust_multiply_lib/.cargo/config.toml | 16 +++++++++++++++ rust_multiply_lib/Cargo.toml | 12 +++++------ rust_multiply_lib/src/lib.rs | 6 +++--- src/index.tsx | 20 +++++++++---------- 7 files changed, 56 insertions(+), 39 deletions(-) create mode 100644 rust_multiply_lib/.cargo/config.toml diff --git a/android/cpp-adapter.cpp b/android/cpp-adapter.cpp index 57b84ec..648fce8 100644 --- a/android/cpp-adapter.cpp +++ b/android/cpp-adapter.cpp @@ -5,6 +5,22 @@ using namespace facebook::jsi; // Optional: +void bridgePlatformFunctions(Runtime &jsi); + +extern "C" JNIEXPORT void JNICALL +Java_com_jsimodule_JsiModuleModule_jniBridgeJSIFunctions(JNIEnv *env, jobject thiz, jobject context, + jlong jsi_pointer) +{ + Runtime *jsi = reinterpret_cast(jsi_pointer); + if (jsi) + { + jsimodule::bridgeJSIFunctions(*jsi); + + // Optional: bridge some platform specific logic too. + bridgePlatformFunctions(*jsi); + } +} + void bridgePlatformFunctions(Runtime &jsi) { auto getDeviceName = Function::createFromHostFunction(jsi, @@ -20,18 +36,4 @@ void bridgePlatformFunctions(Runtime &jsi) }); jsi.global().setProperty(jsi, "getDeviceName", std::move(getDeviceName)); -} - -extern "C" JNIEXPORT void JNICALL -Java_com_jsimodule_JsiModuleModule_jniBridgeJSIFunctions(JNIEnv *env, jobject thiz, jobject context, - jlong jsi_pointer) -{ - Runtime *jsi = reinterpret_cast(jsi_pointer); - if (jsi) - { - jsimodule::bridgeJSIFunctions(*jsi); - - // Optional: bridge some platform specific logic too. - bridgePlatformFunctions(*jsi); - } } \ No newline at end of file diff --git a/cpp/react-native-jsi-module.cpp b/cpp/react-native-jsi-module.cpp index e3dbf9e..e17f539 100644 --- a/cpp/react-native-jsi-module.cpp +++ b/cpp/react-native-jsi-module.cpp @@ -1,16 +1,15 @@ -#include #include "react-native-jsi-module.h" extern "C" { - double multiply(double a, double b); // Declaration of the Rust function + // For instance, C++'s `double` type is the equivalent of Rust's `f64` + double rust_multiply(double a, double b); } namespace jsimodule { void bridgeJSIFunctions(Runtime &jsi) { - // Define `multiplyJSI` using JSI idioms. auto multiplyJSI = Function::createFromHostFunction( jsi, PropNameID::forAscii(jsi, "multiplyJSI"), @@ -25,10 +24,11 @@ namespace jsimodule double a = arguments[0].asNumber(); double b = arguments[1].asNumber(); - return multiply(a, b); + double ret = rust_multiply(a, b); + + return Value(ret); }); - // Export the `multiply` function to React Native's global object jsi.global().setProperty(jsi, "multiply", std::move(multiplyJSI)); } diff --git a/ios/JsiModule.h b/ios/JsiModule.h index 5b83b4b..b8c71d0 100644 --- a/ios/JsiModule.h +++ b/ios/JsiModule.h @@ -4,5 +4,4 @@ #import @interface JsiModule : NSObject - @end diff --git a/rust_multiply_lib/.cargo/config.toml b/rust_multiply_lib/.cargo/config.toml new file mode 100644 index 0000000..b66a0cc --- /dev/null +++ b/rust_multiply_lib/.cargo/config.toml @@ -0,0 +1,16 @@ +# Convenience shorthands. +[alias] +build-ios = ["build", "--target", "aarch64-apple-ios"] +build-android = ["build", "--target", "aarch64-linux-android"] + +# Linker configuration settings. +[target.aarch64-apple-ios] +rustflags = [ + "-C", "link-arg=-isysroot", "-C", "link-arg=$(xcrun --sdk iphoneos --show-sdk-path)", + "-C", "link-arg=-arch", "-C", "link-arg=arm64", + "-C", "link-arg=-mios-version-min=10.0" +] + +[target.aarch64-linux-android] +linker = "aarch64-linux-android21-clang" +ar = "aarch64-linux-android-ar" \ No newline at end of file diff --git a/rust_multiply_lib/Cargo.toml b/rust_multiply_lib/Cargo.toml index c1ce510..9ca1914 100644 --- a/rust_multiply_lib/Cargo.toml +++ b/rust_multiply_lib/Cargo.toml @@ -3,10 +3,10 @@ name = "rust_multiply_lib" version = "0.1.0" edition = "2021" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] - [lib] -crate-type = ["staticlib"] - +crate-type = [ + "staticlib", # Generates a C-compatible static library. + # Optional: Also compile as a Rust library to enable Cargo features + # like `tests/`, doc tests, and `examples/` + "lib", +] \ No newline at end of file diff --git a/rust_multiply_lib/src/lib.rs b/rust_multiply_lib/src/lib.rs index 3af2389..e5dfced 100644 --- a/rust_multiply_lib/src/lib.rs +++ b/rust_multiply_lib/src/lib.rs @@ -1,4 +1,4 @@ -#[no_mangle] -pub extern "C" fn multiply(a: f64, b: f64) -> f64 { +#[no_mangle] pub extern "C" +fn rust_multiply(a: f64, b: f64) -> f64 { a * b -} +} \ No newline at end of file diff --git a/src/index.tsx b/src/index.tsx index d05edee..642e355 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -6,16 +6,16 @@ const LINKING_ERROR = '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n'; -const JsiModule = NativeModules.JsiModule - ? NativeModules.JsiModule - : new Proxy( - {}, - { - get() { - throw new Error(LINKING_ERROR); - }, - } - ); +const JsiModule = + NativeModules.JsiModule ?? + new Proxy( + {}, + { + get() { + throw new Error(LINKING_ERROR); + }, + } + ); const jsiCore = global as unknown as { multiply(a: number, b: number): number;