seed r38 - in trunk: . doc examples/lightsoff libseed



Author: racarr
Date: Sun Oct 26 05:29:27 2008
New Revision: 38
URL: http://svn.gnome.org/viewvc/seed?rev=38&view=rev

Log:
Tutorial updates. Repl in main. Seed.argv. Add menu to lightsoff.


Modified:
   trunk/doc/tutorial.html
   trunk/examples/lightsoff/lightsoff.js
   trunk/libseed/seed-builtins.c
   trunk/libseed/seed-engine.c
   trunk/libseed/seed-types.c
   trunk/main.c

Modified: trunk/doc/tutorial.html
==============================================================================
--- trunk/doc/tutorial.html	(original)
+++ trunk/doc/tutorial.html	Sun Oct 26 05:29:27 2008
@@ -155,7 +155,6 @@
 #!/usr/local/bin/seed
 
 Seed.import_namespace("Gtk");
-Seed.import_namespace("WebKit");
 Gtk.init(null, null);
 
 var window = new Gtk.Window();

Modified: trunk/examples/lightsoff/lightsoff.js
==============================================================================
--- trunk/examples/lightsoff/lightsoff.js	(original)
+++ trunk/examples/lightsoff/lightsoff.js	Sun Oct 26 05:29:27 2008
@@ -7,6 +7,48 @@
 /* SxS size*/
 var size = 5;
 var wincount = 0;
+var moves = 0;
+
+function create_board_size_menu()
+{
+    var menu = new Gtk.Menu();
+    
+    var size_5 = new Gtk.MenuItem({"child": new Gtk.Label({"label": "5x5"})});
+    var size_7 = new Gtk.MenuItem({"child": new Gtk.Label({"label": "7x7"})});
+    var size_9 = new Gtk.MenuItem({"child": new Gtk.Label({"label": "9x9"})});
+    
+    menu.append(size_5);
+    menu.append(size_7);
+    menu.append(size_9);
+    
+    return menu;
+}
+
+function create_menu()
+{
+    var menu = new Gtk.MenuBar();
+    
+    var game_menu = new Gtk.Menu();
+    var size_item = new Gtk.MenuItem({"child":
+                                      new Gtk.Label({"label": "Board Size"})});
+    size_item.signal_activate.connect(Gtk.main_quit);
+    //size_item.submenu = create_board_size_menu(); // crashy?!
+    
+    var quit_item = new Gtk.MenuItem({"child":
+                                      new Gtk.Label({"label": "Quit"})});
+    quit_item.signal_activate.connect(Gtk.main_quit);
+    
+    game_menu.append(size_item);
+    game_menu.append(quit_item);
+    
+    var game_menu_item = new Gtk.MenuItem({"child":
+                                           new Gtk.Label({"label": "Game"})});
+    game_menu_item.submenu = game_menu;
+    
+    menu.append(game_menu_item);
+    
+    return menu;
+}
 
 function create_board()
 {
@@ -53,9 +95,11 @@
 function initialize_game()
 {
 	wincount = 0;
-    
+	  
 	clear_board();
 	random_clicks(); // generate random puzzle
+	
+	moves = 0;
 }
 
 function do_click(x , y)
@@ -70,6 +114,8 @@
 		flip_color(x, y - 1);
 
 	flip_color(x,y);
+	
+	++moves;
 }
 
 function button_clicked( button )
@@ -78,7 +124,7 @@
 
 	if ( wincount == 0 )
 	{
-		Seed.print("GLORIOUS VICTORY");
+		Seed.print("GLORIOUS VICTORY in " + moves + " moves!");
 		initialize_game();
 	}
 }
@@ -137,7 +183,14 @@
 
 var window = new Gtk.Window({"title": "Lights Off", "resizable" : false});
 window.signal_hide.connect(Gtk.main_quit);
-window.add(create_board());
+
+vbox = new Gtk.VBox();
+
+vbox.pack_start(create_menu());
+vbox.pack_start(create_board());
+
+window.add(vbox);
+
 window.show_all();
 initialize_game();
 

Modified: trunk/libseed/seed-builtins.c
==============================================================================
--- trunk/libseed/seed-builtins.c	(original)
+++ trunk/libseed/seed-builtins.c	Sun Oct 26 05:29:27 2008
@@ -76,12 +76,14 @@
 			  const JSValueRef arguments[],
 			  JSValueRef * exception)
 {
-	// TODO: careful!
+	if(argumentCount < 1)
+		return JSValueMakeNull(eng->context);
+	
 	gchar * buf = seed_value_to_string(arguments[0]);
 	printf("%s\n", buf);
 	free(buf);
 	
-	return 0;
+	return JSValueMakeNull(eng->context);
 }
 
 JSValueRef
@@ -116,12 +118,26 @@
 	return valstr;
 }
 
-void seed_init_builtins()
+void seed_init_builtins(int * argc, char *** argv)
 {
+	int i;
+	JSObjectRef arrayObj;
 	JSObjectRef obj = (JSObjectRef)seed_value_get_property(eng->global, "Seed");
 	
 	seed_create_function("include", &seed_include, obj);
 	seed_create_function("print", &seed_print, obj);
 	seed_create_function("readline", &seed_readline, obj);
+	
+	arrayObj = JSObjectMake(eng->context, NULL, NULL);
+	
+	for(i = 0; i < *argc; ++i)
+	{
+		// TODO: exceptions!
+		
+		JSObjectSetPropertyAtIndex(eng->context, arrayObj, i,
+								   seed_value_from_string((*argv)[i]), NULL);
+	}
+	
+	seed_value_set_property(obj, "argv", arrayObj);
 }
 

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Sun Oct 26 05:29:27 2008
@@ -810,7 +810,7 @@
 	JSValueProtect(eng->context, seed_obj_ref);
 
 	seed_create_function("import_namespace", &seed_gi_import_namespace, seed_obj_ref);
-	seed_init_builtins();
+	seed_init_builtins(argc, argv);
 	
 	return TRUE;
 

Modified: trunk/libseed/seed-types.c
==============================================================================
--- trunk/libseed/seed-types.c	(original)
+++ trunk/libseed/seed-types.c	Sun Oct 26 05:29:27 2008
@@ -696,6 +696,7 @@
 static void seed_value_wrong_type()
 {
 	printf("Wrong type in type conversion!\n");
+	abort();
 }
 
 gboolean	seed_value_to_boolean(JSValueRef val)
@@ -964,3 +965,4 @@
 	else
 		return seed_wrap_object(val);
 }
+

Modified: trunk/main.c
==============================================================================
--- trunk/main.c	(original)
+++ trunk/main.c	Sun Oct 26 05:29:27 2008
@@ -24,13 +24,24 @@
 #include "readline/readline.h"
 #include <stdlib.h>
 
+void seed_repl(int argc, char ** argv)
+{
+	SeedScript  * script;
+
+	script = seed_make_script("while(1) { try { Seed.print(eval("
+							  "Seed.readline(\"> \"))); } catch(e) {"
+							  "Seed.print(e.name + \" \" + e.message);}}",
+							  NULL, 0);
+	seed_evaluate(script, 0);
+	
+	g_free(script);
+}
+
 void seed_exec(int argc, char ** argv)
 {
 	SeedScript  * script;
 	SeedException e;
 	gchar * buffer;
-	
-	g_assert((argc==2));
 
 	g_file_get_contents(argv[1], 
 	&buffer, 0, 0);
@@ -62,17 +73,18 @@
 
 	// Apparently our name for glib logging gets set in g*_init. can we set
 	// that ourselves so that when we do on-the-fly init, we don't lose that?
+	
 	gst_init(&argc, &argv);	
 	seed_init(&argc, &argv);
 
 	if (!g_irepository_require(g_irepository_get_default(), 
 				   "GObject", 0, 0))
 		g_critical("Unable to import GObject repository");
-	
-	if(argc == 2)
-		seed_exec(argc, argv);
+		
+	if(argc == 1)
+		seed_repl(argc, argv);
 	else
-		printf("Usage: %s file.js\n", argv[0]);
+		seed_exec(argc, argv);
 	
 	return 0;
 }



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