[gjs] js: Add macros for 'this' and private data



commit b98cf0b7b10b03f7b0c44906642fbbb243df7ba1
Author: Philip Chimento <philip chimento gmail com>
Date:   Thu Oct 6 21:25:38 2016 -0700

    js: Add macros for 'this' and private data
    
    This adds two macros, GJS_GET_THIS() to get a function's 'this' value as
    a rooted object (and if it was not an object, to box it into one), and
    GJS_GET_PRIV() to get our private C data from the 'this' object.
    
    This operation is done over and over again inside most JSNative
    functions, and SpiderMonkey code often uses convenience macros like this.
    These will come in handy even more when we switch to JSNative property
    accessors in a following commit.
    
    At the same time we make things more typesafe by doing a typecheck on
    each 'this' object in the native methods as part of GJS_GET_PRIV() and
    throwing an error if it is not the correct type. This gets rid of the
    args.thisv().toObjectOrNull() idiom which I'm pretty sure is wrong.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/boxed.cpp                      |    7 +--
 gi/function.cpp                   |   16 +----
 gi/fundamental.cpp                |    8 +--
 gi/gerror.cpp                     |   27 +-------
 gi/gtype.cpp                      |    6 +-
 gi/object.cpp                     |   32 +--------
 gi/union.cpp                      |    8 +--
 gjs/byteArray.cpp                 |   11 +---
 gjs/jsapi-util.h                  |   36 ++++++++++
 modules/cairo-context.cpp         |  128 +++++++++----------------------------
 modules/cairo-gradient.cpp        |    8 +--
 modules/cairo-image-surface.cpp   |   16 +----
 modules/cairo-pattern.cpp         |    4 +-
 modules/cairo-region.cpp          |    8 +--
 modules/cairo-surface-pattern.cpp |   16 +----
 modules/cairo-surface.cpp         |    8 +--
 modules/console.cpp               |    3 +-
 17 files changed, 102 insertions(+), 240 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 13275a3..b3ca35e 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -897,16 +897,11 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
+    GJS_GET_PRIV(context, argc, vp, rec, obj, Boxed, priv);
 
-    Boxed *priv;
     bool ret = false;
     JS::Value retval;
 
-    if (!priv_from_js_with_typecheck(context, obj, &priv))
-        goto out;
-    
     if (!_gjs_proxy_to_string_func(context, obj, "boxed", (GIBaseInfo*)priv->info,
                                    priv->gtype, priv->gboxed, &retval))
         goto out;
