Skip to content

Commit 64cfc44

Browse files
p-sunfacebook-github-bot
authored andcommitted
Improve NewArchitectureValidation for AbsoluteBridgeless
Summary: Changelog: [Internal] - Make the new architecture validation easier to understand by enabling validation with `RCTNewArchitectureSetMinValidationLevel(level)`. - When `RCT_ONLY_NEW_ARCHITECTURE` flag is enabled: - `RCTErrorNewArchitectureValidation` calls `RCTLogAssert` instead of `RCTLogError`. - `RCTNewArchitectureValidationPlaceholder` calls `RCTLog`, instead of no-op. Reviewed By: fkgozali Differential Revision: D37555667 fbshipit-source-id: 2c725c287a2dec19e8946c7fe5d8fa111e4a17fa
1 parent daea147 commit 64cfc44

File tree

2 files changed

+53
-34
lines changed

2 files changed

+53
-34
lines changed

React/Base/RCTAssert.h

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,24 +178,24 @@ RCT_EXTERN NSString *RCTFormatStackTrace(NSArray<NSDictionary<NSString *, id> *>
178178
// MARK: - New Architecture Validation
179179

180180
typedef enum {
181-
RCTNotAllowedValidationDisabled = 0,
182-
RCTNotAllowedInAppWideFabric = 1,
183-
RCTNotAllowedInBridgeless = 2,
181+
RCTNotAllowedInBridgeless = 1,
182+
RCTNotAllowedInAppWideFabric = 2,
183+
RCTNotAllowedValidationDisabled = 3,
184184
} RCTNotAllowedValidation;
185185

186186
/**
187187
* Ensure runtime assumptions holds for the new architecture by reporting when assumptions are violated.
188188
* Note: this is work in progress.
189189
*
190-
* When type is RCTNotAllowedInAppWideFabric, validate Fabric assumptions in Bridge or Bridgeless mode.
190+
* When level is RCTNotAllowedInAppWideFabric, validate Fabric assumptions.
191191
* i.e. Report legacy pre-Fabric call sites that should not be used while Fabric is enabled on all surfaces.
192192
*
193-
* When type is RCTNotAllowedInBridgeless, validate Bridgeless assumptions, in Bridgeless mode only.
193+
* When level is RCTNotAllowedInBridgeless, validate Fabric or Bridgeless assumptions.
194194
* i.e. Report Bridge call sites that should not be used while Bridgeless mode is enabled.
195195
*
196196
* Note: enabling this at runtime is not early enough to report issues within ObjC class +load execution.
197197
*/
198-
__attribute__((used)) RCT_EXTERN void RCTNewArchitectureValidationSetEnabled(RCTNotAllowedValidation type);
198+
__attribute__((used)) RCT_EXTERN void RCTNewArchitectureSetMinValidationLevel(RCTNotAllowedValidation level);
199199

200200
// When new architecture validation reporting is enabled, trigger an assertion and crash.
201201
__attribute__((used)) RCT_EXTERN void
@@ -208,3 +208,7 @@ RCTErrorNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSSt
208208
// When ready, switch to stricter variant above.
209209
__attribute__((used)) RCT_EXTERN void
210210
RCTLogNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra);
211+
// A placeholder for callsites that frequently fail validation.
212+
// When ready, switch to stricter variant above.
213+
__attribute__((used)) RCT_EXTERN void
214+
RCTNewArchitectureValidationPlaceholder(RCTNotAllowedValidation type, id context, NSString *extra);

React/Base/RCTAssert.m

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -235,34 +235,25 @@ RCTFatalExceptionHandler RCTGetFatalExceptionHandler(void)
235235
// MARK: - New Architecture Validation - Enable Reporting
236236

237237
#if RCT_ONLY_NEW_ARCHITECTURE
238-
static RCTNotAllowedValidation validationReportingEnabled = RCTNotAllowedInBridgeless;
238+
static RCTNotAllowedValidation minValidationLevel = RCTNotAllowedInBridgeless;
239239
#else
240-
static RCTNotAllowedValidation validationReportingEnabled = RCTNotAllowedValidationDisabled;
240+
static RCTNotAllowedValidation minValidationLevel = RCTNotAllowedValidationDisabled;
241241
#endif
242242

