diff options
author | Danielle Madeley <danielle.madeley@collabora.co.uk> | 2010-06-22 14:42:53 +1000 |
---|---|---|
committer | Danielle Madeley <danielle.madeley@collabora.co.uk> | 2010-06-22 14:42:53 +1000 |
commit | 327a02307582ff27802a104de1b7588c26e9ea45 (patch) | |
tree | e73d6d6743d0ddbab2beb4940dfacdbe14d8c1d0 | |
parent | dd1e36572040870f74a7e84a80034aeac0dd9d38 (diff) | |
parent | 4b95a86780fa4170ec343088143cb7f8d4aac7bc (diff) | |
download | gjs-327a02307582ff27802a104de1b7588c26e9ea45.tar.gz |
Merge branch 'strv-621960'
-rw-r--r-- | gi/arg.c | 59 | ||||
-rw-r--r-- | gi/arg.h | 3 | ||||
-rw-r--r-- | gi/value.c | 14 | ||||
-rw-r--r-- | test/js/testEverythingBasic.js | 9 |
4 files changed, 65 insertions, 20 deletions
@@ -373,6 +373,40 @@ gjs_object_to_g_hash(JSContext *context, } JSBool +gjs_array_from_strv(JSContext *context, + jsval *value_p, + const char **strv) +{ + JSObject *obj; + jsval elem; + guint i; + JSBool result = JS_FALSE; + + obj = JS_NewArrayObject(context, 0, JSVAL_NULL); + if (obj == NULL) + return JS_FALSE; + + *value_p = OBJECT_TO_JSVAL(obj); + + for (i = 0; strv[i] != NULL; i++) { + if (!gjs_string_from_utf8 (context, strv[i], -1, &elem)) + goto out; + + if (!JS_DefineElement(context, obj, i, elem, + NULL, NULL, JSPROP_ENUMERATE)) { + goto out; + } + } + + result = JS_TRUE; + +out: + JS_RemoveRoot(context, &elem); + + return result; +} + +JSBool gjs_array_to_strv(JSContext *context, jsval array_value, unsigned int length, @@ -1784,27 +1818,12 @@ gjs_value_from_g_argument (JSContext *context, 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; + if (param_tag == GI_TYPE_TAG_UTF8) { + result = gjs_array_from_strv(context, + value_p, + arg->v_pointer); } 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); + gjs_throw(context, "FIXME: Only supporting null-terminated arrays of strings"); } g_base_info_unref((GIBaseInfo*) param_info); @@ -76,6 +76,9 @@ JSBool _gjs_flags_value_is_valid (JSContext *context, GType gtype, guint value); +JSBool gjs_array_from_strv (JSContext *context, + jsval *value_p, + const char **strv); JSBool gjs_array_to_strv (JSContext *context, jsval array_value, @@ -566,6 +566,20 @@ gjs_value_from_g_value_internal(JSContext *context, obj = gjs_object_from_g_object(context, gobj); *value_p = OBJECT_TO_JSVAL(obj); + } else if (gtype == G_TYPE_STRV) { + if (!gjs_array_from_strv (context, + value_p, + g_value_get_boxed (gvalue))) { + gjs_throw(context, "Failed to convert strv to array"); + return JS_FALSE; + } + } else if (g_type_is_a(gtype, G_TYPE_HASH_TABLE) || + g_type_is_a(gtype, G_TYPE_ARRAY) || + g_type_is_a(gtype, G_TYPE_BYTE_ARRAY) || + g_type_is_a(gtype, G_TYPE_PTR_ARRAY)) { + gjs_throw(context, + "Unable to introspect element-type of container in GValue"); + return JS_FALSE; } else if (g_type_is_a(gtype, G_TYPE_BOXED)) { GjsBoxedCreationFlags boxed_flags; GIBaseInfo *info; diff --git a/test/js/testEverythingBasic.js b/test/js/testEverythingBasic.js index ee837fc6..fa229085 100644 --- a/test/js/testEverythingBasic.js +++ b/test/js/testEverythingBasic.js @@ -440,4 +440,13 @@ function testObjTortureSignature1Success() { assertEquals(q, 14); } +function testStrvInGValue() { + let v = Everything.test_strv_in_gvalue(); + + assertEquals(v.length, 3); + assertEquals(v[0], "one"); + assertEquals(v[1], "two"); + assertEquals(v[2], "three"); +} + gjstestRun(); |