Skip to content

Commit

Permalink
refactor: add code review recommendations
Browse files Browse the repository at this point in the history
  • Loading branch information
teodorciuraru committed Jun 9, 2024
1 parent ec48c85 commit cd01be3
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 39 deletions.
30 changes: 16 additions & 14 deletions android/cpp-adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Runtime *>(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,
Expand All @@ -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<Runtime *>(jsi_pointer);
if (jsi)
{
jsimodule::bridgeJSIFunctions(*jsi);

// Optional: bridge some platform specific logic too.
bridgePlatformFunctions(*jsi);
}
}
10 changes: 5 additions & 5 deletions cpp/react-native-jsi-module.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#include <jsi/jsi.h>
#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"),
Expand All @@ -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));
}

Expand Down
1 change: 0 additions & 1 deletion ios/JsiModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,4 @@

#import <React/RCTBridgeModule.h>
@interface JsiModule : NSObject <RCTBridgeModule>

@end
16 changes: 16 additions & 0 deletions rust_multiply_lib/.cargo/config.toml
Original file line number Diff line number Diff line change
@@ -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"
12 changes: 6 additions & 6 deletions rust_multiply_lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
6 changes: 3 additions & 3 deletions rust_multiply_lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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
}
}
20 changes: 10 additions & 10 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit cd01be3

Please sign in to comment.