diff --git a/Jint.Tests/Runtime/InteropTests.cs b/Jint.Tests/Runtime/InteropTests.cs index 4c14838442..a6db22779c 100644 --- a/Jint.Tests/Runtime/InteropTests.cs +++ b/Jint.Tests/Runtime/InteropTests.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Linq.Expressions; using System.Reflection; using Jint.Native; using Jint.Native.Array; @@ -121,6 +122,22 @@ public void DelegateWithObjectParameterCanBeExcluded() "); } + [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] public void ExtraParametersAreIgnored() { diff --git a/Jint/Runtime/Interop/DelegateWrapper.cs b/Jint/Runtime/Interop/DelegateWrapper.cs index f8495fc5d1..4990016b32 100644 --- a/Jint/Runtime/Interop/DelegateWrapper.cs +++ b/Jint/Runtime/Interop/DelegateWrapper.cs @@ -3,6 +3,7 @@ using System.Reflection; using Jint.Native; using Jint.Native.Function; +using System.Linq; namespace Jint.Runtime.Interop { @@ -37,6 +38,17 @@ public DelegateWrapper(Engine engine, Delegate d) public override JsValue Call(JsValue thisObject, JsValue[] jsArguments) { 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 delegateNonParamsArgumentsCount = _delegateContainsParamsArgument ? delegateArgumentsCount - 1 : delegateArgumentsCount; diff --git a/appveyor.yml b/appveyor.yml index eb20d89dd0..efac6e3eb1 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -11,6 +11,7 @@ build_script: - dotnet pack -c Release test_script: - 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.Ecma\Jint.Tests.Ecma.csproj -c Release -f netcoreapp2.1 - dotnet test .\Jint.Tests.Test262\Jint.Tests.Test262.csproj -c Release -f netcoreapp2.1