[gjs: 25/45] [cairo] Check status after all cairo calls



commit 0a580912172209d1d8df48a860b0aea2b5239df2
Author: Johan Dahlin <johan gnome org>
Date:   Thu Feb 25 22:31:26 2010 -0300

    [cairo] Check status after all cairo calls

 doc/cairo.txt                   |    1 -
 modules/cairo-context.c         |   29 +++++++++++++++++++++--------
 modules/cairo-gradient.c        |    7 +++++++
 modules/cairo-image-surface.c   |   17 ++++++-----------
 modules/cairo-linear-gradient.c |    9 +++------
 modules/cairo-pattern.c         |    3 +++
 modules/cairo-pdf-surface.c     |    8 +++-----
 modules/cairo-private.h         |    4 ++++
 modules/cairo-ps-surface.c      |   11 ++++++-----
 modules/cairo-radial-gradient.c |    9 +++------
 modules/cairo-surface-pattern.c |   23 +++++++++++++++++------
 modules/cairo-svg-surface.c     |   11 ++++++-----
 modules/cairo.c                 |   15 +++++++++++++++
 13 files changed, 94 insertions(+), 53 deletions(-)
---
diff --git a/doc/cairo.txt b/doc/cairo.txt
index ea39f0c..4594232 100644
--- a/doc/cairo.txt
+++ b/doc/cairo.txt
@@ -79,7 +79,6 @@ gjs> pattern.set_filter(Cairo.Filter.NEAREST);
 
 TODO:
 * string leaks in macros
-* status checking + raising after all calls
 * reference counting
 * context: wrap the remaning methods
 * surface methods
