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
41 changes: 41 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ public void EngineShouldStringifyADictionaryOfStringAndObjectCorrectly()
Assert.Equal("{\"foo\":5,\"bar\":\"A string\"}", result);
}

[Fact]
public void ReadOnlyDictionaryShouldNotBeTreatedAsArrayLike()
{
var engine = new Engine();

var dictionary = new ReadOnlyDictionary(new Dictionary<string, object>
{
{ "foo", 5 },
{ "bar", "A string" }
});
engine.SetValue(nameof(dictionary), dictionary);

var result = engine.Evaluate($"JSON.stringify({nameof(dictionary)})").AsString();
Assert.Equal("{\"foo\":5,\"bar\":\"A string\"}", result);

var keys = engine.Evaluate($"Object.keys({nameof(dictionary)})").AsArray();
Assert.Equal((uint) 2, keys.Length);
}

[Fact]
public void EngineShouldRoundtripParsedJSONBackToStringCorrectly()
{
Expand Down Expand Up @@ -3655,6 +3674,28 @@ public Dictionary<string, int> Metadata
}
}

/// <summary>
/// A custom IReadOnlyDictionary that does NOT implement IDictionary, to verify it's treated as dictionary-like, not array-like.
/// </summary>
private class ReadOnlyDictionary : IReadOnlyDictionary<string, object>
{
private readonly Dictionary<string, object> _dictionary;

public ReadOnlyDictionary(Dictionary<string, object> dictionary)
{
_dictionary = dictionary;
}

public IEnumerator<KeyValuePair<string, object>> GetEnumerator() => _dictionary.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public int Count => _dictionary.Count;
public bool ContainsKey(string key) => _dictionary.ContainsKey(key);
public bool TryGetValue(string key, out object value) => _dictionary.TryGetValue(key, out value!);
public object this[string key] => _dictionary[key];
public IEnumerable<string> Keys => _dictionary.Keys;
public IEnumerable<object> Values => _dictionary.Values;
}

[Fact]
public void CanSelectShadowedPropertiesBasedOnReadableAndWritable()
{
Expand Down
20 changes: 13 additions & 7 deletions Jint/Runtime/Interop/TypeDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ internal sealed class TypeDescriptor
private static readonly PropertyInfo _listIndexer = typeof(IList).GetProperty("Item")!;

private static readonly Type _genericDictionaryType = typeof(IDictionary<,>);
private static readonly Type _readOnlyGenericDictionaryType = typeof(IReadOnlyDictionary<,>);
private static readonly Type _stringType = typeof(string);

private readonly MethodInfo? _tryGetValueMethod;
Expand Down Expand Up @@ -176,21 +177,26 @@ private static void AnalyzeType(
var genericTypeDefinition = type.GetGenericTypeDefinition();

// check if object has any generic dictionary signature that accepts string as key
var b = genericTypeDefinition == _genericDictionaryType;
if (b && type.GenericTypeArguments[0] == _stringType)
var isGenericDictionary = genericTypeDefinition == _genericDictionaryType;
var isReadOnlyGenericDictionary = genericTypeDefinition == _readOnlyGenericDictionaryType;
if ((isGenericDictionary || isReadOnlyGenericDictionary) && type.GenericTypeArguments[0] == _stringType)
{
tryGetValueMethod = type.GetMethod("TryGetValue");
removeMethod = type.GetMethod("Remove");
keysAccessor = type.GetProperty("Keys");
valueType = type.GenericTypeArguments[1];
tryGetValueMethod ??= type.GetMethod("TryGetValue");
keysAccessor ??= type.GetProperty("Keys");
valueType ??= type.GenericTypeArguments[1];

if (isGenericDictionary)
{
removeMethod ??= type.GetMethod("Remove");
}
}

isCollection |= genericTypeDefinition == typeof(IReadOnlyCollection<>) || genericTypeDefinition == typeof(ICollection<>);
if (genericTypeDefinition == typeof(IList<>))
{
integerIndexer ??= type.GetProperty("Item");
}
isDictionary |= genericTypeDefinition == _genericDictionaryType;
isDictionary |= isGenericDictionary || isReadOnlyGenericDictionary;
}
}

Expand Down