Bug 1561911 - Part 2: Add SetPromiseIsHandled. r=jwalden
authorTooru Fujisawa <arai_a@mac.com>
Wed, 04 Dec 2019 09:46:58 +0000
changeset 505291 fc547f512ed850b4b618d69890b4fd0885257cac
parent 505290 c4b38fbe9db01dba1b52e02f5b81c135cfd6e63d
child 505292 4c5f5aad4fd76c7c4040f24d88f7b7695332c540
push id36881
push userdvarga@mozilla.com
push dateWed, 04 Dec 2019 16:22:31 +0000
treeherdermozilla-central@13fb375eaf14 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjwalden
bugs1561911
milestone73.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1561911 - Part 2: Add SetPromiseIsHandled. r=jwalden `SetPromiseIsHandled` performs "Set promise.[[PromiseIsHandled]] to true.", with removing unhandled rejection. Fixed the following steps to use `SetPromiseIsHandled`: * 3.8.4. ReadableStreamReaderGenericInitialize Step 6.c * 3.8.5. ReadableStreamReaderGenericRelease step 5 * 4.4.13. WritableStreamRejectCloseAndClosedPromiseIfNeeded step 4.b * 4.5.3. new WritableStreamDefaultWriter step 7.b * 4.5.3. new WritableStreamDefaultWriter step 9.d * 4.5.3. new WritableStreamDefaultWriter step 9.f * 4.6.5. WritableStreamDefaultWriterEnsureClosedPromiseRejected step 3 * 4.6.6. WritableStreamDefaultWriterEnsureReadyPromiseRejected step 3 Differential Revision: https://phabricator.services.mozilla.com/D55614
js/src/builtin/streams/MiscellaneousOperations-inl.h
js/src/builtin/streams/ReadableStreamReader.cpp
js/src/builtin/streams/WritableStreamDefaultWriter.cpp
js/src/builtin/streams/WritableStreamOperations.cpp
js/src/builtin/streams/WritableStreamWriterOperations.cpp
--- a/js/src/builtin/streams/MiscellaneousOperations-inl.h
+++ b/js/src/builtin/streams/MiscellaneousOperations-inl.h
@@ -105,11 +105,21 @@ inline MOZ_MUST_USE bool RejectUnwrapped
  * Reject the unwrapped promise |unwrappedPromise| with |error|.
  */
 inline MOZ_MUST_USE bool RejectUnwrappedPromiseWithError(
     JSContext* cx, JSObject* unwrappedPromise, JS::Handle<JS::Value> error) {
   JS::Rooted<JSObject*> promise(cx, unwrappedPromise);
   return RejectUnwrappedPromiseWithError(cx, &promise, error);
 }
 
+/**
+ * Sets Promise.[[PromiseIsHandled]] to true and removes rejection from
+ * the list of unhandled rejected promise.
+ */
+inline void SetPromiseIsHandled(JSContext* cx,
+                                JS::Handle<PromiseObject*> promise) {
+  promise->setHandled();
+  cx->runtime()->removeUnhandledRejectedPromise(cx, promise);
+}
+
 }  // namespace js
 
 #endif  // builtin_streams_MiscellaneousOperations_inl_h
--- a/js/src/builtin/streams/ReadableStreamReader.cpp
+++ b/js/src/builtin/streams/ReadableStreamReader.cpp
@@ -19,16 +19,17 @@
 #include "builtin/streams/ReadableStreamController.h"  // js::ReadableStreamController
 #include "builtin/streams/ReadableStreamInternals.h"  // js::ReadableStream{Cancel,CreateReadResult}
 #include "js/RootingAPI.h"                            // JS::Handle, JS::Rooted
 #include "js/Value.h"        // JS::Value, JS::UndefinedHandleValue
 #include "vm/Interpreter.h"  // js::GetAndClearException
 #include "vm/JSContext.h"    // JSContext
 #include "vm/Runtime.h"      // JSRuntime
 
+#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::SetPromiseIsHandled
 #include "vm/Compartment-inl.h"  // JS::Compartment::wrap, js::UnwrapInternalSlot
 #include "vm/List-inl.h"         // js::StoreNewListInFixedSlot
 #include "vm/Realm-inl.h"        // js::AutoRealm
 
 using js::ReadableStreamController;
 using js::UnwrapStreamFromReader;
 
 using JS::Handle;
@@ -106,18 +107,17 @@ MOZ_MUST_USE bool js::ReadableStreamRead
       return false;
     }
     promise = PromiseObject::unforgeableReject(cx, storedError);
     if (!promise) {
       return false;
     }
 
     // Step c. Set reader.[[closedPromise]].[[PromiseIsHandled]] to true.
-    promise->as<PromiseObject>().setHandled();
-    cx->runtime()->removeUnhandledRejectedPromise(cx, promise);
+    SetPromiseIsHandled(cx, promise.as<PromiseObject>());
   }
 
   if (!promise) {
     return false;
   }
 
   reader->setClosedPromise(promise);
 
