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

Array.prototype.indexOf does not work with interop collections #1189

Closed
ivan-menshikov opened this issue Jun 6, 2022 · 1 comment · Fixed by #1190
Closed

Array.prototype.indexOf does not work with interop collections #1189

ivan-menshikov opened this issue Jun 6, 2022 · 1 comment · Fixed by #1190

Comments

@ivan-menshikov
Copy link

If I try to use Array.prototype.indexOf with interop collection such as List<T> it returns -1 for any argument until I call something like Object.keys to create properties (actually I'm using SetPrototypeOf with my custom collection, but I think it's not important).

It seems the issue occurs because ObjectInstanceOperations.GetSmallestIndex returns the length of the collection if there are no cached properties for indices.

[Fact]
public void ArrayPrototypeIndexOfWithInteropList()
{
    var engine = new Jint.Engine();

    engine.SetValue("list", new List<string> { "A", "B", "C" });

    // ok: interop methods IndexOf and LastIndexOf of List<T> are used
    Assert.Equal(1, engine.Evaluate("list.indexOf('B')"));
    Assert.Equal(1, engine.Evaluate("list.lastIndexOf('B')"));

    // error: returns -1 because list does not have properties 0, 1 or 2
    Assert.Equal(-1, engine.Evaluate("Array.prototype.indexOf.call(list, 'B')"));
    // ok: this method does not use GetSmallestIndex
    Assert.Equal(1, engine.Evaluate("Array.prototype.lastIndexOf.call(list, 'B')"));

    // call any method that creates properties
    engine.Execute("Object.keys(list);");

    // now this works as expected
    Assert.Equal(1, engine.Evaluate("Array.prototype.indexOf.call(list, 'B')"));
}
@lahma
Copy link
Collaborator

lahma commented Jun 6, 2022

Thanks for the report, fix should be soon available on MyGet feed, later at some point on the NuGet feed.

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 a pull request may close this issue.

2 participants