Bläddra i källkod

LibGfx+LibWeb: Propagate OOM when creating PaintStyles

MacDue 2 år sedan
förälder
incheckning
3a0a7e3e96

+ 14 - 14
Userland/Libraries/LibGfx/PaintStyle.h

@@ -43,9 +43,9 @@ private:
 
 class SolidColorPaintStyle final : public PaintStyle {
 public:
-    static NonnullRefPtr<SolidColorPaintStyle> create(Color color)
+    static ErrorOr<NonnullRefPtr<SolidColorPaintStyle>> create(Color color)
     {
-        return adopt_ref(*new SolidColorPaintStyle(color));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) SolidColorPaintStyle(color));
     }
 
     virtual Color sample_color(IntPoint) const override { return m_color; }
@@ -91,9 +91,9 @@ private:
 
 class LinearGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<LinearGradientPaintStyle> create(float angle = 0.0f)
+    static ErrorOr<ErrorOr<NonnullRefPtr<LinearGradientPaintStyle>>> create(float angle = 0.0f)
     {
-        return adopt_ref(*new LinearGradientPaintStyle(angle));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) LinearGradientPaintStyle(angle));
     }
 
 private:
@@ -109,9 +109,9 @@ private:
 
 class ConicGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<ConicGradientPaintStyle> create(IntPoint center, float start_angle = 0.0f)
+    static ErrorOr<NonnullRefPtr<ConicGradientPaintStyle>> create(IntPoint center, float start_angle = 0.0f)
     {
-        return adopt_ref(*new ConicGradientPaintStyle(center, start_angle));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) ConicGradientPaintStyle(center, start_angle));
     }
 
 private:
@@ -129,9 +129,9 @@ private:
 
 class RadialGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<RadialGradientPaintStyle> create(IntPoint center, IntSize size)
+    static ErrorOr<NonnullRefPtr<RadialGradientPaintStyle>> create(IntPoint center, IntSize size)
     {
-        return adopt_ref(*new RadialGradientPaintStyle(center, size));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) RadialGradientPaintStyle(center, size));
     }
 
 private:
@@ -153,9 +153,9 @@ private:
 
 class CanvasLinearGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<CanvasLinearGradientPaintStyle> create(FloatPoint p0, FloatPoint p1)
+    static ErrorOr<NonnullRefPtr<CanvasLinearGradientPaintStyle>> create(FloatPoint p0, FloatPoint p1)
     {
-        return adopt_ref(*new CanvasLinearGradientPaintStyle(p0, p1));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasLinearGradientPaintStyle(p0, p1));
     }
 
 private:
@@ -173,9 +173,9 @@ private:
 
 class CanvasConicGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<CanvasConicGradientPaintStyle> create(FloatPoint center, float start_angle = 0.0f)
+    static ErrorOr<NonnullRefPtr<CanvasConicGradientPaintStyle>> create(FloatPoint center, float start_angle = 0.0f)
     {
-        return adopt_ref(*new CanvasConicGradientPaintStyle(center, start_angle));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasConicGradientPaintStyle(center, start_angle));
     }
 
 private:
