move creation of javascript engine responsibility out of RCTHostDelegate (#37327)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/37327

Changelog: [Internal]

in this diff, we enforce at the API level that the `RCTHostDelegate` is not responsible for creating the js engine instance.

Reviewed By: cipolleschi

Differential Revision: D45596321

fbshipit-source-id: 7862344d8b42e9d70e7fb7c7e9980953f0d66a8b
This commit is contained in:
Phillip Pan
2023-05-12 22:26:10 -07:00
committed by Facebook GitHub Bot
parent cd223d6740
commit a620498856
2 changed files with 26 additions and 20 deletions
@@ -39,7 +39,6 @@ typedef std::shared_ptr<facebook::react::JSEngineInstance> (^RCTHostJSEngineProv
@protocol RCTHostDelegate <NSObject>
- (std::shared_ptr<facebook::react::JSEngineInstance>)getJSEngine;
- (NSURL *)getBundleURL;
- (std::shared_ptr<facebook::react::ContextContainer>)createContextContainer;
@@ -61,8 +60,7 @@ typedef std::shared_ptr<facebook::react::JSEngineInstance> (^RCTHostJSEngineProv
- (instancetype)initWithHostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
bindingsInstallFunc:(facebook::react::ReactInstance::BindingsInstallFunc)bindingsInstallFunc
jsEngineProvider:(nullable RCTHostJSEngineProvider)jsEngineProvider NS_DESIGNATED_INITIALIZER
FB_OBJC_DIRECT;
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider NS_DESIGNATED_INITIALIZER FB_OBJC_DIRECT;
/**
* This function initializes an RCTInstance if one does not yet exist. This function is currently only called on the
@@ -58,7 +58,7 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification";
- (instancetype)initWithHostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
bindingsInstallFunc:(facebook::react::ReactInstance::BindingsInstallFunc)bindingsInstallFunc
jsEngineProvider:(nullable RCTHostJSEngineProvider)jsEngineProvider
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
{
if (self = [super init]) {
_hostDelegate = hostDelegate;
@@ -149,14 +149,13 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification";
}
[self _refreshBundleURL];
RCTReloadCommandSetBundleURL(_bundleURL);
_instance =
[[RCTInstance alloc] initWithDelegate:self
jsEngineInstance:_jsEngineProvider ? _jsEngineProvider() : [_hostDelegate getJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
bindingsInstallFunc:_bindingsInstallFunc
moduleRegistry:_moduleRegistry];
_instance = [[RCTInstance alloc] initWithDelegate:self
jsEngineInstance:[self _provideJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
bindingsInstallFunc:_bindingsInstallFunc
moduleRegistry:_moduleRegistry];
}
- (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName
@@ -218,14 +217,13 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification";
_surfaceStartBuffer = [NSMutableArray arrayWithArray:[self _getAttachedSurfaces]];
}
_instance =
[[RCTInstance alloc] initWithDelegate:self
jsEngineInstance:_jsEngineProvider ? _jsEngineProvider() : [_hostDelegate getJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
bindingsInstallFunc:_bindingsInstallFunc
moduleRegistry:_moduleRegistry];
_instance = [[RCTInstance alloc] initWithDelegate:self
jsEngineInstance:[self _provideJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
onInitialBundleLoad:_onInitialBundleLoad
bindingsInstallFunc:_bindingsInstallFunc
moduleRegistry:_moduleRegistry];
[[NSNotificationCenter defaultCenter]
postNotification:[NSNotification notificationWithName:RCTHostDidReloadNotification object:nil]];
@@ -278,6 +276,7 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification";
}
#pragma mark - Private
- (void)_refreshBundleURL FB_OBJC_DIRECT
{
// Reset the _bundleURL ivar if the RCTHost delegate presents a new bundleURL
@@ -308,4 +307,13 @@ NSString *const RCTHostDidReloadNotification = @"RCTHostDidReloadNotification";
return surfaces;
}
- (std::shared_ptr<facebook::react::JSEngineInstance>)_provideJSEngine
{
RCTAssert(_jsEngineProvider, @"_jsEngineProvider must be non-nil");
std::shared_ptr<facebook::react::JSEngineInstance> jsEngine = _jsEngineProvider();
RCTAssert(jsEngine != nullptr, @"_jsEngineProvider must return a nonnull pointer");
return jsEngine;
}
@end