2022-07-19 02:19:24 +03:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibJS/Runtime/Array.h>
|
2023-07-19 13:54:48 +03:00
|
|
|
|
#include <LibJS/Runtime/Iterator.h>
|
2022-09-04 13:48:49 +03:00
|
|
|
|
#include <LibWeb/Bindings/HeadersIteratorPrototype.h>
|
2022-09-26 03:08:29 +03:00
|
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-07-19 02:19:24 +03:00
|
|
|
|
#include <LibWeb/Fetch/HeadersIterator.h>
|
|
|
|
|
|
2023-01-10 01:49:06 +03:00
|
|
|
|
namespace Web::Bindings {
|
|
|
|
|
|
|
|
|
|
template<>
|
|
|
|
|
void Intrinsics::create_web_prototype_and_constructor<HeadersIteratorPrototype>(JS::Realm& realm)
|
|
|
|
|
{
|
2023-08-13 14:05:26 +03:00
|
|
|
|
auto prototype = heap().allocate<HeadersIteratorPrototype>(realm, realm);
|
2023-11-22 02:55:21 +03:00
|
|
|
|
m_prototypes.set("HeadersIterator"_fly_string, prototype);
|
2023-01-10 01:49:06 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 02:19:24 +03:00
|
|
|
|
namespace Web::Fetch {
|
|
|
|
|
|
2023-11-19 21:47:52 +03:00
|
|
|
|
JS_DEFINE_ALLOCATOR(HeadersIterator);
|
|
|
|
|
|
2023-08-13 14:05:26 +03:00
|
|
|
|
JS::NonnullGCPtr<HeadersIterator> HeadersIterator::create(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
2022-09-04 13:48:49 +03:00
|
|
|
|
{
|
2023-08-13 14:05:26 +03:00
|
|
|
|
return headers.heap().allocate<HeadersIterator>(headers.realm(), headers, iteration_kind);
|
2022-09-04 13:48:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HeadersIterator::HeadersIterator(Headers const& headers, JS::Object::PropertyKind iteration_kind)
|
|
|
|
|
: PlatformObject(headers.realm())
|
|
|
|
|
, m_headers(headers)
|
|
|
|
|
, m_iteration_kind(iteration_kind)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HeadersIterator::~HeadersIterator() = default;
|
|
|
|
|
|
2023-08-07 09:41:28 +03:00
|
|
|
|
void HeadersIterator::initialize(JS::Realm& realm)
|
2023-01-10 14:28:20 +03:00
|
|
|
|
{
|
2023-08-07 09:41:28 +03:00
|
|
|
|
Base::initialize(realm);
|
2024-03-16 15:13:08 +03:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(HeadersIterator);
|
2023-01-10 14:28:20 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-04 13:48:49 +03:00
|
|
|
|
void HeadersIterator::visit_edges(JS::Cell::Visitor& visitor)
|
|
|
|
|
{
|
|
|
|
|
Base::visit_edges(visitor);
|
2023-02-27 02:09:02 +03:00
|
|
|
|
visitor.visit(m_headers);
|
2022-09-04 13:48:49 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 02:19:24 +03:00
|
|
|
|
// https://webidl.spec.whatwg.org/#es-iterable, Step 2
|
2024-04-26 20:24:20 +03:00
|
|
|
|
JS::NonnullGCPtr<JS::Object> HeadersIterator::next()
|
2022-07-19 02:19:24 +03:00
|
|
|
|
{
|
|
|
|
|
// The value pairs to iterate over are the return value of running sort and combine with this’s header list.
|
2024-04-26 20:24:20 +03:00
|
|
|
|
auto value_pairs_to_iterate_over = [&]() {
|
|
|
|
|
return m_headers->m_header_list->sort_and_combine();
|
2022-07-19 02:19:24 +03:00
|
|
|
|
};
|
|
|
|
|
|
2024-04-26 20:24:20 +03:00
|
|
|
|
auto pairs = value_pairs_to_iterate_over();
|
2022-07-19 02:19:24 +03:00
|
|
|
|
|
|
|
|
|
if (m_index >= pairs.size())
|
2024-04-26 20:24:20 +03:00
|
|
|
|
return create_iterator_result_object(vm(), JS::js_undefined(), true);
|
2022-07-19 02:19:24 +03:00
|
|
|
|
|
|
|
|
|
auto const& pair = pairs[m_index++];
|
2023-02-09 17:00:14 +03:00
|
|
|
|
StringView pair_name { pair.name };
|
|
|
|
|
StringView pair_value { pair.value };
|
2022-07-19 02:19:24 +03:00
|
|
|
|
|
|
|
|
|
switch (m_iteration_kind) {
|
|
|
|
|
case JS::Object::PropertyKind::Key:
|
2024-04-26 20:24:20 +03:00
|
|
|
|
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_name), false);
|
2022-07-19 02:19:24 +03:00
|
|
|
|
case JS::Object::PropertyKind::Value:
|
2024-04-26 20:24:20 +03:00
|
|
|
|
return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), pair_value), false);
|
2022-07-19 02:19:24 +03:00
|
|
|
|
case JS::Object::PropertyKind::KeyAndValue: {
|
2023-08-08 19:25:57 +03:00
|
|
|
|
auto array = JS::Array::create_from(realm(), { JS::PrimitiveString::create(vm(), pair_name), JS::PrimitiveString::create(vm(), pair_value) });
|
2024-04-26 20:24:20 +03:00
|
|
|
|
return create_iterator_result_object(vm(), array, false);
|
2022-07-19 02:19:24 +03:00
|
|
|
|
}
|
|
|
|
|
default:
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|