forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce native api to access RuntimeExecutor (facebook#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 object for RuntimeExecutor access, `RCTRuntimeExecutorProvider`. The wrapper is necessary so it can be referenced in swift - new API, i.e. `RCTRuntimeExecutionModule`, for modules to access the RuntimeExecutor block - integration within the bridgeless infrastructure Differential Revision: D53256188
- Loading branch information
1 parent
a0b1073
commit 85313c8
Showing
6 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
packages/react-native/React/Base/RCTRuntimeExecutionModule.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/* | ||
* 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 RCTRuntimeExecutorProvider; | ||
|
||
/** | ||
* Have your module conform to this protocol to access the RuntimeExecutor. | ||
* Only available in the bridgeless runtime. | ||
*/ | ||
@protocol RCTRuntimeExecutionModule <NSObject> | ||
|
||
@property (nonatomic, nullable, readwrite) RCTRuntimeExecutorProvider *runtimeExecutorProvider; | ||
|
||
@end |
31 changes: 31 additions & 0 deletions
31
...ReactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecutorProvider.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* 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 <ReactCommon/RuntimeExecutor.h> | ||
#import <jsi/jsi.h> | ||
|
||
typedef void (^RCTJSIRuntimeHandlingBlock)(facebook::jsi::Runtime &runtime); | ||
typedef void (^RCTRuntimeExecutor)(_Nonnull RCTJSIRuntimeHandlingBlock); | ||
|
||
NS_ASSUME_NONNULL_BEGIN | ||
|
||
@interface RCTRuntimeExecutorProvider : 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)initWithRuntimeExecutor:(facebook::react::RuntimeExecutor)runtimeExecutor NS_DESIGNATED_INITIALIZER; | ||
|
||
- (RCTRuntimeExecutor)runtimeExecutor; | ||
|
||
@end | ||
|
||
NS_ASSUME_NONNULL_END |
38 changes: 38 additions & 0 deletions
38
...eactCommon/react/nativemodule/core/platform/ios/ReactCommon/RCTRuntimeExecutorProvider.mm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* 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 "RCTRuntimeExecutorProvider.h" | ||
|
||
@implementation RCTRuntimeExecutorProvider { | ||
facebook::react::RuntimeExecutor _runtimeExecutor; | ||
} | ||
|
||
#pragma mark - Initializer | ||
|
||
- (instancetype)initWithRuntimeExecutor:(facebook::react::RuntimeExecutor)runtimeExecutor | ||
{ | ||
if (self = [super init]) { | ||
_runtimeExecutor = runtimeExecutor; | ||
} | ||
|
||
return self; | ||
} | ||
|
||
#pragma mark - Public API | ||
|
||
- (RCTRuntimeExecutor)runtimeExecutor | ||
{ | ||
__weak __typeof(self) weakSelf = self; | ||
return ^(RCTJSIRuntimeHandlingBlock block) { | ||
__strong __typeof(self) strongSelf = weakSelf; | ||
if (strongSelf && strongSelf->_runtimeExecutor) { | ||
strongSelf->_runtimeExecutor([=](facebook::jsi::Runtime &runtime) { block(runtime); }); | ||
} | ||
}; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters