Skip to content

Commit

Permalink
introduce native api to access RuntimeExecutor (facebook#42758)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
philIip authored and facebook-github-bot committed Jan 31, 2024
1 parent 6a8a28b commit 9b3709e
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 1 deletion.
24 changes: 24 additions & 0 deletions packages/react-native/React/Base/RCTRuntimeExecutionWrapper.h
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions packages/react-native/React/Base/RCTRuntimeExecutionWrapper.m
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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 <jsi/jsi.h>

#import <ReactCommon/CallInvoker.h>

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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@
#import <React/RCTTurboModuleRegistry.h>
#import <ReactCommon/RuntimeExecutor.h>
#import <ReactCommon/TurboModuleBinding.h>

#import "RCTRuntimeExecution.h"
#import "RCTTurboModule.h"

@class RCTTurboModuleManager;

@protocol RCTTurboModuleManagerDelegate <NSObject>

/**
Expand Down Expand Up @@ -52,6 +56,13 @@

@end

@protocol RCTTurboModuleManagerRuntimeHandler <NSObject>

- (void)turboModuleManager:(RCTTurboModuleManager *)turboModuleManager
handleRuntimeBlock:(RCTJSIRuntimeHandlingBlock)block;

@end

@interface RCTTurboModuleManager : NSObject <RCTTurboModuleRegistry>

- (instancetype)initWithBridge:(RCTBridge *)bridge
Expand All @@ -67,4 +78,6 @@

- (void)invalidate;

@property (nonatomic, weak, readwrite) id<RCTTurboModuleManagerRuntimeHandler> runtimeHandler;

@end
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#import <React/RCTLog.h>
#import <React/RCTModuleData.h>
#import <React/RCTPerformanceLogger.h>
#import <React/RCTRuntimeExecutionModule.h>
#import <React/RCTRuntimeExecutionWrapper.h>
#import <React/RCTUtils.h>
#import <ReactCommon/RuntimeExecutor.h>
#import <ReactCommon/TurboCxxModule.h>
Expand Down Expand Up @@ -676,6 +678,20 @@ - (BOOL)_shouldCreateObjCModule:(Class)moduleClass
}
}

if ([module respondsToSelector:@selector(setRuntimeExecutionWrapper:)]) {
__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<RCTRuntimeExecutionModule>)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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void RCTInstanceSetRuntimeDiagnosticFlags(NSString *flags)
sRuntimeDiagnosticFlags = [flags copy];
}

@interface RCTInstance () <RCTTurboModuleManagerDelegate>
@interface RCTInstance () <RCTTurboModuleManagerDelegate, RCTTurboModuleManagerRuntimeHandler>
@end

@implementation RCTInstance {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -259,6 +270,7 @@ - (void)_start
bridgeModuleDecorator:_bridgeModuleDecorator
delegate:self
jsInvoker:std::make_shared<BridgelessJSCallInvoker>(bufferedRuntimeExecutor)];
_turboModuleManager.runtimeHandler = self;

#if RCT_DEV
/**
Expand Down

0 comments on commit 9b3709e

Please sign in to comment.