[seed] Implement the ability to pass Seed.argv to init methods



commit 73c585297248c8254bce3c0417228f788960a166
Author: Robert Carr <racarr svn gnome org>
Date:   Mon May 11 20:07:41 2009 -0400

    Implement the ability to pass Seed.argv to init methods
---
 examples/clutter-0.9.js  |    2 +-
 examples/clutter-cogl.js |    2 +-
 examples/repl.js         |    5 ++-
 libseed/seed-builtins.c  |    4 --
 libseed/seed-builtins.h  |    7 ++++
 libseed/seed-engine.c    |   73 ++++++++++++++++++++++++++++++++++++++++++++++
 libseed/seed-engine.h    |    1 +
 libseed/seed-importer.c  |   14 +++++++--
 libseed/seed-types.c     |    1 +
 9 files changed, 98 insertions(+), 11 deletions(-)

diff --git a/examples/clutter-0.9.js b/examples/clutter-0.9.js
index c7dc8d3..8101d75 100755
--- a/examples/clutter-0.9.js
+++ b/examples/clutter-0.9.js
@@ -5,7 +5,7 @@ imports.gi.versions.Clutter = "0.9";
 Clutter = imports.gi.Clutter;
 GObject = imports.gi.GObject;
 
-Clutter.init(null, null);
+Clutter.init(Seed.argv);
 
 colors = [	"blanched almond", 
 		"OldLace", 
diff --git a/examples/clutter-cogl.js b/examples/clutter-cogl.js
index b8cf5c5..dd13da8 100755
--- a/examples/clutter-cogl.js
+++ b/examples/clutter-cogl.js
@@ -40,7 +40,7 @@ function alpha_func(alpha){
 					(time-=(2.625/2.75))*time+0.984375);
 }
 
-Clutter.init(null, null);
+Clutter.init(Seed.argv);
 
 var template = new Clutter.EffectTemplate.for_duration(RIPPLE_S, alpha_func);
 
diff --git a/examples/repl.js b/examples/repl.js
index 5108800..0d7bb31 100755
--- a/examples/repl.js
+++ b/examples/repl.js
@@ -32,13 +32,14 @@ readline.bind('\t', function(){
     readline.insert("\t");
 });
 
