Browse Source

Better int vs float detection in function overload resolution (#1036)

David 3 years ago
parent
commit
ea1d3991ac

+ 10 - 0
Jint.Tests/Runtime/Domain/OverLoading.cs

@@ -16,5 +16,15 @@ namespace Jint.Tests.Runtime.Domain
         {
         {
             return "uint-enum";
             return "uint-enum";
         }
         }
+
+        public string TestFunc(float f)
+        {
+            return "float-val";
+        }
+
+        public string TestFunc(int i)
+        {
+            return "int-val";
+        }
     }
     }
 }
 }

+ 9 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -2464,6 +2464,15 @@ namespace Jint.Tests.Runtime
             Assert.Equal(1, engine.Evaluate("E.b;").AsNumber());
             Assert.Equal(1, engine.Evaluate("E.b;").AsNumber());
         }
         }
 
 
+        [Fact]
+        public void IntegerAndFloatInFunctionOverloads()
+        {
+            var engine = new Engine(options => options.AllowClr(GetType().Assembly));
+            engine.SetValue("a", new OverLoading());
+            Assert.Equal("int-val", engine.Evaluate("a.testFunc(123);").AsString());
+            Assert.Equal("float-val", engine.Evaluate("a.testFunc(12.3);").AsString());
+        }
+
         public class TestItem
         public class TestItem
         {
         {
             public double Cost { get; set; }
             public double Cost { get; set; }

+ 11 - 0
Jint/Runtime/TypeConverter.cs

@@ -1129,9 +1129,20 @@ namespace Jint.Runtime
                 return 5;
                 return 5;
             }
             }
 
 
+            if (paramType == typeof(int) && jsValue.IsInteger())
+            {
+                return 0;
+            }
+
+            if (paramType == typeof(float) && objectValueType == typeof(Double))
+            {
+                return jsValue.IsInteger() ? 1 : 0;
+            }
+
             if (paramType.IsEnum &&
             if (paramType.IsEnum &&
                 jsValue is JsNumber jsNumber
                 jsValue is JsNumber jsNumber
                 && jsNumber.IsInteger()
                 && jsNumber.IsInteger()
+                && paramType.GetEnumUnderlyingType() == typeof(int)
                 && Enum.IsDefined(paramType, jsNumber.AsInteger()))
                 && Enum.IsDefined(paramType, jsNumber.AsInteger()))
             {
             {
                 // we can do conversion from int value to enum
                 // we can do conversion from int value to enum