forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Class for JS stack frames instead of dictionaries
Summary: Currently React Native codebase treats JS stack traces as array of dictionaries. This diff switches the Red Box to use new `RCTJSStackFrame` for internal data representation, while keeping the exposed API unchanged. The next step would be to replace the rest of manual parsing and usage of dictionaries. The new class has ability to parse the stack from raw strings or dictionaries. Depends on D3429031 Reviewed By: javache Differential Revision: D3473199 fbshipit-source-id: 90d2a4f5e8e054b75c99905f35c2ee54927bb311
- Loading branch information
Showing
4 changed files
with
160 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface RCTJSStackFrame : NSObject | ||
|
||
@property (nonatomic, copy, readonly) NSString *methodName; | ||
@property (nonatomic, copy, readonly) NSString *file; | ||
@property (nonatomic, readonly) NSInteger lineNumber; | ||
@property (nonatomic, readonly) NSInteger column; | ||
|
||
- (instancetype)initWithMethodName:(NSString *)methodName file:(NSString *)file lineNumber:(NSInteger)lineNumber column:(NSInteger)column; | ||
- (NSDictionary *)toDictionary; | ||
|
||
+ (instancetype)stackFrameWithLine:(NSString *)line; | ||
+ (instancetype)stackFrameWithDictionary:(NSDictionary *)dict; | ||
+ (NSArray<RCTJSStackFrame *> *)stackFramesWithLines:(NSString *)lines; | ||
+ (NSArray<RCTJSStackFrame *> *)stackFramesWithDictionaries:(NSArray<NSDictionary *> *)dicts; | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* Copyright (c) 2015-present, Facebook, Inc. | ||
* All rights reserved. | ||
* | ||
* This source code is licensed under the BSD-style license found in the | ||
* LICENSE file in the root directory of this source tree. An additional grant | ||
* of patent rights can be found in the PATENTS file in the same directory. | ||
*/ | ||
|
||
#import "RCTJSStackFrame.h" | ||
#import "RCTLog.h" | ||
|
||
|
||
static NSRegularExpression *RCTJSStackFrameRegex() | ||
{ | ||
static dispatch_once_t onceToken; | ||
static NSRegularExpression *_regex; | ||
dispatch_once(&onceToken, ^{ | ||
NSError *regexError; | ||
_regex = [NSRegularExpression regularExpressionWithPattern:@"^([^@]+)@(.*):(\\d+):(\\d+)$" options:0 error:®exError]; | ||
if (regexError) { | ||
RCTLogError(@"Failed to build regex: %@", [regexError localizedDescription]); | ||
} | ||
}); | ||
return _regex; | ||
} | ||
|
||
@implementation RCTJSStackFrame | ||
|
||
- (instancetype)initWithMethodName:(NSString *)methodName file:(NSString *)file lineNumber:(NSInteger)lineNumber column:(NSInteger)column | ||
{ | ||
if (self = [super init]) { | ||
_methodName = methodName; | ||
_file = file; | ||
_lineNumber = lineNumber; | ||
_column = column; | ||
} | ||
return self; | ||
} | ||
|
||
- (NSDictionary *)toDictionary | ||
{ | ||
return @{ | ||
@"methodName": self.methodName, | ||
@"file": self.file, | ||
@"lineNumber": @(self.lineNumber), | ||
@"column": @(self.column) | ||
}; | ||
} | ||
|
||
+ (instancetype)stackFrameWithLine:(NSString *)line | ||
{ | ||
NSTextCheckingResult *match = [RCTJSStackFrameRegex() firstMatchInString:line options:0 range:NSMakeRange(0, line.length)]; | ||
if (!match) { | ||
return nil; | ||
} | ||
|
||
NSString *methodName = [line substringWithRange:[match rangeAtIndex:1]]; | ||
NSString *file = [line substringWithRange:[match rangeAtIndex:2]]; | ||
NSString *lineNumber = [line substringWithRange:[match rangeAtIndex:3]]; | ||
NSString *column = [line substringWithRange:[match rangeAtIndex:4]]; | ||
|
||
return [[self alloc] initWithMethodName:methodName | ||
file:file | ||
lineNumber:[lineNumber integerValue] | ||
column:[column integerValue]]; | ||
} | ||
|
||
+ (instancetype)stackFrameWithDictionary:(NSDictionary *)dict | ||
{ | ||
return [[self alloc] initWithMethodName:dict[@"methodName"] | ||
file:dict[@"file"] | ||
lineNumber:[dict[@"lineNumber"] integerValue] | ||
column:[dict[@"column"] integerValue]]; | ||
} | ||
|
||
+ (NSArray<RCTJSStackFrame *> *)stackFramesWithLines:(NSString *)lines | ||
{ | ||
NSMutableArray *stack = [NSMutableArray new]; | ||
for (NSString *line in [lines componentsSeparatedByString:@"\n"]) { | ||
RCTJSStackFrame *frame = [self stackFrameWithLine:line]; | ||
if (frame) { | ||
[stack addObject:frame]; | ||
} | ||
} | ||
return stack; | ||
} | ||
|
||
+ (NSArray<RCTJSStackFrame *> *)stackFramesWithDictionaries:(NSArray<NSDictionary *> *)dicts | ||
{ | ||
NSMutableArray *stack = [NSMutableArray new]; | ||
for (NSDictionary *dict in dicts) { | ||
RCTJSStackFrame *frame = [self stackFrameWithDictionary:dict]; | ||
if (frame) { | ||
[stack addObject:frame]; | ||
} | ||
} | ||
return stack; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters