From 96f6c7fae580b3e5e90775c02494daa4f58650ea Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 4 Sep 2022 12:48:49 +0200 Subject: LibWeb: Make Headers and HeadersIterator GC-allocated --- Userland/Libraries/LibWeb/Fetch/Headers.cpp | 21 ++++++---- Userland/Libraries/LibWeb/Fetch/Headers.h | 26 ++++--------- .../Libraries/LibWeb/Fetch/HeadersIterator.cpp | 45 ++++++++++++++-------- Userland/Libraries/LibWeb/Fetch/HeadersIterator.h | 31 +++++---------- Userland/Libraries/LibWeb/Forward.h | 2 - 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 #include +#include namespace Web::Fetch { // https://fetch.spec.whatwg.org/#dom-headers -DOM::ExceptionOr> Headers::create(Optional const& init) +DOM::ExceptionOr> Headers::create_with_global_object(HTML::Window& window, Optional const& init) { // The new Headers(init) constructor steps are: - - auto headers = adopt_ref(*new Headers()); + auto* headers = window.heap().allocate(window.realm(), window); // 1. Set this’s guard to "none". headers->m_guard = Guard::None; @@ -23,9 +22,17 @@ DOM::ExceptionOr> Headers::create(Optional 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 Headers::append(String const& name_string, String const& value_string) { @@ -158,13 +165,11 @@ DOM::ExceptionOr Headers::set(String const& name_string, String const& val // https://webidl.spec.whatwg.org/#es-iterable, Step 4 JS::ThrowCompletionOr 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> { auto headers_or_error = m_header_list.sort_and_combine(); if (headers_or_error.is_error()) - return vm.throw_completion(JS::ErrorType::NotEnoughMemoryToAllocate); + return vm().throw_completion(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 -#include #include #include #include -#include +#include #include #include @@ -20,12 +19,10 @@ namespace Web::Fetch { using HeadersInit = Variant>, OrderedHashMap>; // https://fetch.spec.whatwg.org/#headers-class -class Headers - : public Bindings::Wrappable - , public RefCounted { -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> create(Optional const&); + static DOM::ExceptionOr> create_with_global_object(HTML::Window& window, Optional const& init); - static DOM::ExceptionOr> create_with_global_object(HTML::Window&, Optional const& init) - { - return create(init); - } + virtual ~Headers() override; DOM::ExceptionOr append(Infrastructure::Header); DOM::ExceptionOr append(String const& name, String const& value); @@ -54,7 +48,7 @@ public: private: friend class HeadersIterator; - Headers() = default; + explicit Headers(HTML::Window&); DOM::ExceptionOr 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 #include -#include -#include +#include #include +#include namespace Web::Fetch { +JS::NonnullGCPtr HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind) +{ + return *headers.heap().allocate(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("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 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> { auto headers_or_error = m_headers.m_header_list.sort_and_combine(); if (headers_or_error.is_error()) - return vm.throw_completion(JS::ErrorType::NotEnoughMemoryToAllocate); + return vm().throw_completion(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 +#include #include namespace Web::Fetch { -class HeadersIterator - : public Bindings::Wrappable - , public RefCounted { +class HeadersIterator final : public Bindings::PlatformObject { + WEB_PLATFORM_OBJECT(HeadersIterator, Bindings::PlatformObject); + public: - using WrapperType = Bindings::HeadersIteratorWrapper; + static JS::NonnullGCPtr create(Headers const&, JS::Object::PropertyKind iteration_kind); - static NonnullRefPtr create(Headers const& headers, JS::Object::PropertyKind iteration_kind) - { - return adopt_ref(*new HeadersIterator(headers, iteration_kind)); - } + virtual ~HeadersIterator() override; JS::ThrowCompletionOr 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) -- cgit v1.2.3