Support for generic Actions

This commit is contained in:
Deukhoofd 2018-12-06 15:45:19 +01:00
parent 7996420ee5
commit 81cd22ce28
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
4 changed files with 30 additions and 37 deletions

View File

@ -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();
}
}
}

View File

@ -50,7 +50,15 @@ namespace Upsilon.BaseTypes
if (generic.FullName != null && generic.FullName.Contains("System.Func")) if (generic.FullName != null && generic.FullName.Contains("System.Func"))
{ {
var function = (Delegate)o; var function = (Delegate)o;
var method = new UserDataMethod(function.GetMethodInfo()); var method = new UserDataMethod(function.GetMethodInfo());
return new ScriptMethodInfoFunction(method, null, 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, return new ScriptMethodInfoFunction(method, null, false,
false, false); false, false);
} }

View File

@ -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];
} }
} }
} }

View File

@ -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);
@ -103,7 +107,7 @@ namespace Upsilon.StandardLibraries
private static VariableSymbol BuildFunctionVariableSymbol(string name, System.Type type) private static VariableSymbol BuildFunctionVariableSymbol(string name, System.Type type)
{ {
var genericParameters = type.GetGenericArguments(); var genericParameters = type.GetGenericArguments();
var parameters = new List<InternalFunctionVariableSymbol.InternalFunctionParameter>(); var parameters = new List<InternalFunctionVariableSymbol.InternalFunctionParameter>();
for (var i = 0; i < genericParameters.Length - 1; i++) for (var i = 0; i < genericParameters.Length - 1; i++)
{ {
var t = genericParameters[i].GetScriptType(); var t = genericParameters[i].GetScriptType();
@ -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))