[gjs/simplify-private-pointers: 35/35] cairo: Use cairo_surface_t as the private pointer for Cairo.Surface



commit a01f003077eb439bbf4f9ffd3ace7d7098917821
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun May 3 09:09:15 2020 -0700

    cairo: Use cairo_surface_t as the private pointer for Cairo.Surface
    
    Instead of allocating a separate private struct that allocates a
    cairo_surface_t, use the cairo_surface_t directly as the private struct.
    This makes the code simpler and saves 8 bytes per object.

 modules/cairo-image-surface.cpp |  6 +++---
 modules/cairo-pdf-surface.cpp   |  4 ++--
 modules/cairo-private.h         |  4 +---
 modules/cairo-ps-surface.cpp    |  4 ++--
 modules/cairo-surface.cpp       | 42 +++++++++--------------------------------
 modules/cairo-svg-surface.cpp   |  4 ++--
 6 files changed, 19 insertions(+), 45 deletions(-)
---
diff --git a/modules/cairo-image-surface.cpp b/modules/cairo-image-surface.cpp
index da7655aa..407b9a7e 100644
--- a/modules/cairo-image-surface.cpp
+++ b/modules/cairo-image-surface.cpp
@@ -64,7 +64,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_image_surface)
     if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
         return false;
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
     cairo_surface_destroy(surface);
 
     GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_image_surface);
@@ -110,7 +110,7 @@ createFromPNG_func(JSContext *context,
         gjs_throw(context, "failed to create surface");
         return false;
     }
-    gjs_cairo_surface_construct(context, surface_wrapper, surface);
+    gjs_cairo_surface_construct(surface_wrapper, surface);
     cairo_surface_destroy(surface);
 
     argv.rval().setObject(*surface_wrapper);
@@ -256,7 +256,7 @@ gjs_cairo_image_surface_from_surface(JSContext       *context,
         return nullptr;
     }
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
 
     return object;
 }
diff --git a/modules/cairo-pdf-surface.cpp b/modules/cairo-pdf-surface.cpp
index 683391a8..546509a0 100644
--- a/modules/cairo-pdf-surface.cpp
+++ b/modules/cairo-pdf-surface.cpp
@@ -70,7 +70,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_pdf_surface)
                                 "surface"))
         return false;
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
     cairo_surface_destroy(surface);
 
     GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_pdf_surface);
@@ -113,7 +113,7 @@ gjs_cairo_pdf_surface_from_surface(JSContext       *context,
         return nullptr;
     }
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
 
     return object;
 }
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index 4923cab3..d41db0ef 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -80,9 +80,7 @@ bool gjs_cairo_surface_define_proto(JSContext              *cx,
                                     JS::HandleObject        module,
                                     JS::MutableHandleObject proto);
 
-void             gjs_cairo_surface_construct            (JSContext       *context,
-                                                         JS::HandleObject object,
-                                                         cairo_surface_t *surface);
+void gjs_cairo_surface_construct(JSObject* object, cairo_surface_t* surface);
 void             gjs_cairo_surface_finalize_surface     (JSFreeOp        *fop,
                                                          JSObject        *object);
 GJS_JSAPI_RETURN_CONVENTION
diff --git a/modules/cairo-ps-surface.cpp b/modules/cairo-ps-surface.cpp
index 6eb3a502..a83e7fc8 100644
--- a/modules/cairo-ps-surface.cpp
+++ b/modules/cairo-ps-surface.cpp
@@ -70,7 +70,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_ps_surface)
                                 "surface"))
         return false;
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
     cairo_surface_destroy(surface);
 
     GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_ps_surface);
@@ -121,7 +121,7 @@ gjs_cairo_ps_surface_from_surface(JSContext       *context,
         return nullptr;
     }
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
 
     return object;
 }
diff --git a/modules/cairo-surface.cpp b/modules/cairo-surface.cpp
index 1a7e14bf..4f7560b4 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -43,22 +43,15 @@
 #include "gjs/macros.h"
 #include "modules/cairo-private.h"
 
-typedef struct {
-    cairo_surface_t *surface;
-} GjsCairoSurface;
-
 GJS_DEFINE_PROTO_ABSTRACT_WITH_GTYPE("Surface", cairo_surface,
                                      CAIRO_GOBJECT_TYPE_SURFACE,
                                      JSCLASS_BACKGROUND_FINALIZE)
-GJS_DEFINE_PRIV_FROM_JS(GjsCairoSurface, gjs_cairo_surface_class)
 
 static void gjs_cairo_surface_finalize(JSFreeOp*, JSObject* obj) {
-    GjsCairoSurface *priv;
-    priv = (GjsCairoSurface*) JS_GetPrivate(obj);
-    if (!priv)
-        return;
-    cairo_surface_destroy(priv->surface);
-    g_slice_free(GjsCairoSurface, priv);
+    using AutoSurface =
+        GjsAutoPointer<cairo_surface_t, cairo_surface_t, cairo_surface_destroy>;
+    AutoSurface surface = static_cast<cairo_surface_t*>(JS_GetPrivate(obj));
+    JS_SetPrivate(obj, nullptr);
 }
 
 /* Properties */
@@ -142,7 +135,6 @@ JSFunctionSpec gjs_cairo_surface_static_funcs[] = { JS_FS_END };
 
 /**
  * gjs_cairo_surface_construct:
- * @context: the context
  * @object: object to construct
  * @surface: cairo_surface to attach to the object
  *
@@ -151,23 +143,12 @@ JSFunctionSpec gjs_cairo_surface_static_funcs[] = { JS_FS_END };
  *
  * This is mainly used for subclasses where object is already created.
  */
-void
-gjs_cairo_surface_construct(JSContext       *context,
-                            JS::HandleObject object,
-                            cairo_surface_t *surface)
-{
-    GjsCairoSurface *priv;
-
-    g_return_if_fail(context);
+void gjs_cairo_surface_construct(JSObject* object, cairo_surface_t* surface) {
     g_return_if_fail(object);
     g_return_if_fail(surface);
 
-    priv = g_slice_new0(GjsCairoSurface);
-
-    g_assert(!priv_from_js(context, object));
-    JS_SetPrivate(object, priv);
-
-    priv->surface = cairo_surface_reference(surface);
+    g_assert(!JS_GetPrivate(object));
+    JS_SetPrivate(object, cairo_surface_reference(surface));
 }
 
 /**
@@ -223,7 +204,7 @@ gjs_cairo_surface_from_surface(JSContext       *context,
         return nullptr;
     }
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
 
     return object;
 }
@@ -252,12 +233,7 @@ cairo_surface_t* gjs_cairo_surface_get_surface(
         return nullptr;
     }
 
-    auto* priv = static_cast<GjsCairoSurface*>(JS_GetPrivate(surface_wrapper));
-    if (!priv)
-        return nullptr;
-
-    g_assert(priv->surface);
-    return priv->surface;
+    return static_cast<cairo_surface_t*>(JS_GetPrivate(surface_wrapper));
 }
 
 GJS_USE
diff --git a/modules/cairo-svg-surface.cpp b/modules/cairo-svg-surface.cpp
index 0c6b2538..e620bfdb 100644
--- a/modules/cairo-svg-surface.cpp
+++ b/modules/cairo-svg-surface.cpp
@@ -70,7 +70,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(cairo_svg_surface)
                                 "surface"))
         return false;
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
     cairo_surface_destroy(surface);
 
     GJS_NATIVE_CONSTRUCTOR_FINISH(cairo_svg_surface);
@@ -113,7 +113,7 @@ gjs_cairo_svg_surface_from_surface(JSContext       *context,
         return nullptr;
     }
 
-    gjs_cairo_surface_construct(context, object, surface);
+    gjs_cairo_surface_construct(object, surface);
 
     return object;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]