[gjs/wip/ptomato/mozjs31prep: 2/3] js: Add macros for 'this' and private data



commit 7a58b7282bb7cf4cafa59b2490815de62a16537e
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 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.
    
    In doing so we get rid of the args.thisv().toObjectOrNull() idiom which
    I'm pretty sure is wrong, except in function_call() which is fine to have
    anything as the 'this' object.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/boxed.cpp                      |    7 +--
 gi/function.cpp                   |   13 +---
 gi/fundamental.cpp                |    8 +--
 gi/gerror.cpp                     |   27 +-------
 gi/gtype.cpp                      |    6 +-
 gi/object.cpp                     |   39 +++--------
 gi/union.cpp                      |    8 +--
 gjs/byteArray.cpp                 |   11 +---
 gjs/jsapi-util.h                  |   56 ++++++++++++++++
 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, 129 insertions(+), 237 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 5c38c3d..1086eda 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_WITH_TYPECHECK(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 2b66214..d8af8a5 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1297,6 +1297,8 @@ function_call(JSContext *context,
               JS::Value *vp)
 {
     JS::CallArgs js_argv = JS::CallArgsFromVp (js_argc, vp);
+    /* Don't want to typecheck js_argv.thisv() here, since it's legal for it to
+     * be anything */
     JS::RootedObject object(context, js_argv.thisv().toObjectOrNull()),
         callee(context, &js_argv.callee());
 
@@ -1395,7 +1397,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 +1406,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 faf4ebc..7ca739a 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -497,16 +497,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_WITH_TYPECHECK(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 c2c59b3..e331721 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 6f8ef45..43f9b5a 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -66,15 +66,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 de7606c..d769684 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -50,6 +50,13 @@
 #include <util/hash-x32.h>
 #include <girepository.h>
 
+/* Only used in this file, but could be useful in jsapi-util.h */
+#define GJS_GET_PRIV_WITH_TYPECHECK_THROW(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)
+
 typedef struct {
     GIObjectInfo *info;
     GObject *gobj; /* NULL if we are the prototype and not an instance */
@@ -1568,10 +1575,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_WITH_TYPECHECK_THROW(context, argc, vp, argv, obj, ObjectInstance, priv);
     GClosure *closure;
     gulong id;
     guint signal_id;
@@ -1580,10 +1584,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);
@@ -1670,10 +1670,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_WITH_TYPECHECK_THROW(context, argc, vp, argv, obj, ObjectInstance, priv);
     guint signal_id;
     GQuark signal_detail;
     GSignalQuery signal_query;
@@ -1685,10 +1682,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) {
@@ -1793,18 +1786,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_WITH_TYPECHECK_THROW(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 */
@@ -1845,9 +1830,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 6781efb..b72663d 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_WITH_TYPECHECK(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 771a70b..6a9b847 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -410,15 +410,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 */
 
@@ -508,13 +504,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 fa8fda0..b720ba0 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -107,6 +107,62 @@ 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);  \
+    if (G_UNLIKELY(!args.thisv().isObject())) {        \
+        gjs_throw(cx, "Wrong type for this");          \
+        return false;                                  \
+    }                                                  \
+    JS::RootedObject to(cx, &args.thisv().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().
+ * Use in any JSNative function.
+ */
+#define GJS_GET_PRIV(cx, argc, vp, args, to, type, priv)  \
+    GJS_GET_THIS(cx, argc, vp, args, to);                 \
+    type *priv = priv_from_js(cx, to)
+
+/*
+ * GJS_GET_PRIV_WITH_TYPECHECK:
+ * @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
+ *
+ * Same as GJS_GET_PRIV but using priv_from_js_with_typecheck().
+ * Use in any JSNative function.
+ */
+#define GJS_GET_PRIV_WITH_TYPECHECK(cx, argc, vp, args, to, type, priv)  \
+    GJS_GET_THIS(cx, argc, vp, args, to);                                \
+    type *priv;                                                          \
+    if (!priv_from_js_with_typecheck(cx, to, &priv))                     \
+        return false
+
 /**
  * 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 d7f2514..4b9c372 100644
--- a/modules/cairo-context.cpp
+++ b/modules/cairo-context.cpp
@@ -36,9 +36,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"); \
@@ -52,7 +51,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
@@ -61,7 +59,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
@@ -70,7 +67,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
@@ -81,7 +77,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);               \
@@ -100,7 +95,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);               \
@@ -119,7 +113,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);               \
@@ -142,7 +135,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
@@ -153,7 +145,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
@@ -165,7 +156,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
@@ -178,7 +168,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
@@ -191,7 +180,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
@@ -205,7 +193,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
 
@@ -220,7 +207,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
@@ -237,7 +223,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
@@ -406,12 +391,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;
@@ -425,12 +406,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))
@@ -442,7 +421,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;
@@ -453,16 +431,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;
@@ -473,16 +448,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;
@@ -493,12 +465,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))
@@ -510,7 +480,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"))
@@ -525,13 +494,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,
@@ -545,8 +512,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"))
@@ -561,11 +526,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;
@@ -609,7 +572,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;
@@ -624,12 +586,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))
@@ -641,8 +601,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"))
@@ -658,13 +616,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,
@@ -678,8 +634,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"))
@@ -695,18 +649,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);
 
@@ -723,13 +673,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,
@@ -737,8 +685,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);
 
@@ -754,10 +700,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;
 
@@ -766,7 +710,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;
@@ -787,10 +730,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;
 
@@ -799,7 +740,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;
@@ -821,10 +761,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;
 
@@ -833,7 +771,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;
@@ -855,10 +792,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;
 
@@ -867,7 +802,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 15ba72e..eca60dc 100644
--- a/modules/cairo-gradient.cpp
+++ b/modules/cairo-gradient.cpp
@@ -48,9 +48,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;
 
@@ -77,9 +75,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 ba670b7..6de5682 100644
--- a/modules/cairo-image-surface.cpp
+++ b/modules/cairo-image-surface.cpp
@@ -104,9 +104,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;
 
@@ -130,9 +128,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;
 
@@ -156,9 +152,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;
 
@@ -182,9 +176,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 d2d0c99..ed8ff22 100644
--- a/modules/cairo-pattern.cpp
+++ b/modules/cairo-pattern.cpp
@@ -62,9 +62,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 9263821..e6d12ef 100644
--- a/modules/cairo-region.cpp
+++ b/modules/cairo-region.cpp
@@ -54,10 +54,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");
@@ -75,7 +74,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 1a3b3c6..750b061 100644
--- a/modules/cairo-surface-pattern.cpp
+++ b/modules/cairo-surface-pattern.cpp
@@ -79,9 +79,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;
 
@@ -104,9 +102,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;
 
@@ -131,9 +127,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;
 
@@ -156,9 +150,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 feba845..a9b5c35 100644
--- a/modules/cairo-surface.cpp
+++ b/modules/cairo-surface.cpp
@@ -62,9 +62,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;
 
@@ -91,9 +89,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 eb1faa7..e74588f 100644
--- a/modules/console.cpp
+++ b/modules/console.cpp
@@ -159,8 +159,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]