Browse Source

Fixing method invocation

Sebastien Ros 11 years ago
parent
commit
91fd6493b9

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

@@ -220,6 +220,27 @@ namespace Jint.Tests.Runtime
             Assert.Equal("10", p.Name);
             Assert.Equal(20, p.Age);
         }
+
+        [Fact]
+        public void ShouldCallInstanceMethodWithoutArgument()
+        {
+            _engine.SetValue("a", new A());
+
+            RunTest(@"
+                assert(a.Call1() === 0);
+            ");
+        }
+
+        [Fact]
+        public void ShouldCallInstanceMethodOverloadArgument()
+        {
+            _engine.SetValue("a", new A());
+
+            RunTest(@"
+                assert(a.Call1(1) === 1);
+            ");
+        }
+
         public class Person
         {
             public string Name { get; set; }
@@ -230,5 +251,18 @@ namespace Jint.Tests.Runtime
                 return Name;
             }
         }
+
+        public class A
+        {
+            public int Call1()
+            {
+                return 0;
+            }
+
+            public int Call1(int x)
+            {
+                return x;
+            }
+        }
     }
 }

+ 5 - 4
Jint/Runtime/Interop/MethodInfoFunctionInstance.cs

@@ -1,5 +1,4 @@
 using System;
-using System.Collections.Generic;
 using System.Globalization;
 using System.Linq;
 using System.Reflection;
@@ -34,10 +33,12 @@ namespace Jint.Runtime.Interop
 
             // todo: look for compatible types    
             var method = methods.First();
-            var parameters = new List<object>();
-            for (int i = 0; i < arguments.Length; i++)
+            var parameters = new object[arguments.Length];
+            for (var i = 0; i < arguments.Length; i++)
             {
-                parameters[i] = Convert.ChangeType(arguments[i].ToObject(), method.GetParameters()[i].ParameterType,
+                parameters[i] = Convert.ChangeType(
+                    arguments[i].ToObject(),
+                    method.GetParameters()[i].ParameterType,
                     CultureInfo.InvariantCulture);
             }