Explorar o código

NullReferenceException bugfix for null arguments

postromantic %!s(int64=10) %!d(string=hai) anos
pai
achega
18ae90ed47

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

@@ -1052,5 +1052,16 @@ namespace Jint.Tests.Runtime
             ");
         }
 
+        [Fact]
+        public void NullValueAsArgumentShouldWork()
+        {
+            _engine.SetValue("a", new A());
+
+            RunTest(@"
+                var x = a.Call2(null);
+                assert(x === null);
+            ");
+        }
+
     }
 }

+ 14 - 13
Jint/Runtime/Interop/MethodInfoFunctionInstance.cs

@@ -2,6 +2,7 @@
 using System.Collections.Generic;
 using System.Globalization;
 using System.Linq;
+using System.Linq.Expressions;
 using System.Reflection;
 using Jint.Native;
 using Jint.Native.Array;
@@ -36,28 +37,28 @@ namespace Jint.Runtime.Interop
                 var parameters = new object[arguments.Length];
                 var argumentsMatch = true;
 
-                    for (var i = 0; i < arguments.Length; i++)
-                    {
-                        var parameterType = method.GetParameters()[i].ParameterType;
+                for (var i = 0; i < arguments.Length; i++)
+                {
+                    var parameterType = method.GetParameters()[i].ParameterType;
 
-                        if (parameterType == typeof(JsValue))
-                        {
-                            parameters[i] = arguments[i];
-                        }
-                        else
-                        {
+                    if (parameterType == typeof(JsValue))
+                    {
+                        parameters[i] = arguments[i];
+                    }
+                    else
+                    {
                         if (!converter.TryConvert(arguments[i].ToObject(), parameterType, CultureInfo.InvariantCulture, out parameters[i]))
                         {
                             argumentsMatch = false;
                             break;
                         }
 
-                            if (typeof(System.Linq.Expressions.LambdaExpression).IsAssignableFrom(parameters[i].GetType()))
-                            {
-                                parameters[i] = (parameters[i] as System.Linq.Expressions.LambdaExpression).Compile();
-                            }
+                        if (parameters[i] is LambdaExpression)
+                        {
+                            parameters[i] = (parameters[i] as LambdaExpression).Compile();
                         }
                     }
+                }
 
                 if (!argumentsMatch)
                 {