From b6f871ac8b24a7627021599d4540f2c6c44124d3 Mon Sep 17 00:00:00 2001 From: Kagami Sascha Rosylight Date: Tue, 17 Jan 2023 14:59:28 +0000 Subject: [PATCH] Bug 1809895 - Part 1: Move PromisifyAlgorithm to StreamUtils.h r=smaug Differential Revision: https://phabricator.services.mozilla.com/D166805 --- dom/streams/StreamUtils.h | 34 ++++++++++++++++++++++ dom/streams/TransformerCallbackHelpers.cpp | 24 ++------------- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/dom/streams/StreamUtils.h b/dom/streams/StreamUtils.h index ddfff89c1c97..08a6fad13ba3 100644 --- a/dom/streams/StreamUtils.h +++ b/dom/streams/StreamUtils.h @@ -6,6 +6,9 @@ #define mozilla_dom_StreamUtils_h #include "mozilla/ErrorResult.h" +#include "mozilla/dom/Promise.h" + +class nsIGlobalObject; namespace mozilla::dom { @@ -14,6 +17,37 @@ struct QueuingStrategy; double ExtractHighWaterMark(const QueuingStrategy& aStrategy, double aDefaultHWM, ErrorResult& aRv); +// Promisification algorithm, shared among: +// Step 2 and 3 of https://streams.spec.whatwg.org/#readablestream-set-up +// Step 2 and 3 of +// https://streams.spec.whatwg.org/#readablestream-set-up-with-byte-reading-support +// Step 2 and 3 of https://streams.spec.whatwg.org/#writablestream-set-up +// Step 5 and 6 of https://streams.spec.whatwg.org/#transformstream-set-up +template +MOZ_CAN_RUN_SCRIPT static already_AddRefed PromisifyAlgorithm( + nsIGlobalObject* aGlobal, T aFunc, mozilla::ErrorResult& aRv) { + // Step 1. Let result be the result of running (algorithm). If this throws an + // exception e, return a promise rejected with e. + RefPtr result; + if constexpr (!std::is_same::value) { + result = aFunc(aRv); + } else { + aFunc(aRv); + } + + if (aRv.Failed()) { + return Promise::CreateRejectedWithErrorResult(aGlobal, aRv); + } + + // Step 2. If result is a Promise, then return result. + if (result) { + return result.forget(); + } + + // Step 3. Return a promise resolved with undefined. + return Promise::CreateResolvedWithUndefined(aGlobal, aRv); +} + } // namespace mozilla::dom #endif // mozilla_dom_StreamUtils_h diff --git a/dom/streams/TransformerCallbackHelpers.cpp b/dom/streams/TransformerCallbackHelpers.cpp index 445d9704730e..606c24adda7c 100644 --- a/dom/streams/TransformerCallbackHelpers.cpp +++ b/dom/streams/TransformerCallbackHelpers.cpp @@ -6,6 +6,7 @@ #include "TransformerCallbackHelpers.h" +#include "StreamUtils.h" #include "mozilla/dom/Promise.h" #include "mozilla/dom/TransformStreamDefaultController.h" @@ -84,30 +85,11 @@ already_AddRefed TransformerAlgorithms::FlushCallback( CallbackObject::eRethrowExceptions); } -// https://streams.spec.whatwg.org/#transformstream-set-up -// Step 5 and 6. -template -MOZ_CAN_RUN_SCRIPT static already_AddRefed Promisify( - nsIGlobalObject* aGlobal, T aFunc, mozilla::ErrorResult& aRv) { - // Step 1. Let result be the result of running (algorithm). If this throws an - // exception e, return a promise rejected with e. - aFunc(aRv); - if (aRv.Failed()) { - return Promise::CreateRejectedWithErrorResult(aGlobal, aRv); - } - - // Step 2. If result is a Promise, then return result. - // (This supports no return value since currently no subclass needs one) - - // Step 3. Return a promise resolved with undefined. - return Promise::CreateResolvedWithUndefined(aGlobal, aRv); -} - already_AddRefed TransformerAlgorithmsWrapper::TransformCallback( JSContext*, JS::Handle aChunk, TransformStreamDefaultController& aController, ErrorResult& aRv) { nsCOMPtr global = aController.GetParentObject(); - return Promisify( + return PromisifyAlgorithm( global, [this, &aChunk, &aController](ErrorResult& aRv) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION { @@ -120,7 +102,7 @@ already_AddRefed TransformerAlgorithmsWrapper::FlushCallback( JSContext*, TransformStreamDefaultController& aController, ErrorResult& aRv) { nsCOMPtr global = aController.GetParentObject(); - return Promisify( + return PromisifyAlgorithm( global, [this, &aController](ErrorResult& aRv) MOZ_CAN_RUN_SCRIPT_FOR_DEFINITION { return FlushCallbackImpl(aController, aRv);