Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4194,4 +4194,83 @@ public void ShouldSetDerivedTypePropertyWhenDeclaredTypeHasIndexer()
var geometry = (GeometryWrapperWithProperty) feature.Geometry;
Assert.Equal(99.9, geometry.X);
}

public class TypeWithListConstructor
{
public TypeWithListConstructor(List<string> items)
{
Items = items;
}

public List<string> Items { get; }
}

public class TypeWithCollectionParameters
{
public IList<string> IListItems { get; set; } = [];
public ICollection<string> ICollectionItems { get; set; } = [];
public IEnumerable<string> IEnumerableItems { get; set; } = [];
public IReadOnlyList<string> IReadOnlyListItems { get; set; } = [];
public IReadOnlyCollection<string> IReadOnlyCollectionItems { get; set; } = [];

public void SetIListItems(IList<string> items) => IListItems = items;
public void SetICollectionItems(ICollection<string> items) => ICollectionItems = items;
public void SetIEnumerableItems(IEnumerable<string> items) => IEnumerableItems = items;
public void SetIReadOnlyListItems(IReadOnlyList<string> items) => IReadOnlyListItems = items;
public void SetIReadOnlyCollectionItems(IReadOnlyCollection<string> items) => IReadOnlyCollectionItems = items;
}

[Fact]
public void ShouldConvertJsArrayToListWhenPassedToConstructor()
{
var engine = new Engine(options => options.AllowClr(GetType().Assembly));
engine.SetValue("TypeWithListConstructor", TypeReference.CreateTypeReference(engine, typeof(TypeWithListConstructor)));

var result = engine.Evaluate("new TypeWithListConstructor(['a', 'b', 'c'])");
var obj = result.ToObject() as TypeWithListConstructor;

Assert.NotNull(obj);
Assert.Equal(3, obj.Items.Count);
Assert.Equal("a", obj.Items[0]);
Assert.Equal("b", obj.Items[1]);
Assert.Equal("c", obj.Items[2]);
}

[Fact]
public void ShouldConvertJsArrayToEmptyListWhenPassedToConstructor()
{
var engine = new Engine(options => options.AllowClr(GetType().Assembly));
engine.SetValue("TypeWithListConstructor", TypeReference.CreateTypeReference(engine, typeof(TypeWithListConstructor)));

var result = engine.Evaluate("new TypeWithListConstructor([])");
var obj = result.ToObject() as TypeWithListConstructor;

Assert.NotNull(obj);
Assert.Empty(obj.Items);
}

[Fact]
public void ShouldConvertJsArrayToGenericCollectionTypes()
{
var engine = new Engine(options => options.AllowClr(GetType().Assembly));
var target = new TypeWithCollectionParameters();
engine.SetValue("target", target);

engine.Evaluate("target.SetIListItems(['a', 'b'])");
Assert.Equal(2, target.IListItems.Count);
Assert.Equal("a", target.IListItems[0]);

engine.Evaluate("target.SetICollectionItems(['c', 'd'])");
Assert.Equal(2, target.ICollectionItems.Count);

engine.Evaluate("target.SetIEnumerableItems(['e', 'f'])");
Assert.Equal(2, target.IEnumerableItems.Count());

engine.Evaluate("target.SetIReadOnlyListItems(['g', 'h'])");
Assert.Equal(2, target.IReadOnlyListItems.Count);
Assert.Equal("g", target.IReadOnlyListItems[0]);

engine.Evaluate("target.SetIReadOnlyCollectionItems(['i', 'j'])");
Assert.Equal(2, target.IReadOnlyCollectionItems.Count);
}
}
37 changes: 37 additions & 0 deletions Jint/Runtime/Interop/DefaultTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,43 @@ private bool TryConvert(
return true;
}

// Handle conversion from object[] (JS array) to generic collection types like List<T>, IList<T>, IEnumerable<T>, etc.
// This must come before the generic assignability check because object[] incorrectly satisfies
// the assignability check for IList<string> etc. (since object[] implements IList<object>).
if (value is object?[] sourceArray && type.IsGenericType)
{
var genericArgs = type.GetGenericArguments();

if (genericArgs.Length == 1)
{
var genericTypeDef = type.GetGenericTypeDefinition();
var elementType = genericArgs[0];

if (genericTypeDef != typeof(Collection<>) && InteropHelper.GenericCollectionTypeDefinitions.Contains(genericTypeDef))
{
var targetList = (IList) Activator.CreateInstance(typeof(List<>).MakeGenericType(elementType))!;
foreach (var item in sourceArray)
{
targetList.Add(item is null ? null : Convert(item, elementType, formatProvider));
}
converted = targetList;
return true;
}

if (genericTypeDef == typeof(Collection<>))
{
var innerListType = typeof(List<>).MakeGenericType(elementType);
var innerList = (IList) Activator.CreateInstance(innerListType)!;
foreach (var item in sourceArray)
{
innerList.Add(item is null ? null : Convert(item, elementType, formatProvider));
}
converted = Activator.CreateInstance(type, innerList)!;
return true;
}
}
}

if (type.IsGenericType)
{
var result = InteropHelper.IsAssignableToGenericType(value.GetType(), type);
Expand Down
20 changes: 19 additions & 1 deletion Jint/Runtime/Interop/InteropHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ parameterValue is JsNumber jsNumber
return 1;
}

if (parameterValue.IsArray() && paramType.IsArray)
if (parameterValue.IsArray() && (paramType.IsArray || IsGenericCollectionType(paramType)))
{
// we have potential, TODO if we'd know JS array's internal type we could have exact match
return 2;
Expand Down Expand Up @@ -304,6 +304,24 @@ private static bool CanChangeType(object value, Type targetType)
}
}

internal static readonly HashSet<Type> GenericCollectionTypeDefinitions =
[
typeof(List<>),
typeof(IList<>),
typeof(ICollection<>),
typeof(IEnumerable<>),
typeof(IReadOnlyList<>),
typeof(IReadOnlyCollection<>),
typeof(System.Collections.ObjectModel.Collection<>),
];

internal static bool IsGenericCollectionType(Type type)
{
return type.IsGenericType
&& type.GetGenericArguments().Length == 1
&& GenericCollectionTypeDefinitions.Contains(type.GetGenericTypeDefinition());
}

internal static bool TypeIsNullable(Type type)
{
return !type.IsValueType || Nullable.GetUnderlyingType(type) != null;
Expand Down
7 changes: 7 additions & 0 deletions Jint/Runtime/Interop/MethodInfoFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ private static bool IsGenericParameter(object? argObj, Type parameterType)

if (parameterType.IsGenericParameter || parameterType.IsGenericType)
{
// For fully concrete generic types (no open type parameters), verify the argument
// is actually assignable to prevent incorrect direct assignment when type arguments
// differ (e.g., object[] should not be directly assigned to IList<string>)
if (!parameterType.ContainsGenericParameters && !parameterType.IsAssignableFrom(argObj.GetType()))
{
return false;
}
return true;
}
return false;
Expand Down