seed r329 - trunk/libseed



Author: racarr
Date: Mon Nov 24 07:10:43 2008
New Revision: 329
URL: http://svn.gnome.org/viewvc/seed?rev=329&view=rev

Log:
Factor out struct/union construction/JSON->field conversion in to seed_construct_type_with_parameters


Modified:
   trunk/libseed/seed-engine.c
   trunk/libseed/seed-structs.c
   trunk/libseed/seed-structs.h

Modified: trunk/libseed/seed-engine.c
==============================================================================
--- trunk/libseed/seed-engine.c	(original)
+++ trunk/libseed/seed-engine.c	Mon Nov 24 07:10:43 2008
@@ -56,35 +56,8 @@
 								 JSValueRef * exception)
 {
 	GIBaseInfo * info = JSObjectGetPrivate(constructor);
-	gsize size = 0;
-	gpointer object;
-	GIInfoType type = g_base_info_get_type(info);
-	JSObjectRef ret;
-	gint nparams, i, length;
-	GIFieldInfo * field = 0;
-	JSPropertyNameArrayRef jsprops;
-	JSStringRef jsprop_name;
-	JSValueRef jsprop_value;
-	GArgument field_value;
-	gchar * prop_name;
-	
-	if (type == GI_INFO_TYPE_STRUCT)
-	{
-		size = g_struct_info_get_size((GIStructInfo *)info);
-	}
-	else
-	{
-		size = g_union_info_get_size((GIUnionInfo *) info);
-	}
-	g_assert(size);
-	object = g_slice_alloc(size);
-	
-	if (type == GI_INFO_TYPE_STRUCT)
-		ret = seed_make_struct(object, info);
-	else
-		ret = seed_make_union(object, info);
-
-	seed_pointer_set_free(ret, TRUE);
+	JSValueRef ret;
+	JSObjectRef parameters = 0;
 
 	if (argumentCount == 1)
 	{
@@ -92,56 +65,14 @@
 		{
 			seed_make_exception(exception, "ArgmuentError",
 								"Constructor expects object as argument");
-			return (JSObjectRef) JSValueMakeNull(ctx);			
-		}
-		
-		jsprops = JSObjectCopyPropertyNames(ctx,
-											(JSObjectRef)arguments[0]);
-		nparams = JSPropertyNameArrayGetCount(jsprops);
-
-		while (i < nparams)
-		{
-			GITypeInfo * field_type;
-			jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);
-			
-			length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
-			prop_name = g_malloc(length * sizeof(gchar));
-			JSStringGetUTF8CString(jsprop_name, prop_name, length);
-			
-			if (type == GI_INFO_TYPE_STRUCT)
-				field = seed_struct_find_field((GIStructInfo *) info,
-											   prop_name);
-			else
-				field = seed_union_find_field((GIUnionInfo *) info,
-											   prop_name);
-			if (!field)
-			{
-				gchar *mes =
-				g_strdup_printf("Invalid property for construction: %s",
-								prop_name);
-				seed_make_exception(exception, "PropertyError", mes);
-				
-				g_free(mes);
-
-				return (JSObjectRef) JSValueMakeNull(ctx);
-			}
-			field_type = g_field_info_get_type(field);
-			
-			jsprop_value = JSObjectGetProperty(ctx,
-											   (JSObjectRef)arguments[0],
-											   jsprop_name, NULL);
-			
-			seed_gi_make_argument(jsprop_value, field_type, &field_value, exception);
-			g_field_info_set_field(field, object, &field_value);
-			
-			g_base_info_unref((GIBaseInfo *) field_type);
-			g_base_info_unref((GIBaseInfo *) field);
-			
-			i++;
+			return (JSObjectRef) JSValueMakeNull(ctx);		
 		}
+		parameters = (JSObjectRef)arguments[0];
 	}
-
-	return ret;
+	ret = seed_construct_struct_type_with_parameters(info,
+													 parameters,
+													 exception);
+		
 }
 
 static JSObjectRef

