LibWeb: Move passing of Web object prototypes out of constructors

This commit is contained in:
Timothy Flynn 2023-01-10 06:56:59 -05:00 committed by Andreas Kling
parent 834202aeb9
commit af75493883
Notes: sideshowbarker 2024-07-17 01:51:06 +09:00
35 changed files with 157 additions and 23 deletions

View file

@ -10,8 +10,8 @@
namespace Web::Bindings {
LegacyPlatformObject::LegacyPlatformObject(JS::Object& prototype)
: PlatformObject(prototype)
LegacyPlatformObject::LegacyPlatformObject(JS::Realm& realm)
: PlatformObject(realm)
{
}

View file

@ -34,7 +34,7 @@ public:
virtual bool is_supported_property_index(u32) const;
protected:
explicit LegacyPlatformObject(JS::Object& prototype);
explicit LegacyPlatformObject(JS::Realm& realm);
};
}

View file

@ -0,0 +1,23 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/WindowPrototype.h>
namespace Web::Bindings {
WindowPrototype::WindowPrototype(JS::Realm& realm)
: JS::Object(realm, nullptr)
{
}
void WindowPrototype::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::EventTargetPrototype>(realm, "EventTarget"));
}
}

View file

@ -16,10 +16,10 @@ class WindowPrototype final : public JS::Object {
JS_OBJECT(WindowPrototype, JS::Object);
public:
explicit WindowPrototype(JS::Realm& realm)
: JS::Object(ConstructWithPrototypeTag::Tag, cached_web_prototype(realm, "EventTarget"))
{
}
explicit WindowPrototype(JS::Realm& realm);
private:
virtual void initialize(JS::Realm&) override;
};
}

View file

@ -14,6 +14,7 @@ set(SOURCES
Bindings/OptionConstructor.cpp
Bindings/PlatformObject.cpp
Bindings/WindowConstructor.cpp
Bindings/WindowPrototype.cpp
Crypto/Crypto.cpp
Crypto/SubtleCrypto.cpp
CSS/Angle.cpp

View file

@ -26,7 +26,7 @@ CSSRuleList* CSSRuleList::create(JS::Realm& realm, JS::MarkedVector<CSSRule*> co
}
CSSRuleList::CSSRuleList(JS::Realm& realm)
: Bindings::LegacyPlatformObject(Bindings::ensure_web_prototype<Bindings::CSSRuleListPrototype>(realm, "CSSRuleList"))
: Bindings::LegacyPlatformObject(realm)
{
}
@ -35,6 +35,12 @@ CSSRuleList* CSSRuleList::create_empty(JS::Realm& realm)
return realm.heap().allocate<CSSRuleList>(realm, realm);
}
void CSSRuleList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::CSSRuleListPrototype>(realm, "CSSRuleList"));
}
void CSSRuleList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -66,6 +66,7 @@ public:
private:
explicit CSSRuleList(JS::Realm&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Vector<CSSRule&> m_rules;

View file

@ -18,11 +18,17 @@ MediaList* MediaList::create(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&&
}
MediaList::MediaList(JS::Realm& realm, NonnullRefPtrVector<MediaQuery>&& media)
: Bindings::LegacyPlatformObject(Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"))
: Bindings::LegacyPlatformObject(realm)
, m_media(move(media))
{
}
void MediaList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::MediaListPrototype>(realm, "MediaList"));
}
// https://www.w3.org/TR/cssom-1/#dom-medialist-mediatext
DeprecatedString MediaList::media_text() const
{

View file

@ -39,6 +39,8 @@ public:
private:
MediaList(JS::Realm&, NonnullRefPtrVector<MediaQuery>&&);
virtual void initialize(JS::Realm&) override;
NonnullRefPtrVector<MediaQuery> m_media;
};

View file

@ -52,11 +52,17 @@ StyleSheetList* StyleSheetList::create(DOM::Document& document)
}
StyleSheetList::StyleSheetList(DOM::Document& document)
: Bindings::LegacyPlatformObject(Bindings::ensure_web_prototype<Bindings::StyleSheetListPrototype>(document.realm(), "StyleSheetList"))
: Bindings::LegacyPlatformObject(document.realm())
, m_document(document)
{
}
void StyleSheetList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::StyleSheetListPrototype>(realm, "StyleSheetList"));
}
void StyleSheetList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -41,6 +41,7 @@ public:
private:
explicit StyleSheetList(DOM::Document&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
void sort_sheets();

View file

@ -11,7 +11,7 @@
namespace Web::DOM {
AbstractRange::AbstractRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset)
: Bindings::PlatformObject(Bindings::cached_web_prototype(start_container.realm(), "AbstractRange"))
: Bindings::PlatformObject(start_container.realm())
, m_start_container(start_container)
, m_start_offset(start_offset)
, m_end_container(end_container)
@ -21,6 +21,12 @@ AbstractRange::AbstractRange(Node& start_container, u32 start_offset, Node& end_
AbstractRange::~AbstractRange() = default;
void AbstractRange::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::AbstractRangePrototype>(realm, "AbstractRange"));
}
void AbstractRange::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -36,6 +36,7 @@ public:
protected:
AbstractRange(Node& start_container, u32 start_offset, Node& end_container, u32 end_offset);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
JS::NonnullGCPtr<Node> m_start_container;

