Skip to content

Commit 3a670ae

Browse files
committed
Now during the rethrowing of exceptions are preserved the full call stack trace
1 parent f7898cb commit 3a670ae

File tree

10 files changed

+65
-13
lines changed

10 files changed

+65
-13
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 a blocking of finalizer's thread.</releaseNotes>
23+
<releaseNotes>Now during the rethrowing of exceptions are preserved the full call stack trace.</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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
An attempt was made to prevent a blocking of finalizer's thread.
33+
Now during the rethrowing of exceptions are preserved the full call stack trace.
3434

3535
=============
3636
DOCUMENTATION

NuGet/JavaScriptEngineSwitcher.Core/JavaScriptEngineSwitcher.Core.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (MSIE JavaScript Engine for .Net, Microsoft ClearScript.V8, Jurassic, Jint and ChakraCore). This library allows you to quickly and easily switch to using of another JavaScript engine.</description>
1414
<summary>JavaScript Engine Switcher determines unified interface for access to the basic features of popular JavaScript engines (MSIE JavaScript Engine for .Net, Microsoft ClearScript.V8, Jurassic, Jint and ChakraCore).</summary>
15-
<releaseNotes>Added support of identifier names compliant with ECMAScript 5.</releaseNotes>
15+
<releaseNotes>Now during the rethrowing of exceptions are preserved the full call stack trace.</releaseNotes>
1616
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1717
<language>en-US</language>
1818
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript</tags>

NuGet/JavaScriptEngineSwitcher.Core/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
=============
2020
RELEASE NOTES
2121
=============
22-
Added support of identifier names compliant with ECMAScript 5.
22+
Now during the rethrowing of exceptions are preserved the full call stack trace.
2323

2424
=============
2525
DOCUMENTATION

NuGet/JavaScriptEngineSwitcher.V8/JavaScriptEngineSwitcher.V8.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ This package does not contain the native ClearScript and V8 assemblies. Therefor
1717
* JavaScriptEngineSwitcher.V8.Native.win-x86
1818
* JavaScriptEngineSwitcher.V8.Native.win-x64</description>
1919
<summary>JavaScriptEngineSwitcher.V8 contains adapter `V8JsEngine` (wrapper for the Microsoft ClearScript.V8 version of May 13, 2017).</summary>
20-
<releaseNotes>Microsoft ClearScript.V8 was updated to version of May 13, 2017.</releaseNotes>
20+
<releaseNotes>Now during the rethrowing of exceptions are preserved the full call stack trace.</releaseNotes>
2121
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2222
<language>en-US</language>
2323
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript V8 ClearScript</tags>

NuGet/JavaScriptEngineSwitcher.V8/readme.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
=============
2626
RELEASE NOTES
2727
=============
28-
Microsoft ClearScript.V8 was updated to version of May 13, 2017.
28+
Now during the rethrowing of exceptions are preserved the full call stack trace.
2929

3030
=============
3131
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ScriptDispatcher.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
#if NETSTANDARD1_3 || NET45
4+
using System.Runtime.ExceptionServices;
5+
#endif
36
using System.Threading;
47

58
using JavaScriptEngineSwitcher.Core;
@@ -152,9 +155,17 @@ private object InnnerInvoke(Func<object> del)
152155
waitHandle.WaitOne();
153156
}
154157

155-
if (task.Exception != null)
158+
Exception exception = task.Exception;
159+
if (exception != null)
156160
{
157-
throw task.Exception;
161+
#if NETSTANDARD1_3 || NET45
162+
ExceptionDispatchInfo.Capture(exception).Throw();
163+
#elif NET40
164+
exception.PreserveStackTrace();
165+
throw exception;
166+
#else
167+
#error No implementation for this target
168+
#endif
158169
}
159170

160171
return task.Result;
@@ -202,7 +213,7 @@ public void Invoke(Action action)
202213
});
203214
}
204215

205-
#region IDisposable implementation
216+
#region IDisposable implementation
206217

207218
/// <summary>
208219
/// Destroys object
@@ -227,9 +238,9 @@ public void Dispose()
227238
}
228239
}
229240

230-
#endregion
241+
#endregion
231242

232-
#region Internal types
243+
#region Internal types
233244

234245
/// <summary>
235246
/// Represents a script task, that must be executed on separate thread
@@ -286,6 +297,6 @@ public ScriptTask(Func<object> del, ManualResetEvent waitHandle)
286297
}
287298
}
288299

289-
#endregion
300+
#endregion
290301
}
291302
}

src/JavaScriptEngineSwitcher.Core.Net4/JavaScriptEngineSwitcher.Core.Net40.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
<Link>Utilities\Utils.cs</Link>
118118
</Compile>
119119
<Compile Include="Utilities\DelegateExtensions.cs" />
120+
<Compile Include="Utilities\ExceptionExtensions.cs" />
120121
<Compile Include="Utilities\TypeInfo.cs" />
121122
</ItemGroup>
122123
<ItemGroup>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#if NET40
2+
using System;
3+
using System.Reflection;
4+
5+
namespace JavaScriptEngineSwitcher.Core.Utilities
6+
{
7+
/// <summary>
8+
/// Exception extensions
9+
/// </summary>
10+
public static class ExceptionExtensions
11+
{
12+
/// <summary>
13+
/// Preserves a stack trace of exception
14+
/// </summary>
15+
/// <param name="source">The exception</param>
16+
public static void PreserveStackTrace(this Exception source)
17+
{
18+
if (source == null)
19+
{
20+
throw new ArgumentNullException("source");
21+
}
22+
23+
MethodInfo preserveStackTraceMethodInfo = typeof(Exception).GetMethod("InternalPreserveStackTrace",
24+
BindingFlags.Instance | BindingFlags.NonPublic);
25+
preserveStackTraceMethodInfo.Invoke(source, null);
26+
}
27+
}
28+
}
29+
#endif

src/JavaScriptEngineSwitcher.V8/V8JsEngine.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Reflection;
3+
#if NET45
4+
using System.Runtime.ExceptionServices;
5+
#endif
36
using System.Text.RegularExpressions;
47

58
using Microsoft.ClearScript.V8;
@@ -40,7 +43,7 @@ public sealed class V8JsEngine : JsEngineBase
4043
private static OriginalUndefined _originalUndefinedValue;
4144

4245
/// <summary>
43-
/// Information about `InvokeMethod` method of `Microsoft.ClearScript.V8.V8ScriptItem` type
46+
/// Information about <code>InvokeMethod</code> method of <see cref="V8ScriptItem"/> class
4447
/// </summary>
4548
private static MethodInfo _v8ScriptItemInvokeMethodInfo;
4649

@@ -340,8 +343,16 @@ protected override object InnerCallFunction(string functionName, params object[]
340343
{
341344
throw ConvertScriptEngineExceptionToJsRuntimeException(scriptEngineException);
342345
}
346+
#if NET45
343347

348+
ExceptionDispatchInfo.Capture(innerException).Throw();
349+
#elif NET40
350+
351+
innerException.PreserveStackTrace();
344352
throw innerException;
353+
#else
354+
#error No implementation for this target
355+
#endif
345356
}
346357

347358
throw;

0 commit comments

Comments
 (0)