Skip to content

Commit e21b77d

Browse files
committed
In JsRT modes the compilation error messages now contains a information about the error location
1 parent 48ccbf4 commit e21b77d

File tree

5 files changed

+69
-36
lines changed

5 files changed

+69
-36
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
<description>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript (http://github.com/paulcbetts/SassAndCoffee), Chakra Sample Hosts (http://github.com/panopticoncentral/chakra-host) and jsrt-dotnet (http://github.com/robpaveza/jsrt-dotnet).</description>
1414
<summary>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).</summary>
1515
<releaseNotes>1. In ActiveScript modes now are uses the short names of error categories;
16-
2. In `Classic` mode during debugging now script error contains a full stack trace.</releaseNotes>
16+
2. In `Classic` mode during debugging now script error contains a full stack trace;
17+
3. In JsRT modes the compilation error messages now contains a information about the error location.</releaseNotes>
1718
<copyright>Copyright (c) 2012-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1819
<language>en-US</language>
1920
<tags>JavaScript ECMAScript MSIE IE Edge Chakra</tags>

NuGet/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
=============
2424
1. In ActiveScript modes now are uses the short names of error categories;
2525
2. In `Classic` mode during debugging now script error contains a full stack
26-
trace.
26+
trace;
27+
3. In JsRT modes the compilation error messages now contains a information about
28+
the error location.
2729

2830
============
2931
PROJECT SITE

src/MsieJavaScriptEngine/JsRt/ChakraJsRtJsEngineBase.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Runtime.InteropServices;
55
#endif
6+
using System.Text;
67
using System.Text.RegularExpressions;
78

89
namespace MsieJavaScriptEngine.JsRt
@@ -67,6 +68,39 @@ protected ChakraJsRtJsEngineBase(JsEngineMode engineMode, bool enableDebugging)
6768
}
6869

6970

71+
/// <summary>
72+
/// Generates a error message with location
73+
/// </summary>
74+
/// <param name="category">Error category</param>
75+
/// <param name="message">Error message</param>
76+
/// <param name="lineNumber">Line number</param>
77+
/// <param name="columnNumber">Column number</param>
78+
/// <returns>Error message with location</returns>
79+
protected static string GenerateErrorMessageWithLocation(string category, string message,
80+
int lineNumber, int columnNumber)
81+
{
82+
var messageBuilder = new StringBuilder();
83+
if (!string.IsNullOrWhiteSpace(category))
84+
{
85+
messageBuilder.AppendFormat("{0}: ", category);
86+
}
87+
messageBuilder.Append(message);
88+
if (lineNumber > 0)
89+
{
90+
messageBuilder.AppendLine();
91+
messageBuilder.AppendFormat(" at {0}", lineNumber);
92+
if (columnNumber > 0)
93+
{
94+
messageBuilder.AppendFormat(":{0}", columnNumber);
95+
}
96+
}
97+
98+
string errorMessage = messageBuilder.ToString();
99+
messageBuilder.Clear();
100+
101+
return errorMessage;
102+
}
103+
70104
/// <summary>
71105
/// Gets a error coordinates from message
72106
/// </summary>

src/MsieJavaScriptEngine/JsRt/Edge/ChakraEdgeJsRtJsEngine.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -827,22 +827,6 @@ private JsRuntimeException ConvertJsExceptionToJsRuntimeException(
827827

828828
if (errorValue.IsValid)
829829
{
830-
EdgeJsPropertyId stackPropertyId = EdgeJsPropertyId.FromString("stack");
831-
if (errorValue.HasProperty(stackPropertyId))
832-
{
833-
EdgeJsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
834-
message = stackPropertyValue.ConvertToString().ToString();
835-
}
836-
else
837-
{
838-
EdgeJsValue messagePropertyValue = errorValue.GetProperty("message");
839-
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
840-
if (!string.IsNullOrWhiteSpace(scriptMessage))
841-
{
842-
message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
843-
}
844-
}
845-
846830
EdgeJsPropertyId linePropertyId = EdgeJsPropertyId.FromString("line");
847831
if (errorValue.HasProperty(linePropertyId))
848832
{
@@ -857,6 +841,20 @@ private JsRuntimeException ConvertJsExceptionToJsRuntimeException(
857841
columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1;
858842
}
859843

844+
EdgeJsPropertyId stackPropertyId = EdgeJsPropertyId.FromString("stack");
845+
if (errorValue.HasProperty(stackPropertyId))
846+
{
847+
EdgeJsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
848+
message = stackPropertyValue.ConvertToString().ToString();
849+
}
850+
else
851+
{
852+
EdgeJsValue messagePropertyValue = errorValue.GetProperty("message");
853+
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
854+
message = GenerateErrorMessageWithLocation(message.TrimEnd('.'), scriptMessage,
855+
lineNumber, columnNumber);
856+
}
857+
860858
if (lineNumber <= 0 && columnNumber <= 0)
861859
{
862860
GetErrorCoordinatesFromMessage(message, out lineNumber, out columnNumber);

src/MsieJavaScriptEngine/JsRt/Ie/ChakraIeJsRtJsEngine.cs

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -850,34 +850,32 @@ private JsRuntimeException ConvertJsExceptionToJsRuntimeException(JsException js
850850

851851
if (errorValue.IsValid)
852852
{
853-
IeJsPropertyId stackPropertyId = IeJsPropertyId.FromString("stack");
854-
if (errorValue.HasProperty(stackPropertyId))
855-
{
856-
IeJsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
857-
message = stackPropertyValue.ConvertToString().ToString();
858-
}
859-
else
860-
{
861-
IeJsValue messagePropertyValue = errorValue.GetProperty("message");
862-
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
863-
if (!string.IsNullOrWhiteSpace(scriptMessage))
864-
{
865-
message = string.Format("{0}: {1}", message.TrimEnd('.'), scriptMessage);
866-
}
867-
}
868-
869853
IeJsPropertyId linePropertyId = IeJsPropertyId.FromString("line");
870854
if (errorValue.HasProperty(linePropertyId))
871855
{
872856
IeJsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
873-
lineNumber = (int) linePropertyValue.ConvertToNumber().ToDouble() + 1;
857+
lineNumber = (int)linePropertyValue.ConvertToNumber().ToDouble() + 1;
874858
}
875859

876860
IeJsPropertyId columnPropertyId = IeJsPropertyId.FromString("column");
877861
if (errorValue.HasProperty(columnPropertyId))
878862
{
879863
IeJsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
880-
columnNumber = (int) columnPropertyValue.ConvertToNumber().ToDouble() + 1;
864+
columnNumber = (int)columnPropertyValue.ConvertToNumber().ToDouble() + 1;
865+
}
866+
867+
IeJsPropertyId stackPropertyId = IeJsPropertyId.FromString("stack");
868+
if (errorValue.HasProperty(stackPropertyId))
869+
{
870+
IeJsValue stackPropertyValue = errorValue.GetProperty(stackPropertyId);
871+
message = stackPropertyValue.ConvertToString().ToString();
872+
}
873+
else
874+
{
875+
IeJsValue messagePropertyValue = errorValue.GetProperty("message");
876+
string scriptMessage = messagePropertyValue.ConvertToString().ToString();
877+
message = GenerateErrorMessageWithLocation(message.TrimEnd('.'), scriptMessage,
878+
lineNumber, columnNumber);
881879
}
882880

883881
if (lineNumber <= 0 && columnNumber <= 0)

0 commit comments

Comments
 (0)