Skip to content

Commit

Permalink
Migrate RNTester to Bridgeless (facebook#38477)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: facebook#38477

 {F1051490359}

Changelog:
[iOS][Changed] - Migrate RNTester to Bridgeless

Differential Revision: D47521323

fbshipit-source-id: 7cb62b8d094b2830df505ad3e3647705e97ff909
  • Loading branch information
Lulu Wu authored and facebook-github-bot committed Jul 18, 2023
1 parent 9c15164 commit 278c790
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 17 deletions.
146 changes: 133 additions & 13 deletions packages/react-native/Libraries/AppDelegate/RCTAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,43 @@
#import "RCTAppDelegate.h"
#import <React/RCTRootView.h>
#import "RCTAppSetupUtils.h"
#import "RCTLegacyInteropComponents.h"

#if RCT_NEW_ARCH_ENABLED
#import <React/CoreModulesPlugins.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTComponentViewFactory.h>
#import <React/RCTComponentViewProtocol.h>
#import <React/RCTCxxBridgeDelegate.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTFabricSurfaceHostingProxyRootView.h>
#import <React/RCTLegacyViewManagerInteropComponentView.h>
#import <React/RCTSurfaceHostingProxyRootView.h>
#import <React/RCTSurfacePresenter.h>
#import <React/RCTSurfacePresenterBridgeAdapter.h>
#import <ReactCommon/RCTContextContainerHandling.h>
#if USE_HERMES
#import <ReactCommon/RCTHermesInstance.h>
#else
#import <ReactCommon/RCTJscInstance.h>
#endif
#import <ReactCommon/RCTHost+Internal.h>
#import <ReactCommon/RCTHost.h>
#import <ReactCommon/RCTTurboModuleManager.h>
#import <react/bridgeless/JSEngineInstance.h>
#import <react/config/ReactNativeConfig.h>
#import <react/renderer/runtimescheduler/RuntimeScheduler.h>
#import <react/renderer/runtimescheduler/RuntimeSchedulerCallInvoker.h>
#import "RCTLegacyInteropComponents.h"

static NSString *const kRNConcurrentRoot = @"concurrentRoot";

@interface RCTAppDelegate () <
RCTTurboModuleManagerDelegate,
RCTCxxBridgeDelegate,
RCTComponentViewFactoryComponentProvider> {
RCTComponentViewFactoryComponentProvider,
RCTHostDelegate,
RCTHostRuntimeDelegate,
RCTContextContainerHandling> {
std::shared_ptr<const facebook::react::ReactNativeConfig> _reactNativeConfig;
facebook::react::ContextContainer::Shared _contextContainer;
std::shared_ptr<facebook::react::RuntimeScheduler> _runtimeScheduler;
Expand All @@ -38,7 +53,11 @@ @interface RCTAppDelegate () <

#endif

@implementation RCTAppDelegate
@implementation RCTAppDelegate {
#if RCT_NEW_ARCH_ENABLED
RCTHost *_reactHost;
#endif
}

#if RCT_NEW_ARCH_ENABLED
- (instancetype)init
Expand All @@ -55,26 +74,48 @@ - (instancetype)init
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
BOOL enableTM = NO;
BOOL enableBridgeless = NO;
#if RCT_NEW_ARCH_ENABLED
enableTM = self.turboModuleEnabled;
enableBridgeless = self.bridgelessEnabled;
#endif

RCTAppSetupPrepareApp(application, enableTM);

if (!self.bridge) {
self.bridge = [self createBridgeWithDelegate:self launchOptions:launchOptions];
}
NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView;

if (enableBridgeless) {
#if RCT_NEW_ARCH_ENABLED
self.bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:self.bridge
contextContainer:_contextContainer];
self.bridge.surfacePresenter = self.bridgeAdapter.surfacePresenter;
RCTHost *host = [self getOrCreateReactHost];
RCTFabricSurface *surface = nil;

if (surface == nil) {
surface = [host createSurfaceWithModuleName:self.moduleName initialProperties:launchOptions];
}

[self unstable_registerLegacyComponents];
[RCTComponentViewFactory currentComponentViewFactory].thirdPartyFabricComponentsProvider = self;
RCTSurfaceHostingProxyRootView *surfaceHostingProxyRootView = [[RCTSurfaceHostingProxyRootView alloc]
initWithSurface:surface
sizeMeasureMode:RCTSurfaceSizeMeasureModeWidthExact | RCTSurfaceSizeMeasureModeHeightExact
moduleRegistry:[host getModuleRegistry]];

rootView = (RCTRootView *)surfaceHostingProxyRootView;
#endif
} else {
if (!self.bridge) {
self.bridge = [self createBridgeWithDelegate:self launchOptions:launchOptions];
}
#if RCT_NEW_ARCH_ENABLED
self.bridgeAdapter = [[RCTSurfacePresenterBridgeAdapter alloc] initWithBridge:self.bridge
contextContainer:_contextContainer];
self.bridge.surfacePresenter = self.bridgeAdapter.surfacePresenter;

NSDictionary *initProps = [self prepareInitialProps];
UIView *rootView = [self createRootViewWithBridge:self.bridge moduleName:self.moduleName initProps:initProps];
[self unstable_registerLegacyComponents];
[RCTComponentViewFactory currentComponentViewFactory].thirdPartyFabricComponentsProvider = self;
#endif

rootView = [self createRootViewWithBridge:self.bridge moduleName:self.moduleName initProps:initProps];
}
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [self createRootViewController];
[self setRootView:rootView toRootViewController:rootViewController];
Expand Down Expand Up @@ -198,6 +239,20 @@ - (BOOL)fabricEnabled
return YES;
}

- (BOOL)bridgelessEnabled
{
return YES;
}

- (BOOL)hermesEnabled
{
#if USE_HERMES
return YES;
#else
return NO;
#endif
}

#pragma mark - New Arch Utilities

- (void)unstable_registerLegacyComponents
Expand All @@ -207,6 +262,71 @@ - (void)unstable_registerLegacyComponents
}
}

