Bug 1811538 - Part 2: Use Promise::CreateInfallible in dom/streams r=smaug

Differential Revision: https://phabricator.services.mozilla.com/D167697
This commit is contained in:
Kagami Sascha Rosylight 2023-01-31 19:21:35 +00:00
parent a98aec7bdc
commit ea0cb48ccd
12 changed files with 40 additions and 111 deletions

View File

@ -1895,10 +1895,8 @@ void SetUpReadableByteStreamController(
}
// Let startPromise be a promise resolved with startResult.
RefPtr<Promise> startPromise = Promise::Create(GetIncumbentGlobal(), aRv);
if (aRv.Failed()) {
return;
}
RefPtr<Promise> startPromise =
Promise::CreateInfallible(GetIncumbentGlobal());
startPromise->MaybeResolve(startResult);
// Step 16+17

View File

@ -331,11 +331,8 @@ already_AddRefed<Promise> ReadableStreamCancel(JSContext* aCx,
// Step 2.
if (aStream->State() == ReadableStream::ReaderState::Closed) {
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
promise->MaybeResolveWithUndefined();
return promise.forget();
}
@ -387,10 +384,7 @@ already_AddRefed<Promise> ReadableStreamCancel(JSContext* aCx,
// Step 8.
RefPtr<Promise> promise =
Promise::Create(sourceCancelPromise->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
Promise::CreateInfallible(sourceCancelPromise->GetParentObject());
// ThenWithCycleCollectedArgs will carry promise, keeping it alive until the
// callback executes.
@ -894,10 +888,7 @@ already_AddRefed<Promise> ReadableStream::GetNextIterationResult(
MOZ_ASSERT(reader->GetStream());
// Step 3. Let promise be a new promise.
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise = Promise::CreateInfallible(GetParentObject());
// Step 4. Let readRequest be a new read request with the following items:
RefPtr<ReadRequest> request = new IteratorReadRequest(promise, reader);

View File

@ -255,10 +255,7 @@ already_AddRefed<Promise> ReadableStreamBYOBReader::Read(
}
// Step 5.
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise = Promise::CreateInfallible(GetParentObject());
// Step 6. Let readIntoRequest be a new read-into request with the following
// items:

View File

@ -507,10 +507,7 @@ void SetUpReadableStreamDefaultController(
// Step 10.
RefPtr<Promise> startPromise =
Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aStream->GetParentObject());
startPromise->MaybeResolve(startResult);
// Step 11 & 12:

View File

@ -67,10 +67,8 @@ bool ReadableStreamReaderGenericInitialize(ReadableStreamGenericReader* aReader,
// Step 2.
aStream->SetReader(aReader);
aReader->SetClosedPromise(Promise::Create(aReader->GetParentObject(), aRv));
if (aRv.Failed()) {
return false;
}
aReader->SetClosedPromise(
Promise::CreateInfallible(aReader->GetParentObject()));
switch (aStream->State()) {
// Step 3.
@ -247,7 +245,7 @@ already_AddRefed<Promise> ReadableStreamDefaultReader::Read(ErrorResult& aRv) {
}
// Step 2.
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aRv);
RefPtr<Promise> promise = Promise::CreateInfallible(GetParentObject());
// Step 3.
RefPtr<ReadRequest> request = new Read_ReadRequest(promise);

View File

@ -941,10 +941,8 @@ already_AddRefed<Promise> ReadableStreamPipeTo(
// Note: PipeToPump ensures this by construction.
// Step 13. Let promise be a new promise.
RefPtr<Promise> promise = Promise::Create(aSource->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aSource->GetParentObject());
// Steps 14-15.
RefPtr<PipeToPump> pump = new PipeToPump(

View File

@ -47,10 +47,7 @@ already_AddRefed<TeeState> TeeState::Create(ReadableStream* aStream,
teeState->SetReader(reader);
RefPtr<Promise> promise =
Promise::Create(teeState->GetStream()->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
Promise::CreateInfallible(teeState->GetStream()->GetParentObject());
teeState->SetCancelPromise(promise);
return teeState.forget();

View File

@ -178,7 +178,8 @@ class SetUpTransformWritableMessageEventListener final
Promise* BackpressurePromise() { return mBackpressurePromise; }
void CreateBackpressurePromise(ErrorResult& aRv) {
mBackpressurePromise = Promise::Create(mController->GetParentObject(), aRv);
mBackpressurePromise =
Promise::CreateInfallible(mController->GetParentObject());
}
private:
@ -449,10 +450,7 @@ MOZ_CAN_RUN_SCRIPT static void SetUpCrossRealmTransformWritable(
// Step 3: Let backpressurePromise be a new promise.
RefPtr<Promise> backpressurePromise =
Promise::Create(aWritable->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWritable->GetParentObject());
// Step 4: Add a handler for ports message event with the following steps:
auto listener = MakeRefPtr<SetUpTransformWritableMessageEventListener>(

View File

@ -517,10 +517,7 @@ void TransformStream::SetBackpressure(bool aBackpressure, ErrorResult& aRv) {
}
// Step 3. Set stream.[[backpressureChangePromise]] to a new promise.
RefPtr<Promise> promise = Promise::Create(GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
RefPtr<Promise> promise = Promise::CreateInfallible(GetParentObject());
mBackpressureChangePromise = promise;
// Step 4. Set stream.[[backpressure]] to backpressure.
@ -657,10 +654,7 @@ already_AddRefed<TransformStream> TransformStream::Constructor(
// Step 9. Let startPromise be a new promise.
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
RefPtr<Promise> startPromise = Promise::Create(global, aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> startPromise = Promise::CreateInfallible(global);
// Step 10. Perform ! InitializeTransformStream(this, startPromise,
// writableHighWaterMark, writableSizeAlgorithm, readableHighWaterMark,

View File

@ -422,10 +422,8 @@ void WritableStream::UpdateBackpressure(bool aBackpressure, ErrorResult& aRv) {
// Step 4.1. If backpressure is true, set writer.[[readyPromise]] to a new
// promise.
if (aBackpressure) {
RefPtr<Promise> promise = Promise::Create(writer->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(writer->GetParentObject());
writer->SetReadyPromise(promise);
} else {
// Step 4.2. Otherwise,
@ -513,10 +511,8 @@ already_AddRefed<Promise> WritableStreamAbort(JSContext* aCx,
// resolved with undefined.
if (aStream->State() == WritableStream::WriterState::Closed ||
aStream->State() == WritableStream::WriterState::Errored) {
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
promise->MaybeResolveWithUndefined();
return promise.forget();
}
@ -533,10 +529,8 @@ already_AddRefed<Promise> WritableStreamAbort(JSContext* aCx,
// code and that might have changed the state.
if (aStream->State() == WritableStream::WriterState::Closed ||
aStream->State() == WritableStream::WriterState::Errored) {
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
promise->MaybeResolveWithUndefined();
return promise.forget();
}
@ -565,10 +559,8 @@ already_AddRefed<Promise> WritableStreamAbort(JSContext* aCx,
}
// Step 9. Let promise be a new promise.
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
// Step 10. Set stream.[[pendingAbortRequest]] to a new pending abort request
// whose promise is promise, reason is reason, and was already erroring is
@ -628,10 +620,8 @@ already_AddRefed<Promise> WritableStreamClose(JSContext* aCx,
MOZ_ASSERT(!aStream->CloseQueuedOrInFlight());
// Step 5. Let promise be a new promise.
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
// Step 6. Set stream.[[closeRequest]] to promise.
aStream->SetCloseRequest(promise);
@ -742,10 +732,8 @@ already_AddRefed<Promise> WritableStreamAddWriteRequest(WritableStream* aStream,
MOZ_ASSERT(aStream->State() == WritableStream::WriterState::Writable);
// Step 3. Let promise be a new promise.
RefPtr<Promise> promise = Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return nullptr;
}
RefPtr<Promise> promise =
Promise::CreateInfallible(aStream->GetParentObject());
// Step 4. Append promise to stream.[[writeRequests]].
aStream->AppendWriteRequest(promise);

View File

@ -184,10 +184,7 @@ void SetUpWritableStreamDefaultController(
// Step 16. Let startPromise be a promise resolved with startResult.
RefPtr<Promise> startPromise =
Promise::Create(aStream->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aStream->GetParentObject());
startPromise->MaybeResolve(startResult);
// Step 17/18.

View File

@ -382,10 +382,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// Step 5. If state is "writable",
if (state == WritableStream::WriterState::Writable) {
RefPtr<Promise> readyPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
// Step 5.1 If ! WritableStreamCloseQueuedOrInFlight(stream) is false and
// stream.[[backpressure]] is true, set writer.[[readyPromise]] to a new
@ -401,10 +398,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// Step 5.3. Set writer.[[closedPromise]] to a new promise.
RefPtr<Promise> closedPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
aWriter->SetClosedPromise(closedPromise);
} else if (state == WritableStream::WriterState::Erroring) {
// Step 6. Otherwise, if state is "erroring",
@ -413,10 +407,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// stream.[[storedError]].
JS::Rooted<JS::Value> storedError(RootingCx(), aStream->StoredError());
RefPtr<Promise> readyPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
readyPromise->MaybeReject(storedError);
aWriter->SetReadyPromise(readyPromise);
@ -425,10 +416,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// Step 6.3. Set writer.[[closedPromise]] to a new promise.
RefPtr<Promise> closedPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
aWriter->SetClosedPromise(closedPromise);
} else if (state == WritableStream::WriterState::Closed) {
// Step 7. Otherwise, if state is "closed",
@ -460,10 +448,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// Step 8.3. Set writer.[[readyPromise]] to a promise rejected with
// storedError.
RefPtr<Promise> readyPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
readyPromise->MaybeReject(storedError);
aWriter->SetReadyPromise(readyPromise);
@ -473,10 +458,7 @@ void SetUpWritableStreamDefaultWriter(WritableStreamDefaultWriter* aWriter,
// Step 8.5. Set writer.[[closedPromise]] to a promise rejected with
// storedError.
RefPtr<Promise> closedPromise =
Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
Promise::CreateInfallible(aWriter->GetParentObject());
closedPromise->MaybeReject(storedError);
aWriter->SetClosedPromise(closedPromise);
@ -497,10 +479,7 @@ void WritableStreamDefaultWriterEnsureClosedPromiseRejected(
} else {
// Step 2. Otherwise, set writer.[[closedPromise]] to a promise rejected
// with error.
closedPromise = Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
closedPromise = Promise::CreateInfallible(aWriter->GetParentObject());
closedPromise->MaybeReject(aError);
aWriter->SetClosedPromise(closedPromise);
}
@ -521,10 +500,7 @@ void WritableStreamDefaultWriterEnsureReadyPromiseRejected(
} else {
// Step 2. Otherwise, set writer.[[readyPromise]] to a promise rejected with
// error.
readyPromise = Promise::Create(aWriter->GetParentObject(), aRv);
if (aRv.Failed()) {
return;
}
readyPromise = Promise::CreateInfallible(aWriter->GetParentObject());
readyPromise->MaybeReject(aError);
aWriter->SetReadyPromise(readyPromise);
}