forked from Taritsyn/JavaScriptEngineSwitcher
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNiLJsErrorHelpers.cs
More file actions
122 lines (105 loc) · 3.69 KB
/
NiLJsErrorHelpers.cs
File metadata and controls
122 lines (105 loc) · 3.69 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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text.RegularExpressions;
using JavaScriptEngineSwitcher.Core.Extensions;
using JavaScriptEngineSwitcher.Core.Helpers;
using CoreStrings = JavaScriptEngineSwitcher.Core.Resources.Strings;
namespace JavaScriptEngineSwitcher.NiL.Helpers
{
/// <summary>
/// JS error helpers
/// </summary>
internal static class NiLJsErrorHelpers
{
#region Error location
private const string AtLinePrefix = " at ";
private const string DotNetStackTraceLinePrefix = AtLinePrefix + "NiL.JS.";
private const string OriginalGlobalCode = "anonymous";
private const string OriginalAnonymousFunctionName = "<anonymous method>";
private const string WrapperGlobalCode = "Global code";
private const string WrapperAnonymousFunctionName = "Anonymous function";
/// <summary>
/// Regular expression for working with line of the script error location
/// </summary>
private static readonly Regex _errorLocationLineRegex =
new Regex(@"^" + AtLinePrefix +
@"(?<functionName>" +
@"[\w][\w ]*" +
@"|" +
CommonRegExps.JsFullNamePattern +
@"|" +
Regex.Escape(OriginalAnonymousFunctionName) +
@")" +
@"(?::line (?<lineNumber>\d+):(?<columnNumber>\d+))?$");
/// <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)
{
if (string.IsNullOrWhiteSpace(errorLocation))
{
return new ErrorLocationItem[0];
}
var errorLocationItems = new List<ErrorLocationItem>();
string[] lines = errorLocation.SplitToLines();
int lineCount = lines.Length;
for (int lineIndex = 0; lineIndex < lineCount; lineIndex++)
{
string line = lines[lineIndex];
if (line.Length == 0)
{
continue;
}
// Completing parsing when a .NET stack trace is found
if (line.StartsWith(DotNetStackTraceLinePrefix, StringComparison.Ordinal))
{
break;
}
Match lineMatch = _errorLocationLineRegex.Match(line);
if (lineMatch.Success)
{
GroupCollection lineGroups = lineMatch.Groups;
Group lineNumberGroup = lineGroups["lineNumber"];
Group columnNumberGroup = lineGroups["columnNumber"];
var errorLocationItem = new ErrorLocationItem
{
FunctionName = lineGroups["functionName"].Value,
LineNumber = lineNumberGroup.Success ? int.Parse(lineNumberGroup.Value) : 0,
ColumnNumber = columnNumberGroup.Success ? int.Parse(columnNumberGroup.Value) : 0,
};
errorLocationItems.Add(errorLocationItem);
}
else
{
Debug.WriteLine(string.Format(CoreStrings.Runtime_InvalidErrorLocationLineFormat, line));
return new ErrorLocationItem[0];
}
}
return errorLocationItems.ToArray();
}
/// <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 == OriginalGlobalCode)
{
errorLocationItem.FunctionName = WrapperGlobalCode;
}
else if (functionName == OriginalAnonymousFunctionName)
{
errorLocationItem.FunctionName = WrapperAnonymousFunctionName;
}
}
}
#endregion
}
}