Skip to content

Commit

Permalink
Fix exceptions handling
Browse files Browse the repository at this point in the history
  • Loading branch information
nilproject committed Nov 5, 2023
1 parent f2ea242 commit 1f937fe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
14 changes: 13 additions & 1 deletion NiL.JS/Core/GlobalContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using System.Dynamic;
using System.Threading.Tasks;
using NiL.JS.Backward;
using System.Runtime.ExceptionServices;

namespace NiL.JS.Core
{
Expand Down Expand Up @@ -511,7 +512,18 @@ public JSValue ProxyValue(object value)
Task<JSValue> result;
if (Tools.IsTaskOfT(value.GetType()))
{
result = new Task<JSValue>(() => ProxyValue(value.GetType().GetMethod("get_Result", Type.EmptyTypes).Invoke(value, null)));
result = new Task<JSValue>(() =>
{
try
{
return ProxyValue(value.GetType().GetMethod("get_Result", Type.EmptyTypes).Invoke(value, null));
}
catch (TargetInvocationException e)
{
ExceptionDispatchInfo.Capture(e.InnerException).Throw();
throw;
}
});
}
else
{
Expand Down
7 changes: 5 additions & 2 deletions NiL.JS/ExceptionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using NiL.JS.Statements;
using System.Collections;
using System.Runtime.ExceptionServices;
using NiL.JS.Core.Functions;

namespace NiL.JS
{
Expand All @@ -29,6 +30,7 @@ internal static class ExceptionHelper
typeof(ExceptionHelper),
typeof(ConstructorInfo),
typeof(RuntimeMethodHandle),
typeof(AsyncFunction.Сontinuator),

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level

Check failure on line 33 in NiL.JS/ExceptionHelper.cs

View workflow job for this annotation

GitHub Actions / Tests

'AsyncFunction.Сontinuator' is inaccessible due to its protection level
};

internal sealed class StackTraceState
Expand Down Expand Up @@ -58,8 +60,9 @@ public string ToString(JSException jSException)
{
StackFrame frame = frames[i];
var method = frame.GetMethod();
if (method != null
&& method.GetCustomAttribute(typeof(StackFrameOverrideAttribute)) != null)
if (stack is not null
&& method is not null
&& method.GetCustomAttribute(typeof(StackFrameOverrideAttribute)) != null)
{
stackTraceTexts.RemoveRange(stackTraceTexts.Count - recordsToRemove, recordsToRemove);
recordsToRemove = 0;
Expand Down
17 changes: 9 additions & 8 deletions NiL.JS/Extensions/JSValueExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,22 @@ public static T GetDefinedOr<T>(this JSValue self, T defaultValue)

case TypeCode.Object:
{
if (self is null || self.Value is null)
var value = self.Value;
if (self is null || value is null)
return default(T);

if (self.Value is Function && typeof(Delegate).IsAssignableFrom(typeof(T)))
return ((Function)self.Value).MakeDelegate<T>();
if (value is Function && typeof(Delegate).IsAssignableFrom(typeof(T)))
return ((Function)value).MakeDelegate<T>();

if (typeof(T).IsAssignableFrom(self.Value.GetType()))
return (T)self.Value;

if (typeof(T).IsAssignableFrom(self._oValue.GetType()))
if (self._oValue is not null && typeof(T).IsAssignableFrom(self._oValue.GetType()))
return (T)self._oValue;

if (typeof(T).IsAssignableFrom(value.GetType()))
return (T)value;

try
{
return (T)(Tools.ConvertJStoObj(self, typeof(T), true) ?? self.Value);
return (T)(Tools.ConvertJStoObj(self, typeof(T), true) ?? value);
}
catch (InvalidCastException)
{
Expand Down

0 comments on commit 1f937fe

Please sign in to comment.