Browse Source

Merge pull request #177 from csabakiss/feature/ignoreMethodCasing

Feature/ignore method casing
Sébastien Ros 10 years ago
parent
commit
12d5fac007

+ 15 - 0
Jint.Tests/Runtime/InteropTests.cs

@@ -125,6 +125,21 @@ namespace Jint.Tests.Runtime
             ");
             ");
         }
         }
 
 
+        [Fact]
+        public void CanInvokeObjectMethodsWithPascalCase()
+        {
+            var p = new Person
+            {
+                Name = "Mickey Mouse"
+            };
+
+            _engine.SetValue("p", p);
+
+            RunTest(@"
+                assert(p.toString() === 'Mickey Mouse');
+            ");
+        }
+
         [Fact]
         [Fact]
         public void CanSetObjectProperties()
         public void CanSetObjectProperties()
         {
         {

+ 15 - 12
Jint/Runtime/Interop/DefaultTypeConverter.cs

@@ -178,21 +178,24 @@ namespace Jint.Runtime.Interop
             bool canConvert;
             bool canConvert;
             var key = value == null ? String.Format("Null->{0}", type) : String.Format("{0}->{1}", value.GetType(), type);
             var key = value == null ? String.Format("Null->{0}", type) : String.Format("{0}->{1}", value.GetType(), type);
 
 
-            lock (_lockObject)
+            if (!_knownConversions.TryGetValue(key, out canConvert))
             {
             {
-                if (!_knownConversions.TryGetValue(key, out canConvert))
+                lock (_lockObject)
                 {
                 {
-                    try
+                    if (!_knownConversions.TryGetValue(key, out canConvert))
                     {
                     {
-                        converted = Convert(value, type, formatProvider);
-                        _knownConversions.Add(key, true);
-                        return true;
-                    }
-                    catch
-                    {
-                        converted = null;
-                        _knownConversions.Add(key, false);
-                        return false;
+                        try
+                        {
+                            converted = Convert(value, type, formatProvider);
+                            _knownConversions.Add(key, true);
+                            return true;
+                        }
+                        catch
+                        {
+                            converted = null;
+                            _knownConversions.Add(key, false);
+                            return false;
+                        }
                     }
                     }
                 }
                 }
             }
             }

+ 31 - 0
Jint/Runtime/Interop/ObjectWrapper.cs

@@ -91,6 +91,24 @@ namespace Jint.Runtime.Interop
                 return descriptor;
                 return descriptor;
             }
             }
 
 
+            var pascalCasedPropertyName = char.ToUpperInvariant(propertyName[0]).ToString();
+            if (propertyName.Length > 1)
+            {
+                pascalCasedPropertyName += propertyName.Substring(1);
+            }
+
+            // look for methods using pascal cased name.
+            var pascalCasedMethods = type.GetMethods(BindingFlags.Instance | BindingFlags.Public)
+                .Where(m => m.Name == pascalCasedPropertyName)
+                .ToArray();
+
+            if (pascalCasedMethods.Any())
+            {
+                var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, pascalCasedMethods), false, true, false);
+                Properties.Add(propertyName, descriptor);
+                return descriptor;
+            }
+
             // if no methods are found check if target implemented indexing
             // if no methods are found check if target implemented indexing
             if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
             if (type.GetProperties().Where(p => p.GetIndexParameters().Length != 0).FirstOrDefault() != null)
             {
             {
@@ -125,6 +143,19 @@ namespace Jint.Runtime.Interop
                 return descriptor;
                 return descriptor;
             }
             }
 
 
+            // try to find explicit method implementations using the pascal cased property name
+            var explicitPascalCasedMethods = (from iface in interfaces
+                                              from imethod in iface.GetMethods()
+                                              where pascalCasedPropertyName.Equals(imethod.Name)
+                                              select imethod).ToArray();
+
+            if (explicitPascalCasedMethods.Length > 0)
+            {
+                var descriptor = new PropertyDescriptor(new MethodInfoFunctionInstance(Engine, explicitPascalCasedMethods), false, true, false);
+                Properties.Add(propertyName, descriptor);
+                return descriptor;
+            }
+
             // try to find explicit indexer implementations
             // try to find explicit indexer implementations
             var explicitIndexers =
             var explicitIndexers =
                 (from iface in interfaces
                 (from iface in interfaces