Kaynağa Gözat

Fixes delegates created from expressions. (#585)

Kevin Ackerman 6 yıl önce
ebeveyn
işleme
eb3bcd92fe

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

@@ -1,6 +1,7 @@
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
+using System.Linq.Expressions;
 using System.Reflection;
 using System.Reflection;
 using Jint.Native;
 using Jint.Native;
 using Jint.Native.Array;
 using Jint.Native.Array;
@@ -121,6 +122,22 @@ namespace Jint.Tests.Runtime
             ");
             ");
         }
         }
 
 
+        [Fact]
+        public void DynamicDelegateCanBeSet()
+        {
+#if NETFRAMEWORK
+            var parameters = new[] { Expression.Parameter(typeof(int)), Expression.Parameter(typeof(int)) };
+            var exp = Expression.Add(parameters[0], parameters[1]);
+            var del = Expression.Lambda(exp, parameters).Compile();
+
+            _engine.SetValue("add", del);
+            
+            RunTest(@"
+                assert(add(1,1) === 2);
+            ");
+#endif
+        }
+
         [Fact]
         [Fact]
         public void ExtraParametersAreIgnored()
         public void ExtraParametersAreIgnored()
         {
         {

+ 12 - 0
Jint/Runtime/Interop/DelegateWrapper.cs

@@ -3,6 +3,7 @@ using System.Globalization;
 using System.Reflection;
 using System.Reflection;
 using Jint.Native;
 using Jint.Native;
 using Jint.Native.Function;
 using Jint.Native.Function;
+using System.Linq;
 
 
 namespace Jint.Runtime.Interop
 namespace Jint.Runtime.Interop
 {
 {
@@ -37,6 +38,17 @@ namespace Jint.Runtime.Interop
         public override JsValue Call(JsValue thisObject, JsValue[] jsArguments)
         public override JsValue Call(JsValue thisObject, JsValue[] jsArguments)
         {
         {
             var parameterInfos = _d.Method.GetParameters();
             var parameterInfos = _d.Method.GetParameters();
+            
+#if NETFRAMEWORK
+            if (parameterInfos.Length > 0 && parameterInfos[0].ParameterType == typeof(System.Runtime.CompilerServices.Closure))
+            {
+                var reducedLength = parameterInfos.Length - 1;
+                var reducedParameterInfos = new ParameterInfo[reducedLength];
+                Array.Copy(parameterInfos, 1, reducedParameterInfos, 0, reducedLength);
+                parameterInfos = reducedParameterInfos;
+            }
+#endif
+
             int delegateArgumentsCount = parameterInfos.Length;
             int delegateArgumentsCount = parameterInfos.Length;
             int delegateNonParamsArgumentsCount = _delegateContainsParamsArgument ? delegateArgumentsCount - 1 : delegateArgumentsCount;
             int delegateNonParamsArgumentsCount = _delegateContainsParamsArgument ? delegateArgumentsCount - 1 : delegateArgumentsCount;
 
 

+ 1 - 0
appveyor.yml

@@ -11,6 +11,7 @@ build_script:
   - dotnet pack -c Release
   - dotnet pack -c Release
 test_script:
 test_script:
   - dotnet test .\Jint.Tests\Jint.Tests.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests\Jint.Tests.csproj -c Release -f netcoreapp2.1
+  - dotnet test .\Jint.Tests\Jint.Tests.csproj -c Release -f net452
   - dotnet test .\Jint.Tests.CommonScripts\Jint.Tests.CommonScripts.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests.CommonScripts\Jint.Tests.CommonScripts.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests.Ecma\Jint.Tests.Ecma.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests.Ecma\Jint.Tests.Ecma.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests.Test262\Jint.Tests.Test262.csproj -c Release -f netcoreapp2.1
   - dotnet test .\Jint.Tests.Test262\Jint.Tests.Test262.csproj -c Release -f netcoreapp2.1