ladybird/Userland/Libraries/LibWeb/Fetch/HeadersIterator.cpp
Linus Groh b345a0acca LibJS+LibWeb: Reduce use of GlobalObject as an intermediary
- Prefer VM::current_realm() over GlobalObject::associated_realm()
- Prefer VM::heap() over GlobalObject::heap()
- Prefer Cell::vm() over Cell::global_object()
- Prefer Wrapper::vm() over Wrapper::global_object()
- Inline Realm::global_object() calls used to access intrinsics as they
  will later perform a direct lookup without going through the global
  object
2022-08-23 13:58:30 +01:00

55 lines
1.9 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/IteratorOperations.h>
#include <LibWeb/Bindings/HeadersIteratorWrapper.h>
#include <LibWeb/Bindings/Wrapper.h>
#include <LibWeb/Fetch/HeadersIterator.h>
namespace Web::Fetch {
// 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 thiss 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 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);
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);
case JS::Object::PropertyKind::Value:
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);
}
default:
VERIFY_NOT_REACHED();
}
}
void HeadersIterator::visit_edges(JS::Cell::Visitor& visitor)
{
visitor.visit(m_headers.wrapper());
}
}