diff options
author | Andreas Kling <kling@serenityos.org> | 2022-09-04 12:48:49 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | 96f6c7fae580b3e5e90775c02494daa4f58650ea (patch) | |
tree | 31df0ffb9cc8c15abc94c84cf4aca8d249862a20 | |
parent | a8a42b4432432cc29e56f6a1823f4d4c1d80bc55 (diff) | |
download | serenity-96f6c7fae580b3e5e90775c02494daa4f58650ea.zip |
LibWeb: Make Headers and HeadersIterator GC-allocated
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Headers.cpp | 21 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/Headers.h | 26 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp | 45 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Fetch/HeadersIterator.h | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Forward.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/idl_files.cmake | 2 |
6 files changed, 61 insertions, 66 deletions
diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.cpp b/Userland/Libraries/LibWeb/Fetch/Headers.cpp index 50af058fb8..1b2e0d7d1f 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.cpp +++ b/Userland/Libraries/LibWeb/Fetch/Headers.cpp @@ -4,17 +4,16 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include <LibWeb/Bindings/HeadersWrapper.h> #include <LibWeb/Fetch/Headers.h> +#include <LibWeb/HTML/Window.h> namespace Web::Fetch { // https://fetch.spec.whatwg.org/#dom-headers -DOM::ExceptionOr<NonnullRefPtr<Headers>> Headers::create(Optional<HeadersInit> const& init) +DOM::ExceptionOr<JS::NonnullGCPtr<Headers>> Headers::create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init) { // The new Headers(init) constructor steps are: - - auto headers = adopt_ref(*new Headers()); + auto* headers = window.heap().allocate<Headers>(window.realm(), window); // 1. Set this’s guard to "none". headers->m_guard = Guard::None; @@ -23,9 +22,17 @@ DOM::ExceptionOr<NonnullRefPtr<Headers>> Headers::create(Optional<HeadersInit> c if (init.has_value()) TRY(headers->fill(*init)); - return headers; + return JS::NonnullGCPtr(*headers); +} + +Headers::Headers(HTML::Window& window) + : PlatformObject(window.realm()) +{ + set_prototype(&window.cached_web_prototype("Headers")); } +Headers::~Headers() = default; + // https://fetch.spec.whatwg.org/#dom-headers-append DOM::ExceptionOr<void> Headers::append(String const& name_string, String const& value_string) { @@ -158,13 +165,11 @@ DOM::ExceptionOr<void> Headers::set(String const& name_string, String const& val // https://webidl.spec.whatwg.org/#es-iterable, Step 4 JS::ThrowCompletionOr<void> Headers::for_each(ForEachCallback callback) { - auto& vm = wrapper()->vm(); - // The value pairs to iterate over are the return value of running sort and combine with this’s header list. auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> { auto headers_or_error = m_header_list.sort_and_combine(); if (headers_or_error.is_error()) - return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate); + return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate); return headers_or_error.release_value(); }; diff --git a/Userland/Libraries/LibWeb/Fetch/Headers.h b/Userland/Libraries/LibWeb/Fetch/Headers.h index 05da753472..b7e74c1ad6 100644 --- a/Userland/Libraries/LibWeb/Fetch/Headers.h +++ b/Userland/Libraries/LibWeb/Fetch/Headers.h @@ -7,11 +7,10 @@ #pragma once #include <AK/HashMap.h> -#include <AK/NonnullRefPtr.h> #include <AK/String.h> #include <AK/Variant.h> #include <AK/Vector.h> -#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Headers.h> @@ -20,12 +19,10 @@ namespace Web::Fetch { using HeadersInit = Variant<Vector<Vector<String>>, OrderedHashMap<String, String>>; // https://fetch.spec.whatwg.org/#headers-class -class Headers - : public Bindings::Wrappable - , public RefCounted<Headers> { -public: - using WrapperType = Bindings::HeadersWrapper; +class Headers final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(Headers, Bindings::PlatformObject); +public: enum class Guard { Immutable, Request, @@ -34,12 +31,9 @@ public: None, }; - static DOM::ExceptionOr<NonnullRefPtr<Headers>> create(Optional<HeadersInit> const&); + static DOM::ExceptionOr<JS::NonnullGCPtr<Headers>> create_with_global_object(HTML::Window& window, Optional<HeadersInit> const& init); - static DOM::ExceptionOr<NonnullRefPtr<Headers>> create_with_global_object(HTML::Window&, Optional<HeadersInit> const& init) - { - return create(init); - } + virtual ~Headers() override; DOM::ExceptionOr<void> append(Infrastructure::Header); DOM::ExceptionOr<void> append(String const& name, String const& value); @@ -54,7 +48,7 @@ public: private: friend class HeadersIterator; - Headers() = default; + explicit Headers(HTML::Window&); DOM::ExceptionOr<void> fill(HeadersInit const&); void remove_privileged_no_cors_headers(); @@ -70,8 +64,4 @@ private: } -namespace Web::Bindings { - -HeadersWrapper* wrap(JS::Realm&, Fetch::Headers&); - -} +WRAPPER_HACK(Headers, Web::Fetch) diff --git a/Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp b/Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp index d5981ad0cb..47110fdd4c 100644 --- a/Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp +++ b/Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp @@ -6,50 +6,63 @@ #include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/IteratorOperations.h> -#include <LibWeb/Bindings/HeadersIteratorWrapper.h> -#include <LibWeb/Bindings/Wrapper.h> +#include <LibWeb/Bindings/HeadersIteratorPrototype.h> #include <LibWeb/Fetch/HeadersIterator.h> +#include <LibWeb/HTML/Window.h> namespace Web::Fetch { +JS::NonnullGCPtr<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind) +{ + return *headers.heap().allocate<HeadersIterator>(headers.realm(), headers, iteration_kind); +} + +HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKind iteration_kind) + : PlatformObject(headers.realm()) + , m_headers(headers) + , m_iteration_kind(iteration_kind) +{ + set_prototype(&headers.global_object().ensure_web_prototype<Bindings::HeadersIteratorPrototype>("HeadersIterator")); +} + +HeadersIterator::~HeadersIterator() = default; + +void HeadersIterator::visit_edges(JS::Cell::Visitor& visitor) +{ + Base::visit_edges(visitor); + visitor.visit(&m_headers); +} + // https://webidl.spec.whatwg.org/#es-iterable, Step 2 JS::ThrowCompletionOr<JS::Object*> HeadersIterator::next() { - auto& vm = wrapper()->vm(); - auto& realm = *vm.current_realm(); - // The value pairs to iterate over are the return value of running sort and combine with this’s header list. auto value_pairs_to_iterate_over = [&]() -> JS::ThrowCompletionOr<Vector<Fetch::Infrastructure::Header>> { auto headers_or_error = m_headers.m_header_list.sort_and_combine(); if (headers_or_error.is_error()) - return vm.throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate); + return vm().throw_completion<JS::InternalError>(JS::ErrorType::NotEnoughMemoryToAllocate); return headers_or_error.release_value(); }; auto pairs = TRY(value_pairs_to_iterate_over()); if (m_index >= pairs.size()) - return create_iterator_result_object(vm, JS::js_undefined(), true); + return create_iterator_result_object(vm(), JS::js_undefined(), true); auto const& pair = pairs[m_index++]; switch (m_iteration_kind) { case JS::Object::PropertyKind::Key: - return create_iterator_result_object(vm, JS::js_string(vm, StringView { pair.name }), false); + return create_iterator_result_object(vm(), JS::js_string(vm(), StringView { pair.name }), false); case JS::Object::PropertyKind::Value: - return create_iterator_result_object(vm, JS::js_string(vm, StringView { pair.value }), false); + return create_iterator_result_object(vm(), JS::js_string(vm(), StringView { pair.value }), false); case JS::Object::PropertyKind::KeyAndValue: { - auto* array = JS::Array::create_from(realm, { JS::js_string(vm, StringView { pair.name }), JS::js_string(vm, StringView { pair.value }) }); - return create_iterator_result_object(vm, array, false); + auto* array = JS::Array::create_from(realm(), { JS::js_string(vm(), StringView { pair.name }), JS::js_string(vm(), StringView { pair.value }) }); + return create_iterator_result_object(vm(), array, false); } default: VERIFY_NOT_REACHED(); } } -void HeadersIterator::visit_edges(JS::Cell::Visitor& visitor) -{ - visitor.visit(m_headers.wrapper()); -} - } diff --git a/Userland/Libraries/LibWeb/Fetch/HeadersIterator.h b/Userland/Libraries/LibWeb/Fetch/HeadersIterator.h index bfdf093506..c543c245ea 100644 --- a/Userland/Libraries/LibWeb/Fetch/HeadersIterator.h +++ b/Userland/Libraries/LibWeb/Fetch/HeadersIterator.h @@ -6,32 +6,25 @@ #pragma once -#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/PlatformObject.h> #include <LibWeb/Fetch/Headers.h> namespace Web::Fetch { -class HeadersIterator - : public Bindings::Wrappable - , public RefCounted<HeadersIterator> { +class HeadersIterator final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(HeadersIterator, Bindings::PlatformObject); + public: - using WrapperType = Bindings::HeadersIteratorWrapper; + static JS::NonnullGCPtr<HeadersIterator> create(Headers const&, JS::Object::PropertyKind iteration_kind); - static NonnullRefPtr<HeadersIterator> create(Headers const& headers, JS::Object::PropertyKind iteration_kind) - { - return adopt_ref(*new HeadersIterator(headers, iteration_kind)); - } + virtual ~HeadersIterator() override; JS::ThrowCompletionOr<JS::Object*> next(); - void visit_edges(JS::Cell::Visitor&); - private: - HeadersIterator(Headers const& headers, JS::Object::PropertyKind iteration_kind) - : m_headers(headers) - , m_iteration_kind(iteration_kind) - { - } + virtual void visit_edges(JS::Cell::Visitor&) override; + + HeadersIterator(Headers const&, JS::Object::PropertyKind iteration_kind); Headers const& m_headers; JS::Object::PropertyKind m_iteration_kind; @@ -40,8 +33,4 @@ private: } -namespace Web::Bindings { - -HeadersIteratorWrapper* wrap(JS::Realm&, Fetch::HeadersIterator&); - -} +WRAPPER_HACK(HeadersIterator, Web::Fetch) diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index e8a627ab33..f27edd3746 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -450,8 +450,6 @@ class URLSearchParamsIterator; namespace Web::Bindings { class CryptoWrapper; class DOMExceptionWrapper; -class HeadersWrapper; -class HeadersIteratorWrapper; class IdleDeadlineWrapper; class IntersectionObserverWrapper; class LocationObject; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 0307450c85..6083e113d8 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -53,7 +53,7 @@ libweb_js_wrapper(DOM/TreeWalker NO_INSTANCE) libweb_js_wrapper(DOMParsing/XMLSerializer NO_INSTANCE) libweb_js_wrapper(Encoding/TextDecoder NO_INSTANCE) libweb_js_wrapper(Encoding/TextEncoder NO_INSTANCE) -libweb_js_wrapper(Fetch/Headers ITERABLE) +libweb_js_wrapper(Fetch/Headers ITERABLE NO_INSTANCE) libweb_js_wrapper(FileAPI/Blob NO_INSTANCE) libweb_js_wrapper(FileAPI/File NO_INSTANCE) libweb_js_wrapper(Geometry/DOMPoint NO_INSTANCE) |