1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 13:17:43 +00:00

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
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; return true;
if (type.name == "Blob"sv) if (type.name == "Blob"sv)
return true; return true;
if (type.name == "URL"sv)
return true;
if (type.name == "URLSearchParams"sv)
return true;
return false; return false;
} }

View file

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

View file

@ -6,11 +6,17 @@
*/ */
#include <AK/URLParser.h> #include <AK/URLParser.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URL.h> #include <LibWeb/URL/URL.h>
namespace Web::URL { 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. // 1. Let parsedBase be null.
Optional<AK::URL> parsed_base; 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(); auto& query = parsed_url.query().is_null() ? String::empty() : parsed_url.query();
// 6. Set thiss URL to parsedURL. // 6. Set thiss URL to parsedURL.
// 7. Set thiss query object to a new URLSearchParams object. // 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. // 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. // 9. Set thiss query objects URL object to this.
result_url->m_query->m_url = result_url; result_url->m_query->m_url = result_url;
return 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 String URL::href() const
{ {
// return the serialization of thiss URL. // return the serialization of thiss URL.

View file

@ -9,24 +9,20 @@
#include <AK/String.h> #include <AK/String.h>
#include <AK/URL.h> #include <AK/URL.h>
#include <LibWeb/Bindings/Wrappable.h> #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/URL/URLSearchParams.h> #include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL { namespace Web::URL {
class URL : public Bindings::Wrappable class URL : public Bindings::PlatformObject {
, public RefCounted<URL> WEB_PLATFORM_OBJECT(URL, Bindings::PlatformObject);
, public Weakable<URL> {
public: 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) virtual ~URL() override;
{
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);
String href() const; String href() const;
DOM::ExceptionOr<void> set_href(String 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)); } void set_query(Badge<URLSearchParams>, String query) { m_url.set_query(move(query)); }
private: private:
explicit URL(AK::URL url, NonnullRefPtr<URLSearchParams> query) URL(HTML::Window&, AK::URL, JS::NonnullGCPtr<URLSearchParams> query);
: m_url(move(url))
, m_query(move(query)) {}; virtual void visit_edges(Cell::Visitor&) override;
AK::URL m_url; 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/QuickSort.h>
#include <AK/StringBuilder.h> #include <AK/StringBuilder.h>
#include <AK/Utf8View.h> #include <AK/Utf8View.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URL.h> #include <LibWeb/URL/URL.h>
#include <LibWeb/URL/URLSearchParams.h> #include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL { 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) String url_encode(Vector<QueryParam> const& pairs, AK::URL::PercentEncodeSet percent_encode_set)
{ {
StringBuilder builder; StringBuilder builder;
@ -66,9 +82,14 @@ Vector<QueryParam> url_decode(StringView input)
return output; 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/#dom-urlsearchparams-urlsearchparams
// https://url.spec.whatwg.org/#urlsearchparams-initialize // 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. // 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. // 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] }); 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. // 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) for (auto const& pair : init_record)
list.append(QueryParam { .name = pair.key, .value = pair.value }); list.append(QueryParam { .name = pair.key, .value = pair.value });
return URLSearchParams::create(move(list)); return URLSearchParams::create(window, move(list));
} }
// 3. Otherwise: // 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('?')); StringView stripped_init = init_string.substring_view(init_string.starts_with('?'));
// b. Set querys list to the result of parsing init. // 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) 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() void URLSearchParams::update()
{ {
// 1. If querys URL object is null, then return. // 1. If querys URL object is null, then return.
if (m_url.is_null()) if (!m_url)
return; return;
// 2. Let serializedQuery be the serialization of querys list. // 2. Let serializedQuery be the serialization of querys list.
auto serialized_query = to_string(); auto serialized_query = to_string();

View file

@ -20,17 +20,14 @@ struct QueryParam {
String url_encode(Vector<QueryParam> const&, AK::URL::PercentEncodeSet); String url_encode(Vector<QueryParam> const&, AK::URL::PercentEncodeSet);
Vector<QueryParam> url_decode(StringView); Vector<QueryParam> url_decode(StringView);
class URLSearchParams : public Bindings::Wrappable class URLSearchParams : public Bindings::PlatformObject {
, public RefCounted<URLSearchParams> { WEB_PLATFORM_OBJECT(URLSearchParams, Bindings::PlatformObject);
public: 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) virtual ~URLSearchParams() override;
{
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);
void append(String const& name, String const& value); void append(String const& name, String const& value);
void delete_(String const& name); void delete_(String const& name);
@ -50,19 +47,16 @@ private:
friend class URL; friend class URL;
friend class URLSearchParamsIterator; friend class URLSearchParamsIterator;
explicit URLSearchParams(Vector<QueryParam> list) URLSearchParams(HTML::Window&, Vector<QueryParam> list);
: m_list(move(list)) {};
virtual void visit_edges(Cell::Visitor&) override;
void update(); void update();
Vector<QueryParam> m_list; Vector<QueryParam> m_list;
WeakPtr<URL> m_url; JS::GCPtr<URL> m_url;
}; };
} }
namespace Web::Bindings { WRAPPER_HACK(URLSearchParams, Web::URL)
URLSearchParamsWrapper* wrap(JS::Realm&, URL::URLSearchParams&);
}

View file

@ -6,32 +6,45 @@
#include <LibJS/Runtime/Array.h> #include <LibJS/Runtime/Array.h>
#include <LibJS/Runtime/IteratorOperations.h> #include <LibJS/Runtime/IteratorOperations.h>
#include <LibWeb/Bindings/URLSearchParamsIteratorWrapper.h> #include <LibWeb/Bindings/URLSearchParamsIteratorPrototype.h>
#include <LibWeb/Bindings/Wrapper.h> #include <LibWeb/HTML/Window.h>
#include <LibWeb/URL/URLSearchParamsIterator.h> #include <LibWeb/URL/URLSearchParamsIterator.h>
namespace Web::URL { 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(); return *url_search_params.heap().allocate<URLSearchParamsIterator>(url_search_params.realm(), url_search_params, iteration_kind);
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);
} }
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) 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 #pragma once
#include "URLSearchParams.h" #include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/URL/URLSearchParams.h> #include <LibWeb/URL/URLSearchParams.h>
namespace Web::URL { namespace Web::URL {
class URLSearchParamsIterator : public Bindings::Wrappable class URLSearchParamsIterator : public Bindings::PlatformObject {
, public RefCounted<URLSearchParamsIterator> { WEB_PLATFORM_OBJECT(URLSearchParamsIterator, Bindings::PlatformObject);
public:
using WrapperType = Bindings::URLSearchParamsIteratorWrapper;
static NonnullRefPtr<URLSearchParamsIterator> create(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind) public:
{ static JS::NonnullGCPtr<URLSearchParamsIterator> create(URLSearchParams const&, JS::Object::PropertyKind iteration_kind);
return adopt_ref(*new URLSearchParamsIterator(url_search_params, iteration_kind));
} virtual ~URLSearchParamsIterator() override;
JS::Object* next(); JS::Object* next();
void visit_edges(JS::Cell::Visitor&);
private: private:
explicit URLSearchParamsIterator(URLSearchParams const& url_search_params, JS::Object::PropertyKind iteration_kind) URLSearchParamsIterator(URLSearchParams const&, JS::Object::PropertyKind iteration_kind);
: m_url_search_params(url_search_params)
, m_iteration_kind(iteration_kind) virtual void visit_edges(Cell::Visitor&) override;
{
}
URLSearchParams const& m_url_search_params; URLSearchParams const& m_url_search_params;
JS::Object::PropertyKind m_iteration_kind; JS::Object::PropertyKind m_iteration_kind;
size_t m_index { 0 }; 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())); source = TRY(Bindings::IDL::get_buffer_source_copy(*buffer_source.cell()));
return {}; 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. // 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(); source = url_search_params->to_string().to_byte_buffer();
// Set type to `application/x-www-form-urlencoded;charset=UTF-8`. // Set type to `application/x-www-form-urlencoded;charset=UTF-8`.

View file

@ -24,7 +24,7 @@
namespace Web::XHR { namespace Web::XHR {
// https://fetch.spec.whatwg.org/#typedefdef-xmlhttprequestbodyinit // 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 { class XMLHttpRequest final : public XMLHttpRequestEventTarget {
WEB_PLATFORM_OBJECT(XMLHttpRequest, 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/KeyboardEvent NO_INSTANCE)
libweb_js_wrapper(UIEvents/MouseEvent NO_INSTANCE) libweb_js_wrapper(UIEvents/MouseEvent NO_INSTANCE)
libweb_js_wrapper(UIEvents/UIEvent NO_INSTANCE) libweb_js_wrapper(UIEvents/UIEvent NO_INSTANCE)
libweb_js_wrapper(URL/URL) libweb_js_wrapper(URL/URL NO_INSTANCE)
libweb_js_wrapper(URL/URLSearchParams ITERABLE) libweb_js_wrapper(URL/URLSearchParams ITERABLE NO_INSTANCE)
libweb_js_wrapper(WebGL/WebGLContextEvent NO_INSTANCE) libweb_js_wrapper(WebGL/WebGLContextEvent NO_INSTANCE)
libweb_js_wrapper(WebGL/WebGLRenderingContext NO_INSTANCE) libweb_js_wrapper(WebGL/WebGLRenderingContext NO_INSTANCE)
libweb_js_wrapper(WebSockets/WebSocket NO_INSTANCE) libweb_js_wrapper(WebSockets/WebSocket NO_INSTANCE)