2019-10-16 20:03:47 +03:00
|
|
|
/*
|
2021-12-31 02:08:43 +03:00
|
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
2015-03-23 23:28:42 +03:00
|
|
|
*
|
2018-02-17 05:24:55 +03:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-23 23:28:42 +03:00
|
|
|
*/
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
#import "RCTExceptionsManager.h"
|
|
|
|
|
2019-08-16 05:57:09 +03:00
|
|
|
#import <FBReactNativeSpec/FBReactNativeSpec.h>
|
|
|
|
#import <React/RCTConvert.h>
|
|
|
|
#import <React/RCTDefines.h>
|
|
|
|
#import <React/RCTLog.h>
|
|
|
|
#import <React/RCTRedBox.h>
|
2023-01-04 20:23:38 +03:00
|
|
|
#import <React/RCTRedBoxSetEnabled.h>
|
2019-10-30 22:20:29 +03:00
|
|
|
#import <React/RCTReloadCommand.h>
|
2019-08-16 05:57:09 +03:00
|
|
|
#import <React/RCTRootView.h>
|
|
|
|
|
|
|
|
#import "CoreModulesPlugins.h"
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
@interface RCTExceptionsManager () <NativeExceptionsManagerSpec>
|
2019-08-16 05:57:09 +03:00
|
|
|
|
|
|
|
@end
|
2015-02-20 07:10:52 +03:00
|
|
|
|
|
|
|
@implementation RCTExceptionsManager
|
2015-03-11 23:53:30 +03:00
|
|
|
|
2020-12-13 05:58:58 +03:00
|
|
|
@synthesize moduleRegistry = _moduleRegistry;
|
2015-08-19 15:27:43 +03:00
|
|
|
|
2015-04-08 15:42:43 +03:00
|
|
|
RCT_EXPORT_MODULE()
|
|
|
|
|
2015-03-11 23:53:30 +03:00
|
|
|
- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
|
|
|
|
{
|
2015-11-25 14:09:00 +03:00
|
|
|
if ((self = [self init])) {
|
2015-03-11 23:53:30 +03:00
|
|
|
_delegate = delegate;
|
|
|
|
}
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-04-25 13:26:24 +03:00
|
|
|
- (void)reportSoft:(NSString *)message
|
|
|
|
stack:(NSArray<NSDictionary *> *)stack
|
|
|
|
exceptionId:(double)exceptionId
|
|
|
|
extraDataAsJSON:(nullable NSString *)extraDataAsJSON
|
2020-03-09 08:58:01 +03:00
|
|
|
{
|
2023-01-04 20:23:38 +03:00
|
|
|
if (RCTRedBoxGetEnabled()) {
|
|
|
|
RCTRedBox *redbox = [_moduleRegistry moduleForName:"RedBox"];
|
|
|
|
[redbox showErrorMessage:message withStack:stack errorCookie:(int)exceptionId];
|
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
if (_delegate) {
|
|
|
|
[_delegate handleSoftJSExceptionWithMessage:message
|
|
|
|
stack:stack
|
2022-04-25 13:26:24 +03:00
|
|
|
exceptionId:[NSNumber numberWithDouble:exceptionId]
|
|
|
|
extraDataAsJSON:extraDataAsJSON];
|
2020-03-09 08:58:01 +03:00
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
}
|
|
|
|
|
2022-04-25 13:26:24 +03:00
|
|
|
- (void)reportFatal:(NSString *)message
|
|
|
|
stack:(NSArray<NSDictionary *> *)stack
|
|
|
|
exceptionId:(double)exceptionId
|
|
|
|
extraDataAsJSON:(nullable NSString *)extraDataAsJSON
|
2020-03-09 08:58:01 +03:00
|
|
|
{
|
2023-01-04 20:23:38 +03:00
|
|
|
if (RCTRedBoxGetEnabled()) {
|
|
|
|
RCTRedBox *redbox = [_moduleRegistry moduleForName:"RedBox"];
|
|
|
|
[redbox showErrorMessage:message withStack:stack errorCookie:(int)exceptionId];
|
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
if (_delegate) {
|
|
|
|
[_delegate handleFatalJSExceptionWithMessage:message
|
|
|
|
stack:stack
|
2022-04-25 13:26:24 +03:00
|
|
|
exceptionId:[NSNumber numberWithDouble:exceptionId]
|
|
|
|
extraDataAsJSON:extraDataAsJSON];
|
2020-03-09 08:58:01 +03:00
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
static NSUInteger reloadRetries = 0;
|
|
|
|
if (!RCT_DEBUG && reloadRetries < _maxReloadAttempts) {
|
|
|
|
reloadRetries++;
|
|
|
|
RCTTriggerReloadCommandListeners(@"JS Crash Reload");
|
2021-10-08 21:06:46 +03:00
|
|
|
} else if (!RCT_DEV) {
|
2020-03-09 08:58:01 +03:00
|
|
|
NSString *description = [@"Unhandled JS Exception: " stringByAppendingString:message];
|
2022-04-25 13:26:24 +03:00
|
|
|
NSDictionary *errorInfo =
|
|
|
|
@{NSLocalizedDescriptionKey : description, RCTJSStackTraceKey : stack, RCTJSExtraDataKey : extraDataAsJSON};
|
2020-03-09 08:58:01 +03:00
|
|
|
RCTFatal([NSError errorWithDomain:RCTErrorDomain code:0 userInfo:errorInfo]);
|
|
|
|
}
|
2019-11-01 02:22:38 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(reportSoftException
|
|
|
|
: (NSString *)message stack
|
|
|
|
: (NSArray<NSDictionary *> *)stack exceptionId
|
|
|
|
: (double)exceptionId)
|
2015-05-13 20:56:09 +03:00
|
|
|
{
|
2022-04-25 13:26:24 +03:00
|
|
|
[self reportSoft:message stack:stack exceptionId:exceptionId extraDataAsJSON:nil];
|
2015-05-13 20:56:09 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(reportFatalException
|
|
|
|
: (NSString *)message stack
|
|
|
|
: (NSArray<NSDictionary *> *)stack exceptionId
|
|
|
|
: (double)exceptionId)
|
2015-02-20 07:10:52 +03:00
|
|
|
{
|
2022-04-25 13:26:24 +03:00
|
|
|
[self reportFatal:message stack:stack exceptionId:exceptionId extraDataAsJSON:nil];
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(updateExceptionMessage
|
|
|
|
: (NSString *)message stack
|
|
|
|
: (NSArray<NSDictionary *> *)stack exceptionId
|
|
|
|
: (double)exceptionId)
|
2015-02-20 07:10:52 +03:00
|
|
|
{
|
2023-01-04 20:23:38 +03:00
|
|
|
if (RCTRedBoxGetEnabled()) {
|
|
|
|
RCTRedBox *redbox = [_moduleRegistry moduleForName:"RedBox"];
|
|
|
|
[redbox updateErrorMessage:message withStack:stack errorCookie:(int)exceptionId];
|
|
|
|
}
|
2015-11-05 23:19:56 +03:00
|
|
|
|
|
|
|
if (_delegate && [_delegate respondsToSelector:@selector(updateJSExceptionWithMessage:stack:exceptionId:)]) {
|
2019-08-16 05:57:09 +03:00
|
|
|
[_delegate updateJSExceptionWithMessage:message stack:stack exceptionId:[NSNumber numberWithDouble:exceptionId]];
|
2015-11-05 23:19:56 +03:00
|
|
|
}
|
2015-02-20 07:10:52 +03:00
|
|
|
}
|
|
|
|
|
2015-05-13 20:56:09 +03:00
|
|
|
// Deprecated. Use reportFatalException directly instead.
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(reportUnhandledException : (NSString *)message stack : (NSArray<NSDictionary *> *)stack)
|
2015-05-13 20:56:09 +03:00
|
|
|
{
|
2019-08-16 05:57:09 +03:00
|
|
|
[self reportFatalException:message stack:stack exceptionId:-1];
|
|
|
|
}
|
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(dismissRedbox) {}
|
2019-08-16 05:57:09 +03:00
|
|
|
|
2020-03-09 08:58:01 +03:00
|
|
|
RCT_EXPORT_METHOD(reportException : (JS::NativeExceptionsManager::ExceptionData &)data)
|
2019-08-16 05:57:09 +03:00
|
|
|
{
|
2019-09-06 20:10:23 +03:00
|
|
|
NSString *message = data.message();
|
|
|
|
double exceptionId = data.id_();
|
|
|
|
|
|
|
|
// Reserialize data.stack() into an array of untyped dictionaries.
|
|
|
|
// TODO: (moti) T53588496 Replace `(NSArray<NSDictionary *> *)stack` in
|
|
|
|
// reportFatalException etc with a typed interface.
|
|
|
|
NSMutableArray<NSDictionary *> *stackArray = [NSMutableArray<NSDictionary *> new];
|
2020-03-09 08:58:01 +03:00
|
|
|
for (auto frame : data.stack()) {
|
|
|
|
NSMutableDictionary *frameDict = [NSMutableDictionary new];
|
2022-03-23 03:10:18 +03:00
|
|
|
if (frame.column().has_value()) {
|
2019-09-06 20:10:23 +03:00
|
|
|
frameDict[@"column"] = @(frame.column().value());
|
|
|
|
}
|
|
|
|
frameDict[@"file"] = frame.file();
|
2022-03-23 03:10:18 +03:00
|
|
|
if (frame.lineNumber().has_value()) {
|
2020-03-09 08:58:01 +03:00
|
|
|
frameDict[@"lineNumber"] = @(frame.lineNumber().value());
|
2019-09-06 20:10:23 +03:00
|
|
|
}
|
|
|
|
frameDict[@"methodName"] = frame.methodName();
|
2022-03-23 03:10:18 +03:00
|
|
|
if (frame.collapse().has_value()) {
|
2020-03-09 08:58:01 +03:00
|
|
|
frameDict[@"collapse"] = @(frame.collapse().value());
|
2019-09-06 20:10:23 +03:00
|
|
|
}
|
|
|
|
[stackArray addObject:frameDict];
|
|
|
|
}
|
2019-08-16 05:57:09 +03:00
|
|
|
|
2022-04-25 13:26:24 +03:00
|
|
|
NSDictionary *extraData = (NSDictionary *)data.extraData();
|
|
|
|
NSString *extraDataAsJSON = RCTJSONStringify(extraData, NULL);
|
|
|
|
|
2019-09-06 20:10:23 +03:00
|
|
|
if (data.isFatal()) {
|
2022-04-25 13:26:24 +03:00
|
|
|
[self reportFatal:message stack:stackArray exceptionId:exceptionId extraDataAsJSON:extraDataAsJSON];
|
2019-09-06 20:10:23 +03:00
|
|
|
} else {
|
2022-04-25 13:26:24 +03:00
|
|
|
[self reportSoft:message stack:stackArray exceptionId:exceptionId extraDataAsJSON:extraDataAsJSON];
|
2019-09-06 20:10:23 +03:00
|
|
|
}
|
2019-08-16 05:57:09 +03:00
|
|
|
}
|
|
|
|
|
2022-09-08 00:41:45 +03:00
|
|
|
- (void)reportJsException:(nullable NSString *)message
|
|
|
|
stack:(nullable NSArray<NSDictionary *> *)stack
|
|
|
|
exceptionId:(double)exceptionId
|
|
|
|
isFatal:(bool)isFatal
|
2022-03-10 21:25:32 +03:00
|
|
|
{
|
|
|
|
if (isFatal) {
|
|
|
|
[self reportFatalException:message stack:stack exceptionId:exceptionId];
|
|
|
|
} else {
|
|
|
|
[self reportSoftException:message stack:stack exceptionId:exceptionId];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Part 2: Update ObjC++ codegen classes to use ObjCTurboModule::InitParams
Summary:
## Summary
Please check out D21035209.
## Changes
- Codemod all ObjC NativeModule `getTurboModuleWithJsInvoker:nativeInvoker:perfLogger` methods to `getTurboModule:(const ObjCTurboModule::Args)`
## Script
```
var withSpaces = (...args) => args.join('\s*')
var regexString = withSpaces(
'-',
'\(',
'std::shared_ptr',
'<',
'(?<turboModuleClass>(facebook::react::|react::|::|)TurboModule)',
'>',
'\)',
'getTurboModuleWithJsInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(?<fbNamespace>(facebook::react::|react::|::|))CallInvoker',
'>',
'\)',
'(?<jsInvokerInstance>[A-Za-z0-9]+)',
'nativeInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(facebook::react::|react::|::|)CallInvoker',
'>',
'\)',
'(?<nativeInvokerInstance>[A-Za-z0-9]+)',
'perfLogger',
':',
'\(',
'id',
'<',
'RCTTurboModulePerformanceLogger',
'>',
'\)',
'(?<perfLoggerInstance>[A-Za-z0-9]+)',
'{',
'return',
'std::make_shared',
'<',
'(?<specName>(facebook::react::|react::|::|)Native[%A-Za-z0-9]+SpecJSI)',
'>',
'\(',
'self',
',',
'\k<jsInvokerInstance>',
',',
'\k<nativeInvokerInstance>',
',',
'\k<perfLoggerInstance>',
'\)',
';',
'}',
)
var replaceString = `- (std::shared_ptr<$<turboModuleClass>>) getTurboModule:(const $<fbNamespace>ObjCTurboModule::InitParams &)params
{
return std::make_shared<$<specName>>(params);
}`
const exec = require('../lib/exec');
const abspath = require('../lib/abspath');
const relpath = require('../lib/relpath');
const readFile = (filename) => require('fs').readFileSync(filename, 'utf8');
const writeFile = (filename, content) => require('fs').writeFileSync(filename, content);
function main() {
const tmFiles = exec('cd ~/fbsource && xbgs -n 10000 -l getTurboModuleWithJsInvoker:').split('\n').filter(Boolean);
tmFiles
.filter((filename) => !filename.includes('microsoft-fork-of-react-native'))
.map(abspath)
.forEach((filename) => {
const source = readFile(filename);
const newSource = source.replace(new RegExp(regexString, 'g'), replaceString);
if (source == newSource) {
console.log(relpath(filename));
}
writeFile(filename, newSource);
});
}
if (!module.parent) {
main();
}
```
## Re-generating diff
```
> hg revert -r .^ --all
> node index.js # run script
```
Changelog: [iOS][Changed] - Make all ObjC NativeModules create TurboModules using ObjCTurboModule::Args
Reviewed By: PeteTheHeat
Differential Revision: D21036265
fbshipit-source-id: 404bcc548d1775ef23d793527606d02fe384a0a2
2020-04-17 03:23:39 +03:00
|
|
|
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
|
|
(const facebook::react::ObjCTurboModule::InitParams &)params
|
2019-08-16 05:57:09 +03:00
|
|
|
{
|
Part 2: Update ObjC++ codegen classes to use ObjCTurboModule::InitParams
Summary:
## Summary
Please check out D21035209.
## Changes
- Codemod all ObjC NativeModule `getTurboModuleWithJsInvoker:nativeInvoker:perfLogger` methods to `getTurboModule:(const ObjCTurboModule::Args)`
## Script
```
var withSpaces = (...args) => args.join('\s*')
var regexString = withSpaces(
'-',
'\(',
'std::shared_ptr',
'<',
'(?<turboModuleClass>(facebook::react::|react::|::|)TurboModule)',
'>',
'\)',
'getTurboModuleWithJsInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(?<fbNamespace>(facebook::react::|react::|::|))CallInvoker',
'>',
'\)',
'(?<jsInvokerInstance>[A-Za-z0-9]+)',
'nativeInvoker',
':',
'\(',
'std::shared_ptr',
'<',
'(facebook::react::|react::|::|)CallInvoker',
'>',
'\)',
'(?<nativeInvokerInstance>[A-Za-z0-9]+)',
'perfLogger',
':',
'\(',
'id',
'<',
'RCTTurboModulePerformanceLogger',
'>',
'\)',
'(?<perfLoggerInstance>[A-Za-z0-9]+)',
'{',
'return',
'std::make_shared',
'<',
'(?<specName>(facebook::react::|react::|::|)Native[%A-Za-z0-9]+SpecJSI)',
'>',
'\(',
'self',
',',
'\k<jsInvokerInstance>',
',',
'\k<nativeInvokerInstance>',
',',
'\k<perfLoggerInstance>',
'\)',
';',
'}',
)
var replaceString = `- (std::shared_ptr<$<turboModuleClass>>) getTurboModule:(const $<fbNamespace>ObjCTurboModule::InitParams &)params
{
return std::make_shared<$<specName>>(params);
}`
const exec = require('../lib/exec');
const abspath = require('../lib/abspath');
const relpath = require('../lib/relpath');
const readFile = (filename) => require('fs').readFileSync(filename, 'utf8');
const writeFile = (filename, content) => require('fs').writeFileSync(filename, content);
function main() {
const tmFiles = exec('cd ~/fbsource && xbgs -n 10000 -l getTurboModuleWithJsInvoker:').split('\n').filter(Boolean);
tmFiles
.filter((filename) => !filename.includes('microsoft-fork-of-react-native'))
.map(abspath)
.forEach((filename) => {
const source = readFile(filename);
const newSource = source.replace(new RegExp(regexString, 'g'), replaceString);
if (source == newSource) {
console.log(relpath(filename));
}
writeFile(filename, newSource);
});
}
if (!module.parent) {
main();
}
```
## Re-generating diff
```
> hg revert -r .^ --all
> node index.js # run script
```
Changelog: [iOS][Changed] - Make all ObjC NativeModules create TurboModules using ObjCTurboModule::Args
Reviewed By: PeteTheHeat
Differential Revision: D21036265
fbshipit-source-id: 404bcc548d1775ef23d793527606d02fe384a0a2
2020-04-17 03:23:39 +03:00
|
|
|
return std::make_shared<facebook::react::NativeExceptionsManagerSpecJSI>(params);
|
2015-05-13 20:56:09 +03:00
|
|
|
}
|
2015-11-05 23:19:56 +03:00
|
|
|
|
2015-02-20 07:10:52 +03:00
|
|
|
@end
|
2019-08-16 05:57:09 +03:00
|
|
|
|
|
|
|
Class RCTExceptionsManagerCls(void)
|
|
|
|
{
|
|
|
|
return RCTExceptionsManager.class;
|
|
|
|
}
|