Truly independent web browser
LibJS: Tell the host when an await handles a rejected promise
Shannon Boothe70c7450b36a
diff --git a/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs b/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rsindex 59e793df40c..daea918b8a6 100644--- a/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs+++ b/Libraries/LibJS/Rust/src/runtime/async_function_driver_wrapper.rs@@ -167,7 +167,7 @@ impl AsyncFunctionDriverWrapper {// Already-settled native Promises whose constructor is the intrinsic %Promise%.if is_settled_intrinsic_promise(vm, realm, &promise) {Self::schedule_resume(this, vm, promise.result(), promise.state() == PromiseState::Fulfilled);- promise.set_is_handled();+ promise.mark_handled(vm);return Ok(());}}@@ -304,7 +304,7 @@ impl AsyncFunctionDriverWrapper {&& is_settled_intrinsic_promise(vm, realm, &promise){let is_fulfilled = promise.state() == PromiseState::Fulfilled;- promise.set_is_handled();+ promise.mark_handled(vm);generator_result = if is_fulfilled {generator_object.resume(vm, promise.result(), None)} else {diff --git a/Libraries/LibJS/Rust/src/runtime/module.rs b/Libraries/LibJS/Rust/src/runtime/module.rsindex 14d9969bc72..e93be3cd533 100644--- a/Libraries/LibJS/Rust/src/runtime/module.rs+++ b/Libraries/LibJS/Rust/src/runtime/module.rs@@ -34,7 +34,7 @@ use crate::runtime::module_environment::ModuleEnvironment;use crate::runtime::module_loading::{ImportedModulePayload, ImportedModuleReferrer};use crate::runtime::module_namespace_object::ModuleNamespaceObject;use crate::runtime::module_request::{LoadedModuleRequest, ModuleRequest, module_requests_equal};-use crate::runtime::promise::{Promise, PromiseState, RejectionOperation};+use crate::runtime::promise::{Promise, PromiseState};use crate::runtime::promise_capability::PromiseCapability;use crate::runtime::source_text_module::SOURCE_TEXT_MODULE_METHODS;use crate::runtime::synthetic_module::SYNTHETIC_MODULE_METHODS;@@ -351,12 +351,8 @@ impl Module {// 4. If promise.[[PromiseState]] is REJECTED, thenif promise.state() == PromiseState::Rejected {// a. If promise.[[PromiseIsHandled]] is false, perform HostPromiseRejectionTracker(promise, "handle").- if !promise.is_handled() {- vm.host_promise_rejection_tracker()(vm, promise, RejectionOperation::Handle);- }-// b. Set promise.[[PromiseIsHandled]] to true.- promise.set_is_handled();+ promise.mark_handled(vm);// c. Return ThrowCompletion(promise.[[PromiseResult]]).crate::embedding::completion::log_exception_if_enabled(vm, promise.result());diff --git a/Libraries/LibJS/Rust/src/runtime/promise.rs b/Libraries/LibJS/Rust/src/runtime/promise.rsindex 947a74a58b2..ffc1009c7e4 100644--- a/Libraries/LibJS/Rust/src/runtime/promise.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise.rs@@ -175,6 +175,15 @@ impl Promise {self.is_handled.set(true);}+ /// Sets [[PromiseIsHandled]] to true, first telling the host when that handles a rejection, as PerformPromiseThen+ /// does when it adds a reaction to a settled promise.+ pub fn mark_handled(&self, vm: &Vm) {+ if self.state() == PromiseState::Rejected && !self.is_handled() {+ vm.host_promise_rejection_tracker()(vm, self.as_gc(), RejectionOperation::Handle);+ }+ self.set_is_handled();+ }+fn is_settled(&self) -> bool {matches!(self.state.get(), PromiseState::Fulfilled | PromiseState::Rejected)}diff --git a/Libraries/LibJS/Rust/src/runtime/promise_constructor.rs b/Libraries/LibJS/Rust/src/runtime/promise_constructor.rsindex d28e0367139..e32c36e2f0d 100644--- a/Libraries/LibJS/Rust/src/runtime/promise_constructor.rs+++ b/Libraries/LibJS/Rust/src/runtime/promise_constructor.rs@@ -26,7 +26,7 @@ use crate::runtime::function_object::FunctionObject;use crate::runtime::iterator::{IteratorHint, IteratorRecord, get_iterator, iterator_close, iterator_step_value};use crate::runtime::job_callback::JobCallback;use crate::runtime::native_function::{NativeFunction, define_native_function_class, raw_native};-use crate::runtime::promise::{Promise, PromiseState, RejectionOperation, promise_resolve};+use crate::runtime::promise::{Promise, PromiseState, promise_resolve};use crate::runtime::promise_capability::{PromiseCapability, new_promise_capability, try_or_reject};use crate::runtime::promise_prototype::PromisePrototype;use crate::runtime::promise_resolving_element_functions::{@@ -213,16 +213,13 @@ fn invoke_promise_all_then(if promise.state() == PromiseState::Rejected {let reason = promise.result();- if !promise.is_handled() {- vm.host_promise_rejection_tracker()(vm, promise, RejectionOperation::Handle);- }+ promise.mark_handled(vm);let reject = result_capability.reject();let job = create_heap_function(vm, (reject, reason), |vm, &(reject, reason)| {call_function_object(vm, reject, Value::UNDEFINED, &[reason])});vm.host_enqueue_promise_job()(vm, job, Some(realm));- promise.set_is_handled();return Ok(Value::UNDEFINED);}diff --git a/Tests/LibWeb/Text/expected/await-reported-rejected-promise-fires-rejectionhandled.txt b/Tests/LibWeb/Text/expected/await-reported-rejected-promise-fires-rejectionhandled.txtnew file mode 100644index 00000000000..c6d3a391d5d--- /dev/null+++ b/Tests/LibWeb/Text/expected/await-reported-rejected-promise-fires-rejectionhandled.txt@@ -0,0 +1,4 @@+unhandledrejection: then+unhandledrejection: await+rejectionhandled: then+rejectionhandled: awaitdiff --git a/Tests/LibWeb/Text/input/await-reported-rejected-promise-fires-rejectionhandled.html b/Tests/LibWeb/Text/input/await-reported-rejected-promise-fires-rejectionhandled.htmlnew file mode 100644index 00000000000..207f006c2d4--- /dev/null+++ b/Tests/LibWeb/Text/input/await-reported-rejected-promise-fires-rejectionhandled.html@@ -0,0 +1,29 @@+<!DOCTYPE html>+<script src="include.js"></script>+<script>+ asyncTest(async done => {+ const events = [];+ window.addEventListener("unhandledrejection", event => {+ event.preventDefault();+ events.push(`unhandledrejection: ${event.reason}`);+ });+ window.addEventListener("rejectionhandled", event => {+ events.push(`rejectionhandled: ${event.reason}`);+ });++ const handledByThen = Promise.reject("then");+ const handledByAwait = Promise.reject("await");+ await waitForCondition(() => events.length === 2);++ handledByThen.catch(() => {});+ (async () => {+ try {+ await handledByAwait;+ } catch {}+ })();+ await waitForCondition(() => events.length === 4, 40);++ events.forEach(println);+ done();+ });+</script>