Repository navigation
Expand file tree
/
Copy pathRCTHost.mm
More file actions
470 lines (400 loc) · 15.6 KB
/
Copy pathRCTHost.mm
File metadata and controls
470 lines (400 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#import "RCTHost.h"
#import "RCTHost+Internal.h"
#import <React/RCTAssert.h>
#import <React/RCTBridgeModule.h>
#import <React/RCTConvert.h>
#import <React/RCTFabricSurface.h>
#import <React/RCTInspectorDevServerHelper.h>
#import <React/RCTInspectorNetworkHelper.h>
#import <React/RCTInspectorUtils.h>
#import <React/RCTJSThread.h>
#import <React/RCTLog.h>
#import <React/RCTMockDef.h>
#import <React/RCTPausedInDebuggerOverlayController.h>
#import <React/RCTPerformanceLogger.h>
#import <React/RCTReloadCommand.h>
#import <React/RCTUtils.h>
#import <jsinspector-modern/InspectorFlags.h>
#import <jsinspector-modern/InspectorInterfaces.h>
#import <jsinspector-modern/ReactCdp.h>
#import <optional>
RCT_MOCK_DEF(RCTHost, _RCTLogNativeInternal);
#define _RCTLogNativeInternal RCT_MOCK_USE(RCTHost, _RCTLogNativeInternal)
using namespace facebook::react;
@interface RCTHost () <RCTReloadListener, RCTInstanceDelegate>
@property (nonatomic, readonly) jsinspector_modern::HostTarget *inspectorTarget;
@end
class RCTHostHostTargetDelegate : public facebook::react::jsinspector_modern::HostTargetDelegate {
public:
RCTHostHostTargetDelegate(RCTHost *host)
: host_(host),
pauseOverlayController_([[RCTPausedInDebuggerOverlayController alloc] init]),
networkHelper_([[RCTInspectorNetworkHelper alloc] init])
{
}
jsinspector_modern::HostTargetMetadata getMetadata() override
{
auto metadata = [RCTInspectorUtils getHostMetadata];
#if TARGET_OS_IPHONE
NSString *osName = [[UIDevice currentDevice] systemName];
#else
NSString *osName = @"macOS";
#endif
return {
.appDisplayName = [metadata.appDisplayName UTF8String],
.appIdentifier = [metadata.appIdentifier UTF8String],
.deviceName = [metadata.deviceName UTF8String],
.integrationName = [[NSString stringWithFormat:@"%@ Bridgeless (RCTHost)", osName] UTF8String],
.platform = [metadata.platform UTF8String],
.reactNativeVersion = [metadata.reactNativeVersion UTF8String],
};
}
void onReload(const PageReloadRequest &request) override
{
RCTAssertMainQueue();
[static_cast<id<RCTReloadListener>>(host_) didReceiveReloadCommand];
}
void onSetPausedInDebuggerMessage(const OverlaySetPausedInDebuggerMessageRequest &request) override
{
RCTAssertMainQueue();
if (!request.message.has_value()) {
[pauseOverlayController_ hide];
} else {
__weak RCTHost *hostWeak = host_;
[pauseOverlayController_
showWithMessage:@(request.message.value().c_str())
onResume:^{
RCTAssertMainQueue();
RCTHost *hostStrong = hostWeak;
if (!hostStrong) {
return;
}
if (!hostStrong.inspectorTarget) {
return;
}
hostStrong.inspectorTarget->sendCommand(jsinspector_modern::HostCommand::DebuggerResume);
}];
}
}
void loadNetworkResource(const RCTInspectorLoadNetworkResourceRequest ¶ms, RCTInspectorNetworkExecutor executor)
override
{
[networkHelper_ loadNetworkResourceWithParams:params executor:executor];
}
private:
__weak RCTHost *host_;
RCTPausedInDebuggerOverlayController *pauseOverlayController_;
RCTInspectorNetworkHelper *networkHelper_;
};
@implementation RCTHost {
RCTInstance *_instance;
__weak id<RCTHostDelegate> _hostDelegate;
__weak id<RCTTurboModuleManagerDelegate> _turboModuleManagerDelegate;
__weak id<RCTContextContainerHandling> _contextContainerHandler;
NSURL *_oldDelegateBundleURL;
NSURL *_bundleURL;
RCTBundleManager *_bundleManager;
RCTHostBundleURLProvider _bundleURLProvider;
RCTHostJSEngineProvider _jsEngineProvider;
NSDictionary *_launchOptions;
std::vector<__weak RCTFabricSurface *> _attachedSurfaces;
RCTModuleRegistry *_moduleRegistry;
std::unique_ptr<RCTHostHostTargetDelegate> _inspectorHostDelegate;
std::shared_ptr<jsinspector_modern::HostTarget> _inspectorTarget;
std::optional<int> _inspectorPageId;
}
+ (void)initialize
{
_RCTInitializeJSThreadConstantInternal();
}
- (instancetype)initWithBundleURL:(NSURL *)bundleURL
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
launchOptions:(nullable NSDictionary *)launchOptions
{
return [self
initWithBundleURLProvider:^{
return bundleURL;
}
hostDelegate:hostDelegate
turboModuleManagerDelegate:turboModuleManagerDelegate
jsEngineProvider:jsEngineProvider
launchOptions:launchOptions];
}
/**
Host initialization should not be resource intensive. A host may be created before any intention of using React Native
has been expressed.
*/
- (instancetype)initWithBundleURLProvider:(RCTHostBundleURLProvider)provider
hostDelegate:(id<RCTHostDelegate>)hostDelegate
turboModuleManagerDelegate:(id<RCTTurboModuleManagerDelegate>)turboModuleManagerDelegate
jsEngineProvider:(RCTHostJSEngineProvider)jsEngineProvider
launchOptions:(nullable NSDictionary *)launchOptions
{
if (self = [super init]) {
_hostDelegate = hostDelegate;
_turboModuleManagerDelegate = turboModuleManagerDelegate;
_bundleManager = [RCTBundleManager new];
_moduleRegistry = [RCTModuleRegistry new];
_jsEngineProvider = [jsEngineProvider copy];
_launchOptions = [launchOptions copy];
__weak RCTHost *weakSelf = self;
auto bundleURLGetter = ^NSURL *() {
RCTHost *strongSelf = weakSelf;
if (!strongSelf) {
return nil;
}
return strongSelf->_bundleURL;
};
auto bundleURLSetter = ^(NSURL *bundleURL_) {
[weakSelf _setBundleURL:bundleURL_];
};
auto defaultBundleURLGetter = ^NSURL *() {
RCTHost *strongSelf = weakSelf;
if (!strongSelf || !strongSelf->_bundleURLProvider) {
return nil;
}
return strongSelf->_bundleURLProvider();
};
[_bundleManager setBridgelessBundleURLGetter:bundleURLGetter
andSetter:bundleURLSetter
andDefaultGetter:defaultBundleURLGetter];
RCTExecuteOnMainQueue(^{
// Listen to reload commands
RCTRegisterReloadCommandListener(self);
});
_inspectorHostDelegate = std::make_unique<RCTHostHostTargetDelegate>(self);
}
return self;
}
#pragma mark - Public
- (void)start
{
if (_bundleURLProvider) {
[self _setBundleURL:_bundleURLProvider()];
}
auto &inspectorFlags = jsinspector_modern::InspectorFlags::getInstance();
if (inspectorFlags.getFuseboxEnabled() && !_inspectorPageId.has_value()) {
_inspectorTarget =
facebook::react::jsinspector_modern::HostTarget::create(*_inspectorHostDelegate, [](auto callback) {
RCTExecuteOnMainQueue(^{
callback();
});
});
__weak RCTHost *weakSelf = self;
_inspectorPageId = facebook::react::jsinspector_modern::getInspectorInstance().addPage(
"React Native Bridgeless",
/* vm */ "",
[weakSelf](std::unique_ptr<facebook::react::jsinspector_modern::IRemoteConnection> remote)
-> std::unique_ptr<facebook::react::jsinspector_modern::ILocalConnection> {
RCTHost *strongSelf = weakSelf;
if (!strongSelf) {
// This can happen if we're about to be dealloc'd. Reject the connection.
return nullptr;
}
return strongSelf->_inspectorTarget->connect(std::move(remote));
},
{.nativePageReloads = true, .prefersFuseboxFrontend = true});
}
if (_instance) {
RCTLogWarn(
@"RCTHost should not be creating a new instance if one already exists. This implies there is a bug with how/when this method is being called.");
[_instance invalidate];
}
_instance = [[RCTInstance alloc] initWithDelegate:self
jsRuntimeFactory:[self _provideJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
moduleRegistry:_moduleRegistry
parentInspectorTarget:_inspectorTarget.get()
launchOptions:_launchOptions];
[_hostDelegate hostDidStart:self];
}
- (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName
mode:(DisplayMode)displayMode
initialProperties:(NSDictionary *)properties
{
RCTFabricSurface *surface = [[RCTFabricSurface alloc] initWithSurfacePresenter:self.surfacePresenter
moduleName:moduleName
initialProperties:properties];
surface.surfaceHandler.setDisplayMode(displayMode);
[self _attachSurface:surface];
__weak RCTFabricSurface *weakSurface = surface;
// Use the BufferedRuntimeExecutor to start the surface after the main JS bundle was fully executed.
[_instance callFunctionOnBufferedRuntimeExecutor:[weakSurface](facebook::jsi::Runtime &_) { [weakSurface start]; }];
return surface;
}
- (RCTFabricSurface *)createSurfaceWithModuleName:(NSString *)moduleName initialProperties:(NSDictionary *)properties
{
return [self createSurfaceWithModuleName:moduleName mode:DisplayMode::Visible initialProperties:properties];
}
- (RCTModuleRegistry *)moduleRegistry
{
return _moduleRegistry;
}
- (RCTSurfacePresenter *)surfacePresenter
{
return [_instance surfacePresenter];
}
- (RCTBundleManager *)bundleManager
{
return _bundleManager;
}
- (void)callFunctionOnJSModule:(NSString *)moduleName method:(NSString *)method args:(NSArray *)args
{
[_instance callFunctionOnJSModule:moduleName method:method args:args];
}
#pragma mark - RCTReloadListener
- (void)didReceiveReloadCommand
{
[self _reloadWithShouldRestartSurfaces:YES];
}
- (void)dealloc
{
[_instance invalidate];
if (_inspectorPageId.has_value()) {
facebook::react::jsinspector_modern::getInspectorInstance().removePage(*_inspectorPageId);
_inspectorPageId.reset();
_inspectorTarget.reset();
}
}
#pragma mark - RCTInstanceDelegate
- (NSArray<NSString *> *)unstableModulesRequiringMainQueueSetup
{
if ([_hostDelegate respondsToSelector:@selector(unstableModulesRequiringMainQueueSetup)]) {
return [_hostDelegate unstableModulesRequiringMainQueueSetup];
}
return @[];
}
- (BOOL)instance:(RCTInstance *)instance
didReceiveJSErrorStack:(NSArray<NSDictionary<NSString *, id> *> *)stack
message:(NSString *)message
originalMessage:(NSString *_Nullable)originalMessage
name:(NSString *_Nullable)name
componentStack:(NSString *_Nullable)componentStack
exceptionId:(NSUInteger)exceptionId
isFatal:(BOOL)isFatal
extraData:(NSDictionary<NSString *, id> *)extraData
{
if (![_hostDelegate respondsToSelector:@selector(host:
didReceiveJSErrorStack:message:originalMessage:name:componentStack
:exceptionId:isFatal:extraData:)]) {
return NO;
}
[_hostDelegate host:self
didReceiveJSErrorStack:stack
message:message
originalMessage:originalMessage
name:name
componentStack:componentStack
exceptionId:exceptionId
isFatal:isFatal
extraData:extraData];
return YES;
}
- (void)instance:(RCTInstance *)instance didInitializeRuntime:(facebook::jsi::Runtime &)runtime
{
[self.runtimeDelegate host:self didInitializeRuntime:runtime];
}
- (void)loadBundleAtURL:(NSURL *)sourceURL
onProgress:(RCTSourceLoadProgressBlock)onProgress
onComplete:(RCTSourceLoadBlock)loadCallback
{
if ([_hostDelegate respondsToSelector:@selector(loadBundleAtURL:onProgress:onComplete:)]) {
[_hostDelegate loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback];
} else {
[RCTJavaScriptLoader loadBundleAtURL:sourceURL onProgress:onProgress onComplete:loadCallback];
}
}
#pragma mark - RCTContextContainerHandling
- (void)didCreateContextContainer:(std::shared_ptr<facebook::react::ContextContainer>)contextContainer
{
[_contextContainerHandler didCreateContextContainer:contextContainer];
}
#pragma mark - Internal
- (void)registerSegmentWithId:(NSNumber *)segmentId path:(NSString *)path
{
[_instance registerSegmentWithId:segmentId path:path];
}
- (void)setBundleURLProvider:(RCTHostBundleURLProvider)bundleURLProvider
{
_bundleURLProvider = [bundleURLProvider copy];
}
- (void)setContextContainerHandler:(id<RCTContextContainerHandling>)contextContainerHandler
{
_contextContainerHandler = contextContainerHandler;
}
- (void)reload
{
[self _reloadWithShouldRestartSurfaces:NO];
}
#pragma mark - Private
- (void)_attachSurface:(RCTFabricSurface *)surface
{
_attachedSurfaces.push_back(surface);
}
- (NSArray<RCTFabricSurface *> *)_getAttachedSurfaces
{
NSMutableArray<RCTFabricSurface *> *surfaces = [NSMutableArray new];
for (RCTFabricSurface *surface : _attachedSurfaces) {
if (surface) {
[surfaces addObject:surface];
}
}
return surfaces;
}
- (std::shared_ptr<facebook::react::JSRuntimeFactory>)_provideJSEngine
{
RCTAssert(_jsEngineProvider, @"_jsEngineProvider must be non-nil");
std::shared_ptr<facebook::react::JSRuntimeFactory> jsEngine = _jsEngineProvider();
RCTAssert(jsEngine != nullptr, @"_jsEngineProvider must return a nonnull pointer");
return jsEngine;
}
- (void)_setBundleURL:(NSURL *)bundleURL
{
// Reset the _bundleURL ivar if the RCTHost delegate presents a new bundleURL
NSURL *newDelegateBundleURL = bundleURL;
if (newDelegateBundleURL && ![newDelegateBundleURL isEqual:_oldDelegateBundleURL]) {
_oldDelegateBundleURL = newDelegateBundleURL;
_bundleURL = newDelegateBundleURL;
}
// Sanitize the bundle URL
_bundleURL = [RCTConvert NSURL:_bundleURL.absoluteString];
// Update the global bundle URLq
RCTReloadCommandSetBundleURL(_bundleURL);
}
- (void)_reloadWithShouldRestartSurfaces:(BOOL)shouldRestartSurfaces
{
[_instance invalidate];
_instance = nil;
if (_bundleURLProvider) {
[self _setBundleURL:_bundleURLProvider()];
}
_instance = [[RCTInstance alloc] initWithDelegate:self
jsRuntimeFactory:[self _provideJSEngine]
bundleManager:_bundleManager
turboModuleManagerDelegate:_turboModuleManagerDelegate
moduleRegistry:_moduleRegistry
parentInspectorTarget:_inspectorTarget.get()
launchOptions:_launchOptions];
[_hostDelegate hostDidStart:self];
for (RCTFabricSurface *surface in [self _getAttachedSurfaces]) {
[surface resetWithSurfacePresenter:self.surfacePresenter];
if (shouldRestartSurfaces) {
[_instance callFunctionOnBufferedRuntimeExecutor:[surface](facebook::jsi::Runtime &_) { [surface start]; }];
}
}
}
#pragma mark - jsinspector_modern
- (jsinspector_modern::HostTarget *)inspectorTarget
{
return _inspectorTarget.get();
}
@end