sebastienros / jint

Javascript Interpreter for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Integer key based indexer is omitted when string key based indexer exists in interfaces

uleelx opened this issue · comments

Version used

3.0.0-beta-2053

Describe the bug

Integer key based indexer is omitted when string key based indexer exists in interfaces.

To Reproduce

using Jint;

namespace ConsoleApp1
{
    public interface IIndexer<out T>
    {
        T? this[int index] { get; }
    }

    public interface IStringCollection : IIndexer<string>
    {
        string? this[string name] { get; }
    }

    public class Strings : IStringCollection
    {
        private readonly string[] _strings;
        public Strings(List<string> strings)
        {
            _strings = strings.ToArray();
        }
        public string? this[string name] => _strings.FirstOrDefault(x => x.Contains(name));

        public string this[int index] => _strings[index];

    }

    public class Utils
    {
        public IStringCollection GetStrings()
        {
            IStringCollection strings = new Strings(new List<string>() { "aaa", "bbb", "ccc" });
            return strings;
        }
    }

    internal class Program
    {
        static void Main(string[] args)
        {
            Engine engine = new Engine();

            engine.SetValue("Utils", new Utils());
            engine.Execute(@"const strings = Utils.GetStrings(); var result = strings[2];");
            var result = engine.GetValue("result").ToString();
            Console.WriteLine($"strings[2]: {result}");
        }
    }
}

Expected behavior

Print "strings[2]: ccc" expected, but "strings[2]: null"

Should be now fixed in main / soon available on MyGet feed.

Should be now fixed in main / soon available on MyGet feed.

This issue remains in version 3.0.0-preview-506(Myget feed). @lahma

Should be now fixed in main / soon available on MyGet feed.

This issue remains in version 3.0.0-preview-506(Myget feed). @lahma

The test case associated with the PR now passes and earlier it didn't resolve to integer indexer. Can you show a failing test case to diagnose?

Should be now fixed in main / soon available on MyGet feed.

This issue remains in version 3.0.0-preview-506(Myget feed). @lahma

The test case associated with the PR now passes and earlier it didn't resolve to integer indexer. Can you show a failing test case to diagnose?

Just remove line 3300 of Jint.Tests/Runtime/InteropTests.cs:

        public interface IStringCollection : IIndexer<string>, ICountable<string>
        {
            string this[string name] { get; }
            string this[int index] { get; } // remove this line
        }

because interface "IStringCollection" inherits "IIndexer" which claims the integer indexer above.

Inherited indexer that needs type coercion might be a limitation I'd be willing to accept, unless someone wants to submit a PR to improve the logic.

Now indexer selection knows how to prioritize better and latest issue should be covered by #1669

Now indexer selection knows how to prioritize better and latest issue should be covered by #1669

Kudos to you for your exceptional PR! Your expertise and attention to detail are highly appreciated.