From 1a5bf15b4da25e4b9362519439ed1bc2b021bc24 Mon Sep 17 00:00:00 2001 From: Luke Wilde Date: Fri, 10 Dec 2021 19:48:51 +0000 Subject: LibWeb: Implement AbortSignal.throwIfAborted See: https://github.com/whatwg/dom/commit/cfe2f1e --- Userland/Libraries/LibWeb/DOM/AbortSignal.cpp | 16 ++++++++++++++++ Userland/Libraries/LibWeb/DOM/AbortSignal.h | 2 ++ Userland/Libraries/LibWeb/DOM/AbortSignal.idl | 1 + 3 files changed, 19 insertions(+) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp b/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp index f86bd74c57..1cb2e23cc7 100644 --- a/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp +++ b/Userland/Libraries/LibWeb/DOM/AbortSignal.cpp @@ -73,6 +73,22 @@ HTML::EventHandler AbortSignal::onabort() return event_handler_attribute(HTML::EventNames::abort); } +// https://dom.spec.whatwg.org/#dom-abortsignal-throwifaborted +JS::ThrowCompletionOr AbortSignal::throw_if_aborted() const +{ + auto& global_object = wrapper()->global_object(); + auto& vm = global_object.vm(); + + // The throwIfAborted() method steps are to throw this’s abort reason, if this is aborted. + if (!aborted()) + return {}; + + // FIXME: Remove this once VM::exception() has been removed. + vm.throw_exception(global_object, m_abort_reason); + + return JS::throw_completion(m_abort_reason); +} + void AbortSignal::visit_edges(JS::Cell::Visitor& visitor) { visitor.visit(m_abort_reason); diff --git a/Userland/Libraries/LibWeb/DOM/AbortSignal.h b/Userland/Libraries/LibWeb/DOM/AbortSignal.h index fbbb83bb5c..300c7758bd 100644 --- a/Userland/Libraries/LibWeb/DOM/AbortSignal.h +++ b/Userland/Libraries/LibWeb/DOM/AbortSignal.h @@ -53,6 +53,8 @@ public: // https://dom.spec.whatwg.org/#dom-abortsignal-reason JS::Value reason() const { return m_abort_reason; } + JS::ThrowCompletionOr throw_if_aborted() const; + void visit_edges(JS::Cell::Visitor&); // ^EventTarget diff --git a/Userland/Libraries/LibWeb/DOM/AbortSignal.idl b/Userland/Libraries/LibWeb/DOM/AbortSignal.idl index d28ef8a5d5..902626de92 100644 --- a/Userland/Libraries/LibWeb/DOM/AbortSignal.idl +++ b/Userland/Libraries/LibWeb/DOM/AbortSignal.idl @@ -4,6 +4,7 @@ interface AbortSignal : EventTarget { readonly attribute boolean aborted; readonly attribute any reason; + undefined throwIfAborted(); attribute EventHandler onabort; }; -- cgit v1.2.3