From 4450c1692a130dc7f88a14afcef601e771e8889d Mon Sep 17 00:00:00 2001 From: Zibi Braniecki Date: Tue, 26 Mar 2019 19:34:29 +0000 Subject: [PATCH] Bug 1483036 - Add MaybeResolveWithClone and MaybeRejectWithClone to Promise. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D24385 --HG-- extra : moz-landing-system : lando --- dom/promise/Promise.cpp | 27 +++++++++++++++++++++++++++ dom/promise/Promise.h | 3 +++ dom/promise/moz.build | 1 + 3 files changed, 31 insertions(+) diff --git a/dom/promise/Promise.cpp b/dom/promise/Promise.cpp index eed98d517772..3377c26e7c69 100644 --- a/dom/promise/Promise.cpp +++ b/dom/promise/Promise.cpp @@ -41,6 +41,7 @@ #include "PromiseWorkerProxy.h" #include "WrapperFactory.h" #include "xpcpublic.h" +#include "xpcprivate.h" namespace mozilla { namespace dom { @@ -548,6 +549,32 @@ void Promise::ReportRejectedPromise(JSContext* aCx, JS::HandleObject aPromise) { } } +void Promise::MaybeResolveWithClone(JSContext* aCx, + JS::Handle aValue) { + JS::Rooted sourceScope(aCx, JS::CurrentGlobalOrNull(aCx)); + AutoEntryScript aes(GetParentObject(), "Promise resolution"); + JSContext* cx = aes.cx(); + JS::Rooted value(cx, aValue); + + xpc::StackScopedCloneOptions options; + options.wrapReflectors = true; + StackScopedClone(cx, options, sourceScope, &value); + MaybeResolve(aCx, value); +} + +void Promise::MaybeRejectWithClone(JSContext* aCx, + JS::Handle aValue) { + JS::Rooted sourceScope(aCx, JS::CurrentGlobalOrNull(aCx)); + AutoEntryScript aes(GetParentObject(), "Promise rejection"); + JSContext* cx = aes.cx(); + JS::Rooted value(cx, aValue); + + xpc::StackScopedCloneOptions options; + options.wrapReflectors = true; + StackScopedClone(cx, options, sourceScope, &value); + MaybeReject(aCx, value); +} + JSObject* Promise::GlobalJSObject() const { return mGlobal->GetGlobalJSObject(); } diff --git a/dom/promise/Promise.h b/dom/promise/Promise.h index b672606f9641..24fc203dcc0d 100644 --- a/dom/promise/Promise.h +++ b/dom/promise/Promise.h @@ -103,6 +103,9 @@ class Promise : public nsISupports, public SupportsWeakPtr { void MaybeRejectWithUndefined(); + void MaybeResolveWithClone(JSContext* aCx, JS::Handle aValue); + void MaybeRejectWithClone(JSContext* aCx, JS::Handle aValue); + // DO NOT USE MaybeRejectBrokenly with in new code. Promises should be // rejected with Error instances. // Note: MaybeRejectBrokenly is a template so we can use it with DOMException diff --git a/dom/promise/moz.build b/dom/promise/moz.build index 5ee852ab0ac0..a6cf61dfda73 100644 --- a/dom/promise/moz.build +++ b/dom/promise/moz.build @@ -23,6 +23,7 @@ UNIFIED_SOURCES += [ LOCAL_INCLUDES += [ '../base', '../ipc', + '/js/xpconnect/src', ] include('/ipc/chromium/chromium-config.mozbuild')