Skip to content

Commit 0dc2e8f

Browse files
committed
Fix exceptions handling
1 parent f2ea242 commit 0dc2e8f

File tree

5 files changed

+29
-13
lines changed

5 files changed

+29
-13
lines changed

NiL.JS/Core/Functions/AsyncFunction.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace NiL.JS.Core.Functions
1010
[Prototype(typeof(Function), true)]
1111
internal sealed class AsyncFunction : Function
1212
{
13-
private sealed class Сontinuator
13+
internal sealed class Сontinuator
1414
{
1515
private readonly AsyncFunction _asyncFunction;
1616
private readonly Context _context;

NiL.JS/Core/GlobalContext.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using System.Dynamic;
1111
using System.Threading.Tasks;
1212
using NiL.JS.Backward;
13+
using System.Runtime.ExceptionServices;
1314

1415
namespace NiL.JS.Core
1516
{
@@ -511,7 +512,18 @@ public JSValue ProxyValue(object value)
511512
Task<JSValue> result;
512513
if (Tools.IsTaskOfT(value.GetType()))
513514
{
514-
result = new Task<JSValue>(() => ProxyValue(value.GetType().GetMethod("get_Result", Type.EmptyTypes).Invoke(value, null)));
515+
result = new Task<JSValue>(() =>
516+
{
517+
try
518+
{
519+
return ProxyValue(value.GetType().GetMethod("get_Result", Type.EmptyTypes).Invoke(value, null));
520+
}
521+
catch (TargetInvocationException e)
522+
{
523+
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
524+
throw;
525+
}
526+
});
515527
}
516528
else
517529
{

NiL.JS/ExceptionHelper.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using NiL.JS.Statements;
1313
using System.Collections;
1414
using System.Runtime.ExceptionServices;
15+
using NiL.JS.Core.Functions;
1516

1617
namespace NiL.JS
1718
{
@@ -29,6 +30,7 @@ internal static class ExceptionHelper
2930
typeof(ExceptionHelper),
3031
typeof(ConstructorInfo),
3132
typeof(RuntimeMethodHandle),
33+
typeof(AsyncFunction.Сontinuator),
3234
};
3335

3436
internal sealed class StackTraceState
@@ -58,8 +60,9 @@ public string ToString(JSException jSException)
5860
{
5961
StackFrame frame = frames[i];
6062
var method = frame.GetMethod();
61-
if (method != null
62-
&& method.GetCustomAttribute(typeof(StackFrameOverrideAttribute)) != null)
63+
if (stack is not null
64+
&& method is not null
65+
&& method.GetCustomAttribute(typeof(StackFrameOverrideAttribute)) != null)
6366
{
6467
stackTraceTexts.RemoveRange(stackTraceTexts.Count - recordsToRemove, recordsToRemove);
6568
recordsToRemove = 0;

NiL.JS/Extensions/JSValueExtensions.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,21 +170,22 @@ public static T GetDefinedOr<T>(this JSValue self, T defaultValue)
170170

171171
case TypeCode.Object:
172172
{
173-
if (self is null || self.Value is null)
173+
var value = self.Value;
174+
if (self is null || value is null)
174175
return default(T);
175176

176-
if (self.Value is Function && typeof(Delegate).IsAssignableFrom(typeof(T)))
177-
return ((Function)self.Value).MakeDelegate<T>();
177+
if (value is Function && typeof(Delegate).IsAssignableFrom(typeof(T)))
178+
return ((Function)value).MakeDelegate<T>();
178179

179-
if (typeof(T).IsAssignableFrom(self.Value.GetType()))
180-
return (T)self.Value;
181-
182-
if (typeof(T).IsAssignableFrom(self._oValue.GetType()))
180+
if (self._oValue is not null && typeof(T).IsAssignableFrom(self._oValue.GetType()))
183181
return (T)self._oValue;
184182

183+
if (typeof(T).IsAssignableFrom(value.GetType()))
184+
return (T)value;
185+
185186
try
186187
{
187-
return (T)(Tools.ConvertJStoObj(self, typeof(T), true) ?? self.Value);
188+
return (T)(Tools.ConvertJStoObj(self, typeof(T), true) ?? value);
188189
}
189190
catch (InvalidCastException)
190191
{

Tests/Core/Functions/AsyncFunctionTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public async Task RejectedPromiseShouldBeReturnedAsFaultedTask()
4343
context.Eval("let result = null; async function testAsync() { result = await testAwaitable('test'); }");
4444

4545
var task = context.GetVariable("testAsync").As<Function>().Call(new Arguments()).As<Promise>().Task;
46-
await Assert.ThrowsExceptionAsync<AggregateException>(async () =>
46+
await Assert.ThrowsExceptionAsync<JSException>(async () =>
4747
{
4848
await task;
4949
});

0 commit comments

Comments
 (0)