From b65e78a3eb741bf6b8b3292b4cf7f7d4c490922d Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Wed, 4 Jan 2023 13:32:00 -0800 Subject: [PATCH] [wasm] fix marshaling Error to C# as JSType.Any (#79352) Co-authored-by: pavelsavara --- .../InteropServices/JavaScript/JSImportExportTest.cs | 8 ++++++++ .../InteropServices/JavaScript/JavaScriptTestHelper.cs | 4 ++++ .../InteropServices/JavaScript/JavaScriptTestHelper.mjs | 4 ++++ src/mono/wasm/runtime/marshal-to-cs.ts | 4 +--- 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs index 79e9e3e0cecf5..9fbac261b76eb 100644 --- a/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs +++ b/src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs @@ -1363,6 +1363,14 @@ public void JsExportThrows() Assert.Contains("-t-e-s-t-", ex.Message); } + [Fact] + public void JSImportReturnError() + { + var err = JavaScriptTestHelper.returnError() as Exception; + Assert.NotNull(err); + Assert.Contains("this-is-error", err.Message); + } + [Fact] public void JsExportCatchToString() { 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 67fe77cfe5fe8..4f06fc8f4a42e 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 @@ -67,6 +67,10 @@ public static DateTime Now() [return: JSMarshalAs] internal static partial void throw0(); + [JSImport("returnError", "JavaScriptTestHelper")] + [return: JSMarshalAs] + internal static partial object returnError(); + [JSImport("echo1", "JavaScriptTestHelper")] [return: JSMarshalAs>] internal static partial Task echo1_Task([JSMarshalAs>] Task arg1); 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 0811be477c64f..927a5194c561f 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 @@ -110,6 +110,10 @@ export function throw0fn() { throw new Error('throw-0-msg'); } +export function returnError() { + return new Error('this-is-error'); +} + export function catch1toString(message, functionName) { const JavaScriptTestHelper = dllExports.System.Runtime.InteropServices.JavaScript.Tests.JavaScriptTestHelper; const fn = JavaScriptTestHelper[functionName]; diff --git a/src/mono/wasm/runtime/marshal-to-cs.ts b/src/mono/wasm/runtime/marshal-to-cs.ts index e7cd7cf7e0234..93943bcd4c246 100644 --- a/src/mono/wasm/runtime/marshal-to-cs.ts +++ b/src/mono/wasm/runtime/marshal-to-cs.ts @@ -449,9 +449,7 @@ function _marshal_cs_object_to_cs(arg: JSMarshalerArgument, value: any): void { set_arg_date(arg, value); } else if (value instanceof Error) { - set_arg_type(arg, MarshalerType.JSException); - const js_handle = mono_wasm_get_js_handle(value); - set_js_handle(arg, js_handle); + marshal_exception_to_cs(arg, value); } else if (value instanceof Uint8Array) { marshal_array_to_cs_impl(arg, value, MarshalerType.Byte);