[gjs] object: add a custom hook to run at instance_init



commit f90468bc72800f2aff7df7ea58b87fc500c6b73c
Author: Giovanni Campagna <gcampagna src gnome org>
Date:   Tue May 14 23:44:07 2013 +0200

    object: add a custom hook to run at instance_init
    
    This allows to run code that requires to be inside GTypeInstance
    init(), such as gtk_widget_init_template()
    
    https://bugzilla.gnome.org/show_bug.cgi?id=700347

 gi/object.cpp                          |   14 ++++++++++++++
 gjs/context.cpp                        |    2 +-
 gjs/jsapi-util.h                       |    1 +
 installed-tests/js/testGObjectClass.js |   21 +++++++++++++++++++++
 4 files changed, 37 insertions(+), 1 deletions(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index b571878..0771234 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -2481,6 +2481,7 @@ gjs_object_custom_init(GTypeInstance *instance,
     JSContext *context;
     JSObject *object;
     ObjectInstance *priv;
+    jsval v, r;
 
     object = (JSObject*) object_init_list->data;
     priv = (ObjectInstance*) JS_GetPrivate(object);
@@ -2499,6 +2500,19 @@ gjs_object_custom_init(GTypeInstance *instance,
     context = (JSContext*) gjs_context_get_native_context(gjs_context);
 
     associate_js_gobject(context, object, G_OBJECT (instance));
+
+    if (!gjs_object_get_property_const(context, object,
+                                       GJS_STRING_INSTANCE_INIT, &v)) {
+        gjs_log_exception(context);
+        return;
+    }
+
+    if (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))
+        return;
+
+    if (!JS_CallFunctionValue(context, object, v,
+                              0, NULL, &r))
+        gjs_log_exception(context);
 }
 
 static inline void
diff --git a/gjs/context.cpp b/gjs/context.cpp
index ae67489..d8e3785 100644
--- a/gjs/context.cpp
+++ b/gjs/context.cpp
@@ -80,7 +80,7 @@ static const char *const_strings[] = {
     "imports", "__parentModule__", "__init__", "searchPath",
     "__gjsKeepAlive", "__gjsPrivateNS",
     "gi", "versions", "overrides",
-    "_init", "_new_internal", "new",
+    "_init", "_instance_init", "_new_internal", "new",
     "message", "code", "stack", "fileName", "lineNumber", "name",
     "x", "y", "width", "height",
 };
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index 7c26232..f1182af 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -416,6 +416,7 @@ typedef enum {
   GJS_STRING_GI_VERSIONS,
   GJS_STRING_GI_OVERRIDES,
   GJS_STRING_GOBJECT_INIT,
+  GJS_STRING_INSTANCE_INIT,
   GJS_STRING_NEW_INTERNAL,
   GJS_STRING_NEW,
   GJS_STRING_MESSAGE,
diff --git a/installed-tests/js/testGObjectClass.js b/installed-tests/js/testGObjectClass.js
index 27d1316..b2017dd 100644
--- a/installed-tests/js/testGObjectClass.js
+++ b/installed-tests/js/testGObjectClass.js
@@ -153,6 +153,23 @@ const Derived = new Lang.Class({
     }
 });
 
+const MyCustomInit = new Lang.Class({
+    Name: 'MyCustomInit',
+    Extends: GObject.Object,
+
+    _init: function() {
+        this.foo = false;
+
+        this.parent();
+
+        JSUnit.assert(this.foo);
+    },
+
+    _instance_init: function() {
+        this.foo = true;
+    }
+});
+
 function testGObjectClass() {
     let myInstance = new MyObject();
 
@@ -274,4 +291,8 @@ function testDerived() {
     JSUnit.assertEquals('yes', derived.readwrite);
 }
 
+function testInstanceInit() {
+    new MyCustomInit();
+}
+
 JSUnit.gjstestRun(this, JSUnit.setUp, JSUnit.tearDown);


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