Modified: trunk/libseed/seed-structs.c
==============================================================================
--- trunk/libseed/seed-structs.c	(original)
+++ trunk/libseed/seed-structs.c	Mon Nov 24 07:10:43 2008
@@ -476,3 +476,91 @@
 	seed_boxed_def.parentClass = seed_struct_class;
 	seed_boxed_class = JSClassCreate(&seed_boxed_def);
 }
+
+JSObjectRef 
+seed_construct_struct_type_with_parameters(GIBaseInfo * info,
+										   JSObjectRef parameters,
+										   JSValueRef * exception)
+{
+	gsize size = 0;
+	gpointer object;
+	GIInfoType type = g_base_info_get_type(info);
+	JSObjectRef ret;
+	gint nparams, i, length;
+	GIFieldInfo * field = 0;
+	JSPropertyNameArrayRef jsprops;
+	JSStringRef jsprop_name;
+	JSValueRef jsprop_value;
+	GArgument field_value;
+	gchar * prop_name;
+
+	if (type == GI_INFO_TYPE_STRUCT)
+	{
+		size = g_struct_info_get_size((GIStructInfo *)info);
+	}
+	else
+	{
+		size = g_union_info_get_size((GIUnionInfo *) info);
+	}
+	g_assert(size);
+	object = g_slice_alloc(size);
+	
+	if (type == GI_INFO_TYPE_STRUCT)
+		ret = seed_make_struct(object, info);
+	else
+		ret = seed_make_union(object, info);
+
+	seed_pointer_set_free(ret, TRUE);
+	
+	if (!parameters)
+		return ret;
+
+	jsprops = JSObjectCopyPropertyNames(eng->context,
+										(JSObjectRef)parameters);
+	nparams = JSPropertyNameArrayGetCount(jsprops);
+	
+	while (i < nparams)
+	{
+		GITypeInfo * field_type;
+		jsprop_name = JSPropertyNameArrayGetNameAtIndex(jsprops, i);
+		
+		length = JSStringGetMaximumUTF8CStringSize(jsprop_name);
+		prop_name = g_malloc(length * sizeof(gchar));
+		JSStringGetUTF8CString(jsprop_name, prop_name, length);
+		
+		if (type == GI_INFO_TYPE_STRUCT)
+			field = seed_struct_find_field((GIStructInfo *) info,
+										   prop_name);
+		else
+			field = seed_union_find_field((GIUnionInfo *) info,
+										  prop_name);
+		if (!field)
+		{
+			gchar *mes =
+				g_strdup_printf("Invalid property for construction: %s",
+								prop_name);
+			seed_make_exception(exception, "PropertyError", mes);
+			
+			g_free(mes);
+			
+			return (JSObjectRef) JSValueMakeNull(eng->context);
+		}
+		field_type = g_field_info_get_type(field);
+		
+		jsprop_value = JSObjectGetProperty(eng->context,
+										   (JSObjectRef)parameters,
+										   jsprop_name, NULL);
+		
+		seed_gi_make_argument(jsprop_value, field_type,
+							  &field_value, exception);
+		g_field_info_set_field(field, object, &field_value);
+		
+		g_base_info_unref((GIBaseInfo *) field_type);
+		g_base_info_unref((GIBaseInfo *) field);
+		
+		i++;
+	}
+
+
+return ret;
+}

Modified: trunk/libseed/seed-structs.h
==============================================================================
--- trunk/libseed/seed-structs.h	(original)
+++ trunk/libseed/seed-structs.h	Mon Nov 24 07:10:43 2008
@@ -42,6 +42,12 @@
 GIFieldInfo *seed_union_find_field(GIUnionInfo * info,
 								   gchar * field_name);
 
+JSObjectRef 
+seed_construct_struct_type_with_parameters(GIBaseInfo * info,
+										   JSObjectRef parameters,
+										   JSValueRef * exception);
+
+
 void seed_structs_init();
 
 #endif



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