gjs r119 - in trunk: gi test/js



Author: otaylor
Date: Wed Nov 19 22:31:17 2008
New Revision: 119
URL: http://svn.gnome.org/viewvc/gjs?rev=119&view=rev

Log:
Allow enum and flag structure fields

With gobject-introspection fixed to read and write enum and flags fields
(bug 561296), we can allow them as part of simple value structures.

gi/boxed.c: Classify enum and flags fields as simple and when reading and
 writing them, go through the normal g_field_info_set/get_field code path,
 rather than the "nested_interface" code path.

test/js/testEverythingEncapsulated.js: Test enum fields in structures and
 boxed types.

https://bugzilla.gnome.org/show_bug.cgi?id=560808

Modified:
   trunk/gi/boxed.c
   trunk/test/js/testEverythingEncapsulated.js

Modified: trunk/gi/boxed.c
==============================================================================
--- trunk/gi/boxed.c	(original)
+++ trunk/gi/boxed.c	Wed Nov 19 22:31:17 2008
@@ -607,28 +607,25 @@
                              Boxed       *parent_priv,
                              GIFieldInfo *field_info,
                              GITypeInfo  *type_info,
+                             GIBaseInfo  *interface_info,
                              jsval       *value)
 {
     JSObject *obj;
     JSObject *proto;
-    GIBaseInfo *info;
     int offset;
-    gboolean success = FALSE;
 
-    info = g_type_info_get_interface (type_info);
-    if (!(g_base_info_get_type (info) == GI_INFO_TYPE_STRUCT || g_base_info_get_type (info) == GI_INFO_TYPE_BOXED) ||
-        !struct_is_simple ((GIStructInfo *)info)) {
+    if (!struct_is_simple ((GIStructInfo *)interface_info)) {
         gjs_throw(context, "Reading field %s.%s is not supported",
                   g_base_info_get_name ((GIBaseInfo *)parent_priv->info),
                   g_base_info_get_name ((GIBaseInfo *)field_info));
 
-        goto out;
+        return JS_FALSE;
     }
 
-    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) info);
+    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) interface_info);
 
     offset = g_field_info_get_offset (field_info);