-var re = /[^=<>*-^/]=[^=<>*-^/]\(*\s*(new\s*)?[^:punct:]|'|"+\)*$/
+//var re = /[^=<>*-^/]=[^=<>*-^/]\(*\s*(new\s*)?[^:punct:]|'|"+\)*$/
 
 while(1){
     try{
 	item = readline.readline("> ");
 	result = context.eval(item);
-	if (!re.exec(item) && (result != undefined))
+//	if (!re.exec(item) && (result != undefined))
+	if (result != undefined)
 	    Seed.print(result)
 
     }
diff --git a/libseed/seed-builtins.c b/libseed/seed-builtins.c
index 4f23d2d..9dc5a17 100644
--- a/libseed/seed-builtins.c
+++ b/libseed/seed-builtins.c
@@ -421,10 +421,6 @@ seed_breakpoint (JSContextRef ctx,
   return JSValueMakeNull(ctx);
 }
 
-typedef struct _SeedArgvPrivates {
-  gchar ** argv;
-  gint argc;
-} SeedArgvPrivates;
 
 static JSValueRef
 seed_argv_get_property (JSContextRef ctx,
diff --git a/libseed/seed-builtins.h b/libseed/seed-builtins.h
index 43c95a8..247bd53 100644
--- a/libseed/seed-builtins.h
+++ b/libseed/seed-builtins.h
@@ -26,4 +26,11 @@
 
 void seed_init_builtins (SeedEngine * local_eng, gint * argc, gchar *** argv);
 
+extern JSClassRef seed_argv_class;
+typedef struct _SeedArgvPrivates {
+  gchar ** argv;
+  gint argc;
+} SeedArgvPrivates;
+
+
 #endif
diff --git a/libseed/seed-engine.c b/libseed/seed-engine.c
index 871dd40..59d6a2f 100644
--- a/libseed/seed-engine.c
+++ b/libseed/seed-engine.c
@@ -267,6 +267,55 @@ seed_gobject_method_finalize (JSObjectRef method)
     g_base_info_unref (info);
 }
 
+typedef void (*InitMethodCallback) (gint *argc, gchar ***argv);
+
+static JSValueRef
+seed_gobject_init_method_invoked (JSContextRef ctx,
+				  JSObjectRef function,
+				  JSObjectRef this_object,
+				  size_t argumentCount,
+				  const JSValueRef arguments[],
+				  JSValueRef * exception)
+{
+  GIBaseInfo *info;
+  GTypelib *typelib;
+  InitMethodCallback c;
+  SeedArgvPrivates *priv;
+  if (argumentCount != 1 && argumentCount != 2)
+    {
+      seed_make_exception (ctx, exception, 
+			   "ArgumentError", "init method expects 1 argument, got %d", 
+			   argumentCount);
+      return JSValueMakeUndefined (ctx);
+    }
+
+  // TODO: In future support all arrays.Through a seperate code path.
+  if (!argumentCount == 2)
+    {
+      if (JSValueIsNull (ctx, arguments[0]) || !JSValueIsObject (ctx, arguments[0]) ||
+	  !JSValueIsObjectOfClass (ctx, arguments[0], seed_argv_class))
+	{
+	  seed_make_exception (ctx, exception,
+			       "ArgumentError", "init method expects an argv object as argument");
+	}
+    }
+  
+  info = JSObjectGetPrivate (function);
+  typelib = g_base_info_get_typelib (info);
+  g_typelib_symbol (typelib, g_function_info_get_symbol ((GIFunctionInfo *)info), (gpointer *)&c);
+  // Backwards compatibility
+  if (argumentCount == 2)
+    {
+      c(NULL, NULL);
+      return JSValueMakeUndefined (ctx);
+    }
+
+  priv = JSObjectGetPrivate ((JSObjectRef)arguments[0]);
+  c(&priv->argc, &priv->argv);
+    
+  return JSValueMakeUndefined (ctx);
+}
+
 static JSValueRef
 seed_gobject_method_invoked (JSContextRef ctx,
 			     JSObjectRef function,
@@ -973,6 +1022,26 @@ JSClassDefinition gobject_method_def = {
   NULL				/* Convert To Type */
 };
 
+JSClassDefinition gobject_init_method_def = {
+  0,				/* Version, always 0 */
+  0,
+  "init_method",		/* Class Name */
+  NULL,				/* Parent Class */
+  NULL,				/* Static Values */
+  NULL,				/* Static Functions */
+  NULL,
+  seed_gobject_method_finalize,	/* Finalize */
+  NULL,				/* Has Property */
+  NULL,				/* Get Property */
+  NULL,				/* Set Property */
+  NULL,				/* Delete Property */
+  NULL,				/* Get Property Names */
+  seed_gobject_init_method_invoked,	/* Call As Function */
+  NULL,				/* Call As Constructor */
+  NULL,				/* Has Instance */
+  NULL				/* Convert To Type */
+};
+
 JSClassDefinition seed_callback_def = {
   0,				/* Version, always 0 */
   0,
@@ -1244,6 +1313,8 @@ seed_init (gint * argc, gchar *** argv)
   JSClassRetain (seed_callback_class);
   seed_struct_constructor_class = JSClassCreate (&struct_constructor_def);
   JSClassRetain (seed_struct_constructor_class);
+  gobject_init_method_class = JSClassCreate (&gobject_init_method_def);
+  JSClassRetain (gobject_init_method_class);
 
   g_type_set_qdata (G_TYPE_OBJECT, qname, gobject_class);
 
@@ -1327,6 +1398,8 @@ seed_init_with_context_group (gint * argc,
   JSClassRetain (seed_callback_class);
   seed_struct_constructor_class = JSClassCreate (&struct_constructor_def);
   JSClassRetain (seed_struct_constructor_class);
+  gobject_init_method_class = JSClassCreate (&gobject_init_method_def);
+  JSClassRetain (gobject_init_method_class);
 
   g_type_set_qdata (G_TYPE_OBJECT, qname, gobject_class);
 
diff --git a/libseed/seed-engine.h b/libseed/seed-engine.h
index a9417dc..03f1b70 100644
--- a/libseed/seed-engine.h
+++ b/libseed/seed-engine.h
@@ -29,6 +29,7 @@ extern JSClassRef gobject_method_class;
 extern JSClassRef gobject_constructor_class;
 extern JSClassRef gobject_named_constructor_class;
 extern JSClassRef seed_struct_constructor_class; 
+extern JSClassRef gobject_init_method_class;
 
 extern JSClassRef seed_callback_class;
 extern SeedEngine *eng;
diff --git a/libseed/seed-importer.c b/libseed/seed-importer.c
index e1b64c0..ae379cf 100644
--- a/libseed/seed-importer.c
+++ b/libseed/seed-importer.c
@@ -62,9 +62,17 @@ seed_gi_importer_handle_function (JSContextRef ctx,
 				  GIFunctionInfo *info,
 				  JSValueRef *exception)
 {
-  seed_gobject_define_property_from_function_info (ctx, (GIFunctionInfo *) info,
-						   namespace_ref, FALSE);
-  g_base_info_ref ((GIBaseInfo *) info);
+  if (strcmp(g_base_info_get_name ((GIBaseInfo *)info), "init"))
+    seed_gobject_define_property_from_function_info (ctx, (GIFunctionInfo *) info,
+						     namespace_ref, FALSE);
+  else
+    {
+      JSObjectRef init_method;
+      
+      init_method = JSObjectMake(ctx, gobject_init_method_class, 
+				 g_base_info_ref ((GIBaseInfo *)info));
+      seed_object_set_property (ctx, namespace_ref, "init", init_method);
+    }
 }
 
 /*
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index bff09e6..077e665 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -23,6 +23,7 @@ JSClassRef gobject_class;
 JSClassRef gobject_method_class;
 JSClassRef gobject_constructor_class;
 JSClassRef seed_callback_class;
+JSClassRef gobject_init_method_class;
 SeedEngine *eng;
 
 static gboolean



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