ladybird/Libraries/LibWeb/Fetch/HeadersIterator.cpp
Shannon Booth 9b79a686eb LibJS+LibWeb: Use realm.create<T> instead of heap.allocate<T>
The main motivation behind this is to remove JS specifics of the Realm
from the implementation of the Heap.

As a side effect of this change, this is a bit nicer to read than the
previous approach, and in my opinion, also makes it a little more clear
that this method is specific to a JavaScript Realm.
2024-11-13 16:51:44 -05:00

85 lines
2.6 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/Iterator.h>
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/Fetch/HeadersIterator.h>
namespace Web::Bindings {
template<>
void Intrinsics::create_web_prototype_and_constructor<HeadersIteratorPrototype>(JS::Realm& realm)
{
auto prototype = realm.create<HeadersIteratorPrototype>(realm);
m_prototypes.set("HeadersIterator"_fly_string, prototype);
}
}
namespace Web::Fetch {
JS_DEFINE_ALLOCATOR(HeadersIterator);
JS::NonnullGCPtr<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
{
return headers.realm().create<HeadersIterator>(headers, iteration_kind);
}
HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKind iteration_kind)
: PlatformObject(headers.realm())
, m_headers(headers)
, m_iteration_kind(iteration_kind)
{
}
HeadersIterator::~HeadersIterator() = default;
void HeadersIterator::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(HeadersIterator);
}
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::NonnullGCPtr<JS::Object> HeadersIterator::next()
{
// The value pairs to iterate over are the return value of running sort and combine with thiss header list.
auto value_pairs_to_iterate_over = [&]() {
return m_headers->m_header_list->sort_and_combine();
};
auto pairs = value_pairs_to_iterate_over();
if (m_index >= pairs.size())
return create_iterator_result_object(vm(), JS::js_undefined(), true);
auto const& pair = pairs[m_index++];
StringView pair_name { pair.name };
StringView pair_value { pair.value };
switch (m_iteration_kind) {
case JS::Object::PropertyKind::Key:
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_name), false);
case JS::Object::PropertyKind::Value:
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_value), false);
case JS::Object::PropertyKind::KeyAndValue: {
auto array = JS::Array::create_from(realm(), { JS::PrimitiveString::create(vm(), pair_name), JS::PrimitiveString::create(vm(), pair_value) });
return create_iterator_result_object(vm(), array, false);
}
default:
VERIFY_NOT_REACHED();
}
}
}