[gjs] boxed, gerror, union, param, interface: apply the same cleanup as object



commit 52224e0a1581b25645b66f0e224274abf4883d95
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Thu Apr 25 01:51:37 2013 +0200

    boxed, gerror, union, param, interface: apply the same cleanup as object
    
    Clearly distinguish class definition from prototype lookup, with
    the added benefit that we don't need to create classes on the fly,
    because if something is not introspectable we simply bail out.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=699818

 gi/boxed.c       |   68 ++++-------------------------------------------------
 gi/boxed.h       |    6 +---
 gi/enumeration.c |   56 ++++++++++++++++----------------------------
 gi/enumeration.h |    3 +-
 gi/gerror.c      |   65 +++------------------------------------------------
 gi/gerror.h      |    6 +----
 gi/interface.c   |   33 +-------------------------
 gi/interface.h   |    3 +-
 gi/param.c       |   65 +++++++++++++++++++--------------------------------
 gi/param.h       |    7 +----
 gi/repo.c        |   52 ++++++++++++++++++++++++++++++++--------
 gi/repo.h        |    2 +
 gi/union.c       |   60 ++---------------------------------------------
 gi/union.h       |    6 +----
 14 files changed, 109 insertions(+), 323 deletions(-)
---
diff --git a/gi/boxed.c b/gi/boxed.c
index 47857e6..86ce443 100644
--- a/gi/boxed.c
+++ b/gi/boxed.c
@@ -593,7 +593,7 @@ get_nested_interface_object (JSContext   *context,
         return JS_FALSE;
     }
 
-    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) interface_info);
+    proto = gjs_lookup_generic_prototype(context, (GIBoxedInfo*) interface_info);
     proto_priv = priv_from_js(context, proto);
 
     offset = g_field_info_get_offset (field_info);
@@ -719,7 +719,7 @@ set_nested_interface_object (JSContext   *context,
         return JS_FALSE;
     }
 
-    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) interface_info);
+    proto = gjs_lookup_generic_prototype(context, (GIBoxedInfo*) interface_info);
     proto_priv = priv_from_js(context, proto);
 
     /* If we can't directly copy from the source object we need
@@ -952,25 +952,6 @@ static JSFunctionSpec gjs_boxed_proto_funcs[] = {
     { NULL }
 };
 
-JSObject*
-gjs_lookup_boxed_prototype(JSContext    *context,
-                           GIBoxedInfo  *info)
-{
-    JSObject *ns;
-    JSObject *proto;
-
-    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
-
-    if (ns == NULL)
-        return NULL;
-
-    proto = NULL;
-    if (gjs_define_boxed_class(context, ns, info, NULL, &proto))
-        return proto;
-    else
-        return NULL;
-}
-
 static gboolean
 type_can_be_allocated_directly(GITypeInfo *type_info)
 {
@@ -1153,12 +1134,10 @@ boxed_fill_prototype_info(JSContext *context,
     }
 }
 
-JSBool
+void
 gjs_define_boxed_class(JSContext    *context,
-                          JSObject     *in_object,
-                          GIBoxedInfo  *info,
-                          JSObject    **constructor_p,
-                          JSObject    **prototype_p)
+                       JSObject     *in_object,
+                       GIBoxedInfo  *info)
 {
     const char *constructor_name;
     JSObject *prototype;
@@ -1173,33 +1152,6 @@ gjs_define_boxed_class(JSContext    *context,
 
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
-    if (!JS_GetProperty(context, in_object, constructor_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        JSObject *constructor;
-
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like a constructor",
-                      constructor_name);
-            return JS_FALSE;
-        }
-
-        constructor = JSVAL_TO_OBJECT(value);
-
-        gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "boxed %s prototype property does not appear to exist or has wrong type", 
constructor_name);
-            return JS_FALSE;
-        } else {
-            if (prototype_p)
-                *prototype_p = JSVAL_TO_OBJECT(value);
-            if (constructor_p)
-                *constructor_p = constructor;
-
-            return JS_TRUE;
-        }
-    }
-
     if (!gjs_init_class_dynamic(context, in_object,
                                 NULL, /* parent prototype */
                                 g_base_info_get_namespace( (GIBaseInfo*) info),
