[xpcom] Clean up array handling
authorKalle Vahlman <kalle.vahlman@movial.com>
Wed, 22 Apr 2009 09:25:33 +0000 (12:25 +0300)
committerKalle Vahlman <kalle.vahlman@movial.com>
Wed, 22 Apr 2009 10:05:55 +0000 (13:05 +0300)
xpcom-dbusservice/DBusMarshaling.cpp

index df192a8b9ea0f088b922d032d8c22f5704a742d1..c3bccbb60aed65dada2f6598b7e08001a0875105 100644 (file)
@@ -486,7 +486,7 @@ addVariantToIter(JSContext* cx, nsIVariant *aVariant, DBusMessageIter *aIter, DB
                 }
             }
         } else {
-          BDBLOG(("  add array from variant\n"));
+            BDBLOG(("  add array from variant\n"));
 
             // need to recurse into array
             PRUint16 type = 0;
@@ -509,91 +509,19 @@ addVariantToIter(JSContext* cx, nsIVariant *aVariant, DBusMessageIter *aIter, DB
 
             int dbustype = dbus_signature_iter_get_current_type(&aChildSigIter);
 
-            if (typesMatch(type, dbustype))
+            DBusMessageIter arrayIter;
+            if (!dbus_message_iter_open_container(aIter, DBUS_TYPE_ARRAY,
+                                                    array_signature, &arrayIter))
             {
-                BDBLOG(("*** Yay, types match!\n"));
-
-                DBusMessageIter arrayIter;
-                if (dbus_message_iter_open_container(aIter, DBUS_TYPE_ARRAY,
-                                                     array_signature, &arrayIter))
-                {
-                    addArrayDataToIter(cx, data_ptr, 0, count, type,
-                                       &arrayIter, &aChildSigIter);
-                    dbus_message_iter_close_container(aIter, &arrayIter);
-                }
-                nsMemory::Free(data_ptr);
-            } else {
-                BDBLOG(("*** Bummer, types don't match so we'll need to iterate\n"));
-
-                DBusMessageIter arrayIter;
-                dbus_message_iter_open_container(aIter, DBUS_TYPE_ARRAY,
-                                                 array_signature, &arrayIter);
-
-                /* Seems stupid not to be able to iterate over the values as
-                 * nsIVariants directly, but no API to that effect seems to be
-                 * available...
-                 */
-                for (PRUint32 i = 0; i < count; i++)
-                {
-                    nsCOMPtr<nsIWritableVariant> variant = do_CreateInstance("@mozilla.org/variant;1");
-                    
-                    switch (type)
-                    {
-#define SET_INT(bits) \
-    case nsIDataType::VTYPE_INT ## bits: \
-        variant->SetAsInt ## bits(*(((PRInt ## bits*)data_ptr)+i)); \
-        break; \
-    case nsIDataType::VTYPE_UINT ## bits: \
-        variant->SetAsUint ## bits(*(((PRUint ## bits*)data_ptr)+i)); \
-        break;
-                        SET_INT(16)
-                        SET_INT(32)
-                        SET_INT(64)
-                        case nsIDataType::VTYPE_INTERFACE_IS:
-                        {
-                            nsISupports **data = (nsISupports **)data_ptr;
-
-                            // We might have array of variants
-                            nsCOMPtr<nsIVariant> data_variant =
-                                do_QueryInterface(data[i]);
-
-                            if (data_variant) {
-                                variant->SetFromVariant(data_variant);
-                                break;
-                            }
-
-                            // We might have a wrapped JS obect in the variant
-                            // eg. dicts
-                            nsCOMPtr<nsIXPConnectWrappedJS> wrapped =
-                                do_QueryInterface(data[i]);
-
-                            if (!wrapped)
-                                break;
-
-                            JSObject *js_obj = nsnull;
-                            if (!NS_SUCCEEDED(wrapped->GetJSObject(&js_obj)))
-                                break;
-
-                            jsval js_obj_as_value = OBJECT_TO_JSVAL(js_obj);
-                            addJSValueToIter(cx, &js_obj_as_value,
-                                             &arrayIter, &aChildSigIter);
-                            break;
-                        }
-                        default:
-                            BDBLOG(("*** No conversion from %i to %c\n", type, dbustype));
-                            break;
-                    }
-
-                    PRUint16 vtype = 0;
-                    variant->GetDataType(&vtype);
-                    if (vtype != nsIDataType::VTYPE_EMPTY)
-                        addVariantToIter(cx, variant, &arrayIter, &aChildSigIter);
-                }
-
-                dbus_message_iter_close_container(aIter, &arrayIter);
                 nsMemory::Free(data_ptr);
+                return;
             }
 
+            addArrayDataToIter(cx, data_ptr, 0, count, type,
+                                &arrayIter, &aChildSigIter);
+
+            dbus_message_iter_close_container(aIter, &arrayIter);
+            nsMemory::Free(data_ptr);
         }
     }
     else if (element_type == DBUS_TYPE_VARIANT)
@@ -836,14 +764,34 @@ void addArrayDataToIter(JSContext* cx, void *data_ptr, PRUint32 start, PRUint32
         case nsIDataType::VTYPE_INTERFACE_IS:
         {
             nsISupports **data = (nsISupports **)data_ptr;
-            for (PRUint32 i = start; i < count; i++)
+            for (PRUint32 i = 0; i < count; i++)
             {
-                nsCOMPtr<nsIVariant> data_variant = do_QueryInterface(data[i]);
-                if (data_variant)
-                    addVariantToIter(cx, data_variant, aIter, aSigIter);
-                else
-                    BDBLOG(("  interface not nsIVariant\n"));
+                // We might have a variant
+                nsCOMPtr<nsIVariant> variant = do_QueryInterface(data[i]);
+
+                if (variant) {
+                    addVariantToIter(cx, variant, aIter, aSigIter);
+                    dbus_signature_iter_next(aSigIter);
+                    continue;
+                }
+
+                // We might have a wrapped JS object in the variant
+                // eg. dicts
+                nsCOMPtr<nsIXPConnectWrappedJS> wrapped = do_QueryInterface(data[i]);
+
+                if (!wrapped)
+                    break;
+
+                JSObject *js_obj = nsnull;
+                if (!NS_SUCCEEDED(wrapped->GetJSObject(&js_obj)))
+                    break;
+
+                jsval js_obj_as_value = OBJECT_TO_JSVAL(js_obj);
+                addJSValueToIter(cx, &js_obj_as_value,
+                                    aIter, aSigIter);
+                dbus_signature_iter_next(aSigIter);
             }
+
             break;
         }
         default: