Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix array method scoring under interop #1274

Merged
merged 1 commit into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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