Better handling of interop
This commit is contained in:
parent
834d65f38e
commit
6bc3469860
|
@ -49,7 +49,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, EvaluationScope scope, TextSpan span, EvaluationState state)
|
||||
{
|
||||
state.Stacktrace.Push(this);
|
||||
var result = Execute(variables, state.Script, scope, span);
|
||||
var result = Execute(variables, state, scope, span);
|
||||
state.Stacktrace.Pop();
|
||||
if (_directTypeManipulation)
|
||||
return (ScriptType)result;
|
||||
|
@ -61,7 +61,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
TextSpan span , EvaluationState state)
|
||||
{
|
||||
state.Stacktrace.Push(this);
|
||||
var result = Execute(variables, state.Script, scope, span);
|
||||
var result = Execute(variables, state, scope, span);
|
||||
if (result is IEnumerator enumerator)
|
||||
{
|
||||
while (enumerator.MoveNext())
|
||||
|
@ -75,11 +75,11 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
state.Stacktrace.Pop();
|
||||
}
|
||||
|
||||
private object Execute(IEnumerable<ScriptType> variables, Script script, EvaluationScope scope, TextSpan span)
|
||||
private object Execute(IEnumerable<ScriptType> variables, EvaluationState state, EvaluationScope scope, TextSpan span)
|
||||
{
|
||||
var objects = new List<object>();
|
||||
if (_passScriptReference)
|
||||
objects.Add(script);
|
||||
objects.Add(state.Script);
|
||||
if (_passScopeReference)
|
||||
objects.Add(scope);
|
||||
objects.AddRange(variables.Select(x => (object) x).ToList() );
|
||||
|
@ -92,7 +92,8 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
// grab the parameters, and just invoke it
|
||||
var array = objects.ToArray();
|
||||
var methodInfo = Method.GetMethod(ref array);
|
||||
result = methodInfo.Invoke(null, array);
|
||||
result = methodInfo.Invoke(null, BindingFlags.InvokeMethod, UpsilonBinder.Default, array,
|
||||
CultureInfo.InvariantCulture);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -137,7 +138,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
// if we haven't found a method, we throw an exception
|
||||
if (method == null)
|
||||
{
|
||||
throw new EvaluationException(script.FileName,
|
||||
throw new EvaluationException(state.Script.FileName,
|
||||
$"Can't find method {Method.Name} with parameter types {string.Join(", ", convertedTypes.Select(x => x.Name))} on type {_object.GetType().Name}",
|
||||
span);
|
||||
}
|
||||
|
@ -156,8 +157,10 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
// Catch any exception inside the invocation
|
||||
catch (TargetInvocationException e)
|
||||
{
|
||||
if (e.InnerException != null) throw e.InnerException;
|
||||
throw;
|
||||
Exception exception = e;
|
||||
if (e.InnerException != null) exception = e.InnerException;
|
||||
throw new EvaluationException(state.Script.FileName,
|
||||
"An Exception occured while executing a C# function:\n" + exception, span, state.Stacktrace);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -100,9 +100,9 @@ namespace Upsilon.BaseTypes.UserData
|
|||
return true;
|
||||
}
|
||||
|
||||
public override object ChangeType(object value, System.Type type, CultureInfo culture)
|
||||
public override object ChangeType(object value, System.Type requiredType, CultureInfo culture)
|
||||
{
|
||||
if (type.IsInstanceOfType(value))
|
||||
if (requiredType.IsInstanceOfType(value))
|
||||
{
|
||||
return value;
|
||||
}
|
||||
|
@ -114,7 +114,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
return null;
|
||||
}
|
||||
|
||||
var typeCode = System.Type.GetTypeCode(type);
|
||||
var typeCode = System.Type.GetTypeCode(requiredType);
|
||||
switch (typeCode)
|
||||
{
|
||||
case TypeCode.Boolean:
|
||||
|
@ -166,7 +166,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
|
||||
var valueType = value.GetType();
|
||||
if (type == typeof(IIterable))
|
||||
if (requiredType == typeof(IIterable))
|
||||
{
|
||||
if (typeof(IDictionary).IsAssignableFrom(valueType))
|
||||
{
|
||||
|
@ -178,13 +178,13 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
}
|
||||
|
||||
if (typeof(IDictionary).IsAssignableFrom(type))
|
||||
if (typeof(IDictionary).IsAssignableFrom(requiredType))
|
||||
{
|
||||
if (value is DictionaryUserData d)
|
||||
return d.Dictionary;
|
||||
if (value is ScriptTable.ScriptTable table)
|
||||
{
|
||||
var generics = type.GetGenericArguments();
|
||||
var generics = requiredType.GetGenericArguments();
|
||||
var d1 = typeof(Dictionary<,>);
|
||||
var requiredDictionaryType = d1.MakeGenericType(typeof(string), generics[1]);
|
||||
var dictionary = (IDictionary)Activator.CreateInstance(requiredDictionaryType);
|
||||
|
@ -195,14 +195,14 @@ namespace Upsilon.BaseTypes.UserData
|
|||
return dictionary;
|
||||
}
|
||||
}
|
||||
if (typeof(IList).IsAssignableFrom(type))
|
||||
if (typeof(IList).IsAssignableFrom(requiredType))
|
||||
{
|
||||
if (value is ListUserData d)
|
||||
{
|
||||
if (d.List.GetType() == type)
|
||||
if (d.List.GetType() == requiredType)
|
||||
return d.List;
|
||||
|
||||
var generics = type.GetGenericArguments();
|
||||
var generics = requiredType.GetGenericArguments();
|
||||
var l1 = typeof(List<>);
|
||||
var requiredListType = l1.MakeGenericType(generics[0]);
|
||||
var list = (IList)Activator.CreateInstance(requiredListType);
|
||||
|
@ -214,7 +214,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
if (value is ScriptTable.ScriptTable table)
|
||||
{
|
||||
var generics = type.GetGenericArguments();
|
||||
var generics = requiredType.GetGenericArguments();
|
||||
if (generics.Length > 0)
|
||||
{
|
||||
var l1 = typeof(List<>);
|
||||
|
@ -228,7 +228,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
else
|
||||
{
|
||||
var elementType = type.GetElementType();
|
||||
var elementType = requiredType.GetElementType();
|
||||
var input = table.EvaluationScope.Variables.Select(x => ChangeType(x.Value, elementType, culture)).ToArray();
|
||||
var array = Array.CreateInstance(elementType, input.Length);
|
||||
input.CopyTo(array, 0);
|
||||
|
@ -237,13 +237,23 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
}
|
||||
|
||||
if (typeof(IEnumerable).IsAssignableFrom(type))
|
||||
if (typeof(IEnumerable).IsAssignableFrom(requiredType))
|
||||
{
|
||||
if (value is ListUserData d)
|
||||
return d.List;
|
||||
{
|
||||
var generics = requiredType.GetGenericArguments();
|
||||
var l1 = typeof(List<>);
|
||||
var requiredListType = l1.MakeGenericType(generics[0]);
|
||||
var list = (IList)Activator.CreateInstance(requiredListType);
|
||||
foreach (var variable in d.List)
|
||||
{
|
||||
list.Add(variable);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
if (value is ScriptTable.ScriptTable table)
|
||||
{
|
||||
var generics = type.GetGenericArguments();
|
||||
var generics = requiredType.GetGenericArguments();
|
||||
var l1 = typeof(List<>);
|
||||
var requiredListType = l1.MakeGenericType(generics[0]);
|
||||
var list = (IList)Activator.CreateInstance(requiredListType);
|
||||
|
@ -255,7 +265,7 @@ namespace Upsilon.BaseTypes.UserData
|
|||
}
|
||||
}
|
||||
|
||||
var isScriptTypeRequired = typeof(ScriptType).IsAssignableFrom(type);
|
||||
var isScriptTypeRequired = typeof(ScriptType).IsAssignableFrom(requiredType);
|
||||
var isScriptType = value is ScriptType;
|
||||
if (!isScriptTypeRequired && isScriptType)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue