Skip to content

Commit e6ef083

Browse files
luluwu2032facebook-github-bot
authored andcommitted
Migrate JS error handler to MapBuffer
Summary: Changelog: [iOS][Changed] Replace Folly with MapBuffer for passing js error data Reviewed By: sammy-SC Differential Revision: D38460203 fbshipit-source-id: 98f6243e31da42cc601727545b331392300cee20
1 parent 7909b91 commit e6ef083

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

React/CoreModules/BUCK

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
load("@fbsource//tools/build_defs/apple:flag_defs.bzl", "get_objc_arc_preprocessor_flags", "get_preprocessor_flags_for_build_mode")
2-
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "rn_apple_library", "rn_extra_build_flags")
2+
load("@fbsource//tools/build_defs/oss:rn_defs.bzl", "react_native_xplat_target", "rn_apple_library", "rn_extra_build_flags")
33
load(
44
"@fbsource//xplat/configurations/buck/apple/plugins/sad_xplat_hosted_configurations:react_module_registration.bzl",
55
"react_module_plugin_providers",
@@ -129,6 +129,9 @@ rn_apple_library(
129129
],
130130
reexport_all_header_dependencies = True,
131131
visibility = ["PUBLIC"],
132+
deps = [
133+
react_native_xplat_target("react/renderer/mapbuffer:mapbufferApple"),
134+
],
132135
exported_deps = [
133136
"//xplat/js/react-native-github:FBReactNativeSpecApple",
134137
"//xplat/js/react-native-github:RCTLinkingApple",

React/CoreModules/RCTExceptionsManager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#import <Foundation/Foundation.h>
99
#import <React/RCTBridgeModule.h>
10+
#import <react/renderer/mapbuffer/MapBuffer.h>
1011

1112
NS_ASSUME_NONNULL_BEGIN
1213

@@ -38,7 +39,7 @@ NS_ASSUME_NONNULL_BEGIN
3839
- (void)reportFatalException:(nullable NSString *)message
3940
stack:(nullable NSArray<NSDictionary *> *)stack
4041
exceptionId:(double)exceptionId;
41-
- (void)reportJsException:(NSString *)errorMap;
42+
- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap;
4243

4344
@property (nonatomic, weak) id<RCTExceptionsManagerDelegate> delegate;
4445

React/CoreModules/RCTExceptionsManager.mm

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ @implementation RCTExceptionsManager
2525

2626
@synthesize moduleRegistry = _moduleRegistry;
2727

28+
const int FILE_KEY_OF_JS_ERROR = 0;
29+
const int METHOD_NAME_KEY_OF_JS_ERROR = 1;
30+
const int LINE_NUMBER_KEY_OF_JS_ERROR = 2;
31+
const int COLUMN_KEY_OF_JS_ERROR = 3;
32+
const int FRAMES_KEY_OF_JS_ERROR = 4;
33+
const int MESSAGE_KEY_OF_JS_ERROR = 5;
34+
const int ID_KEY_OF_JS_ERROR = 6;
35+
const int IS_FATAL_KEY_OF_JS_ERROR = 7;
36+
2837
RCT_EXPORT_MODULE()
2938

3039
- (instancetype)initWithDelegate:(id<RCTExceptionsManagerDelegate>)delegate
@@ -150,19 +159,25 @@ - (void)reportFatal:(NSString *)message
150159
}
151160
}
152161

153-
- (void)reportJsException:(NSString *)errorStr
162+
- (void)reportJsException:(const facebook::react::MapBuffer &)errorMap
154163
{
155-
NSData *jsonData = [errorStr dataUsingEncoding:NSUTF8StringEncoding];
156-
NSError *jsonError;
157-
NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData
158-
options:NSJSONWritingPrettyPrinted
159-
error:&jsonError];
160-
161-
NSString *message = [dict objectForKey:@"message"];
162-
double exceptionId = [[dict objectForKey:@"id"] doubleValue];
163-
NSArray *stack = [dict objectForKey:@"stack"];
164-
BOOL isFatal = [[dict objectForKey:@"isFatal"] boolValue];
165-
164+
NSString *message = [NSString stringWithCString:errorMap.getString(MESSAGE_KEY_OF_JS_ERROR).c_str()
165+
encoding:[NSString defaultCStringEncoding]];
166+
int exceptionId = errorMap.getInt(ID_KEY_OF_JS_ERROR);
167+
BOOL isFatal = errorMap.getBool(IS_FATAL_KEY_OF_JS_ERROR);
168+
std::vector<facebook::react::MapBuffer> frames = errorMap.getMapBufferList(FRAMES_KEY_OF_JS_ERROR);
169+
NSMutableArray *stack = [[NSMutableArray alloc] init];
170+
for (facebook::react::MapBuffer const &mapBuffer : frames) {
171+
NSDictionary *frame = @{
172+
@"file" : [NSString stringWithCString:mapBuffer.getString(FILE_KEY_OF_JS_ERROR).c_str()
173+
encoding:[NSString defaultCStringEncoding]],
174+
@"methodName" : [NSString stringWithCString:mapBuffer.getString(METHOD_NAME_KEY_OF_JS_ERROR).c_str()
175+
encoding:[NSString defaultCStringEncoding]],
176+
@"lineNumber" : [NSNumber numberWithInt:mapBuffer.getInt(LINE_NUMBER_KEY_OF_JS_ERROR)],
177+
@"column" : [NSNumber numberWithInt:mapBuffer.getInt(COLUMN_KEY_OF_JS_ERROR)],
178+
};
179+
[stack addObject:frame];
180+
}
166181
if (isFatal) {
167182
[self reportFatalException:message stack:stack exceptionId:exceptionId];
168183
} else {

0 commit comments

Comments
 (0)