@@ -1240,14 +1192,6 @@ gjs_define_boxed_class(JSContext    *context,
     value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, priv->gtype));
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
-
-    if (constructor_p)
-        *constructor_p = constructor;
-
-    if (prototype_p)
-        *prototype_p = prototype;
-
-    return JS_TRUE;
 }
 
 JSObject*
@@ -1268,7 +1212,7 @@ gjs_boxed_from_c_struct(JSContext             *context,
                       "Wrapping struct %s %p with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info), gboxed);
 
-    proto = gjs_lookup_boxed_prototype(context, info);
+    proto = gjs_lookup_generic_prototype(context, info);
     proto_priv = priv_from_js(context, proto);
 
     obj = JS_NewObjectWithGivenProto(context,
diff --git a/gi/boxed.h b/gi/boxed.h
index b00ac2a..d113d97 100644
--- a/gi/boxed.h
+++ b/gi/boxed.h
@@ -40,11 +40,9 @@ typedef enum {
 /* Hack for now... why doesn't gobject-introspection have this? */
 typedef GIStructInfo GIBoxedInfo;
 
-JSBool    gjs_define_boxed_class       (JSContext             *context,
+void      gjs_define_boxed_class       (JSContext             *context,
                                         JSObject              *in_object,
-                                        GIBoxedInfo           *info,
-                                        JSObject             **constructor_p,
-                                        JSObject             **prototype_p);
+                                        GIBoxedInfo           *info);
 JSObject* gjs_lookup_boxed_prototype   (JSContext             *context,
                                         GIBoxedInfo           *info);
 void*     gjs_c_struct_from_boxed      (JSContext             *context,
diff --git a/gi/enumeration.c b/gi/enumeration.c
index 76de0be..0fb0132 100644
--- a/gi/enumeration.c
+++ b/gi/enumeration.c
@@ -38,21 +38,26 @@
 
 JSObject*
 gjs_lookup_enumeration(JSContext    *context,
-                          GIEnumInfo   *info)
+                       GIEnumInfo   *info)
 {
-    JSObject *ns;
-    JSObject *enum_obj;
+    JSObject *in_object;
+    const char *enum_name;
+    jsval value;
 
-    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
+    in_object = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
 
-    if (ns == NULL)
+    if (G_UNLIKELY (!in_object))
         return NULL;
 
-    if (gjs_define_enumeration(context, ns, info,
-                               &enum_obj))
-        return enum_obj;
-    else
+    enum_name = g_base_info_get_name((GIBaseInfo*) info);
+
+    if (!JS_GetProperty(context, in_object, enum_name, &value))
         return NULL;
+
+    if (G_UNLIKELY (!JSVAL_IS_OBJECT(value)))
+        return NULL;
+
+    return JSVAL_TO_OBJECT(value);
 }
 
 static JSBool
@@ -138,13 +143,10 @@ gjs_define_enum_values(JSContext    *context,
 JSBool
 gjs_define_enumeration(JSContext    *context,
                        JSObject     *in_object,
-                       GIEnumInfo   *info,
-                       JSObject    **enumeration_p)
+                       GIEnumInfo   *info)
 {
     const char *enum_name;
     JSObject *enum_obj;
-    jsval value;
-
 
     /* An enumeration is simply an object containing integer attributes for
      * each enum value. It does not have a special JSClass.
@@ -156,27 +158,12 @@ gjs_define_enumeration(JSContext    *context,
      */
 
     enum_name = g_base_info_get_name( (GIBaseInfo*) info);
-
-    if (!JS_GetProperty(context, in_object, enum_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like an enum object",
-                      enum_name);
-            return JS_FALSE;
-        }
-
-        enum_obj = JSVAL_TO_OBJECT(value);
-
-        if (enumeration_p)
-            *enumeration_p = enum_obj;
-
-        return JS_TRUE;
-    }
-
     enum_obj = JS_NewObject(context, NULL, NULL, gjs_get_import_global (context));
-    if (enum_obj == NULL)
-        return JS_FALSE;
+    if (enum_obj == NULL) {
+        g_error("Could not create enumeration %s.%s",
+                       g_base_info_get_namespace( (GIBaseInfo*) info),
+                enum_name);
+    }
 
     /* https://bugzilla.mozilla.org/show_bug.cgi?id=599651 means we
      * can't just pass in the global as the parent */
@@ -199,8 +186,5 @@ gjs_define_enumeration(JSContext    *context,
         return JS_FALSE;
     }
 
-    if (enumeration_p)
-        *enumeration_p = enum_obj;
-
     return JS_TRUE;
 }
diff --git a/gi/enumeration.h b/gi/enumeration.h
index 2048887..afb3431 100644
--- a/gi/enumeration.h
+++ b/gi/enumeration.h
@@ -37,8 +37,7 @@ JSBool    gjs_define_enum_values       (JSContext    *context,
                                         GIEnumInfo   *info);
 JSBool    gjs_define_enumeration       (JSContext    *context,
                                         JSObject     *in_object,
-                                        GIEnumInfo   *info,
-                                        JSObject    **enumeration_p);
+                                        GIEnumInfo   *info);
 JSObject* gjs_lookup_enumeration       (JSContext    *context,
                                         GIEnumInfo   *info);
 
diff --git a/gi/gerror.c b/gi/gerror.c
index 329a07b..9eb3497 100644
--- a/gi/gerror.c
+++ b/gi/gerror.c
@@ -353,37 +353,15 @@ static JSFunctionSpec gjs_error_constructor_funcs[] = {
     JS_FS_END
 };
 
-JSObject*
-gjs_lookup_error_prototype(JSContext   *context,
-                           GIEnumInfo  *info)
-{
-    JSObject *ns;
-    JSObject *proto;
-
-    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
-
-    if (ns == NULL)
-        return NULL;
-
-    proto = NULL;
-    if (gjs_define_error_class(context, ns, info, NULL, &proto))
-        return proto;
-    else
-        return NULL;
-}
-
-JSBool
+void
 gjs_define_error_class(JSContext    *context,
                        JSObject     *in_object,
-                       GIEnumInfo   *info,
-                       JSObject    **constructor_p,
-                       JSObject    **prototype_p)
+                       GIEnumInfo   *info)
 {
     const char *constructor_name;
     GIBoxedInfo *glib_error_info;
     JSObject *prototype, *parent_proto;
     JSObject *constructor;
-    jsval value;
     Error *priv;
 
     /* See the comment in gjs_define_boxed_class() for an
@@ -393,36 +371,9 @@ gjs_define_error_class(JSContext    *context,
 
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
-    if (!JS_GetProperty(context, in_object, constructor_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        JSObject *constructor;
-
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like a constructor",
-                         constructor_name);
-            return JS_FALSE;
-        }
-
-        constructor = JSVAL_TO_OBJECT(value);
-
-        gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "error %s prototype property does not appear to exist or has wrong type", 
constructor_name);
-            return JS_FALSE;
-        } else {
-            if (prototype_p)
-                *prototype_p = JSVAL_TO_OBJECT(value);
-            if (constructor_p)
-                *constructor_p = constructor;
-
-            return JS_TRUE;
-        }
-    }
-
     g_irepository_require(NULL, "GLib", "2.0", 0, NULL);
     glib_error_info = (GIBoxedInfo*) g_irepository_find_by_name(NULL, "GLib", "Error");
-    parent_proto = gjs_lookup_boxed_prototype(context, glib_error_info);
+    parent_proto = gjs_lookup_generic_prototype(context, glib_error_info);
     g_base_info_unref((GIBaseInfo*)glib_error_info);
 
     if (!gjs_init_class_dynamic(context, in_object,
@@ -457,14 +408,6 @@ gjs_define_error_class(JSContext    *context,
               constructor_name, prototype, JS_GetClass(prototype), in_object);
 
     gjs_define_enum_values(context, constructor, priv->info);
-
-    if (constructor_p)
-        *constructor_p = constructor;
-
-    if (prototype_p)
-        *prototype_p = prototype;
-
-    return JS_TRUE;
 }
 
 static GIEnumInfo *
@@ -559,7 +502,7 @@ gjs_error_from_gerror(JSContext             *context,
                       "Wrapping struct %s %p with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info), gboxed);
 
-    proto = gjs_lookup_error_prototype(context, info);
+    proto = gjs_lookup_generic_prototype(context, info);
     proto_priv = priv_from_js(context, proto);
 
     obj = JS_NewObjectWithGivenProto(context,
diff --git a/gi/gerror.h b/gi/gerror.h
index 6a42f28..dfd2a38 100644
--- a/gi/gerror.h
+++ b/gi/gerror.h
@@ -31,12 +31,8 @@
 
 G_BEGIN_DECLS
 
-JSBool    gjs_define_error_class       (JSContext             *context,
+void      gjs_define_error_class       (JSContext             *context,
                                         JSObject              *in_object,
-                                        GIEnumInfo            *info,
-                                        JSObject             **constructor_p,
-                                        JSObject             **prototype_p);
-JSObject* gjs_lookup_error_prototype   (JSContext             *context,
                                         GIEnumInfo            *info);
 GError*   gjs_gerror_from_error        (JSContext             *context,
                                         JSObject              *obj);
diff --git a/gi/interface.c b/gi/interface.c
index b4f1907..ad39178 100644
--- a/gi/interface.c
+++ b/gi/interface.c
@@ -170,8 +170,7 @@ static JSFunctionSpec gjs_interface_proto_funcs[] = {
 JSBool
 gjs_define_interface_class(JSContext       *context,
                            JSObject        *in_object,
-                           GIInterfaceInfo *info,
-                           JSObject       **prototype_p)
+                           GIInterfaceInfo *info)
 {
     Interface *priv;
     const char *constructor_name;
@@ -181,33 +180,6 @@ gjs_define_interface_class(JSContext       *context,
 
     constructor_name = g_base_info_get_name((GIBaseInfo*)info);
 
-    if (!JS_GetProperty(context, in_object, constructor_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        JSObject *constructor;
-
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like a constructor",
-                      constructor_name);
-            return JS_FALSE;
-        }
-
-        constructor = JSVAL_TO_OBJECT(value);
-
-        gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "prototype property does not appear to exist or has wrong type");
-            return JS_FALSE;
-        } else {
-            if (prototype_p)
-                *prototype_p = JSVAL_TO_OBJECT(value);
-
-            return JS_TRUE;
-        }
-
-        return JS_TRUE;
-    }
-
     if (!gjs_init_class_dynamic(context, in_object,
                                 NULL,
                                 g_base_info_get_namespace((GIBaseInfo*)info),
@@ -240,8 +212,5 @@ gjs_define_interface_class(JSContext       *context,
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
 
-    if (prototype_p)
-        *prototype_p = prototype;
-
     return JS_TRUE;
 }
diff --git a/gi/interface.h b/gi/interface.h
index f8c3c4d..e926285 100644
--- a/gi/interface.h
+++ b/gi/interface.h
@@ -33,8 +33,7 @@ G_BEGIN_DECLS
 
 JSBool gjs_define_interface_class (JSContext       *context,
                                    JSObject        *in_object,
-                                   GIInterfaceInfo *info,
-                                   JSObject       **prototype_p);
+                                   GIInterfaceInfo *info);
 
 G_END_DECLS
 
diff --git a/gi/param.c b/gi/param.c
index 3c58f04..4ccdb9c 100644
--- a/gi/param.c
+++ b/gi/param.c
@@ -456,29 +456,42 @@ static JSFunctionSpec gjs_param_constructor_funcs[] = {
     { NULL }
 };
 
-JSObject*
+static JSObject*
 gjs_lookup_param_prototype(JSContext    *context)
 {
-    JSObject *ns;
-    JSObject *proto;
+    JSObject *in_object;
+    JSObject *constructor;
     jsid gobject_name;
+    jsval value;
 
     gobject_name = gjs_intern_string_to_id(context, "GObject");
-    ns = gjs_lookup_namespace_object_by_name(context, gobject_name);
+    in_object = gjs_lookup_namespace_object_by_name(context, gobject_name);
 
-    if (ns == NULL)
+    if (G_UNLIKELY (!in_object))
         return NULL;
 
-    if (gjs_define_param_class(context, ns, &proto))
-        return proto;
-    else
+    if (!JS_GetProperty(context, in_object, "ParamSpec", &value))
+        return NULL;
+
+    if (G_UNLIKELY (!JSVAL_IS_OBJECT(value) || JSVAL_IS_NULL(value)))
         return NULL;
+
+    constructor = JSVAL_TO_OBJECT(value);
+    g_assert(constructor != NULL);
+
+    if (!gjs_object_get_property_const(context, constructor,
+                                       GJS_STRING_PROTOTYPE, &value))
+        return NULL;
+
+    if (G_UNLIKELY (!JSVAL_IS_OBJECT(value)))
+        return NULL;
+
+    return JSVAL_TO_OBJECT(value);
 }
 
-JSBool
+void
 gjs_define_param_class(JSContext    *context,
-                       JSObject     *in_object,
-                       JSObject    **prototype_p)
+                       JSObject     *in_object)
 {
     const char *constructor_name;
     JSObject *prototype;
@@ -487,31 +500,6 @@ gjs_define_param_class(JSContext    *context,
 
     constructor_name = "ParamSpec";
 
-    if (!JS_GetProperty(context, in_object, constructor_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like a constructor",
-                      constructor_name);
-            return JS_FALSE;
-        }
-
-        constructor = JSVAL_TO_OBJECT(value);
-
-        gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "prototype property does not appear to exist or has wrong type");
-            return JS_FALSE;
-        } else {
-            if (prototype_p)
-                *prototype_p = JSVAL_TO_OBJECT(value);
-
-            return JS_TRUE;
-        }
-
-        return JS_TRUE;
-    }
-
     if (!gjs_init_class_dynamic(context, in_object,
                                 NULL,
                                 "GObject",
@@ -534,14 +522,9 @@ gjs_define_param_class(JSContext    *context,
     value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, G_TYPE_PARAM));
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
-    
-    if (prototype_p)
-        *prototype_p = prototype;
 
     gjs_debug(GJS_DEBUG_GPARAM, "Defined class %s prototype is %p class %p in object %p",
               constructor_name, prototype, JS_GetClass(prototype), in_object);
-
-    return JS_TRUE;
 }
 
 JSObject*
diff --git a/gi/param.h b/gi/param.h
index 15b004d..cb3e69c 100644
--- a/gi/param.h
+++ b/gi/param.h
@@ -30,9 +30,8 @@
 
 G_BEGIN_DECLS
 
-JSBool      gjs_define_param_class     (JSContext  *context,
-                                        JSObject   *in_object,
-                                        JSObject  **prototype_p);
+void        gjs_define_param_class     (JSContext  *context,
+                                        JSObject   *in_object);
 GParamSpec* gjs_g_param_from_param     (JSContext  *context,
                                         JSObject   *obj);
 JSObject*   gjs_param_from_g_param     (JSContext  *context,
@@ -41,8 +40,6 @@ JSBool      gjs_typecheck_param        (JSContext  *context,
                                         JSObject   *obj,
                                         GType       expected_type,
                                         JSBool      throw);
-JSObject*   gjs_lookup_param_prototype (JSContext  *context);
-
 
 G_END_DECLS
 
diff --git a/gi/repo.c b/gi/repo.c
index 1f94c3a..9d667f0 100644
--- a/gi/repo.c
+++ b/gi/repo.c
@@ -470,8 +470,7 @@ gjs_define_info(JSContext  *context,
             gtype = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)info);
 
             if (g_type_is_a (gtype, G_TYPE_PARAM)) {
-                if (!gjs_define_param_class(context, in_object, NULL))
-                    return JS_FALSE;
+                gjs_define_param_class(context, in_object);
             } else if (g_type_is_a (gtype, G_TYPE_OBJECT)) {
                 gjs_define_object_class(context, in_object, (GIObjectInfo*) info, gtype, NULL);
             } else {
@@ -484,23 +483,21 @@ gjs_define_info(JSContext  *context,
         break;
     case GI_INFO_TYPE_STRUCT:
     case GI_INFO_TYPE_BOXED:
-        if (!gjs_define_boxed_class(context, in_object, (GIBoxedInfo*) info, NULL, NULL))
-            return JS_FALSE;
+        gjs_define_boxed_class(context, in_object, (GIBoxedInfo*) info);
         break;
     case GI_INFO_TYPE_UNION:
-        if (!gjs_define_union_class(context, in_object, (GIUnionInfo*) info, NULL, NULL))
-            return JS_FALSE;
+        gjs_define_union_class(context, in_object, (GIUnionInfo*) info);
         break;
     case GI_INFO_TYPE_ENUM:
         if (g_enum_info_get_error_domain((GIEnumInfo*) info)) {
             /* define as GError subclass */
-            if (!gjs_define_error_class(context, in_object, (GIEnumInfo*) info, NULL, NULL))
-                return JS_FALSE;
+            gjs_define_error_class(context, in_object, (GIEnumInfo*) info);
+            break;
         }
         /* fall through */
 
     case GI_INFO_TYPE_FLAGS:
