diff options
author | networkException <git@nwex.de> | 2023-02-13 11:58:39 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2023-02-13 12:11:32 +0000 |
commit | f1dd4f42bcd0cf5e9f7fd4c0ffb1d06789a29631 (patch) | |
tree | 3954082f76e22b513963677fa654c699be1d35a2 /Userland/Libraries/LibWeb | |
parent | 863afbaf38a132e50b23584b44c3d0c41cbfc157 (diff) | |
download | serenity-f1dd4f42bcd0cf5e9f7fd4c0ffb1d06789a29631.zip |
LibWeb: Clarify WebIDL::Promise as an alias for JS::PromiseCapability
This patch adds the WebIDL::Promise type explicitly defined in the
WebIDL spec to be a PromiseCapability Record from ecma262.
Diffstat (limited to 'Userland/Libraries/LibWeb')
6 files changed, 38 insertions, 32 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp b/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp index bd910b3965..61e3bb0059 100644 --- a/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp +++ b/Userland/Libraries/LibWeb/Fetch/FetchMethod.cpp @@ -151,13 +151,13 @@ JS::NonnullGCPtr<JS::Promise> fetch_impl(JS::VM& vm, RequestInfo const& input, R } // https://fetch.spec.whatwg.org/#abort-fetch -void abort_fetch(JS::VM& vm, JS::PromiseCapability const& promise_capability, JS::NonnullGCPtr<Infrastructure::Request> request, JS::GCPtr<Response> response_object, JS::Value error) +void abort_fetch(JS::VM& vm, WebIDL::Promise const& promise, JS::NonnullGCPtr<Infrastructure::Request> request, JS::GCPtr<Response> response_object, JS::Value error) { dbgln_if(WEB_FETCH_DEBUG, "Fetch: Aborting fetch with: request @ {}, error = {}", request.ptr(), error); // 1. Reject promise with error. // NOTE: This is a no-op if promise has already fulfilled. - WebIDL::reject_promise(vm, promise_capability, error); + WebIDL::reject_promise(vm, promise, error); // 2. If request’s body is non-null and is readable, then cancel request’s body with error. if (auto* body = request->body().get_pointer<Infrastructure::Body>(); body != nullptr && body->stream()->is_readable()) { diff --git a/Userland/Libraries/LibWeb/Fetch/FetchMethod.h b/Userland/Libraries/LibWeb/Fetch/FetchMethod.h index 8369b14de9..7f0b31e306 100644 --- a/Userland/Libraries/LibWeb/Fetch/FetchMethod.h +++ b/Userland/Libraries/LibWeb/Fetch/FetchMethod.h @@ -10,10 +10,11 @@ #include <LibJS/Forward.h> #include <LibJS/Heap/GCPtr.h> #include <LibWeb/Fetch/Request.h> +#include <LibWeb/WebIDL/Promise.h> namespace Web::Fetch { JS::NonnullGCPtr<JS::Promise> fetch_impl(JS::VM&, RequestInfo const& input, RequestInit const& init = {}); -void abort_fetch(JS::VM&, JS::PromiseCapability const&, JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Response>, JS::Value error); +void abort_fetch(JS::VM&, WebIDL::Promise const&, JS::NonnullGCPtr<Infrastructure::Request>, JS::GCPtr<Response>, JS::Value error); } diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp index 4282d35bda..15ce59f037 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.cpp @@ -41,7 +41,7 @@ WebIDL::ExceptionOr<Body> Body::clone() const } // https://fetch.spec.whatwg.org/#fully-reading-body-as-promise -JS::NonnullGCPtr<JS::PromiseCapability> Body::fully_read_as_promise() const +JS::NonnullGCPtr<WebIDL::Promise> Body::fully_read_as_promise() const { auto& vm = Bindings::main_thread_vm(); auto& realm = *vm.current_realm(); diff --git a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h index e7bc50d198..a8787f0aa7 100644 --- a/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h +++ b/Userland/Libraries/LibWeb/Fetch/Infrastructure/HTTP/Bodies.h @@ -15,6 +15,7 @@ #include <LibJS/Heap/Handle.h> #include <LibWeb/FileAPI/Blob.h> #include <LibWeb/Streams/ReadableStream.h> +#include <LibWeb/WebIDL/Promise.h> namespace Web::Fetch::Infrastructure { @@ -32,7 +33,7 @@ public: [[nodiscard]] WebIDL::ExceptionOr<Body> clone() const; - [[nodiscard]] JS::NonnullGCPtr<JS::PromiseCapability> fully_read_as_promise() const; + [[nodiscard]] JS::NonnullGCPtr<WebIDL::Promise> fully_read_as_promise() const; private: // https://fetch.spec.whatwg.org/#concept-body-stream diff --git a/Userland/Libraries/LibWeb/WebIDL/Promise.cpp b/Userland/Libraries/LibWeb/WebIDL/Promise.cpp index 4a8c74a2ba..436c03cfbe 100644 --- a/Userland/Libraries/LibWeb/WebIDL/Promise.cpp +++ b/Userland/Libraries/LibWeb/WebIDL/Promise.cpp @@ -16,7 +16,7 @@ namespace Web::WebIDL { // https://webidl.spec.whatwg.org/#a-new-promise -JS::NonnullGCPtr<JS::PromiseCapability> create_promise(JS::Realm& realm) +JS::NonnullGCPtr<Promise> create_promise(JS::Realm& realm) { auto& vm = realm.vm(); @@ -29,7 +29,7 @@ JS::NonnullGCPtr<JS::PromiseCapability> create_promise(JS::Realm& realm) } // https://webidl.spec.whatwg.org/#a-promise-resolved-with -JS::NonnullGCPtr<JS::PromiseCapability> create_resolved_promise(JS::Realm& realm, JS::Value value) +JS::NonnullGCPtr<Promise> create_resolved_promise(JS::Realm& realm, JS::Value value) { auto& vm = realm.vm(); @@ -50,7 +50,7 @@ JS::NonnullGCPtr<JS::PromiseCapability> create_resolved_promise(JS::Realm& realm } // https://webidl.spec.whatwg.org/#a-promise-rejected-with -JS::NonnullGCPtr<JS::PromiseCapability> create_rejected_promise(JS::Realm& realm, JS::Value reason) +JS::NonnullGCPtr<Promise> create_rejected_promise(JS::Realm& realm, JS::Value reason) { auto& vm = realm.vm(); @@ -69,7 +69,7 @@ JS::NonnullGCPtr<JS::PromiseCapability> create_rejected_promise(JS::Realm& realm } // https://webidl.spec.whatwg.org/#resolve -void resolve_promise(JS::VM& vm, JS::PromiseCapability const& promise_capability, JS::Value value) +void resolve_promise(JS::VM& vm, Promise const& promise, JS::Value value) { // 1. If x is not given, then let it be the undefined value. // NOTE: This is done via the default argument. @@ -77,20 +77,20 @@ void resolve_promise(JS::VM& vm, JS::PromiseCapability const& promise_capability // 2. Let value be the result of converting x to an ECMAScript value. // 3. Perform ! Call(p.[[Resolve]], undefined, « value »). - MUST(JS::call(vm, *promise_capability.resolve(), JS::js_undefined(), value)); + MUST(JS::call(vm, *promise.resolve(), JS::js_undefined(), value)); } // https://webidl.spec.whatwg.org/#reject -void reject_promise(JS::VM& vm, JS::PromiseCapability const& promise_capability, JS::Value reason) +void reject_promise(JS::VM& vm, Promise const& promise, JS::Value reason) { // 1. Perform ! Call(p.[[Reject]], undefined, « r »). - MUST(JS::call(vm, *promise_capability.reject(), JS::js_undefined(), reason)); + MUST(JS::call(vm, *promise.reject(), JS::js_undefined(), reason)); } // https://webidl.spec.whatwg.org/#dfn-perform-steps-once-promise-is-settled -JS::NonnullGCPtr<JS::Promise> react_to_promise(JS::PromiseCapability const& promise_capability, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback) +JS::NonnullGCPtr<JS::Promise> react_to_promise(Promise const& promise, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback) { - auto& realm = promise_capability.promise()->shape().realm(); + auto& realm = promise.promise()->shape().realm(); auto& vm = realm.vm(); // 1. Let onFulfilledSteps be the following steps given argument V: @@ -135,16 +135,16 @@ JS::NonnullGCPtr<JS::Promise> react_to_promise(JS::PromiseCapability const& prom auto new_capability = MUST(JS::new_promise_capability(vm, constructor)); // 7. Return PerformPromiseThen(promise.[[Promise]], onFulfilled, onRejected, newCapability). - auto promise = verify_cast<JS::Promise>(promise_capability.promise().ptr()); - auto value = promise->perform_then(on_fulfilled, on_rejected, new_capability); + auto promise_object = verify_cast<JS::Promise>(promise.promise().ptr()); + auto value = promise_object->perform_then(on_fulfilled, on_rejected, new_capability); return verify_cast<JS::Promise>(value.as_object()); } // https://webidl.spec.whatwg.org/#upon-fulfillment -JS::NonnullGCPtr<JS::Promise> upon_fulfillment(JS::PromiseCapability const& promise_capability, ReactionSteps steps) +JS::NonnullGCPtr<JS::Promise> upon_fulfillment(Promise const& promise, ReactionSteps steps) { // 1. Return the result of reacting to promise: - return react_to_promise(promise_capability, + return react_to_promise(promise, // - If promise was fulfilled with value v, then: [steps = move(steps)](auto value) { // 1. Perform steps with v. @@ -156,10 +156,10 @@ JS::NonnullGCPtr<JS::Promise> upon_fulfillment(JS::PromiseCapability const& prom } // https://webidl.spec.whatwg.org/#upon-rejection -JS::NonnullGCPtr<JS::Promise> upon_rejection(JS::PromiseCapability const& promise_capability, ReactionSteps steps) +JS::NonnullGCPtr<JS::Promise> upon_rejection(Promise const& promise, ReactionSteps steps) { // 1. Return the result of reacting to promise: - return react_to_promise(promise_capability, {}, + return react_to_promise(promise, {}, // - If promise was rejected with reason r, then: [steps = move(steps)](auto reason) { // 1. Perform steps with r. @@ -170,11 +170,11 @@ JS::NonnullGCPtr<JS::Promise> upon_rejection(JS::PromiseCapability const& promis } // https://webidl.spec.whatwg.org/#mark-a-promise-as-handled -void mark_promise_as_handled(JS::PromiseCapability const& promise_capability) +void mark_promise_as_handled(Promise const& promise) { // To mark as handled a Promise<T> promise, set promise.[[Promise]].[[PromiseIsHandled]] to true. - auto promise = verify_cast<JS::Promise>(promise_capability.promise().ptr()); - promise->set_is_handled(); + auto promise_object = verify_cast<JS::Promise>(promise.promise().ptr()); + promise_object->set_is_handled(); } } diff --git a/Userland/Libraries/LibWeb/WebIDL/Promise.h b/Userland/Libraries/LibWeb/WebIDL/Promise.h index 0e221dc518..f8f609c490 100644 --- a/Userland/Libraries/LibWeb/WebIDL/Promise.h +++ b/Userland/Libraries/LibWeb/WebIDL/Promise.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> + * Copyright (c) 2023, networkException <networkexception@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,14 +15,17 @@ namespace Web::WebIDL { using ReactionSteps = JS::SafeFunction<WebIDL::ExceptionOr<JS::Value>(JS::Value)>; -JS::NonnullGCPtr<JS::PromiseCapability> create_promise(JS::Realm&); -JS::NonnullGCPtr<JS::PromiseCapability> create_resolved_promise(JS::Realm&, JS::Value); -JS::NonnullGCPtr<JS::PromiseCapability> create_rejected_promise(JS::Realm&, JS::Value); -void resolve_promise(JS::VM&, JS::PromiseCapability const&, JS::Value = JS::js_undefined()); -void reject_promise(JS::VM&, JS::PromiseCapability const&, JS::Value); -JS::NonnullGCPtr<JS::Promise> react_to_promise(JS::PromiseCapability const&, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback); -JS::NonnullGCPtr<JS::Promise> upon_fulfillment(JS::PromiseCapability const&, ReactionSteps); -JS::NonnullGCPtr<JS::Promise> upon_rejection(JS::PromiseCapability const&, ReactionSteps); -void mark_promise_as_handled(JS::PromiseCapability const&); +// https://webidl.spec.whatwg.org/#es-promise +using Promise = JS::PromiseCapability; + +JS::NonnullGCPtr<Promise> create_promise(JS::Realm&); +JS::NonnullGCPtr<Promise> create_resolved_promise(JS::Realm&, JS::Value); +JS::NonnullGCPtr<Promise> create_rejected_promise(JS::Realm&, JS::Value); +void resolve_promise(JS::VM&, Promise const&, JS::Value = JS::js_undefined()); +void reject_promise(JS::VM&, Promise const&, JS::Value); +JS::NonnullGCPtr<JS::Promise> react_to_promise(Promise const&, Optional<ReactionSteps> on_fulfilled_callback, Optional<ReactionSteps> on_rejected_callback); +JS::NonnullGCPtr<JS::Promise> upon_fulfillment(Promise const&, ReactionSteps); +JS::NonnullGCPtr<JS::Promise> upon_rejection(Promise const&, ReactionSteps); +void mark_promise_as_handled(Promise const&); } |