-    unthreadsafe_template_for_constructor.info = (GIBoxedInfo*) info;
+    unthreadsafe_template_for_constructor.info = (GIBoxedInfo*) interface_info;
     unthreadsafe_template_for_constructor.gboxed = ((char *)parent_priv->gboxed) + offset;
 
     /* Rooting the object here is a little paranoid; the JSObject has to be kept
@@ -643,14 +640,11 @@
     JS_RemoveRoot(context, &unthreadsafe_template_for_constructor.parent_jsval);
 
     if (obj != NULL) {
-        success = TRUE;
         *value = OBJECT_TO_JSVAL(obj);
+        return JS_TRUE;
+    } else {
+        return JS_FALSE;
     }
-
-out:
-    g_base_info_unref( (GIBaseInfo*) info);
-
-    return success;
 }
 
 static JSBool
@@ -685,24 +679,35 @@
     if (!g_type_info_is_pointer (type_info) &&
         g_type_info_get_tag (type_info) == GI_TYPE_TAG_INTERFACE) {
 
-        if (!get_nested_interface_object (context, obj, priv, field_info, type_info, value))
-            goto out;
+        GIBaseInfo *interface_info = g_type_info_get_interface(type_info);
+
+        if (g_base_info_get_type (interface_info) == GI_INFO_TYPE_STRUCT ||
+            g_base_info_get_type (interface_info) == GI_INFO_TYPE_BOXED) {
+
+            success = get_nested_interface_object (context, obj, priv,
+                                                   field_info, type_info, interface_info,
+                                                   value);
+
+            g_base_info_unref ((GIBaseInfo *)interface_info);
 
-    } else {
-        if (!g_field_info_get_field (field_info, priv->gboxed, &arg)) {
-            gjs_throw(context, "Reading field %s.%s is not supported",
-                      g_base_info_get_name ((GIBaseInfo *)priv->info),
-                      g_base_info_get_name ((GIBaseInfo *)field_info));
             goto out;
         }
 
-        if (!gjs_value_from_g_argument (context, value,
-                                        type_info,
-                                        &arg))
-            goto out;
+        g_base_info_unref ((GIBaseInfo *)interface_info);
+    }
 
+    if (!g_field_info_get_field (field_info, priv->gboxed, &arg)) {
+        gjs_throw(context, "Reading field %s.%s is not supported",
+                  g_base_info_get_name ((GIBaseInfo *)priv->info),
+                  g_base_info_get_name ((GIBaseInfo *)field_info));
+        goto out;
     }
 
+    if (!gjs_value_from_g_argument (context, value,
+                                    type_info,
+                                    &arg))
+        goto out;
+
     success = TRUE;
 
 out:
@@ -717,26 +722,23 @@
                              Boxed       *parent_priv,
                              GIFieldInfo *field_info,
                              GITypeInfo  *type_info,
+                             GIBaseInfo  *interface_info,
                              jsval        value)
 {
     JSObject *proto;
-    GIBaseInfo *info;
     int offset;
-    gboolean success = FALSE;
     Boxed *proto_priv;
     Boxed *source_priv;
 
-    info = g_type_info_get_interface (type_info);
-    if (!(g_base_info_get_type (info) == GI_INFO_TYPE_STRUCT || g_base_info_get_type (info) == GI_INFO_TYPE_BOXED) ||
-        !struct_is_simple ((GIStructInfo *)info)) {
+    if (!struct_is_simple ((GIStructInfo *)interface_info)) {
         gjs_throw(context, "Writing field %s.%s is not supported",
                   g_base_info_get_name ((GIBaseInfo *)parent_priv->info),
                   g_base_info_get_name ((GIBaseInfo *)field_info));
 
-        goto out;
+        return JS_FALSE;
     }
 
-    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) info);
+    proto = gjs_lookup_boxed_prototype(context, (GIBoxedInfo*) interface_info);
     proto_priv = priv_from_js(context, proto);
 
     /* If we can't directly copy from the source object we need
@@ -745,11 +747,11 @@
     if (!boxed_get_copy_source(context, proto_priv, value, &source_priv)) {
         JSObject *tmp_object = gjs_construct_object_dynamic(context, proto, 1, &value);
         if (!tmp_object)
-            goto out;
+            return JS_FALSE;
 
         source_priv = priv_from_js(context, tmp_object);
         if (!source_priv)
-            goto out;
+            return JS_FALSE;
     }
 
     offset = g_field_info_get_offset (field_info);
@@ -757,12 +759,7 @@
            source_priv->gboxed,
            g_struct_info_get_size (source_priv->info));
 
-    success = TRUE;
-
-out:
-    g_base_info_unref( (GIBaseInfo*) info);
-
-    return success;
+    return JS_TRUE;
 }
 
 static JSBool
@@ -781,25 +778,38 @@
     if (!g_type_info_is_pointer (type_info) &&
         g_type_info_get_tag (type_info) == GI_TYPE_TAG_INTERFACE) {
 
-        if (!set_nested_interface_object (context, priv, field_info, type_info, value))
-            goto out;
+        GIBaseInfo *interface_info = g_type_info_get_interface(type_info);
 
-    } else {
-        if (!gjs_value_to_g_argument(context, value,
-                                     type_info,
-                                     g_base_info_get_name ((GIBaseInfo *)field_info),
-                                     GJS_ARGUMENT_FIELD,
-                                     TRUE, &arg))
-            goto out;
+        if (g_base_info_get_type (interface_info) == GI_INFO_TYPE_STRUCT ||
+            g_base_info_get_type (interface_info) == GI_INFO_TYPE_BOXED) {
+
+            success = set_nested_interface_object (context, priv,
+                                                   field_info, type_info,
+                                                   interface_info, value);
 
-        need_release = TRUE;
+            g_base_info_unref ((GIBaseInfo *)interface_info);
 
-        if (!g_field_info_set_field (field_info, priv->gboxed, &arg)) {
-            gjs_throw(context, "Writing field %s.%s is not supported",
-                      g_base_info_get_name ((GIBaseInfo *)priv->info),
-                      g_base_info_get_name ((GIBaseInfo *)field_info));
             goto out;
         }
+
+        g_base_info_unref ((GIBaseInfo *)interface_info);
+
+    }
+
+    if (!gjs_value_to_g_argument(context, value,
+                                 type_info,
+                                 g_base_info_get_name ((GIBaseInfo *)field_info),
+                                 GJS_ARGUMENT_FIELD,
+                                 TRUE, &arg))
+        goto out;
+
+    need_release = TRUE;
+
+    if (!g_field_info_set_field (field_info, priv->gboxed, &arg)) {
+        gjs_throw(context, "Writing field %s.%s is not supported",
+                  g_base_info_get_name ((GIBaseInfo *)priv->info),
+                  g_base_info_get_name ((GIBaseInfo *)field_info));
+        goto out;
     }
 
     success = TRUE;
@@ -1059,9 +1069,6 @@
                     break;
 		case GI_INFO_TYPE_ENUM:
 		case GI_INFO_TYPE_FLAGS:
-                    is_simple = FALSE;
-                    /* FIXME: Needs to be implemented in g_field_info_set_field */
-                    is_simple = FALSE;
                     break;
 		case GI_INFO_TYPE_OBJECT:
 		case GI_INFO_TYPE_VFUNC:

Modified: trunk/test/js/testEverythingEncapsulated.js
==============================================================================
--- trunk/test/js/testEverythingEncapsulated.js	(original)
+++ trunk/test/js/testEverythingEncapsulated.js	Wed Nov 19 22:31:17 2008
@@ -5,9 +5,11 @@
     struct.some_int = 42;
     struct.some_int8 = 43;
     struct.some_double = 42.5;
+    struct.some_enum = Everything.TestEnum.VALUE3;
     assertEquals(42, struct.some_int);
     assertEquals(43, struct.some_int8);
     assertEquals(42.5, struct.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, struct.some_enum);
 }
 
 function testStructConstructor()
@@ -15,11 +17,13 @@
     // "Copy" an object from a hash of field values
     let struct = new Everything.TestStructA({ some_int: 42,
 					      some_int8: 43,
-					      some_double: 42.5 });
+					      some_double: 42.5,
+					      some_enum: Everything.TestEnum.VALUE3 });
 
     assertEquals(42, struct.some_int);
     assertEquals(43, struct.some_int8);
     assertEquals(42.5, struct.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, struct.some_enum);
 
     // Make sure we catch bad field names
     assertRaises(function() {
@@ -32,6 +36,7 @@
     assertEquals(42, copy.some_int);
     assertEquals(43, copy.some_int8);
     assertEquals(42.5, copy.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, copy.some_enum);
 }
 
 function testSimpleBoxed() {
@@ -39,9 +44,11 @@
     simple_boxed.some_int = 42;
     simple_boxed.some_int8 = 43;
     simple_boxed.some_double = 42.5;
+    simple_boxed.some_enum = Everything.TestEnum.VALUE3;
     assertEquals(42, simple_boxed.some_int);
     assertEquals(43, simple_boxed.some_int8);
     assertEquals(42.5, simple_boxed.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, simple_boxed.some_enum);
 }
 
 function testBoxedCopyConstructor()
@@ -49,11 +56,13 @@
     // "Copy" an object from a hash of field values
     let simple_boxed = new Everything.TestSimpleBoxedA({ some_int: 42,
 							 some_int8: 43,
-							 some_double: 42.5 });
+							 some_double: 42.5,
+							 some_enum: Everything.TestEnum.VALUE3 });
 
     assertEquals(42, simple_boxed.some_int);
     assertEquals(43, simple_boxed.some_int8);
     assertEquals(42.5, simple_boxed.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, simple_boxed.some_enum);
 
     // Make sure we catch bad field names
     assertRaises(function() {
@@ -66,6 +75,7 @@
     assertEquals(42, copy.some_int);
     assertEquals(43, copy.some_int8);
     assertEquals(42.5, copy.some_double);
+    assertEquals(Everything.TestEnum.VALUE3, copy.some_enum);
  }
 
 function testNestedSimpleBoxed() {



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