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

Support passing a dictionary inside an object #740

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,5 +2044,39 @@ public void ShouldNotResolvetoPrimitiveSymbol()
Assert.NotNull(c.ToString());
Assert.Equal((uint)0, c.As<ObjectInstance>().Length);
}

class DictionaryWrapper
{
public IDictionary<string, object> Values { get; set; }
}

class DictionaryTest
{
public void Test1(IDictionary<string, object> values)
{
Assert.Equal(1, Convert.ToInt32(values["a"]));
}

public void Test2(DictionaryWrapper dictionaryObject)
{
Assert.Equal(1, Convert.ToInt32(dictionaryObject.Values["a"]));
}
}

[Fact]
public void ShouldBeAbleToPassDictionaryToMethod()
{
var engine = new Engine();
engine.SetValue("dictionaryTest", new DictionaryTest());
engine.Execute("dictionaryTest.test1({ a: 1 });");
}

[Fact]
public void ShouldBeAbleToPassDictionaryInObjectToMethod()
{
var engine = new Engine();
engine.SetValue("dictionaryTest", new DictionaryTest());
engine.Execute("dictionaryTest.test2({ values: { a: 1 } });");
}
}
}
4 changes: 2 additions & 2 deletions Jint/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ internal static void SetValue(this MemberInfo memberInfo, object forObject, obje
{
case MemberTypes.Field:
var fieldInfo = (FieldInfo) memberInfo;
if (fieldInfo.FieldType == value?.GetType())
if (value != null && fieldInfo.FieldType.IsAssignableFrom(value.GetType()))
{
fieldInfo.SetValue(forObject, value);
}

break;
case MemberTypes.Property:
var propertyInfo = (PropertyInfo) memberInfo;
if (propertyInfo.PropertyType == value?.GetType())
if (value != null && propertyInfo.PropertyType.IsAssignableFrom(value.GetType()))
{
propertyInfo.SetValue(forObject, value);
}
Expand Down