From 600ab9bfb99ebd4516ce83cdc1e4ade73c654f19 Mon Sep 17 00:00:00 2001 From: Ben Kelly Date: Thu, 30 Oct 2014 11:12:09 -0400 Subject: [PATCH] Bug 1091091: Support ErrorResult in Promise::MaybeReject(). r=bz --- dom/bindings/ToJSValue.cpp | 14 ++++++++++++++ dom/bindings/ToJSValue.h | 8 ++++++++ dom/promise/Promise.h | 5 +++++ 3 files changed, 27 insertions(+) diff --git a/dom/bindings/ToJSValue.cpp b/dom/bindings/ToJSValue.cpp index 42122716565f..4fea13b51692 100644 --- a/dom/bindings/ToJSValue.cpp +++ b/dom/bindings/ToJSValue.cpp @@ -60,5 +60,19 @@ ToJSValue(JSContext* aCx, return ToJSValue(aCx, exception, aValue); } +bool +ToJSValue(JSContext* aCx, + ErrorResult& aArgument, + JS::MutableHandle aValue) +{ + MOZ_ASSERT(aArgument.Failed()); + ThrowMethodFailedWithDetails(aCx, aArgument, "", ""); + if (!JS_GetPendingException(aCx, aValue)) { + return false; + } + JS_ClearPendingException(aCx); + return true; +} + } // namespace dom } // namespace mozilla diff --git a/dom/bindings/ToJSValue.h b/dom/bindings/ToJSValue.h index c300307f59f1..49d7d35d04c6 100644 --- a/dom/bindings/ToJSValue.h +++ b/dom/bindings/ToJSValue.h @@ -261,6 +261,14 @@ ToJSValue(JSContext* aCx, nsresult aArgument, JS::MutableHandle aValue); +// Accept ErrorResult, for use in rejections, and create an exception +// representing the failure. Note, the ErrorResult must indicate a failure +// with aArgument.Failure() returning true. +bool +ToJSValue(JSContext* aCx, + ErrorResult& aArgument, + JS::MutableHandle aValue); + // Accept pointers to other things we accept template typename EnableIf::value, bool>::Type diff --git a/dom/promise/Promise.h b/dom/promise/Promise.h index 8c71421ffbc7..b76433e509aa 100644 --- a/dom/promise/Promise.h +++ b/dom/promise/Promise.h @@ -101,6 +101,11 @@ public: MaybeSomething(aArg, &Promise::MaybeReject); } + inline void MaybeReject(ErrorResult& aArg) { + MOZ_ASSERT(aArg.Failed()); + MaybeSomething(aArg, &Promise::MaybeReject); + } + void MaybeReject(const nsRefPtr& aArg); // DO NOT USE MaybeRejectBrokenly with in new code. Promises should be