[gjs/wip/ptomato/warnings: 5/15] arg: Support boolean-valued arrays
- From: Philip Chimento <pchimento src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gjs/wip/ptomato/warnings: 5/15] arg: Support boolean-valued arrays
- Date: Tue, 18 Oct 2016 20:09:00 +0000 (UTC)
commit 532d47a73d2f6d6e6aa33a2c64eab10666600043
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 689d552..3d252e7 100644
--- a/gi/arg.cpp
+++ b/gi/arg.cpp
@@ -559,6 +559,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,
@@ -907,6 +932,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);
@@ -987,6 +1014,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;
@@ -2148,6 +2178,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;
@@ -2417,6 +2450,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;
@@ -3149,6 +3187,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:
@@ -3224,6 +3263,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]