[gjs] gi: Add support for returning a NULL-terminated string array



commit 5bf111ea52a914944399328facadfb70b52065b2
Author: Johan Bilien <jobi litl com>
Date:   Mon Jun 15 14:39:02 2009 +0100

    gi: Add support for returning a NULL-terminated string array

 gi/arg.c                       |   48 +++++++++++++++++++++++++++++++++++++--
 test/js/testEverythingBasic.js |   14 ++++++++++-
 2 files changed, 58 insertions(+), 4 deletions(-)
---
diff --git a/gi/arg.c b/gi/arg.c
index aa1ad87..a966197 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -1448,8 +1448,47 @@ gjs_value_from_g_argument (JSContext  *context,
         if (arg->v_pointer == NULL) {
             /* OK, but no conversion to do */
         } else {
-            gjs_throw(context, "FIXME: Only supporting null ARRAYs");
-            return JS_FALSE;
+
+            if (g_type_info_is_zero_terminated(type_info)) {
+                GITypeInfo *param_info;
+                GITypeTag param_tag;
+                JSBool result;
+
+                param_info = g_type_info_get_param_type(type_info, 0);
+                g_assert(param_info != NULL);
+
+                param_tag = g_type_info_get_tag((GITypeInfo*) param_info);
+
+                if (param_tag != GI_TYPE_TAG_UTF8) {
+                    gjs_throw(context, "FIXME: Only supporting null-terminated arrays of strings");
+                    result = JS_FALSE;
+                } else {
+                    GSList *list;
+                    char ** strv;
+
+                    list = NULL;
+                    for (strv = arg->v_pointer; *strv; strv ++) {
+                        list = g_slist_prepend(list, *strv);
+                    }
+                    list = g_slist_reverse(list);
+
+                    result = gjs_array_from_g_list(context,
+                                                   value_p,
+                                                   type_tag,
+                                                   param_info,
+                                                   NULL,
+                                                   list);
+
+                    g_slist_free(list);
+                }
+
+                g_base_info_unref((GIBaseInfo*) param_info);
+
+                return result;
+            } else {
+                gjs_throw(context, "FIXME: Only supporting zero-terminated ARRAYs");
+                return JS_FALSE;
+            }
         }
         break;
 
@@ -1699,7 +1738,10 @@ gjs_g_arg_release_internal(JSContext  *context,
             switch (element_type) {
             case GI_TYPE_TAG_UTF8:
             case GI_TYPE_TAG_FILENAME:
-                g_strfreev (arg->v_pointer);
+                if (transfer == GI_TRANSFER_CONTAINER)
+                    g_free(arg->v_pointer);
+                else if (transfer == GI_TRANSFER_EVERYTHING)
+                    g_strfreev (arg->v_pointer);
                 break;
 
             case GI_TYPE_TAG_UINT8:
diff --git a/test/js/testEverythingBasic.js b/test/js/testEverythingBasic.js
index 5ff5c64..f21ec97 100644
--- a/test/js/testEverythingBasic.js
+++ b/test/js/testEverythingBasic.js
@@ -121,7 +121,19 @@ function testStrv() {
     // Second two are deliberately not strings
     assertRaises(function() { Everything.test_strv_in(['1', 2, 3]); });
 
-    // FIXME: Everything.test_strv_out not implemented.
+    let strv = Everything.test_strv_out();
+    assertEquals(5, strv.length);
+    assertEquals("thanks", strv[0]);
+    assertEquals("for", strv[1]);
+    assertEquals("all", strv[2]);
+    assertEquals("the", strv[3]);
+    assertEquals("fish", strv[4]);
+
+    strv = Everything.test_strv_out_container();
+    assertEquals(3, strv.length);
+    assertEquals("1", strv[0]);
+    assertEquals("2", strv[1]);
+    assertEquals("3", strv[2]);
 }
 
 function testUtf8() {



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