diff --git a/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs b/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs index ad32e54..f3e93ea 100644 --- a/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs +++ b/Upsilon/BaseTypes/ScriptFunction/ScriptRuntimeFunction.cs @@ -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); } diff --git a/Upsilon/Evaluator/Evaluator.cs b/Upsilon/Evaluator/Evaluator.cs index f291707..efa004a 100644 --- a/Upsilon/Evaluator/Evaluator.cs +++ b/Upsilon/Evaluator/Evaluator.cs @@ -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; } diff --git a/UpsilonTests/GeneralTests/UserDataTests.cs b/UpsilonTests/GeneralTests/UserDataTests.cs index 211816c..200b00b 100644 --- a/UpsilonTests/GeneralTests/UserDataTests.cs +++ b/UpsilonTests/GeneralTests/UserDataTests.cs @@ -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(input, "test", new[] {obj}, Options)); + } + + + public UserDataTests(StaticScriptFixture fix) : base(fix) { }