[seed] Rework some of the GClosure logic to remove some code duplication



commit ffd8b44c7f65bf7f42694818608f29ac5403dbe6
Author: Robert Carr <racarr svn gnome org>
Date:   Sat May 9 18:07:47 2009 -0400

    Rework some of the GClosure logic to remove some code duplication
---
 libseed/seed-closure.c |   40 ++++++++++++++++++++++++++++++++--------
 libseed/seed-closure.h |    9 +++++++--
 libseed/seed-signals.c |   43 +++----------------------------------------
 3 files changed, 42 insertions(+), 50 deletions(-)

diff --git a/libseed/seed-closure.c b/libseed/seed-closure.c
index 1667693..7ed989b 100644
--- a/libseed/seed-closure.c
+++ b/libseed/seed-closure.c
@@ -318,27 +318,51 @@ seed_make_native_closure (JSContextRef ctx,
   return privates;
 }
 
-SeedClosure *
-seed_make_gclosure (JSContextRef ctx, JSObjectRef function, JSObjectRef this)
+static void
+closure_invalidated (gpointer data, GClosure * c)
+{
+  JSContextRef ctx = JSGlobalContextCreateInGroup (context_group,
+						   0);
+  SeedClosure *closure = (SeedClosure *) c;
+  
+  SEED_NOTE (FINALIZATION, "Finalizing closure.");
+  if (closure->user_data && !JSValueIsUndefined(ctx, closure->user_data))
+    JSValueUnprotect(ctx, closure->user_data);
+  if (!JSValueIsUndefined (ctx, closure->function))
+    JSValueUnprotect (ctx, closure->function);
+
+  JSGlobalContextRelease ((JSGlobalContextRef) ctx);
+}
+
+JSObjectRef
+seed_closure_get_callable (GClosure *c)
+{
+  return ((SeedClosure *)c)->function;
+}
+
+
+GClosure *
+seed_make_gclosure (JSContextRef ctx, JSObjectRef function, JSObjectRef user_data)
 {
   GClosure *closure;
 
   closure = g_closure_new_simple (sizeof (SeedClosure), 0);
+  g_closure_add_finalize_notifier (closure, 0, closure_invalidated);
   g_closure_set_marshal (closure, seed_signal_marshal_func);
 
   ((SeedClosure *) closure)->function = function;
   ((SeedClosure *) closure)->object = 0;
-  if (this && !JSValueIsNull (ctx, this))
+  if (user_data && !JSValueIsNull (ctx, user_data))
     {
-      JSValueProtect (ctx, this);
-      ((SeedClosure *) closure)->this = this;
+      ((SeedClosure *) closure)->user_data = user_data;
+      JSValueProtect(ctx, user_data);
     }
   else
-    ((SeedClosure *) closure)->this = 0;
-
+    ((SeedClosure *) closure)->user_data = NULL;
+  
   JSValueProtect (ctx, function);
 
-  return (SeedClosure *) closure;
+  return closure;
 }
 
 JSClassDefinition seed_native_callback_def = {
diff --git a/libseed/seed-closure.h b/libseed/seed-closure.h
index 95c8335..c371913 100644
--- a/libseed/seed-closure.h
+++ b/libseed/seed-closure.h
@@ -51,8 +51,13 @@ extern JSClassRef seed_native_callback_class;
 SeedNativeClosure *seed_make_native_closure (JSContextRef ctx,
 					     GICallableInfo * info,
 					     JSValueRef function);
-SeedClosure *seed_make_gclosure (JSContextRef ctx,
-				 JSObjectRef function, JSObjectRef this);
+GClosure *seed_make_gclosure (JSContextRef ctx,
+				 JSObjectRef function, JSObjectRef user_data);
+
+JSObjectRef
+seed_closure_get_callable (GClosure *c);
+
+
 
 void seed_closures_init ();
 
diff --git a/libseed/seed-signals.c b/libseed/seed-signals.c
index b6794d1..2e5a56a 100644
--- a/libseed/seed-signals.c
+++ b/libseed/seed-signals.c
@@ -37,21 +37,7 @@ seed_signal_finalize (JSObjectRef object)
 }
 
 
-static void
-closure_invalidated (gpointer data, GClosure * c)
-{
-  JSContextRef ctx = JSGlobalContextCreateInGroup (context_group,
-						   0);
-  SeedClosure *closure = (SeedClosure *) c;
-  
-  SEED_NOTE (FINALIZATION, "Finalizing closure.");
-  if (closure->user_data && !JSValueIsUndefined(ctx, closure->user_data))
-    JSValueUnprotect(ctx, closure->user_data);
-  if (!JSValueIsUndefined (ctx, closure->function))
-    JSValueUnprotect (ctx, closure->function);
 
-  JSGlobalContextRelease ((JSGlobalContextRef) ctx);
-}
 
 void
 seed_gobject_signal_connect (JSContextRef ctx,
@@ -81,32 +67,9 @@ seed_gobject_signal_connect (JSContextRef ctx,
   }
 #endif
 
-  closure = g_closure_new_simple (sizeof (SeedClosure), 0);
-  g_closure_add_finalize_notifier (closure, 0, closure_invalidated);
-  g_closure_set_marshal (closure, seed_signal_marshal_func);
-
-  JSValueProtect (ctx, func);
-  ((SeedClosure *) closure)->function = func;
-
-  //((SeedClosure *) closure)->object = on_obj;
+  closure = seed_make_gclosure (ctx, func, user_data);
+  // This seems wrong...
   ((SeedClosure *) closure)->return_type = query.return_type;
-
-  if (this_obj && !JSValueIsNull (ctx, this_obj))
-    {
-      JSValueProtect (ctx, this_obj);
-//      ((SeedClosure *) closure)->this = this_obj;
-    }
-  else
-    {
-      ((SeedClosure *) closure)->this = 0;
-    }
-
-  if (user_data && !JSValueIsNull (ctx, user_data))
-    {
-      ((SeedClosure *) closure)->user_data = user_data;
-      JSValueProtect(ctx, user_data);
-    }
-
   g_signal_connect_closure (on_obj, signal_name, closure, FALSE);
 }
 



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