From: Christian Dywan Date: Mon, 21 Jul 2008 18:04:54 +0000 (+0200) Subject: Add gjs_value_forall and gjs_get_nth_attribute X-Git-Url: https://spindle.queued.net/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1213c80d9f7dcd6ae1a22e36b8f79c5675e26d9;p=midori Add gjs_value_forall and gjs_get_nth_attribute --- diff --git a/midori/gjs.c b/midori/gjs.c index 0d432f39..ca842655 100644 --- a/midori/gjs.c +++ b/midori/gjs.c @@ -230,13 +230,45 @@ gjs_value_get_attribute_string (GjsValue* value, return gjs_value_get_string (attribute); } +/** + * gjs_value_get_nth_attribute: + * @value: a #GjsValue + * @n: an index + * + * Retrieves the indiced attribute at the specified + * position. This is particularly faster than looking + * up attributes by name. + * + * This can only be used on object values. + * + * Return value: a new #GjsValue, or %NULL + **/ +GjsValue* +gjs_value_get_nth_attribute (GjsValue* value, + guint n) +{ + JSObjectRef js_object; + JSValueRef js_value; + + g_return_val_if_fail (gjs_value_is_object (value), NULL); + + js_object = JSValueToObject (value->js_context, value->js_value, NULL); + js_value = JSObjectGetPropertyAtIndex (value->js_context, js_object, n, NULL); + if (JSValueIsUndefined (value->js_context, js_value)) + return NULL; + + return gjs_value_new (value->js_context, js_value); +} + /** * gjs_value_foreach: * @value: a #GjsValue * @callback: a callback * @user_data: user data * - * Runs the specified callback for each attribute of the value. + * Calls the specified callback for each indiced attribute. + * This is a particularly fast way of looping through the + * elements of an array. * * This can only be used on object values. **/ @@ -244,6 +276,40 @@ void gjs_value_foreach (GjsValue* value, GjsCallback callback, gpointer user_data) +{ + guint i; + GjsValue* attribute; + + g_return_if_fail (gjs_value_is_object (value)); + + i = 0; + do + { + attribute = gjs_value_get_nth_attribute (value, i); + if (attribute) + { + callback (attribute, user_data); + g_object_unref (attribute); + } + i++; + } + while (attribute); +} + +/** + * gjs_value_forall: + * @value: a #GjsValue + * @callback: a callback + * @user_data: user data + * + * Calls the specified callback for each attribute. + * + * This can only be used on object values. + **/ +void +gjs_value_forall (GjsValue* value, + GjsCallback callback, + gpointer user_data) { JSObjectRef js_object; JSPropertyNameArrayRef js_properties; diff --git a/midori/gjs.h b/midori/gjs.h index 47e2b0a3..19bf4d4e 100644 --- a/midori/gjs.h +++ b/midori/gjs.h @@ -66,6 +66,10 @@ const gchar* gjs_value_get_attribute_string (GjsValue* value, const gchar* name); +GjsValue* +gjs_value_get_nth_attribute (GjsValue* value, + guint n); + typedef void (*GjsCallback) (GjsValue* value, gpointer user_data); @@ -75,6 +79,11 @@ gjs_value_foreach (GjsValue* value, GjsCallback callback, gpointer user_data); +void +gjs_value_forall (GjsValue* value, + GjsCallback callback, + gpointer user_data); + GjsValue* gjs_value_get_by_name (GjsValue* value, const gchar* name);