@@ -205,18 +205,17 @@ MOZ_MUST_USE bool js::ReadableStreamRead
     AutoRealm ar(cx, unwrappedReader);
     if (!cx->compartment()->wrap(cx, &closedPromise)) {
       return false;
     }
     unwrappedReader->setClosedPromise(closedPromise);
   }
 
   // Step 5: Set reader.[[closedPromise]].[[PromiseIsHandled]] to true.
-  unwrappedClosedPromise->setHandled();
-  cx->runtime()->removeUnhandledRejectedPromise(cx, unwrappedClosedPromise);
+  SetPromiseIsHandled(cx, unwrappedClosedPromise);
 
   // Step 6: Set reader.[[ownerReadableStream]].[[reader]] to undefined.
   unwrappedStream->clearReader();
 
   // Step 7: Set reader.[[ownerReadableStream]] to undefined.
   unwrappedReader->clearStream();
 
   return true;
--- a/js/src/builtin/streams/WritableStreamDefaultWriter.cpp
+++ b/js/src/builtin/streams/WritableStreamDefaultWriter.cpp
@@ -23,16 +23,17 @@
 #include "js/CallArgs.h"  // JS::CallArgs{,FromVp}
 #include "js/Class.h"     // js::ClassSpec, JS_NULL_CLASS_OPS
 #include "js/PropertySpec.h"  // JS{Function,Property}Spec, JS_{FS,PS}_END, JS_{FN,PSG}
 #include "js/RootingAPI.h"   // JS::Handle
 #include "js/Value.h"        // JS::Value
 #include "vm/Compartment.h"  // JS::Compartment
 #include "vm/JSContext.h"    // JSContext
 
+#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::SetPromiseIsHandled
 #include "vm/Compartment-inl.h"  // JS::Compartment::wrap, js::UnwrapAndTypeCheck{Argument,This}
 #include "vm/JSObject-inl.h"      // js::NewObjectWithClassProto
 #include "vm/NativeObject-inl.h"  // js::ThrowIfNotConstructing
 #include "vm/Realm-inl.h"         // js::AutoRealm
 
 using JS::CallArgs;
 using JS::CallArgsFromVp;
 using JS::Handle;
