[seed] Adding support to convert from JSString to gi array



commit 9605b892f9de4bd38bc1eb095d629d7efe77e077
Author: Danilo Cesar Lemes de Paula <danilo cesar collabora co uk>
Date:   Tue Jan 26 10:55:27 2016 -0200

    Adding support to convert from JSString to gi array
    
    When running some examples like the http-server from GJS, seed
    failed to convert a string into an array, to serve as argument for
    libsoup. This patch should fix that behavior.

 libseed/seed-types.c |   56 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 56 insertions(+), 0 deletions(-)
---
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index 70a3ef5..af88621 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -332,6 +332,48 @@ seed_gi_make_jsarray (JSContextRef ctx,
 }
 
 static gboolean
+seed_gi_make_array_from_string (JSContextRef ctx,
+                                JSValueRef value,
+                                GITypeInfo *param_type,
+                                void **array_p,
+                                JSValueRef *exception)
+{
+    GITypeTag element_type;
+    element_type = g_type_info_get_tag (param_type);
+
+    // This could be handled by the case where the value is an object,
+    // however, getting length from a string crashs inside JSC. So,
+    // we're now proper handling strings here.
+    JSStringRef js_string = JSValueToStringCopy(ctx, value, exception);
+    size_t length = JSStringGetLength(js_string);
+    const JSChar *js_ptr = JSStringGetCharactersPtr(js_string);
+
+    switch (element_type)
+      {
+        case GI_TYPE_TAG_UINT8:
+          {
+            guint8 *guint8result;
+            guint8result = g_new0 (guint8, length + 1);
+            for (int i = 0; i < length; i++)
+            {
+                guint8result[i] = js_ptr[i];
+            }
+            *array_p = guint8result;
+            break;
+          }
+        default:
+          {
+              JSStringRelease(js_string);
+              seed_make_exception (ctx, exception, "ArgumentError",
+                                   "Unhandled array element type");
+              return FALSE;
+          }
+      }
+    JSStringRelease(js_string);
+    return TRUE;
+}
+
+static gboolean
 seed_gi_make_array (JSContextRef ctx,
                    JSValueRef array,
                    guint length,
@@ -803,6 +845,20 @@ seed_value_to_gi_argument (JSContextRef ctx,
            arg->v_pointer = NULL;
            break;
          }
+       else if (JSValueIsString(ctx, value))
+          {
+            GITypeInfo *param_type;
+            param_type = g_type_info_get_param_type (type_info, 0);
+
+            if (!seed_gi_make_array_from_string (ctx, value, param_type,
+                                         &arg->v_pointer, exception))
+              {
+                    g_base_info_unref ((GIBaseInfo *) param_type);
+                    return FALSE;
+              }
+            g_base_info_unref ((GIBaseInfo *) param_type);
+            break;
+          }
        else if (!JSValueIsObject (ctx, value))
          {
            // TODO: FIXME: Is this right?


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