Skip to content

Commit 43a7c5d

Browse files
committed
Now during debugging in ActiveScript modes the script error contains a error location
1 parent fc55399 commit 43a7c5d

File tree

7 files changed

+88
-11
lines changed

7 files changed

+88
-11
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
<releaseNotes>1. Added support of .NET Core 1.0.4;
1616
2. In JsRT modes now script error contains a full stack trace;
1717
3. In `MsieJsEngine` class was added overloaded versions of the `Evaluate`, `Evaluate&lt;T&gt;` and `Execute` methods, which take the document name as second parameter;
18-
4. Now all modes support the possibility to debug in Visual Studio by adding the `debugger` statement to script code.</releaseNotes>
18+
4. Now all modes support the possibility to debug in Visual Studio by adding the `debugger` statement to script code;
19+
5. Now during debugging in ActiveScript modes the script error contains a error location.</releaseNotes>
1920
<copyright>Copyright (c) 2012-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2021
<language>en-US</language>
2122
<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
@@ -27,7 +27,9 @@
2727
`Evaluate<T>` and `Execute` methods, which take the document name as second
2828
parameter;
2929
4. Now all modes support the possibility to debug in Visual Studio by adding the
30-
`debugger` statement to script code.
30+
`debugger` statement to script code;
31+
5. Now during debugging in ActiveScript modes the script error contains a error
32+
location.
3133

3234
============
3335
PROJECT SITE

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptException.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,8 @@ private ActiveScriptException(SerializationInfo info, StreamingContext context)
149149
}
150150

151151

152-
internal static ActiveScriptException Create(IActiveScriptError error)
152+
internal static ActiveScriptException Create(string message, IActiveScriptError error)
153153
{
154-
string message = string.Empty;
155154
int errorCode = 0;
156155
short errorWCode = 0;
157156
uint sourceContext = 0;
@@ -186,7 +185,6 @@ internal static ActiveScriptException Create(IActiveScriptError error)
186185
EXCEPINFO excepInfo;
187186
error.GetExceptionInfo(out excepInfo);
188187

189-
message = excepInfo.bstrDescription;
190188
subcategory = excepInfo.bstrSource;
191189
errorCode = excepInfo.scode;
192190
errorWCode = excepInfo.wCode;

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.ScriptSite.cs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,56 @@ public ScriptSite(ActiveScriptJsEngineBase jsEngine)
3434
_jsEngine = jsEngine;
3535
}
3636

37+
/// <summary>
38+
/// Gets a error details
39+
/// </summary>
40+
/// <param name="error">Instance of Active Script error</param>
41+
/// <returns>Error details</returns>
42+
private string GetErrorDetails(IActiveScriptError error)
43+
{
44+
EXCEPINFO excepInfo;
45+
error.GetExceptionInfo(out excepInfo);
46+
47+
string errorDetails = excepInfo.bstrDescription;
48+
if (_jsEngine._processDebugManagerWrapper != null)
49+
{
50+
string errorLocation = GetErrorLocation(error);
51+
if (!string.IsNullOrWhiteSpace(errorLocation))
52+
{
53+
errorDetails += Environment.NewLine + errorLocation;
54+
}
55+
}
56+
57+
return errorDetails;
58+
}
59+
60+
/// <summary>
61+
/// Gets a error location
62+
/// </summary>
63+
/// <param name="error">Instance of Active Script error</param>
64+
/// <returns>Error location</returns>
65+
private string GetErrorLocation(IActiveScriptError error)
66+
{
67+
string errorLocation = string.Empty;
68+
uint sourceContext;
69+
uint lineNumber;
70+
int columnNumber;
71+
72+
error.GetSourcePosition(out sourceContext, out lineNumber, out columnNumber);
73+
++lineNumber;
74+
++columnNumber;
75+
76+
DebugDocument document;
77+
if (_jsEngine._debugDocuments.TryGetValue(new UIntPtr(sourceContext), out document))
78+
{
79+
string documentName;
80+
document.GetName(DocumentNameType.Title, out documentName);
81+
82+
errorLocation = string.Format(" at ({0}:{1}:{2})", documentName, lineNumber, columnNumber);
83+
}
84+
85+
return errorLocation;
86+
}
3787

