diff --git a/cpp/callstack-react-native-rapier.cpp b/cpp/callstack-react-native-rapier.cpp deleted file mode 100644 index 92675b3..0000000 --- a/cpp/callstack-react-native-rapier.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include "callstack-react-native-rapier.h" - -namespace callstack_reactnativerapier { - double multiply(double a, double b) { - return a * b; - } -} diff --git a/cpp/callstack-react-native-rapier.h b/cpp/callstack-react-native-rapier.h deleted file mode 100644 index 582c111..0000000 --- a/cpp/callstack-react-native-rapier.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef CALLSTACK_REACTNATIVERAPIER_H -#define CALLSTACK_REACTNATIVERAPIER_H - -namespace callstack_reactnativerapier { - double multiply(double a, double b); -} - -#endif /* CALLSTACK_REACTNATIVERAPIER_H */ diff --git a/cpp/macros.h b/cpp/macros.h new file mode 100644 index 0000000..f87ea6a --- /dev/null +++ b/cpp/macros.h @@ -0,0 +1,11 @@ +#ifndef macros_h +#define macros_h + +#define HOSTFN(name) \ +jsi::Function::createFromHostFunction( \ +rt, \ +jsi::PropNameID::forAscii(rt, name), \ +0, \ +[=](jsi::Runtime &rt, const jsi::Value &thisValue, const jsi::Value *args, size_t count) -> jsi::Value + +#endif /* macros_h */ diff --git a/cpp/react-native-rapier.cpp b/cpp/react-native-rapier.cpp new file mode 100644 index 0000000..f6ee4d1 --- /dev/null +++ b/cpp/react-native-rapier.cpp @@ -0,0 +1,21 @@ +#include "react-native-rapier.h" +#include "macros.h" + + +namespace callstack { +namespace react_native_rapier { +void install(jsi::Runtime& rt) { + auto multiply = HOSTFN("multiply") { + auto num1 = args[0].asNumber(); + auto num2 = args[1].asNumber(); + return jsi::Value(num1 + num2); + }); + + auto rapier = jsi::Object { rt }; + rapier.setProperty(rt, "multiply", std::move(multiply)); + + rt.global().setProperty(rt, "__RapierProxy", std::move(rapier)); +} +} +} + diff --git a/cpp/react-native-rapier.h b/cpp/react-native-rapier.h new file mode 100644 index 0000000..0faf7dc --- /dev/null +++ b/cpp/react-native-rapier.h @@ -0,0 +1,9 @@ +#include + +using namespace facebook; + +namespace callstack { + namespace react_native_rapier { + void install(jsi::Runtime& rt); + } +} diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index d402d87..795437c 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -1,26 +1,5 @@ PODS: - boost (1.84.0) - - callstack-react-native-rapier (0.1.0): - - DoubleConversion - - glog - - RCT-Folly (= 2024.01.01.00) - - RCTRequired - - RCTTypeSafety - - React-Core - - React-debug - - React-Fabric - - React-featureflags - - React-graphics - - React-ImageManager - - React-jsi - - React-NativeModulesApple - - React-RCTFabric - - React-rendererdebug - - React-utils - - ReactCodegen - - ReactCommon/turbomodule/bridging - - ReactCommon/turbomodule/core - - Yoga - DoubleConversion (1.1.6) - FBLazyVector (0.75.4) - fmt (9.1.0) @@ -1230,6 +1209,27 @@ PODS: - ReactCommon/turbomodule/bridging - ReactCommon/turbomodule/core - Yoga + - react-native-rapier (0.1.0): + - DoubleConversion + - glog + - RCT-Folly (= 2024.01.01.00) + - RCTRequired + - RCTTypeSafety + - React-Core + - React-debug + - React-Fabric + - React-featureflags + - React-graphics + - React-ImageManager + - React-jsi + - React-NativeModulesApple + - React-RCTFabric + - React-rendererdebug + - React-utils + - ReactCodegen + - ReactCommon/turbomodule/bridging + - ReactCommon/turbomodule/core + - Yoga - React-nativeconfig (0.75.4) - React-NativeModulesApple (0.75.4): - glog @@ -1505,7 +1505,6 @@ PODS: DEPENDENCIES: - boost (from `../node_modules/react-native/third-party-podspecs/boost.podspec`) - - callstack-react-native-rapier (from `../..`) - DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`) - FBLazyVector (from `../node_modules/react-native/Libraries/FBLazyVector`) - fmt (from `../node_modules/react-native/third-party-podspecs/fmt.podspec`) @@ -1542,6 +1541,7 @@ DEPENDENCIES: - React-logger (from `../node_modules/react-native/ReactCommon/logger`) - React-Mapbuffer (from `../node_modules/react-native/ReactCommon`) - React-microtasksnativemodule (from `../node_modules/react-native/ReactCommon/react/nativemodule/microtasks`) + - react-native-rapier (from `../..`) - React-nativeconfig (from `../node_modules/react-native/ReactCommon`) - React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`) - React-perflogger (from `../node_modules/react-native/ReactCommon/reactperflogger`) @@ -1579,8 +1579,6 @@ SPEC REPOS: EXTERNAL SOURCES: boost: :podspec: "../node_modules/react-native/third-party-podspecs/boost.podspec" - callstack-react-native-rapier: - :path: "../.." DoubleConversion: :podspec: "../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec" FBLazyVector: @@ -1647,6 +1645,8 @@ EXTERNAL SOURCES: :path: "../node_modules/react-native/ReactCommon" React-microtasksnativemodule: :path: "../node_modules/react-native/ReactCommon/react/nativemodule/microtasks" + react-native-rapier: + :path: "../.." React-nativeconfig: :path: "../node_modules/react-native/ReactCommon" React-NativeModulesApple: @@ -1708,7 +1708,6 @@ EXTERNAL SOURCES: SPEC CHECKSUMS: boost: 4cb898d0bf20404aab1850c656dcea009429d6c1 - callstack-react-native-rapier: 1aab49f4a734f14d57c2479ca64b78943104358b DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5 FBLazyVector: 430e10366de01d1e3d57374500b1b150fe482e6d fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120 @@ -1742,6 +1741,7 @@ SPEC CHECKSUMS: React-logger: d79b704bf215af194f5213a6b7deec50ba8e6a9b React-Mapbuffer: b982d5bba94a8bc073bda48f0d27c9b28417fae3 React-microtasksnativemodule: 8fa285fed833a04a754bf575f8ded65fc240b88d + react-native-rapier: c1cee03d6596ae88e095fc42bc9d08191d59633d React-nativeconfig: 8c83d992b9cc7d75b5abe262069eaeea4349f794 React-NativeModulesApple: b8465afc883f5bf3fe8bac3767e394d581a5f123 React-perflogger: 59e1a3182dca2cee7b9f1f7aab204018d46d1914 diff --git a/example/src/App.tsx b/example/src/App.tsx index b1f328d..979b982 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -1,12 +1,12 @@ import { useState, useEffect } from 'react'; import { StyleSheet, View, Text } from 'react-native'; -import { multiply } from '@callstack/react-native-rapier'; +import Rapier from '@callstack/react-native-rapier'; export default function App() { const [result, setResult] = useState(); useEffect(() => { - multiply(3, 7).then(setResult); + setResult(Rapier.multiply(3, 7)); }, []); return ( diff --git a/ios/ReactNativeRapier.h b/ios/ReactNativeRapier.h index 89f7643..199d54f 100644 --- a/ios/ReactNativeRapier.h +++ b/ios/ReactNativeRapier.h @@ -1,5 +1,5 @@ #ifdef __cplusplus -#import "callstack-react-native-rapier.h" +#import "react-native-rapier.h" #endif #ifdef RCT_NEW_ARCH_ENABLED diff --git a/ios/ReactNativeRapier.mm b/ios/ReactNativeRapier.mm index d14e3ab..1f9c2f3 100644 --- a/ios/ReactNativeRapier.mm +++ b/ios/ReactNativeRapier.mm @@ -1,26 +1,27 @@ #import "ReactNativeRapier.h" +#import + + +@interface ReactNativeRapier () +@end @implementation ReactNativeRapier + RCT_EXPORT_MODULE() -// Example method -// See // https://reactnative.dev/docs/native-modules-ios -RCT_EXPORT_METHOD(multiply:(double)a - b:(double)b - resolve:(RCTPromiseResolveBlock)resolve - reject:(RCTPromiseRejectBlock)reject) -{ - NSNumber *result = @(callstack_reactnativerapier::multiply(a, b)); +#pragma mark - RCTTurboModuleWithJSIBindings - resolve(result); +- (void)installJSIBindingsWithRuntime:(facebook::jsi::Runtime &)runtime +{ + callstack::react_native_rapier::install(runtime); } // Don't compile this code when we build for the old architecture. #ifdef RCT_NEW_ARCH_ENABLED - (std::shared_ptr)getTurboModule: - (const facebook::react::ObjCTurboModule::InitParams &)params +(const facebook::react::ObjCTurboModule::InitParams &)params { - return std::make_shared(params); + return std::make_shared(params); } #endif diff --git a/callstack-react-native-rapier.podspec b/react-native-rapier.podspec similarity index 97% rename from callstack-react-native-rapier.podspec rename to react-native-rapier.podspec index 609cc58..acb1bcd 100644 --- a/callstack-react-native-rapier.podspec +++ b/react-native-rapier.podspec @@ -4,7 +4,7 @@ package = JSON.parse(File.read(File.join(__dir__, "package.json"))) folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32' Pod::Spec.new do |s| - s.name = "callstack-react-native-rapier" + s.name = "react-native-rapier" s.version = package["version"] s.summary = package["description"] s.homepage = package["homepage"] diff --git a/src/NativeReactNativeRapier.ts b/src/NativeReactNativeRapier.ts index c7fd96a..a1771a1 100644 --- a/src/NativeReactNativeRapier.ts +++ b/src/NativeReactNativeRapier.ts @@ -1,8 +1,6 @@ import type { TurboModule } from 'react-native'; import { TurboModuleRegistry } from 'react-native'; -export interface Spec extends TurboModule { - multiply(a: number, b: number): Promise; -} +export interface Spec extends TurboModule {} export default TurboModuleRegistry.getEnforcing('ReactNativeRapier'); diff --git a/src/index.tsx b/src/index.tsx index fc442df..151e590 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -13,7 +13,7 @@ const ReactNativeRapierModule = isTurboModuleEnabled ? require('./NativeReactNativeRapier').default : NativeModules.ReactNativeRapier; -const ReactNativeRapier = ReactNativeRapierModule +ReactNativeRapierModule ? ReactNativeRapierModule : new Proxy( {}, @@ -24,6 +24,16 @@ const ReactNativeRapier = ReactNativeRapierModule } ); -export function multiply(a: number, b: number): Promise { - return ReactNativeRapier.multiply(a, b); +declare global { + var __RapierProxy: object | undefined; } + +if (global.__RapierProxy == null) { + throw new Error(LINKING_ERROR); +} + +const proxy = global.__RapierProxy; +// Type cast this to Rapier types +const Rapier = proxy as any; + +export default Rapier;