[gjs] object: Don't crash when given a GObject singleton



commit c63b9f34380352c301fa5e211d03f20a0a4afd97
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Thu Jan 12 13:36:48 2012 -0500

    object: Don't crash when given a GObject singleton
    
    Some GObject libraries, like IBus, implement singletons by adding a custom
    construct vfunc, from which they return an existing GObject. This means that
    g_object_newv may not always return a new object. In these cases, returning the
    existing wrapper JS object should be enough to help clients out.

 gi/object.c |   16 ++++++++++++++++
 1 files changed, 16 insertions(+), 0 deletions(-)
---
diff --git a/gi/object.c b/gi/object.c
index 79a921d..d7caefb 100644
--- a/gi/object.c
+++ b/gi/object.c
@@ -703,6 +703,21 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
     }
 
     priv->gobj = g_object_newv(gtype, n_params, params);
+
+    if (peek_js_obj(context, priv->gobj) != NULL) {
+        /* g_object_newv returned an object that's already tracked by a JS
+         * object. Let's assume this is a singleton like IBus.IBus and return
+         * the existing JS wrapper object.
+         *
+         * 'object' has a value that was originally created by
+         * JS_NewObjectForConstructor in GJS_NATIVE_CONSTRUCTOR_PRELUDE, but
+         * we're not actually using it, so just let it get collected. Avoiding
+         * this would require a non-trivial amount of work.
+         * */
+        object = peek_js_obj(context, priv->gobj);
+        goto out;
+    }
+
     free_g_params(params, n_params);
 
     g_type_query(gtype, &query);
@@ -732,6 +747,7 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(object_instance)
     TRACE(GJS_OBJECT_PROXY_NEW(priv, priv->gobj, g_base_info_get_namespace ( (GIBaseInfo*) priv->info),
                                g_base_info_get_name ( (GIBaseInfo*) priv->info) ));
 
+ out:
     GJS_NATIVE_CONSTRUCTOR_FINISH(object_instance);
 
     return JS_TRUE;



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