More type support, allow inheriting types to be assigned to a function
This commit is contained in:
@@ -17,6 +17,6 @@ namespace Upsilon.BoundTypes
|
||||
}
|
||||
|
||||
public TypeContainer ScriptType { get; }
|
||||
public System.Type[] ValidInternalTypes { get; }
|
||||
public System.Type[] ValidInternalTypes { get; protected set; }
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ namespace Upsilon.BoundTypes
|
||||
{
|
||||
dic.TryAdd("string", new BoundTypeDefinition(Type.String, typeof(string)));
|
||||
dic.TryAdd("number", new BoundTypeDefinition(Type.Number,
|
||||
new[] {typeof(int), typeof(long), typeof(float), typeof(double)}));
|
||||
new[] {typeof(int), typeof(long), typeof(float), typeof(double), typeof(byte)}));
|
||||
dic.TryAdd("bool", new BoundTypeDefinition(Type.Boolean, typeof(bool)));
|
||||
dic.TryAdd("table", new BoundTypeDefinition(Type.Table, typeof(IEnumerator)));
|
||||
dic.TryAdd("function", new BoundTypeDefinition(Type.Function, new System.Type[0]));
|
||||
|
||||
@@ -20,6 +20,30 @@ namespace Upsilon.BoundTypes
|
||||
: base(new TypeContainer(name), backingType)
|
||||
{
|
||||
Name = backingType.Name;
|
||||
ValidInternalTypes = ValidInternalTypes.Concat(GetParentTypes(backingType)).ToArray();
|
||||
}
|
||||
|
||||
private static IEnumerable<System.Type> GetParentTypes(System.Type type)
|
||||
{
|
||||
// is there any base type?
|
||||
if (type == null)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
|
||||
// return all implemented or inherited interfaces
|
||||
foreach (var i in type.GetInterfaces())
|
||||
{
|
||||
yield return i;
|
||||
}
|
||||
|
||||
// return all inherited types
|
||||
var currentBaseType = type.BaseType;
|
||||
while (currentBaseType != null)
|
||||
{
|
||||
yield return currentBaseType;
|
||||
currentBaseType = currentBaseType.BaseType;
|
||||
}
|
||||
}
|
||||
|
||||
public UserDataBoundTypeDefinition(string name, Dictionary<string, UserDataBoundProperty> backingType)
|
||||
@@ -196,14 +220,16 @@ namespace Upsilon.BoundTypes
|
||||
{
|
||||
ResultType = resultType;
|
||||
Parameters = parameters;
|
||||
NonOptionalParameterCount = Parameters.Count(x => !x.IsOptional);
|
||||
}
|
||||
|
||||
public TypeContainer ResultType { get; set; }
|
||||
public UserDataBoundFunctionParameter[] Parameters { get; set; }
|
||||
public TypeContainer ResultType { get; }
|
||||
public UserDataBoundFunctionParameter[] Parameters { get; }
|
||||
private int NonOptionalParameterCount { get; }
|
||||
|
||||
public bool ValidateParameters(ImmutableArray<BoundExpression> callingParameters)
|
||||
{
|
||||
if (callingParameters.Length < Parameters.Count(x => !x.IsOptional)
|
||||
if (callingParameters.Length < NonOptionalParameterCount
|
||||
|| callingParameters.Length > Parameters.Length)
|
||||
{
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user