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

Fix detecting extension method when there are none #866

Merged
merged 1 commit into from
Apr 11, 2021
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
21 changes: 20 additions & 1 deletion Jint.Tests/Runtime/ExtensionMethods/ExtensionMethodsTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Jint.Tests.Runtime.Domain;
using Jint.Native;
using Jint.Tests.Runtime.Domain;
using Xunit;

namespace Jint.Tests.Runtime.ExtensionMethods
Expand Down Expand Up @@ -69,5 +70,23 @@ public void PrototypeFunctionsShouldNotBeOverridden()
Assert.Equal("yes", arr[0]);
Assert.Equal("no", arr[1]);
}

[Fact]
public void HasOwnPropertyShouldWorkCorrectlyInPresenceOfExtensionMethods()
{
var person = new Person();

var options = new Options();
options.AddExtensionMethods(typeof(PersonExtensions));

var engine = new Engine(options);
engine.SetValue("person", person);

var isBogusInPerson = engine.Execute("'bogus' in person").GetCompletionValue().AsBoolean();
Assert.False(isBogusInPerson);

var propertyValue = engine.Execute("person.bogus").GetCompletionValue();
Assert.Equal(JsValue.Undefined, propertyValue);
}
}
}
5 changes: 4 additions & 1 deletion Jint/Runtime/Interop/ObjectWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,10 @@ private static ReflectionAccessor ResolvePropertyDescriptorFactory(Engine engine
matches.Add(method);
}
}
return new MethodAccessor(MethodDescriptor.Build(matches));
if (matches.Count > 0)
{
return new MethodAccessor(MethodDescriptor.Build(matches));
}
}

return ConstantValueAccessor.NullAccessor;
Expand Down
2 changes: 1 addition & 1 deletion Jint/Runtime/Interop/Reflection/ExtensionMethodCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public bool TryGetExtensionMethods(Type objectType, out MethodInfo[] methods)

if (methodLookup.TryGetValue(objectType, out methods))
{
return true;
return methods.Length > 0;
}

var results = new List<MethodInfo>();
Expand Down