Skip to content

Commit d70dcac

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore now for error processing are used the JsGetAndClearExceptionWithMetadata method instead of the JsGetAndClearException method
1 parent a7ba5bd commit d70dcac

File tree

7 files changed

+72
-53
lines changed

7 files changed

+72
-53
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.debian-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>An attempt was made to prevent occurrence of the access violation exception.</releaseNotes>
23+
<releaseNotes>Now for error processing are used the `JsGetAndClearExceptionWithMetadata` method instead of the `JsGetAndClearException` method.</releaseNotes>
2424
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2525
<language>en-US</language>
2626
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
An attempt was made to prevent occurrence of the access violation exception.
33+
Now for error processing are used the `JsGetAndClearExceptionWithMetadata`
34+
method instead of the `JsGetAndClearException` method.
3435

3536
=============
3637
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Linq;
55
using System.Reflection;
66
using System.Runtime.InteropServices;
7-
using System.Text.RegularExpressions;
87

98
using OriginalJsException = JavaScriptEngineSwitcher.ChakraCore.JsRt.JsException;
109

@@ -33,13 +32,6 @@ public sealed class ChakraCoreJsEngine : JsEngineBase
3332
/// </summary>
3433
private const string EngineVersion = "1.5.0";
3534

36-
/// <summary>
37-
/// Regular expression for working with the string representation of error
38-
/// </summary>
39-
private static readonly Regex _errorStringRegex =
40-
new Regex(@"[ ]{3,5}at (?:[A-Za-z_\$][0-9A-Za-z_\$ ]* )?" +
41-
@"\([^\s*?""<>|][^\t\n\r*?""<>|]*?:(?<lineNumber>\d+):(?<columnNumber>\d+)\)");
42-
4335
/// <summary>
4436
/// Instance of JS runtime
4537
/// </summary>
@@ -894,7 +886,8 @@ private static JsRuntimeException ConvertJsExceptionToJsRuntimeException(
894886
if (jsScriptException != null)
895887
{
896888
category = "Script error";
897-
JsValue errorValue = jsScriptException.Error;
889+
JsValue metadataValue = jsScriptException.Metadata;
890+
JsValue errorValue = metadataValue.GetProperty("exception");
898891

899892
JsPropertyId stackPropertyId = JsPropertyId.FromString("stack");
900893
if (errorValue.HasProperty(stackPropertyId))
@@ -913,35 +906,23 @@ private static JsRuntimeException ConvertJsExceptionToJsRuntimeException(
913906
}
914907

915908
JsPropertyId linePropertyId = JsPropertyId.FromString("line");
916-
if (errorValue.HasProperty(linePropertyId))
909+
if (metadataValue.HasProperty(linePropertyId))
917910
{
918-
JsValue linePropertyValue = errorValue.GetProperty(linePropertyId);
911+
JsValue linePropertyValue = metadataValue.GetProperty(linePropertyId);
919912
lineNumber = linePropertyValue.ConvertToNumber().ToInt32() + 1;
920913
}
921914

922915
JsPropertyId columnPropertyId = JsPropertyId.FromString("column");
923-
if (errorValue.HasProperty(columnPropertyId))
916+
if (metadataValue.HasProperty(columnPropertyId))
924917
{
925-
JsValue columnPropertyValue = errorValue.GetProperty(columnPropertyId);
918+
JsValue columnPropertyValue = metadataValue.GetProperty(columnPropertyId);
926919
columnNumber = columnPropertyValue.ConvertToNumber().ToInt32() + 1;
927920
}
928921

929-
if (lineNumber <= 0 && columnNumber <= 0)
930-
{
931-
Match errorStringMatch = _errorStringRegex.Match(message);
932-
if (errorStringMatch.Success)
933-
{
934-
GroupCollection errorStringGroups = errorStringMatch.Groups;
935-
936-
lineNumber = int.Parse(errorStringGroups["lineNumber"].Value);
937-
columnNumber = int.Parse(errorStringGroups["columnNumber"].Value);
938-
}
939-
}
940-
941922
JsPropertyId sourcePropertyId = JsPropertyId.FromString("source");
942-
if (errorValue.HasProperty(sourcePropertyId))
923+
if (metadataValue.HasProperty(sourcePropertyId))
943924
{
944-
JsValue sourcePropertyValue = errorValue.GetProperty(sourcePropertyId);
925+
JsValue sourcePropertyValue = metadataValue.GetProperty(sourcePropertyId);
945926
sourceFragment = sourcePropertyValue.ConvertToString().ToString();
946927
}
947928
}

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsContext.cs

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,10 @@ public static ulong SerializeScript(string script, byte[] buffer)
317317
/// <remarks>
318318
/// <para>
319319
/// If the runtime of the current context is not in an exception state, this API will throw
320-
/// <c>JsErrorInvalidArgument</c>. If the runtime is disabled, this will return an exception
321-
/// indicating that the script was terminated, but it will not clear the exception (the
322-
/// exception will be cleared if the runtime is re-enabled using
323-
/// <c>EnableRuntimeExecution</c>).
320+
/// <see cref="JsErrorCode.InvalidArgument"/>. If the runtime is disabled, this will return
321+
/// an exception indicating that the script was terminated, but it will not clear the exception
322+
/// (the exception will be cleared if the runtime is re-enabled using
323+
/// <c>JsEnableRuntimeExecution</c>).
324324
/// </para>
325325
/// <para>
326326
/// Requires an active script context.
@@ -329,10 +329,44 @@ public static ulong SerializeScript(string script, byte[] buffer)
329329
/// <returns>The exception for the runtime of the current context</returns>
330330
public static JsValue GetAndClearException()
331331
{
332-
JsValue reference;
333-
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetAndClearException(out reference));
332+
JsValue exception;
333+
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetAndClearException(out exception));
334334

