seed r128 - in trunk: libseed src



Author: racarr
Date: Wed Nov  5 12:16:43 2008
New Revision: 128
URL: http://svn.gnome.org/viewvc/seed?rev=128&view=rev

Log:
More exceptions and some random cleanup, courtesy of Matt. Fix typo in 
last commit.


Modified:
   trunk/libseed/seed-builtins.c
   trunk/libseed/seed-engine.c
   trunk/libseed/seed-engine.h
   trunk/libseed/seed-signals.c
   trunk/libseed/seed-structs.c
   trunk/libseed/seed-types.c
   trunk/libseed/seed-types.h
   trunk/libseed/seed.h
   trunk/src/main.c

Modified: trunk/libseed/seed-builtins.c
==============================================================================
--- trunk/libseed/seed-builtins.c	(original)
+++ trunk/libseed/seed-builtins.c	Wed Nov  5 12:16:43 2008
@@ -41,17 +41,18 @@
 		    g_strdup_printf("Seed.include expected 1 argument, "
 				    "got %d", argumentCount);
 		seed_make_exception(exception, "ArgumentError", mes);
-		return JSValueMakeNull(eng->context);
+		g_free(mes);
+		return JSValueMakeNull(ctx);
 	}
 	import_file = seed_value_to_string(arguments[0], exception);
 
 	g_file_get_contents(import_file, &buffer, 0, 0);
 
 	if (!buffer) {
-		//gchar * mes = g_strdup_printf("File not found: %s.\n", import_file);
-		//seed_make_exception(exception, "FileNotFound", mes);
-		//g_free(mes);
-		return 0;	// TODO: raise exception?
+		gchar * mes = g_strdup_printf("File not found: %s.\n", import_file);
+		seed_make_exception(exception, "FileNotFound", mes);
+		g_free(mes);
+		return JSValueMakeNull(ctx);
 	}
 
 	walk = buffer;
@@ -72,7 +73,7 @@
 	JSStringRelease(file_contents);
 	JSStringRelease(file_name);
 
-	return 0;
+	return JSValueMakeNull(ctx);
 }
 
 JSValueRef
@@ -82,18 +83,19 @@
 	   size_t argumentCount,
 	   const JSValueRef arguments[], JSValueRef * exception)
 {
-	if (argumentCount < 1) {
+	if (argumentCount != 1) {
 		gchar *mes = g_strdup_printf("Seed.print Expected 1 argument,"
 					     " got %d", argumentCount);
 		seed_make_exception(exception, "ArgumentError", mes);
-		return JSValueMakeNull(eng->context);
+		g_free(mes);
+		return JSValueMakeNull(ctx);
 	}
 
 	gchar *buf = seed_value_to_string(arguments[0], exception);
 	printf("%s\n", buf);
-	free(buf);
+	g_free(buf);
 
-	return JSValueMakeNull(eng->context);
+	return JSValueMakeNull(ctx);
 }
 
 JSValueRef
@@ -116,7 +118,8 @@
 		    g_strdup_printf("Seed.readline Expected 1 argument, "
 				    "got %d", argumentCount);
 		seed_make_exception(exception, "ArgumentError", mes);
-		return JSValueMakeNull(eng->context);
+		g_free(mes);
+		return JSValueMakeNull(ctx);
 	}
 
 	buf = seed_value_to_string(arguments[0], exception);
@@ -125,13 +128,13 @@
 	if (str && *str) {
 		add_history(str);
 		valstr = seed_value_from_string(str, exception);
-		free(str);
+		g_free(str);
 	}
 
-	free(buf);
+	g_free(buf);
 
 	if (valstr == 0)
-		valstr = JSValueMakeNull(eng->context);
+		valstr = JSValueMakeNull(ctx);
 
 	return valstr;
 }
@@ -146,7 +149,14 @@
 	GType type;
 
 	if (argumentCount != 1)
