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)
|
public ScriptRuntimeFunctionOption GetValidOption(object[] variables)
|
||||||
{
|
{
|
||||||
|
if (variables == null)
|
||||||
|
{
|
||||||
|
return Options.FirstOrDefault(x => x.Parameters.Length == 0);
|
||||||
|
}
|
||||||
foreach (var option in Options)
|
foreach (var option in Options)
|
||||||
{
|
{
|
||||||
if (option.Parameters.Length != variables.Length)
|
if (option.Parameters.Length != variables.Length)
|
||||||
|
@ -43,13 +47,16 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
||||||
var parameter = option.Parameters[index];
|
var parameter = option.Parameters[index];
|
||||||
var parameterSymbol = ((UserDataVariableSymbol)parameter.VariableSymbol);
|
var parameterSymbol = ((UserDataVariableSymbol)parameter.VariableSymbol);
|
||||||
var parameterType = variables[index].GetType();
|
var parameterType = variables[index].GetType();
|
||||||
var validSymbol =
|
if (parameterSymbol.BoundTypeDefinition != null)
|
||||||
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
|
|
||||||
validType.IsAssignableFrom(parameterType));
|
|
||||||
if (!validSymbol)
|
|
||||||
{
|
{
|
||||||
isCompatible = false;
|
var validSymbol =
|
||||||
break;
|
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
|
||||||
|
validType.IsAssignableFrom(parameterType));
|
||||||
|
if (!validSymbol)
|
||||||
|
{
|
||||||
|
isCompatible = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!isCompatible)
|
if (!isCompatible)
|
||||||
|
@ -102,11 +109,14 @@ namespace Upsilon.BaseTypes.ScriptFunction
|
||||||
public ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script, EvaluationScope scope, TextSpan span)
|
public ScriptType Run(Diagnostics diagnostics, ScriptType[] variables, Script script, EvaluationScope scope, TextSpan span)
|
||||||
{
|
{
|
||||||
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope, script);
|
var innerEvaluator = new Evaluator.Evaluator(diagnostics, EvaluationScope, script);
|
||||||
for (var i = 0; i < Parameters.Length; i++)
|
if (Parameters != null)
|
||||||
{
|
{
|
||||||
var parameterVariable = Parameters[i];
|
for (var i = 0; i < Parameters.Length; i++)
|
||||||
var parameterValue = variables[i];
|
{
|
||||||
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
|
var parameterVariable = Parameters[i];
|
||||||
|
var parameterValue = variables[i];
|
||||||
|
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return innerEvaluator.EvaluateNode(Block);
|
return innerEvaluator.EvaluateNode(Block);
|
||||||
}
|
}
|
||||||
|
|
|
@ -86,12 +86,12 @@ namespace Upsilon.Evaluator
|
||||||
throw new ArgumentException(($"Function '{functionName}' could not be found"));
|
throw new ArgumentException(($"Function '{functionName}' could not be found"));
|
||||||
}
|
}
|
||||||
var function = (ScriptRuntimeFunction) statement;
|
var function = (ScriptRuntimeFunction) statement;
|
||||||
var innerEvaluator = new Evaluator(_diagnostics, Scope, _script);
|
|
||||||
var option = function.GetValidOption(parameters);
|
var option = function.GetValidOption(parameters);
|
||||||
if (option == null)
|
if (option == null)
|
||||||
throw new EvaluationException(
|
throw new EvaluationException(
|
||||||
$"No function found with name '{functionName}' and available parameters {string.Join(", ", parameters.Select(x => $"{x.GetType().Name}"))}");
|
$"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;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -43,6 +43,16 @@ namespace UpsilonTests.GeneralTests
|
||||||
return a + b;
|
return a + b;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public bool OverloadMethod(string s)
|
||||||
|
{
|
||||||
|
return s == "test";
|
||||||
|
}
|
||||||
|
|
||||||
|
public int OverloadMethod(int i)
|
||||||
|
{
|
||||||
|
return i + 10;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
#pragma warning restore 414, 649
|
#pragma warning restore 414, 649
|
||||||
|
|
||||||
|
@ -136,6 +146,20 @@ end
|
||||||
Assert.False(obj.PrivateSet);
|
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)
|
public UserDataTests(StaticScriptFixture fix) : base(fix)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue