Skip to content

Commit

Permalink
[browser] fix marshaling reject(null) (#102549)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelsavara committed May 22, 2024
1 parent 4157348 commit 994a410
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ public unsafe void OutOfRange()
Assert.Contains("Overflow: value 9007199254740991 is out of -2147483648 2147483647 range", ex.Message);
}

[Fact]
public async Task RejectString()
{
var ex = await Assert.ThrowsAsync<JSException>(() => JavaScriptTestHelper.Reject("noodles"));
Assert.Contains("noodles", ex.Message);
}

[Fact]
public async Task RejectException()
{
var expected = new Exception("noodles");
var actual = await Assert.ThrowsAsync<Exception>(() => JavaScriptTestHelper.Reject(expected));
Assert.Equal(expected, actual);
}

[Fact]
public async Task RejectNull()
{
var ex = await Assert.ThrowsAsync<JSException>(() => JavaScriptTestHelper.Reject(null));
}

[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotWasmThreadingSupported))]
public unsafe void OptimizedPaths()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public static void ConsoleWriteLine([JSMarshalAs<JSType.String>] string message)
[JSImport("delay", "JavaScriptTestHelper")]
public static partial Task Delay(int ms);

[JSImport("reject", "JavaScriptTestHelper")]
public static partial Task Reject([JSMarshalAs<JSType.Any>] object what);

[JSImport("intentionallyMissingImport", "JavaScriptTestHelper")]
public static partial void IntentionallyMissingImport();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,3 +448,7 @@ export async function setup() {
export function delay(ms) {
return new Promise(resolve => globalThis.setTimeout(resolve, ms));
}

export function reject(what) {
return new Promise((_, reject) => globalThis.setTimeout(() => reject(what), 0));
}
5 changes: 4 additions & 1 deletion src/mono/browser/runtime/cancelable-promise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,12 @@ export class PromiseHolder extends ManagedObject {
mono_log_debug("This promise rejection can't be propagated to managed code, mono runtime already exited.");
return;
}
if (!reason) {
reason = new Error() as any;
}
mono_assert(!this.isResolved, "reject could be called only once");
mono_assert(!this.isDisposed, "resolve is already disposed.");
const isCancelation = reason && reason[promise_holder_symbol] === this;
const isCancelation = reason[promise_holder_symbol] === this;
if (WasmEnableThreads && !isCancelation && !this.setIsResolving()) {
// we know that cancelation is in flight
// because we need to keep the GCHandle alive until until the cancelation arrives
Expand Down

0 comments on commit 994a410

Please sign in to comment.