Skip to content

Commit

Permalink
Fix array method scoring under interop (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Aug 27, 2022
1 parent 41309de commit 7f80dc8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
29 changes: 29 additions & 0 deletions Jint.Tests/Runtime/MethodAmbiguityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,22 @@ public void ShouldFavorOtherOverloadsOverObjectParameter()
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
{
public static string Print(ExpandoObject eo) => nameof(Class1) + "." + nameof(ExpandoObject);
Expand All @@ -114,5 +130,18 @@ private struct Class2
public static string Print(int i) => nameof(Class2) + "." + nameof(Int32);
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);
}
}
}
4 changes: 2 additions & 2 deletions Jint/Runtime/TypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ internal static AssignableResult IsAssignableToGenericType(Type givenType, Type
return 0;
}

if (objectValue == null)
if (objectValue is null)
{
if (!TypeIsNullable(paramType))
{
Expand Down Expand Up @@ -1251,7 +1251,7 @@ internal static AssignableResult IsAssignableToGenericType(Type givenType, Type
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
return 2;
Expand Down

0 comments on commit 7f80dc8

Please sign in to comment.