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) 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,6 +47,8 @@ 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();
if (parameterSymbol.BoundTypeDefinition != null)
{
var validSymbol = var validSymbol =
parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType => parameterSymbol.BoundTypeDefinition.ValidInternalTypes.Any(validType =>
validType.IsAssignableFrom(parameterType)); validType.IsAssignableFrom(parameterType));
@ -52,6 +58,7 @@ namespace Upsilon.BaseTypes.ScriptFunction
break; break;
} }
} }
}
if (!isCompatible) if (!isCompatible)
continue; continue;
return option; return option;
@ -102,12 +109,15 @@ 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);
if (Parameters != null)
{
for (var i = 0; i < Parameters.Length; i++) for (var i = 0; i < Parameters.Length; i++)
{ {
var parameterVariable = Parameters[i]; var parameterVariable = Parameters[i];
var parameterValue = variables[i]; var parameterValue = variables[i];
innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue); innerEvaluator.Scope.CreateLocal(parameterVariable.VariableSymbol, parameterValue);
} }
}
return innerEvaluator.EvaluateNode(Block); return innerEvaluator.EvaluateNode(Block);
} }
} }

View File

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

View File

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