Bug 1501387 - Handlify Streams.h and avoid using `const JSObject*`. r=tcampbell
authorJason Orendorff <jorendorff@mozilla.com>
Tue, 23 Oct 2018 19:02:04 +0000
changeset 491008 9ca97be22a381d0eb8d9a53931f18e37dd50baaf
parent 491007 9acfa4df8ac9b091bc9d5204d0bf4f36ccd7c485
child 491009 59205b705c5cbba29276d8a342179ff0caebf071
push id247
push userfmarier@mozilla.com
push dateSat, 27 Oct 2018 01:06:44 +0000
reviewerstcampbell
bugs1501387
milestone65.0a1
Bug 1501387 - Handlify Streams.h and avoid using `const JSObject*`. r=tcampbell Differential Revision: https://phabricator.services.mozilla.com/D9566
js/public/Stream.h
js/src/jsapi.cpp
--- a/js/public/Stream.h
+++ b/js/public/Stream.h
@@ -215,17 +215,17 @@ NewReadableExternalSourceStreamObject(JS
  * when creating the given stream.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  *
  * Asserts that the given stream has an embedding-provided underlying source.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamGetEmbeddingFlags(JSContext* cx, const JSObject* stream, uint8_t* flags);
+ReadableStreamGetEmbeddingFlags(JSContext* cx, HandleObject stream, uint8_t* flags);
 
 /**
  * Returns the embedding-provided underlying source of the given |stream|.
  *
  * Can be used to optimize operations if both the underlying source and the
  * intended sink are embedding-provided. In that case it might be
  * preferrable to pipe data directly from source to sink without interacting
  * with the stream at all.
@@ -257,17 +257,17 @@ ReadableStreamGetExternalUnderlyingSourc
  * ReadableStreamGetExternalUnderlyingSource.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  *
  * Asserts that the stream has an embedding-provided underlying source.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamReleaseExternalUnderlyingSource(JSContext* cx, JSObject* stream);
+ReadableStreamReleaseExternalUnderlyingSource(JSContext* cx, HandleObject stream);
 
 /**
  * Update the amount of data available at the underlying source of the given
  * |stream|.
  *
  * Can only be used for streams with an embedding-provided underlying source.
  * The JS engine will use the given value to satisfy read requests for the
  * stream by invoking the JS::WriteIntoReadRequestBuffer callback.
@@ -279,80 +279,80 @@ extern JS_PUBLIC_API(bool)
 ReadableStreamUpdateDataAvailableFromSource(JSContext* cx, HandleObject stream,
                                             uint32_t availableData);
 
 /**
  * Returns true if the given object is a ReadableStream object or an
  * unwrappable wrapper for one, false otherwise.
  */
 extern JS_PUBLIC_API(bool)
-IsReadableStream(const JSObject* obj);
+IsReadableStream(JSObject* obj);
 
 /**
  * Returns true if the given object is a ReadableStreamDefaultReader or
  * ReadableStreamBYOBReader object or an unwrappable wrapper for one, false
  * otherwise.
  */
 extern JS_PUBLIC_API(bool)
-IsReadableStreamReader(const JSObject* obj);
+IsReadableStreamReader(JSObject* obj);
 
 /**
  * Returns true if the given object is a ReadableStreamDefaultReader object
  * or an unwrappable wrapper for one, false otherwise.
  */
 extern JS_PUBLIC_API(bool)
-IsReadableStreamDefaultReader(const JSObject* obj);
+IsReadableStreamDefaultReader(JSObject* obj);
 
 enum class ReadableStreamMode {
     Default,
     Byte,
     ExternalSource
 };
 
 /**
  * Returns the stream's ReadableStreamMode. If the mode is |Byte| or
  * |ExternalSource|, it's possible to acquire a BYOB reader for more optimized
  * operations.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamGetMode(JSContext* cx, const JSObject* stream, ReadableStreamMode* mode);
+ReadableStreamGetMode(JSContext* cx, HandleObject stream, ReadableStreamMode* mode);
 
 enum class ReadableStreamReaderMode {
     Default
 };
 
 /**
  * Returns true if the given ReadableStream is readable, false if not.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamIsReadable(JSContext* cx, const JSObject* stream, bool* result);
+ReadableStreamIsReadable(JSContext* cx, HandleObject stream, bool* result);
 
 /**
  * Returns true if the given ReadableStream is locked, false if not.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamIsLocked(JSContext* cx, const JSObject* stream, bool* result);
+ReadableStreamIsLocked(JSContext* cx, HandleObject stream, bool* result);
 
 /**
  * Returns true if the given ReadableStream is disturbed, false if not.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamIsDisturbed(JSContext* cx, const JSObject* stream, bool* result);
+ReadableStreamIsDisturbed(JSContext* cx, HandleObject stream, bool* result);
 
 /**
  * Cancels the given ReadableStream with the given reason and returns a
  * Promise resolved according to the result.
  *
  * Asserts that |stream| is a ReadableStream object or an unwrappable wrapper
  * for one.
  */
@@ -417,17 +417,17 @@ ReadableStreamClose(JSContext* cx, Handl
 
 /**
  * Returns true if the given ReadableStream reader is locked, false otherwise.
  *
  * Asserts that |reader| is a ReadableStreamDefaultReader or
  * ReadableStreamBYOBReader object or an unwrappable wrapper for one.
  */
 extern JS_PUBLIC_API(bool)
-ReadableStreamReaderIsClosed(JSContext* cx, const JSObject* reader, bool* result);
+ReadableStreamReaderIsClosed(JSContext* cx, HandleObject reader, bool* result);
 
 /**
  * Enqueues the given chunk in the given ReadableStream.
  *
  * Throws a TypeError and returns false if the enqueing operation fails.
  *
  * Note: This is semantically equivalent to the |enqueue| method on
  * the stream controller's prototype in JS. We expose it with the stream
--- a/js/src/jsapi.cpp
+++ b/js/src/jsapi.cpp
@@ -4712,36 +4712,39 @@ JS::NewReadableExternalSourceStreamObjec
     MOZ_ASSERT(rt->readableStreamErroredCallback);
     MOZ_ASSERT(rt->readableStreamFinalizeCallback);
 #endif // DEBUG
 
     return ReadableStream::createExternalSourceStream(cx, underlyingSource, flags, proto);
 }
 
 JS_PUBLIC_API(bool)
-JS::IsReadableStream(const JSObject* obj)
-{
-    if (IsWrapper(const_cast<JSObject*>(obj)))
-        obj = CheckedUnwrap(const_cast<JSObject*>(obj));
+JS::IsReadableStream(JSObject* obj)
+{
+    if (IsWrapper(obj)) {
+        obj = CheckedUnwrap(obj);
+    }
     return obj && obj->is<ReadableStream>();
 }
 
 JS_PUBLIC_API(bool)
-JS::IsReadableStreamReader(const JSObject* obj)
-{
-    if (IsWrapper(const_cast<JSObject*>(obj)))
-        obj = CheckedUnwrap(const_cast<JSObject*>(obj));
+JS::IsReadableStreamReader(JSObject* obj)
+{
+    if (IsWrapper(obj)) {
+        obj = CheckedUnwrap(obj);
+    }
     return obj && obj->is<ReadableStreamDefaultReader>();
 }
 
 JS_PUBLIC_API(bool)
-JS::IsReadableStreamDefaultReader(const JSObject* obj)
-{
-    if (IsWrapper(const_cast<JSObject*>(obj)))
-        obj = CheckedUnwrap(const_cast<JSObject*>(obj));
+JS::IsReadableStreamDefaultReader(JSObject* obj)
+{
+    if (IsWrapper(obj)) {
+        obj = CheckedUnwrap(obj);
+    }
     return obj && obj->is<ReadableStreamDefaultReader>();
 }
 
 template<class T>
 static MOZ_MUST_USE T*
 ToUnwrapped(JSContext* cx, JSObject* obj)
 {
     cx->check(obj);
@@ -4751,52 +4754,52 @@ ToUnwrapped(JSContext* cx, JSObject* obj
             ReportAccessDenied(cx);
             return nullptr;
         }
     }
     return &obj->as<T>();
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamIsReadable(JSContext* cx, const JSObject* streamObj, bool* result)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamIsReadable(JSContext* cx, HandleObject streamObj, bool* result)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     *result = stream->readable();
     return true;
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamIsLocked(JSContext* cx, const JSObject* streamObj, bool* result)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamIsLocked(JSContext* cx, HandleObject streamObj, bool* result)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     *result = stream->locked();
     return true;
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamIsDisturbed(JSContext* cx, const JSObject* streamObj, bool* result)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamIsDisturbed(JSContext* cx, HandleObject streamObj, bool* result)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     *result = stream->disturbed();
     return true;
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamGetEmbeddingFlags(JSContext* cx, const JSObject* streamObj, uint8_t* flags)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamGetEmbeddingFlags(JSContext* cx, HandleObject streamObj, uint8_t* flags)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     *flags = stream->embeddingFlags();
     return true;
 }
 
 JS_PUBLIC_API(JSObject*)
@@ -4809,19 +4812,19 @@ JS::ReadableStreamCancel(JSContext* cx, 
     Rooted<ReadableStream*> stream(cx, ToUnwrapped<ReadableStream>(cx, streamObj));
     if (!stream)
         return nullptr;
 
     return ReadableStream::cancel(cx, stream, reason);
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamGetMode(JSContext* cx, const JSObject* streamObj, JS::ReadableStreamMode* mode)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamGetMode(JSContext* cx, HandleObject streamObj, JS::ReadableStreamMode* mode)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     *mode = stream->mode();
     return true;
 }
 
 JS_PUBLIC_API(JSObject*)
@@ -4848,19 +4851,19 @@ JS::ReadableStreamGetExternalUnderlyingS
     Rooted<ReadableStream*> stream(cx, ToUnwrapped<ReadableStream>(cx, streamObj));
     if (!stream)
         return false;
 
     return ReadableStream::getExternalSource(cx, stream, source);
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamReleaseExternalUnderlyingSource(JSContext* cx, JSObject* streamObj)
-{
-    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, const_cast<JSObject*>(streamObj));
+JS::ReadableStreamReleaseExternalUnderlyingSource(JSContext* cx, HandleObject streamObj)
+{
+    ReadableStream* stream = ToUnwrapped<ReadableStream>(cx, streamObj);
     if (!stream)
         return false;
 
     stream->releaseExternalSource();
     return true;
 }
 
 JS_PUBLIC_API(bool)
@@ -4955,19 +4958,19 @@ JS::ReadableStreamError(JSContext* cx, H
     Rooted<ReadableStream*> stream(cx, ToUnwrapped<ReadableStream>(cx, streamObj));
     if (!stream)
         return false;
 
     return js::ReadableStream::error(cx, stream, error);
 }
 
 JS_PUBLIC_API(bool)
-JS::ReadableStreamReaderIsClosed(JSContext* cx, const JSObject* reader, bool* result)
-{
-    reader = ToUnwrapped<NativeObject>(cx, const_cast<JSObject*>(reader));
+JS::ReadableStreamReaderIsClosed(JSContext* cx, HandleObject readerObj, bool* result)
+{
+    RootedObject reader(cx, ToUnwrapped<NativeObject>(cx, readerObj));
     if (!reader)
         return false;
 
     *result = js::ReadableStreamReaderIsClosed(reader);
     return true;
 }
 
 JS_PUBLIC_API(bool)