Bug 1483036 - Add MaybeResolveWithClone and MaybeRejectWithClone to Promise. r=smaug
authorZibi Braniecki <zbraniecki@mozilla.com>
Tue, 26 Mar 2019 19:34:29 +0000
changeset 466198 0bd696ed34eace2844a540509a5022dc57b48508
parent 466197 b7663bdb3f6e5c01f10c4af4d96e471811860917
child 466199 5100b0065b01ac6ac19ae9246483fa0003a8c6a3
push id81477
push userzbraniecki@mozilla.com
push dateTue, 26 Mar 2019 19:35:51 +0000
treeherderautoland@ba00aeb3eb20 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug
bugs1483036
milestone68.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1483036 - Add MaybeResolveWithClone and MaybeRejectWithClone to Promise. r=smaug Differential Revision: https://phabricator.services.mozilla.com/D24385
dom/promise/Promise.cpp
dom/promise/Promise.h
dom/promise/moz.build
--- a/dom/promise/Promise.cpp
+++ b/dom/promise/Promise.cpp
@@ -36,16 +36,17 @@
 #include "nsJSPrincipals.h"
 #include "nsJSUtils.h"
 #include "nsPIDOMWindow.h"
 #include "PromiseDebugging.h"
 #include "PromiseNativeHandler.h"
 #include "PromiseWorkerProxy.h"
 #include "WrapperFactory.h"
 #include "xpcpublic.h"
+#include "xpcprivate.h"
 
 namespace mozilla {
 namespace dom {
 
 namespace {
 // Generator used by Promise::GetID.
 Atomic<uintptr_t> gIDGenerator(0);
 }  // namespace
@@ -543,16 +544,42 @@ void Promise::ReportRejectedPromise(JSCo
   RefPtr<nsIRunnable> event = new AsyncErrorReporter(xpcReport);
   if (win) {
     win->Dispatch(mozilla::TaskCategory::Other, event.forget());
   } else {
     NS_DispatchToMainThread(event);
   }
 }
 
+void Promise::MaybeResolveWithClone(JSContext* aCx,
+                                    JS::Handle<JS::Value> aValue) {
+  JS::Rooted<JSObject*> sourceScope(aCx, JS::CurrentGlobalOrNull(aCx));
+  AutoEntryScript aes(GetParentObject(), "Promise resolution");
+  JSContext* cx = aes.cx();
+  JS::Rooted<JS::Value> value(cx, aValue);
+
+  xpc::StackScopedCloneOptions options;
+  options.wrapReflectors = true;
+  StackScopedClone(cx, options, sourceScope, &value);
+  MaybeResolve(aCx, value);
+}
+
+void Promise::MaybeRejectWithClone(JSContext* aCx,
+                                   JS::Handle<JS::Value> aValue) {
+  JS::Rooted<JSObject*> sourceScope(aCx, JS::CurrentGlobalOrNull(aCx));
+  AutoEntryScript aes(GetParentObject(), "Promise rejection");
+  JSContext* cx = aes.cx();
+  JS::Rooted<JS::Value> value(cx, aValue);
+
+  xpc::StackScopedCloneOptions options;
+  options.wrapReflectors = true;
+  StackScopedClone(cx, options, sourceScope, &value);
+  MaybeReject(aCx, value);
+}
+
 JSObject* Promise::GlobalJSObject() const {
   return mGlobal->GetGlobalJSObject();
 }
 
 JS::Compartment* Promise::Compartment() const {
   return js::GetObjectCompartment(GlobalJSObject());
 }
 
--- a/dom/promise/Promise.h
+++ b/dom/promise/Promise.h
@@ -98,16 +98,19 @@ class Promise : public nsISupports, publ
     MOZ_ASSERT(aArg.Failed());
     MaybeSomething(aArg, &Promise::MaybeReject);
   }
 
   void MaybeReject(const RefPtr<MediaStreamError>& aArg);
 
   void MaybeRejectWithUndefined();
 
+  void MaybeResolveWithClone(JSContext* aCx, JS::Handle<JS::Value> aValue);
+  void MaybeRejectWithClone(JSContext* aCx, JS::Handle<JS::Value> 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
   // without instantiating the DOMException specialization of MaybeSomething in
   // every translation unit that includes this header, because that would
   // require use to include DOMException.h either here or in all those
   // translation units.
   template <typename T>
--- a/dom/promise/moz.build
+++ b/dom/promise/moz.build
@@ -18,16 +18,17 @@ EXPORTS.mozilla.dom += [
 UNIFIED_SOURCES += [
     'Promise.cpp',
     'PromiseDebugging.cpp',
 ]
 
 LOCAL_INCLUDES += [
     '../base',
     '../ipc',
+    '/js/xpconnect/src',
 ]
 
 include('/ipc/chromium/chromium-config.mozbuild')
 
 FINAL_LIBRARY = 'xul'
 
 MOCHITEST_MANIFESTS += ['tests/mochitest.ini']