mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-09-11 13:36:13 +03:00
LibWeb: Make Headers and HeadersIterator GC-allocated
This commit is contained in:
parent
a8a42b4432
commit
96f6c7fae5
Notes:
sideshowbarker
2024-07-17 07:25:13 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/96f6c7fae5 Pull-request: https://github.com/SerenityOS/serenity/pull/14816 Reviewed-by: https://github.com/ADKaster Reviewed-by: https://github.com/linusg ✅
@ -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();
|
||||
};
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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> {
|
||||
public:
|
||||
using WrapperType = Bindings::HeadersIteratorWrapper;
|
||||
class HeadersIterator final : public Bindings::PlatformObject {
|
||||
WEB_PLATFORM_OBJECT(HeadersIterator, Bindings::PlatformObject);
|
||||
|
||||
static NonnullRefPtr<HeadersIterator> create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
||||
{
|
||||
return adopt_ref(*new HeadersIterator(headers, iteration_kind));
|
||||
}
|
||||
public:
|
||||
static JS::NonnullGCPtr<HeadersIterator> create(Headers const&, JS::Object::PropertyKind 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)
|
||||
|
@ -450,8 +450,6 @@ class URLSearchParamsIterator;
|
||||
namespace Web::Bindings {
|
||||
class CryptoWrapper;
|
||||
class DOMExceptionWrapper;
|
||||
class HeadersWrapper;
|
||||
class HeadersIteratorWrapper;
|
||||
class IdleDeadlineWrapper;
|
||||
class IntersectionObserverWrapper;
|
||||
class LocationObject;
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user