View file

@ -24,13 +24,19 @@ JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document
}
DOMImplementation::DOMImplementation(Document& document)
: PlatformObject(Bindings::cached_web_prototype(document.realm(), "DOMImplementation"))
: PlatformObject(document.realm())
, m_document(document)
{
}
DOMImplementation::~DOMImplementation() = default;
void DOMImplementation::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMImplementationPrototype>(realm, "DOMImplementation"));
}
void DOMImplementation::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -30,6 +30,7 @@ public:
private:
explicit DOMImplementation(Document&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Document& document() { return m_document; }

View file

@ -60,7 +60,7 @@ DOMTokenList* DOMTokenList::create(Element const& associated_element, Deprecated
// https://dom.spec.whatwg.org/#ref-for-domtokenlist%E2%91%A0%E2%91%A2
DOMTokenList::DOMTokenList(Element const& associated_element, DeprecatedFlyString associated_attribute)
: Bindings::LegacyPlatformObject(Bindings::cached_web_prototype(associated_element.realm(), "DOMTokenList"))
: Bindings::LegacyPlatformObject(associated_element.realm())
, m_associated_element(associated_element)
, m_associated_attribute(move(associated_attribute))
{
@ -68,6 +68,12 @@ DOMTokenList::DOMTokenList(Element const& associated_element, DeprecatedFlyStrin
associated_attribute_changed(value);
}
void DOMTokenList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMTokenListPrototype>(realm, "DOMTokenList"));
}
void DOMTokenList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -45,6 +45,7 @@ public:
private:
DOMTokenList(Element const& associated_element, DeprecatedFlyString associated_attribute);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
WebIDL::ExceptionOr<void> validate_token(StringView token) const;

View file

@ -25,14 +25,14 @@ JS::NonnullGCPtr<Event> Event::construct_impl(JS::Realm& realm, DeprecatedFlyStr
}
Event::Event(JS::Realm& realm, DeprecatedFlyString const& type)
: PlatformObject(Bindings::cached_web_prototype(realm, "Event"))
: PlatformObject(realm)
, m_type(type)
, m_initialized(true)
{
}
Event::Event(JS::Realm& realm, DeprecatedFlyString const& type, EventInit const& event_init)
: PlatformObject(Bindings::cached_web_prototype(realm, "Event"))
: PlatformObject(realm)
, m_type(type)
, m_bubbles(event_init.bubbles)
, m_cancelable(event_init.cancelable)
@ -41,6 +41,12 @@ Event::Event(JS::Realm& realm, DeprecatedFlyString const& type, EventInit const&
{
}
void Event::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::EventPrototype>(realm, "Event"));
}
void Event::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -145,6 +145,8 @@ public:
protected:
void initialize_event(DeprecatedString const&, bool, bool);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Visitor&) override;
private:

View file

@ -19,7 +19,7 @@ JS::NonnullGCPtr<HTMLCollection> HTMLCollection::create(ParentNode& root, Functi
}
HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
: LegacyPlatformObject(Bindings::cached_web_prototype(root.realm(), "HTMLCollection"))
: LegacyPlatformObject(root.realm())
, m_root(root)
, m_filter(move(filter))
{
@ -27,6 +27,12 @@ HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)>
HTMLCollection::~HTMLCollection() = default;
void HTMLCollection::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::HTMLCollectionPrototype>(realm, "HTMLCollection"));
}
void HTMLCollection::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -47,6 +47,8 @@ public:
protected:
HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter);
virtual void initialize(JS::Realm&) override;
JS::NonnullGCPtr<ParentNode> root() { return *m_root; }
private:

View file

