Serenity Operating System
at master 68 lines 2.6 kB view raw
1/* 2 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibJS/Runtime/Array.h> 8#include <LibJS/Runtime/IteratorOperations.h> 9#include <LibWeb/Bindings/Intrinsics.h> 10#include <LibWeb/Bindings/URLSearchParamsIteratorPrototype.h> 11#include <LibWeb/URL/URLSearchParamsIterator.h> 12 13namespace Web::Bindings { 14 15template<> 16void Intrinsics::create_web_prototype_and_constructor<URLSearchParamsIteratorPrototype>(JS::Realm& realm) 17{ 18 auto prototype = heap().allocate<URLSearchParamsIteratorPrototype>(realm, realm).release_allocated_value_but_fixme_should_propagate_errors(); 19 m_prototypes.set("URLSearchParamsIterator"sv, prototype); 20} 21 22} 23 24namespace Web::URL { 25 26WebIDL::ExceptionOr<JS::NonnullGCPtr<URLSearchParamsIterator>> URLSearchParamsIterator::create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind) 27{ 28 return MUST_OR_THROW_OOM(url_search_params.heap().allocate<URLSearchParamsIterator>(url_search_params.realm(), url_search_params, iteration_kind)); 29} 30 31URLSearchParamsIterator::URLSearchParamsIterator(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind) 32 : PlatformObject(url_search_params.realm()) 33 , m_url_search_params(url_search_params) 34 , m_iteration_kind(iteration_kind) 35{ 36} 37 38URLSearchParamsIterator::~URLSearchParamsIterator() = default; 39 40JS::ThrowCompletionOr<void> URLSearchParamsIterator::initialize(JS::Realm& realm) 41{ 42 MUST_OR_THROW_OOM(Base::initialize(realm)); 43 set_prototype(&Bindings::ensure_web_prototype<Bindings::URLSearchParamsIteratorPrototype>(realm, "URLSearchParamsIterator")); 44 45 return {}; 46} 47 48void URLSearchParamsIterator::visit_edges(JS::Cell::Visitor& visitor) 49{ 50 Base::visit_edges(visitor); 51 visitor.visit(&m_url_search_params); 52} 53 54JS::Object* URLSearchParamsIterator::next() 55{ 56 if (m_index >= m_url_search_params.m_list.size()) 57 return create_iterator_result_object(vm(), JS::js_undefined(), true); 58 59 auto& entry = m_url_search_params.m_list[m_index++]; 60 if (m_iteration_kind == JS::Object::PropertyKind::Key) 61 return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), entry.name), false); 62 else if (m_iteration_kind == JS::Object::PropertyKind::Value) 63 return create_iterator_result_object(vm(), JS::PrimitiveString::create(vm(), entry.value), false); 64 65 return create_iterator_result_object(vm(), JS::Array::create_from(realm(), { JS::PrimitiveString::create(vm(), entry.name), JS::PrimitiveString::create(vm(), entry.value) }), false); 66} 67 68}