LibWeb: Make URL, URLSearchParams & URLSearchParamsIterator GC-allocated

This commit is contained in:
Andreas Kling 2022-09-04 14:04:42 +02:00
parent 0dc2c27fa3
commit fe9c5395d4
Notes: sideshowbarker 2024-07-17 07:24:53 +09:00
11 changed files with 125 additions and 86 deletions

View File

@ -163,6 +163,10 @@ static bool impl_is_wrapper(Type const& type)
return true;
if (type.name == "Blob"sv)
return true;
if (type.name == "URL"sv)
return true;
if (type.name == "URLSearchParams"sv)
return true;
return false;
}

View File

@ -453,9 +453,6 @@ class IntersectionObserverWrapper;
class LocationObject;
class OptionConstructor;
class RangePrototype;
class URLSearchParamsIteratorWrapper;
class URLSearchParamsWrapper;
class URLWrapper;
class WindowProxy;
class WorkerLocationWrapper;
class WorkerNavigatorWrapper;

View File

@ -6,11 +6,17 @@
*/
#include <AK/URLParser.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URL.h>
namespace Web::URL {
DOM::ExceptionOr<NonnullRefPtr<URL>> URL::create_with_global_object(HTML::Window& window_object, String const& url, String const& base)
JS::NonnullGCPtr<URL> URL::create(HTML::Window& window, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
{
return *window.heap().allocate<URL>(window.realm(), window, move(url), move(query));
}
DOM::ExceptionOr<JS::NonnullGCPtr<URL>> URL::create_with_global_object(HTML::Window& window, String const& url, String const& base)
{
// 1. Let parsedBase be null.
Optional<AK::URL> parsed_base;
@ -35,15 +41,31 @@ DOM::ExceptionOr<NonnullRefPtr<URL>> URL::create_with_global_object(HTML::Window
auto& query = parsed_url.query().is_null() ? String::empty() : parsed_url.query();
// 6. Set thiss URL to parsedURL.
// 7. Set thiss query object to a new URLSearchParams object.
auto query_object = MUST(URLSearchParams::create_with_global_object(window_object, query));
auto query_object = MUST(URLSearchParams::create_with_global_object(window, query));
// 8. Initialize thiss query object with query.
auto result_url = URL::create(move(parsed_url), move(query_object));
auto result_url = URL::create(window, move(parsed_url), move(query_object));
// 9. Set thiss query objects URL object to this.
result_url->m_query->m_url = result_url;
return result_url;
}
URL::URL(HTML::Window& window, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query)
: PlatformObject(window.realm())
, m_url(move(url))
, m_query(move(query))
{
set_prototype(&window.cached_web_prototype("URL"));
}
URL::~URL() = default;
void URL::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_query.ptr());
}
String URL::href() const
{
// return the serialization of thiss URL.

View File

@ -9,24 +9,20 @@
#include <AK/String.h>
#include <AK/URL.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
class URL : public Bindings::Wrappable
, public RefCounted<URL>
, public Weakable<URL> {
class URL : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(URL, Bindings::PlatformObject);
public:
using WrapperType = Bindings::URLWrapper;
static JS::NonnullGCPtr<URL> create(HTML::Window&, AK::URL url, JS::NonnullGCPtr<URLSearchParams> query);
static DOM::ExceptionOr<JS::NonnullGCPtr<URL>> create_with_global_object(HTML::Window&, String const& url, String const& base);
static NonnullRefPtr<URL> create(AK::URL url, NonnullRefPtr<URLSearchParams> query)
{
return adopt_ref(*new URL(move(url), move(query)));
}
static DOM::ExceptionOr<NonnullRefPtr<URL>> create_with_global_object(HTML::Window&, String const& url, String const& base);
virtual ~URL() override;
String href() const;
DOM::ExceptionOr<void> set_href(String const&);
@ -67,12 +63,14 @@ public:
void set_query(Badge<URLSearchParams>, String query) { m_url.set_query(move(query)); }
private:
explicit URL(AK::URL url, NonnullRefPtr<URLSearchParams> query)
: m_url(move(url))
, m_query(move(query)) {};
URL(HTML::Window&, AK::URL, JS::NonnullGCPtr<URLSearchParams> query);
virtual void visit_edges(Cell::Visitor&) override;
AK::URL m_url;
NonnullRefPtr<URLSearchParams> m_query;
JS::NonnullGCPtr<URLSearchParams> m_query;
};
}
WRAPPER_HACK(URL, Web::URL)

View File

@ -7,11 +7,27 @@
#include <AK/QuickSort.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URL.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
URLSearchParams::URLSearchParams(HTML::Window& window, Vector<QueryParam> list)
: PlatformObject(window.realm())
, m_list(move(list))
{
set_prototype(&window.cached_web_prototype("URLSearchParams"));
}
URLSearchParams::~URLSearchParams() = default;
void URLSearchParams::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_url);
}
String url_encode(Vector<QueryParam> const& pairs, AK::URL::PercentEncodeSet percent_encode_set)
{
StringBuilder builder;
@ -66,9 +82,14 @@ Vector<QueryParam> url_decode(StringView input)
return output;
}
JS::NonnullGCPtr<URLSearchParams> URLSearchParams::create(HTML::Window& window, Vector<QueryParam> list)
{
return *window.heap().allocate<URLSearchParams>(window.realm(), window, move(list));
}
// https://url.spec.whatwg.org/#dom-urlsearchparams-urlsearchparams
// https://url.spec.whatwg.org/#urlsearchparams-initialize
DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> URLSearchParams::create_with_global_object(HTML::Window&, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init)
DOM::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> URLSearchParams::create_with_global_object(HTML::Window& window, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init)
{
// 1. If init is a string and starts with U+003F (?), then remove the first code point from init.
// NOTE: We do this when we know that it's a string on step 3 of initialization.
@ -93,7 +114,7 @@ DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> URLSearchParams::create_with_gl
list.append(QueryParam { .name = pair[0], .value = pair[1] });
}
return URLSearchParams::create(move(list));
return URLSearchParams::create(window, move(list));
}
// 2. Otherwise, if init is a record, then for each name → value of init, append a new name-value pair whose name is name and value is value, to querys list.
@ -106,7 +127,7 @@ DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> URLSearchParams::create_with_gl
for (auto const& pair : init_record)
list.append(QueryParam { .name = pair.key, .value = pair.value });
return URLSearchParams::create(move(list));
return URLSearchParams::create(window, move(list));
}
// 3. Otherwise:
@ -118,7 +139,7 @@ DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> URLSearchParams::create_with_gl
StringView stripped_init = init_string.substring_view(init_string.starts_with('?'));
// b. Set querys list to the result of parsing init.
return URLSearchParams::create(url_decode(stripped_init));
return URLSearchParams::create(window, url_decode(stripped_init));
}
void URLSearchParams::append(String const& name, String const& value)
@ -132,7 +153,7 @@ void URLSearchParams::append(String const& name, String const& value)
void URLSearchParams::update()
{
// 1. If querys URL object is null, then return.
if (m_url.is_null())
if (!m_url)
return;
// 2. Let serializedQuery be the serialization of querys list.
auto serialized_query = to_string();

View File

@ -20,17 +20,14 @@ struct QueryParam {
String url_encode(Vector<QueryParam> const&, AK::URL::PercentEncodeSet);
Vector<QueryParam> url_decode(StringView);
class URLSearchParams : public Bindings::Wrappable
, public RefCounted<URLSearchParams> {
class URLSearchParams : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(URLSearchParams, Bindings::PlatformObject);
public:
using WrapperType = Bindings::URLSearchParamsWrapper;
static JS::NonnullGCPtr<URLSearchParams> create(HTML::Window&, Vector<QueryParam> list);
static DOM::ExceptionOr<JS::NonnullGCPtr<URLSearchParams>> create_with_global_object(HTML::Window&, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init);
static NonnullRefPtr<URLSearchParams> create(Vector<QueryParam> list)
{
return adopt_ref(*new URLSearchParams(move(list)));
}
static DOM::ExceptionOr<NonnullRefPtr<URLSearchParams>> create_with_global_object(HTML::Window&, Variant<Vector<Vector<String>>, OrderedHashMap<String, String>, String> const& init);
virtual ~URLSearchParams() override;
void append(String const& name, String const& value);
void delete_(String const& name);
@ -50,19 +47,16 @@ private:
friend class URL;
friend class URLSearchParamsIterator;
explicit URLSearchParams(Vector<QueryParam> list)
: m_list(move(list)) {};
URLSearchParams(HTML::Window&, Vector<QueryParam> list);
virtual void visit_edges(Cell::Visitor&) override;
void update();
Vector<QueryParam> m_list;
WeakPtr<URL> m_url;
JS::GCPtr<URL> m_url;
};
}
namespace Web::Bindings {
URLSearchParamsWrapper* wrap(JS::Realm&, URL::URLSearchParams&);
}
WRAPPER_HACK(URLSearchParams, Web::URL)

View File

@ -6,32 +6,45 @@
#include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h>
#include <LibWeb/Bindings/URLSearchParamsIteratorWrapper.h>
#include <LibWeb/Bindings/Wrapper.h>
#include <LibWeb/Bindings/URLSearchParamsIteratorPrototype.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URLSearchParamsIterator.h>
namespace Web::URL {
JS::Object* URLSearchParamsIterator::next()
JS::NonnullGCPtr<URLSearchParamsIterator> URLSearchParamsIterator::create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
{
auto& vm = wrapper()->vm();
auto& realm = *vm.current_realm();
if (m_index >= m_url_search_params.m_list.size())
return create_iterator_result_object(vm, JS::js_undefined(), true);
auto& entry = m_url_search_params.m_list[m_index++];
if (m_iteration_kind == JS::Object::PropertyKind::Key)
return create_iterator_result_object(vm, JS::js_string(vm, entry.name), false);
else if (m_iteration_kind == JS::Object::PropertyKind::Value)
return create_iterator_result_object(vm, JS::js_string(vm, entry.value), false);
return create_iterator_result_object(vm, JS::Array::create_from(realm, { JS::js_string(vm, entry.name), JS::js_string(vm, entry.value) }), false);
return *url_search_params.heap().allocate<URLSearchParamsIterator>(url_search_params.realm(), url_search_params, iteration_kind);
}
URLSearchParamsIterator::URLSearchParamsIterator(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
: PlatformObject(url_search_params.realm())
, m_url_search_params(url_search_params)
, m_iteration_kind(iteration_kind)
{
set_prototype(&url_search_params.global_object().ensure_web_prototype<Bindings::URLSearchParamsIteratorPrototype>("URLSearchParamsIterator"));
}
URLSearchParamsIterator::~URLSearchParamsIterator() = default;
void URLSearchParamsIterator::visit_edges(JS::Cell::Visitor& visitor)
{
visitor.visit(m_url_search_params.wrapper());
Base::visit_edges(visitor);
visitor.visit(&m_url_search_params);
}
JS::Object* URLSearchParamsIterator::next()
{
if (m_index >= m_url_search_params.m_list.size())
return create_iterator_result_object(vm(), JS::js_undefined(), true);
auto& entry = m_url_search_params.m_list[m_index++];
if (m_iteration_kind == JS::Object::PropertyKind::Key)
return create_iterator_result_object(vm(), JS::js_string(vm(), entry.name), false);
else if (m_iteration_kind == JS::Object::PropertyKind::Value)
return create_iterator_result_object(vm(), JS::js_string(vm(), entry.value), false);
return create_iterator_result_object(vm(), JS::Array::create_from(realm(), { JS::js_string(vm(), entry.name), JS::js_string(vm(), entry.value) }), false);
}
}

View File

@ -6,41 +6,31 @@
#pragma once
#include "URLSearchParams.h"
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL {
class URLSearchParamsIterator : public Bindings::Wrappable
, public RefCounted<URLSearchParamsIterator> {
public:
using WrapperType = Bindings::URLSearchParamsIteratorWrapper;
class URLSearchParamsIterator : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(URLSearchParamsIterator, Bindings::PlatformObject);
static NonnullRefPtr<URLSearchParamsIterator> create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
{
return adopt_ref(*new URLSearchParamsIterator(url_search_params, iteration_kind));
}
public:
static JS::NonnullGCPtr<URLSearchParamsIterator> create(URLSearchParams const&, JS::Object::PropertyKind iteration_kind);
virtual ~URLSearchParamsIterator() override;
JS::Object* next();
void visit_edges(JS::Cell::Visitor&);
private:
explicit URLSearchParamsIterator(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind)
: m_url_search_params(url_search_params)
, m_iteration_kind(iteration_kind)
{
}
URLSearchParamsIterator(URLSearchParams const&, JS::Object::PropertyKind iteration_kind);
virtual void visit_edges(Cell::Visitor&) override;
URLSearchParams const& m_url_search_params;
JS::Object::PropertyKind m_iteration_kind;
size_t m_index { 0 };
};
}
namespace Web::Bindings {
URLSearchParamsIteratorWrapper* wrap(JS::Realm&, URL::URLSearchParamsIterator&);
}
WRAPPER_HACK(URLSearchParamsIterator, Web::URL)

View File

@ -301,7 +301,7 @@ static ErrorOr<Fetch::Infrastructure::BodyWithType> extract_body(XMLHttpRequestB
source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
return {};
},
[&](NonnullRefPtr<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
[&](JS::Handle<URL::URLSearchParams> const& url_search_params) -> ErrorOr<void> {
// Set source to the result of running the application/x-www-form-urlencoded serializer with objects list.
source = url_search_params->to_string().to_byte_buffer();
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`.

View File

@ -24,7 +24,7 @@
namespace Web::XHR {
// https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit
using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, NonnullRefPtr<URL::URLSearchParams>, String>;
using XMLHttpRequestBodyInit = Variant<JS::Handle<FileAPI::Blob>, JS::Handle<JS::Object>, JS::Handle<URL::URLSearchParams>, String>;
class XMLHttpRequest final : public XMLHttpRequestEventTarget {
WEB_PLATFORM_OBJECT(XMLHttpRequest, XMLHttpRequestEventTarget);

View File

@ -180,8 +180,8 @@ libweb_js_wrapper(UIEvents/FocusEvent NO_INSTANCE)
libweb_js_wrapper(UIEvents/KeyboardEvent NO_INSTANCE)
libweb_js_wrapper(UIEvents/MouseEvent NO_INSTANCE)
libweb_js_wrapper(UIEvents/UIEvent NO_INSTANCE)
libweb_js_wrapper(URL/URL)
libweb_js_wrapper(URL/URLSearchParams ITERABLE)
libweb_js_wrapper(URL/URL NO_INSTANCE)
libweb_js_wrapper(URL/URLSearchParams ITERABLE NO_INSTANCE)
libweb_js_wrapper(WebGL/WebGLContextEvent NO_INSTANCE)
libweb_js_wrapper(WebGL/WebGLRenderingContext NO_INSTANCE)
libweb_js_wrapper(WebSockets/WebSocket NO_INSTANCE)