Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[browser] fix marshaling reject(null) #102549

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading