[seed] Calling _init on external GI modules



commit 8cfcbe0caed11f780b50f2f99d835e3b270b92c4
Author: Danilo Cesar Lemes de Paula <danilo cesar collabora co uk>
Date:   Tue Mar 8 15:49:36 2016 -0300

    Calling _init on external GI modules

 extensions/gjs/GObject.js |   43 ++++++++++++++++++++++++-------------------
 libseed/seed-importer.c   |   21 +++++++++++++++++----
 2 files changed, 41 insertions(+), 23 deletions(-)
---
diff --git a/extensions/gjs/GObject.js b/extensions/gjs/GObject.js
index cc41485..e2fbb65 100644
--- a/extensions/gjs/GObject.js
+++ b/extensions/gjs/GObject.js
@@ -351,7 +351,8 @@ function _init() {
         return GObject.param_spec_param(name, nick, blurb, param_type, flags);
     };
 
-    this.ParamSpec.override = Gi.override_property;
+    // XXX:: Commented out due to compatibility issues
+    //this.ParamSpec.override = Gi.override_property;
 
     Object.defineProperties(this.ParamSpec.prototype, {
         'name': { configurable: false,
@@ -386,23 +387,27 @@ function _init() {
 
     this.Class = GObjectMeta;
     this.Interface = GObjectInterface;
-    this.Object.prototype.__metaclass__ = this.Class;
 
-    // For compatibility with Lang.Class... we need a _construct
-    // or the Lang.Class constructor will fail.
-    this.Object.prototype._construct = function() {
-        this._init.apply(this, arguments);
-        return this;
-    };
-
-    // fake enum for signal accumulators, keep in sync with gi/object.c
-    this.AccumulatorType = {
-        NONE: 0,
-        FIRST_WINS: 1,
-        TRUE_HANDLED: 2
-    };
-
-    this.Object.prototype.disconnect = function(id) {
-        return GObject.signal_handler_disconnect(this, id);
-    };
+    /* XXX: removed due to compability issues between gjs and seed
+     * 
+     * //this.Object.prototype.__metaclass__ = this.Class;
+     *
+     * // For compatibility with Lang.Class... we need a _construct
+     * // or the Lang.Class constructor will fail.
+     * this.Object.prototype._construct = function() {
+     *     this._init.apply(this, arguments);
+     *     return this;
+     * };
+
+     * // fake enum for signal accumulators, keep in sync with gi/object.c
+     * this.AccumulatorType = {
+     *     NONE: 0,
+     *     FIRST_WINS: 1,
+     *     TRUE_HANDLED: 2
+     * };
+
+     * this.Object.prototype.disconnect = function(id) {
+     *     return GObject.signal_handler_disconnect(this, id);
+     * };
+     */
 }
diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index 7c83691..0f30f38 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -630,6 +630,7 @@ seed_importer_get_search_path(JSContextRef ctx, JSValueRef* exception)
 static JSObjectRef seed_importer_handle_file(JSContextRef ctx,
                                              const gchar* dir,
                                              const gchar* file,
+                                             const gchar* module_name,
                                              JSValueRef* exception);
 
 static JSObjectRef
@@ -673,7 +674,7 @@ seed_importer_handle_native_module(JSContextRef ctx,
     JSValueProtect(ctx, module_obj);
 
     file_path = g_strconcat("libseed_", prop, ".js", NULL);
-    seed_importer_handle_file(ctx, dir, file_path, exception);
+    seed_importer_handle_file(ctx, dir, file_path, prop, exception);
     g_free(file_path);
 
     return module_obj;
@@ -696,12 +697,13 @@ static JSObjectRef
 seed_importer_handle_file(JSContextRef ctx,
                           const gchar* dir,
                           const gchar* file,
+                          const gchar* module_name,
                           JSValueRef* exception)
 {
     JSContextRef nctx;
     JSValueRef js_file_dirname;
     JSObjectRef global, c_global;
-    JSStringRef file_contents, file_name;
+    JSStringRef file_contents, file_name, initscript;
     gchar *contents, *walk, *file_path, *canonical, *absolute_path;
     char* normalized_path;
 
@@ -765,6 +767,16 @@ seed_importer_handle_file(JSContextRef ctx,
 
     JSEvaluateScript(nctx, file_contents, NULL, file_name, 0, exception);
 
+    if (gi_imports && g_hash_table_lookup(gi_imports, module_name)) {
+        SEED_NOTE(IMPORTER, "Calling %s_init():", module_name);
+        g_autofree gchar* initstr = g_strdup_printf(
+          "if (typeof(_init) === \"function\") { _init.apply(imports.gi.%s)}",
+          module_name, module_name);
+        initscript = JSStringCreateWithUTF8CString(initstr);
+        JSEvaluateScript(nctx, initscript, NULL, file_name, 0, exception);
+        JSStringRelease(initscript);
+    }
+
     // Does leak...but it's a debug statement.
     SEED_NOTE(IMPORTER, "Evaluated file, exception: %s",
               *exception ? seed_exception_to_string(ctx, *exception)
@@ -797,7 +809,7 @@ seed_importer_try_load(JSContextRef ctx,
     // check if prop is a file or dir (imports['foo.js'] or imports.mydir)
     file_path = g_build_filename(test_path, prop, NULL);
     if (g_file_test(file_path, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_DIR)) {
-        ret = seed_importer_handle_file(ctx, test_path, prop, exception);
+        ret = seed_importer_handle_file(ctx, test_path, prop, prop, exception);
         g_free(file_path);
         return ret;
     }
@@ -806,7 +818,8 @@ seed_importer_try_load(JSContextRef ctx,
     // check if prop is file ending with '.js'
     file_path = g_build_filename(test_path, prop_as_js, NULL);
     if (g_file_test(file_path, G_FILE_TEST_IS_REGULAR)) {
-        ret = seed_importer_handle_file(ctx, test_path, prop_as_js, exception);
+        ret = seed_importer_handle_file(ctx, test_path, prop_as_js, prop,
+                                        exception);
         g_free(file_path);
         return ret;
     }


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