Skip to content

Commit 3456bac

Browse files
pavelsavarasteveharter
authored andcommitted
[browser] fix marshaling reject(null) (dotnet#102549)
1 parent 43b5fab commit 3456bac

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

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

+21
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,27 @@ public unsafe void OutOfRange()
127127
Assert.Contains("Overflow: value 9007199254740991 is out of -2147483648 2147483647 range", ex.Message);
128128
}
129129

130+
[Fact]
131+
public async Task RejectString()
132+
{
133+
var ex = await Assert.ThrowsAsync<JSException>(() => JavaScriptTestHelper.Reject("noodles"));
134+
Assert.Contains("noodles", ex.Message);
135+
}
136+
137+
[Fact]
138+
public async Task RejectException()
139+
{
140+
var expected = new Exception("noodles");
141+
var actual = await Assert.ThrowsAsync<Exception>(() => JavaScriptTestHelper.Reject(expected));
142+
Assert.Equal(expected, actual);
143+
}
144+
145+
[Fact]
146+
public async Task RejectNull()
147+
{
148+
var ex = await Assert.ThrowsAsync<JSException>(() => JavaScriptTestHelper.Reject(null));
149+
}
150+
130151
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))]
131152
public unsafe void OptimizedPaths()
132153
{

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

+3
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public static void ConsoleWriteLine([JSMarshalAs<JSType.String>] string message)
3737
[JSImport("delay", "JavaScriptTestHelper")]
3838
public static partial Task Delay(int ms);
3939

40+
[JSImport("reject", "JavaScriptTestHelper")]
41+
public static partial Task Reject([JSMarshalAs<JSType.Any>] object what);
42+
4043
[JSImport("intentionallyMissingImport", "JavaScriptTestHelper")]
4144
public static partial void IntentionallyMissingImport();
4245

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

+4
Original file line numberDiff line numberDiff line change
@@ -448,3 +448,7 @@ export async function setup() {
448448
export function delay(ms) {
449449
return new Promise(resolve => globalThis.setTimeout(resolve, ms));
450450
}
451+
452+
export function reject(what) {
453+
return new Promise((_, reject) => globalThis.setTimeout(() => reject(what), 0));
454+
}

src/mono/browser/runtime/cancelable-promise.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,12 @@ export class PromiseHolder extends ManagedObject {
111111
mono_log_debug("This promise rejection can't be propagated to managed code, mono runtime already exited.");
112112
return;
113113
}
114+
if (!reason) {
115+
reason = new Error() as any;
116+
}
114117
mono_assert(!this.isResolved, "reject could be called only once");
115118
mono_assert(!this.isDisposed, "resolve is already disposed.");
116-
const isCancelation = reason && reason[promise_holder_symbol] === this;
119+
const isCancelation = reason[promise_holder_symbol] === this;
117120
if (WasmEnableThreads && !isCancelation && !this.setIsResolving()) {
118121
// we know that cancelation is in flight
119122
// because we need to keep the GCHandle alive until until the cancelation arrives

0 commit comments

Comments
 (0)