Browse Source

LibWeb: Make DOMImplementation GC-allocated

Andreas Kling 2 years ago
parent
commit
967a3e5a45

+ 19 - 1
Userland/Libraries/LibWeb/DOM/DOMImplementation.cpp

@@ -1,9 +1,12 @@
 /*
 /*
  * Copyright (c) 2020, the SerenityOS developers.
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
 
 
+#include <LibWeb/Bindings/DOMImplementationPrototype.h>
+#include <LibWeb/Bindings/WindowObject.h>
 #include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/DOMImplementation.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/DocumentType.h>
 #include <LibWeb/DOM/DocumentType.h>
@@ -14,9 +17,24 @@
 
 
 namespace Web::DOM {
 namespace Web::DOM {
 
 
+JS::NonnullGCPtr<DOMImplementation> DOMImplementation::create(Document& document)
+{
+    auto& window_object = document.preferred_window_object();
+    return *window_object.heap().allocate<DOMImplementation>(window_object.realm(), document);
+}
+
 DOMImplementation::DOMImplementation(Document& document)
 DOMImplementation::DOMImplementation(Document& document)
-    : RefCountForwarder(document)
+    : PlatformObject(document.preferred_window_object().ensure_web_prototype<Bindings::DOMImplementationPrototype>("DOMImplementation"))
+    , m_document(document)
+{
+}
+
+DOMImplementation::~DOMImplementation() = default;
+
+void DOMImplementation::visit_edges(Cell::Visitor& visitor)
 {
 {
+    Base::visit_edges(visitor);
+    visitor.visit(m_document);
 }
 }
 
 
 // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument
 // https://dom.spec.whatwg.org/#dom-domimplementation-createdocument

+ 19 - 16
Userland/Libraries/LibWeb/DOM/DOMImplementation.h

@@ -1,5 +1,6 @@
 /*
 /*
  * Copyright (c) 2020, the SerenityOS developers.
  * Copyright (c) 2020, the SerenityOS developers.
+ * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
  *
  *
  * SPDX-License-Identifier: BSD-2-Clause
  * SPDX-License-Identifier: BSD-2-Clause
  */
  */
@@ -7,25 +8,20 @@
 #pragma once
 #pragma once
 
 
 #include <AK/NonnullRefPtr.h>
 #include <AK/NonnullRefPtr.h>
-#include <AK/RefCountForwarder.h>
-#include <AK/RefCounted.h>
-#include <AK/Weakable.h>
-#include <LibWeb/Bindings/Wrappable.h>
+#include <LibWeb/Bindings/PlatformObject.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/Document.h>
 
 
 namespace Web::DOM {
 namespace Web::DOM {
 
 
-class DOMImplementation final
-    : public RefCountForwarder<Document>
-    , public Weakable<DOMImplementation>
-    , public Bindings::Wrappable {
+class DOMImplementation final : public Bindings::PlatformObject {
+    JS_OBJECT(DOMImplementation, Bindings::PlatformObject);
+
 public:
 public:
-    using WrapperType = Bindings::DOMImplementationWrapper;
+    static JS::NonnullGCPtr<DOMImplementation> create(Document&);
+    explicit DOMImplementation(Document&);
+    virtual ~DOMImplementation();
 
 
-    static NonnullOwnPtr<DOMImplementation> create(Badge<Document>, Document& document)
-    {
-        return adopt_own(*new DOMImplementation(document));
-    }
+    DOMImplementation& impl() { return *this; }
 
 
     ExceptionOr<NonnullRefPtr<Document>> create_document(String const&, String const&, RefPtr<DocumentType>) const;
     ExceptionOr<NonnullRefPtr<Document>> create_document(String const&, String const&, RefPtr<DocumentType>) const;
     NonnullRefPtr<Document> create_html_document(String const& title) const;
     NonnullRefPtr<Document> create_html_document(String const& title) const;
@@ -35,10 +31,17 @@ public:
     bool has_feature() const { return true; }
     bool has_feature() const { return true; }
 
 
 private:
 private:
-    explicit DOMImplementation(Document&);
+    virtual void visit_edges(Cell::Visitor&) override;
 
 
-    Document& document() { return ref_count_target(); }
-    Document const& document() const { return ref_count_target(); }
+    Document& document() { return m_document; }
+    Document const& document() const { return m_document; }
+
+    Document& m_document;
 };
 };
 
 
 }
 }
