Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes delegates created from expressions. #585

Merged
merged 5 commits into from
Jan 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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()
{
Expand Down
12 changes: 12 additions & 0 deletions Jint/Runtime/Interop/DelegateWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Reflection;
using Jint.Native;
using Jint.Native.Function;
using System.Linq;

namespace Jint.Runtime.Interop
{
Expand Down Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down