Support for generic Actions
This commit is contained in:
parent
7996420ee5
commit
81cd22ce28
|
@ -1,31 +0,0 @@
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Reflection;
|
|
||||||
using Upsilon.Evaluator;
|
|
||||||
|
|
||||||
namespace Upsilon.BaseTypes.ScriptFunction
|
|
||||||
{
|
|
||||||
public class ScriptGenericFunction : ScriptFunction
|
|
||||||
{
|
|
||||||
public ScriptGenericFunction(Func<object, IEnumerable<object>> function)
|
|
||||||
{
|
|
||||||
Function = function;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ScriptGenericFunction(object o)
|
|
||||||
{
|
|
||||||
var type = o.GetType();
|
|
||||||
var generics = type.GetGenericArguments();
|
|
||||||
Function.GetMethodInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Func<object, IEnumerable<object>> Function { get; }
|
|
||||||
|
|
||||||
public override ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script,
|
|
||||||
EvaluationScope scope)
|
|
||||||
{
|
|
||||||
return Function.Invoke(variables.Select(x => x.ToCSharpObject())).ToScriptType();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -54,6 +54,14 @@ namespace Upsilon.BaseTypes
|
||||||
return new ScriptMethodInfoFunction(method, null, false,
|
return new ScriptMethodInfoFunction(method, null, false,
|
||||||
false, false);
|
false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (generic.FullName != null && generic.FullName.Contains("System.Action"))
|
||||||
|
{
|
||||||
|
var function = (Delegate)o;
|
||||||
|
var method = new UserDataMethod(function.GetMethodInfo());
|
||||||
|
return new ScriptMethodInfoFunction(method, null, false,
|
||||||
|
false, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return new GenericUserData(o);
|
return new GenericUserData(o);
|
||||||
|
|
|
@ -6,12 +6,12 @@ namespace Upsilon.BaseTypes.UserData
|
||||||
{
|
{
|
||||||
public static class UserDataTypeHandler
|
public static class UserDataTypeHandler
|
||||||
{
|
{
|
||||||
private static readonly Dictionary<System.Type, UserDataType> _types = new Dictionary<System.Type, UserDataType>();
|
private static readonly Dictionary<System.Type, UserDataType> Types = new Dictionary<System.Type, UserDataType>();
|
||||||
|
|
||||||
public static void LoadType(System.Type t)
|
public static void LoadType(System.Type t)
|
||||||
{
|
{
|
||||||
var info = new UserDataType(t);
|
var info = new UserDataType(t);
|
||||||
_types.Add(t, info);
|
Types.Add(t, info);
|
||||||
UserDataBoundTypeDefinition boundType;
|
UserDataBoundTypeDefinition boundType;
|
||||||
if (t.IsEnum)
|
if (t.IsEnum)
|
||||||
{
|
{
|
||||||
|
@ -31,7 +31,7 @@ namespace Upsilon.BaseTypes.UserData
|
||||||
|
|
||||||
internal static UserDataType GetTypeInfo(System.Type t)
|
internal static UserDataType GetTypeInfo(System.Type t)
|
||||||
{
|
{
|
||||||
return _types[t];
|
return Types[t];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -87,10 +87,14 @@ namespace Upsilon.StandardLibraries
|
||||||
if (type.IsGenericType)
|
if (type.IsGenericType)
|
||||||
{
|
{
|
||||||
var generic = type.GetGenericTypeDefinition();
|
var generic = type.GetGenericTypeDefinition();
|
||||||
if (generic.FullName != null && generic.FullName.Contains("System.Func"))
|
if (generic.FullName != null && generic.FullName.StartsWith("System.Func"))
|
||||||
{
|
{
|
||||||
varSymbol = BuildFunctionVariableSymbol(name, type);
|
varSymbol = BuildFunctionVariableSymbol(name, type);
|
||||||
}
|
}
|
||||||
|
else if (generic.FullName != null && generic.FullName.StartsWith("System.Action"))
|
||||||
|
{
|
||||||
|
varSymbol = BuildActionVariableSymbol(name, type);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (varSymbol == null)
|
if (varSymbol == null)
|
||||||
throw new Exception("Unknown type: " + type);
|
throw new Exception("Unknown type: " + type);
|
||||||
|
@ -114,6 +118,18 @@ namespace Upsilon.StandardLibraries
|
||||||
return new InternalFunctionVariableSymbol(name, true, result, parameters.ToArray());
|
return new InternalFunctionVariableSymbol(name, true, result, parameters.ToArray());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static VariableSymbol BuildActionVariableSymbol(string name, System.Type type)
|
||||||
|
{
|
||||||
|
var genericParameters = type.GetGenericArguments();
|
||||||
|
var parameters = new List<InternalFunctionVariableSymbol.InternalFunctionParameter>();
|
||||||
|
for (var i = 0; i < genericParameters.Length; i++)
|
||||||
|
{
|
||||||
|
var t = genericParameters[i].GetScriptType();
|
||||||
|
parameters.Add(new InternalFunctionVariableSymbol.InternalFunctionParameter(t, false));
|
||||||
|
}
|
||||||
|
return new InternalFunctionVariableSymbol(name, true, Type.Nil, parameters.ToArray());
|
||||||
|
}
|
||||||
|
|
||||||
private static Type DeriveValidTypes(System.Type type)
|
private static Type DeriveValidTypes(System.Type type)
|
||||||
{
|
{
|
||||||
if (type == typeof(ScriptString))
|
if (type == typeof(ScriptString))
|
||||||
|
|
Loading…
Reference in New Issue