[seed] Fix a issue when converting JSStrings to C arrays



commit cb868bcdfd5032adda003371a755748b5f903884
Author: Danilo Cesar Lemes de Paula <danilo cesar collabora co uk>
Date:   Thu Jun 9 18:42:25 2016 -0300

    Fix a issue when converting JSStrings to C arrays
    
    When transforming a string into an array we probably don't want the \0
    character.
    This behavior change make it more compatible to GJS, as GJS uses arrays
    in that way. (see egJSON.js and gjs/example/http-server.js) example.

 libseed/seed-types.c |   17 ++++++++++++++---
 1 files changed, 14 insertions(+), 3 deletions(-)
---
diff --git a/libseed/seed-types.c b/libseed/seed-types.c
index ebbac23..649927c 100644
--- a/libseed/seed-types.c
+++ b/libseed/seed-types.c
@@ -331,18 +331,29 @@ seed_gi_make_array_from_string(JSContextRef ctx,
     guint real_size = JSStringGetUTF8CString(js_string, buffer, length);
 
     switch (element_type) {
+        case GI_TYPE_TAG_INT8:
         case GI_TYPE_TAG_UINT8: {
             *array_p = buffer;
+
+            // So, GJS doesn't computer the \0 at the end of line.
+            // it does make sense, as converting a string to an array
+            // we don't want the EOS. However, I couldn't find a way to do it
+            // with
+            // JSC, so I'm basically reducing one byte.
+            if (out_array_length)
+                *out_array_length = real_size - 1;
             break;
         }
+        case GI_TYPE_TAG_INT16:
+        case GI_TYPE_TAG_UINT16:
+        // TODO: implement utf16 support
         default: {
             seed_make_exception(ctx, exception, "ArgumentError",
-                                "Unhandled array element type");
+                                "Cannot convert string to array of '%s'",
+                                g_type_tag_to_string(element_type));
             return FALSE;
         }
     }
-    if (out_array_length)
-        *out_array_length = real_size;
 
     return TRUE;
 }


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