Bug 677788 - Remove array/string 'capacity' argument from XPCConvert. r=mrbkap
authorBobby Holley <bobbyholley@gmail.com>
Sun, 06 Nov 2011 13:32:25 +0100
changeset 80508 6c1a7d01303f1497a439ae399828ef09074484de
parent 80507 ab0a659233a0c9fc77f1feb8c9de758b5b3c652e
child 80509 104c466724acd2cb7e9d3b648d140f355510bb15
push id506
push userclegnitto@mozilla.com
push dateWed, 09 Nov 2011 02:03:18 +0000
treeherdermozilla-aurora@63587fc7bb93 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs677788
milestone10.0a1
Bug 677788 - Remove array/string 'capacity' argument from XPCConvert. r=mrbkap Because length_is duplicates size_is, 'capacity' is just an alias for 'count'.
js/xpconnect/src/XPCConvert.cpp
js/xpconnect/src/XPCVariant.cpp
js/xpconnect/src/XPCWrappedJSClass.cpp
js/xpconnect/src/XPCWrappedNative.cpp
js/xpconnect/src/xpcprivate.h
--- a/js/xpconnect/src/XPCConvert.cpp
+++ b/js/xpconnect/src/XPCConvert.cpp
@@ -1778,19 +1778,18 @@ failure:
     return JS_FALSE;
 
 #undef POPULATE
 }
 
 // static
 JSBool
 XPCConvert::JSArray2Native(XPCCallContext& ccx, void** d, jsval s,
-                           JSUint32 count, JSUint32 capacity,
-                           const nsXPTType& type, const nsID* iid,
-                           uintN* pErr)
+                           JSUint32 count, const nsXPTType& type,
+                           const nsID* iid, uintN* pErr)
 {
     NS_PRECONDITION(d, "bad param");
 
     JSContext* cx = ccx.GetJSContext();
 
     // No Action, FRee memory, RElease object
     enum CleanupMode {na, fr, re};
 
@@ -1807,21 +1806,16 @@ XPCConvert::JSArray2Native(XPCCallContex
 
     if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) {
         if (0 != count) {
             if (pErr)
                 *pErr = NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY;
             return JS_FALSE;
         }
 
-        // If a non-zero capacity was indicated then we build an
-        // empty array rather than return nsnull.
-        if (0 != capacity)
-            goto fill_array;
-
         *d = nsnull;
         return JS_TRUE;
     }
 
     if (!JSVAL_IS_OBJECT(s)) {
         if (pErr)
             *pErr = NS_ERROR_XPC_CANT_CONVERT_PRIMITIVE_TO_ARRAY;
         return JS_FALSE;
@@ -1830,31 +1824,31 @@ XPCConvert::JSArray2Native(XPCCallContex
     jsarray = JSVAL_TO_OBJECT(s);
     if (!JS_IsArrayObject(cx, jsarray)) {
         if (pErr)
             *pErr = NS_ERROR_XPC_CANT_CONVERT_OBJECT_TO_ARRAY;
         return JS_FALSE;
     }
 
     jsuint len;
-    if (!JS_GetArrayLength(cx, jsarray, &len) || len < count || capacity < count) {
+    if (!JS_GetArrayLength(cx, jsarray, &len) || len < count) {
         if (pErr)
             *pErr = NS_ERROR_XPC_NOT_ENOUGH_ELEMENTS_IN_ARRAY;
         return JS_FALSE;
     }
 
     if (pErr)
         *pErr = NS_ERROR_XPC_BAD_CONVERT_JS;
 
 #define POPULATE(_mode, _t)                                                   \
     PR_BEGIN_MACRO                                                            \
         cleanupMode = _mode;                                                  \
         size_t max = PR_UINT32_MAX / sizeof(_t);                              \
-        if (capacity > max ||                                                 \
-            nsnull == (array = nsMemory::Alloc(capacity * sizeof(_t)))) {     \
+        if (count > max ||                                                    \
+            nsnull == (array = nsMemory::Alloc(count * sizeof(_t)))) {        \
             if (pErr)                                                         \
                 *pErr = NS_ERROR_OUT_OF_MEMORY;                               \
             goto failure;                                                     \
         }                                                                     \
         for (initedCount = 0; initedCount < count; initedCount++) {           \
             if (!JS_GetElement(cx, jsarray, initedCount, &current) ||         \
                 !JSData2Native(ccx, ((_t*)array)+initedCount, current, type,  \
                                JS_TRUE, iid, pErr))                           \
@@ -1862,17 +1856,16 @@ XPCConvert::JSArray2Native(XPCCallContex
         }                                                                     \
     PR_END_MACRO
 
 
     // XXX check IsPtr - esp. to handle array of nsID (as opposed to nsID*)
 
     // XXX make extra space at end of char* and wchar* and null termintate
 
-fill_array:
     switch (type.TagPart()) {
     case nsXPTType::T_I8            : POPULATE(na, int8);           break;
     case nsXPTType::T_I16           : POPULATE(na, int16);          break;
     case nsXPTType::T_I32           : POPULATE(na, int32);          break;
     case nsXPTType::T_I64           : POPULATE(na, int64);          break;
     case nsXPTType::T_U8            : POPULATE(na, uint8);          break;
     case nsXPTType::T_U16           : POPULATE(na, uint16);         break;
     case nsXPTType::T_U32           : POPULATE(na, uint32);         break;
@@ -1970,36 +1963,29 @@ XPCConvert::NativeStringWithSize2JS(JSCo
             return JS_FALSE;
     }
     return JS_TRUE;
 }
 
 // static
 JSBool
 XPCConvert::JSStringWithSize2Native(XPCCallContext& ccx, void* d, jsval s,
-                                    JSUint32 count, JSUint32 capacity,
-                                    const nsXPTType& type,
+                                    JSUint32 count, const nsXPTType& type,
                                     uintN* pErr)
 {
     NS_PRECONDITION(!JSVAL_IS_NULL(s), "bad param");
     NS_PRECONDITION(d, "bad param");
 
     JSContext* cx = ccx.GetJSContext();
 
     JSUint32 len;
 
     if (pErr)
         *pErr = NS_ERROR_XPC_BAD_CONVERT_NATIVE;
 
-    if (capacity < count) {
-        if (pErr)
-            *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING;
-        return JS_FALSE;
-    }
-
     if (!type.IsPointer()) {
         XPC_LOG_ERROR(("XPCConvert::JSStringWithSize2Native : unsupported type"));
         return JS_FALSE;
     }
     switch (type.TagPart()) {
         case nsXPTType::T_PSTRING_SIZE_IS:
         {
             if (JSVAL_IS_VOID(s) || JSVAL_IS_NULL(s)) {
@@ -2009,18 +1995,18 @@ XPCConvert::JSStringWithSize2Native(XPCC
                     return JS_FALSE;
                 }
                 if (type.IsReference()) {
                     if (pErr)
                         *pErr = NS_ERROR_XPC_BAD_CONVERT_JS_NULL_REF;
                     return JS_FALSE;
                 }
 
-                if (0 != capacity) {
-                    len = (capacity + 1) * sizeof(char);
+                if (0 != count) {
+                    len = (count + 1) * sizeof(char);
                     if (!(*((void**)d) = nsMemory::Alloc(len)))
                         return JS_FALSE;
                     return JS_TRUE;
                 }
                 // else ...
 
                 *((char**)d) = nsnull;
                 return JS_TRUE;
@@ -2037,18 +2023,18 @@ XPCConvert::JSStringWithSize2Native(XPCC
             }
             if (length > count) {
                 if (pErr)
                     *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING;
                 return JS_FALSE;
             }
             len = PRUint32(length);
 
-            if (len < capacity)
-                len = capacity;
+            if (len < count)
+                len = count;
 
             JSUint32 alloc_len = (len + 1) * sizeof(char);
             char *buffer = static_cast<char *>(nsMemory::Alloc(alloc_len));
             if (!buffer) {
                 return JS_FALSE;
             }
             JS_EncodeStringToBuffer(str, buffer, len);
             buffer[len] = '\0';
@@ -2069,18 +2055,18 @@ XPCConvert::JSStringWithSize2Native(XPCC
                     return JS_FALSE;
                 }
                 if (type.IsReference()) {
                     if (pErr)
                         *pErr = NS_ERROR_XPC_BAD_CONVERT_JS_NULL_REF;
                     return JS_FALSE;
                 }
 
-                if (0 != capacity) {
-                    len = (capacity + 1) * sizeof(jschar);
+                if (0 != count) {
+                    len = (count + 1) * sizeof(jschar);
                     if (!(*((void**)d) = nsMemory::Alloc(len)))
                         return JS_FALSE;
                     return JS_TRUE;
                 }
 
                 // else ...
                 *((const jschar**)d) = nsnull;
                 return JS_TRUE;
@@ -2091,18 +2077,18 @@ XPCConvert::JSStringWithSize2Native(XPCC
             }
 
             len = JS_GetStringLength(str);
             if (len > count) {
                 if (pErr)
                     *pErr = NS_ERROR_XPC_NOT_ENOUGH_CHARS_IN_STRING;
                 return JS_FALSE;
             }
-            if (len < capacity)
-                len = capacity;
+            if (len < count)
+                len = count;
 
             if (!(chars = JS_GetStringCharsZ(cx, str))) {
                 return JS_FALSE;
             }
             JSUint32 alloc_len = (len + 1) * sizeof(jschar);
             if (!(*((void**)d) = nsMemory::Alloc(alloc_len))) {
                 // XXX should report error
                 return JS_FALSE;
--- a/js/xpconnect/src/XPCVariant.cpp
+++ b/js/xpconnect/src/XPCVariant.cpp
@@ -370,18 +370,17 @@ JSBool XPCVariant::InitializeData(XPCCal
 
         nsXPTType type;
         nsID id;
 
         if (!XPCArrayHomogenizer::GetTypeForArray(ccx, jsobj, len, &type, &id))
             return JS_FALSE;
 
         if (!XPCConvert::JSArray2Native(ccx, &mData.u.array.mArrayValue,
-                                        val, len, len,
-                                        type, &id, nsnull))
+                                        val, len, type, &id, nsnull))
             return JS_FALSE;
 
         mData.mType = nsIDataType::VTYPE_ARRAY;
         if (type.IsInterfacePointer())
             mData.u.array.mArrayInterfaceID = id;
         mData.u.array.mArrayCount = len;
         mData.u.array.mArrayType = type.TagPart();
 
--- a/js/xpconnect/src/XPCWrappedJSClass.cpp
+++ b/js/xpconnect/src/XPCWrappedJSClass.cpp
@@ -1672,25 +1672,24 @@ pre_call_clean_up:
                                            i, GET_LENGTH, nativeParams,
                                            &array_count))
                     break;
             }
 
             if (isArray) {
                 if (array_count &&
                     !XPCConvert::JSArray2Native(ccx, (void**)&pv->val, val,
-                                                array_count, array_count,
-                                                datum_type, &param_iid,
-                                                nsnull))
+                                                array_count, datum_type,
+                                                &param_iid, nsnull))
                     break;
             } else if (isSizedString) {
                 if (!XPCConvert::JSStringWithSize2Native(ccx,
                                                          (void*)&pv->val, val,
-                                                         array_count, array_count,
-                                                         datum_type, nsnull))
+                                                         array_count, datum_type,
+                                                         nsnull))
                     break;
             } else {
                 if (!XPCConvert::JSData2Native(ccx, &pv->val, val, type,
                                                JS_TRUE, &param_iid,
                                                nsnull))
                     break;
             }
         }
--- a/js/xpconnect/src/XPCWrappedNative.cpp
+++ b/js/xpconnect/src/XPCWrappedNative.cpp
@@ -2766,29 +2766,27 @@ CallMethodHelper::ConvertDependentParams
         if (isArray || isSizedString) {
             if (!GetArraySizeFromParam(i, &array_capacity) ||
                 !GetArrayLengthFromParam(i, &array_count))
                 return JS_FALSE;
 
             if (isArray) {
                 if (array_count &&
                     !XPCConvert::JSArray2Native(mCallContext, (void**)&dp->val, src,
-                                                array_count, array_capacity,
-                                                datum_type,
-                                                &param_iid, &err)) {
+                                                array_count, datum_type, &param_iid,
+                                                &err)) {
                     // XXX need exception scheme for arrays to indicate bad element
                     ThrowBadParam(err, i, mCallContext);
                     return JS_FALSE;
                 }
             } else // if (isSizedString)
             {
                 if (!XPCConvert::JSStringWithSize2Native(mCallContext,
                                                          (void*)&dp->val,
-                                                         src,
-                                                         array_count, array_capacity,
+                                                         src, array_count,
                                                          datum_type, &err)) {
                     ThrowBadParam(err, i, mCallContext);
                     return JS_FALSE;
                 }
             }
         } else {
             if (!XPCConvert::JSData2Native(mCallContext, &dp->val, src, type,
                                            JS_TRUE, &param_iid, &err)) {
--- a/js/xpconnect/src/xpcprivate.h
+++ b/js/xpconnect/src/xpcprivate.h
@@ -3314,29 +3314,28 @@ public:
      * @param pErr [out] relevant error code, if any.
      */
     static JSBool NativeArray2JS(XPCLazyCallContext& ccx,
                                  jsval* d, const void** s,
                                  const nsXPTType& type, const nsID* iid,
                                  JSUint32 count, nsresult* pErr);
 
     static JSBool JSArray2Native(XPCCallContext& ccx, void** d, jsval s,
-                                 JSUint32 count, JSUint32 capacity,
-                                 const nsXPTType& type, const nsID* iid,
-                                 uintN* pErr);
+                                 JSUint32 count, const nsXPTType& type,
+                                 const nsID* iid, uintN* pErr);
 
     static JSBool NativeStringWithSize2JS(JSContext* cx,
                                           jsval* d, const void* s,
                                           const nsXPTType& type,
                                           JSUint32 count,
                                           nsresult* pErr);
 
     static JSBool JSStringWithSize2Native(XPCCallContext& ccx, void* d, jsval s,
-                                          JSUint32 count, JSUint32 capacity,
-                                          const nsXPTType& type, uintN* pErr);
+                                          JSUint32 count, const nsXPTType& type,
+                                          uintN* pErr);
 
     static nsresult JSValToXPCException(XPCCallContext& ccx,
                                         jsval s,
                                         const char* ifaceName,
                                         const char* methodName,
                                         nsIException** exception);
 
     static nsresult JSErrorToXPCException(XPCCallContext& ccx,