Skip to content

Commit 8ec0e69

Browse files
sota000facebook-github-bot
authored andcommitted
Add turbo module support in the default app template (#32752)
Summary: Pull Request resolved: #32752 Changelog: [internal] Add an optional support for Turbomodule. Define RCT_TM_FABRIC_ENABLED to enable the new architecture. Reviewed By: philIip Differential Revision: D33052777 fbshipit-source-id: 6d32790586bb51f9c9244344522c95245c912114
1 parent 82b8f40 commit 8ec0e69

File tree

6 files changed

+169
-2
lines changed

6 files changed

+169
-2
lines changed

React-Core.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ Pod::Spec.new do |s|
4747
s.compiler_flags = folly_compiler_flags + ' ' + boost_compiler_flags
4848
s.header_dir = "React"
4949
s.framework = "JavaScriptCore"
50-
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/RCT-Folly\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\" \"${PODS_ROOT}/Headers/Public/FlipperKit\"", "DEFINES_MODULE" => "YES" }
50+
s.pod_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_TARGET_SRCROOT)/ReactCommon\" \"$(PODS_ROOT)/boost\" \"$(PODS_ROOT)/DoubleConversion\" \"$(PODS_ROOT)/RCT-Folly\" \"${PODS_ROOT}/Headers/Public/React-hermes\" \"${PODS_ROOT}/Headers/Public/hermes-engine\" \"${PODS_ROOT}/Headers/Public/FlipperKit\" \"$(PODS_ROOT)/Headers/Public/ReactCommon\"", "DEFINES_MODULE" => "YES" }
5151
s.user_target_xcconfig = { "HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/Headers/Private/React-Core\""}
5252
s.default_subspec = "Default"
5353

React/AppSetup/RCTAppSetupUtils.h

+26
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,35 @@
99
#import <React/RCTBridge.h>
1010
#import <React/RCTRootView.h>
1111

12+
#if RCT_TM_FABRIC_ENABLED
13+
14+
#ifndef RCT_USE_HERMES
15+
#if __has_include(<reacthermes/HermesExecutorFactory.h>)
16+
#define RCT_USE_HERMES 1
17+
#else
18+
#define RCT_USE_HERMES 0
19+
#endif
20+
#endif
21+
22+
#if RCT_USE_HERMES
23+
#import <reacthermes/HermesExecutorFactory.h>
24+
#else
25+
#import <React/JSCExecutorFactory.h>
26+
#endif
27+
28+
#import <ReactCommon/RCTTurboModuleManager.h>
29+
#endif
30+
1231
@interface RCTAppSetupUtils : NSObject
1332
+ (void)prepareApp:(UIApplication *_Nonnull)application;
1433
+ (RCTRootView *_Nonnull)defaultRootViewWithBridge:(RCTBridge *_Nonnull)bridge
1534
moduleName:(NSString *_Nonnull)moduleName
1635
initialProperties:(nullable NSDictionary *)initialProperties;
36+
37+
#if RCT_TM_FABRIC_ENABLED
38+
+ (id<RCTTurboModule> _Nonnull)defaultModuleInstanceFromClass:(Class _Nonnull)moduleClass;
39+
+ (std::unique_ptr<facebook::react::JSExecutorFactory>)
40+
defaultJsExecutorFactoryForBridge:(RCTBridge *_Nonnull)bridge
41+
withTurboModuleManager:(RCTTurboModuleManager *_Nonnull)turboModuleManager;
42+
#endif
1743
@end

React/AppSetup/RCTAppSetupUtils.mm

+78
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
#import "RCTAppSetupUtils.h"
99

10+
#if RCT_TM_FABRIC_ENABLED
11+
#import <React/CoreModulesPlugins.h>
12+
#import <React/RCTDataRequestHandler.h>
13+
#import <React/RCTFileRequestHandler.h>
14+
#import <React/RCTGIFImageDecoder.h>
15+
#import <React/RCTHTTPRequestHandler.h>
16+
#import <React/RCTImageLoader.h>
17+
#import <React/RCTJSIExecutorRuntimeInstaller.h>
18+
#import <React/RCTLocalAssetImageLoader.h>
19+
#import <React/RCTNetworking.h>
20+
#endif
21+
1022
#ifdef FB_SONARKIT_ENABLED
1123
#import <FlipperKit/FlipperClient.h>
1224
#import <FlipperKitLayoutPlugin/FlipperKitLayoutPlugin.h>
@@ -35,6 +47,10 @@ + (void)prepareApp:(UIApplication *)application
3547
#ifdef FB_SONARKIT_ENABLED
3648
InitializeFlipper(application);
3749
#endif
50+
51+
#if RCT_TM_FABRIC_ENABLED
52+
RCTEnableTurboModule(YES);
53+
#endif
3854
}
3955

4056
+ (RCTRootView *)defaultRootViewWithBridge:(RCTBridge *)bridge
@@ -44,4 +60,66 @@ + (RCTRootView *)defaultRootViewWithBridge:(RCTBridge *)bridge
4460
return [[RCTRootView alloc] initWithBridge:bridge moduleName:moduleName initialProperties:initialProperties];
4561
}
4662

63+
#if RCT_TM_FABRIC_ENABLED
64+
+ (id<RCTTurboModule>)defaultModuleInstanceFromClass:(Class)moduleClass
65+
{
66+
// Set up the default RCTImageLoader and RCTNetworking modules.
67+
if (moduleClass == RCTImageLoader.class) {
68+
return [[moduleClass alloc] initWithRedirectDelegate:nil
69+
loadersProvider:^NSArray<id<RCTImageURLLoader>> *(RCTModuleRegistry *moduleRegistry) {
70+
return @[ [RCTLocalAssetImageLoader new] ];
71+
}
72+
decodersProvider:^NSArray<id<RCTImageDataDecoder>> *(RCTModuleRegistry *moduleRegistry) {
73+
return @[ [RCTGIFImageDecoder new] ];
74+
}];
75+
} else if (moduleClass == RCTNetworking.class) {
76+
return [[moduleClass alloc]
77+
initWithHandlersProvider:^NSArray<id<RCTURLRequestHandler>> *(RCTModuleRegistry *moduleRegistry) {
78+
return @[
79+
[RCTHTTPRequestHandler new],
80+
[RCTDataRequestHandler new],
81+
[RCTFileRequestHandler new],
82+
];
83+
}];
84+
}
85+
// No custom initializer here.
86+
return [moduleClass new];
87+
}
88+
89+
+ (std::unique_ptr<facebook::react::JSExecutorFactory>)defaultJsExecutorFactoryForBridge:(RCTBridge *)bridge
90+
withTurboModuleManager:
91+
(RCTTurboModuleManager *)turboModuleManager;
92+
{
93+
// Necessary to allow NativeModules to lookup TurboModules
94+
[bridge setRCTTurboModuleRegistry:turboModuleManager];
95+
96+
#if RCT_DEV
97+
if (!RCTTurboModuleEagerInitEnabled()) {
98+
/**
99+
* Instantiating DevMenu has the side-effect of registering
100+
* shortcuts for CMD + d, CMD + i, and CMD + n via RCTDevMenu.
101+
* Therefore, when TurboModules are enabled, we must manually create this
102+
* NativeModule.
103+
*/
104+
[turboModuleManager moduleForName:"RCTDevMenu"];
105+
}
106+
#endif
107+
108+
#if RCT_USE_HERMES
109+
return std::make_unique<facebook::react::HermesExecutorFactory>(
110+
#else
111+
return std::make_unique<facebook::react::JSCExecutorFactory>(
112+
#endif
113+
facebook::react::RCTJSIExecutorRuntimeInstaller([turboModuleManager, bridge](facebook::jsi::Runtime &runtime) {
114+
if (!bridge || !turboModuleManager) {
115+
return;
116+
}
117+
facebook::react::RuntimeExecutor syncRuntimeExecutor =
118+
[&](std::function<void(facebook::jsi::Runtime & runtime_)> &&callback) { callback(runtime); };
119+
[turboModuleManager installJSBindingWithRuntimeExecutor:syncRuntimeExecutor];
120+
}));
121+
}
122+
123+
#endif
124+
47125
@end

packages/rn-tester/Podfile.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ SPEC CHECKSUMS:
901901
React: f64c9f6db5428717922a3292ba6a448615a2e143
902902
React-callinvoker: c5d61e29df57793f0dc10ec2bc01c846f863e51f
903903
React-Codegen: 2256e335ccce7326eeca6d7a668e05c4de259289
904-
React-Core: 50d95abfb3c60d95e33a4b74e05e2a414bea4b73
904+
React-Core: 2753750098f0ad7e351a61c2e63d44ae0e95c3fe
905905
React-CoreModules: a8e2bdc1ebbf8d440478456197abd58d1691f61a
906906
React-cxxreact: cfc1663dae1ea52b465bbf021ef7b1527c5dc80c
907907
React-Fabric: 002345cff43721617e0a3c0866f6f76bae8c50ff

template/ios/HelloWorld.xcodeproj/project.pbxproj

+14
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,7 @@
482482
baseConfigurationReference = 3B4392A12AC88292D35C810B /* Pods-HelloWorld.debug.xcconfig */;
483483
buildSettings = {
484484
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
485+
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
485486
CLANG_ENABLE_MODULES = YES;
486487
CURRENT_PROJECT_VERSION = 1;
487488
ENABLE_BITCODE = NO;
@@ -508,6 +509,7 @@
508509
baseConfigurationReference = 5709B34CF0A7D63546082F79 /* Pods-HelloWorld.release.xcconfig */;
509510
buildSettings = {
510511
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
512+
CLANG_CXX_LANGUAGE_STANDARD = "c++17";
511513
CLANG_ENABLE_MODULES = YES;
512514
CURRENT_PROJECT_VERSION = 1;
513515
INFOPLIST_FILE = HelloWorld/Info.plist;
@@ -588,6 +590,12 @@
588590
);
589591
MTL_ENABLE_DEBUG_INFO = YES;
590592
ONLY_ACTIVE_ARCH = YES;
593+
OTHER_CPLUSPLUSFLAGS = (
594+
"$(OTHER_CFLAGS)",
595+
"-DFOLLY_NO_CONFIG",
596+
"-DFOLLY_MOBILE=1",
597+
"-DFOLLY_USE_LIBCPP=1",
598+
);
591599
SDKROOT = iphoneos;
592600
};
593601
name = Debug;
@@ -645,6 +653,12 @@
645653
"\"$(inherited)\"",
646654
);
647655
MTL_ENABLE_DEBUG_INFO = NO;
656+
OTHER_CPLUSPLUSFLAGS = (
657+
"$(OTHER_CFLAGS)",
658+
"-DFOLLY_NO_CONFIG",
659+
"-DFOLLY_MOBILE=1",
660+
"-DFOLLY_USE_LIBCPP=1",
661+
);
648662
SDKROOT = iphoneos;
649663
VALIDATE_PRODUCT = YES;
650664
};

template/ios/HelloWorld/AppDelegate.mm

+49
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,17 @@
66

77
#import <React/RCTAppSetupUtils.h>
88

9+
#if RCT_TM_FABRIC_ENABLED
10+
#import <React/CoreModulesPlugins.h>
11+
#import <React/RCTCxxBridgeDelegate.h>
12+
#import <ReactCommon/RCTTurboModuleManager.h>
13+
14+
@interface AppDelegate () <RCTCxxBridgeDelegate, RCTTurboModuleManagerDelegate> {
15+
RCTTurboModuleManager *_turboModuleManager;
16+
}
17+
@end
18+
#endif
19+
920
@implementation AppDelegate
1021

1122
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
@@ -40,4 +51,42 @@ - (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
4051
#endif
4152
}
4253

54+
#if RCT_TM_FABRIC_ENABLED
55+
56+
#pragma mark - RCTCxxBridgeDelegate
57+
58+
- (std::unique_ptr<facebook::react::JSExecutorFactory>)jsExecutorFactoryForBridge:(RCTBridge *)bridge
59+
{
60+
_turboModuleManager = [[RCTTurboModuleManager alloc] initWithBridge:bridge
61+
delegate:self
62+
jsInvoker:bridge.jsCallInvoker];
63+
return [RCTAppSetupUtils defaultJsExecutorFactoryForBridge:bridge withTurboModuleManager:_turboModuleManager];
64+
}
65+
66+
#pragma mark RCTTurboModuleManagerDelegate
67+
68+
- (Class)getModuleClassFromName:(const char *)name
69+
{
70+
return RCTCoreModulesClassProvider(name);
71+
}
72+
73+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
74+
jsInvoker:(std::shared_ptr<facebook::react::CallInvoker>)jsInvoker
75+
{
76+
return nullptr;
77+
}
78+
79+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:(const std::string &)name
80+
initParams:(const facebook::react::ObjCTurboModule::InitParams &)params
81+
{
82+
return nullptr;
83+
}
84+
85+
- (id<RCTTurboModule>)getModuleInstanceFromClass:(Class)moduleClass
86+
{
87+
return [RCTAppSetupUtils defaultModuleInstanceFromClass: moduleClass];
88+
}
89+
90+
#endif
91+
4392
@end

0 commit comments

Comments
 (0)