@@ -193,9 +193,9 @@ private:
 
 class CanvasRadialGradientPaintStyle final : public GradientPaintStyle {
 public:
-    static NonnullRefPtr<CanvasRadialGradientPaintStyle> create(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
+    static ErrorOr<NonnullRefPtr<CanvasRadialGradientPaintStyle>> create(FloatPoint start_center, float start_radius, FloatPoint end_center, float end_radius)
     {
-        return adopt_ref(*new CanvasRadialGradientPaintStyle(start_center, start_radius, end_center, end_radius));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasRadialGradientPaintStyle(start_center, start_radius, end_center, end_radius));
     }
 
 private:

+ 1 - 1
Userland/Libraries/LibWeb/HTML/Canvas/CanvasState.cpp

@@ -45,7 +45,7 @@ NonnullRefPtr<Gfx::PaintStyle> CanvasState::FillOrStrokeStyle::to_gfx_paint_styl
     return m_fill_or_stroke_style.visit(
         [&](Gfx::Color color) -> NonnullRefPtr<Gfx::PaintStyle> {
             if (!m_color_paint_style)
-                m_color_paint_style = Gfx::SolidColorPaintStyle::create(color);
+                m_color_paint_style = Gfx::SolidColorPaintStyle::create(color).release_value_but_fixme_should_propagate_errors();
             return m_color_paint_style.release_nonnull();
         },
         [&](auto handle) {

+ 3 - 3
Userland/Libraries/LibWeb/HTML/CanvasGradient.cpp

@@ -21,21 +21,21 @@ WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_rad
     if (r1 < 0)
         return WebIDL::IndexSizeError::create(realm, "The r1 passed is less than 0");
 
-    auto radial_gradient = Gfx::CanvasRadialGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, r0, Gfx::FloatPoint { x1, y1 }, r1);
+    auto radial_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasRadialGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, r0, Gfx::FloatPoint { x1, y1 }, r1));
     return MUST_OR_THROW_OOM(realm.heap().allocate<CanvasGradient>(realm, realm, *radial_gradient));
 }
 
 // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createlineargradient
 WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_linear(JS::Realm& realm, double x0, double y0, double x1, double y1)
 {
-    auto linear_gradient = Gfx::CanvasLinearGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, Gfx::FloatPoint { x1, y1 });
+    auto linear_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasLinearGradientPaintStyle::create(Gfx::FloatPoint { x0, y0 }, Gfx::FloatPoint { x1, y1 }));
     return MUST_OR_THROW_OOM(realm.heap().allocate<CanvasGradient>(realm, realm, *linear_gradient));
 }
 
 // https://html.spec.whatwg.org/multipage/canvas.html#dom-context-2d-createconicgradient
 WebIDL::ExceptionOr<JS::NonnullGCPtr<CanvasGradient>> CanvasGradient::create_conic(JS::Realm& realm, double start_angle, double x, double y)
 {
-    auto conic_gradient = Gfx::CanvasConicGradientPaintStyle::create(Gfx::FloatPoint { x, y }, start_angle);
+    auto conic_gradient = TRY_OR_THROW_OOM(realm.vm(), Gfx::CanvasConicGradientPaintStyle::create(Gfx::FloatPoint { x, y }, start_angle));
     return MUST_OR_THROW_OOM(realm.heap().allocate<CanvasGradient>(realm, realm, *conic_gradient));
 }
 

+ 1 - 1
Userland/Libraries/LibWeb/HTML/CanvasPattern.cpp

@@ -125,7 +125,7 @@ WebIDL::ExceptionOr<JS::GCPtr<CanvasPattern>> CanvasPattern::create(JS::Realm& r
     auto const& bitmap = *image.visit([](auto const& source) -> Gfx::Bitmap const* { return source->bitmap(); });
 
     // 6. Let pattern be a new CanvasPattern object with the image image and the repetition behavior given by repetition.
-    auto pattern = CanvasPatternPaintStyle::create(bitmap, *repetition_value);
+    auto pattern = TRY_OR_THROW_OOM(realm.vm(), CanvasPatternPaintStyle::create(bitmap, *repetition_value));
 
     // FIXME: 7. If image is not origin-clean, then mark pattern as not origin-clean.
 

+ 2 - 2
Userland/Libraries/LibWeb/HTML/CanvasPattern.h

@@ -21,9 +21,9 @@ public:
         NoRepeat
     };
 
-    static NonnullRefPtr<CanvasPatternPaintStyle> create(Gfx::Bitmap const& bitmap, Repetition repetition)
+    static ErrorOr<NonnullRefPtr<CanvasPatternPaintStyle>> create(Gfx::Bitmap const& bitmap, Repetition repetition)
     {
-        return adopt_ref(*new CanvasPatternPaintStyle(bitmap, repetition));
+        return adopt_nonnull_ref_or_enomem(new (nothrow) CanvasPatternPaintStyle(bitmap, repetition));
     }
 
     virtual void paint(Gfx::IntRect physical_bounding_box, PaintFunction paint) const override;