Allow non-standard libraries to have a script pass variable as well
This commit is contained in:
parent
80afeacda6
commit
59909b0d53
|
@ -63,7 +63,8 @@ namespace Upsilon.BoundTypes
|
|||
Properties = new Dictionary<string, UserDataBoundProperty>(),
|
||||
Name = name
|
||||
};
|
||||
var fields = backingType.GetFields(BindingFlags.Public | BindingFlags.Instance).Select(x => new UserDataBoundProperty()
|
||||
const BindingFlags generalBindAttributes = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static;
|
||||
var fields = backingType.GetFields(generalBindAttributes).Select(x => new UserDataBoundProperty()
|
||||
{
|
||||
Name = x.Name,
|
||||
ActualType = x.FieldType.Name,
|
||||
|
@ -79,11 +80,11 @@ namespace Upsilon.BoundTypes
|
|||
{
|
||||
realProperties = new[]{backingType}
|
||||
.Concat(backingType.GetInterfaces())
|
||||
.SelectMany(x => x.GetProperties(BindingFlags.Public | BindingFlags.Instance)).ToArray();
|
||||
.SelectMany(x => x.GetProperties(generalBindAttributes)).ToArray();
|
||||
}
|
||||
else
|
||||
{
|
||||
realProperties = backingType.GetProperties(BindingFlags.Public | BindingFlags.Instance);
|
||||
realProperties = backingType.GetProperties(generalBindAttributes);
|
||||
}
|
||||
var properties = realProperties.Select(x => new UserDataBoundProperty()
|
||||
{
|
||||
|
@ -96,16 +97,18 @@ namespace Upsilon.BoundTypes
|
|||
obj.Properties.Add(f.Name.ToLowerInvariant(), f);
|
||||
}
|
||||
var methods = new Dictionary<string, UserDataBoundMethod>();
|
||||
var backingMethods = backingType.GetMethods(BindingFlags.Public | BindingFlags.Instance);
|
||||
var backingMethods = backingType.GetMethods(generalBindAttributes);
|
||||
foreach (var backingMethod in backingMethods)
|
||||
{
|
||||
if (backingMethod.IsSpecialName)
|
||||
continue;
|
||||
var methodName = backingMethod.Name;
|
||||
var passScriptReference = false;
|
||||
var attribute = backingMethod.GetCustomAttribute(typeof(ScriptFunctionAttribute));
|
||||
if (attribute is ScriptFunctionAttribute sfa )
|
||||
{
|
||||
methodName = sfa.Name;
|
||||
passScriptReference = sfa.PassScriptReference;
|
||||
}
|
||||
|
||||
var parameters = backingMethod.GetParameters().Select(parameter => new UserDataBoundFunctionParameter()
|
||||
|
@ -115,7 +118,7 @@ namespace Upsilon.BoundTypes
|
|||
Type = StaticScope.DeriveValidTypes(parameter.ParameterType),
|
||||
IsOptional = parameter.IsOptional
|
||||
}).ToArray();
|
||||
var option = new UserDataBoundMethodOption(backingMethod.ReturnType.GetScriptType(), parameters);
|
||||
var option = new UserDataBoundMethodOption(backingMethod.ReturnType.GetScriptType(), parameters, passScriptReference);
|
||||
if (methods.TryGetValue(methodName, out var func))
|
||||
{
|
||||
func.Options.Add(option);
|
||||
|
@ -219,29 +222,43 @@ namespace Upsilon.BoundTypes
|
|||
|
||||
public class UserDataBoundMethodOption
|
||||
{
|
||||
public UserDataBoundMethodOption(TypeContainer resultType, UserDataBoundFunctionParameter[] parameters)
|
||||
public UserDataBoundMethodOption(TypeContainer resultType, UserDataBoundFunctionParameter[] parameters,
|
||||
bool passScriptReference)
|
||||
{
|
||||
ResultType = resultType;
|
||||
Parameters = parameters;
|
||||
PassScriptReference = passScriptReference;
|
||||
NonOptionalParameterCount = Parameters.Count(x => !x.IsOptional);
|
||||
}
|
||||
|
||||
public TypeContainer ResultType { get; }
|
||||
public UserDataBoundFunctionParameter[] Parameters { get; }
|
||||
public bool PassScriptReference { get; }
|
||||
private int NonOptionalParameterCount { get; }
|
||||
|
||||
public bool ValidateParameters(ImmutableArray<BoundExpression> callingParameters)
|
||||
{
|
||||
if (callingParameters.Length < NonOptionalParameterCount
|
||||
|| callingParameters.Length > Parameters.Length)
|
||||
var effectiveLength = callingParameters.Length;
|
||||
if (PassScriptReference)
|
||||
effectiveLength++;
|
||||
|
||||
if (effectiveLength < NonOptionalParameterCount
|
||||
|| effectiveLength > Parameters.Length)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var callingIndexOffset = 0;
|
||||
for (var i = 0; i < callingParameters.Length; i++)
|
||||
{
|
||||
var functionParameter = Parameters[i];
|
||||
var callingParameter = callingParameters[i];
|
||||
if (i == 0 && PassScriptReference)
|
||||
{
|
||||
callingIndexOffset++;
|
||||
continue;
|
||||
}
|
||||
|
||||
var callingParameter = callingParameters[i + callingIndexOffset];
|
||||
if (callingParameter.ValueType == Type.Unknown || callingParameter.ValueType == Type.Nil ||
|
||||
functionParameter.Type == Type.Unknown)
|
||||
continue;
|
||||
|
|
Loading…
Reference in New Issue