2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-12 05:41:21 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* 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/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2018-08-18 19:43:22 +03:00
|
|
|
#include "mozilla/dom/Promise-inl.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
|
2014-11-17 12:42:00 +03:00
|
|
|
#include "js/Debug.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
|
|
|
#include "mozilla/Atomics.h"
|
2019-12-05 07:44:32 +03:00
|
|
|
#include "mozilla/BasePrincipal.h"
|
2016-09-14 16:47:32 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "mozilla/HoldDropJSObjects.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "mozilla/OwningNonNull.h"
|
|
|
|
#include "mozilla/Preferences.h"
|
2018-08-18 19:43:22 +03:00
|
|
|
#include "mozilla/ResultExtensions.h"
|
2018-10-10 00:42:22 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
2021-03-05 18:29:49 +03:00
|
|
|
#include "mozilla/dom/AutoEntryScript.h"
|
2014-07-19 05:31:11 +04:00
|
|
|
#include "mozilla/dom/BindingUtils.h"
|
2016-04-20 02:29:21 +03:00
|
|
|
#include "mozilla/dom/DOMException.h"
|
|
|
|
#include "mozilla/dom/DOMExceptionBinding.h"
|
2020-07-15 18:34:49 +03:00
|
|
|
#include "mozilla/dom/Exceptions.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "mozilla/dom/MediaStreamError.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-07-19 05:31:11 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2019-09-20 23:51:25 +03:00
|
|
|
#include "mozilla/dom/UserActivation.h"
|
2018-01-31 10:25:30 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerRunnable.h"
|
2018-03-14 17:26:33 +03:00
|
|
|
#include "mozilla/dom/WorkerRef.h"
|
2020-04-09 09:50:44 +03:00
|
|
|
#include "mozilla/dom/WorkletImpl.h"
|
|
|
|
#include "mozilla/dom/WorkletGlobalScope.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2020-05-14 07:44:33 +03:00
|
|
|
#include "js/Exception.h" // JS::ExceptionStack
|
2020-09-09 01:55:38 +03:00
|
|
|
#include "js/Object.h" // JS::GetCompartment
|
2015-09-02 19:20:30 +03:00
|
|
|
#include "js/StructuredClone.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "nsContentUtils.h"
|
2022-04-06 01:19:47 +03:00
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2015-08-06 15:45:21 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
|
|
|
#include "nsJSPrincipals.h"
|
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2015-04-10 18:27:57 +03:00
|
|
|
#include "PromiseDebugging.h"
|
2013-11-19 22:43:51 +04:00
|
|
|
#include "PromiseNativeHandler.h"
|
2014-02-24 17:56:54 +04:00
|
|
|
#include "PromiseWorkerProxy.h"
|
2015-11-25 23:48:09 +03:00
|
|
|
#include "WrapperFactory.h"
|
2014-07-04 09:24:59 +04:00
|
|
|
#include "xpcpublic.h"
|
2019-03-26 22:34:29 +03:00
|
|
|
#include "xpcprivate.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2022-03-22 19:44:58 +03:00
|
|
|
namespace mozilla::dom {
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// Promise
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2022-09-07 14:22:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_SINGLE_ZONE_SCRIPT_HOLDER_CLASS(Promise)
|
2013-08-02 05:29:05 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise)
|
2014-02-26 01:34:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
|
2020-02-25 22:44:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_WEAK_PTR
|
2016-02-10 01:40:31 +03:00
|
|
|
tmp->mPromiseObj = nullptr;
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise)
|
2014-02-26 01:34:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGlobal)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Promise)
|
2022-09-07 14:22:51 +03:00
|
|
|
// If you add new JS member variables, you may need to stop using
|
|
|
|
// NS_IMPL_CYCLE_COLLECTION_SINGLE_ZONE_SCRIPT_HOLDER_CLASS.
|
2016-02-10 01:40:31 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mPromiseObj);
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2020-07-08 01:20:01 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(Promise, AddRef)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(Promise, Release)
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
Promise::Promise(nsIGlobalObject* aGlobal)
|
2016-02-10 01:40:31 +03:00
|
|
|
: mGlobal(aGlobal), mPromiseObj(nullptr) {
|
2014-02-26 01:34:55 +04:00
|
|
|
MOZ_ASSERT(mGlobal);
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-08-17 00:10:17 +04:00
|
|
|
Promise::~Promise() { mozilla::DropJSObjects(this); }
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Create(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, ErrorResult& aRv,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction) {
|
2016-04-25 12:06:30 +03:00
|
|
|
if (!aGlobal) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
RefPtr<Promise> p = new Promise(aGlobal);
|
2019-10-26 11:14:05 +03:00
|
|
|
p->CreateWrapper(aRv, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
bool Promise::MaybePropagateUserInputEventHandling() {
|
|
|
|
JS::PromiseUserInputEventHandlingState state =
|
2019-09-20 23:51:25 +03:00
|
|
|
UserActivation::IsHandlingUserInput()
|
2018-10-10 00:42:22 +03:00
|
|
|
? JS::PromiseUserInputEventHandlingState::HadUserInteractionAtCreation
|
|
|
|
: JS::PromiseUserInputEventHandlingState::
|
|
|
|
DidntHaveUserInteractionAtCreation;
|
|
|
|
JS::Rooted<JSObject*> p(RootingCx(), mPromiseObj);
|
|
|
|
return JS::SetPromiseUserInputEventHandlingState(p, state);
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Resolve(
|
|
|
|
nsIGlobalObject* aGlobal, JSContext* aCx, JS::Handle<JS::Value> aValue,
|
2018-10-10 00:42:22 +03:00
|
|
|
ErrorResult& aRv, PropagateUserInteraction aPropagateUserInteraction) {
|
2018-08-02 09:49:00 +03:00
|
|
|
JSAutoRealm ar(aCx, aGlobal->GetGlobalJSObject());
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, JS::CallOriginalPromiseResolve(aCx, aValue));
|
|
|
|
if (!p) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
return CreateFromExisting(aGlobal, p, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::Reject(nsIGlobalObject* aGlobal,
|
|
|
|
JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2018-08-02 09:49:00 +03:00
|
|
|
JSAutoRealm ar(aCx, aGlobal->GetGlobalJSObject());
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, JS::CallOriginalPromiseReject(aCx, aValue));
|
|
|
|
if (!p) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
// This promise will never be resolved, so we pass
|
|
|
|
// eDontPropagateUserInteraction for aPropagateUserInteraction
|
|
|
|
// unconditionally.
|
|
|
|
return CreateFromExisting(aGlobal, p, eDontPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::All(
|
2018-10-10 00:42:22 +03:00
|
|
|
JSContext* aCx, const nsTArray<RefPtr<Promise>>& aPromiseList,
|
|
|
|
ErrorResult& aRv, PropagateUserInteraction aPropagateUserInteraction) {
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> globalObj(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!globalObj) {
|
2018-04-13 00:03:49 +03:00
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = xpc::NativeGlobal(globalObj);
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2018-04-14 17:25:15 +03:00
|
|
|
|
2019-03-26 17:00:51 +03:00
|
|
|
JS::RootedVector<JSObject*> promises(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!promises.reserve(aPromiseList.Length())) {
|
2018-04-14 02:31:42 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& promise : aPromiseList) {
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(aCx, promise->PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
// Just in case, make sure these are all in the context compartment.
|
2018-04-14 02:31:42 +03:00
|
|
|
if (!JS_WrapObject(aCx, &promiseObj)) {
|
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
promises.infallibleAppend(promiseObj);
|
|
|
|
}
|
|
|
|
|
2018-04-14 02:31:42 +03:00
|
|
|
JS::Rooted<JSObject*> result(aCx, JS::GetWaitForAllPromise(aCx, promises));
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!result) {
|
2018-04-14 02:31:42 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
return CreateFromExisting(global, result, aPropagateUserInteraction);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::Then(JSContext* aCx,
|
|
|
|
// aCalleeGlobal may not be in the compartment of aCx, when
|
|
|
|
// called over Xrays.
|
|
|
|
JS::Handle<JSObject*> aCalleeGlobal,
|
|
|
|
AnyCallback* aResolveCallback, AnyCallback* aRejectCallback,
|
|
|
|
JS::MutableHandle<JS::Value> aRetval, ErrorResult& aRv) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
// Let's hope this does the right thing with Xrays... Ensure everything is
|
|
|
|
// just in the caller compartment; that ought to do the trick. In theory we
|
|
|
|
// should consider aCalleeGlobal, but in practice our only caller is
|
|
|
|
// DOMRequest::Then, which is not working with a Promise subclass, so things
|
|
|
|
// should be OK.
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> promise(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &promise)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> resolveCallback(aCx);
|
|
|
|
if (aResolveCallback) {
|
2016-11-15 08:25:37 +03:00
|
|
|
resolveCallback = aResolveCallback->CallbackOrNull();
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &resolveCallback)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectCallback(aCx);
|
|
|
|
if (aRejectCallback) {
|
2016-11-15 08:25:37 +03:00
|
|
|
rejectCallback = aRejectCallback->CallbackOrNull();
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS_WrapObject(aCx, &rejectCallback)) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> retval(aCx);
|
|
|
|
retval = JS::CallOriginalPromiseThen(aCx, promise, resolveCallback,
|
|
|
|
rejectCallback);
|
|
|
|
if (!retval) {
|
2016-03-10 12:50:56 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aRetval.setObject(*retval);
|
|
|
|
}
|
|
|
|
|
2022-03-21 19:45:36 +03:00
|
|
|
static void SettlePromise(Promise* aSettlingPromise, Promise* aCallbackPromise,
|
|
|
|
ErrorResult& aRv) {
|
2022-03-21 19:45:36 +03:00
|
|
|
if (!aSettlingPromise) {
|
|
|
|
return;
|
|
|
|
}
|
2022-03-21 19:45:35 +03:00
|
|
|
if (aRv.Failed()) {
|
2022-03-21 19:45:36 +03:00
|
|
|
aSettlingPromise->MaybeReject(std::move(aRv));
|
2022-03-21 19:45:35 +03:00
|
|
|
return;
|
|
|
|
}
|
2022-03-21 19:45:36 +03:00
|
|
|
if (aCallbackPromise) {
|
|
|
|
aSettlingPromise->MaybeResolve(aCallbackPromise);
|
2019-02-27 22:54:31 +03:00
|
|
|
} else {
|
2022-03-21 19:45:36 +03:00
|
|
|
aSettlingPromise->MaybeResolveWithUndefined();
|
2019-02-27 22:54:31 +03:00
|
|
|
}
|
2018-08-18 19:43:22 +03:00
|
|
|
}
|
|
|
|
|
2022-03-21 19:45:36 +03:00
|
|
|
void PromiseNativeThenHandlerBase::ResolvedCallback(
|
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv) {
|
2022-03-22 19:44:58 +03:00
|
|
|
if (!HasResolvedCallback()) {
|
|
|
|
mPromise->MaybeResolve(aValue);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-21 19:45:36 +03:00
|
|
|
RefPtr<Promise> promise = CallResolveCallback(aCx, aValue, aRv);
|
|
|
|
SettlePromise(mPromise, promise, aRv);
|
|
|
|
}
|
|
|
|
|
2018-08-18 19:43:22 +03:00
|
|
|
void PromiseNativeThenHandlerBase::RejectedCallback(
|
2022-01-20 22:11:39 +03:00
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv) {
|
2022-03-22 19:44:58 +03:00
|
|
|
if (!HasRejectedCallback()) {
|
|
|
|
mPromise->MaybeReject(aValue);
|
|
|
|
return;
|
|
|
|
}
|
2022-03-21 19:45:36 +03:00
|
|
|
RefPtr<Promise> promise = CallRejectCallback(aCx, aValue, aRv);
|
|
|
|
SettlePromise(mPromise, promise, aRv);
|
2018-08-18 19:43:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PromiseNativeThenHandlerBase)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
tmp->Traverse(cb);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->Unlink();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseNativeThenHandlerBase)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2022-04-06 01:19:47 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(PromiseNativeThenHandlerBase)
|
|
|
|
tmp->Trace(aCallbacks, aClosure);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2018-08-18 19:43:22 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseNativeThenHandlerBase)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseNativeThenHandlerBase)
|
|
|
|
|
|
|
|
Result<RefPtr<Promise>, nsresult> Promise::ThenWithoutCycleCollection(
|
|
|
|
const std::function<already_AddRefed<Promise>(
|
2022-06-05 23:55:00 +03:00
|
|
|
JSContext* aCx, JS::Handle<JS::Value> aValue, ErrorResult& aRv)>&
|
|
|
|
aCallback) {
|
2018-08-18 19:43:22 +03:00
|
|
|
return ThenWithCycleCollectedArgs(aCallback);
|
|
|
|
}
|
|
|
|
|
2018-10-10 00:42:22 +03:00
|
|
|
void Promise::CreateWrapper(
|
2019-10-26 11:14:05 +03:00
|
|
|
ErrorResult& aRv, PropagateUserInteraction aPropagateUserInteraction) {
|
2016-02-10 01:40:31 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (!jsapi.Init(mGlobal)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2019-10-26 11:14:05 +03:00
|
|
|
mPromiseObj = JS::NewPromiseObject(cx, nullptr);
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!mPromiseObj) {
|
2016-02-10 01:40:31 +03:00
|
|
|
JS_ClearPendingException(cx);
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
2018-10-10 00:42:22 +03:00
|
|
|
if (aPropagateUserInteraction == ePropagateUserInteraction) {
|
|
|
|
Unused << MaybePropagateUserInputEventHandling();
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::MaybeResolve(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS::ResolvePromise(aCx, p, aValue)) {
|
|
|
|
// Now what? There's nothing sane to do here.
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::MaybeReject(JSContext* aCx, JS::Handle<JS::Value> aValue) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> p(aCx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (!JS::RejectPromise(aCx, p, aValue)) {
|
|
|
|
// Now what? There's nothing sane to do here.
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
#define SLOT_NATIVEHANDLER 0
|
|
|
|
#define SLOT_NATIVEHANDLER_TASK 1
|
|
|
|
|
|
|
|
enum class NativeHandlerTask : int32_t { Resolve, Reject };
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2016-02-10 01:40:31 +03:00
|
|
|
static bool NativeHandlerCallback(JSContext* aCx, unsigned aArgc,
|
|
|
|
JS::Value* aVp) {
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Value v =
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(), SLOT_NATIVEHANDLER);
|
2016-02-10 01:40:31 +03:00
|
|
|
MOZ_ASSERT(v.isObject());
|
|
|
|
|
2017-07-10 23:05:24 +03:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, &v.toObject());
|
2016-09-07 00:38:13 +03:00
|
|
|
PromiseNativeHandler* handler = nullptr;
|
2017-07-10 23:05:24 +03:00
|
|
|
if (NS_FAILED(UNWRAP_OBJECT(PromiseNativeHandler, &obj, handler))) {
|
2016-02-10 01:40:31 +03:00
|
|
|
return Throw(aCx, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
|
|
|
v = js::GetFunctionNativeReserved(&args.callee(), SLOT_NATIVEHANDLER_TASK);
|
|
|
|
NativeHandlerTask task = static_cast<NativeHandlerTask>(v.toInt32());
|
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
ErrorResult rv;
|
2016-02-10 01:40:31 +03:00
|
|
|
if (task == NativeHandlerTask::Resolve) {
|
2019-03-19 18:53:43 +03:00
|
|
|
// handler is kept alive by "obj" on the stack.
|
2022-01-20 22:11:39 +03:00
|
|
|
MOZ_KnownLive(handler)->ResolvedCallback(aCx, args.get(0), rv);
|
2016-02-10 01:40:31 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(task == NativeHandlerTask::Reject);
|
2019-03-19 18:53:43 +03:00
|
|
|
// handler is kept alive by "obj" on the stack.
|
2022-01-20 22:11:39 +03:00
|
|
|
MOZ_KnownLive(handler)->RejectedCallback(aCx, args.get(0), rv);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
2022-01-20 22:11:39 +03:00
|
|
|
return !rv.MaybeSetPendingException(aCx);
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static JSObject* CreateNativeHandlerFunction(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aHolder,
|
|
|
|
NativeHandlerTask aTask) {
|
|
|
|
JSFunction* func = js::NewFunctionWithReserved(aCx, NativeHandlerCallback,
|
|
|
|
/* nargs = */ 1,
|
|
|
|
/* flags = */ 0, nullptr);
|
|
|
|
if (!func) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
|
|
|
|
|
2019-05-03 13:09:11 +03:00
|
|
|
JS::AssertObjectIsNotGray(aHolder);
|
2016-02-10 01:40:31 +03:00
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_NATIVEHANDLER,
|
|
|
|
JS::ObjectValue(*aHolder));
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_NATIVEHANDLER_TASK,
|
|
|
|
JS::Int32Value(static_cast<int32_t>(aTask)));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:13 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class PromiseNativeHandlerShim final : public PromiseNativeHandler {
|
|
|
|
RefPtr<PromiseNativeHandler> mInner;
|
2022-05-10 21:54:52 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
enum InnerState{
|
|
|
|
NotCleared,
|
|
|
|
ClearedFromResolve,
|
|
|
|
ClearedFromReject,
|
|
|
|
ClearedFromCC,
|
|
|
|
};
|
|
|
|
InnerState mState = NotCleared;
|
|
|
|
#endif
|
2016-09-07 00:38:13 +03:00
|
|
|
|
2020-03-09 18:07:21 +03:00
|
|
|
~PromiseNativeHandlerShim() = default;
|
2016-09-07 00:38:13 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit PromiseNativeHandlerShim(PromiseNativeHandler* aInner)
|
|
|
|
: mInner(aInner) {
|
2022-05-02 10:36:53 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInner);
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2022-01-20 22:11:39 +03:00
|
|
|
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override {
|
2022-05-10 21:54:52 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromResolve);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromReject);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromCC);
|
|
|
|
#else
|
|
|
|
if (!mInner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-13 17:38:48 +03:00
|
|
|
RefPtr<PromiseNativeHandler> inner = std::move(mInner);
|
2022-01-20 22:11:39 +03:00
|
|
|
inner->ResolvedCallback(aCx, aValue, aRv);
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_ASSERT(!mInner);
|
2022-05-10 21:54:52 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
mState = ClearedFromResolve;
|
|
|
|
#endif
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_CAN_RUN_SCRIPT
|
2022-01-20 22:11:39 +03:00
|
|
|
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) override {
|
2022-05-10 21:54:52 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromResolve);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromReject);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mState != ClearedFromCC);
|
|
|
|
#else
|
|
|
|
if (!mInner) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
#endif
|
2020-02-13 17:38:48 +03:00
|
|
|
RefPtr<PromiseNativeHandler> inner = std::move(mInner);
|
2022-01-20 22:11:39 +03:00
|
|
|
inner->RejectedCallback(aCx, aValue, aRv);
|
2019-03-19 18:53:43 +03:00
|
|
|
MOZ_ASSERT(!mInner);
|
2022-05-10 21:54:52 +03:00
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
mState = ClearedFromReject;
|
|
|
|
#endif
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
|
|
|
|
JS::MutableHandle<JSObject*> aWrapper) {
|
2018-06-26 00:20:54 +03:00
|
|
|
return PromiseNativeHandler_Binding::Wrap(aCx, this, aGivenProto, aWrapper);
|
2016-09-07 00:38:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(PromiseNativeHandlerShim)
|
|
|
|
};
|
|
|
|
|
2022-05-10 21:54:52 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PromiseNativeHandlerShim)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PromiseNativeHandlerShim)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mInner)
|
|
|
|
#ifdef MOZ_DIAGNOSTIC_ASSERT_ENABLED
|
|
|
|
tmp->mState = ClearedFromCC;
|
|
|
|
#endif
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PromiseNativeHandlerShim)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2016-09-07 00:38:13 +03:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseNativeHandlerShim)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseNativeHandlerShim)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseNativeHandlerShim)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
void Promise::AppendNativeHandler(PromiseNativeHandler* aRunnable) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mGlobal))) {
|
|
|
|
// Our API doesn't allow us to return a useful error. Not like this should
|
|
|
|
// happen anyway.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-07 00:38:13 +03:00
|
|
|
// The self-hosted promise js may keep the object we pass to it alive
|
|
|
|
// for quite a while depending on when GC runs. Therefore, pass a shim
|
|
|
|
// object instead. The shim will free its inner PromiseNativeHandler
|
|
|
|
// after the promise has settled just like our previous c++ promises did.
|
|
|
|
RefPtr<PromiseNativeHandlerShim> shim =
|
|
|
|
new PromiseNativeHandlerShim(aRunnable);
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
JS::Rooted<JSObject*> handlerWrapper(cx);
|
|
|
|
// Note: PromiseNativeHandler is NOT wrappercached. So we can't use
|
|
|
|
// ToJSValue here, because it will try to do XPConnect wrapping on it, sadly.
|
2016-09-07 00:38:13 +03:00
|
|
|
if (NS_WARN_IF(!shim->WrapObject(cx, nullptr, &handlerWrapper))) {
|
2016-02-10 01:40:31 +03:00
|
|
|
// Again, no way to report errors.
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> resolveFunc(cx);
|
|
|
|
resolveFunc = CreateNativeHandlerFunction(cx, handlerWrapper,
|
|
|
|
NativeHandlerTask::Resolve);
|
|
|
|
if (NS_WARN_IF(!resolveFunc)) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectFunc(cx);
|
|
|
|
rejectFunc = CreateNativeHandlerFunction(cx, handlerWrapper,
|
|
|
|
NativeHandlerTask::Reject);
|
|
|
|
if (NS_WARN_IF(!rejectFunc)) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-02-10 01:40:31 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(cx, PromiseObj());
|
2016-02-10 01:40:31 +03:00
|
|
|
if (NS_WARN_IF(
|
|
|
|
!JS::AddPromiseReactions(cx, promiseObj, resolveFunc, rejectFunc))) {
|
|
|
|
jsapi.ClearException();
|
|
|
|
return;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Promise::HandleException(JSContext* aCx) {
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (JS_GetPendingException(aCx, &exn)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2019-07-29 17:43:54 +03:00
|
|
|
// Always reject even if this was called in *Resolve.
|
2016-02-10 01:40:31 +03:00
|
|
|
MaybeReject(aCx, exn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<Promise> Promise::CreateFromExisting(
|
2018-10-10 00:42:22 +03:00
|
|
|
nsIGlobalObject* aGlobal, JS::Handle<JSObject*> aPromiseObj,
|
|
|
|
PropagateUserInteraction aPropagateUserInteraction) {
|
2020-09-09 01:55:38 +03:00
|
|
|
MOZ_ASSERT(JS::GetCompartment(aGlobal->GetGlobalJSObjectPreserveColor()) ==
|
|
|
|
JS::GetCompartment(aPromiseObj));
|
2016-02-10 01:40:31 +03:00
|
|
|
RefPtr<Promise> p = new Promise(aGlobal);
|
2016-02-10 01:40:31 +03:00
|
|
|
p->mPromiseObj = aPromiseObj;
|
2018-10-10 00:42:22 +03:00
|
|
|
if (aPropagateUserInteraction == ePropagateUserInteraction &&
|
|
|
|
!p->MaybePropagateUserInputEventHandling()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-10 01:40:31 +03:00
|
|
|
return p.forget();
|
|
|
|
}
|
|
|
|
|
2016-08-10 05:07:42 +03:00
|
|
|
void Promise::MaybeResolveWithUndefined() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
|
|
|
MaybeResolve(JS::UndefinedHandleValue);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
void Promise::MaybeReject(const RefPtr<MediaStreamError>& aArg) {
|
2016-04-11 18:16:26 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
2014-09-20 10:20:41 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2016-06-09 22:26:47 +03:00
|
|
|
void Promise::MaybeRejectWithUndefined() {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(Promise);
|
|
|
|
|
|
|
|
MaybeSomething(JS::UndefinedHandleValue, &Promise::MaybeReject);
|
2016-01-19 09:35:01 +03:00
|
|
|
}
|
|
|
|
|
2022-06-05 23:55:00 +03:00
|
|
|
void Promise::ReportRejectedPromise(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aPromise) {
|
2016-03-22 18:22:24 +03:00
|
|
|
MOZ_ASSERT(!js::IsWrapper(aPromise));
|
|
|
|
|
|
|
|
MOZ_ASSERT(JS::GetPromiseState(aPromise) == JS::PromiseState::Rejected);
|
|
|
|
|
2020-04-09 09:50:44 +03:00
|
|
|
bool isChrome = false;
|
|
|
|
uint64_t innerWindowID = 0;
|
2021-03-07 22:51:39 +03:00
|
|
|
nsGlobalWindowInner* winForDispatch = nullptr;
|
2020-04-09 09:50:44 +03:00
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
isChrome = nsContentUtils::ObjectPrincipal(aPromise)->IsSystemPrincipal();
|
2021-03-07 22:51:39 +03:00
|
|
|
|
|
|
|
if (nsGlobalWindowInner* win = xpc::WindowGlobalOrNull(aPromise)) {
|
|
|
|
winForDispatch = win;
|
|
|
|
innerWindowID = win->WindowID();
|
|
|
|
} else if (nsGlobalWindowInner* win = xpc::SandboxWindowOrNull(
|
|
|
|
JS::GetNonCCWObjectGlobal(aPromise), aCx)) {
|
|
|
|
// Don't dispatch rejections from the sandbox to the associated DOM
|
|
|
|
// window.
|
|
|
|
innerWindowID = win->WindowID();
|
|
|
|
}
|
2020-04-09 09:50:44 +03:00
|
|
|
} else if (const WorkerPrivate* wp = GetCurrentThreadWorkerPrivate()) {
|
|
|
|
isChrome = wp->UsesSystemPrincipal();
|
|
|
|
innerWindowID = wp->WindowID();
|
|
|
|
} else if (nsCOMPtr<nsIGlobalObject> global = xpc::NativeGlobal(aPromise)) {
|
|
|
|
if (nsCOMPtr<WorkletGlobalScope> workletGlobal =
|
|
|
|
do_QueryInterface(global)) {
|
|
|
|
WorkletImpl* impl = workletGlobal->Impl();
|
|
|
|
isChrome = impl->PrincipalInfo().type() ==
|
|
|
|
mozilla::ipc::PrincipalInfo::TSystemPrincipalInfo;
|
|
|
|
innerWindowID = impl->LoadInfo().InnerWindowID();
|
|
|
|
}
|
|
|
|
}
|
2018-08-24 00:01:04 +03:00
|
|
|
|
2020-05-15 23:11:30 +03:00
|
|
|
JS::Rooted<JS::Value> result(aCx, JS::GetPromiseResult(aPromise));
|
|
|
|
// resolutionSite can be null if async stacks are disabled.
|
|
|
|
JS::Rooted<JSObject*> resolutionSite(aCx,
|
|
|
|
JS::GetPromiseResolutionSite(aPromise));
|
|
|
|
|
2019-09-11 17:10:06 +03:00
|
|
|
// We're inspecting the rejection value only to report it to the console, and
|
|
|
|
// we do so without side-effects, so we can safely unwrap it without regard to
|
|
|
|
// the privileges of the Promise object that holds it. If we don't unwrap
|
|
|
|
// before trying to create the error report, we wind up reporting any
|
|
|
|
// cross-origin objects as "uncaught exception: Object".
|
2020-05-15 23:11:30 +03:00
|
|
|
RefPtr<xpc::ErrorReport> xpcReport = new xpc::ErrorReport();
|
2020-05-08 23:54:17 +03:00
|
|
|
{
|
|
|
|
Maybe<JSAutoRealm> ar;
|
|
|
|
JS::Rooted<JS::Value> unwrapped(aCx, result);
|
|
|
|
if (unwrapped.isObject()) {
|
|
|
|
unwrapped.setObject(*js::UncheckedUnwrap(&unwrapped.toObject()));
|
|
|
|
ar.emplace(aCx, &unwrapped.toObject());
|
|
|
|
}
|
|
|
|
|
2020-05-14 07:44:15 +03:00
|
|
|
JS::ErrorReportBuilder report(aCx);
|
2020-05-08 23:54:17 +03:00
|
|
|
RefPtr<Exception> exn;
|
|
|
|
if (unwrapped.isObject() &&
|
|
|
|
(NS_SUCCEEDED(UNWRAP_OBJECT(DOMException, &unwrapped, exn)) ||
|
|
|
|
NS_SUCCEEDED(UNWRAP_OBJECT(Exception, &unwrapped, exn)))) {
|
|
|
|
xpcReport->Init(aCx, exn, isChrome, innerWindowID);
|
2020-05-14 07:44:33 +03:00
|
|
|
} else {
|
2020-05-15 23:11:30 +03:00
|
|
|
// Use the resolution site as the exception stack
|
|
|
|
JS::ExceptionStack exnStack(aCx, unwrapped, resolutionSite);
|
2020-05-14 07:44:33 +03:00
|
|
|
if (!report.init(aCx, exnStack, JS::ErrorReportBuilder::NoSideEffects)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-08 23:54:17 +03:00
|
|
|
xpcReport->Init(report.report(), report.toStringResult().c_str(),
|
|
|
|
isChrome, innerWindowID);
|
|
|
|
}
|
2018-08-24 00:01:04 +03:00
|
|
|
}
|
2016-03-22 18:22:24 +03:00
|
|
|
|
2020-05-15 23:18:34 +03:00
|
|
|
// Used to initialize the similarly named nsISciptError attribute.
|
|
|
|
xpcReport->mIsPromiseRejection = true;
|
|
|
|
|
2016-03-22 18:22:24 +03:00
|
|
|
// Now post an event to do the real reporting async
|
2020-05-08 23:54:17 +03:00
|
|
|
RefPtr<AsyncErrorReporter> event = new AsyncErrorReporter(xpcReport);
|
2021-03-07 22:51:39 +03:00
|
|
|
if (winForDispatch) {
|
|
|
|
if (!winForDispatch->IsDying()) {
|
2020-05-08 23:54:17 +03:00
|
|
|
// Exceptions from a dying window will cause the window to leak.
|
|
|
|
event->SetException(aCx, result);
|
2020-05-15 23:11:30 +03:00
|
|
|
if (resolutionSite) {
|
|
|
|
event->SerializeStack(aCx, resolutionSite);
|
|
|
|
}
|
2020-05-08 23:54:17 +03:00
|
|
|
}
|
2021-03-07 22:51:39 +03:00
|
|
|
winForDispatch->Dispatch(mozilla::TaskCategory::Other, event.forget());
|
2018-02-20 17:52:32 +03:00
|
|
|
} else {
|
|
|
|
NS_DispatchToMainThread(event);
|
|
|
|
}
|
2016-03-22 18:22:24 +03:00
|
|
|
}
|
|
|
|
|
2019-03-26 22:34:29 +03:00
|
|
|
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;
|
2019-07-29 17:43:54 +03:00
|
|
|
if (!StackScopedClone(cx, options, sourceScope, &value)) {
|
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 22:34:29 +03:00
|
|
|
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;
|
2019-07-29 17:43:54 +03:00
|
|
|
if (!StackScopedClone(cx, options, sourceScope, &value)) {
|
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
2019-03-26 22:34:29 +03:00
|
|
|
MaybeReject(aCx, value);
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// A WorkerRunnable to resolve/reject the Promise on the worker thread.
|
|
|
|
// Calling thread MUST hold PromiseWorkerProxy's mutex before creating this.
|
|
|
|
class PromiseWorkerProxyRunnable : public WorkerRunnable {
|
|
|
|
public:
|
|
|
|
PromiseWorkerProxyRunnable(PromiseWorkerProxy* aPromiseWorkerProxy,
|
|
|
|
PromiseWorkerProxy::RunCallbackFunc aFunc)
|
|
|
|
: WorkerRunnable(aPromiseWorkerProxy->GetWorkerPrivate(),
|
|
|
|
WorkerThreadUnchangedBusyCount),
|
|
|
|
mPromiseWorkerProxy(aPromiseWorkerProxy),
|
|
|
|
mFunc(aFunc) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mPromiseWorkerProxy);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2017-11-06 06:37:28 +03:00
|
|
|
virtual bool WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate) override {
|
2016-12-11 20:12:01 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate == mWorkerPrivate);
|
|
|
|
|
|
|
|
MOZ_ASSERT(mPromiseWorkerProxy);
|
|
|
|
RefPtr<Promise> workerPromise = mPromiseWorkerProxy->WorkerPromise();
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Here we convert the buffer to a JS::Value.
|
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
if (!mPromiseWorkerProxy->Read(aCx, &value)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
2014-10-20 06:27:12 +04:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
(workerPromise->*mFunc)(aCx, value);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Release the Promise because it has been resolved/rejected for sure.
|
|
|
|
mPromiseWorkerProxy->CleanUp();
|
2014-01-23 22:47:29 +04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
protected:
|
2020-03-09 18:07:21 +03:00
|
|
|
~PromiseWorkerProxyRunnable() = default;
|
2015-01-13 06:35:33 +03:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
private:
|
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseWorkerProxy;
|
2014-01-23 22:47:29 +04:00
|
|
|
|
2016-12-11 20:12:01 +03:00
|
|
|
// Function pointer for calling Promise::{ResolveInternal,RejectInternal}.
|
|
|
|
PromiseWorkerProxy::RunCallbackFunc mFunc;
|
|
|
|
};
|
2014-01-23 22:47:29 +04:00
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<PromiseWorkerProxy> PromiseWorkerProxy::Create(
|
2016-07-17 17:48:58 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate, Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCb) {
|
2014-12-17 17:49:36 +03:00
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPromise);
|
2015-09-05 12:22:13 +03:00
|
|
|
MOZ_ASSERT_IF(aCb, !!aCb->Write && !!aCb->Read);
|
2014-12-17 17:49:36 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> proxy =
|
2018-03-14 17:26:33 +03:00
|
|
|
new PromiseWorkerProxy(aWorkerPromise, aCb);
|
2014-12-17 17:49:36 +03:00
|
|
|
|
2021-12-16 22:35:44 +03:00
|
|
|
// Maintain a reference so that we have a valid object to clean up when
|
|
|
|
// removing the feature.
|
|
|
|
proxy.get()->AddRef();
|
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
// We do this to make sure the worker thread won't shut down before the
|
|
|
|
// promise is resolved/rejected on the worker thread.
|
2018-03-14 17:26:33 +03:00
|
|
|
RefPtr<StrongWorkerRef> workerRef = StrongWorkerRef::Create(
|
|
|
|
aWorkerPrivate, "PromiseWorkerProxy", [proxy]() { proxy->CleanUp(); });
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!workerRef)) {
|
2014-12-17 17:49:36 +03:00
|
|
|
// Probably the worker is terminating. We cannot complete the operation
|
2022-01-04 16:58:23 +03:00
|
|
|
// and we have to release all the resources. CleanUp releases the extra
|
|
|
|
// ref, too
|
2021-12-16 22:35:44 +03:00
|
|
|
proxy->CleanUp();
|
2014-12-17 17:49:36 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
proxy->mWorkerRef = new ThreadSafeWorkerRef(workerRef);
|
|
|
|
|
2014-12-17 17:49:36 +03:00
|
|
|
return proxy.forget();
|
|
|
|
}
|
|
|
|
|
2015-07-09 09:56:00 +03:00
|
|
|
NS_IMPL_ISUPPORTS0(PromiseWorkerProxy)
|
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
PromiseWorkerProxy::PromiseWorkerProxy(
|
|
|
|
Promise* aWorkerPromise,
|
2015-09-05 12:22:13 +03:00
|
|
|
const PromiseWorkerProxyStructuredCloneCallbacks* aCallbacks)
|
2018-03-14 17:26:33 +03:00
|
|
|
: mWorkerPromise(aWorkerPromise),
|
2014-02-24 17:56:54 +04:00
|
|
|
mCleanedUp(false),
|
|
|
|
mCallbacks(aCallbacks),
|
|
|
|
mCleanUpLock("cleanUpLock") {}
|
|
|
|
|
|
|
|
PromiseWorkerProxy::~PromiseWorkerProxy() {
|
|
|
|
MOZ_ASSERT(mCleanedUp);
|
|
|
|
MOZ_ASSERT(!mWorkerPromise);
|
2018-03-14 17:26:33 +03:00
|
|
|
MOZ_ASSERT(!mWorkerRef);
|
2015-09-02 20:07:26 +03:00
|
|
|
}
|
|
|
|
|
2014-02-24 17:56:54 +04:00
|
|
|
WorkerPrivate* PromiseWorkerProxy::GetWorkerPrivate() const {
|
2015-06-11 01:35:18 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
mCleanUpLock.AssertCurrentThreadOwns();
|
|
|
|
}
|
|
|
|
#endif
|
2015-09-02 20:07:26 +03:00
|
|
|
// Safe to check this without a lock since we assert lock ownership on the
|
|
|
|
// main thread above.
|
|
|
|
MOZ_ASSERT(!mCleanedUp);
|
2018-03-14 17:26:33 +03:00
|
|
|
MOZ_ASSERT(mWorkerRef);
|
2015-06-11 01:35:18 +03:00
|
|
|
|
2018-03-14 17:26:33 +03:00
|
|
|
return mWorkerRef->Private();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2022-03-24 03:27:40 +03:00
|
|
|
bool PromiseWorkerProxy::OnWritingThread() const {
|
|
|
|
return IsCurrentThreadRunningWorker();
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
Promise* PromiseWorkerProxy::WorkerPromise() const {
|
2018-03-14 17:25:47 +03:00
|
|
|
MOZ_ASSERT(IsCurrentThreadRunningWorker());
|
2015-09-02 20:07:26 +03:00
|
|
|
MOZ_ASSERT(mWorkerPromise);
|
2014-02-24 17:56:54 +04:00
|
|
|
return mWorkerPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::RunCallback(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
RunCallbackFunc aFunc) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(Lock());
|
2014-02-24 17:56:54 +04:00
|
|
|
// If the worker thread's been cancelled we don't need to resolve the Promise.
|
2015-09-02 20:07:26 +03:00
|
|
|
if (CleanedUp()) {
|
2014-02-24 17:56:54 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
// The |aValue| is written into the StructuredCloneHolderBase.
|
2015-09-05 12:22:13 +03:00
|
|
|
if (!Write(aCx, aValue)) {
|
2014-02-24 17:56:54 +04:00
|
|
|
JS_ClearPendingException(aCx);
|
2015-09-05 12:22:13 +03:00
|
|
|
MOZ_ASSERT(false,
|
|
|
|
"cannot serialize the value with the StructuredCloneAlgorithm!");
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxyRunnable> runnable =
|
2015-09-05 12:22:13 +03:00
|
|
|
new PromiseWorkerProxyRunnable(this, aFunc);
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
runnable->Dispatch();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::ResolvedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2016-02-10 01:40:31 +03:00
|
|
|
RunCallback(aCx, aValue, &Promise::MaybeResolve);
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void PromiseWorkerProxy::RejectedCallback(JSContext* aCx,
|
2022-01-20 22:11:39 +03:00
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
ErrorResult& aRv) {
|
2016-02-10 01:40:31 +03:00
|
|
|
RunCallback(aCx, aValue, &Promise::MaybeReject);
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2016-02-29 22:52:42 +03:00
|
|
|
void PromiseWorkerProxy::CleanUp() {
|
2015-09-02 20:07:26 +03:00
|
|
|
// Can't release Mutex while it is still locked, so scope the lock.
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(Lock());
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
if (CleanedUp()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2022-01-04 16:58:23 +03:00
|
|
|
// We can be called if we failed to get a WorkerRef
|
|
|
|
if (mWorkerRef) {
|
|
|
|
mWorkerRef->Private()->AssertIsOnWorkerThread();
|
|
|
|
}
|
2014-02-24 17:56:54 +04:00
|
|
|
|
2016-07-18 10:14:14 +03:00
|
|
|
// Release the Promise and remove the PromiseWorkerProxy from the holders of
|
2015-09-02 20:07:26 +03:00
|
|
|
// the worker thread since the Promise has been resolved/rejected or the
|
|
|
|
// worker thread has been cancelled.
|
2021-12-16 22:35:44 +03:00
|
|
|
mCleanedUp = true;
|
|
|
|
mWorkerPromise = nullptr;
|
|
|
|
mWorkerRef = nullptr;
|
|
|
|
|
|
|
|
// Clear the StructuredCloneHolderBase class.
|
|
|
|
Clear();
|
2015-09-02 20:07:26 +03:00
|
|
|
}
|
|
|
|
Release();
|
2014-02-24 17:56:54 +04:00
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
JSObject* PromiseWorkerProxy::CustomReadHandler(
|
2020-01-24 19:31:21 +03:00
|
|
|
JSContext* aCx, JSStructuredCloneReader* aReader,
|
|
|
|
const JS::CloneDataPolicy& aCloneDataPolicy, uint32_t aTag,
|
2015-09-30 15:22:08 +03:00
|
|
|
uint32_t aIndex) {
|
2015-09-05 12:22:13 +03:00
|
|
|
if (NS_WARN_IF(!mCallbacks)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCallbacks->Read(aCx, aReader, this, aTag, aIndex);
|
|
|
|
}
|
|
|
|
|
2015-09-30 15:22:08 +03:00
|
|
|
bool PromiseWorkerProxy::CustomWriteHandler(JSContext* aCx,
|
|
|
|
JSStructuredCloneWriter* aWriter,
|
2020-01-24 10:57:56 +03:00
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
bool* aSameProcessScopeRequired) {
|
2015-09-05 12:22:13 +03:00
|
|
|
if (NS_WARN_IF(!mCallbacks)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCallbacks->Write(aCx, aWriter, this, aObj);
|
|
|
|
}
|
|
|
|
|
2014-06-03 19:38:38 +04:00
|
|
|
// Specializations of MaybeRejectBrokenly we actually support.
|
|
|
|
template <>
|
2017-08-06 20:47:00 +03:00
|
|
|
void Promise::MaybeRejectBrokenly(const RefPtr<DOMException>& aArg) {
|
2014-06-03 19:38:38 +04:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
template <>
|
|
|
|
void Promise::MaybeRejectBrokenly(const nsAString& aArg) {
|
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2016-09-07 05:51:41 +03:00
|
|
|
Promise::PromiseState Promise::State() const {
|
|
|
|
JS::Rooted<JSObject*> p(RootingCx(), PromiseObj());
|
|
|
|
const JS::PromiseState state = JS::GetPromiseState(p);
|
|
|
|
|
|
|
|
if (state == JS::PromiseState::Fulfilled) {
|
|
|
|
return PromiseState::Resolved;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (state == JS::PromiseState::Rejected) {
|
|
|
|
return PromiseState::Rejected;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PromiseState::Pending;
|
|
|
|
}
|
|
|
|
|
2022-02-25 05:32:04 +03:00
|
|
|
bool Promise::SetSettledPromiseIsHandled() {
|
2020-11-23 19:10:47 +03:00
|
|
|
AutoAllowLegacyScriptExecution exemption;
|
|
|
|
AutoEntryScript aes(mGlobal, "Set settled promise handled");
|
|
|
|
JSContext* cx = aes.cx();
|
2022-06-05 23:55:00 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(cx, mPromiseObj);
|
2022-02-25 05:32:04 +03:00
|
|
|
return JS::SetSettledPromiseIsHandled(cx, promiseObj);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Promise::SetAnyPromiseIsHandled() {
|
|
|
|
AutoAllowLegacyScriptExecution exemption;
|
|
|
|
AutoEntryScript aes(mGlobal, "Set any promise handled");
|
|
|
|
JSContext* cx = aes.cx();
|
2022-06-05 23:55:00 +03:00
|
|
|
JS::Rooted<JSObject*> promiseObj(cx, mPromiseObj);
|
2022-02-25 05:32:04 +03:00
|
|
|
return JS::SetAnyPromiseIsHandled(cx, promiseObj);
|
2020-11-23 19:10:47 +03:00
|
|
|
}
|
|
|
|
|
2021-11-16 01:01:38 +03:00
|
|
|
/* static */
|
|
|
|
already_AddRefed<Promise> Promise::CreateResolvedWithUndefined(
|
|
|
|
nsIGlobalObject* global, ErrorResult& aRv) {
|
|
|
|
RefPtr<Promise> returnPromise = Promise::Create(global, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
returnPromise->MaybeResolveWithUndefined();
|
|
|
|
return returnPromise.forget();
|
|
|
|
}
|
|
|
|
|
2022-08-09 21:36:02 +03:00
|
|
|
already_AddRefed<Promise> Promise::CreateRejected(
|
|
|
|
nsIGlobalObject* aGlobal, JS::Handle<JS::Value> aRejectionError,
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
RefPtr<Promise> promise = Promise::Create(aGlobal, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
promise->MaybeReject(aRejectionError);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> Promise::CreateRejectedWithTypeError(
|
|
|
|
nsIGlobalObject* aGlobal, const nsACString& aMessage, ErrorResult& aRv) {
|
|
|
|
RefPtr<Promise> returnPromise = Promise::Create(aGlobal, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
returnPromise->MaybeRejectWithTypeError(aMessage);
|
|
|
|
return returnPromise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise> Promise::CreateRejectedWithErrorResult(
|
|
|
|
nsIGlobalObject* aGlobal, ErrorResult& aRejectionError) {
|
|
|
|
RefPtr<Promise> returnPromise = Promise::Create(aGlobal, IgnoreErrors());
|
|
|
|
if (!returnPromise) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
returnPromise->MaybeReject(std::move(aRejectionError));
|
|
|
|
return returnPromise.forget();
|
|
|
|
}
|
|
|
|
|
2022-03-22 19:44:58 +03:00
|
|
|
} // namespace mozilla::dom
|
2020-12-12 05:55:49 +03:00
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
|
|
|
|
// These functions are used in the implementation of ffi bindings for
|
|
|
|
// dom::Promise from Rust.
|
|
|
|
|
|
|
|
void DomPromise_AddRef(mozilla::dom::Promise* aPromise) {
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
aPromise->AddRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DomPromise_Release(mozilla::dom::Promise* aPromise) {
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
aPromise->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
#define DOM_PROMISE_FUNC_WITH_VARIANT(name, func) \
|
|
|
|
void name(mozilla::dom::Promise* aPromise, nsIVariant* aVariant) { \
|
|
|
|
MOZ_ASSERT(aPromise); \
|
|
|
|
MOZ_ASSERT(aVariant); \
|
|
|
|
mozilla::dom::AutoEntryScript aes(aPromise->GetGlobalObject(), \
|
|
|
|
"Promise resolution or rejection"); \
|
|
|
|
JSContext* cx = aes.cx(); \
|
|
|
|
\
|
|
|
|
JS::Rooted<JS::Value> val(cx); \
|
|
|
|
nsresult rv = NS_OK; \
|
|
|
|
if (!XPCVariant::VariantDataToJS(cx, aVariant, &rv, &val)) { \
|
|
|
|
aPromise->MaybeRejectWithTypeError( \
|
|
|
|
"Failed to convert nsIVariant to JS"); \
|
|
|
|
return; \
|
|
|
|
} \
|
|
|
|
aPromise->func(val); \
|
|
|
|
}
|
|
|
|
|
|
|
|
DOM_PROMISE_FUNC_WITH_VARIANT(DomPromise_RejectWithVariant, MaybeReject)
|
|
|
|
DOM_PROMISE_FUNC_WITH_VARIANT(DomPromise_ResolveWithVariant, MaybeResolve)
|
|
|
|
|
|
|
|
#undef DOM_PROMISE_FUNC_WITH_VARIANT
|
|
|
|
}
|