+
+namespace Web::Bindings {
+inline JS::Object* wrap(JS::Realm&, Web::DOM::DOMImplementation& object) { return &object; }
+using DOMImplementationWrapper = Web::DOM::DOMImplementation;
+}

+ 4 - 3
Userland/Libraries/LibWeb/DOM/Document.cpp

@@ -281,7 +281,6 @@ Document::Document(const AK::URL& url)
     , m_style_computer(make<CSS::StyleComputer>(*this))
     , m_style_computer(make<CSS::StyleComputer>(*this))
     , m_url(url)
     , m_url(url)
     , m_window(HTML::Window::create_with_document(*this))
     , m_window(HTML::Window::create_with_document(*this))
-    , m_implementation(DOMImplementation::create({}, *this))
     , m_history(HTML::History::create(*this))
     , m_history(HTML::History::create(*this))
 {
 {
     m_style_sheets = JS::make_handle(CSS::StyleSheetList::create(*this));
     m_style_sheets = JS::make_handle(CSS::StyleSheetList::create(*this));
@@ -1621,9 +1620,11 @@ void Document::evaluate_media_rules()
     }
     }
 }
 }
 
 
-NonnullRefPtr<DOMImplementation> Document::implementation() const
+DOMImplementation* Document::implementation()
 {
 {
-    return *m_implementation;
+    if (!m_implementation.cell())
+        m_implementation = JS::make_handle(*DOMImplementation::create(*this));
+    return m_implementation.cell();
 }
 }
 
 
 bool Document::has_focus() const
 bool Document::has_focus() const

+ 2 - 2
Userland/Libraries/LibWeb/DOM/Document.h

@@ -300,7 +300,7 @@ public:
 
 
     void completely_finish_loading();
     void completely_finish_loading();
 
 
-    NonnullRefPtr<DOMImplementation> implementation() const;
+    DOMImplementation* implementation();
 
 
     RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
     RefPtr<HTML::HTMLScriptElement> current_script() const { return m_current_script; }
     void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
     void set_current_script(Badge<HTML::HTMLScriptElement>, RefPtr<HTML::HTMLScriptElement> script) { m_current_script = move(script); }
@@ -457,7 +457,7 @@ private:
 
 
     bool m_ready_for_post_load_tasks { false };
     bool m_ready_for_post_load_tasks { false };
 
 
-    NonnullOwnPtr<DOMImplementation> m_implementation;
+    JS::Handle<DOMImplementation> m_implementation;
     RefPtr<HTML::HTMLScriptElement> m_current_script;
     RefPtr<HTML::HTMLScriptElement> m_current_script;
 
 
     bool m_should_invalidate_styles_on_attribute_changes { true };
     bool m_should_invalidate_styles_on_attribute_changes { true };

+ 0 - 1
Userland/Libraries/LibWeb/Forward.h

@@ -464,7 +464,6 @@ class DocumentFragmentWrapper;
 class DocumentTypeWrapper;
 class DocumentTypeWrapper;
 class DocumentWrapper;
 class DocumentWrapper;
 class DOMExceptionWrapper;
 class DOMExceptionWrapper;
-class DOMImplementationWrapper;
 class DOMParserWrapper;
 class DOMParserWrapper;
 class DOMPointWrapper;
 class DOMPointWrapper;
 class DOMPointReadOnlyWrapper;
 class DOMPointReadOnlyWrapper;

+ 1 - 1
Userland/Libraries/LibWeb/idl_files.cmake

@@ -32,7 +32,7 @@ libweb_js_wrapper(DOM/Document)
 libweb_js_wrapper(DOM/DocumentFragment)
 libweb_js_wrapper(DOM/DocumentFragment)
 libweb_js_wrapper(DOM/DocumentType)
 libweb_js_wrapper(DOM/DocumentType)
 libweb_js_wrapper(DOM/DOMException)
 libweb_js_wrapper(DOM/DOMException)
-libweb_js_wrapper(DOM/DOMImplementation)
+libweb_js_wrapper(DOM/DOMImplementation NO_INSTANCE)
 libweb_js_wrapper(DOM/DOMTokenList NO_INSTANCE)
 libweb_js_wrapper(DOM/DOMTokenList NO_INSTANCE)
 libweb_js_wrapper(DOM/Element)
 libweb_js_wrapper(DOM/Element)
 libweb_js_wrapper(DOM/Event)
 libweb_js_wrapper(DOM/Event)