-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathJintJsErrorHelpers.cs
More file actions
205 lines (178 loc) · 6.29 KB
/
JintJsErrorHelpers.cs
File metadata and controls
205 lines (178 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Text;
using System.Text.RegularExpressions;
using AdvancedStringBuilder;
using JavaScriptEngineSwitcher.Core.Helpers;
namespace JavaScriptEngineSwitcher.Jint.Helpers
{
/// <summary>
/// JS error helpers
/// </summary>
internal static class JintJsErrorHelpers
{
#region Error location
private const string OriginalAnonymousFunctionName = "(anonymous)";
private const string WrapperAnonymousFunctionName = "Anonymous function";
private const string DelegateFunctionName = "delegate";
/// <summary>
/// Pattern for working with document names with coordinates
/// </summary>
private static readonly string DocumentNameWithCoordinatesPattern =
@"(?<documentName>" + CommonRegExps.DocumentNamePattern + @"):" +
@"(?<lineNumber>\d+)(?::(?<columnNumber>\d+))?";
/// <summary>
/// Regular expression for working with line of the script error location
/// </summary>
private static readonly Regex _errorLocationLineRegex =
new Regex(@"^[ ]{4}at " +
@"(?:" +
@"(?<functionName>" +
@"[\w][\w ]*" +
@"|" +
CommonRegExps.JsFullNamePattern +
@"|" +
Regex.Escape(OriginalAnonymousFunctionName) +
@") " +
@"\(" + DocumentNameWithCoordinatesPattern + @"\)" +
@"|" +
DocumentNameWithCoordinatesPattern +
@")" +
"$");
/// <summary>
/// Regular expression for working with the syntax error message
/// </summary>
private static readonly Regex _syntaxErrorMessageRegex =
new Regex(@"^(?<description>[\s\S]+?) \(" + CommonRegExps.DocumentNamePattern + @":\d+:\d+\)$");
/// <summary>
/// Regular expression for working with the script preparation error message
/// </summary>
private static readonly Regex _scriptPreparationErrorMessageRegex =
new Regex(@"^Could not prepare script: (?<description>[\s\S]+?) " +
@"\((?<documentName>" + CommonRegExps.DocumentNamePattern + @"):" +
@"(?<lineNumber>\d+):(?<columnNumber>\d+)\)$");
/// <summary>
/// Call chain separator
/// </summary>
private static readonly string[] _callChainSeparator = ["->"];
/// <summary>
/// Parses a string representation of the script error location to produce an array of
/// <see cref="ErrorLocationItem"/> instances
/// </summary>
/// <param name="errorLocation">String representation of the script error location</param>
/// <returns>An array of <see cref="ErrorLocationItem"/> instances</returns>
public static ErrorLocationItem[] ParseErrorLocation(string errorLocation)
{
return JsErrorHelpers.ParseErrorLocation(errorLocation, MapErrorLocationItem);
}
private static ErrorLocationItem MapErrorLocationItem(string errorLocationLine)
{
ErrorLocationItem item = null;
Match lineMatch = _errorLocationLineRegex.Match(errorLocationLine);
if (lineMatch.Success)
{
GroupCollection lineGroups = lineMatch.Groups;
item = new ErrorLocationItem
{
FunctionName = lineGroups["functionName"].Value,
DocumentName = lineGroups["documentName"].Value,
LineNumber = int.Parse(lineGroups["lineNumber"].Value),
ColumnNumber = lineGroups["columnNumber"].Success ?
int.Parse(lineGroups["columnNumber"].Value) : 0
};
}
return item;
}
/// <summary>
/// Fixes a error location items
/// </summary>
/// <param name="errorLocationItems">An array of <see cref="ErrorLocationItem"/> instances</param>
public static void FixErrorLocationItems(ErrorLocationItem[] errorLocationItems)
{
foreach (ErrorLocationItem errorLocationItem in errorLocationItems)
{
string functionName = errorLocationItem.FunctionName;
if (functionName.Length == 0 || functionName == DelegateFunctionName)
{
errorLocationItem.FunctionName = "Global code";
}
else if (functionName == OriginalAnonymousFunctionName)
{
errorLocationItem.FunctionName = WrapperAnonymousFunctionName;
}
}
}
/// <summary>
/// Converts a call chain to stack
/// </summary>
/// <param name="callChain">Call chain</param>
/// <returns>Call stack</returns>
public static string ConvertCallChainToStack(string callChain)
{
string callStack = string.Empty;
string[] callChainItems = callChain
.Split(_callChainSeparator, StringSplitOptions.None)
;
if (callChainItems.Length > 0)
{
var stringBuilderPool = StringBuilderPool.Shared;
StringBuilder stackBuilder = stringBuilderPool.Rent();
for (int chainItemIndex = callChainItems.Length - 1; chainItemIndex >= 0; chainItemIndex--)
{
string chainItem = callChainItems[chainItemIndex];
if (chainItem == OriginalAnonymousFunctionName)
{
chainItem = WrapperAnonymousFunctionName;
}
JsErrorHelpers.WriteErrorLocationLine(stackBuilder, chainItem, string.Empty, 0, 0);
if (chainItemIndex > 0)
{
stackBuilder.AppendLine();
}
}
callStack = stackBuilder.ToString();
stringBuilderPool.Return(stackBuilder);
}
return callStack;
}
/// <summary>
/// Gets a description from the syntax error message
/// </summary>
/// <param name="message">Error message</param>
/// <returns>Description of error</returns>
public static string GetDescriptionFromSyntaxErrorMessage(string message)
{
Match messageMatch = _syntaxErrorMessageRegex.Match(message);
string description = messageMatch.Success ?
messageMatch.Groups["description"].Value : message;
return description;
}
/// <summary>
/// Parses a script preparation error message
/// </summary>
/// <param name="message">Error message</param>
/// <param name="description">Description of error</param>
/// <param name="errorLocation">Script error location</param>
public static void ParseScriptPreparationErrorMessage(string message, out string description,
out ErrorLocationItem errorLocation)
{
Match messageMatch = _scriptPreparationErrorMessageRegex.Match(message);
if (messageMatch.Success)
{
GroupCollection messageGroups = messageMatch.Groups;
description = messageGroups["description"].Value;
errorLocation = new ErrorLocationItem
{
DocumentName = messageGroups["documentName"].Value,
LineNumber = int.Parse(messageGroups["lineNumber"].Value),
ColumnNumber = int.Parse(messageGroups["columnNumber"].Value)
};
}
else
{
description = message;
errorLocation = new ErrorLocationItem();
}
}
#endregion
}
}