Fixes for function execution when parameter is not bound

This commit is contained in:
Deukhoofd 2019-01-20 15:17:06 +01:00
parent 3c0e5f5b13
commit 921abce011
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 46 additions and 12 deletions

View File

@ -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,13 +47,16 @@ namespace Upsilon.BaseTypes.ScriptFunction
var parameter = option.Parameters[index];
var parameterSymbol = ((UserDataVariableSymbol)parameter.VariableSymbol);
var parameterType = variables[index].GetType();
var validSymbol =
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
validType.IsAssignableFrom(parameterType));
if (!validSymbol)
if (parameterSymbol.BoundTypeDefinition != null)
{
isCompatible = false;
break;
var validSymbol =
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
validType.IsAssignableFrom(parameterType));
if (!validSymbol)
{
isCompatible = false;
break;
}
}
}
if (!isCompatible)
@ -102,11 +109,14 @@ 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);
for (var i = 0; i < Parameters.Length; i++)
if (Parameters != null)
{
var parameterVariable = Parameters[i];
var parameterValue = variables[i];
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
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);
}

View File

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

View File

@ -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)
{
}