3888
#region IActiveScriptSite implementation
3989

@@ -75,7 +125,7 @@ public void OnStateChange(ScriptState state)
75125

76126
public void OnScriptError(IActiveScriptError error)
77127
{
78-
_jsEngine._lastException = ActiveScriptException.Create(error);
128+
_jsEngine._lastException = ActiveScriptException.Create(GetErrorDetails(error), error);
79129
}
80130

81131
public void OnEnterScript()
@@ -96,9 +146,10 @@ public void GetRootApplicationNode(out IDebugApplicationNode node)
96146
public void OnScriptErrorDebug(IActiveScriptErrorDebug errorDebug, out bool enterDebugger,
97147
out bool callOnScriptErrorWhenContinuing)
98148
{
99-
if (errorDebug != null)
149+
var error = errorDebug as IActiveScriptError;
150+
if (error != null)
100151
{
101-
_jsEngine._lastException = ActiveScriptException.Create((IActiveScriptError)errorDebug);
152+
_jsEngine._lastException = ActiveScriptException.Create(GetErrorDetails(error), error);
102153
}
103154

104155
enterDebugger = true;

src/MsieJavaScriptEngine/ActiveScript/Debugging/DebugDocument.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public void GetPositionOfLine(uint lineNumber, out uint position)
203203
position = 0;
204204
int lineCount = _lines.Count;
205205

206-
if (lineNumber == 0 || lineNumber >= lineCount)
206+
if (lineNumber == 0 || lineNumber > lineCount)
207207
{
208208
throw new ArgumentOutOfRangeException("lineNumber");
209209
}

src/MsieJavaScriptEngine/MsieJsEngine.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ public void ExecuteFile(string path, Encoding encoding = null)
349349
{
350350
VerifyNotDisposed();
351351

352+
if (path == null)
353+
{
354+
throw new ArgumentNullException(
355+
"path", string.Format(CommonStrings.Common_ArgumentIsNull, "path"));
356+
}
357+
352358
if (string.IsNullOrWhiteSpace(path))
353359
{
354360
throw new ArgumentException(
@@ -392,7 +398,11 @@ public void ExecuteResource(string resourceName, Type type)
392398
string.Format(CommonStrings.Common_ArgumentIsEmpty, "resourceName"), "resourceName");
393399
}
394400

395-
string code = Utils.GetResourceAsString(resourceName, type);
401+
Assembly assembly = type.GetTypeInfo().Assembly;
402+
string nameSpace = type.Namespace;
403+
string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName;
404+
405+
string code = Utils.GetResourceAsString(resourceFullName, assembly);
396406
Execute(code, resourceName);
397407
}
398408

src/MsieJavaScriptEngine/Utilities/UniqueDocumentNameManager.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.IO;
34

5+
using MsieJavaScriptEngine.Resources;
6+
47
namespace MsieJavaScriptEngine.Utilities
58
{
69
/// <summary>
@@ -30,6 +33,18 @@ internal sealed class UniqueDocumentNameManager
3033
/// <param name="defaultName">Default document name</param>
3134
public UniqueDocumentNameManager(string defaultName)
3235
{
36+
if (defaultName == null)
37+
{
38+
throw new ArgumentNullException(
39+
"defaultName", string.Format(CommonStrings.Common_ArgumentIsNull, "defaultName"));
40+
}
41+
42+
if (string.IsNullOrWhiteSpace(defaultName))
43+
{
44+
throw new ArgumentException(
45+
string.Format(CommonStrings.Common_ArgumentIsEmpty, "defaultName"), "defaultName");
46+
}
47+
3348
_defaultName = defaultName;
3449
}
3550

0 commit comments

Comments
 (0)