Browse Source

Fix detecting extension method when there are none (#866)

Gökhan Kurt 4 years ago
parent
commit
4b9c78e3fc

+ 20 - 1
Jint.Tests/Runtime/ExtensionMethods/ExtensionMethodsTest.cs

@@ -1,4 +1,5 @@
-using Jint.Tests.Runtime.Domain;
+using Jint.Native;
+using Jint.Tests.Runtime.Domain;
 using Xunit;
 
 namespace Jint.Tests.Runtime.ExtensionMethods
@@ -69,5 +70,23 @@ namespace Jint.Tests.Runtime.ExtensionMethods
             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);
+        }
     }
 }

+ 4 - 1
Jint/Runtime/Interop/ObjectWrapper.cs

@@ -354,7 +354,10 @@ namespace Jint.Runtime.Interop
                         matches.Add(method);
                     }
                 }
-                return new MethodAccessor(MethodDescriptor.Build(matches));
+                if (matches.Count > 0)
+                {
+                    return new MethodAccessor(MethodDescriptor.Build(matches));
+                }
             }
 
             return ConstantValueAccessor.NullAccessor;

+ 1 - 1
Jint/Runtime/Interop/Reflection/ExtensionMethodCache.cs

@@ -43,7 +43,7 @@ namespace Jint.Runtime.Interop.Reflection
 
             if (methodLookup.TryGetValue(objectType, out methods))
             {
-                return true;
+                return methods.Length > 0;
             }
 
             var results = new List<MethodInfo>();