[gjs: 8/16] boxed, gerror: Make define_class() functions fallible



commit d69605e2d89da86e0a2175a46ef2dde6bf72bbf1
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Oct 28 16:45:58 2018 -0400

    boxed, gerror: Make define_class() functions fallible
    
    Operations done during these functions can throw exceptions, so they need
    to be fallible so that the exceptions can be properly propagated.

 gi/boxed.cpp  | 23 ++++++++++++-----------
 gi/boxed.h    |  6 +++---
 gi/gerror.cpp | 14 +++++---------
 gi/gerror.h   |  6 +++---
 gi/repo.cpp   |  6 ++++--
 5 files changed, 27 insertions(+), 28 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 421607f6..86d08ffc 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -1117,11 +1117,8 @@ boxed_fill_prototype_info(JSContext *context,
     }
 }
 
-void
-gjs_define_boxed_class(JSContext       *context,
-                       JS::HandleObject in_object,
-                       GIBoxedInfo     *info)
-{
+bool gjs_define_boxed_class(JSContext* context, JS::HandleObject in_object,
+                            GIBoxedInfo* info) {
     const char *constructor_name;
     JS::RootedObject prototype(context), constructor(context);
     Boxed *priv;
@@ -1149,8 +1146,7 @@ gjs_define_boxed_class(JSContext       *context,
                                 NULL,
                                 &prototype,
                                 &constructor)) {
-        gjs_log_exception(context);
-        g_error("Can't init class %s", constructor_name);
+        return false;
     }
 
     GJS_INC_COUNTER(boxed);
@@ -1169,13 +1165,18 @@ gjs_define_boxed_class(JSContext       *context,
 
     priv->can_allocate_directly = struct_is_simple (priv->info);
 
-    define_boxed_class_fields (context, priv, prototype);
-    gjs_define_static_methods (context, constructor, priv->gtype, priv->info);
+    if (!define_boxed_class_fields(context, priv, prototype) ||
+        !gjs_define_static_methods(context, constructor, priv->gtype,
+                                   priv->info))
+        return false;
 
     JS::RootedObject gtype_obj(context,
         gjs_gtype_create_gtype_wrapper(context, priv->gtype));
-    JS_DefineProperty(context, constructor, "$gtype", gtype_obj,
-                      JSPROP_PERMANENT);
+    if (!gtype_obj)
+        return false;
+
+    return JS_DefineProperty(context, constructor, "$gtype", gtype_obj,
+                             JSPROP_PERMANENT);
 }
 
 JSObject*
diff --git a/gi/boxed.h b/gi/boxed.h
index e323679c..c7f167a6 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -42,9 +42,9 @@ typedef enum {
 /* Hack for now... why doesn't gobject-introspection have this? */
 typedef GIStructInfo GIBoxedInfo;
 
-void      gjs_define_boxed_class       (JSContext             *context,
-                                        JS::HandleObject       in_object,
-                                        GIBoxedInfo           *info);
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_define_boxed_class(JSContext* cx, JS::HandleObject in_object,
+                            GIBoxedInfo* info);
 
 GJS_JSAPI_RETURN_CONVENTION
 void*     gjs_c_struct_from_boxed      (JSContext             *context,
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index bc2ce6d4..9f591c4f 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -307,11 +307,8 @@ static JSFunctionSpec gjs_error_constructor_funcs[] = {
     JS_FN("valueOf", error_constructor_value_of, 0, GJS_MODULE_PROP_FLAGS),
     JS_FS_END};
 
-void
-gjs_define_error_class(JSContext       *context,
-                       JS::HandleObject in_object,
-                       GIEnumInfo      *info)
-{
+bool gjs_define_error_class(JSContext* context, JS::HandleObject in_object,
+                            GIEnumInfo* info) {
     const char *constructor_name;
     GIBoxedInfo *glib_error_info;
     JS::RootedObject prototype(context), constructor(context);
@@ -340,8 +337,7 @@ gjs_define_error_class(JSContext       *context,
             gjs_error_constructor_funcs,  // funcs of constructor,
                                           // MyConstructor.myfunc()
             &prototype, &constructor)) {
-        gjs_log_exception(context);
-        g_error("Can't init class %s", constructor_name);
+        return false;
     }
 
     GJS_INC_COUNTER(gerror);
@@ -356,8 +352,8 @@ gjs_define_error_class(JSContext       *context,
               constructor_name, prototype.get(), JS_GetClass(prototype),
               in_object.get());
 
-    gjs_define_enum_values(context, constructor, priv->info);
-    gjs_define_enum_static_methods(context, constructor, priv->info);
+    return gjs_define_enum_values(context, constructor, priv->info) &&
+           gjs_define_enum_static_methods(context, constructor, priv->info);
 }
 
 GJS_USE
diff --git a/gi/gerror.h b/gi/gerror.h
index 1ff0a300..2ac3e944 100644
--- a/gi/gerror.h
+++ b/gi/gerror.h
@@ -33,9 +33,9 @@
 
 G_BEGIN_DECLS
 
-void      gjs_define_error_class       (JSContext             *context,
-                                        JS::HandleObject       in_object,
-                                        GIEnumInfo            *info);
+GJS_JSAPI_RETURN_CONVENTION
+bool gjs_define_error_class(JSContext* cx, JS::HandleObject in_object,
+                            GIEnumInfo* info);
 GJS_JSAPI_RETURN_CONVENTION
 GError*   gjs_gerror_from_error        (JSContext             *context,
                                         JS::HandleObject       obj);
diff --git a/gi/repo.cpp b/gi/repo.cpp
index 6948dfc5..0f34c84a 100644
--- a/gi/repo.cpp
+++ b/gi/repo.cpp
@@ -460,7 +460,8 @@ gjs_define_info(JSContext       *context,
         /* Fall through */
 
     case GI_INFO_TYPE_BOXED:
-        gjs_define_boxed_class(context, in_object, (GIBoxedInfo*) info);
+        if (!gjs_define_boxed_class(context, in_object, info))
+            return false;
         break;
     case GI_INFO_TYPE_UNION:
         if (!gjs_define_union_class(context, in_object, (GIUnionInfo*) info))
@@ -469,7 +470,8 @@ gjs_define_info(JSContext       *context,
     case GI_INFO_TYPE_ENUM:
         if (g_enum_info_get_error_domain((GIEnumInfo*) info)) {
             /* define as GError subclass */
-            gjs_define_error_class(context, in_object, (GIEnumInfo*) info);
+            if (!gjs_define_error_class(context, in_object, info))
+                return false;
             break;
         }
         /* fall through */


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