Skip to content

Fix JS array → C# generic collection conversion in CLR interop#2384

Merged
lahma merged 3 commits into
mainfrom
copilot/fix-list-conversion-issues
Apr 6, 2026
Merged

Fix JS array → C# generic collection conversion in CLR interop#2384
lahma merged 3 commits into
mainfrom
copilot/fix-list-conversion-issues

Conversation

Copilot AI commented Apr 6, 2026

Copy link
Copy Markdown
Contributor

Passing a JS array to a CLR constructor or method expecting List<T>, IList<T>, IEnumerable<T>, or related collection types threw InvalidCastException: Object must implement IConvertible.

new TreeItemData([])  // throws InvalidCastException
public TreeItemData(List<TreeItemColumnData> columns) { ... }

Root causes

Two independent bugs, both stemming from object[] (the .ToObject() representation of a JsArray) incorrectly satisfying generic type checks meant for already-compatible types:

  • DefaultTypeConverter.TryConvert: The early generic assignability check returned true for object[] → IList<string> because object[] does implement IList<> (with object, not string). This caused converted = object[] to be returned unchanged, failing at invocation.
  • MethodInfoFunction.IsGenericParameter: Returned true for the same reason, setting parameters[i] = object[] without conversion and letting MethodBase.Invoke throw.

Changes

  • DefaultTypeConverter.TryConvert: Added object[] → generic collection conversion (covering List<T>, IList<T>, ICollection<T>, IEnumerable<T>, IReadOnlyList<T>, IReadOnlyCollection<T>, Collection<T>). Placed before the generic assignability check to prevent the incorrect early return.
  • MethodInfoFunction.IsGenericParameter: For fully concrete generic types (!ContainsGenericParameters), now also verifies parameterType.IsAssignableFrom(argObj.GetType()) before taking the direct-assignment path.
  • InteropHelper.CalculateMethodParameterScore: JS array → generic collection parameter now scores 2 (same as JS array → C# array).
  • InteropHelper: Extracted a static HashSet<Type> GenericCollectionTypeDefinitions for O(1) lookups, shared with DefaultTypeConverter.

fixes #2383

Copilot AI linked an issue Apr 6, 2026 that may be closed by this pull request
Copilot AI and others added 2 commits April 6, 2026 16:39
… and MethodInfoFunction

Agent-Logs-Url: https://github.com/sebastienros/jint/sessions/a0cdbe69-2cee-46f9-bdf3-384e7049081e

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
…ltTypeConverter and InteropHelper

Agent-Logs-Url: https://github.com/sebastienros/jint/sessions/a0cdbe69-2cee-46f9-bdf3-384e7049081e

Co-authored-by: lahma <171892+lahma@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix conversion issues in TreeItemData initialization Fix JS array → C# generic collection conversion in CLR interop Apr 6, 2026
Copilot AI requested a review from lahma April 6, 2026 16:45
Copilot finished work on behalf of lahma April 6, 2026 16:45
@lahma
lahma marked this pull request as ready for review April 6, 2026 16:55
@lahma
lahma enabled auto-merge (squash) April 6, 2026 16:56
@lahma
lahma merged commit 6969707 into main Apr 6, 2026
10 of 12 checks passed
@lahma
lahma deleted the copilot/fix-list-conversion-issues branch April 6, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

List conversion issues

2 participants