From b0bf973424714711303257139de432bca109acfd Mon Sep 17 00:00:00 2001 From: kackerman Date: Thu, 10 Jan 2019 18:36:34 -0600 Subject: [PATCH 1/5] Fixes bug where delegates generated from expressions contain a closure as their first argument, causing mapping of parameters to fail. --- Jint.Tests/Runtime/InteropTests.cs | 16 ++++++++++++++++ Jint/Runtime/Interop/DelegateWrapper.cs | 7 +++++++ 2 files changed, 23 insertions(+) diff --git a/Jint.Tests/Runtime/InteropTests.cs b/Jint.Tests/Runtime/InteropTests.cs index 4c14838442..18abda55b3 100644 --- a/Jint.Tests/Runtime/InteropTests.cs +++ b/Jint.Tests/Runtime/InteropTests.cs @@ -1,6 +1,8 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Collections.ObjectModel; +using System.Linq.Expressions; using System.Reflection; using Jint.Native; using Jint.Native.Array; @@ -121,6 +123,20 @@ public void DelegateWithObjectParameterCanBeExcluded() "); } + [Fact] + public void DynamicDelegateCanBeSet() + { + 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); + "); + } + [Fact] public void ExtraParametersAreIgnored() { diff --git a/Jint/Runtime/Interop/DelegateWrapper.cs b/Jint/Runtime/Interop/DelegateWrapper.cs index f8495fc5d1..3d77684416 100644 --- a/Jint/Runtime/Interop/DelegateWrapper.cs +++ b/Jint/Runtime/Interop/DelegateWrapper.cs @@ -1,6 +1,7 @@ using System; using System.Globalization; using System.Reflection; +using System.Linq; using Jint.Native; using Jint.Native.Function; @@ -37,6 +38,12 @@ public DelegateWrapper(Engine engine, Delegate d) public override JsValue Call(JsValue thisObject, JsValue[] jsArguments) { var parameterInfos = _d.Method.GetParameters(); + + if (parameterInfos.Length > 0 && parameterInfos[0].ParameterType.FullName == "System.Runtime.CompilerServices.Closure") + { + parameterInfos = parameterInfos.Skip(1).ToArray(); + } + int delegateArgumentsCount = parameterInfos.Length; int delegateNonParamsArgumentsCount = _delegateContainsParamsArgument ? delegateArgumentsCount - 1 : delegateArgumentsCount; From 857b6d6aadfdc6c5cec363863ff0639aa144c993 Mon Sep 17 00:00:00 2001 From: kackerman Date: Fri, 11 Jan 2019 18:28:17 -0600 Subject: [PATCH 2/5] Improves the efficiency of removing the parameter by 0.1 ticks on average and only runs the test and check in NETFRAMEWORK --- Jint.Tests/Runtime/InteropTests.cs | 5 +++-- Jint/Runtime/Interop/DelegateWrapper.cs | 9 +++++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Jint.Tests/Runtime/InteropTests.cs b/Jint.Tests/Runtime/InteropTests.cs index 18abda55b3..a6db22779c 100644 --- a/Jint.Tests/Runtime/InteropTests.cs +++ b/Jint.Tests/Runtime/InteropTests.cs @@ -1,7 +1,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Collections.ObjectModel; using System.Linq.Expressions; using System.Reflection; using Jint.Native; @@ -126,15 +125,17 @@ 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] diff --git a/Jint/Runtime/Interop/DelegateWrapper.cs b/Jint/Runtime/Interop/DelegateWrapper.cs index 3d77684416..4d3f77c65e 100644 --- a/Jint/Runtime/Interop/DelegateWrapper.cs +++ b/Jint/Runtime/Interop/DelegateWrapper.cs @@ -38,11 +38,16 @@ 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.FullName == "System.Runtime.CompilerServices.Closure") { - parameterInfos = parameterInfos.Skip(1).ToArray(); + 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; From 73bd4c044cc4297ef51ae05393362425f833a808 Mon Sep 17 00:00:00 2001 From: kackerman Date: Fri, 11 Jan 2019 18:44:56 -0600 Subject: [PATCH 3/5] Changes check to typeof instead of string comparison. --- Jint/Runtime/Interop/DelegateWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jint/Runtime/Interop/DelegateWrapper.cs b/Jint/Runtime/Interop/DelegateWrapper.cs index 4d3f77c65e..59a80a3f77 100644 --- a/Jint/Runtime/Interop/DelegateWrapper.cs +++ b/Jint/Runtime/Interop/DelegateWrapper.cs @@ -40,7 +40,7 @@ public override JsValue Call(JsValue thisObject, JsValue[] jsArguments) var parameterInfos = _d.Method.GetParameters(); #if NETFRAMEWORK - if (parameterInfos.Length > 0 && parameterInfos[0].ParameterType.FullName == "System.Runtime.CompilerServices.Closure") + if (parameterInfos.Length > 0 && parameterInfos[0].ParameterType == typeof(System.Runtime.CompilerServices.Closure)) { var reducedLength = parameterInfos.Length - 1; var reducedParameterInfos = new ParameterInfo[reducedLength]; From bf08e01e6341169d297bc2f4e8332a7f22761f94 Mon Sep 17 00:00:00 2001 From: kackerman Date: Sat, 12 Jan 2019 09:40:05 -0600 Subject: [PATCH 4/5] Adds netframework to yaml tests. --- appveyor.yml | 1 + 1 file changed, 1 insertion(+) 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 From 564282bbefd45da20230a74d30e19d8b1c7240e4 Mon Sep 17 00:00:00 2001 From: kackerman Date: Sat, 12 Jan 2019 09:49:58 -0600 Subject: [PATCH 5/5] Removes unused System.Linq reference. --- Jint/Runtime/Interop/DelegateWrapper.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jint/Runtime/Interop/DelegateWrapper.cs b/Jint/Runtime/Interop/DelegateWrapper.cs index 59a80a3f77..4990016b32 100644 --- a/Jint/Runtime/Interop/DelegateWrapper.cs +++ b/Jint/Runtime/Interop/DelegateWrapper.cs @@ -1,9 +1,9 @@ using System; using System.Globalization; using System.Reflection; -using System.Linq; using Jint.Native; using Jint.Native.Function; +using System.Linq; namespace Jint.Runtime.Interop {