[libpeas] Make gjs extensions actual objects rather than copied arrays.



commit 9d6dd5e7538347a140c60594494bf156ef65e231
Author: Steve Frécinaux <code istique net>
Date:   Mon Apr 4 22:42:20 2011 +0200

    Make gjs extensions actual objects rather than copied arrays.
    
    Those objects are instantiated using the 'new' keyword, and behave just
    like regular objects created in javascript.

 loaders/gjs/peas-plugin-loader-gjs.c               |   27 ++++++++++++-------
 peas-demo/plugins/gjshello/gjshello.js             |   20 +++++++-------
 .../libpeas/plugins/extension-gjs/extension-gjs.js |   22 ++++++++--------
 3 files changed, 38 insertions(+), 31 deletions(-)
---
diff --git a/loaders/gjs/peas-plugin-loader-gjs.c b/loaders/gjs/peas-plugin-loader-gjs.c
index c3a4539..30a3a2c 100644
--- a/loaders/gjs/peas-plugin-loader-gjs.c
+++ b/loaders/gjs/peas-plugin-loader-gjs.c
@@ -152,7 +152,7 @@ peas_plugin_loader_gjs_create_extension (PeasPluginLoader *loader,
   PeasPluginLoaderGjs *gloader = PEAS_PLUGIN_LOADER_GJS (loader);
   GjsInfo *ginfo;
   JSContext *js_context;
-  jsval extension_methods;
+  jsval extension_ctor;
   JSObject *extension;
   guint i;
   jsval js_value;
@@ -163,21 +163,28 @@ peas_plugin_loader_gjs_create_extension (PeasPluginLoader *loader,
   js_context = gjs_context_get_native_context (ginfo->context);
 
   if (!JS_GetProperty (js_context, ginfo->extensions,
-                       g_type_name (exten_type), &extension_methods) ||
-      JSVAL_IS_VOID (extension_methods) || JSVAL_IS_NULL (extension_methods))
+                       g_type_name (exten_type), &extension_ctor) ||
+      JSVAL_IS_VOID (extension_ctor) || JSVAL_IS_NULL (extension_ctor))
     return NULL;
 
-  if (!JSVAL_IS_OBJECT (extension_methods))
+  if (!JSVAL_IS_OBJECT (extension_ctor))
     {
-      g_warning ("Extension '%s' in plugin '%s' in not a valid object",
+      g_warning ("Extension '%s' in plugin '%s' in not a valid constructor object",
                  g_type_name (exten_type),
                  peas_plugin_info_get_module_name (info));
       return NULL;
     }
 
-  /* Copy the original extension_methods object to a new specific object. */
-  extension = JS_NewObject (js_context, NULL,
-                            JSVAL_TO_OBJECT (extension_methods), NULL);
+  /* Instantiate the extension ctor object to a new specific object. */
+  extension = JS_New (js_context, JSVAL_TO_OBJECT (extension_ctor), 0, NULL);
+
+  if (!extension)
+    {
+      g_warning ("Extension '%s' in plugin '%s' is not a valid constructor object",
+                 g_type_name (exten_type),
+                 peas_plugin_info_get_module_name (info));
+      return NULL;
+    }
 
   /* Cannot use g_object_set_property()
    * because the property may be construct-only
@@ -196,8 +203,7 @@ peas_plugin_loader_gjs_create_extension (PeasPluginLoader *loader,
 
       if (!gjs_value_from_g_value (js_context, &js_value, &parameters[i].value))
         {
-          g_warning ("Error: failed to convert GValue to "
-                     "jsval for property '%s'", prop_name);
+          g_warning ("Error: failed to convert GValue to jsval for property '%s'", prop_name);
         }
       else if (!JS_SetProperty (js_context, extension, prop_name, &js_value))
         {
@@ -207,6 +213,7 @@ peas_plugin_loader_gjs_create_extension (PeasPluginLoader *loader,
       g_free (prop_name);
     }
 
+
   /* Set the plugin info as an attribute of the instance */
   g_value_init (&gvalue, PEAS_TYPE_PLUGIN_INFO);
   g_value_set_boxed (&gvalue, info);
diff --git a/peas-demo/plugins/gjshello/gjshello.js b/peas-demo/plugins/gjshello/gjshello.js
index 6301289..5c5ad32 100644
--- a/peas-demo/plugins/gjshello/gjshello.js
+++ b/peas-demo/plugins/gjshello/gjshello.js
@@ -4,27 +4,27 @@ var LABEL_STRING = "GJS Also Says Hello!";
 
 print("LABEL_STRING=" +  LABEL_STRING);
 
-var activatable_extension = {
-  activate: function() {
+function activatable_extension() {
+  this.activate = function() {
     print("GJSHelloPlugin.activate");
     this.object._gjshello_label = new Gtk.Label({ label: LABEL_STRING });
     this.object._gjshello_label.show();
     this.object.get_child().add(this.object._gjshello_label);
-  },
-  deactivate: function() {
+  };
+  this.deactivate = function() {
     print("GJSHelloPlugin.deactivate");
     this.object.get_child().remove(this.object._gjshello_label);
     this.object._gjshello_label.destroy();
-  },
-  update_state: function() {
+  };
+  this.update_state = function() {
     print("GJSHelloPlugin.update_state");
-  }
+  };
 };
 
-var configurable_extension = {
-  create_configure_widget: function () {
+function configurable_extension() {
+  this.create_configure_widget = function () {
     return new Gtk.Label({ label: "Example of configuration dialog for a GJS plugin" });
-  }
+  };
 };
 
 var extensions = {
diff --git a/tests/libpeas/plugins/extension-gjs/extension-gjs.js b/tests/libpeas/plugins/extension-gjs/extension-gjs.js
index e547f18..94b432d 100644
--- a/tests/libpeas/plugins/extension-gjs/extension-gjs.js
+++ b/tests/libpeas/plugins/extension-gjs/extension-gjs.js
@@ -1,20 +1,20 @@
-var callable_extension = {
-  call_with_return: function() {
+function callable_extension() {
+  this.call_with_return = function() {
     return "Hello, World!"
-  },
-  call_no_args: function() {
-  },
-  call_single_arg: function() {
+  };
+  this.call_no_args = function() {
+  };
+  this.call_single_arg = function() {
     return true
-  },
-  call_multi_args: function() {
+  };
+  this.call_multi_args = function() {
     return [ true, true, true ]
   }
 };
 
-var properties_extension = {
-  read_only: "read-only",
-  readwrite: "readwrite"
+function properties_extension() {
+  this.read_only = "read-only",
+  this.readwrite = "readwrite"
 };
 
 var extensions = {



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