Bug 1466385. Use forwarding reference in MaybeResolve. r?bz
This allows move semantics when resolving Promises
MozReview-Commit-ID: EYFd3Abr7Ec
--- a/dom/media/eme/DetailedPromise.h
+++ b/dom/media/eme/DetailedPromise.h
@@ -29,21 +29,21 @@ public:
static already_AddRefed<DetailedPromise>
Create(nsIGlobalObject* aGlobal, ErrorResult& aRv,
const nsACString& aName,
Telemetry::HistogramID aSuccessLatencyProbe,
Telemetry::HistogramID aFailureLatencyProbe);
template <typename T>
- void MaybeResolve(const T& aArg)
+ void MaybeResolve(T&& aArg)
{
EME_LOG("%s promise resolved", mName.get());
MaybeReportTelemetry(eStatus::kSucceeded);
- Promise::MaybeResolve<T>(aArg);
+ Promise::MaybeResolve(std::forward<T>(aArg));
}
void MaybeReject(nsresult aArg) = delete;
void MaybeReject(nsresult aArg, const nsACString& aReason);
void MaybeReject(ErrorResult& aArg) = delete;
void MaybeReject(ErrorResult&, const nsACString& aReason);
--- a/dom/promise/Promise.h
+++ b/dom/promise/Promise.h
@@ -4,16 +4,17 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef mozilla_dom_Promise_h
#define mozilla_dom_Promise_h
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
+#include "mozilla/Move.h"
#include "mozilla/TimeStamp.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsCycleCollectionParticipant.h"
#include "mozilla/dom/PromiseBinding.h"
#include "mozilla/dom/ToJSValue.h"
#include "mozilla/WeakPtr.h"
#include "nsWrapperCache.h"
@@ -67,18 +68,18 @@ public:
void MaybeReject(JSContext* aCx,
JS::Handle<JS::Value> aValue);
// Helpers for using Promise from C++.
// Most DOM objects are handled already. To add a new type T, add a
// ToJSValue overload in ToJSValue.h.
// aArg is a const reference so we can pass rvalues like integer constants
template <typename T>
- void MaybeResolve(const T& aArg) {
- MaybeSomething(aArg, &Promise::MaybeResolve);
+ void MaybeResolve(T&& aArg) {
+ MaybeSomething(std::forward<T>(aArg), &Promise::MaybeResolve);
}
void MaybeResolveWithUndefined();
inline void MaybeReject(nsresult aArg) {
MOZ_ASSERT(NS_FAILED(aArg));
MaybeSomething(aArg, &Promise::MaybeReject);
}
@@ -177,24 +178,24 @@ protected:
virtual ~Promise();
// Do JS-wrapping after Promise creation. Passing null for aDesiredProto will
// use the default prototype for the sort of Promise we have.
void CreateWrapper(JS::Handle<JSObject*> aDesiredProto, ErrorResult& aRv);
private:
template <typename T>
- void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
+ void MaybeSomething(T&& aArgument, MaybeFunc aFunc) {
MOZ_ASSERT(PromiseObj()); // It was preserved!
AutoEntryScript aes(mGlobal, "Promise resolution or rejection");
JSContext* cx = aes.cx();
JS::Rooted<JS::Value> val(cx);
- if (!ToJSValue(cx, aArgument, &val)) {
+ if (!ToJSValue(cx, std::forward<T>(aArgument), &val)) {
HandleException(cx);
return;
}
(this->*aFunc)(cx, val);
}
void HandleException(JSContext* aCx);