From e0cdd87f5bb1d7de9d24241a79f8ca92567f3626 Mon Sep 17 00:00:00 2001
From: C. Scott Ananian <cscott@litl.com>
Date: Thu, 7 May 2009 16:07:26 -0400
Subject: [PATCH] Convert arrays of lists/hashes/arrays from javascript to C.

---
 gi/arg.c |   52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 52 insertions(+), 0 deletions(-)

diff --git a/gi/arg.c b/gi/arg.c
index 0c38945..ae9c4b0 100644
--- a/gi/arg.c
+++ b/gi/arg.c
@@ -410,6 +410,50 @@ gjs_array_to_intarray(JSContext   *context,
 }
 
 static JSBool
+gjs_array_to_ptrarray(JSContext *context,
+                      jsval array_value,
+                      unsigned int length,
+                      GITypeInfo *param_info,
+                      void ***arr_p)
+{
+    void **result = g_malloc0(length * sizeof (*result));
+    unsigned i;
+
+    for (i = 0; i < length; i++) {
+        jsval elem = JSVAL_VOID;
+        GArgument arg;
+
+        if (!JS_GetElement(context, JSVAL_TO_OBJECT(array_value), i, &elem)) {
+            gjs_throw(context,
+                      "Missing pointer array element %u",
+                      i);
+            goto error;
+        }
+
+        if (!gjs_value_to_g_argument(context, elem, param_info, NULL,
+                                     GJS_ARGUMENT_LIST_ELEMENT, TRUE,
+                                     &arg))
+            goto error;
+
+        result[i] = arg.v_pointer;
+    }
+
+    *arr_p = result;
+
+    return JS_TRUE;
+
+ error:
+    // free partial conversion results
+    for (i = 0; i < length; i++) {
+        GArgument arg = { .v_pointer = result[i] };
+        gjs_g_argument_release (context, GI_TRANSFER_EVERYTHING,
+                                param_info, &arg);
+    }
+    g_free (result);
+    return JS_FALSE;
+}
+
+static JSBool
 gjs_array_to_array(JSContext   *context,
                    jsval        array_value,
                    unsigned int length,
@@ -424,6 +468,7 @@ gjs_array_to_array(JSContext   *context,
 
     switch (element_type) {
     case GI_TYPE_TAG_UTF8:
+    case GI_TYPE_TAG_FILENAME:
         return gjs_array_to_strv (context, array_value, length, arr_p);
     case GI_TYPE_TAG_UINT8:
         return gjs_array_to_intarray
@@ -443,6 +488,13 @@ gjs_array_to_array(JSContext   *context,
     case GI_TYPE_TAG_INT32:
         return gjs_array_to_intarray
             (context, array_value, length, arr_p, 4, SIGNED);
+    case GI_TYPE_TAG_ARRAY:
+    case GI_TYPE_TAG_INTERFACE:
+    case GI_TYPE_TAG_GLIST:
+    case GI_TYPE_TAG_GSLIST:
+    case GI_TYPE_TAG_GHASH:
+        return gjs_array_to_ptrarray
+            (context, array_value, length, param_info, arr_p);
     default:
         gjs_throw(context,
                   "Unhandled array element type %d", element_type);
-- 
1.5.6.3