@@ -132,27 +133,26 @@ MOZ_MUST_USE WritableStreamDefaultWriter
     if (!cx->compartment()->wrap(cx, &storedError)) {
       return nullptr;
     }
 
     // Step 7: Otherwise, if state is "erroring",
     if (unwrappedStream->erroring()) {
       // Step 7.a: Set this.[[readyPromise]] to a promise rejected with
       //           stream.[[storedError]].
-      JSObject* promise = PromiseObject::unforgeableReject(cx, storedError);
+      Rooted<JSObject*> promise(
+          cx, PromiseObject::unforgeableReject(cx, storedError));
       if (!promise) {
         return nullptr;
       }
 
       writer->setReadyPromise(promise);
 
       // Step 7.b: Set this.[[readyPromise]].[[PromiseIsHandled]] to true.
-      Rooted<PromiseObject*> readyPromise(cx, &promise->as<PromiseObject>());
-      readyPromise->setHandled();
-      cx->runtime()->removeUnhandledRejectedPromise(cx, readyPromise);
+      SetPromiseIsHandled(cx, promise.as<PromiseObject>());
 
       // Step 7.c: Set this.[[closedPromise]] to a new promise.
       JSObject* closedPromise = PromiseObject::createSkippingExecutor(cx);
       if (!closedPromise) {
         return nullptr;
       }
 
       writer->setClosedPromise(closedPromise);
@@ -170,31 +170,29 @@ MOZ_MUST_USE WritableStreamDefaultWriter
       promise = PromiseObject::unforgeableReject(cx, storedError);
       if (!promise) {
         return nullptr;
       }
 
       writer->setReadyPromise(promise);
 
       // Step 9.d: Set this.[[readyPromise]].[[PromiseIsHandled]] to true.
-      promise->as<PromiseObject>().setHandled();
-      cx->runtime()->removeUnhandledRejectedPromise(cx, promise);
+      SetPromiseIsHandled(cx, promise.as<PromiseObject>());
 
       // Step 9.e: Set this.[[closedPromise]] to a promise rejected with
       //           storedError.
       promise = PromiseObject::unforgeableReject(cx, storedError);
       if (!promise) {
         return nullptr;
       }
 
       writer->setClosedPromise(promise);
 
       // Step 9.f: Set this.[[closedPromise]].[[PromiseIsHandled]] to true.
-      promise->as<PromiseObject>().setHandled();
-      cx->runtime()->removeUnhandledRejectedPromise(cx, promise);
+      SetPromiseIsHandled(cx, promise.as<PromiseObject>());
     }
   }
 
   // Step 4 (reordered): Set stream.[[writer]] to this.
   // Doing this last prevents a partially-initialized writer from being attached
   // to the stream (and possibly left there on OOM).
   {
     AutoRealm ar(cx, unwrappedStream);
--- a/js/src/builtin/streams/WritableStreamOperations.cpp
+++ b/js/src/builtin/streams/WritableStreamOperations.cpp
@@ -26,17 +26,17 @@
 #include "js/Promise.h"     // JS::{Reject,Resolve}Promise
 #include "js/RootingAPI.h"  // JS::Handle, JS::Rooted
 #include "js/Value.h"  // JS::Value, JS::ObjecValue, JS::UndefinedHandleValue
 #include "vm/Compartment.h"  // JS::Compartment
 #include "vm/JSContext.h"    // JSContext
 #include "vm/List.h"         // js::ListObject
 
 #include "builtin/streams/HandlerFunction-inl.h"  // js::NewHandler, js::TargetFromHandler
-#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::ResolveUnwrappedPromiseWithUndefined, js::RejectUnwrappedPromiseWithError
+#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::ResolveUnwrappedPromiseWithUndefined, js::RejectUnwrappedPromiseWithError, js::SetPromiseIsHandled
 #include "builtin/streams/WritableStream-inl.h"  // js::UnwrapWriterFromStream
 #include "builtin/streams/WritableStreamDefaultWriter-inl.h"  // js::WritableStreamDefaultWriter::closedPromise
 #include "vm/Compartment-inl.h"  // JS::Compartment::wrap, js::UnwrapAndDowncastObject
 #include "vm/JSContext-inl.h"  // JSContext::check
 #include "vm/JSObject-inl.h"   // js::NewObjectWithClassProto
 #include "vm/List-inl.h"       // js::{AppendTo,StoreNew}ListInFixedSlot
 #include "vm/Realm-inl.h"      // js::AutoRealm
 
@@ -857,18 +857,17 @@ MOZ_MUST_USE bool js::WritableStreamReje
     // Step 4.b: Set writer.[[closedPromise]].[[PromiseIsHandled]] to true.
     Rooted<PromiseObject*> unwrappedClosedPromise(
         cx, UnwrapAndDowncastObject<PromiseObject>(
                 cx, unwrappedWriter->closedPromise()));
     if (!unwrappedClosedPromise) {
       return false;
     }
 
-    unwrappedClosedPromise->setHandled();
-    cx->runtime()->removeUnhandledRejectedPromise(cx, unwrappedClosedPromise);
+    SetPromiseIsHandled(cx, unwrappedClosedPromise);
   }
 
   return true;
 }
 
 /**
  * Streams spec, 4.4.14.
  *      WritableStreamUpdateBackpressure ( stream, backpressure )
--- a/js/src/builtin/streams/WritableStreamWriterOperations.cpp
+++ b/js/src/builtin/streams/WritableStreamWriterOperations.cpp
@@ -22,17 +22,17 @@
 #include "builtin/streams/WritableStreamDefaultWriter.h"  // js::WritableStreamDefaultWriter
 #include "builtin/streams/WritableStreamOperations.h"  // js::WritableStream{Abort,CloseQueuedOrInFlight}
 #include "js/Promise.h"                                // JS::PromiseState
 #include "js/Value.h"        // JS::Value, JS::{Int32,Null}Value
 #include "vm/Compartment.h"  // JS::Compartment
 #include "vm/Interpreter.h"  // js::GetAndClearException
 #include "vm/JSContext.h"    // JSContext
 
-#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::ResolveUnwrappedPromiseWithUndefined
+#include "builtin/streams/MiscellaneousOperations-inl.h"  // js::ResolveUnwrappedPromiseWithUndefined, js::SetPromiseIsHandled
 #include "builtin/streams/WritableStream-inl.h"  // js::WritableStream::setCloseRequest
 #include "builtin/streams/WritableStreamDefaultWriter-inl.h"  // js::UnwrapStreamFromWriter
 #include "vm/Compartment-inl.h"  // js::UnwrapAnd{DowncastObject,TypeCheckThis}
 #include "vm/JSContext-inl.h"    // JSContext::check
 #include "vm/Realm-inl.h"        // js::AutoRealm
 
 using JS::Handle;
 using JS::Int32Value;
@@ -180,18 +180,17 @@ static bool EnsurePromiseRejected(
 
     // Directly-unobservable rejected promises aren't collapsed like resolved
     // promises, and this promise is created in the current realm, so it's
     // always an actual Promise.
     unwrappedPromise = &rejectedWithError->as<PromiseObject>();
   }
 
   // 4.6.{5,6} step 3: Set writer.[[<field>]].[[PromiseIsHandled]] to true.
-  unwrappedPromise->setHandled();
-  cx->runtime()->removeUnhandledRejectedPromise(cx, unwrappedPromise);
+  SetPromiseIsHandled(cx, unwrappedPromise);
   return true;
 }
 
 /**
  * Streams spec, 4.6.5.
  *  WritableStreamDefaultWriterEnsureClosedPromiseRejected( writer, error )
  */
 MOZ_MUST_USE bool js::WritableStreamDefaultWriterEnsureClosedPromiseRejected(