소스 검색

LibWeb: Use JS::HeapFunction for resumption steps in HTMLImageElement

Aliaksandr Kalenik 1 년 전
부모
커밋
0c46d79e78

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

@@ -3282,7 +3282,7 @@ void Document::start_intersection_observing_a_lazy_loading_element(Element& elem
                 auto& entry = verify_cast<IntersectionObserver::IntersectionObserverEntry>(entries.get_without_side_effects(property_key).as_object());
 
                 // 1. Let resumptionSteps be null.
-                JS::SafeFunction<void()> resumption_steps;
+                JS::GCPtr<JS::HeapFunction<void()>> resumption_steps;
 
                 // 2. If entry.isIntersecting is true, then set resumptionSteps to entry.target's lazy load resumption steps.
                 if (entry.is_intersecting()) {
@@ -3306,7 +3306,7 @@ void Document::start_intersection_observing_a_lazy_loading_element(Element& elem
                 m_lazy_load_intersection_observer->unobserve(entry.target());
 
                 // 6. Invoke resumptionSteps.
-                resumption_steps();
+                resumption_steps->function()();
             }
 
             return JS::js_undefined();

+ 12 - 4
Userland/Libraries/LibWeb/HTML/HTMLImageElement.cpp

@@ -69,6 +69,7 @@ void HTMLImageElement::visit_edges(Cell::Visitor& visitor)
     Base::visit_edges(visitor);
     visitor.visit(m_current_request);
     visitor.visit(m_pending_request);
+    visitor.visit(m_lazy_load_resumption_steps);
 }
 
 void HTMLImageElement::apply_presentational_hints(CSS::StyleProperties& style) const
@@ -159,6 +160,11 @@ void HTMLImageElement::set_visible_in_viewport(bool)
     // FIXME: Loosen grip on image data when it's not visible, e.g via volatile memory.
 }
 
+void HTMLImageElement::set_lazy_load_resumption_steps(Function<void()> steps)
+{
+    m_lazy_load_resumption_steps = JS::create_heap_function(vm().heap(), move(steps));
+}
+
 // https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-width
 unsigned HTMLImageElement::width() const
 {
@@ -549,9 +555,9 @@ after_step_7:
         // 24. If the will lazy load element steps given the img return true, then:
         if (will_lazy_load()) {
             // 1. Set the img's lazy load resumption steps to the rest of this algorithm starting with the step labeled fetch the image.
-            m_lazy_load_resumption_steps = [this, request, image_request]() {
+            set_lazy_load_resumption_steps([this, request, image_request]() {
                 image_request->fetch_image(realm(), request);
-            };
+            });
 
             // 2. Start intersection-observing a lazy loading element for the img element.
             document().start_intersection_observing_a_lazy_loading_element(*this);
@@ -1022,9 +1028,11 @@ bool HTMLImageElement::will_lazy_load() const
     return lazy_loading() == LazyLoading::Lazy;
 }
 
-JS::SafeFunction<void()> HTMLImageElement::take_lazy_load_resumption_steps(Badge<DOM::Document>)
+JS::GCPtr<JS::HeapFunction<void()>> HTMLImageElement::take_lazy_load_resumption_steps(Badge<DOM::Document>)
 {
-    return move(m_lazy_load_resumption_steps);
+    auto lazy_load_resumption_steps = m_lazy_load_resumption_steps;
+    m_lazy_load_resumption_steps = nullptr;
+    return lazy_load_resumption_steps;
 }
 
 }

+ 4 - 2
Userland/Libraries/LibWeb/HTML/HTMLImageElement.h

@@ -9,6 +9,7 @@
 #include <AK/ByteBuffer.h>
 #include <AK/OwnPtr.h>
 #include <LibGfx/Forward.h>
+#include <LibJS/Heap/HeapFunction.h>
 #include <LibWeb/DOM/Document.h>
 #include <LibWeb/DOM/DocumentLoadEventDelayer.h>
 #include <LibWeb/HTML/BrowsingContext.h>
@@ -90,7 +91,8 @@ public:
     virtual RefPtr<Gfx::Bitmap const> current_image_bitmap(Gfx::IntSize = {}) const override;
     virtual void set_visible_in_viewport(bool) override;
 
-    JS::SafeFunction<void()> take_lazy_load_resumption_steps(Badge<DOM::Document>);
+    void set_lazy_load_resumption_steps(Function<void()>);
+    JS::GCPtr<JS::HeapFunction<void()>> take_lazy_load_resumption_steps(Badge<DOM::Document>);
 
     virtual void visit_edges(Cell::Visitor&) override;
 
@@ -136,7 +138,7 @@ private:
 
     // https://html.spec.whatwg.org/multipage/urls-and-fetching.html#lazy-load-resumption-steps
     // Each img and iframe element has associated lazy load resumption steps, initially null.
-    JS::SafeFunction<void()> m_lazy_load_resumption_steps;
+    JS::GCPtr<JS::HeapFunction<void()>> m_lazy_load_resumption_steps;
 
     CSSPixelSize m_last_seen_viewport_size;
 };