99
1010using MsieJavaScriptEngine . Extensions ;
1111using MsieJavaScriptEngine . Resources ;
12+ using MsieJavaScriptEngine . Utilities ;
1213
1314namespace MsieJavaScriptEngine . Helpers
1415{
@@ -28,6 +29,33 @@ public static class JsErrorHelpers
2829 @"\((?<documentName>" + CommonRegExps . DocumentNamePattern + @"):" +
2930 @"(?<lineNumber>\d+):(?<columnNumber>\d+)\)$" ) ;
3031
32+
33+ /// <summary>
34+ /// Gets a string representation of the script call stack
35+ /// </summary>
36+ /// <param name="message">Error message with the script call stack</param>
37+ /// <param name="messageWithoutCallStack">Error message without the script call stack</param>
38+ /// <returns>String representation of the script call stack</returns>
39+ internal static string GetCallStackFromMessage ( string message , string messageWithoutCallStack )
40+ {
41+ if ( string . IsNullOrWhiteSpace ( message ) )
42+ {
43+ return string . Empty ;
44+ }
45+
46+ if ( string . IsNullOrWhiteSpace ( messageWithoutCallStack ) )
47+ {
48+ return message ;
49+ }
50+
51+ string callStack = message
52+ . TrimStart ( messageWithoutCallStack )
53+ . TrimStart ( EnvironmentShortcuts . NewLineChars )
54+ ;
55+
56+ return callStack ;
57+ }
58+
3159 /// <summary>
3260 /// Parses a string representation of the script call stack to produce an array of
3361 /// <see cref="CallStackItem"/> instances
@@ -41,26 +69,15 @@ internal static CallStackItem[] ParseCallStack(string callStack)
4169 return [ ] ;
4270 }
4371
44- string [ ] lines = callStack . SplitToLines ( ) ;
72+ string [ ] lines = callStack . SplitToLines ( StringSplitOptions . RemoveEmptyEntries ) ;
4573 int lineCount = lines . Length ;
4674 var callStackItems = new List < CallStackItem > ( lineCount ) ;
4775
48- for ( int lineIndex = 0 ; lineIndex < lineCount ; lineIndex ++ )
76+ foreach ( string line in lines )
4977 {
50- string line = lines [ lineIndex ] ;
51- Match lineMatch = _callStackLineRegex . Match ( line ) ;
52-
53- if ( lineMatch . Success )
78+ CallStackItem callStackItem = MapCallStackItem ( line ) ;
79+ if ( callStackItem is not null )
5480 {
55- GroupCollection lineGroups = lineMatch . Groups ;
56-
57- var callStackItem = new CallStackItem
58- {
59- FunctionName = lineGroups [ "functionName" ] . Value ,
60- DocumentName = lineGroups [ "documentName" ] . Value ,
61- LineNumber = int . Parse ( lineGroups [ "lineNumber" ] . Value ) ,
62- ColumnNumber = int . Parse ( lineGroups [ "columnNumber" ] . Value )
63- } ;
6481 callStackItems . Add ( callStackItem ) ;
6582 }
6683 else
@@ -73,6 +90,26 @@ internal static CallStackItem[] ParseCallStack(string callStack)
7390 return callStackItems . ToArray ( ) ;
7491 }
7592
93+ private static CallStackItem MapCallStackItem ( string callStackLine )
94+ {
95+ CallStackItem item = null ;
96+ Match lineMatch = _callStackLineRegex . Match ( callStackLine ) ;
97+
98+ if ( lineMatch . Success )
99+ {
100+ GroupCollection lineGroups = lineMatch . Groups ;
101+ item = new CallStackItem
102+ {
103+ FunctionName = lineGroups [ "functionName" ] . Value ,
104+ DocumentName = lineGroups [ "documentName" ] . Value ,
105+ LineNumber = int . Parse ( lineGroups [ "lineNumber" ] . Value ) ,
106+ ColumnNumber = int . Parse ( lineGroups [ "columnNumber" ] . Value )
107+ } ;
108+ }
109+
110+ return item ;
111+ }
112+
76113 /// <summary>
77114 /// Produces a string representation of the script call stack from array of
78115 /// <see cref="CallStackItem"/> instances
0 commit comments