- (RCTHost *)getOrCreateReactHost
{
if (!_reactHost) {
__weak __typeof(self) weakSelf = self;
_reactHost = [[RCTHost alloc] initWithBundleURL:[self getBundleURL]
hostDelegate:self
turboModuleManagerDelegate:self
jsEngineProvider:^std::shared_ptr<facebook::react::JSEngineInstance>() {
return [weakSelf createJSEngineInstance];
}];
[_reactHost setBundleURLProvider:^NSURL *() {
return [weakSelf getBundleURL];
}];
_reactHost.runtimeDelegate = self;
[_reactHost setContextContainerHandler:self];
[_reactHost start];
}

return _reactHost;
}

- (std::shared_ptr<facebook::react::JSEngineInstance>)createJSEngineInstance
{
#if USE_HERMES
return std::make_shared<facebook::react::RCTHermesInstance>(
[self createReactNativeConfig], [self createCrashManagerProvider]);
#else
return std::make_shared<facebook::react::RCTJscInstance>();
#endif
}

- (void)didCreateContextContainer:(std::shared_ptr<facebook::react::ContextContainer>)contextContainer
{
contextContainer->insert("ReactNativeConfig", [self createReactNativeConfig]);
}

// Overridable methods for Bridgeless integration

- (NSURL *)getBundleURL
{
return nullptr;
}

- (std::shared_ptr<const facebook::react::ReactNativeConfig>)createReactNativeConfig
{
return std::make_shared<const facebook::react::EmptyReactNativeConfig>();
}

#if USE_HERMES
- (facebook::react::CrashManagerProvider)createCrashManagerProvider
{
return nullptr;
}
#endif

- (void)hostDidStart:(RCTHost *)host
{
// no-op
}

- (void)host:(RCTHost *)host didInitializeRuntime:(facebook::jsi::Runtime &)runtime
{
// no-op
}

#endif

@end
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ folly_flags = ' -DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1'
folly_compiler_flags = folly_flags + ' ' + '-Wno-comma -Wno-shorten-64-to-32'

is_new_arch_enabled = ENV["RCT_NEW_ARCH_ENABLED"] == "1"
use_hermes = ENV['USE_HERMES'] == nil || ENV['USE_HERMES'] == '1'
use_frameworks = ENV['USE_FRAMEWORKS'] != nil

new_arch_enabled_flag = (is_new_arch_enabled ? " -DRCT_NEW_ARCH_ENABLED" : "")
is_fabric_enabled = is_new_arch_enabled || ENV["RCT_FABRIC_ENABLED"]
fabric_flag = (is_fabric_enabled ? " -DRN_FABRIC_ENABLED" : "")
other_cflags = "$(inherited)" + folly_flags + new_arch_enabled_flag + fabric_flag

use_hermes = ENV['USE_HERMES'] == '1'
use_frameworks = ENV['USE_FRAMEWORKS'] != nil
hermes_flag = (use_hermes ? " -DUSE_HERMES" : "")
other_cflags = "$(inherited)" + folly_flags + new_arch_enabled_flag + fabric_flag + hermes_flag

header_search_paths = [
"$(PODS_TARGET_SRCROOT)/../../ReactCommon",
Expand Down
9 changes: 9 additions & 0 deletions packages/rn-tester/RNTester/AppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,13 @@ - (void)application:(__unused UIApplication *)application
}
#endif

// Implementation of Bridgeless APIs
#if RCT_NEW_ARCH_ENABLED
- (NSURL *)getBundleURL
{
return [[RCTBundleURLProvider sharedSettings]
jsBundleURLForBundleRoot:@"react-native-github/packages/rn-tester/js/RNTesterApp.ios"];
}
#endif

@end

0 comments on commit 278c790

Please sign in to comment.