diff --git a/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm b/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm index c4c761902b1..dd424dfbcb7 100644 --- a/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm +++ b/packages/react-native/ReactCommon/react/runtime/iostests/RCTHostTests.mm @@ -162,6 +162,17 @@ static ShimRCTInstance *shimmedRCTInstance; } - (void)testDidInitializeRuntime +{ + auto hermesRuntime = facebook::hermes::makeHermesRuntime(); + facebook::jsi::Runtime *rt = hermesRuntime.get(); + + id instanceDelegate = (id)_subject; + [instanceDelegate instance:[OCMArg any] didInitializeRuntime:*rt]; + + OCMVerify(OCMTimes(1), [_mockHostDelegate host:_subject didInitializeRuntime:*rt]); +} + +- (void)testDidInitializeRuntime_DEPRECATED { id mockRuntimeDelegate = OCMProtocolMock(@protocol(RCTHostRuntimeDelegate)); _subject.runtimeDelegate = mockRuntimeDelegate; diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h index 9f4721be7f6..d845c8e4690 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.h @@ -49,11 +49,24 @@ typedef NSURL *_Nullable (^RCTHostBundleURLProvider)(void); exceptionId:(NSUInteger)exceptionId isFatal:(BOOL)isFatal extraData:(NSDictionary *)extraData __attribute__((deprecated)); + +/** + Delegate method invoked after the host has finished initializing the JavaScript runtime. At this stage, + bindings for Turbo Modules and the NativeComponentRegistry are already installed, + but the JavaScript bundle has not yet been executed. This method is called on the JavaScript thread; + accessing the runtime from any other thread is prohibited and will result in crashes. + */ +- (void)host:(RCTHost *)host didInitializeRuntime:(facebook::jsi::Runtime &)runtime; + @end -@protocol RCTHostRuntimeDelegate +// `RCTHostRuntimeDelegate` has been merged into `RCTHostDelegate` in 0.84 +[[deprecated("Use 'RCTHostDelegate' instead")]] +@protocol RCTHostRuntimeDelegate -- (void)host:(RCTHost *)host didInitializeRuntime:(facebook::jsi::Runtime &)runtime; +- (void)host:(RCTHost *)host + didInitializeRuntime:(facebook::jsi::Runtime &)runtime + [[deprecated("Use an equivalent method from 'RCTHostDelegate' instead")]]; @end @@ -84,7 +97,8 @@ typedef std::shared_ptr (^RCTHostJSEngineProv - (instancetype)init NS_UNAVAILABLE; + (instancetype)new NS_UNAVAILABLE; -@property (nonatomic, weak, nullable) id runtimeDelegate; +@property (nonatomic, weak, nullable) id runtimeDelegate + [[deprecated("Use 'RCTHostDelegate' instead")]]; @property (nonatomic, readonly) RCTSurfacePresenter *surfacePresenter; diff --git a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm index 030ed9d490a..5136c342047 100644 --- a/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm +++ b/packages/react-native/ReactCommon/react/runtime/platform/ios/ReactCommon/RCTHost.mm @@ -371,7 +371,14 @@ class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::Ho - (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime { + if ([_hostDelegate respondsToSelector:@selector(host:didInitializeRuntime:)]) { + [_hostDelegate host:self didInitializeRuntime:runtime]; + } + // Runtime delegate is deprecated as of 0.84 +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated-declarations" [self.runtimeDelegate host:self didInitializeRuntime:runtime]; +#pragma clang diagnostic pop } - (void)loadBundleAtURL:(NSURL *)sourceURL