Merge birch to m-c.
authorRyan VanderMeulen <ryanvm@gmail.com>
Mon, 22 Apr 2013 21:15:00 -0400
changeset 129468 f59811fe0fb3d303fd20abc4e10432488c3d3db5
parent 129465 52864fd204f38743013ba7a4f44372136ec64a43 (current diff)
parent 129467 a991d0f4469bc71103f9409090973e43c5483566 (diff)
child 129495 acf388eaf9e91f948ac95131c8860fdb8aecd68d
push idunknown
push userunknown
push dateunknown
milestone23.0a1
Merge birch to m-c.
--- a/dom/network/src/TCPSocket.js
+++ b/dom/network/src/TCPSocket.js
@@ -558,17 +558,19 @@ TCPSocket.prototype = {
     }
 
     this.callListener("close");
   },
 
   // nsIStreamListener (Triggered by _inputStreamPump.asyncRead)
   onDataAvailable: function ts_onDataAvailable(request, context, inputStream, offset, count) {
     if (this._binaryType === "arraybuffer") {
-      this.callListener("data", this._inputStreamBinary.readArrayBuffer(count));
+      let buffer = new (this.useWin ? this.useWin.ArrayBuffer : ArrayBuffer)(count);
+      this._inputStreamBinary.readArrayBuffer(count, buffer);
+      this.callListener("data", buffer);
     } else {
       this.callListener("data", this._inputStreamScriptable.read(count));
     }
   },
 
   classID: Components.ID("{cda91b22-6472-11e1-aa11-834fec09cd0a}"),
 
   classInfo: XPCOMUtils.generateCI({
--- a/netwerk/base/src/ArrayBufferInputStream.cpp
+++ b/netwerk/base/src/ArrayBufferInputStream.cpp
@@ -6,40 +6,54 @@
 #include <algorithm>
 #include "ArrayBufferInputStream.h"
 #include "nsStreamUtils.h"
 #include "jsfriendapi.h"
 
 NS_IMPL_ISUPPORTS2(ArrayBufferInputStream, nsIArrayBufferInputStream, nsIInputStream);
 
 ArrayBufferInputStream::ArrayBufferInputStream()
-: mBuffer(nullptr)
+: mRt(nullptr)
+, mArrayBuffer(JSVAL_VOID)
+, mBuffer(nullptr)
 , mBufferLength(0)
 , mOffset(0)
 , mClosed(false)
 {
 }
 
+ArrayBufferInputStream::~ArrayBufferInputStream()
+{
+  if (mRt) {
+    JS_RemoveValueRootRT(mRt, &mArrayBuffer);
+  }
+}
+
 NS_IMETHODIMP
 ArrayBufferInputStream::SetData(const JS::Value& aBuffer,
                                 uint32_t aByteOffset,
                                 uint32_t aLength,
                                 JSContext* aCx)
 {
   if (!aBuffer.isObject()) {
     return NS_ERROR_FAILURE;
   }
-  mArrayBuffer.construct(aCx, &aBuffer.toObject());
-  if (!JS_IsArrayBufferObject(mArrayBuffer.ref())) {
+  JS::RootedObject arrayBuffer(aCx, &aBuffer.toObject());
+  if (!JS_IsArrayBufferObject(arrayBuffer)) {
     return NS_ERROR_FAILURE;
   }
-  uint32_t buflen = JS_GetArrayBufferByteLength(mArrayBuffer.ref());
+
+  mRt = JS_GetRuntime(aCx);
+  mArrayBuffer = aBuffer;
+  JS_AddNamedValueRootRT(mRt, &mArrayBuffer, "mArrayBuffer");
+
+  uint32_t buflen = JS_GetArrayBufferByteLength(arrayBuffer);
   mOffset = std::min(buflen, aByteOffset);
   mBufferLength = std::min(buflen - mOffset, aLength);
-  mBuffer = JS_GetArrayBufferData(mArrayBuffer.ref());
+  mBuffer = JS_GetArrayBufferData(arrayBuffer);
   return NS_OK;
 }
 
 NS_IMETHODIMP
 ArrayBufferInputStream::Close()
 {
   mClosed = true;
   return NS_OK;
--- a/netwerk/base/src/ArrayBufferInputStream.h
+++ b/netwerk/base/src/ArrayBufferInputStream.h
@@ -17,22 +17,23 @@
     0xaa1c,                                          \
     0x41db,                                          \
     {0x87, 0xd0, 0x48, 0x76, 0x4a, 0x37, 0x10, 0xf6} \
 }
 
 class ArrayBufferInputStream : public nsIArrayBufferInputStream {
 public:
   ArrayBufferInputStream();
-  virtual ~ArrayBufferInputStream() {}
+  virtual ~ArrayBufferInputStream();
   NS_DECL_ISUPPORTS
   NS_DECL_NSIARRAYBUFFERINPUTSTREAM
   NS_DECL_NSIINPUTSTREAM
 
 private:
-  mozilla::Maybe<JS::RootedObject> mArrayBuffer;
+  JSRuntime* mRt;
+  jsval mArrayBuffer;
   uint8_t* mBuffer;
   uint32_t mBufferLength;
   uint32_t mOffset;
   bool mClosed;
 };
 
 #endif // ArrayBufferInputStream_h
--- a/xpcom/io/nsBinaryStream.cpp
+++ b/xpcom/io/nsBinaryStream.cpp
@@ -714,36 +714,38 @@ nsBinaryInputStream::ReadBytes(uint32_t 
 
 NS_IMETHODIMP
 nsBinaryInputStream::ReadByteArray(uint32_t aLength, uint8_t* *_rval)
 {
     return ReadBytes(aLength, reinterpret_cast<char **>(_rval));
 }
 
 NS_IMETHODIMP
-nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, JSContext* cx, JS::Value* _rval)
+nsBinaryInputStream::ReadArrayBuffer(uint32_t aLength, const JS::Value& aBuffer, JSContext* cx)
 {
     JSAutoRequest ar(cx);
-    JS::RootedObject buffer(cx, JS_NewArrayBuffer(cx, aLength));
-    if (!buffer) {
+    if (!aBuffer.isObject()) {
         return NS_ERROR_FAILURE;
     }
-    uint8_t* data = JS_GetArrayBufferData(buffer);
+    JS::RootedObject buffer(cx, &aBuffer.toObject());
+    if (!JS_IsArrayBufferObject(buffer) ||
+        JS_GetArrayBufferByteLength(buffer) < aLength) {
+        return NS_ERROR_FAILURE;
+    }
+    uint8_t* data = JS_GetArrayBufferData(&aBuffer.toObject());
     if (!data) {
         return NS_ERROR_FAILURE;
     }
 
     uint32_t bytesRead;
     nsresult rv = Read(reinterpret_cast<char*>(data), aLength, &bytesRead);
     NS_ENSURE_SUCCESS(rv, rv);
     if (bytesRead != aLength) {
         return NS_ERROR_FAILURE;
     }
-
-    *_rval = JS::ObjectValue(*buffer);
     return NS_OK;
 }
 
 NS_IMETHODIMP
 nsBinaryInputStream::ReadObject(bool aIsStrongRef, nsISupports* *aObject)
 {
     nsCID cid;
     nsIID iid;
--- a/xpcom/io/nsIBinaryInputStream.idl
+++ b/xpcom/io/nsIBinaryInputStream.idl
@@ -68,21 +68,24 @@ interface nsIBinaryInputStream : nsIInpu
      */
     void readByteArray(in uint32_t aLength,
                        [array, size_is(aLength), retval] out uint8_t aBytes);
 
     /**
      * Read opaque bytes from the stream, storing the results in an ArrayBuffer.
      *
      * @param aLength the number of bytes that must be read
+     * @param aArrayBuffer the arraybuffer in which to store the results
+     * Note: passing view.buffer, where view is an ArrayBufferView of an
+     *       ArrayBuffer, is not valid unless view.byteOffset == 0.
      *
      * @throws NS_ERROR_FAILURE if it can't read aLength bytes
      */
     [implicit_jscontext]
-    jsval readArrayBuffer(in uint32_t aLength);
+    void readArrayBuffer(in uint32_t aLength, in jsval aArrayBuffer);
 };
 
 %{C++
 
 #ifdef MOZILLA_INTERNAL_API
 #include "nsString.h"
 
 inline nsresult