Skip to content

Commit b65e78a

Browse files
[wasm] fix marshaling Error to C# as JSType.Any (#79352)
Co-authored-by: pavelsavara <[email protected]>
1 parent d5f6805 commit b65e78a

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JSImportExportTest.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1363,6 +1363,14 @@ public void JsExportThrows()
13631363
Assert.Contains("-t-e-s-t-", ex.Message);
13641364
}
13651365

1366+
[Fact]
1367+
public void JSImportReturnError()
1368+
{
1369+
var err = JavaScriptTestHelper.returnError() as Exception;
1370+
Assert.NotNull(err);
1371+
Assert.Contains("this-is-error", err.Message);
1372+
}
1373+
13661374
[Fact]
13671375
public void JsExportCatchToString()
13681376
{

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ public static DateTime Now()
6767
[return: JSMarshalAs<JSType.Discard>]
6868
internal static partial void throw0();
6969

70+
[JSImport("returnError", "JavaScriptTestHelper")]
71+
[return: JSMarshalAs<JSType.Any>]
72+
internal static partial object returnError();
73+
7074
[JSImport("echo1", "JavaScriptTestHelper")]
7175
[return: JSMarshalAs<JSType.Promise<JSType.Void>>]
7276
internal static partial Task echo1_Task([JSMarshalAs<JSType.Promise<JSType.Void>>] Task arg1);

src/libraries/System.Runtime.InteropServices.JavaScript/tests/System.Runtime.InteropServices.JavaScript.UnitTests/System/Runtime/InteropServices/JavaScript/JavaScriptTestHelper.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ export function throw0fn() {
110110
throw new Error('throw-0-msg');
111111
}
112112

113+
export function returnError() {
114+
return new Error('this-is-error');
115+
}
116+
113117
export function catch1toString(message, functionName) {
114118
const JavaScriptTestHelper = dllExports.System.Runtime.InteropServices.JavaScript.Tests.JavaScriptTestHelper;
115119
const fn = JavaScriptTestHelper[functionName];

src/mono/wasm/runtime/marshal-to-cs.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,7 @@ function _marshal_cs_object_to_cs(arg: JSMarshalerArgument, value: any): void {
449449
set_arg_date(arg, value);
450450
}
451451
else if (value instanceof Error) {
452-
set_arg_type(arg, MarshalerType.JSException);
453-
const js_handle = mono_wasm_get_js_handle(value);
454-
set_js_handle(arg, js_handle);
452+
marshal_exception_to_cs(arg, value);
455453
}
456454
else if (value instanceof Uint8Array) {
457455
marshal_array_to_cs_impl(arg, value, MarshalerType.Byte);

0 commit comments

Comments
 (0)