2016-07-11 13:05:51 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-07-11 13:05:51 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#import "RCTJSStackFrame.h"
|
2017-04-06 02:44:39 +00:00
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
#import "RCTLog.h"
|
2016-08-26 16:47:25 +00:00
|
|
|
#import "RCTUtils.h"
|
2016-07-11 13:05:51 +00:00
|
|
|
|
2018-04-20 15:56:02 +00:00
|
|
|
/**
|
|
|
|
* The RegEx used to parse Error.stack.
|
|
|
|
*
|
|
|
|
* JavaScriptCore has the following format:
|
|
|
|
*
|
|
|
|
* Exception: Error: argh
|
|
|
|
* func1@/path/to/file.js:2:18
|
|
|
|
* func2@/path/to/file.js:6:8
|
|
|
|
* eval@[native code]
|
|
|
|
* global code@/path/to/file.js:13:5
|
|
|
|
*
|
|
|
|
* Another supported format:
|
|
|
|
*
|
|
|
|
* Error: argh
|
|
|
|
* at func1 (/path/to/file.js:2:18)
|
|
|
|
* at func2 (/path/to/file.js:6:8)
|
|
|
|
* at eval (native)
|
|
|
|
* at global (/path/to/file.js:13:5)
|
|
|
|
*/
|
2016-07-11 13:05:51 +00:00
|
|
|
static NSRegularExpression *RCTJSStackFrameRegex()
|
|
|
|
{
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
static NSRegularExpression *_regex;
|
|
|
|
dispatch_once(&onceToken, ^{
|
2018-04-20 15:56:02 +00:00
|
|
|
NSString *pattern =
|
|
|
|
@"\\s*(?:at)?\\s*" // Skip leading "at" and whitespace, noncapturing
|
|
|
|
@"(.+?)" // Capture the function name (group 1)
|
|
|
|
@"\\s*[@(]" // Skip whitespace, then @ or (
|
|
|
|
@"(.*):" // Capture the file name (group 2), then colon
|
|
|
|
@"(\\d+):(\\d+)" // Line and column number (groups 3 and 4)
|
|
|
|
@"\\)?$" // Optional closing paren and EOL
|
|
|
|
;
|
2016-07-11 13:05:51 +00:00
|
|
|
NSError *regexError;
|
2018-04-20 15:56:02 +00:00
|
|
|
_regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:®exError];
|
2016-07-11 13:05:51 +00:00
|
|
|
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 @{
|
2016-08-26 16:47:25 +00:00
|
|
|
@"methodName": RCTNullIfNil(self.methodName),
|
|
|
|
@"file": RCTNullIfNil(self.file),
|
|
|
|
@"lineNumber": @(self.lineNumber),
|
|
|
|
@"column": @(self.column)
|
|
|
|
};
|
2016-07-11 13:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (instancetype)stackFrameWithLine:(NSString *)line
|
|
|
|
{
|
|
|
|
NSTextCheckingResult *match = [RCTJSStackFrameRegex() firstMatchInString:line options:0 range:NSMakeRange(0, line.length)];
|
|
|
|
if (!match) {
|
|
|
|
return nil;
|
|
|
|
}
|
|
|
|
|
2017-10-05 20:17:07 +00:00
|
|
|
// methodName may not be present for e.g. anonymous functions
|
|
|
|
const NSRange methodNameRange = [match rangeAtIndex:1];
|
|
|
|
NSString *methodName = methodNameRange.location == NSNotFound ? nil : [line substringWithRange:methodNameRange];
|
2016-07-11 13:05:51 +00:00
|
|
|
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
|
|
|
|
{
|
2017-10-05 20:17:07 +00:00
|
|
|
return [[self alloc] initWithMethodName:RCTNilIfNull(dict[@"methodName"])
|
2016-07-11 13:05:51 +00:00
|
|
|
file:dict[@"file"]
|
2017-03-31 20:19:00 +00:00
|
|
|
lineNumber:[RCTNilIfNull(dict[@"lineNumber"]) integerValue]
|
|
|
|
column:[RCTNilIfNull(dict[@"column"]) integerValue]];
|
2016-07-11 13:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (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;
|
|
|
|
}
|
|
|
|
|
2017-04-06 02:44:39 +00:00
|
|
|
- (NSString *)description {
|
|
|
|
return [NSString stringWithFormat:@"<%@: %p method name: %@; file name: %@; line: %ld; column: %ld>",
|
|
|
|
self.class,
|
|
|
|
self,
|
|
|
|
self.methodName,
|
|
|
|
self.file,
|
|
|
|
(long)self.lineNumber,
|
|
|
|
(long)self.column];
|
|
|
|
}
|
|
|
|
|
2016-07-11 13:05:51 +00:00
|
|
|
@end
|