diff --git a/modules/cairo-context.c b/modules/cairo-context.c
index 3d5fe8d..e97faa3 100644
--- a/modules/cairo-context.c
+++ b/modules/cairo-context.c
@@ -36,8 +36,10 @@ mname##_func(JSContext *context,                    \
 {                                                   \
     cairo_t *cr;
 
-#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END          \
-    return JS_TRUE;                                 \
+#define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END                             \
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context")) \
+        return JS_FALSE;                                               \
+    return JS_TRUE;                                                    \
 }
 
 #define _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(m)                        \
@@ -228,7 +230,6 @@ gjs_cairo_context_constructor(JSContext *context,
     JSObject *surface_wrapper;
     cairo_surface_t *surface;
     cairo_t *cr;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -244,12 +245,9 @@ gjs_cairo_context_constructor(JSContext *context,
     }
 
     cr = cairo_create(surface);
-    status = cairo_status(cr);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Could not create context: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
         return JS_FALSE;
-    }
 
     _gjs_cairo_context_construct_internal(context, obj, cr);
 
@@ -376,6 +374,9 @@ mask_func(JSContext *context,
     cr = gjs_cairo_context_get_context(context, obj);
     cairo_mask(cr, pattern);
 
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
@@ -404,8 +405,12 @@ maskSurface_func(JSContext *context,
     }
 
     cr = gjs_cairo_context_get_context(context, obj);
+
     cairo_mask_surface(cr, surface, x, y);
 
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
@@ -431,8 +436,12 @@ setSource_func(JSContext *context,
     }
 
     cr = gjs_cairo_context_get_context(context, obj);
+
     cairo_set_source(cr, pattern);
 
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 static JSBool
@@ -460,8 +469,12 @@ setSourceSurface_func(JSContext *context,
     }
 
     cr = gjs_cairo_context_get_context(context, obj);
+
     cairo_set_source_surface(cr, surface, x, y);
 
+    if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
diff --git a/modules/cairo-gradient.c b/modules/cairo-gradient.c
index ad813a7..98c761f 100644
--- a/modules/cairo-gradient.c
+++ b/modules/cairo-gradient.c
@@ -60,8 +60,12 @@ addColorStopRGB_func(JSContext *context,
         return JS_FALSE;
 
     pattern = gjs_cairo_pattern_get_pattern(context, object);
+
     cairo_pattern_add_color_stop_rgb(pattern, offset, red, green, blue);
 
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
@@ -86,6 +90,9 @@ addColorStopRGBA_func(JSContext *context,
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     cairo_pattern_add_color_stop_rgba(pattern, offset, red, green, blue, alpha);
 
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
diff --git a/modules/cairo-image-surface.c b/modules/cairo-image-surface.c
index f15710b..abcf18d 100644
--- a/modules/cairo-image-surface.c
+++ b/modules/cairo-image-surface.c
@@ -37,7 +37,6 @@ gjs_cairo_image_surface_constructor(JSContext *context,
 {
     int format, width, height;
     cairo_surface_t *surface;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -50,12 +49,10 @@ gjs_cairo_image_surface_constructor(JSContext *context,
         return JS_FALSE;
 
     surface = cairo_image_surface_create(format, width, height);
-    status = cairo_surface_status(surface);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo surface: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
         return JS_FALSE;
-    }
+
     gjs_cairo_surface_construct(context, obj, surface);
 
     return JS_TRUE;
@@ -82,18 +79,16 @@ createFromPNG_func(JSContext *context,
     char *filename;
     cairo_surface_t *surface;
     JSObject *surface_wrapper;
-    cairo_status_t status;
 
     if (!gjs_parse_args(context, "createFromPNG", "s", argc, argv,
                         "filename", &filename))
         return JS_FALSE;
 
     surface = cairo_image_surface_create_from_png(filename);
-    status = cairo_surface_status(surface);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "failed to create surface: %s", cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_surface_status(surface), "surface"))
         return JS_FALSE;
-    }
+
     surface_wrapper = JS_NewObject(context, &gjs_cairo_image_surface_class, NULL, NULL);
     if (!surface_wrapper) {
         gjs_throw(context, "failed to create surface");
diff --git a/modules/cairo-linear-gradient.c b/modules/cairo-linear-gradient.c
index d39404a..b37ed7d 100644
--- a/modules/cairo-linear-gradient.c
+++ b/modules/cairo-linear-gradient.c
@@ -37,7 +37,6 @@ gjs_cairo_linear_gradient_constructor(JSContext *context,
 {
     double x0, y0, x1, y1;
     cairo_pattern_t *pattern;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -50,12 +49,10 @@ gjs_cairo_linear_gradient_constructor(JSContext *context,
         return JS_FALSE;
 
     pattern = cairo_pattern_create_linear(x0, y0, x1, y1);
-    status = cairo_pattern_status(pattern);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo pattern: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
         return JS_FALSE;
-    }
+
     gjs_cairo_pattern_construct(context, obj, pattern);
 
     return JS_TRUE;
diff --git a/modules/cairo-pattern.c b/modules/cairo-pattern.c
index 45404c3..7750db6 100644
--- a/modules/cairo-pattern.c
+++ b/modules/cairo-pattern.c
@@ -84,6 +84,9 @@ getType_func(JSContext *context,
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     type = cairo_pattern_get_type(pattern);
 
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     *retval = INT_TO_JSVAL(type);
     return JS_TRUE;
 }
diff --git a/modules/cairo-pdf-surface.c b/modules/cairo-pdf-surface.c
index b328141..26838bd 100644
--- a/modules/cairo-pdf-surface.c
+++ b/modules/cairo-pdf-surface.c
@@ -41,7 +41,6 @@ gjs_cairo_pdf_surface_constructor(JSContext *context,
     char *filename;
     double width, height;
     cairo_surface_t *surface;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -53,10 +52,9 @@ gjs_cairo_pdf_surface_constructor(JSContext *context,
         return JS_FALSE;
 
     surface = cairo_pdf_surface_create(filename, width, height);
-    status = cairo_surface_status(surface);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo surface: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
+                                "surface")) {
         g_free(filename);
         return JS_FALSE;
     }
diff --git a/modules/cairo-private.h b/modules/cairo-private.h
index e11e67a..4a2c25c 100644
--- a/modules/cairo-private.h
+++ b/modules/cairo-private.h
@@ -35,6 +35,10 @@ typedef struct {
 JSBool           gjs_js_define_cairo_stuff              (JSContext       *context,
                                                          JSObject        *module);
 
+JSBool           gjs_cairo_check_status                 (JSContext       *context,
+                                                         cairo_status_t   status,
+                                                         const char      *name);
+
 jsval            gjs_cairo_context_create_proto         (JSContext       *context,
                                                          JSObject        *module,
                                                          const char      *proto_name,
diff --git a/modules/cairo-ps-surface.c b/modules/cairo-ps-surface.c
index 13cc6eb..8f58f7a 100644
--- a/modules/cairo-ps-surface.c
+++ b/modules/cairo-ps-surface.c
@@ -41,7 +41,6 @@ gjs_cairo_ps_surface_constructor(JSContext *context,
     char *filename;
     double width, height;
     cairo_surface_t *surface;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -53,13 +52,15 @@ gjs_cairo_ps_surface_constructor(JSContext *context,
         return JS_FALSE;
 
     surface = cairo_ps_surface_create(filename, width, height);
-    status = cairo_surface_status(surface);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo surface: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
+                                "surface")) {
+        g_free(filename);
         return JS_FALSE;
     }
+
     gjs_cairo_surface_construct(context, obj, surface);
+    g_free(filename);
 
     return JS_TRUE;
 }
diff --git a/modules/cairo-radial-gradient.c b/modules/cairo-radial-gradient.c
index d94ea54..3c24166 100644
--- a/modules/cairo-radial-gradient.c
+++ b/modules/cairo-radial-gradient.c
@@ -37,7 +37,6 @@ gjs_cairo_radial_gradient_constructor(JSContext *context,
 {
     double cx0, cy0, radius0, cx1, cy1, radius1;
     cairo_pattern_t *pattern;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -52,12 +51,10 @@ gjs_cairo_radial_gradient_constructor(JSContext *context,
         return JS_FALSE;
 
     pattern = cairo_pattern_create_radial(cx0, cy0, radius0, cx1, cy1, radius1);
-    status = cairo_pattern_status(pattern);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo pattern: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
         return JS_FALSE;
-    }
+
     gjs_cairo_pattern_construct(context, obj, pattern);
 
     return JS_TRUE;
diff --git a/modules/cairo-surface-pattern.c b/modules/cairo-surface-pattern.c
index f8ad52b..841f715 100644
--- a/modules/cairo-surface-pattern.c
+++ b/modules/cairo-surface-pattern.c
@@ -38,7 +38,6 @@ gjs_cairo_surface_pattern_constructor(JSContext *context,
     JSObject *surface_wrapper;
     cairo_surface_t *surface;
     cairo_pattern_t *pattern;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -54,12 +53,10 @@ gjs_cairo_surface_pattern_constructor(JSContext *context,
     }
 
     pattern = cairo_pattern_create_for_surface(surface);
-    status = cairo_pattern_status(pattern);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo pattern: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
         return JS_FALSE;
-    }
+
     gjs_cairo_pattern_construct(context, obj, pattern);
 
     return JS_TRUE;
@@ -95,6 +92,9 @@ setExtend_func(JSContext *context,
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     cairo_pattern_set_extend(pattern, extend);
 
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
@@ -115,6 +115,10 @@ getExtend_func(JSContext *context,
 
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     extend = cairo_pattern_get_extend(pattern);
+
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     *retval = INT_TO_JSVAL(extend);
 
     return JS_TRUE;
@@ -137,6 +141,9 @@ setFilter_func(JSContext *context,
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     cairo_pattern_set_filter(pattern, filter);
 
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     return JS_TRUE;
 }
 
@@ -157,6 +164,10 @@ getFilter_func(JSContext *context,
 
     pattern = gjs_cairo_pattern_get_pattern(context, object);
     filter = cairo_pattern_get_filter(pattern);
+
+    if (!gjs_cairo_check_status(context, cairo_pattern_status(pattern), "pattern"))
+        return JS_FALSE;
+
     *retval = INT_TO_JSVAL(filter);
 
     return JS_TRUE;
diff --git a/modules/cairo-svg-surface.c b/modules/cairo-svg-surface.c
index 449d4e3..854cc53 100644
--- a/modules/cairo-svg-surface.c
+++ b/modules/cairo-svg-surface.c
@@ -41,7 +41,6 @@ gjs_cairo_svg_surface_constructor(JSContext *context,
     char *filename;
     double width, height;
     cairo_surface_t *surface;
-    cairo_status_t status;
 
     if (!gjs_check_constructing(context))
         return JS_FALSE;
@@ -53,14 +52,16 @@ gjs_cairo_svg_surface_constructor(JSContext *context,
         return JS_FALSE;
 
     surface = cairo_svg_surface_create(filename, width, height);
-    status = cairo_surface_status(surface);
-    if (status != CAIRO_STATUS_SUCCESS) {
-        gjs_throw(context, "Failed to create cairo surface: %s",
-                  cairo_status_to_string(status));
+
+    if (!gjs_cairo_check_status(context, cairo_surface_status(surface),
+                                "surface")) {
+        g_free(filename);
         return JS_FALSE;
     }
+
     gjs_cairo_surface_construct(context, obj, surface);
 
+    g_free(filename);
     return JS_TRUE;
 }
 
diff --git a/modules/cairo.c b/modules/cairo.c
index bfa231c..ea11496 100644
--- a/modules/cairo.c
+++ b/modules/cairo.c
@@ -27,6 +27,21 @@
 #include "cairo-private.h"
 
 JSBool
+gjs_cairo_check_status(JSContext      *context,
+                       cairo_status_t  status,
+                       const char     *name)
+{
+    if (status != CAIRO_STATUS_SUCCESS) {
+        gjs_throw(context, "Could not create %s: %s",
+                  name,
+                  cairo_status_to_string(status));
+        return JS_FALSE;
+    }
+
+    return JS_TRUE;
+}
+
+JSBool
 gjs_js_define_cairo_stuff(JSContext *context,
                           JSObject  *module)
 {



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