[gjs: 5/26] wrapperutils: Use GIWrapperBase::typecheck() directly in calling code



commit ec595f81b1920a3cf68e069b1cb4ce8f1e8b6d4f
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Jan 20 16:53:32 2019 -0800

    wrapperutils: Use GIWrapperBase::typecheck() directly in calling code
    
    This removes the gjs_typecheck_foo() methods (which by now only call
    GIWrapperBase::typecheck() anyway) in favour of calling typecheck()
    directly.

 gi/arg.cpp         |  4 +++-
 gi/boxed.cpp       | 14 --------------
 gi/boxed.h         |  6 ------
 gi/fundamental.cpp | 13 -------------
 gi/fundamental.h   |  6 ------
 gi/gerror.cpp      | 31 ++++++++++++++++++-------------
 gi/gerror.h        | 11 ++++++-----
 gi/interface.cpp   |  3 ++-
 gi/object.cpp      | 23 ++++++++---------------
 gi/object.h        | 17 +++++++++++------
 gi/union.cpp       | 14 --------------
 gi/union.h         |  6 ------
 gi/value.cpp       |  9 +++++----
 gjs/byteArray.cpp  |  2 +-
 14 files changed, 54 insertions(+), 105 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index b8039943..7f209fbd 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -1753,7 +1753,9 @@ gjs_value_to_g_argument(JSContext      *context,
                     } else if (G_TYPE_IS_INTERFACE(gtype)) {
                         /* Could be a GObject interface that's missing a prerequisite, or could
                            be a fundamental */
-                        if (gjs_typecheck_object(context, obj, gtype, false)) {
+                        if (ObjectBase::typecheck(
+                                context, obj, nullptr, gtype,
+                                ObjectBase::TypecheckNoThrow())) {
                             if (!ObjectBase::transfer_to_gi_argument(
                                     context, obj, arg, GI_DIRECTION_IN,
                                     transfer, gtype))
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index bf2b926f..2a471ba3 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -1046,17 +1046,3 @@ void* BoxedInstance::copy_ptr(JSContext* cx, GType gtype, void* ptr) {
               "boxed");
     return nullptr;
 }
-
-bool
-gjs_typecheck_boxed(JSContext       *context,
-                    JS::HandleObject object,
-                    GIStructInfo    *expected_info,
-                    GType            expected_type,
-                    bool             throw_error)
-{
-    if (throw_error)
-        return BoxedBase::typecheck(context, object, expected_info,
-                                    expected_type);
-    return BoxedBase::typecheck(context, object, expected_info, expected_type,
-                                BoxedBase::TypecheckNoThrow());
-}
diff --git a/gi/boxed.h b/gi/boxed.h
index 4940b1ac..5c134c5a 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -230,12 +230,6 @@ JSObject* gjs_boxed_from_c_struct      (JSContext             *context,
                                         GIStructInfo          *info,
                                         void                  *gboxed,
                                         GjsBoxedCreationFlags  flags);
-GJS_USE
-bool      gjs_typecheck_boxed          (JSContext             *context,
-                                        JS::HandleObject       obj,
-                                        GIStructInfo          *expected_info,
-                                        GType                  expected_type,
-                                        bool                   throw_error);
 
 G_END_DECLS
 
diff --git a/gi/fundamental.cpp b/gi/fundamental.cpp
index e7505cdc..389d922e 100644
--- a/gi/fundamental.cpp
+++ b/gi/fundamental.cpp
@@ -503,19 +503,6 @@ bool FundamentalBase::to_gvalue(JSContext* cx, JS::HandleObject obj,
     return true;
 }
 
-bool
-gjs_typecheck_fundamental(JSContext       *context,
-                          JS::HandleObject object,
-                          GType            expected_gtype,
-                          bool             throw_error)
-{
-    if (throw_error)
-        return FundamentalBase::typecheck(context, object, nullptr,
-                                          expected_gtype);
-    return FundamentalBase::typecheck(context, object, nullptr, expected_gtype,
-                                      FundamentalBase::TypecheckNoThrow());
-}
-
 void* FundamentalInstance::copy_ptr(JSContext* cx, GType gtype,
                                     void* gfundamental) {
     auto* priv = FundamentalPrototype::for_gtype(cx, gtype);
diff --git a/gi/fundamental.h b/gi/fundamental.h
index cafe0405..9f2960e0 100644
--- a/gi/fundamental.h
+++ b/gi/fundamental.h
@@ -189,12 +189,6 @@ JSObject *gjs_fundamental_from_g_value       (JSContext     *context,
                                               const GValue  *value,
                                               GType          gtype);
 
-GJS_USE
-bool      gjs_typecheck_fundamental(JSContext       *context,
-                                    JS::HandleObject object,
-                                    GType            expected_gtype,
-                                    bool             throw_error);
-
 void      gjs_fundamental_unref              (JSContext     *context,
                                               void          *fobj);
 
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 9af86fe0..9d3c656a 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -135,7 +135,8 @@ bool ErrorBase::to_string(JSContext* context, unsigned argc, JS::Value* vp) {
 
     // An error created via `new GLib.Error` will have a Boxed* private pointer,
     // not an Error*, so we can't call regular gjs_gerror_to_string() on it.
-    if (gjs_typecheck_boxed(context, self, nullptr, G_TYPE_ERROR, false)) {
+    if (BoxedBase::typecheck(context, self, nullptr, G_TYPE_ERROR,
+                             BoxedBase::TypecheckNoThrow())) {
         auto* gerror = BoxedBase::to_c_ptr<GError>(context, self);
         if (!gerror)
             return false;
@@ -401,7 +402,8 @@ GError* ErrorBase::to_c_ptr(JSContext* cx, JS::HandleObject obj) {
     /* If this is a plain GBoxed (i.e. a GError without metadata),
        delegate marshalling.
     */
-    if (gjs_typecheck_boxed(cx, obj, nullptr, G_TYPE_ERROR, false))
+    if (BoxedBase::typecheck(cx, obj, nullptr, G_TYPE_ERROR,
+                             BoxedBase::TypecheckNoThrow()))
         return BoxedBase::to_c_ptr<GError>(cx, obj);
 
     return GIWrapperBase::to_c_ptr<GError>(cx, obj);
@@ -414,7 +416,7 @@ bool ErrorBase::transfer_to_gi_argument(JSContext* cx, JS::HandleObject obj,
     g_assert(transfer_direction != GI_DIRECTION_INOUT &&
              "transfer_to_gi_argument() must choose between in or out");
 
-    if (!gjs_typecheck_gerror(cx, obj, true)) {
+    if (!ErrorBase::typecheck(cx, obj)) {
         arg->v_pointer = nullptr;
         return false;
     }
@@ -436,24 +438,27 @@ bool ErrorBase::transfer_to_gi_argument(JSContext* cx, JS::HandleObject obj,
     return true;
 }
 
-bool
-gjs_typecheck_gerror (JSContext       *context,
-                      JS::HandleObject obj,
-                      bool             throw_error)
-{
-    if (gjs_typecheck_boxed (context, obj, NULL, G_TYPE_ERROR, false))
+// Overrides GIWrapperBase::typecheck()
+bool ErrorBase::typecheck(JSContext* cx, JS::HandleObject obj) {
+    if (BoxedBase::typecheck(cx, obj, nullptr, G_TYPE_ERROR,
+                             BoxedBase::TypecheckNoThrow()))
         return true;
+    return GIWrapperBase::typecheck(cx, obj, nullptr, G_TYPE_ERROR);
+}
 
-    if (throw_error)
-        return !!ErrorBase::for_js_typecheck(context, obj);
-    return !!ErrorBase::for_js(context, obj);
+bool ErrorBase::typecheck(JSContext* cx, JS::HandleObject obj,
+                          TypecheckNoThrow no_throw) {
+    if (BoxedBase::typecheck(cx, obj, nullptr, G_TYPE_ERROR,
+                             BoxedBase::TypecheckNoThrow()))
+        return true;
+    return GIWrapperBase::typecheck(cx, obj, nullptr, G_TYPE_ERROR, no_throw);
 }
 
 GError *
 gjs_gerror_make_from_error(JSContext       *cx,
                            JS::HandleObject obj)
 {
-    if (gjs_typecheck_gerror(cx, obj, false)) {
+    if (ErrorBase::typecheck(cx, obj, ErrorBase::TypecheckNoThrow())) {
         /* This is already a GError, just copy it */
         GError* inner = ErrorBase::to_c_ptr(cx, obj);
         if (!inner)
diff --git a/gi/gerror.h b/gi/gerror.h
index 16b31eff..f6fc1544 100644
--- a/gi/gerror.h
+++ b/gi/gerror.h
@@ -100,6 +100,12 @@ class ErrorBase
                                         GIArgument* arg,
                                         GIDirection transfer_direction,
                                         GITransfer transfer_ownership);
+
+    GJS_JSAPI_RETURN_CONVENTION
+    static bool typecheck(JSContext* cx, JS::HandleObject obj);
+    GJS_USE
+    static bool typecheck(JSContext* cx, JS::HandleObject obj,
+                          TypecheckNoThrow);
 };
 
 class ErrorPrototype : public GIWrapperPrototype<ErrorBase, ErrorPrototype,
@@ -159,11 +165,6 @@ GJS_JSAPI_RETURN_CONVENTION
 JSObject* gjs_error_from_gerror        (JSContext             *context,
                                         GError                *gerror,
                                         bool                   add_stack);
-GJS_USE
-bool      gjs_typecheck_gerror         (JSContext             *context,
-                                        JS::HandleObject       obj,
-                                        bool                   throw_error);
-
 GJS_JSAPI_RETURN_CONVENTION
 GError *gjs_gerror_make_from_error(JSContext       *cx,
                                    JS::HandleObject obj);
diff --git a/gi/interface.cpp b/gi/interface.cpp
index 0d10061c..516c391d 100644
--- a/gi/interface.cpp
+++ b/gi/interface.cpp
@@ -111,7 +111,8 @@ bool InterfacePrototype::has_instance_impl(JSContext* cx,
     g_assert(args.length() == 1);
     g_assert(args[0].isObject());
     JS::RootedObject instance(cx, &args[0].toObject());
-    bool isinstance = gjs_typecheck_object(cx, instance, m_gtype, false);
+    bool isinstance = ObjectBase::typecheck(cx, instance, nullptr, m_gtype,
+                                            ObjectBase::TypecheckNoThrow());
     args.rval().setBoolean(isinstance);
     return true;
 }
diff --git a/gi/object.cpp b/gi/object.cpp
index d7db7be4..b3a5aab3 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -165,10 +165,14 @@ const JSObject* ObjectBase::jsobj_addr(void) const {
     return to_instance()->wrapper();
 }
 
-static bool
-throw_priv_is_null_error(JSContext *context)
-{
-    gjs_throw(context,
+// Overrides GIWrapperBase::typecheck(). We only override the overload that
+// throws, so that we can throw our own more informative error.
+bool ObjectBase::typecheck(JSContext* cx, JS::HandleObject obj,
+                           GIObjectInfo* expected_info, GType expected_gtype) {
+    if (GIWrapperBase::typecheck(cx, obj, expected_info, expected_gtype))
+        return true;
+
+    gjs_throw(cx,
               "This JS object wrapper isn't wrapping a GObject."
               " If this is a custom subclass, are you sure you chained"
               " up to the parent _init properly?");
@@ -2089,17 +2093,6 @@ gjs_typecheck_is_object(JSContext       *context,
     return !!ObjectBase::for_js(context, object);
 }
 
-bool gjs_typecheck_object(JSContext* cx, JS::HandleObject object,
-                          GType expected_type, bool throw_error) {
-    if (throw_error) {
-        if (!ObjectBase::typecheck(cx, object, nullptr, expected_type))
-            return throw_priv_is_null_error(cx);
-        return true;
-    }
-    return ObjectBase::typecheck(cx, object, nullptr, expected_type,
-                                 ObjectBase::TypecheckNoThrow());
-}
-
 // Overrides GIWrapperInstance::typecheck_impl()
 bool ObjectInstance::typecheck_impl(JSContext* cx, GIBaseInfo* expected_info,
                                     GType expected_type) const {
diff --git a/gi/object.h b/gi/object.h
index b56452c9..195ac504 100644
--- a/gi/object.h
+++ b/gi/object.h
@@ -125,6 +125,17 @@ class ObjectBase
  public:
     void type_query_dynamic_safe(GTypeQuery* query);
 
+    GJS_JSAPI_RETURN_CONVENTION
+    static bool typecheck(JSContext* cx, JS::HandleObject obj,
+                          GIObjectInfo* expected_info, GType expected_gtype);
+    GJS_USE
+    static bool typecheck(JSContext* cx, JS::HandleObject obj,
+                          GIObjectInfo* expected_info, GType expected_gtype,
+                          TypecheckNoThrow no_throw) {
+        return GIWrapperBase::typecheck(cx, obj, expected_info, expected_gtype,
+                                        no_throw);
+    }
+
     /* Methods to manipulate the list of closures */
 
  protected:
@@ -439,12 +450,6 @@ GJS_JSAPI_RETURN_CONVENTION
 JSObject* gjs_object_from_g_object      (JSContext     *context,
                                          GObject       *gobj);
 
-GJS_USE
-bool      gjs_typecheck_object(JSContext       *context,
-                               JS::HandleObject obj,
-                               GType            expected_type,
-                               bool             throw_error);
-
 GJS_USE
 bool      gjs_typecheck_is_object(JSContext       *context,
                                   JS::HandleObject obj,
diff --git a/gi/union.cpp b/gi/union.cpp
index ee112f26..9ac8490e 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -253,17 +253,3 @@ void* UnionInstance::copy_ptr(JSContext* cx, GType gtype, void* ptr) {
               "boxed");
     return nullptr;
 }
-
-bool
-gjs_typecheck_union(JSContext       *context,
-                    JS::HandleObject object,
-                    GIStructInfo    *expected_info,
-                    GType            expected_type,
-                    bool             throw_error)
-{
-    if (throw_error)
-        return UnionBase::typecheck(context, object, expected_info,
-                                    expected_type);
-    return UnionBase::typecheck(context, object, expected_info, expected_type,
-                                UnionBase::TypecheckNoThrow());
-}
diff --git a/gi/union.h b/gi/union.h
index e58106ac..36dbddce 100644
--- a/gi/union.h
+++ b/gi/union.h
@@ -106,12 +106,6 @@ GJS_JSAPI_RETURN_CONVENTION
 JSObject* gjs_union_from_c_union       (JSContext    *context,
                                         GIUnionInfo  *info,
                                         void         *gboxed);
-GJS_JSAPI_RETURN_CONVENTION
-bool      gjs_typecheck_union          (JSContext             *context,
-                                        JS::HandleObject       obj,
-                                        GIStructInfo          *expected_info,
-                                        GType                  expected_type,
-                                        bool                   throw_error);
 
 G_END_DECLS
 
diff --git a/gi/value.cpp b/gi/value.cpp
index 43613d21..241c35ad 100644
--- a/gi/value.cpp
+++ b/gi/value.cpp
@@ -462,7 +462,7 @@ gjs_value_to_g_value_internal(JSContext      *context,
             /* nothing to do */
         } else if (value.isObject()) {
             JS::RootedObject obj(context, &value.toObject());
-            if (!gjs_typecheck_object(context, obj, gtype, true) ||
+            if (!ObjectBase::typecheck(context, obj, nullptr, gtype) ||
                 !ObjectBase::to_c_ptr(context, obj, &gobj))
                 return false;
             if (!gobj)
@@ -564,10 +564,11 @@ gjs_value_to_g_value_internal(JSContext      *context,
                    loading the typelib.
                 */
                 if (!gboxed) {
-                    if (gjs_typecheck_union(context, obj, NULL, gtype, false)) {
+                    if (UnionBase::typecheck(context, obj, nullptr, gtype,
+                                             UnionBase::TypecheckNoThrow())) {
                         gboxed = UnionBase::to_c_ptr(context, obj);
                     } else {
-                        if (!gjs_typecheck_boxed(context, obj, NULL, gtype, true))
+                        if (!BoxedBase::typecheck(context, obj, nullptr, gtype))
                             return false;
 
                         gboxed = BoxedBase::to_c_ptr(context, obj);
@@ -592,7 +593,7 @@ gjs_value_to_g_value_internal(JSContext      *context,
         } else if (value.isObject()) {
             JS::RootedObject obj(context, &value.toObject());
 
-            if (!gjs_typecheck_boxed(context, obj, NULL, G_TYPE_VARIANT, true))
+            if (!BoxedBase::typecheck(context, obj, nullptr, G_TYPE_VARIANT))
                 return false;
 
             variant = BoxedBase::to_c_ptr<GVariant>(context, obj);
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 78316f70..49d279a2 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -298,7 +298,7 @@ from_gbytes_func(JSContext *context,
                              "bytes", &bytes_obj))
         return false;
 
-    if (!gjs_typecheck_boxed(context, bytes_obj, NULL, G_TYPE_BYTES, true))
+    if (!BoxedBase::typecheck(context, bytes_obj, nullptr, G_TYPE_BYTES))
         return false;
 
     gbytes = BoxedBase::to_c_ptr<GBytes>(context, bytes_obj);


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