+	{
+		gchar *mes =
+		    g_strdup_printf("Seed.prototype Expected 1 argument, "
+				    "got %d", argumentCount);
+		seed_make_exception(exception, "ArgumentError", mes);
+		g_free(mes);
 		return JSValueMakeNull(eng->context);
+	}
 	if (!JSValueIsObject(eng->context, arguments[0]))
 		return JSValueMakeNull(eng->context);
 
@@ -155,11 +165,11 @@
 	return seed_gobject_get_prototype_for_gtype(type);
 }
 
-const char *seed_g_type_name_to_string(GITypeInfo * type)
+const gchar *seed_g_type_name_to_string(GITypeInfo * type)
 {
 	GITypeTag type_tag = g_type_info_get_tag(type);
 
-	const char *type_name;
+	const gchar *type_name;
 
 	if (type_tag == GI_TYPE_TAG_INTERFACE) {
 		GIBaseInfo *interface = g_type_info_get_interface(type);
@@ -183,19 +193,27 @@
 
 	GICallableInfo *info;
 	JSObjectRef data_obj, args_obj;
-	int i;
+	gint i;
 
 	if (argumentCount != 1)
-		return JSValueMakeNull(eng->context);
-	if (!JSValueIsObject(eng->context, arguments[0]))
-		return JSValueMakeNull(eng->context);
-	if (!JSValueIsObjectOfClass(eng->context, arguments[0],
+	{
+		gchar *mes =
+		    g_strdup_printf("Seed.introspection Expected 1 argument, "
+				    "got %d", argumentCount);
+		seed_make_exception(exception, "ArgumentError", mes);
+		g_free(mes);
+		return JSValueMakeNull(ctx);
+	}
+
+	if (!JSValueIsObject(ctx, arguments[0]))
+		return JSValueMakeNull(ctx);
+	if (!JSValueIsObjectOfClass(ctx, arguments[0],
 				    gobject_method_class))
-		return JSValueMakeNull(eng->context);
+		return JSValueMakeNull(ctx);
 
 	info =
 	    (GICallableInfo *) JSObjectGetPrivate((JSObjectRef) arguments[0]);
-	data_obj = JSObjectMake(eng->context, NULL, NULL);
+	data_obj = JSObjectMake(ctx, NULL, NULL);
 
 	seed_value_set_property(data_obj, "name",
 				(JSValueRef)
@@ -212,7 +230,7 @@
 	seed_value_set_property(data_obj, "args", args_obj);
 
 	for (i = 0; i < g_callable_info_get_n_args(info); ++i) {
-		JSObjectRef argument = JSObjectMake(eng->context, NULL, NULL);
+		JSObjectRef argument = JSObjectMake(ctx, NULL, NULL);
 
 		const gchar *arg_name =
 		    seed_g_type_name_to_string(g_arg_info_get_type
@@ -222,7 +240,7 @@
 		seed_value_set_property(argument, "type",
 								seed_value_from_string(arg_name, exception));
 
-		JSObjectSetPropertyAtIndex(eng->context, args_obj, i, argument,
+		JSObjectSetPropertyAtIndex(ctx, args_obj, i, argument,
 					   NULL);
 	}
 
@@ -237,7 +255,7 @@
 		  const JSValueRef arguments[], JSValueRef * exception)
 {
 	if (argumentCount != 0) {
-		JSStringRef jsstr = JSValueToStringCopy(eng->context,
+		JSStringRef jsstr = JSValueToStringCopy(ctx,
 							arguments[0],
 							exception);
 		JSCheckScriptSyntax(ctx, jsstr, 0, 0, exception);
@@ -245,10 +263,12 @@
 			JSStringRelease(jsstr);
 	} else {
 		gchar *mes = g_strdup_printf("Seed.check_syntax expected"
-					     "1 argument, got %d",
+					     "0 argument, got %d",
 					     argumentCount);
+		seed_make_exception(exception, "ArgumentError", mes);
+		g_free(mes);
 	}
-	return JSValueMakeNull(eng->context);
+	return JSValueMakeNull(ctx);
 }
 
 JSValueRef
@@ -288,12 +308,17 @@
 	//if (!g_main_loop_is_running(loop))
 	//      return JSValueMakeBoolean(ctx, 0);
 
-	// TODO: convert to use an exception! (Matt!)
-
-	if (argumentCount != 2)
+	if (argumentCount != 1)
+	{
+		gchar *mes =
+		    g_strdup_printf("Seed.set_timeout Expected 1 argument, "
+				    "got %d", argumentCount);
+		seed_make_exception(exception, "ArgumentError", mes);
+		g_free(mes);
 		return JSValueMakeBoolean(ctx, 0);
+	}
 
-	JSStringRef jsstr = JSValueToStringCopy(eng->context,
+	JSStringRef jsstr = JSValueToStringCopy(ctx,
 						arguments[0],
 						exception);
 
@@ -370,9 +395,9 @@
 		return JSObjectMake(eng->context, seed_native_callback_class, privates);
 }
 
-void seed_init_builtins(int *argc, char ***argv)
+void seed_init_builtins(gint *argc, gchar ***argv)
 {
-	int i;
+	guint i;
 	JSObjectRef arrayObj;
 	JSValueRef argcref;
 	JSObjectRef obj =
@@ -405,3 +430,4 @@
 	seed_value_set_property(arrayObj, "length", argcref);
 	seed_value_set_property(obj, "argv", arrayObj);
 }
+

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Wed Nov  5 12:16:43 2008
@@ -74,7 +74,7 @@
 	// Do we free GParamSpecs...? It's not clear.
 	GParamSpec *param_spec;
 	gchar *prop_name;
-	int i, nparams = 0, length;
+	gint i, nparams = 0, length;
 	JSObjectRef ret;
 	JSPropertyNameArrayRef jsprops = 0;
 	JSStringRef jsprop_name;
@@ -103,7 +103,7 @@
 		jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);
 
 		length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
-		prop_name = malloc(length * sizeof(gchar));
+		prop_name = g_malloc(length * sizeof(gchar));
 		JSStringGetUTF8CString(jsprop_name, prop_name, length);
 
 		param_spec = g_object_class_find_property(oclass, prop_name);
@@ -449,7 +449,7 @@
 		return 0;
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = malloc(length * sizeof(gchar));
+	cproperty_name = g_malloc(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	spec = g_object_class_find_property(G_OBJECT_GET_CLASS(b),
@@ -494,7 +494,7 @@
 	obj = seed_value_to_object(object, 0);
 
 	length = JSStringGetMaximumUTF8CStringSize(property_name);
-	cproperty_name = malloc(length * sizeof(gchar));
+	cproperty_name = g_malloc(length * sizeof(gchar));
 	JSStringGetUTF8CString(property_name, cproperty_name, length);
 
 	spec = g_object_class_find_property(G_OBJECT_GET_CLASS(obj),
@@ -838,7 +838,7 @@
 	NULL			/* Convert To Type */
 };
 
-void seed_create_function(char *name, gpointer func, JSObjectRef obj)
+void seed_create_function(gchar *name, gpointer func, JSObjectRef obj)
 {
 	JSObjectRef oref;
 
@@ -854,7 +854,7 @@
 	glib_message = g_strdup(message);
 }
 
-gboolean seed_init(int *argc, char ***argv)
+gboolean seed_init(gint *argc, gchar ***argv)
 {
 	JSObjectRef seed_obj_ref;
 	JSStringRef defaults_script;
@@ -866,7 +866,7 @@
 	qname = g_quark_from_static_string("js-type");
 	qprototype = g_quark_from_static_string("js-prototype");
 
-	eng = (SeedEngine *) malloc(sizeof(SeedEngine));
+	eng = (SeedEngine *) g_malloc(sizeof(SeedEngine));
 
 	eng->context = JSGlobalContextCreateInGroup(NULL, NULL);
 	eng->global = JSContextGetGlobalObject(eng->context);
@@ -903,7 +903,7 @@
 }
 
 SeedScript *seed_make_script(const gchar * js, const gchar * source_url,
-			     int line_number)
+			     gint line_number)
 {
 	SeedScript *ret = g_new0(SeedScript, 1);
 

Modified: trunk/libseed/seed-engine.h
==============================================================================
--- trunk/libseed/seed-engine.h	(original)
+++ trunk/libseed/seed-engine.h	Wed Nov  5 12:16:43 2008
@@ -33,7 +33,7 @@
 	JSValueRef exception;
 
 	JSStringRef source_url;
-	int line_number;
+	gint line_number;
 } SeedScript;
 
 JSObjectRef seed_gobject_get_prototype_for_gtype(GType type);
@@ -42,7 +42,7 @@
 void seed_gobject_define_property_from_function_info(GIFunctionInfo * info,
 						     JSObjectRef object,
 						     gboolean instance);
-void seed_create_function(char *name, gpointer func, JSObjectRef obj);
+void seed_create_function(gchar *name, gpointer func, JSObjectRef obj);
 void seed_make_exception(JSValueRef * exception,
 			 const gchar * name, const gchar * message);
 #endif

Modified: trunk/libseed/seed-signals.c
==============================================================================
--- trunk/libseed/seed-signals.c	(original)
+++ trunk/libseed/seed-signals.c	Wed Nov  5 12:16:43 2008
@@ -31,7 +31,7 @@
 {
 	guint k;
 	JSObjectRef signal_ref;
-	signal_privates *priv = malloc(sizeof(signal_privates));
+	signal_privates *priv = g_malloc(sizeof(signal_privates));
 	gchar *my_signal_name = g_strdup(signal->signal_name);
 	gchar *modified_signal_name;
 

Modified: trunk/libseed/seed-structs.c
==============================================================================
--- trunk/libseed/seed-structs.c	(original)
+++ trunk/libseed/seed-structs.c	Wed Nov  5 12:16:43 2008
@@ -76,7 +76,7 @@
 JSObjectRef seed_make_struct(gpointer strukt, GIBaseInfo * info)
 {
 	JSObjectRef object;
-	int i, n_methods;
+	gint i, n_methods;
 
 	if (!seed_struct_class)
 		seed_struct_class = JSClassCreate(&gobject_struct_def);
@@ -97,3 +97,4 @@
 
 	return object;
 }
+

Modified: trunk/libseed/seed-types.c
==============================================================================
--- trunk/libseed/seed-types.c	(original)
+++ trunk/libseed/seed-types.c	Wed Nov  5 12:16:43 2008
@@ -55,7 +55,8 @@
 
 	class = seed_gobject_get_class_for_gtype(type);
 
-	while (!class && (type = g_type_parent(type))) {
+	while (!class && (type = g_type_parent(type)))
+	{
 		class = seed_gobject_get_class_for_gtype(type);
 	}
 
@@ -170,7 +171,8 @@
 {
 	GITypeTag gi_tag = g_type_info_get_tag(type_info);
 
-	if (!value || JSValueIsNull(eng->context, value)) {
+	if (!value || JSValueIsNull(eng->context, value))
+	{
 		arg->v_pointer = 0;
 		return 1;
 	}
@@ -258,18 +260,23 @@
 							      value, NULL);
 				break;
 			} else if (interface_type == GI_INFO_TYPE_STRUCT) {
-				if (JSValueIsObjectOfClass(eng->context, value, gobject_method_class))
-					arg->v_pointer = seed_struct_get_pointer(value);
+				if (JSValueIsObjectOfClass(eng->context, 
+										   value, seed_struct_class))
+						arg->v_pointer = seed_struct_get_pointer(value);
 				else
 				{
-						GType type = g_registered_type_info_get_g_type((GIRegisteredTypeInfo*)interface);
+						GType type = 
+								g_registered_type_info_get_g_type(
+										(GIRegisteredTypeInfo*)interface);
 						if (!type)
 								return FALSE;
 						else if (g_type_is_a(type, G_TYPE_CLOSURE))
 						{
-								if (JSObjectIsFunction(eng->context, (JSObjectRef)value))
+								if (JSObjectIsFunction(eng->context,
+													   (JSObjectRef)value))
 								{
-										arg->v_pointer = seed_make_gclosure((JSObjectRef)value, 0);
+										arg->v_pointer = 
+								  seed_make_gclosure((JSObjectRef)value, 0);
 								}
 						}
 				}
@@ -712,7 +719,7 @@
 	return FALSE;
 }
 
-SeedValue seed_value_get_property(SeedValue val, const char *name)
+SeedValue seed_value_get_property(SeedValue val, const gchar *name)
 {
 
 	JSStringRef jname = JSStringCreateWithUTF8CString(name);
@@ -726,7 +733,7 @@
 }
 
 gboolean seed_value_set_property(JSObjectRef object,
-				 const char *name, JSValueRef value)
+				 const gchar *name, JSValueRef value)
 {
 	JSStringRef jname = JSStringCreateWithUTF8CString(name);
 
@@ -999,7 +1006,7 @@
 		jsstr = JSValueToStringCopy(eng->context, val, NULL);
 		length = JSStringGetMaximumUTF8CStringSize(jsstr);
 
-		buf = malloc(length * sizeof(gchar));
+		buf = g_malloc(length * sizeof(gchar));
 		JSStringGetUTF8CString(jsstr, buf, length);
 		JSStringRelease(jsstr);
 	}

Modified: trunk/libseed/seed-types.h
==============================================================================
--- trunk/libseed/seed-types.h	(original)
+++ trunk/libseed/seed-types.h	Wed Nov  5 12:16:43 2008
@@ -23,10 +23,10 @@
 #include "seed-private.h"
 
 SeedValue seed_value_from_gvalue(GValue * gval, JSValueRef * exception);
-SeedValue seed_value_get_property(SeedValue val, const char *name);
+SeedValue seed_value_get_property(SeedValue val, const gchar *name);
 
 gboolean seed_value_set_property(JSObjectRef object,
-				 const char *name, JSValueRef value);
+				 const gchar *name, JSValueRef value);
 gboolean seed_gvalue_from_seed_value(SeedValue val, GType type, GValue * gval,
 									 JSValueRef * exception);
 gboolean seed_gi_supports_type(GITypeInfo * type_info);

Modified: trunk/libseed/seed.h
==============================================================================
--- trunk/libseed/seed.h	(original)
+++ trunk/libseed/seed.h	Wed Nov  5 12:16:43 2008
@@ -29,10 +29,10 @@
 typedef struct _SeedScript SeedScript;
 
 /* seed-engine.c */
-gboolean seed_init(int *argc, char ***argv);
+gboolean seed_init(gint *argc, gchar ***argv);
 
 SeedScript *seed_make_script(const gchar * s, const gchar * source_url,
-			     int line_number);
+			     gint line_number);
 SeedException seed_script_exception(SeedScript * s);
 SeedException seed_make_exception(gchar * name, gchar * message);
 gchar *seed_exception_get_name(SeedException e);

Modified: trunk/src/main.c
==============================================================================
--- trunk/src/main.c	(original)
+++ trunk/src/main.c	Wed Nov  5 12:16:43 2008
@@ -38,7 +38,7 @@
 	g_free(script);
 }
 
-void seed_exec(int argc, char ** argv)
+void seed_exec(gint argc, gchar ** argv)
 {
 	SeedScript  * script;
 	SeedException e;
@@ -74,7 +74,7 @@
 	g_free(script);
 }
 
-int main(int argc, char ** argv)
+int main(gint argc, gchar ** argv)
 {	
 	g_set_prgname("seed");
 	g_thread_init(0);
@@ -91,3 +91,4 @@
 	
 	return 0;
 }
+



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