From 55d2adab3aa7e3fb764a44bf5661cffd21936596 Mon Sep 17 00:00:00 2001 From: Max Katz Date: Sat, 27 Apr 2024 12:22:16 -0700 Subject: [PATCH] [WASM] Fix third parameter marshaling on callbacks (#101638) * Fix typo with get_signature_arg3_type incrorrecty arg2 instead of arg3 get_signature_arg3_type method is expected to read argument type by Arg3MarshalerType (=28) offset instead of Arg2MarshalerType (=24) * Add another callback tests to cover different parameter types --- .../InteropServices/JavaScript/JSImportTest.cs | 17 +++++++++++++++++ .../JavaScript/JavaScriptTestHelper.cs | 2 ++ .../JavaScript/JavaScriptTestHelper.mjs | 17 ++++++++++++++++- src/mono/browser/runtime/marshal.ts | 2 +- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs index b4c220519da09..632b27b105c94 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportTest.cs @@ -1337,6 +1337,23 @@ public void JsImportCallback_ActionIntLong() Assert.Equal(43, calledB); } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] + public void JsImportCallback_ActionIntLongDouble() + { + int calledA = -1; + long calledB = -1; + double calledC = -1; + JavaScriptTestHelper.back4_ActionIntLongDouble((a, b, c) => + { + calledA = a; + calledB = b; + calledC = c; + }, 42, 43, 44.5); + Assert.Equal(42, calledA); + Assert.Equal(43, calledB); + Assert.Equal(44.5, calledC); + } + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))] public void JsImportCallback_ActionIntThrow() { diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs index b2127558a1520..c64be2635a56e 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs @@ -493,6 +493,8 @@ public static async Task AwaitTaskOfObject([JSMarshalAs] internal static partial int back3_FunctionIntInt([JSMarshalAs>] Func? fun, [JSMarshalAs] int a); + [JSImport("back4", "JavaScriptTestHelper")] + internal static partial void back4_ActionIntLongDouble([JSMarshalAs>] Action? action, [JSMarshalAs] int a, [JSMarshalAs] long b, [JSMarshalAs] double c); [JSImport("invoke1", "JavaScriptTestHelper")] [return: JSMarshalAs>] diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs index e0cf11376d8d3..9d4851575bf34 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs @@ -383,6 +383,21 @@ export function back3(arg1, arg2, arg3) { } } +export function back4(arg1, arg2, arg3, arg4) { + if (globalThis.gc) { + // console.log('globalThis.gc'); + globalThis.gc(); + } + try { + if (!(arg1 instanceof Function)) throw new Error('expecting Function!') + + return arg1(arg2, arg3, arg4); + } + catch (ex) { + throw ex; + } +} + export function backback(arg1, arg2, arg3) { if (globalThis.gc) { // console.log('globalThis.gc'); @@ -432,4 +447,4 @@ export async function setup() { export function delay(ms) { return new Promise(resolve => globalThis.setTimeout(resolve, ms)); -} \ No newline at end of file +} diff --git a/src/mono/browser/runtime/marshal.ts b/src/mono/browser/runtime/marshal.ts index e49387cc51eef..b26a94926f88e 100644 --- a/src/mono/browser/runtime/marshal.ts +++ b/src/mono/browser/runtime/marshal.ts @@ -143,7 +143,7 @@ export function get_signature_arg2_type (sig: JSMarshalerType): MarshalerType { export function get_signature_arg3_type (sig: JSMarshalerType): MarshalerType { mono_assert(sig, "Null sig"); - return getU8(sig + JSBindingTypeOffsets.Arg2MarshalerType); + return getU8(sig + JSBindingTypeOffsets.Arg3MarshalerType); } export function get_signature_argument_count (signature: JSFunctionSignature): number {