]> spindle.queued.net Git - midori/commitdiff
Add gjs_value_forall and gjs_get_nth_attribute
authorChristian Dywan <christian@twotoasts.de>
Mon, 21 Jul 2008 18:04:54 +0000 (20:04 +0200)
committerChristian Dywan <christian@twotoasts.de>
Mon, 21 Jul 2008 18:04:54 +0000 (20:04 +0200)
midori/gjs.c
midori/gjs.h

index 0d432f3974fbab481278590e07a7a1a6f06aad43..ca8426557d12681bd7bdc691014bb5a2ea343442 100644 (file)
@@ -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;
index 47e2b0a30ef4ad93435a1b3107d6fad26a203a74..19bf4d4ecffb45de0150720faedb9d2cb8d4506a 100644 (file)
@@ -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);