Skip to content

Commit

Permalink
address feedback
Browse files Browse the repository at this point in the history
fix
  • Loading branch information
lofcz committed Aug 17, 2024
1 parent c2d36eb commit 654d557
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 10 deletions.
36 changes: 27 additions & 9 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3499,7 +3499,7 @@ public void ShouldNotSeeClrMethods()
{
var engine = new Engine(opt =>
{
opt.Interop.ObjectWrapperReportedMemberTypes = MemberTypes.Field | MemberTypes.Property;
});

engine.SetValue("clrInstance", new ClrMembersVisibilityTestClass());
Expand All @@ -3510,25 +3510,43 @@ public void ShouldNotSeeClrMethods()
var props = obj.GetOwnProperties().ToList();

Assert.Equal(props.Count, 1);
props.Should().ContainSingle(x => x.Key == "A");
}

[Fact]
public void ShouldSeeClrMethods()
{
var engine = new Engine(opt =>
{
opt.Interop.ObjectWrapperReportedMemberTypes = MemberTypes.Method | MemberTypes.Field | MemberTypes.Property;
});
var engine = new Engine();

engine.SetValue("clrInstance", new ClrMembersVisibilityTestClass());

var getValue = engine.Evaluate("clrInstance.get_A()");
Assert.Equal(getValue, 10);

var val = engine.GetValue("clrInstance");
var obj = val.AsObject();
var props = obj.GetOwnProperties().ToList();

Assert.Equal(props.Count, 8); // A get/set + F + GetType + ToString + Equals + GetHashCode
Assert.Equal(props.Count, 2);
props.Should().ContainSingle(x => x.Key.AsString() == "A");
props.Should().ContainSingle(x => x.Key.AsString() == "F");
}

private class ClrMembersVisibilityTestClass2
{
public int Get_A { get; set; } = 5;
}

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

engine.SetValue("clrInstance", new ClrMembersVisibilityTestClass2());

var val = engine.GetValue("clrInstance");

var obj = val.AsObject();
var props = obj.GetOwnProperties().ToList();

Assert.Equal(props.Count, 1);
props.Should().ContainSingle(x => x.Key == "Get_A");
}
}
2 changes: 1 addition & 1 deletion Jint/Options.cs
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ public class InteropOptions
/// Supported values are: <see cref="MemberTypes.Field"/>, <see cref="MemberTypes.Property"/>, <see cref="MemberTypes.Method"/>.
/// All other values are ignored.
/// </summary>
public MemberTypes ObjectWrapperReportedMemberTypes { get; set; } = MemberTypes.Field | MemberTypes.Property;
public MemberTypes ObjectWrapperReportedMemberTypes { get; set; } = MemberTypes.Field | MemberTypes.Property | MemberTypes.Method;
}

public class ConstraintOptions
Expand Down
5 changes: 5 additions & 0 deletions Jint/Runtime/Interop/ObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,11 @@ private IEnumerable<JsValue> EnumerateOwnPropertyKeys(Types types)
{
foreach (var m in ClrType.GetMethods(BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public))
{
if (m.IsSpecialName || m.DeclaringType == typeof(object))
{
continue;
}

var jsString = JsString.Create(m.Name);
yield return jsString;
}
Expand Down

0 comments on commit 654d557

Please sign in to comment.