335-
return reference;
335+
return exception;
336+
}
337+
338+
/// <summary>
339+
/// Returns a metadata relating to the exception that caused the runtime of the current context
340+
/// to be in the exception state and resets the exception state for that runtime. The metadata
341+
/// includes a reference to the exception itself.
342+
/// </summary>
343+
/// <remarks>
344+
/// <para>
345+
/// If the runtime of the current context is not in an exception state, this API will throw
346+
/// <see cref="JsErrorCode.InvalidArgument"/>. If the runtime is disabled, this will return
347+
/// an exception indicating that the script was terminated, but it will not clear the exception
348+
/// (the exception will be cleared if the runtime is re-enabled using
349+
/// <c>JsEnableRuntimeExecution</c>).
350+
/// </para>
351+
/// <para>
352+
/// The metadata value is a javascript object with the following properties: <c>exception</c>, the
353+
/// thrown exception object; <c>line</c>, the 0 indexed line number where the exception was thrown;
354+
/// <c>column</c>, the 0 indexed column number where the exception was thrown; <c>length</c>, the
355+
/// source-length of the cause of the exception; <c>source</c>, a string containing the line of
356+
/// source code where the exception was thrown; and <c>url</c>, a string containing the name of
357+
/// the script file containing the code that threw the exception.
358+
/// </para>
359+
/// <para>
360+
/// Requires an active script context.
361+
/// </para>
362+
/// </remarks>
363+
/// <returns>The exception metadata for the runtime of the current context</returns>
364+
public static JsValue JsGetAndClearExceptionWithMetadata()
365+
{
366+
JsValue metadata;
367+
JsErrorHelpers.ThrowIfError(NativeMethods.JsGetAndClearExceptionWithMetadata(out metadata));
368+
369+
return metadata;
336370
}
337371

338372
/// <summary>

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsErrorHelpers.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,28 @@ public static void ThrowIfError(JsErrorCode error)
117117