diff --git a/gi/function.cpp b/gi/function.cpp
index e26f4f5..82a5692 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1296,9 +1296,8 @@ function_call(JSContext *context,
               unsigned   js_argc,
               JS::Value *vp)
 {
-    JS::CallArgs js_argv = JS::CallArgsFromVp (js_argc, vp);
-    JS::RootedObject object(context, js_argv.thisv().toObjectOrNull()),
-        callee(context, &js_argv.callee());
+    GJS_GET_THIS(context, js_argc, vp, js_argv, object);
+    JS::RootedObject callee(context, &js_argv.callee());
 
     bool success;
     Function *priv;
@@ -1395,7 +1394,7 @@ function_to_string (JSContext *context,
                     guint      argc,
                     JS::Value *vp)
 {
-    Function *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, to, Function, priv);
     gchar *string;
     bool free;
     JS::Value retval;
@@ -1404,15 +1403,6 @@ function_to_string (JSContext *context,
     GString *arg_names_str;
     gchar *arg_names;
 
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-
-    if (rec.thisv().isNull()) {
-        gjs_throw(context, "this cannot be null");
-        return false;
-    }
-    JS::RootedObject self(context, &rec.thisv().toObject());
-
-    priv = priv_from_js (context, self);
     if (priv == NULL) {
         string = (gchar *) "function () {\n}";
         free = false;
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index 484d963..19013f9 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -498,16 +498,10 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    FundamentalInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, FundamentalInstance, priv);
     bool ret = false;
     JS::Value retval;
 
-    if (!priv_from_js_with_typecheck(context, obj, &priv))
-        goto out;
-
     if (!priv->prototype) {
         Fundamental *proto_priv = (Fundamental *) priv;
 
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 80279d8..46dfb95 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -211,23 +211,11 @@ error_to_string(JSContext *context,
                 unsigned   argc,
                 JS::Value *vp)
 {
-    JS::Value v_self;
-    Error *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, self, Error, priv);
     JS::Value v_out;
     gchar *descr;
     bool retval;
 
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    v_self = rec.thisv();
-    if (!v_self.isObject()) {
-        /* Lie a bit here... */
-        gjs_throw(context, "GLib.Error.prototype.toString() called on a non object");
-        return false;
-    }
-
-    JS::RootedObject self(context, &v_self.toObject());
-    priv = priv_from_js(context, self);
-
     if (priv == NULL)
         return false;
 
@@ -267,20 +255,13 @@ error_constructor_value_of(JSContext *context,
                            unsigned   argc,
                            JS::Value *vp)
 {
-    JS::Value v_self, v_prototype;
+    GJS_GET_THIS(context, argc, vp, rec, self);
+    JS::Value v_prototype;
     Error *priv;
     jsid prototype_name;
 
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    v_self = rec.thisv();
-    if (!v_self.isObject()) {
-        /* Lie a bit here... */
-        gjs_throw(context, "GLib.Error.valueOf() called on a non object");
-        return false;
-    }
-
     prototype_name = gjs_context_get_const_string(context, GJS_STRING_PROTOTYPE);
-    if (!gjs_object_require_property(context, &v_self.toObject(), "constructor",
+    if (!gjs_object_require_property(context, self, "constructor",
                                      prototype_name, &v_prototype))
         return false;
 
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index ac82e4f..6e6018f 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -65,15 +65,13 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, rec, obj, void, priv);
     GType gtype;
     gchar *strval;
     bool ret;
     JS::Value retval;
 
-    gtype = GPOINTER_TO_SIZE(priv_from_js(context, obj));
+    gtype = GPOINTER_TO_SIZE(priv);
 
     if (gtype == 0)
         strval = g_strdup("[object GType prototype]");
diff --git a/gi/object.cpp b/gi/object.cpp
index 97644dd..0a8444e 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -1567,10 +1567,7 @@ real_connect_func(JSContext *context,
                   JS::Value *vp,
                   bool       after)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
-    ObjectInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, argv, obj, ObjectInstance, priv);
     GClosure *closure;
     gulong id;
     guint signal_id;
@@ -1579,10 +1576,6 @@ real_connect_func(JSContext *context,
     ConnectData *connect_data;
     bool ret = false;
 
-    if (!do_base_typecheck(context, obj, true))
-        return false;
-
-    priv = priv_from_js(context, obj);
     gjs_debug_gsignal("connect obj %p priv %p argc %d", obj.get(), priv, argc);
     if (priv == NULL) {
         throw_priv_is_null_error(context);
@@ -1669,10 +1662,7 @@ emit_func(JSContext *context,
           unsigned   argc,
           JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
-    ObjectInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, argv, obj, ObjectInstance, priv);
     guint signal_id;
     GQuark signal_detail;
     GSignalQuery signal_query;
@@ -1684,10 +1674,6 @@ emit_func(JSContext *context,
     JS::Value retval;
     bool ret = false;
 
-    if (!do_base_typecheck(context, obj, true))
-        return false;
-
-    priv = priv_from_js(context, obj);
     gjs_debug_gsignal("emit obj %p priv %p argc %d", obj.get(), priv, argc);
 
     if (priv == NULL) {
@@ -1792,18 +1778,10 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    ObjectInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, ObjectInstance, priv);
     bool ret = false;
     JS::Value retval;
 
-    if (!do_base_typecheck(context, obj, true))
-        goto out;
-
-    priv = priv_from_js(context, obj);
-
     if (priv == NULL) {
         throw_priv_is_null_error(context);
         goto out;  /* wrong class passed in */
@@ -1844,9 +1822,7 @@ init_func (JSContext *context,
            unsigned   argc,
            JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     bool ret;
 
     if (!do_base_typecheck(context, obj, true))
diff --git a/gi/union.cpp b/gi/union.cpp
index def9465..cd5c2de 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -287,16 +287,10 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    Union *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, Union, priv);
     bool ret = false;
     JS::Value retval;
 
-    if (!priv_from_js_with_typecheck(context, obj, &priv))
-        goto out;
-    
     if (!_gjs_proxy_to_string_func(context, obj, "union", (GIBaseInfo*)priv->info,
                                    priv->gtype, priv->gboxed, &retval))
         goto out;
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index a5e6ede..a2e689d 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -409,15 +409,11 @@ to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject object(context, argv.thisv().toObjectOrNull());
-    ByteArrayInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, argv, to, ByteArrayInstance, priv);
     char *encoding;
     bool encoding_is_utf8;
     gchar *data;
 
-    priv = priv_from_js(context, object);
-
     if (priv == NULL)
         return true; /* prototype, not instance */
 
@@ -507,13 +503,10 @@ to_gbytes_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject object(context, rec.thisv().toObjectOrNull());
-    ByteArrayInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, to, ByteArrayInstance, priv);
     JSObject *ret_bytes_obj;
     GIBaseInfo *gbytes_info;
 
-    priv = priv_from_js(context, object);
     if (priv == NULL)
         return true; /* prototype, not instance */
 
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index bf38c02..6cc164a 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -104,6 +104,42 @@ typedef struct GjsRootedArray GjsRootedArray;
         return true;                                                    \
     }
 
+/*
+ * GJS_GET_THIS:
+ * @cx: JSContext pointer passed into JSNative function
+ * @argc: Number of arguments passed into JSNative function
+ * @vp: Argument value array passed into JSNative function
+ * @args: Name for JS::CallArgs variable defined by this code snippet
+ * @to: Name for JS::RootedObject variable referring to function's this
+ *
+ * A convenience macro for getting the 'this' object a function was called with.
+ * Use in any JSNative function.
+ */
+#define GJS_GET_THIS(cx, argc, vp, args, to)                   \
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);          \
+    JS::RootedObject to(cx, &args.computeThis(cx).toObject())
+
+/*
+ * GJS_GET_PRIV:
+ * @cx: JSContext pointer passed into JSNative function
+ * @argc: Number of arguments passed into JSNative function
+ * @vp: Argument value array passed into JSNative function
+ * @args: Name for JS::CallArgs variable defined by this code snippet
+ * @to: Name for JS::RootedObject variable referring to function's this
+ * @type: Type of private data
+ * @priv: Name for private data variable defined by this code snippet
+ *
+ * A convenience macro for getting the private data from GJS classes using
+ * priv_from_js().
+ * Throws an error if the 'this' object is not the right type.
+ * Use in any JSNative function.
+ */
+#define GJS_GET_PRIV(cx, argc, vp, args, to, type, priv)  \
+    GJS_GET_THIS(cx, argc, vp, args, to);                 \
+    if (!do_base_typecheck(cx, to, true))                 \
+        return false;                                     \
+    type *priv = priv_from_js(cx, to)
+
 /**
  * GJS_DEFINE_PROTO:
  * @tn: The name of the prototype, as a string
diff --git a/modules/cairo-context.cpp b/modules/cairo-context.cpp
index 8b06d47..128ef7a 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -35,9 +35,8 @@ mname##_func(JSContext *context,                    \
               unsigned   argc,                      \
               JS::Value *vp)                        \
 {                                                   \
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);             \
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());  \
-    cairo_t *cr;
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv); \
+    cairo_t *cr = priv ? priv->cr : NULL;
 
 #define _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END                               \
     return gjs_cairo_check_status(context, cairo_status(cr), "context"); \
@@ -51,7 +50,6 @@ mname##_func(JSContext *context,                    \
 
 #define _GJS_CAIRO_CONTEXT_DEFINE_FUNC0(method, cfunc)                     \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr);                                                             \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -60,7 +58,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     int ret;                                                               \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     ret = (int)cfunc(cr);                                                  \
     argv.rval().setInt32(ret);                                             \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -69,7 +66,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     cairo_bool_t ret;                                                      \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     ret = cfunc(cr);                                                       \
     argv.rval().setBoolean(ret);                                           \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -80,7 +76,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, "ff", argv,                 \
                         #n1, &arg1, #n2, &arg2))                           \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, &arg1, &arg2);                                               \
     if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
       JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
@@ -99,7 +94,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     double arg1, arg2;                                                     \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, &arg1, &arg2);                                               \
     if (cairo_status(cr) == CAIRO_STATUS_SUCCESS) {                        \
       JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
@@ -118,7 +112,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     double arg1, arg2, arg3, arg4;                                         \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, &arg1, &arg2, &arg3, &arg4);                                 \
     {                                                                      \
       JSObject *array = JS_NewArrayObject(context, 0, NULL);               \
@@ -141,7 +134,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     double ret;                                                            \
    _GJS_CAIRO_CONTEXT_CHECK_NO_ARGS(method)                                \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     ret = cfunc(cr);                                                       \
     argv.rval().setNumber(ret);                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -152,7 +144,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
                         #n1, &arg1))                                       \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1);                                                       \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -164,7 +155,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
                         #n1, &arg1, #n2, &arg2))                           \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1, arg2);                                                 \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -177,7 +167,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, fmt, argv,                 \
                         #n1, &arg1, #n2, &arg2))                           \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     ret = cfunc(cr, arg1, arg2);                                           \
     argv.rval().setBoolean(ret);                                           \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -190,7 +179,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
                         #n1, &arg1, #n2, &arg2, #n3, &arg3))               \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1, arg2, arg3);                                           \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -204,7 +192,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
     if (!gjs_parse_call_args(context, #method, fmt, argv,                  \
                         #n1, &arg1, #n2, &arg2, #n3, &arg3, #n4, &arg4))   \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1, arg2, arg3, arg4);                                     \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
 
@@ -219,7 +206,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
                         #n1, &arg1, #n2, &arg2, #n3, &arg3,                \
                         #n4, &arg4, #n5, &arg5))                           \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1, arg2, arg3, arg4, arg5);                               \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -236,7 +222,6 @@ _GJS_CAIRO_CONTEXT_DEFINE_FUNC_BEGIN(method)                               \
                         #n1, &arg1, #n2, &arg2, #n3, &arg3,                \
                         #n4, &arg4, #n5, &arg5, #n6, &arg6))               \
         return false;                                                      \
-    cr = gjs_cairo_context_get_context(context, obj);                      \
     cfunc(cr, arg1, arg2, arg3, arg4, arg5, arg6);                         \
     argv.rval().setUndefined();                                            \
 _GJS_CAIRO_CONTEXT_DEFINE_FUNC_END
@@ -405,12 +390,8 @@ dispose_func(JSContext *context,
              unsigned   argc,
              JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    GjsCairoContext *priv;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, GjsCairoContext, priv);
 
-    priv = priv_from_js(context, obj);
     if (priv->cr != NULL) {
         cairo_destroy(priv->cr);
         priv->cr = NULL;
@@ -424,12 +405,10 @@ appendPath_func(JSContext *context,
                 unsigned   argc,
                 JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     JSObject *path_wrapper;
     cairo_path_t *path;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "path", "o", argv,
                         "path", &path_wrapper))
@@ -441,7 +420,6 @@ appendPath_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     cairo_append_path(cr, path);
     argv.rval().setUndefined();
     return true;
@@ -452,16 +430,13 @@ copyPath_func(JSContext *context,
               unsigned   argc,
               JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     cairo_path_t *path;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "", "", argv))
         return false;
 
-    cr = gjs_cairo_context_get_context(context, obj);
     path = cairo_copy_path(cr);
     argv.rval().setObjectOrNull(gjs_cairo_path_from_path(context, path));
     return true;
@@ -472,16 +447,13 @@ copyPathFlat_func(JSContext *context,
                   unsigned   argc,
                   JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     cairo_path_t *path;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "", "", argv))
         return false;
 
-    cr = gjs_cairo_context_get_context(context, obj);
     path = cairo_copy_path_flat(cr);
     argv.rval().setObjectOrNull(gjs_cairo_path_from_path(context, path));
     return true;
@@ -492,12 +464,10 @@ mask_func(JSContext *context,
           unsigned   argc,
           JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     JSObject *pattern_wrapper;
     cairo_pattern_t *pattern;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "mask", "o", argv,
                         "pattern", &pattern_wrapper))
@@ -509,7 +479,6 @@ mask_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     cairo_mask(cr, pattern);
 
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
@@ -524,13 +493,11 @@ maskSurface_func(JSContext *context,
                  unsigned   argc,
                  JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     JSObject *surface_wrapper;
     double x, y;
     cairo_surface_t *surface;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "maskSurface", "off", argv,
                         "surface", &surface_wrapper,
@@ -544,8 +511,6 @@ maskSurface_func(JSContext *context,
         return false;
     }
 
-    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"))
@@ -560,11 +525,9 @@ setDash_func(JSContext *context,
              unsigned   argc,
              JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     guint i;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
     JS::RootedObject dashes(context);
     double offset;
     bool retval = false;
@@ -608,7 +571,6 @@ setDash_func(JSContext *context,
         g_array_append_val(dashes_c, b);
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     cairo_set_dash(cr, (double*)dashes_c->data, dashes_c->len, offset);
     argv.rval().setUndefined();
     retval = true;
@@ -623,12 +585,10 @@ setSource_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     JSObject *pattern_wrapper;
     cairo_pattern_t *pattern;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "setSource", "o", argv,
                         "pattern", &pattern_wrapper))
@@ -640,8 +600,6 @@ setSource_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
-
     cairo_set_source(cr, pattern);
 
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
@@ -657,13 +615,11 @@ setSourceSurface_func(JSContext *context,
                       unsigned   argc,
                       JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     JSObject *surface_wrapper;
     double x, y;
     cairo_surface_t *surface;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "setSourceSurface", "off", argv,
                         "surface", &surface_wrapper,
@@ -677,8 +633,6 @@ setSourceSurface_func(JSContext *context,
         return false;
     }
 
-    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"))
@@ -694,18 +648,14 @@ showText_func(JSContext *context,
               unsigned   argc,
               JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     char *utf8;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "showText", "s", argv,
                         "utf8", &utf8))
         return false;
 
-    cr = gjs_cairo_context_get_context(context, obj);
-
     cairo_show_text(cr, utf8);
     g_free(utf8);
 
@@ -722,13 +672,11 @@ selectFontFace_func(JSContext *context,
                     unsigned   argc,
                     JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull());
-
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoContext, priv);
     char *family;
     cairo_font_slant_t slant;
     cairo_font_weight_t weight;
-    cairo_t *cr;
+    cairo_t *cr = priv ? priv->cr : NULL;
 
     if (!gjs_parse_call_args(context, "selectFontFace", "sii", argv,
                         "family", &family,
@@ -736,8 +684,6 @@ selectFontFace_func(JSContext *context,
                         "weight", &weight))
         return false;
 
-    cr = gjs_cairo_context_get_context(context, obj);
-
     cairo_select_font_face(cr, family, slant, weight);
     g_free(family);
 
@@ -753,10 +699,8 @@ popGroup_func(JSContext *context,
               unsigned   argc,
               JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    cairo_t *cr;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, GjsCairoContext, priv);
+    cairo_t *cr = priv ? priv->cr : NULL;
     cairo_pattern_t *pattern;
     JSObject *pattern_wrapper;
 
@@ -765,7 +709,6 @@ popGroup_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     pattern = cairo_pop_group(cr);
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
         return false;
@@ -786,10 +729,8 @@ getSource_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    cairo_t *cr;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, GjsCairoContext, priv);
+    cairo_t *cr = priv ? priv->cr : NULL;
     cairo_pattern_t *pattern;
     JSObject *pattern_wrapper;
 
@@ -798,7 +739,6 @@ getSource_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     pattern = cairo_get_source(cr);
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
         return false;
@@ -820,10 +760,8 @@ getTarget_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    cairo_t *cr;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, GjsCairoContext, priv);
+    cairo_t *cr = priv ? priv->cr : NULL;
     cairo_surface_t *surface;
     JSObject *surface_wrapper;
 
@@ -832,7 +770,6 @@ getTarget_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     surface = cairo_get_target(cr);
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
         return false;
@@ -854,10 +791,8 @@ getGroupTarget_func(JSContext *context,
                     unsigned   argc,
                     JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JS::RootedObject obj(context, rec.thisv().toObjectOrNull());
-
-    cairo_t *cr;
+    GJS_GET_PRIV(context, argc, vp, rec, obj, GjsCairoContext, priv);
+    cairo_t *cr = priv ? priv->cr : NULL;
     cairo_surface_t *surface;
     JSObject *surface_wrapper;
 
@@ -866,7 +801,6 @@ getGroupTarget_func(JSContext *context,
         return false;
     }
 
-    cr = gjs_cairo_context_get_context(context, obj);
     surface = cairo_get_group_target(cr);
     if (!gjs_cairo_check_status(context, cairo_status(cr), "context"))
         return false;
diff --git a/modules/cairo-gradient.cpp b/modules/cairo-gradient.cpp
index 3e1e253..29a84b8 100644
--- a/modules/cairo-gradient.cpp
+++ b/modules/cairo-gradient.cpp
@@ -47,9 +47,7 @@ addColorStopRGB_func(JSContext *context,
                      unsigned   argc,
                      JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *obj = argv.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     double offset, red, green, blue;
     cairo_pattern_t *pattern;
 
@@ -76,9 +74,7 @@ addColorStopRGBA_func(JSContext *context,
                       unsigned   argc,
                       JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *obj = argv.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     double offset, red, green, blue, alpha;
     cairo_pattern_t *pattern;
 
diff --git a/modules/cairo-image-surface.cpp b/modules/cairo-image-surface.cpp
index dfd11ed..3dec670 100644
--- a/modules/cairo-image-surface.cpp
+++ b/modules/cairo-image-surface.cpp
@@ -103,9 +103,7 @@ getFormat_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_surface_t *surface;
     cairo_format_t format;
 
@@ -129,9 +127,7 @@ getWidth_func(JSContext *context,
               unsigned   argc,
               JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_surface_t *surface;
     int width;
 
@@ -155,9 +151,7 @@ getHeight_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_surface_t *surface;
     int height;
 
@@ -181,9 +175,7 @@ getStride_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_surface_t *surface;
     int stride;
 
diff --git a/modules/cairo-pattern.cpp b/modules/cairo-pattern.cpp
index 8f27cda..b29e3af 100644
--- a/modules/cairo-pattern.cpp
+++ b/modules/cairo-pattern.cpp
@@ -61,9 +61,7 @@ getType_func(JSContext *context,
              unsigned   argc,
              JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_pattern_t *pattern;
     cairo_pattern_type_t type;
 
diff --git a/modules/cairo-region.cpp b/modules/cairo-region.cpp
index 66e1098..67a3651 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -53,10 +53,9 @@ static bool
 fill_rectangle(JSContext *context, JSObject *obj,
                cairo_rectangle_int_t *rect);
 
-#define PRELUDE                                                 \
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);          \
-    JS::RootedObject obj(context, argv.thisv().toObjectOrNull()); \
-    cairo_region_t *this_region = get_region(context, obj);
+#define PRELUDE                                                       \
+    GJS_GET_PRIV(context, argc, vp, argv, obj, GjsCairoRegion, priv); \
+    cairo_region_t *this_region = priv ? priv->region : NULL;
 
 #define RETURN_STATUS                                           \
     return gjs_cairo_check_status(context, cairo_region_status(this_region), "region");
@@ -74,7 +73,6 @@ fill_rectangle(JSContext *context, JSObject *obj,
                                  "other_region", other_obj.address())) \
             return false;                                       \
                                                                 \
-        this_region = get_region(context, obj);                 \
         other_region = get_region(context, other_obj);          \
                                                                 \
         cairo_region_##method(this_region, other_region);       \
diff --git a/modules/cairo-surface-pattern.cpp b/modules/cairo-surface-pattern.cpp
index 3440f32..7f56d86 100644
--- a/modules/cairo-surface-pattern.cpp
+++ b/modules/cairo-surface-pattern.cpp
@@ -78,9 +78,7 @@ setExtend_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *obj = argv.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     cairo_extend_t extend;
     cairo_pattern_t *pattern;
 
@@ -103,9 +101,7 @@ getExtend_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_extend_t extend;
     cairo_pattern_t *pattern;
 
@@ -130,9 +126,7 @@ setFilter_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *obj = argv.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     cairo_filter_t filter;
     cairo_pattern_t *pattern;
 
@@ -155,9 +149,7 @@ getFilter_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_filter_t filter;
     cairo_pattern_t *pattern;
 
diff --git a/modules/cairo-surface.cpp b/modules/cairo-surface.cpp
index 7d2dc67..2b05528 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -61,9 +61,7 @@ writeToPNG_func(JSContext *context,
                 unsigned   argc,
                 JS::Value *vp)
 {
-    JS::CallArgs argv = JS::CallArgsFromVp (argc, vp);
-    JSObject *obj = argv.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, argv, obj);
     char *filename;
     cairo_surface_t *surface;
 
@@ -90,9 +88,7 @@ getType_func(JSContext *context,
              unsigned   argc,
              JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *obj = rec.thisv().toObjectOrNull();
-
+    GJS_GET_THIS(context, argc, vp, rec, obj);
     cairo_surface_t *surface;
     cairo_surface_type_t type;
 
diff --git a/modules/console.cpp b/modules/console.cpp
index e54a511..530d09c 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -158,8 +158,7 @@ gjs_console_interact(JSContext *context,
                      unsigned   argc,
                      JS::Value *vp)
 {
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-    JSObject *object = rec.thisv().toObjectOrNull();
+    GJS_GET_THIS(context, argc, vp, argv, object);
     bool eof = false;
     JS::Value result;
     JSString *str;



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