[seed] Made named constuctors compatible with gjs



commit 4941addf6d0967391ee6c9db8ab253ca3bce900d
Author: Peter Rustler <peter rustler basyskom com>
Date:   Wed Dec 9 12:24:05 2015 +0000

    Made named constuctors compatible with gjs
    
    In gjs you can call named constuctors like this:
    var x = Object.new(parameter)
    var x = Object.new_from_file(parameter)
    In seed this is:
    var x = new Object.c_new(parameter)
    var x = new Object.from_file(parameter)
    
    This patch adds the possibility to call named constructors as methods,
    register a version of constructors named "new" as "c_new" and "new" and
    register constructors named new_* also with their native names.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=759171

 libseed/seed-engine.c   |    9 +++++++--
 libseed/seed-importer.c |   32 ++++++++++++++++++++++++--------
 2 files changed, 31 insertions(+), 10 deletions(-)
---
diff --git a/libseed/seed-engine.c b/libseed/seed-engine.c
index 95489ee..35be98f 100644
--- a/libseed/seed-engine.c
+++ b/libseed/seed-engine.c
@@ -1005,8 +1005,11 @@ seed_gobject_define_property_from_function_info (JSContextRef ctx,
   JSObjectSetPrototype (ctx, method_ref, function_proto);
 
   name = g_base_info_get_name ((GIBaseInfo *) info);
-  if (!g_strcmp0 (name, "new"))
+  if (!g_strcmp0 (name, "new")) {
+    // To be compatible with gjs, we need to have new as function, too.
+    seed_object_set_property (ctx, object, name, method_ref);
     name = "c_new";
+  }
   seed_object_set_property (ctx, object, name, method_ref);
   /*
     //  Disabled as this crashes in a recursive loop now
@@ -1528,7 +1531,9 @@ JSClassDefinition gobject_named_constructor_def = {
   NULL,                                /* Set Property */
   NULL,                                /* Delete Property */
   NULL,                                /* Get Property Names */
-  NULL,                                /* Call As Function */
+  // To be compatible with gjs you need to be able to call named
+  // constructors without the new in front
+  seed_gobject_method_invoked,                         /* Call As Function */
   seed_gobject_named_constructor_invoked,      /* Call As Constructor */
   seed_gobject_has_instance,                           /* Has Instance */
   seed_gobject_constructor_convert_to_type
diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index 7ba98c8..380e18f 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -202,10 +202,19 @@ seed_gi_importer_handle_object (JSContextRef ctx,
                                                      finfo);
              const gchar *fname =
                g_base_info_get_name ((GIBaseInfo *) finfo);
-             if (g_strrstr (fname, "new_") == fname)
-               fname += 4;
-             else if (!g_strcmp0 (fname, "new"))
-               fname = "c_new";
+             if (g_strrstr (fname, "new_") == fname) {
+                     // To be compatible with gjs, we need to have a method with new_, too.
+                     seed_object_set_property (ctx,
+                                               constructor_ref, fname, constructor);
+                     fname += 4;
+             }
+
+             else if (!g_strcmp0 (fname, "new")) {
+                     // To be compatible with gjs, we need to have new as function, too.
+                     seed_object_set_property (ctx,
+                                               constructor_ref, fname, constructor);
+                     fname = "c_new";
+             }
 
              seed_object_set_property (ctx,
                                        constructor_ref, fname, constructor);
@@ -309,10 +318,17 @@ seed_gi_importer_handle_struct (JSContextRef ctx,
                                                  gobject_named_constructor_class,
                                                  finfo);
          const gchar *fname = g_base_info_get_name ((GIBaseInfo *) finfo);
-         if (g_str_has_prefix (fname, "new_"))
-           fname += 4;
-         else if (!g_strcmp0 (fname, "new"))
-           fname = "c_new";
+      if (g_strrstr (fname, "new_") == fname) {
+             // To be compatible with gjs, we need to have a method with new_, too.
+             seed_object_set_property (ctx, struct_ref, fname, constructor);
+             fname += 4;
+      }
+
+      else if (!g_strcmp0 (fname, "new")) {
+             // To be compatible with gjs, we need to have new as function, too.
+             seed_object_set_property (ctx, struct_ref, fname, constructor);
+             fname = "c_new";
+      }
 
          seed_object_set_property (ctx, struct_ref, fname, constructor);
        }


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