From c328238ed58470b27e338587042ac074b8ce14c6 Mon Sep 17 00:00:00 2001 From: Phillip Pan Date: Wed, 31 Jan 2024 16:51:05 -0800 Subject: [PATCH] introduce native api to access RuntimeExecutor (#42758) Summary: Changelog: [Added][iOS] introduce native api to access RuntimeExecutor The goal of this API is to provide a safe way to access the `jsi::runtime` in bridgeless mode. The decision to limit access to the runtime in bridgeless was a conscious one - the runtime pointer is not thread-safe and its lifecycle must be managed correctly by owners. However, interacting with the runtime is an advanced use case we would want to support. Our recommended ways to access the runtime in bridgeless mode is either 1) via the RuntimeExecutor, or 2) via a C++ TurboModule. This diff introduces the API that would allow for 1). The integration consists of these parts: - Wrapper objects for RuntimeExecutor access. These are `RCTRuntimeExecution` and `RCTRuntimeExecutionWrapper`. RCTRuntimeExecutionWrapper is a C++ free wrapper of RCTRuntimeExecution to be reliably used in Swift modules. - The new API on RCTBridgeModule, following a similar pattern to other decoration APIs, and integration with our module infrastructure. Differential Revision: D53256188 --- .../React/Base/RCTRuntimeExecutionWrapper.h | 24 ++++++++++++++ .../React/Base/RCTRuntimeExecutionWrapper.m | 32 +++++++++++++++++++ .../ios/ReactCommon/RCTRuntimeExecution.h | 30 +++++++++++++++++ .../ios/ReactCommon/RCTRuntimeExecution.mm | 32 +++++++++++++++++++ .../ios/ReactCommon/RCTTurboModuleManager.h | 13 ++++++++ .../ios/ReactCommon/RCTTurboModuleManager.mm | 16 ++++++++++ .../platform/ios/ReactCommon/RCTInstance.mm | 14 +++++++- 7 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 packages/react-native/React/Base/RCTRuntimeExecutionWrapper.h create mode 100644 packages/react-native/React/Base/RCTRuntimeExecutionWrapper.m create mode 100644 packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.h create mode 100644 packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.mm diff --git a/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.h b/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.h new file mode 100644 index 00000000000000..a20b94fc2ce5bb --- /dev/null +++ b/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.h @@ -0,0 +1,24 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +@class RCTRuntimeExecution; + +NS_ASSUME_NONNULL_BEGIN + +/** + Swift friendly wrapper of RCTRuntimeExecution. + */ +@interface RCTRuntimeExecutionWrapper : NSObject + +- (instancetype)init NS_UNAVAILABLE; +- (instancetype)initWithRuntimeExecution:(RCTRuntimeExecution *)runtimeExecution; + +- (RCTRuntimeExecution *)getRuntimeExecution; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.m b/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.m new file mode 100644 index 00000000000000..4ed8b699ced635 --- /dev/null +++ b/packages/react-native/React/Base/RCTRuntimeExecutionWrapper.m @@ -0,0 +1,32 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTRuntimeExecutionWrapper.h" + +@implementation RCTRuntimeExecutionWrapper { + RCTRuntimeExecution *_runtimeExecution; +} + +#pragma mark - Initializer + +- (instancetype)initWithRuntimeExecution:(RCTRuntimeExecution *)runtimeExecution +{ + if (self = [super init]) { + _runtimeExecution = runtimeExecution; + } + + return self; +} + +#pragma mark - Public API + +- (RCTRuntimeExecution *)getRuntimeExecution +{ + return _runtimeExecution; +} + +@end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.h new file mode 100644 index 00000000000000..a9c3baa9f76086 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import + +NS_ASSUME_NONNULL_BEGIN + +typedef void (^RCTJSIRuntimeHandlingBlock)(facebook::jsi::Runtime &runtime); +typedef void (^RCTRuntimeExecutorBlock)(RCTJSIRuntimeHandlingBlock); + +@interface RCTRuntimeExecution : NSObject + +- (instancetype)init NS_UNAVAILABLE; + +/** + Initializes an object that wraps ways to access the RuntimeExecutor. + + @param runtimeExecutorBlock A block that provides thread-safe access to jsi::runtime. + */ +- (instancetype)initWithRuntimeExecutorBlock:(RCTRuntimeExecutorBlock)runtimeExecutorBlock NS_DESIGNATED_INITIALIZER; + +- (RCTRuntimeExecutorBlock)runtimeExecutorBlock; + +@end + +NS_ASSUME_NONNULL_END diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.mm new file mode 100644 index 00000000000000..aa09dc23cf1f7d --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecution.mm @@ -0,0 +1,32 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#import "RCTRuntimeExecution.h" + +@implementation RCTRuntimeExecution { + RCTRuntimeExecutorBlock _runtimeExecutorBlock; +} + +#pragma mark - Initializer + +- (instancetype)initWithRuntimeExecutorBlock:(RCTRuntimeExecutorBlock)runtimeExecutorBlock +{ + if (self = [super init]) { + _runtimeExecutorBlock = [runtimeExecutorBlock copy]; + } + + return self; +} + +#pragma mark - Public API + +- (RCTRuntimeExecutorBlock)runtimeExecutorBlock +{ + return _runtimeExecutorBlock; +} + +@end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.h b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.h index 1ef120d83dced6..095e9245e83cd4 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.h +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.h @@ -15,8 +15,12 @@ #import #import #import + +#import "RCTRuntimeExecution.h" #import "RCTTurboModule.h" +@class RCTTurboModuleManager; + @protocol RCTTurboModuleManagerDelegate /** @@ -52,6 +56,13 @@ @end +@protocol RCTTurboModuleManagerRuntimeHandler + +- (void)turboModuleManager:(RCTTurboModuleManager *)turboModuleManager + handleRuntimeBlock:(RCTJSIRuntimeHandlingBlock)block; + +@end + @interface RCTTurboModuleManager : NSObject - (instancetype)initWithBridge:(RCTBridge *)bridge @@ -67,4 +78,6 @@ - (void)invalidate; +@property (nonatomic, weak, readwrite) id runtimeHandler; + @end diff --git a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm index 997737d1a0c387..b5f9dc087e2f67 100644 --- a/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm +++ b/packages/react-native/ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTTurboModuleManager.mm @@ -24,6 +24,8 @@ #import #import #import +#import +#import #import #import #import @@ -676,6 +678,20 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass } } + if ([module conformsToProtocol:@protocol(RCTRuntimeExecutionModule)]) { + __weak __typeof(self) weakSelf = self; + RCTRuntimeExecution *runtimeExecution = + [[RCTRuntimeExecution alloc] initWithRuntimeExecutorBlock:^void(RCTJSIRuntimeHandlingBlock block) { + __strong __typeof(self) strongSelf = weakSelf; + if (strongSelf) { + [strongSelf->_runtimeHandler turboModuleManager:strongSelf handleRuntimeBlock:block]; + } + }]; + RCTRuntimeExecutionWrapper *runtimeExecutionWrapper = + [[RCTRuntimeExecutionWrapper alloc] initWithRuntimeExecution:runtimeExecution]; + [(id)module setRuntimeExecutionWrapper:runtimeExecutionWrapper]; + } + /** * Some modules need their own queues, but don't provide any, so we need to create it for them. * These modules typically have the following: 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 6a5644353bb4eb..d5f55f33327410 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 @@ -63,7 +63,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags) sRuntimeDiagnosticFlags = [flags copy]; } -@interface RCTInstance () +@interface RCTInstance () @end @implementation RCTInstance { @@ -208,6 +208,17 @@ - (Class)getModuleClassFromName:(const char *)name return nullptr; } +#pragma mark - RCTTurboModuleManagerRuntimeHandler + +- (void)turboModuleManager:(RCTTurboModuleManager *)turboModuleManager + handleRuntimeBlock:(RCTJSIRuntimeHandlingBlock)block; +{ + if (_valid) { + RuntimeExecutor bufferedRuntimeExecutor = _reactInstance->getBufferedRuntimeExecutor(); + bufferedRuntimeExecutor([=](jsi::Runtime &runtime) { block(runtime); }); + } +} + #pragma mark - Private - (void)_start @@ -259,6 +270,7 @@ - (void)_start bridgeModuleDecorator:_bridgeModuleDecorator delegate:self jsInvoker:std::make_shared(bufferedRuntimeExecutor)]; + _turboModuleManager.runtimeHandler = self; #if RCT_DEV /**