Fixes for function execution when parameter is not bound
This commit is contained in:
parent
3c0e5f5b13
commit
921abce011
|
@ -33,6 +33,10 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
|
||||
public ScriptRuntimeFunctionOption GetValidOption(object[] variables)
|
||||
{
|
||||
if (variables == null)
|
||||
{
|
||||
return Options.FirstOrDefault(x => x.Parameters.Length == 0);
|
||||
}
|
||||
foreach (var option in Options)
|
||||
{
|
||||
if (option.Parameters.Length != variables.Length)
|
||||
|
@ -43,6 +47,8 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
var parameter = option.Parameters[index];
|
||||
var parameterSymbol = ((UserDataVariableSymbol)parameter.VariableSymbol);
|
||||
var parameterType = variables[index].GetType();
|
||||
if (parameterSymbol.BoundTypeDefinition != null)
|
||||
{
|
||||
var validSymbol =
|
||||
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
|
||||
validType.IsAssignableFrom(parameterType));
|
||||
|
@ -52,6 +58,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isCompatible)
|
||||
continue;
|
||||
return option;
|
||||
|
@ -102,12 +109,15 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
|||
public ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script, EvaluationScope scope, TextSpan span)
|
||||
{
|
||||
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope, script);
|
||||
if (Parameters != null)
|
||||
{
|
||||
for (var i = 0; i < Parameters.Length; i++)
|
||||
{
|
||||
var parameterVariable = Parameters[i];
|
||||
var parameterValue = variables[i];
|
||||
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
|
||||
}
|
||||
}
|
||||
return innerEvaluator.EvaluateNode(Block);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -86,12 +86,12 @@ namespace Upsilon.Evaluator
|
|||
throw new ArgumentException(($"Function '{functionName}' could not be found"));
|
||||
}
|
||||
var function = (ScriptRuntimeFunction) statement;
|
||||
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
|
||||
var option = function.GetValidOption(parameters);
|
||||
if (option == null)
|
||||
throw new EvaluationException(
|
||||
$"No function found with name '{functionName}' and available parameters {string.Join(", ", parameters.Select(x => $"{x.GetType().Name}"))}");
|
||||
var result = option.Run(_diagnostics, parameters.Select(x => x.ToScriptType()).ToArray(), _script, Scope, new TextSpan());
|
||||
|
||||
var result = option.Run(_diagnostics, parameters?.Select(x => x.ToScriptType()).ToArray(), _script, Scope, new TextSpan());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
|
@ -43,6 +43,16 @@ namespace UpsilonTests.GeneralTests
|
|||
return a + b;
|
||||
}
|
||||
|
||||
public bool OverloadMethod(string s)
|
||||
{
|
||||
return s == "test";
|
||||
}
|
||||
|
||||
public int OverloadMethod(int i)
|
||||
{
|
||||
return i + 10;
|
||||
}
|
||||
|
||||
}
|
||||
#pragma warning restore 414, 649
|
||||
|
||||
|
@ -136,6 +146,20 @@ end
|
|||
Assert.False(obj.PrivateSet);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void OverloadMethods()
|
||||
{
|
||||
var obj = new UserDataHelper();
|
||||
const string input = @"
|
||||
function test(o)
|
||||
return o.overloadMethod(""test"") and o.OverloadMethod(10) == 20
|
||||
end
|
||||
";
|
||||
Assert.True(Executor.EvaluateFunction<bool>(input, "test", new[] {obj}, Options));
|
||||
}
|
||||
|
||||
|
||||
|
||||
public UserDataTests(StaticScriptFixture fix) : base(fix)
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue