diff --git a/src/libraries/System.Private.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Runtime.cs b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Runtime.cs
index 57a2cac48c6188..4c2a55c745810c 100644
--- a/src/libraries/System.Private.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Runtime.cs
+++ b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/src/System/Runtime/InteropServices/JavaScript/Runtime.cs
@@ -262,12 +262,12 @@ private struct IntPtrAndHandle
internal RuntimeMethodHandle handle;
}
- public static string GetCallSignature(IntPtr methodHandle)
+ public static string GetCallSignature(IntPtr methodHandle, object objForRuntimeType)
{
IntPtrAndHandle tmp = default(IntPtrAndHandle);
tmp.ptr = methodHandle;
- MethodBase? mb = MethodBase.GetMethodFromHandle(tmp.handle);
+ MethodBase? mb = objForRuntimeType == null ? MethodBase.GetMethodFromHandle(tmp.handle) : MethodBase.GetMethodFromHandle(tmp.handle, Type.GetTypeHandle(objForRuntimeType));
if (mb == null)
return string.Empty;
diff --git a/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System.Private.Runtime.InteropServices.JavaScript.Tests.csproj b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System.Private.Runtime.InteropServices.JavaScript.Tests.csproj
index 11dec891deeeb0..e97ca510a53848 100644
--- a/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System.Private.Runtime.InteropServices.JavaScript.Tests.csproj
+++ b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System.Private.Runtime.InteropServices.JavaScript.Tests.csproj
@@ -12,8 +12,9 @@
+
-
+
diff --git a/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/DelegateTests.cs b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/DelegateTests.cs
new file mode 100644
index 00000000000000..2414a6ca17ccd4
--- /dev/null
+++ b/src/libraries/System.Private.Runtime.InteropServices.JavaScript/tests/System/Runtime/InteropServices/JavaScript/DelegateTests.cs
@@ -0,0 +1,199 @@
+// Licensed to the .NET Foundation under one or more agreements.
+// The .NET Foundation licenses this file to you under the MIT license.
+
+using System.Runtime.InteropServices.JavaScript;
+using System.Collections.Generic;
+using Xunit;
+
+namespace System.Runtime.InteropServices.JavaScript.Tests
+{
+ public static class DelegateTests
+ {
+ private static Function _objectPrototype;
+
+ [Fact]
+ public static void InvokeFunction()
+ {
+ HelperMarshal._functionResultValue = 0;
+ HelperMarshal._i32Value = 0;
+
+ Runtime.InvokeJS(@"
+ var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
+ var res = funcDelegate (10, 20);
+ App.call_test_method (""InvokeI32"", [ res, res ]);
+ ");
+
+ Assert.Equal(30, HelperMarshal._functionResultValue);
+ Assert.Equal(60, HelperMarshal._i32Value);
+ }
+
+ [Fact]
+ public static void InvokeFunctionInLoopUsingConstanceValues()
+ {
+ HelperMarshal._functionResultValue = 0;
+ HelperMarshal._i32Value = 0;
+
+ Runtime.InvokeJS(@"
+ var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
+ var res = funcDelegate (10, 20);
+ for (x = 0; x < 1000; x++)
+ {
+ res = funcDelegate (10, 20);
+ }
+ App.call_test_method (""InvokeI32"", [ res, res ]);
+ ");
+
+ Assert.Equal(30, HelperMarshal._functionResultValue);
+ Assert.Equal(60, HelperMarshal._i32Value);
+ }
+
+ [Fact]
+ public static void InvokeFunctionInLoopUsingIncrementedValues()
+ {
+ HelperMarshal._functionResultValue = 0;
+ HelperMarshal._i32Value = 0;
+ Runtime.InvokeJS(@"
+ var funcDelegate = App.call_test_method (""CreateFunctionDelegate"", [ ]);
+ var res = funcDelegate (10, 20);
+ for (x = 0; x < 1000; x++)
+ {
+ res = funcDelegate (x, x);
+ }
+ App.call_test_method (""InvokeI32"", [ res, res ]);
+ ");
+
+ Assert.Equal(1998, HelperMarshal._functionResultValue);
+ Assert.Equal(3996, HelperMarshal._i32Value);
+ }
+
+ [Fact]
+ public static void InvokeActionTReturnedByInvokingFuncT()
+ {
+ HelperMarshal._functionActionResultValue = 0;
+ HelperMarshal._functionActionResultValueOfAction = 0;
+
+ Runtime.InvokeJS(@"
+ var funcDelegate = App.call_test_method (""CreateFunctionDelegateWithAction"", [ ]);
+ var actionDelegate = funcDelegate (10, 20);
+ actionDelegate(30,40);
+ ");
+
+ Assert.Equal(30, HelperMarshal._functionActionResultValue);
+ Assert.Equal(70, HelperMarshal._functionActionResultValueOfAction);
+ }
+
+ [Fact]
+ public static void InvokeActionIntInt()
+ {
+ HelperMarshal._actionResultValue = 0;
+
+ Runtime.InvokeJS(@"
+ var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
+ actionDelegate(30,40);
+ ");
+
+ Assert.Equal(70, HelperMarshal._actionResultValue);
+ }
+
+ [Fact]
+ public static void InvokeActionFloatIntToIntInt()
+ {
+ HelperMarshal._actionResultValue = 0;
+ Runtime.InvokeJS(@"
+ var actionDelegate = App.call_test_method (""CreateActionDelegate"", [ ]);
+ actionDelegate(3.14,40);
+ ");
+
+ Assert.Equal(43, HelperMarshal._actionResultValue);
+ }
+
+ [Fact]
+ public static void InvokeDelegateMethod()
+ {
+ HelperMarshal._delMethodResultValue = string.Empty;
+ Runtime.InvokeJS(@"
+ var del = App.call_test_method (""CreateDelegateMethod"", [ ]);
+ del(""Hic sunt dracones"");
+ ");
+
+ Assert.Equal("Hic sunt dracones", HelperMarshal._delMethodResultValue);
+ }
+
+ [Fact]
+ public static void InvokeDelegateMethodReturnString()
+ {
+ HelperMarshal._delMethodStringResultValue = string.Empty;
+ Runtime.InvokeJS(@"
+ var del = App.call_test_method (""CreateDelegateMethodReturnString"", [ ]);
+ var res = del(""Hic sunt dracones"");
+ App.call_test_method (""SetTestString1"", [ res ]);
+ ");
+
+ Assert.Equal("Received: Hic sunt dracones", HelperMarshal._delMethodStringResultValue);
+ }
+
+ [Theory]
+ [InlineData("CreateCustomMultiCastDelegate_VoidString", "Moin")]
+ [InlineData("CreateMultiCastAction_VoidString", "MoinMoin")]
+ public static void InvokeMultiCastDelegate_VoidString(string creator, string testStr)
+ {
+ HelperMarshal._delegateCallResult = string.Empty;
+ Runtime.InvokeJS($@"
+ var del = App.call_test_method (""{creator}"", [ ]);
+ del(""{testStr}"");
+ ");
+ Assert.Equal($" Hello, {testStr}! GoodMorning, {testStr}!", HelperMarshal._delegateCallResult);
+ }
+
+ [Theory]
+ [InlineData("CreateDelegateFromAnonymousMethod_VoidString")]
+ [InlineData("CreateDelegateFromLambda_VoidString")]
+ [InlineData("CreateDelegateFromMethod_VoidString")]
+ [InlineData("CreateActionT_VoidString")]
+ public static void InvokeDelegate_VoidString(string creator)
+ {
+ HelperMarshal._delegateCallResult = string.Empty;
+ var s = Runtime.InvokeJS($@"
+ var del = App.call_test_method (""{creator}"", [ ]);
+ del(""Hic sunt dracones"");
+ ");
+
+ Assert.Equal("Notification received for: Hic sunt dracones", HelperMarshal._delegateCallResult);
+ }
+
+ public static IEnumerable