gscript r7 - trunk



Author: alexl
Date: Tue Sep 16 11:20:21 2008
New Revision: 7
URL: http://svn.gnome.org/viewvc/gscript?rev=7&view=rev

Log:
2008-09-16  Alexander Larsson  <alexl redhat com>

        * js-gtk.c:
	Add add_idle function.
	Load and eval gtk.js at start

        * gtk.js:
	Added new js test file.
	Contains some experiments with how to bind async i/o nicely



Added:
   trunk/gtk.js
Modified:
   trunk/ChangeLog
   trunk/js-gtk.c

Added: trunk/gtk.js
==============================================================================
--- (empty file)
+++ trunk/gtk.js	Tue Sep 16 11:20:21 2008
@@ -0,0 +1,108 @@
+/* Simulate GIO-style async operation using idles */
+
+function open_async(filename, flags, cb) {
+    function on_idle() {
+	var res = new Object();
+	if (filename == "test.txt")
+	    res.data = "Test data";
+	else if (filename == "test2.txt")
+	    res.data = "Test2 data";
+	else
+	    res.data = "other data";
+	cb (filename, res);
+	
+    }
+    add_idle (on_idle);
+}
+
+function open_finish(filename, res) {
+    return res.data;
+}
+
+/* Wrap the GIO-style async op for the yield format */
+
+function open_yield (filename, flags) {
+    let f = filename;
+    let fl = flags;
+    let curried = function(cb) {
+	open_async (f, fl, cb);
+    }
+    curried.finish = function(res) {
+	return open_finish (f, res);
+    }
+    /* Basically we return a curried version of open_async with everything but the
+     * callback argument bound. Plus we pass the (curried) paired finish method as a
+     * property of the return value function.
+     */
+    return curried;
+}
+
+/* This function lets us run a generator function as an async operation,
+   handling all the callbacks from the yields. You can also pass in
+   a callback to run when the operation is done and a list of arguments
+   for the generator */
+
+function do_async (fun, cb /* , args ... */) {
+    function async_cb (obj, res) {
+	let ret;
+	try {
+	    ret = f.finish (res);
+	    f = gen.send(ret);
+	} catch (e if e instanceof StopIteration) {
+	    f = null;
+	} catch (e) {
+	    f = gen.throw(e);
+	}
+	if (f)
+	    f (async_cb);
+	else if (cb)
+	    cb ();
+    }
+    let extra_args = [];
+    for (let i = 2; i < arguments.length; i++)
+	extra_args[i-2] = arguments[i];
+    let gen = fun.apply (this, extra_args);
+    let f = gen.next();
+    f (async_cb);
+}
+
+/* Lets you call other async generators from an async generator */
+
+function call_async (fun) {
+    var args = [fun, null];
+    for (let i = 1; i < arguments.length; i++)
+	args[i+1] = arguments[i];
+    let f = function (cb) {
+	function wrap_cb () {
+	    cb (null, null);
+	}
+	args[1] = wrap_cb;
+	do_async.apply (this, args);
+    }
+    f.finish = function (res) {
+	return undefined;
+    }
+    return f;
+}
+
+/* Test this stuff */
+
+function do_stuff2 () {
+    print ("do_stuff2");
+    var data3 = yield open_yield ("other", 0);
+    print ("data3 = " + data3);
+    print ("end of do_stuff2");
+}
+
+function do_stuff (filename1, filename2) {
+    print ("do_stuff");
+    var data = yield open_yield (filename1, 0);
+    print ("data = " + data);
+    var data2 = yield open_yield (filename2, 0);
+    print ("data2 = " + data2);
+    yield call_async (do_stuff2);
+    print ("end of do_stuff");
+}
+
+do_async (do_stuff, null, "test.txt", "test2.txt");
+

Modified: trunk/js-gtk.c
==============================================================================
--- trunk/js-gtk.c	(original)
+++ trunk/js-gtk.c	Tue Sep 16 11:20:21 2008
@@ -97,6 +97,29 @@
   return NULL;
 }
 
+static gboolean
+idle_cb (gpointer data)
+{
+  GScriptValue *arg = G_SCRIPT_VALUE (data);
+
+  g_script_engine_call (engine,
+			NULL,
+			arg, 0, NULL);
+  
+  g_object_unref (arg);
+  return FALSE;
+}
+
+static GScriptValue *
+add_idle (int n_args, GScriptValue **args)
+{
+  guint tag;
+
+  tag = g_idle_add (idle_cb, g_object_ref (args[0]));
+
+  return g_script_value_new_from_uint32 (tag);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -153,20 +176,27 @@
 			       f);
   g_object_unref (f);
   
-  if (1)
+  f = g_script_value_new_from_function (add_idle, 1);
+  g_script_value_set_property (global,
+			       "add_idle",
+			       f);
+  g_object_unref (f);
+  
+  if (0)
     {
-      print ("Running script");
+      print ("Running test script");
       print ("---------------------");
       
       
       res = g_script_engine_evaluate_script (engine,
-					     "print (label.move_cursor);"
-					     "print (label.move_cursor.connect);"
-					     "label.move_cursor.connect( function (x, y, z) {print (this);print(x);print(y);print(z)});"
+					     "print (label.on_move_cursor);"
+					     "print (label.on_move_cursor.connect);"
+					     "label.on_move_cursor.connect( function (x, y, z) {print (this);print(x);print(y);print(z)});"
 					     "print (label.label);"
 					     "print (label.style);"
 					     "print (label.wrap);"
 					     "print (label.nonexist);"
+					     "add_idle (function () { print ('in idle'); });"
 					     );
       
       print ("---------------------");
@@ -176,6 +206,25 @@
       g_signal_emit_by_name (label, "move_cursor", 0, 0, FALSE);
     }
 
+  {
+    gchar *contents;
+    if (g_file_get_contents ("gtk.js",
+			     &contents, NULL, NULL))
+      {
+	print ("Running gtk.js");
+	print ("---------------------");
+      
+      
+	res = g_script_engine_evaluate_script (engine, contents);
+      
+      print ("---------------------");
+      print ("script result:");
+      print (g_script_value_to_string (res));
+      print ("---------------------");
+    }
+  }
+  
+  
   gtk_main ();
   
   return 0;



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