118118
case JsErrorCode.ScriptException:
119119
{
120-
JsValue errorObject;
121-
JsErrorCode innerError = NativeMethods.JsGetAndClearException(out errorObject);
120+
JsValue errorMetadata;
121+
JsErrorCode innerError = NativeMethods.JsGetAndClearExceptionWithMetadata(out errorMetadata);
122122

123123
if (innerError != JsErrorCode.NoError)
124124
{
125125
throw new JsFatalException(innerError);
126126
}
127127

128-
throw new JsScriptException(error, errorObject, "Script threw an exception.");
128+
throw new JsScriptException(error, errorMetadata, "Script threw an exception.");
129129
}
130130

131131
case JsErrorCode.ScriptCompile:
132132
{
133-
JsValue errorObject;
134-
JsErrorCode innerError = NativeMethods.JsGetAndClearException(out errorObject);
133+
JsValue errorMetadata;
134+
JsErrorCode innerError = NativeMethods.JsGetAndClearExceptionWithMetadata(out errorMetadata);
135135

136136
if (innerError != JsErrorCode.NoError)
137137
{
138138
throw new JsFatalException(innerError);
139139
}
140140

141-
throw new JsScriptException(error, errorObject, "Compile error.");
141+
throw new JsScriptException(error, errorMetadata, "Compile error.");
142142
}
143143

144144
case JsErrorCode.ScriptTerminated:

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsScriptException.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,42 @@ namespace JavaScriptEngineSwitcher.ChakraCore.JsRt
1414
internal sealed class JsScriptException : JsException
1515
{
1616
/// <summary>
17-
/// The error
17+
/// The error metadata
1818
/// </summary>
1919
#if !NETSTANDARD1_3
2020
[NonSerialized]
2121
#endif
22-
private readonly JsValue _error;
22+
private readonly JsValue _metadata;
2323

2424
/// <summary>
25-
/// Gets a JavaScript object representing the script error
25+
/// Gets a JavaScript object representing the error metadata
2626
/// </summary>
27-
public JsValue Error
27+
public JsValue Metadata
2828
{
29-
get { return _error; }
29+
get { return _metadata; }
3030
}
3131

3232

3333
/// <summary>
3434
/// Initializes a new instance of the <see cref="JsScriptException"/> class
3535
/// </summary>
3636
/// <param name="errorCode">The error code returned</param>
37-
/// <param name="error">The JavaScript error object</param>
38-
public JsScriptException(JsErrorCode errorCode, JsValue error)
39-
: this(errorCode, error, "JavaScript Exception")
37+
/// <param name="metadata">The JavaScript error metadata</param>
38+
public JsScriptException(JsErrorCode errorCode, JsValue metadata)
39+
: this(errorCode, metadata, "JavaScript Exception")
4040
{ }
4141

4242
/// <summary>
4343
/// Initializes a new instance of the <see cref="JsScriptException"/> class
4444
/// with a specified error message
4545
/// </summary>
4646
/// <param name="errorCode">The error code returned</param>
47-
/// <param name="error">The JavaScript error object</param>
47+
/// <param name="metadata">The JavaScript error metadata</param>
4848
/// <param name="message">The error message</param>
49-
public JsScriptException(JsErrorCode errorCode, JsValue error, string message)
49+
public JsScriptException(JsErrorCode errorCode, JsValue metadata, string message)
5050
: base(errorCode, message)
5151
{
52-
_error = error;
52+
_metadata = metadata;
5353
}
5454
#if !NETSTANDARD1_3
5555

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/NativeMethods.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,9 @@ internal static extern JsErrorCode JsCreateNamedFunction(JsValue name, JsNativeF
303303
[DllImport(DllName)]
304304
internal static extern JsErrorCode JsGetAndClearException(out JsValue exception);
305305

306+
[DllImport(DllName)]
307+
internal static extern JsErrorCode JsGetAndClearExceptionWithMetadata(out JsValue metadata);
308+
306309
[DllImport(DllName)]
307310
internal static extern JsErrorCode JsSetException(JsValue exception);
308311

0 commit comments

Comments
 (0)