243-
__attribute__((used)) RCT_EXTERN void RCTNewArchitectureValidationSetEnabled(RCTNotAllowedValidation type)
243+
__attribute__((used)) RCT_EXTERN void RCTNewArchitectureSetMinValidationLevel(RCTNotAllowedValidation level)
244244
{
245245
#if RCT_ONLY_NEW_ARCHITECTURE
246246
// Cannot disable the reporting in this mode.
247247
#else
248-
validationReportingEnabled = type;
248+
minValidationLevel = level;
249249
#endif
250250
}
251251

252252
// MARK: - New Architecture Validation - Private
253253

254254
static BOOL shouldEnforceValidation(RCTNotAllowedValidation type)
255255
{
256-
switch (type) {
257-
case RCTNotAllowedInAppWideFabric:
258-
return validationReportingEnabled == RCTNotAllowedInBridgeless ||
259-
validationReportingEnabled == RCTNotAllowedInAppWideFabric;
260-
case RCTNotAllowedInBridgeless:
261-
return validationReportingEnabled == RCTNotAllowedInBridgeless;
262-
case RCTNotAllowedValidationDisabled:
263-
return NO;
264-
}
265-
return NO;
256+
return type >= minValidationLevel;
266257
}
267258

268259
static NSString *stringDescribingContext(id context)
@@ -284,7 +275,7 @@ static BOOL shouldEnforceValidation(RCTNotAllowedValidation type)
284275
switch (type) {
285276
case RCTNotAllowedValidationDisabled:
286277
RCTAssert(0, @"RCTNotAllowedValidationDisabled not a validation type.");
287-
break;
278+
return nil;
288279
case RCTNotAllowedInAppWideFabric:
289280
notAllowedType = @"Fabric";
290281
break;
@@ -300,31 +291,55 @@ static BOOL shouldEnforceValidation(RCTNotAllowedValidation type)
300291
extra ?: @""];
301292
}
302293

303-
// MARK: - New Architecture Validation - Public
304-
305-
void RCTEnforceNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra)
294+
static void
295+
newArchitectureValidationInternal(RCTLogLevel level, RCTNotAllowedValidation type, id context, NSString *extra)
306296
{
307297
if (!shouldEnforceValidation(type)) {
308298
return;
309299
}
310300

311-
RCTAssert(0, @"%@", validationMessage(type, context, extra));
301+
NSString *msg = validationMessage(type, context, extra);
302+
if (msg) {
303+
switch (level) {
304+
case RCTLogLevelInfo:
305+
RCTLogInfo(@"%@", msg);
306+
break;
307+
case RCTLogLevelError:
308+
RCTLogError(@"%@", msg);
309+
break;
310+
case RCTLogLevelFatal:
311+
RCTAssert(0, @"%@", msg);
312+
break;
313+
default:
314+
RCTAssert(0, @"New architecture validation is only for info, error, and fatal levels.");
315+
}
316+
}
312317
}
313318

314-
void RCTErrorNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra)
319+
// MARK: - New Architecture Validation - Public
320+
321+
void RCTEnforceNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra)
315322
{
316-
if (!shouldEnforceValidation(type)) {
317-
return;
318-
}
323+
newArchitectureValidationInternal(RCTLogLevelFatal, type, context, extra);
324+
}
319325

320-
RCTLogError(@"%@", validationMessage(type, context, extra));
326+
void RCTErrorNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra)
327+
{
328+
#if RCT_ONLY_NEW_ARCHITECTURE
329+
newArchitectureValidationInternal(RCTLogLevelFatal, type, context, extra);
330+
#else
331+
newArchitectureValidationInternal(RCTLogLevelError, type, context, extra);
332+
#endif
321333
}
322334

323335
void RCTLogNewArchitectureValidation(RCTNotAllowedValidation type, id context, NSString *extra)
324336
{
325-
if (!shouldEnforceValidation(type)) {
326-
return;
327-
}
337+
newArchitectureValidationInternal(RCTLogLevelInfo, type, context, extra);
338+
}
328339

329-
RCTLogInfo(@"%@", validationMessage(type, context, extra));
340+
void RCTNewArchitectureValidationPlaceholder(RCTNotAllowedValidation type, id context, NSString *extra)
341+
{
342+
#if RCT_ONLY_NEW_ARCHITECTURE
343+
newArchitectureValidationInternal(RCTLogLevelInfo, type, context, extra);
344+
#endif
330345
}

0 commit comments

Comments
 (0)