Handle exceptions in userdata functions gracefully, instead of crashing the whole stack down.

This commit is contained in:
Deukhoofd 2019-09-28 13:09:33 +02:00
parent 30627a650f
commit f8193d2925
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
6 changed files with 56 additions and 6 deletions

View File

@ -1,6 +1,7 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using PorygonSharp.UserData;
using PorygonSharp.Utilities;
namespace PorygonSharp.EvalValues
@ -32,8 +33,15 @@ namespace PorygonSharp.EvalValues
parameters[i] = Caster.Cast(val, parametersTypes[i]);
}
var result = _delegate.DynamicInvoke(parameters);
return EvalValueCreator.CreateValue(result).GetPointer();
try
{
var result = _delegate.DynamicInvoke(parameters);
return new UserDataReturnValue(EvalValueCreator.CreateValue(result)).GetPointer();
}
catch (Exception e)
{
return new UserDataReturnValue(e.ToString()).GetPointer();
}
}
}
}

View File

@ -105,7 +105,7 @@ namespace PorygonSharp.EvalValues
return Handle;
}
public bool IsNumericValueFloat()
private bool IsNumericValueFloat()
{
return IsNumericValueFloat(Handle) == 1;
}

View File

@ -42,8 +42,15 @@ namespace PorygonSharp.UserData
}
var parentObj = GCHandle.FromIntPtr(parent).Target;
var result = method.Invoke(parentObj, evaluatedParameters);
return EvalValueCreator.CreateValue(result).GetPointer();
try
{
var result = method.Invoke(parentObj, evaluatedParameters);
return new UserDataReturnValue(EvalValueCreator.CreateValue(result)).GetPointer();
}
catch (Exception e)
{
return new UserDataReturnValue(e.ToString()).GetPointer();
}
};
}

View File

@ -0,0 +1,35 @@
using System;
using System.Runtime.InteropServices;
using PorygonSharp.EvalValues;
namespace PorygonSharp.UserData
{
internal class UserDataReturnValue
{
private readonly IntPtr _ptr;
public UserDataReturnValue(EvalValue value)
{
_ptr = ReturnValue(value.GetPointer());
}
public UserDataReturnValue(string message)
{
_ptr = ErrorValue(message);
}
public static implicit operator UserDataReturnValue(EvalValue val)
{
return new UserDataReturnValue(val);
}
internal IntPtr GetPointer()
{
return _ptr;
}
[DllImport("PorygonLang", EntryPoint = "ReturnValue", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ReturnValue(IntPtr value);
[DllImport("PorygonLang", EntryPoint = "ErrorValue", CallingConvention = CallingConvention.Cdecl)]
private static extern IntPtr ErrorValue(string message);
}
}

Binary file not shown.

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>