From 16276ce0f6115fd4decaccaf33cf3364bbd2d469 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Wed, 14 Feb 2024 19:01:02 -0800 Subject: [PATCH] build backwards compat API for runtime pointer (#43012) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/43012 Changelog: [iOS][Added] This implements the functionality to give access to the jsi::Runtime in iOS in bridgeless. In bridge, this value is a private selector on RCTBridge that is exposed via category. We build this into the backwards compatible RCTBridgeProxy here. This should work out of the box in bridgeless if you are already retrieveing the pointer via the bridge. However, we recommend users to eventually migrate towards C++ TurboModule or the RuntimeExecutor if possible. This will be removed in the future. Reviewed By: RSNara Differential Revision: D53646413 fbshipit-source-id: a5584f22d433a580d537b8780a3bcd503680acb8 --- .../react-native/React/Base/RCTBridgeProxy.h | 3 ++- .../react-native/React/Base/RCTBridgeProxy.mm | 23 +++++++++++-------- .../platform/ios/ReactCommon/RCTInstance.mm | 3 ++- 3 files changed, 17 insertions(+), 12 deletions(-) diff --git a/packages/react-native/React/Base/RCTBridgeProxy.h b/packages/react-native/React/Base/RCTBridgeProxy.h index f8639c418e0..79c75c3b491 100644 --- a/packages/react-native/React/Base/RCTBridgeProxy.h +++ b/packages/react-native/React/Base/RCTBridgeProxy.h @@ -20,7 +20,8 @@ bundleManager:(RCTBundleManager *)bundleManager callableJSModules:(RCTCallableJSModules *)callableJSModules dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread - registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId NS_DESIGNATED_INITIALIZER; + registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId + runtime:(void *)runtime NS_DESIGNATED_INITIALIZER; - (NSMethodSignature *)methodSignatureForSelector:(SEL)sel; - (void)forwardInvocation:(NSInvocation *)invocation; diff --git a/packages/react-native/React/Base/RCTBridgeProxy.mm b/packages/react-native/React/Base/RCTBridgeProxy.mm index e4ece263b12..af6b7afb687 100644 --- a/packages/react-native/React/Base/RCTBridgeProxy.mm +++ b/packages/react-native/React/Base/RCTBridgeProxy.mm @@ -28,6 +28,7 @@ using namespace facebook; RCTCallableJSModules *_callableJSModules; void (^_dispatchToJSThread)(dispatch_block_t); void (^_registerSegmentWithId)(NSNumber *, NSString *); + void *_runtime; } - (instancetype)initWithViewRegistry:(RCTViewRegistry *)viewRegistry @@ -36,15 +37,17 @@ using namespace facebook; callableJSModules:(RCTCallableJSModules *)callableJSModules dispatchToJSThread:(void (^)(dispatch_block_t))dispatchToJSThread registerSegmentWithId:(void (^)(NSNumber *, NSString *))registerSegmentWithId + runtime:(void *)runtime { self = [super self]; if (self) { - self->_uiManagerProxy = [[RCTUIManagerProxy alloc] initWithViewRegistry:viewRegistry]; - self->_moduleRegistry = moduleRegistry; - self->_bundleManager = bundleManager; - self->_callableJSModules = callableJSModules; - self->_dispatchToJSThread = dispatchToJSThread; - self->_registerSegmentWithId = registerSegmentWithId; + _uiManagerProxy = [[RCTUIManagerProxy alloc] initWithViewRegistry:viewRegistry]; + _moduleRegistry = moduleRegistry; + _bundleManager = bundleManager; + _callableJSModules = callableJSModules; + _dispatchToJSThread = dispatchToJSThread; + _registerSegmentWithId = registerSegmentWithId; + _runtime = runtime; } return self; } @@ -75,10 +78,10 @@ using namespace facebook; * Used By: * - RCTBlobCollector */ -- (jsi::Runtime *)runtime +- (void *)runtime { - [self logWarning:@"This method is unsupported. Returning nullptr." cmd:_cmd]; - return nullptr; + [self logWarning:@"Please migrate to C++ TurboModule or RuntimeExecutor." cmd:_cmd]; + return _runtime; } /** @@ -162,7 +165,7 @@ using namespace facebook; - (void)registerSegmentWithId:(NSUInteger)segmentId path:(NSString *)path { - self->_registerSegmentWithId(@(segmentId), path); + _registerSegmentWithId(@(segmentId), path); } - (id)delegate diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm index 5bcec41c63d..a123105a672 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTInstance.mm @@ -263,7 +263,8 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) if (strongSelf && strongSelf->_valid) { [strongSelf registerSegmentWithId:segmentId path:path]; } - }]; + } + runtime:_reactInstance->getJavaScriptContext()]; [RCTBridge setCurrentBridge:(RCTBridge *)bridgeProxy]; // Set up TurboModules