@ -20,11 +20,17 @@ JS::NonnullGCPtr<NamedNodeMap> NamedNodeMap::create(Element& element)
}
NamedNodeMap::NamedNodeMap(Element& element)
: Bindings::LegacyPlatformObject(Bindings::cached_web_prototype(element.realm(), "NamedNodeMap"))
: Bindings::LegacyPlatformObject(element.realm())
, m_element(element)
{
}
void NamedNodeMap::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::NamedNodeMapPrototype>(realm, "NamedNodeMap"));
}
void NamedNodeMap::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -56,6 +56,7 @@ public:
private:
explicit NamedNodeMap(Element&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Element& associated_element() { return *m_element; }

View file

@ -12,7 +12,7 @@
namespace Web::DOM {
NodeIterator::NodeIterator(Node& root)
: PlatformObject(Bindings::cached_web_prototype(root.realm(), "NodeIterator"))
: PlatformObject(root.realm())
, m_root(root)
, m_reference({ root })
{
@ -21,6 +21,12 @@ NodeIterator::NodeIterator(Node& root)
NodeIterator::~NodeIterator() = default;
void NodeIterator::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::NodeIteratorPrototype>(realm, "NodeIterator"));
}
void NodeIterator::finalize()
{
Base::finalize();

View file

@ -37,6 +37,7 @@ public:
private:
explicit NodeIterator(Node& root);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
virtual void finalize() override;

View file

@ -11,12 +11,18 @@
namespace Web::DOM {
NodeList::NodeList(JS::Realm& realm)
: LegacyPlatformObject(Bindings::cached_web_prototype(realm, "NodeList"))
: LegacyPlatformObject(realm)
{
}
NodeList::~NodeList() = default;
void NodeList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::NodeListPrototype>(realm, "NodeList"));
}
JS::Value NodeList::item_value(size_t index) const
{
auto* node = item(index);

View file

@ -26,6 +26,8 @@ public:
protected:
explicit NodeList(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
}

View file

@ -14,7 +14,7 @@
namespace Web::DOM {
TreeWalker::TreeWalker(Node& root)
: PlatformObject(Bindings::cached_web_prototype(root.realm(), "TreeWalker"))
: PlatformObject(root.realm())
, m_root(root)
, m_current(root)
{
@ -22,6 +22,12 @@ TreeWalker::TreeWalker(Node& root)
TreeWalker::~TreeWalker() = default;
void TreeWalker::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::TreeWalkerPrototype>(realm, "TreeWalker"));
}
void TreeWalker::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -39,6 +39,7 @@ public:
private:
explicit TreeWalker(Node& root);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
enum class ChildTraversalType {

View file

@ -17,13 +17,19 @@ JS::NonnullGCPtr<FileList> FileList::create(JS::Realm& realm, Vector<JS::Nonnull
}
FileList::FileList(JS::Realm& realm, Vector<JS::NonnullGCPtr<File>>&& files)
: Bindings::LegacyPlatformObject(Bindings::cached_web_prototype(realm, "FileList"))
: Bindings::LegacyPlatformObject(realm)
, m_files(move(files))
{
}
FileList::~FileList() = default;
void FileList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::FileListPrototype>(realm, "FileList"));
}
// https://w3c.github.io/FileAPI/#dfn-item
bool FileList::is_supported_property_index(u32 index) const
{

View file

@ -35,6 +35,7 @@ public:
private:
FileList(JS::Realm&, Vector<JS::NonnullGCPtr<File>>&&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Vector<JS::NonnullGCPtr<File>> m_files;

View file

@ -20,13 +20,19 @@ JS::NonnullGCPtr<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<JS::H
}
DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects)
: Bindings::LegacyPlatformObject(Bindings::cached_web_prototype(realm, "DOMRectList"))
: Bindings::LegacyPlatformObject(realm)
, m_rects(move(rects))
{
}
DOMRectList::~DOMRectList() = default;
void DOMRectList::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList"));
}
// https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
u32 DOMRectList::length() const
{

View file

@ -30,6 +30,8 @@ public:
private:
DOMRectList(JS::Realm&, Vector<JS::NonnullGCPtr<DOMRect>>);
virtual void initialize(JS::Realm&) override;
Vector<JS::NonnullGCPtr<DOMRect>> m_rects;
};

View file

@ -19,13 +19,19 @@ JS::NonnullGCPtr<DOMStringMap> DOMStringMap::create(DOM::Element& element)
}
DOMStringMap::DOMStringMap(DOM::Element& element)
: LegacyPlatformObject(Bindings::cached_web_prototype(element.realm(), "DOMStringMap"))
: LegacyPlatformObject(element.realm())
, m_associated_element(element)
{
}
DOMStringMap::~DOMStringMap() = default;
void DOMStringMap::initialize(JS::Realm& realm)
{
Base::initialize(realm);
set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMStringMapPrototype>(realm, "DOMStringMap"));
}
void DOMStringMap::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);

View file

@ -31,6 +31,7 @@ public:
private:
explicit DOMStringMap(DOM::Element&);
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
// ^LegacyPlatformObject