-        if (!gjs_define_enumeration(context, in_object, (GIEnumInfo*) info, NULL))
+        if (!gjs_define_enumeration(context, in_object, (GIEnumInfo*) info))
             return JS_FALSE;
         break;
     case GI_INFO_TYPE_CONSTANT:
@@ -508,8 +505,7 @@ gjs_define_info(JSContext  *context,
             return JS_FALSE;
         break;
     case GI_INFO_TYPE_INTERFACE:
-        if (!gjs_define_interface_class(context, in_object, (GIInterfaceInfo*) info, NULL))
-            return JS_FALSE;
+        gjs_define_interface_class(context, in_object, (GIInterfaceInfo*) info);
         break;
     default:
         gjs_throw(context, "API of type %s not implemented, cannot define %s.%s",
@@ -744,3 +740,37 @@ gjs_hyphen_from_camel(const char *camel_name)
 
     return g_string_free(s, FALSE);
 }
+
+JSObject *
+gjs_lookup_generic_prototype(JSContext  *context,
+                             GIBaseInfo *info)
+{
+    JSObject *in_object;
+    JSObject *constructor;
+    const char *constructor_name;
+    jsval value;
+
+    in_object = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
+    constructor_name = g_base_info_get_name((GIBaseInfo*) info);
+
+    if (G_UNLIKELY (!in_object))
+        return NULL;
+
+    if (!JS_GetProperty(context, in_object, constructor_name, &value))
+        return NULL;
+
+    if (G_UNLIKELY (!JSVAL_IS_OBJECT(value) || JSVAL_IS_NULL(value)))
+        return NULL;
+
+    constructor = JSVAL_TO_OBJECT(value);
+    g_assert(constructor != NULL);
+
+    if (!gjs_object_get_property_const(context, constructor,
+                                       GJS_STRING_PROTOTYPE, &value))
+        return NULL;
+
+    if (G_UNLIKELY (!JSVAL_IS_OBJECT(value)))
+        return NULL;
+
+    return JSVAL_TO_OBJECT(value);
+}
diff --git a/gi/repo.h b/gi/repo.h
index ff3214c..940a6e7 100644
--- a/gi/repo.h
+++ b/gi/repo.h
@@ -43,6 +43,8 @@ JSObject*   gjs_lookup_namespace_object_by_name (JSContext      *context,
                                                  jsid            name);
 JSObject*   gjs_lookup_function_object          (JSContext      *context,
                                                  GIFunctionInfo *info);
+JSObject *  gjs_lookup_generic_prototype        (JSContext      *context,
+                                                 GIBaseInfo     *info);
 JSBool      gjs_define_info                     (JSContext      *context,
                                                  JSObject       *in_object,
                                                  GIBaseInfo     *info);
diff --git a/gi/union.c b/gi/union.c
index 0f338d5..ffeb7e6 100644
--- a/gi/union.c
+++ b/gi/union.c
@@ -333,31 +333,10 @@ static JSFunctionSpec gjs_union_proto_funcs[] = {
     { NULL }
 };
 
-JSObject*
-gjs_lookup_union_prototype(JSContext    *context,
-                           GIUnionInfo  *info)
-{
-    JSObject *ns;
-    JSObject *proto;
-
-    ns = gjs_lookup_namespace_object(context, (GIBaseInfo*) info);
-
-    if (ns == NULL)
-        return NULL;
-
-    proto = NULL;
-    if (gjs_define_union_class(context, ns, info, NULL, &proto))
-        return proto;
-    else
-        return NULL;
-}
-
-JSBool
+void
 gjs_define_union_class(JSContext    *context,
                        JSObject     *in_object,
-                       GIUnionInfo  *info,
-                       JSObject    **constructor_p,
-                       JSObject    **prototype_p)
+                       GIUnionInfo  *info)
 {
     const char *constructor_name;
     JSObject *prototype;
@@ -382,31 +361,6 @@ gjs_define_union_class(JSContext    *context,
 
     constructor_name = g_base_info_get_name( (GIBaseInfo*) info);
 
-    if (!JS_GetProperty(context, in_object, constructor_name, &value))
-        return JS_FALSE;
-    if (!JSVAL_IS_VOID(value)) {
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "Existing property '%s' does not look like a constructor",
-                         constructor_name);
-            return JS_FALSE;
-        }
-
-        constructor = JSVAL_TO_OBJECT(value);
-
-        gjs_object_get_property_const(context, constructor, GJS_STRING_PROTOTYPE, &value);
-        if (!JSVAL_IS_OBJECT(value)) {
-            gjs_throw(context, "union %s prototype property does not appear to exist or has wrong type", 
constructor_name);
-            return JS_FALSE;
-        } else {
-            if (prototype_p)
-                *prototype_p = JSVAL_TO_OBJECT(value);
-            if (constructor_p)
-                *constructor_p = constructor;
-
-            return JS_TRUE;
-        }
-    }
-
     if (!gjs_init_class_dynamic(context, in_object,
                                 NULL,
                                 g_base_info_get_namespace( (GIBaseInfo*) info),
@@ -439,14 +393,6 @@ gjs_define_union_class(JSContext    *context,
     value = OBJECT_TO_JSVAL(gjs_gtype_create_gtype_wrapper(context, gtype));
     JS_DefineProperty(context, constructor, "$gtype", value,
                       NULL, NULL, JSPROP_PERMANENT);
-
-    if (constructor_p)
-        *constructor_p = constructor;
-
-    if (prototype_p)
-        *prototype_p = prototype;
-
-    return JS_TRUE;
 }
 
 JSObject*
@@ -475,7 +421,7 @@ gjs_union_from_c_union(JSContext    *context,
                       "Wrapping union %s %p with JSObject",
                       g_base_info_get_name((GIBaseInfo *)info), gboxed);
 
-    proto = gjs_lookup_union_prototype(context, (GIUnionInfo*) info);
+    proto = gjs_lookup_generic_prototype(context, (GIUnionInfo*) info);
 
     obj = JS_NewObjectWithGivenProto(context,
                                      JS_GetClass(proto), proto,
diff --git a/gi/union.h b/gi/union.h
index 9b78770..94744ce 100644
--- a/gi/union.h
+++ b/gi/union.h
@@ -30,12 +30,8 @@
 
 G_BEGIN_DECLS
 
-JSBool    gjs_define_union_class       (JSContext    *context,
+void      gjs_define_union_class       (JSContext    *context,
                                         JSObject     *in_object,
-                                        GIUnionInfo  *info,
-                                        JSObject    **constructor_p,
-                                        JSObject    **prototype_p);
-JSObject* gjs_lookup_union_prototype   (JSContext    *context,
                                         GIUnionInfo  *info);
 void*     gjs_c_union_from_union       (JSContext    *context,
                                         JSObject     *obj);


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