Skip to content

Commit

Permalink
move creation of javascript engine responsibility out of RCTHostDeleg…
Browse files Browse the repository at this point in the history
…ate (#37327)

Summary:
Pull Request resolved: #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
  • Loading branch information
philIip authored and facebook-github-bot committed May 13, 2023
1 parent cd223d6 commit a620498
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ + (void)initialize
- (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;
Expand Down Expand Up @@ -149,14 +149,13 @@ - (void)preload
}
[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
Expand Down Expand Up @@ -218,14 +217,13 @@ - (void)didReceiveReloadCommand
_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]];

Expand Down Expand Up @@ -278,6 +276,7 @@ - (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path
}

#pragma mark - Private

- (void)_refreshBundleURL FB_OBJC_DIRECT
{
// Reset the _bundleURL ivar if the RCTHost delegate presents a new bundleURL
Expand Down Expand Up @@ -308,4 +307,13 @@ - (void)_attachSurface:(RCTFabricSurface *)surface FB_OBJC_DIRECT
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

0 comments on commit a620498

Please sign in to comment.