[gjs/wip/ptomato/warnings: 6/16] arg: Support boolean-valued arrays



commit c6d05ee2d7775755cb794eac9f8bf009524bfd8d
Author: Philip Chimento <philip chimento gmail com>
Date:   Mon Oct 10 17:06:16 2016 -0700

    arg: Support boolean-valued arrays
    
    Compiling with -Wswitch revealed that we don't support these, but it's
    actually quite trivial to do.
    
    However, we don't support zero-terminated boolean arrays as they are
    nonsensical, they could only contain TRUE. Throw an exception in the
    unlikely case that we encounter this.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=772790

 gi/arg.cpp                              |   40 +++++++++++++++++++++++++++++++
 installed-tests/js/testGIMarshalling.js |    9 +++++++
 2 files changed, 49 insertions(+), 0 deletions(-)
---
diff --git a/gi/arg.cpp b/gi/arg.cpp
index e8a26c9..589f8a8 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -560,6 +560,31 @@ gjs_string_to_intarray(JSContext   *context,
 }
 
 static bool
+gjs_array_to_gboolean_array(JSContext      *cx,
+                            JS::Value       array_value,
+                            unsigned        length,
+                            void          **arr_p)
+{
+    unsigned i;
+
+    gboolean *result = g_new0(gboolean, length);
+
+    for (i = 0; i < length; i++) {
+        JS::RootedValue elem(cx);
+        if (!JS_GetElement(cx, array_value.toObjectOrNull(), i, elem.address())) {
+            g_free(result);
+            gjs_throw(cx, "Missing array element %u", i);
+            return false;
+        }
+        bool val = JS::ToBoolean(elem);
+        result[i] = val;
+    }
+
+    *arr_p = result;
+    return true;
+}
+
+static bool
 gjs_array_to_intarray(JSContext   *context,
                       JS::Value    array_value,
                       unsigned int length,
@@ -908,6 +933,8 @@ gjs_array_to_array(JSContext   *context,
     switch (element_type) {
     case GI_TYPE_TAG_UTF8:
         return gjs_array_to_strv (context, array_value, length, arr_p);
+    case GI_TYPE_TAG_BOOLEAN:
+        return gjs_array_to_gboolean_array(context, array_value, length, arr_p);
     case GI_TYPE_TAG_UNICHAR:
         return gjs_array_to_intarray(context, array_value, length, arr_p,
             sizeof(gunichar), UNSIGNED);
@@ -988,6 +1015,9 @@ gjs_g_array_new_for_type(JSContext    *context,
     }
 
     switch (element_type) {
+    case GI_TYPE_TAG_BOOLEAN:
+        element_size = sizeof(gboolean);
+        break;
     case GI_TYPE_TAG_UNICHAR:
         element_size = sizeof(gunichar);
         break;
@@ -2149,6 +2179,9 @@ gjs_array_from_carray_internal (JSContext  *context,
         case GI_TYPE_TAG_UINT8:
         case GI_TYPE_TAG_UNICHAR:
             g_assert_not_reached();
+        case GI_TYPE_TAG_BOOLEAN:
+            ITERATE(boolean);
+            break;
         case GI_TYPE_TAG_INT8:
           ITERATE(int8);
           break;
@@ -2418,6 +2451,11 @@ gjs_array_from_zero_terminated_c_array (JSContext  *context,
         case GI_TYPE_TAG_ERROR:
           ITERATE(pointer);
           break;
+        /* Boolean zero-terminated array makes no sense, because FALSE is also
+         * zero */
+        case GI_TYPE_TAG_BOOLEAN:
+            gjs_throw(context, "Boolean zero-terminated array not supported");
+            goto finally;
         default:
           gjs_throw(context, "Unknown element-type %d", element_type);
           goto finally;
@@ -3150,6 +3188,7 @@ gjs_g_arg_release_internal(JSContext  *context,
                     g_strfreev ((gchar **) arg->v_pointer);
                 break;
 
+            case GI_TYPE_TAG_BOOLEAN:
             case GI_TYPE_TAG_UINT8:
             case GI_TYPE_TAG_UINT16:
             case GI_TYPE_TAG_UINT32:
@@ -3225,6 +3264,7 @@ gjs_g_arg_release_internal(JSContext  *context,
             element_type = g_type_info_get_tag(param_info);
 
             switch (element_type) {
+            case GI_TYPE_TAG_BOOLEAN:
             case GI_TYPE_TAG_UNICHAR:
             case GI_TYPE_TAG_UINT8:
             case GI_TYPE_TAG_UINT16:
diff --git a/installed-tests/js/testGIMarshalling.js b/installed-tests/js/testGIMarshalling.js
index 36b26cd..6657e6c 100644
--- a/installed-tests/js/testGIMarshalling.js
+++ b/installed-tests/js/testGIMarshalling.js
@@ -69,6 +69,13 @@ function testCArray() {
     assertEquals(1, array[2]);
     assertEquals(5, array[3]);
 
+    array = GIMarshallingTests.array_bool_out();
+    assertEquals(4, array.length);
+    assertEquals(true, array[0]);
+    assertEquals(false, array[1]);
+    assertEquals(true, array[2]);
+    assertEquals(true, array[3]);
+
     assertEquals('const \u2665 utf8', GIMarshallingTests.array_unichar_out());
     assertEquals('const \u2665 utf8',
         GIMarshallingTests.array_zero_terminated_return_unichar());
@@ -120,6 +127,7 @@ function testCArray() {
     GIMarshallingTests.array_in_guint8_len(array);
     GIMarshallingTests.array_int64_in(array);
     GIMarshallingTests.array_uint64_in(array);
+    GIMarshallingTests.array_bool_in(array);
 }
 
 function testGArray() {
@@ -147,6 +155,7 @@ function testGArray() {
     GIMarshallingTests.garray_unichar_none_in('const \u2665 utf8');
     GIMarshallingTests.garray_unichar_none_in([0x63, 0x6f, 0x6e, 0x73, 0x74,
         0x20, 0x2665, 0x20, 0x75, 0x74, 0x66, 0x38]);
+    GIMarshallingTests.garray_bool_none_in([-1, 0, 1, 2]);
 
     array = GIMarshallingTests.garray_utf8_none_out();
     assertEquals("0", array[0]);


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