Browse Source

Fix array method scoring under interop (#1274)

Marko Lahma 3 years ago
parent
commit
7f80dc894d
2 changed files with 31 additions and 2 deletions
  1. 29 0
      Jint.Tests/Runtime/MethodAmbiguityTests.cs
  2. 2 2
      Jint/Runtime/TypeConverter.cs

+ 29 - 0
Jint.Tests/Runtime/MethodAmbiguityTests.cs

@@ -100,6 +100,22 @@ namespace Jint.Tests.Runtime
             Assert.Equal("Class2.Object", engine.Evaluate("Class2.Print(() => '');"));
             Assert.Equal("Class2.Object", engine.Evaluate("Class2.Print(() => '');"));
         }
         }
 
 
+        [Fact]
+        public void ShouldMatchCorrectConstructors()
+        {
+            Engine engine = new Engine();
+            engine.SetValue("MyClass", TypeReference.CreateTypeReference(engine, typeof(Class3)));
+
+            engine.Execute(@"
+                const a = new MyClass();
+                a.Print(1); // Works
+                a.Print([1, 2]); // Works
+
+                const b = new MyClass(1); // Works
+                const c = new MyClass([1, 2]); // Throws 'an object must implement IConvertible' exception
+			");
+        }
+
         private struct Class1
         private struct Class1
         {
         {
             public static string Print(ExpandoObject eo) => nameof(Class1) + "." + nameof(ExpandoObject);
             public static string Print(ExpandoObject eo) => nameof(Class1) + "." + nameof(ExpandoObject);
@@ -114,5 +130,18 @@ namespace Jint.Tests.Runtime
             public static string Print(int i) => nameof(Class2) + "." + nameof(Int32);
             public static string Print(int i) => nameof(Class2) + "." + nameof(Int32);
             public static string Print(object o) => nameof(Class2) + "." + nameof(Object);
             public static string Print(object o) => nameof(Class2) + "." + nameof(Object);
         }
         }
+
+        public class Class3
+        {
+            public Class3() { }
+
+            public Class3(int a) => Console.WriteLine("Class1(int a): " + a);
+
+            public Class3(object a) => Console.WriteLine("Class1(object a): " + a);
+
+            public void Print(int a) => Console.WriteLine("Print(int a): " + a);
+
+            public void Print(object a) => Console.WriteLine("Print(object a): " + a);
+        }
     }
     }
 }
 }

+ 2 - 2
Jint/Runtime/TypeConverter.cs

@@ -1202,7 +1202,7 @@ namespace Jint.Runtime
                 return 0;
                 return 0;
             }
             }
 
 
-            if (objectValue == null)
+            if (objectValue is null)
             {
             {
                 if (!TypeIsNullable(paramType))
                 if (!TypeIsNullable(paramType))
                 {
                 {
@@ -1251,7 +1251,7 @@ namespace Jint.Runtime
                 return 1;
                 return 1;
             }
             }
 
 
-            if (jsValue.IsArray() && objectValueType!.IsArray)
+            if (jsValue.IsArray() && paramType.IsArray)
             {
             {
                 // we have potential, TODO if we'd know JS array's internal type we could have exact match
                 // we have potential, TODO if we'd know JS array's internal type we could have exact match
                 return 2;
                 return 2;