Skip to content

Commit

Permalink
feat: base setup for JSI
Browse files Browse the repository at this point in the history
  • Loading branch information
okwasniewski committed Oct 3, 2024
1 parent dce18bc commit 07ffd7e
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 61 deletions.
7 changes: 0 additions & 7 deletions cpp/callstack-react-native-rapier.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions cpp/callstack-react-native-rapier.h

This file was deleted.

11 changes: 11 additions & 0 deletions cpp/macros.h
Original file line number Diff line number Diff line change
@@ -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 */
21 changes: 21 additions & 0 deletions cpp/react-native-rapier.cpp
Original file line number Diff line number Diff line change
@@ -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));
}
}
}

9 changes: 9 additions & 0 deletions cpp/react-native-rapier.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <ReactCommon/TurboModule.h>

using namespace facebook;

namespace callstack {
namespace react_native_rapier {
void install(jsi::Runtime& rt);
}
}
50 changes: 25 additions & 25 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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`)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -1708,7 +1708,6 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
boost: 4cb898d0bf20404aab1850c656dcea009429d6c1
callstack-react-native-rapier: 1aab49f4a734f14d57c2479ca64b78943104358b
DoubleConversion: 76ab83afb40bddeeee456813d9c04f67f78771b5
FBLazyVector: 430e10366de01d1e3d57374500b1b150fe482e6d
fmt: 4c2741a687cc09f0634a2e2c72a838b99f1ff120
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
@@ -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<number | undefined>();

useEffect(() => {
multiply(3, 7).then(setResult);
setResult(Rapier.multiply(3, 7));
}, []);

return (
Expand Down
2 changes: 1 addition & 1 deletion ios/ReactNativeRapier.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#ifdef __cplusplus
#import "callstack-react-native-rapier.h"
#import "react-native-rapier.h"
#endif

#ifdef RCT_NEW_ARCH_ENABLED
Expand Down
23 changes: 12 additions & 11 deletions ios/ReactNativeRapier.mm
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
#import "ReactNativeRapier.h"
#import <ReactCommon/RCTTurboModuleWithJSIBindings.h>


@interface ReactNativeRapier () <RCTTurboModuleWithJSIBindings>
@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<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return std::make_shared<facebook::react::NativeReactNativeRapierSpecJSI>(params);
return std::make_shared<facebook::react::NativeReactNativeRapierSpecJSI>(params);
}
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 1 addition & 3 deletions src/NativeReactNativeRapier.ts
Original file line number Diff line number Diff line change
@@ -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<number>;
}
export interface Spec extends TurboModule {}

export default TurboModuleRegistry.getEnforcing<Spec>('ReactNativeRapier');
16 changes: 13 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ReactNativeRapierModule = isTurboModuleEnabled
? require('./NativeReactNativeRapier').default
: NativeModules.ReactNativeRapier;

const ReactNativeRapier = ReactNativeRapierModule
ReactNativeRapierModule
? ReactNativeRapierModule
: new Proxy(
{},
Expand All @@ -24,6 +24,16 @@ const ReactNativeRapier = ReactNativeRapierModule
}
);

export function multiply(a: number, b: number): Promise<number> {
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;

0 comments on commit 07ffd7e

Please sign in to comment.