2015-05-03 19:32:37 +00: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 01:41:21 +00: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-11 20:40:36 +00:00
|
|
|
#ifndef mozilla_dom_Promise_h
|
|
|
|
#define mozilla_dom_Promise_h
|
2013-06-12 01:41:21 +00:00
|
|
|
|
|
|
|
#include "mozilla/Attributes.h"
|
|
|
|
#include "mozilla/ErrorResult.h"
|
2014-10-20 17:02:21 +00:00
|
|
|
#include "mozilla/TimeStamp.h"
|
2014-02-25 21:34:55 +00:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2013-06-12 01:41:21 +00:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2013-07-11 20:40:36 +00:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-04-10 18:57:07 +00:00
|
|
|
#include "mozilla/dom/ToJSValue.h"
|
2014-08-01 16:42:19 +00:00
|
|
|
#include "mozilla/WeakPtr.h"
|
2013-06-12 01:41:21 +00:00
|
|
|
#include "nsWrapperCache.h"
|
|
|
|
#include "nsAutoPtr.h"
|
2013-08-28 02:59:14 +00:00
|
|
|
#include "js/TypeDecls.h"
|
2015-01-15 22:39:02 +00:00
|
|
|
#include "jspubtd.h"
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
// Bug 1083361 introduces a new mechanism for tracking uncaught
|
|
|
|
// rejections. This #define serves to track down the parts of code
|
|
|
|
// that need to be removed once clients have been put together
|
|
|
|
// to take advantage of the new mechanism. New code should not
|
|
|
|
// depend on code #ifdefed to this #define.
|
|
|
|
#define DOM_PROMISE_DEPRECATED_REPORTING 1
|
|
|
|
|
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
#include "mozilla/dom/workers/bindings/WorkerFeature.h"
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
|
2014-02-25 21:34:55 +00:00
|
|
|
class nsIGlobalObject;
|
|
|
|
|
2013-06-12 01:41:21 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
class AnyCallback;
|
2014-06-03 15:38:38 +00:00
|
|
|
class DOMError;
|
2014-09-20 06:20:41 +00:00
|
|
|
class MediaStreamError;
|
2013-11-19 18:43:51 +00:00
|
|
|
class PromiseCallback;
|
|
|
|
class PromiseInit;
|
|
|
|
class PromiseNativeHandler;
|
2014-07-02 04:19:48 +00:00
|
|
|
class PromiseDebugging;
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2014-03-12 14:31:03 +00:00
|
|
|
class Promise;
|
2015-04-10 15:27:57 +00:00
|
|
|
|
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
class PromiseReportRejectFeature : public workers::WorkerFeature
|
|
|
|
{
|
2015-05-22 05:17:00 +00:00
|
|
|
// PromiseReportRejectFeature is held by an nsAutoPtr on the Promise which
|
|
|
|
// means that this object will be destroyed before the Promise is destroyed.
|
|
|
|
Promise* MOZ_NON_OWNING_REF mPromise;
|
2014-03-12 14:31:03 +00:00
|
|
|
|
|
|
|
public:
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit PromiseReportRejectFeature(Promise* aPromise)
|
2014-03-12 14:31:03 +00:00
|
|
|
: mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 16:28:04 +00:00
|
|
|
Notify(JSContext* aCx, workers::Status aStatus) override;
|
2014-03-12 14:31:03 +00:00
|
|
|
};
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
|
2014-12-26 02:11:20 +00:00
|
|
|
#define NS_PROMISE_IID \
|
|
|
|
{ 0x1b8d6215, 0x3e67, 0x43ba, \
|
|
|
|
{ 0x8a, 0xf9, 0x31, 0x5e, 0x8f, 0xce, 0x75, 0x65 } }
|
|
|
|
|
2014-09-12 02:18:49 +00:00
|
|
|
class Promise : public nsISupports,
|
|
|
|
public nsWrapperCache,
|
|
|
|
public SupportsWeakPtr<Promise>
|
2013-06-12 01:41:21 +00:00
|
|
|
{
|
2013-11-19 18:43:51 +00:00
|
|
|
friend class NativePromiseCallback;
|
2015-08-06 15:18:30 +00:00
|
|
|
friend class PromiseReactionJob;
|
2013-07-11 20:40:36 +00:00
|
|
|
friend class PromiseResolverTask;
|
2013-11-24 19:26:07 +00:00
|
|
|
friend class PromiseTask;
|
2015-04-10 15:27:57 +00:00
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
friend class PromiseReportRejectFeature;
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-02-24 13:56:54 +00:00
|
|
|
friend class PromiseWorkerProxy;
|
|
|
|
friend class PromiseWorkerProxyRunnable;
|
2013-09-11 16:03:04 +00:00
|
|
|
friend class RejectPromiseCallback;
|
2013-11-24 19:26:07 +00:00
|
|
|
friend class ResolvePromiseCallback;
|
2015-08-03 16:48:34 +00:00
|
|
|
friend class PromiseResolveThenableJob;
|
|
|
|
friend class FastPromiseResolveThenableJob;
|
2013-09-11 16:03:04 +00:00
|
|
|
friend class WrapperPromiseCallback;
|
2013-06-12 01:41:21 +00:00
|
|
|
|
|
|
|
public:
|
2014-12-26 02:11:20 +00:00
|
|
|
NS_DECLARE_STATIC_IID_ACCESSOR(NS_PROMISE_IID)
|
2013-06-12 01:41:21 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
2015-02-24 22:24:45 +00:00
|
|
|
NS_DECL_CYCLE_COLLECTION_SKIPPABLE_SCRIPT_HOLDER_CLASS(Promise)
|
2015-04-24 16:43:01 +00:00
|
|
|
MOZ_DECLARE_WEAKREFERENCE_TYPENAME(Promise)
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2014-07-19 01:31:11 +00:00
|
|
|
// Promise creation tries to create a JS reflector for the Promise, so is
|
|
|
|
// fallible. Furthermore, we don't want to do JS-wrapping on a 0-refcount
|
|
|
|
// object, so we addref before doing that and return the addrefed pointer
|
|
|
|
// here.
|
|
|
|
static already_AddRefed<Promise>
|
2015-07-31 17:30:55 +00:00
|
|
|
Create(nsIGlobalObject* aGlobal, ErrorResult& aRv,
|
|
|
|
// Passing null for aDesiredProto will use Promise.prototype.
|
|
|
|
JS::Handle<JSObject*> aDesiredProto = nullptr);
|
2014-02-25 21:34:55 +00:00
|
|
|
|
|
|
|
typedef void (Promise::*MaybeFunc)(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2013-09-26 18:09:16 +00:00
|
|
|
void MaybeResolve(JSContext* aCx,
|
2013-11-19 18:39:51 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 18:09:16 +00:00
|
|
|
void MaybeReject(JSContext* aCx,
|
2013-11-19 18:39:51 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 18:09:16 +00:00
|
|
|
|
2014-02-25 21:34:55 +00:00
|
|
|
// Helpers for using Promise from C++.
|
2014-04-16 19:13:41 +00:00
|
|
|
// 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
|
2014-02-25 21:34:55 +00:00
|
|
|
template <typename T>
|
2014-04-16 19:13:41 +00:00
|
|
|
void MaybeResolve(const T& aArg) {
|
2014-02-25 21:34:55 +00:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeResolve);
|
|
|
|
}
|
|
|
|
|
2014-06-03 15:38:38 +00:00
|
|
|
inline void MaybeReject(nsresult aArg) {
|
|
|
|
MOZ_ASSERT(NS_FAILED(aArg));
|
2014-02-25 21:34:55 +00:00
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
2014-09-20 06:20:41 +00:00
|
|
|
|
2014-10-30 15:12:09 +00:00
|
|
|
inline void MaybeReject(ErrorResult& aArg) {
|
|
|
|
MOZ_ASSERT(aArg.Failed());
|
|
|
|
MaybeSomething(aArg, &Promise::MaybeReject);
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
void MaybeReject(const RefPtr<MediaStreamError>& aArg);
|
2014-09-20 06:20:41 +00:00
|
|
|
|
2014-06-03 15:38:38 +00:00
|
|
|
// 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 DOMError
|
|
|
|
// without instantiating the DOMError specialization of MaybeSomething in
|
|
|
|
// every translation unit that includes this header, because that would
|
|
|
|
// require use to include DOMError.h either here or in all those translation
|
|
|
|
// units.
|
|
|
|
template<typename T>
|
|
|
|
void MaybeRejectBrokenly(const T& aArg); // Not implemented by default; see
|
|
|
|
// specializations in the .cpp for
|
|
|
|
// the T values we support.
|
2014-02-25 21:34:55 +00:00
|
|
|
|
2014-10-28 12:08:19 +00:00
|
|
|
// Called by DOM to let us execute our callbacks. May be called recursively.
|
2014-11-11 13:47:28 +00:00
|
|
|
// Returns true if at least one microtask was processed.
|
|
|
|
static bool PerformMicroTaskCheckpoint();
|
2014-10-28 12:08:19 +00:00
|
|
|
|
2013-06-12 01:41:21 +00:00
|
|
|
// WebIDL
|
|
|
|
|
2014-02-25 21:34:55 +00:00
|
|
|
nsIGlobalObject* GetParentObject() const
|
2013-06-12 01:41:21 +00:00
|
|
|
{
|
2014-02-25 21:34:55 +00:00
|
|
|
return mGlobal;
|
2013-06-12 01:41:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject*
|
2015-03-21 16:28:04 +00:00
|
|
|
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2013-07-11 20:40:36 +00:00
|
|
|
static already_AddRefed<Promise>
|
2013-08-23 05:17:08 +00:00
|
|
|
Constructor(const GlobalObject& aGlobal, PromiseInit& aInit,
|
2015-07-31 17:30:55 +00:00
|
|
|
ErrorResult& aRv, JS::Handle<JSObject*> aDesiredProto);
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
static already_AddRefed<Promise>
|
2015-11-25 20:48:08 +00:00
|
|
|
Resolve(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
|
2015-11-26 05:02:55 +00:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
2013-06-12 01:41:22 +00:00
|
|
|
|
2014-01-13 22:36:03 +00:00
|
|
|
static already_AddRefed<Promise>
|
2014-02-25 21:34:55 +00:00
|
|
|
Resolve(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-01-13 22:36:03 +00:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
static already_AddRefed<Promise>
|
2015-11-25 20:48:08 +00:00
|
|
|
Reject(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
|
2015-11-26 05:02:55 +00:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
2013-06-12 01:41:22 +00:00
|
|
|
|
2014-01-13 22:36:03 +00:00
|
|
|
static already_AddRefed<Promise>
|
2014-02-25 21:34:55 +00:00
|
|
|
Reject(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-01-13 22:36:03 +00:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv);
|
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Then(JSContext* aCx, AnyCallback* aResolveCallback,
|
|
|
|
AnyCallback* aRejectCallback, ErrorResult& aRv);
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
Catch(JSContext* aCx, AnyCallback* aRejectCallback, ErrorResult& aRv);
|
2013-11-19 21:53:00 +00:00
|
|
|
|
2015-04-29 15:59:43 +00:00
|
|
|
static already_AddRefed<Promise>
|
2015-11-25 20:48:08 +00:00
|
|
|
All(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
|
2015-11-26 05:02:55 +00:00
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
2015-04-29 15:59:43 +00:00
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
static already_AddRefed<Promise>
|
|
|
|
All(const GlobalObject& aGlobal,
|
|
|
|
const nsTArray<RefPtr<Promise>>& aPromiseList, ErrorResult& aRv);
|
2013-11-19 21:53:00 +00:00
|
|
|
|
2015-11-26 05:02:55 +00:00
|
|
|
static already_AddRefed<Promise>
|
2015-11-25 20:48:08 +00:00
|
|
|
Race(const GlobalObject& aGlobal, JS::Handle<JS::Value> aThisv,
|
2015-11-26 05:02:55 +00:00
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv);
|
2015-11-25 20:48:08 +00:00
|
|
|
|
2015-11-25 20:48:08 +00:00
|
|
|
static bool
|
|
|
|
PromiseSpecies(JSContext* aCx, unsigned aArgc, JS::Value* aVp);
|
|
|
|
|
2013-11-19 18:43:51 +00:00
|
|
|
void AppendNativeHandler(PromiseNativeHandler* aRunnable);
|
|
|
|
|
2015-01-15 22:39:02 +00:00
|
|
|
JSObject* GlobalJSObject() const;
|
|
|
|
|
|
|
|
JSCompartment* Compartment() const;
|
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
// Return a unique-to-the-process identifier for this Promise.
|
|
|
|
uint64_t GetID();
|
|
|
|
|
2015-07-14 19:44:00 +00:00
|
|
|
// Queue an async microtask to current main or worker thread.
|
|
|
|
static void
|
|
|
|
DispatchToMicroTask(nsIRunnable* aRunnable);
|
|
|
|
|
2014-09-12 02:18:49 +00:00
|
|
|
protected:
|
2015-11-25 20:48:08 +00:00
|
|
|
struct PromiseCapability;
|
|
|
|
|
2014-07-19 01:31:11 +00:00
|
|
|
// Do NOT call this unless you're Promise::Create. I wish we could enforce
|
|
|
|
// that from inside this class too, somehow.
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit Promise(nsIGlobalObject* aGlobal);
|
2014-07-19 01:31:11 +00:00
|
|
|
|
2014-09-12 02:18:49 +00:00
|
|
|
virtual ~Promise();
|
|
|
|
|
2015-07-31 17:30:55 +00:00
|
|
|
// 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);
|
2014-09-12 02:18:49 +00:00
|
|
|
|
|
|
|
// Create the JS resolving functions of resolve() and reject(). And provide
|
|
|
|
// references to the two functions by calling PromiseInit passed from Promise
|
|
|
|
// constructor.
|
|
|
|
void CallInitFunction(const GlobalObject& aGlobal, PromiseInit& aInit,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2015-11-25 20:48:09 +00:00
|
|
|
// The NewPromiseCapability function from
|
|
|
|
// <http://www.ecma-international.org/ecma-262/6.0/#sec-newpromisecapability>.
|
|
|
|
// Errors are communicated via aRv. If aForceCallbackCreation is
|
|
|
|
// true, then this function will ensure that aCapability has a
|
|
|
|
// useful mResolve/mReject even if mNativePromise is non-null.
|
|
|
|
static void NewPromiseCapability(JSContext* aCx, nsIGlobalObject* aGlobal,
|
|
|
|
JS::Handle<JS::Value> aConstructor,
|
|
|
|
bool aForceCallbackCreation,
|
|
|
|
PromiseCapability& aCapability,
|
|
|
|
ErrorResult& aRv);
|
|
|
|
|
2014-09-12 02:18:49 +00:00
|
|
|
bool IsPending()
|
|
|
|
{
|
|
|
|
return mResolvePending;
|
|
|
|
}
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
void GetDependentPromises(nsTArray<RefPtr<Promise>>& aPromises);
|
2014-10-20 02:27:36 +00:00
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
bool IsLastInChain() const
|
|
|
|
{
|
|
|
|
return mIsLastInChain;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetNotifiedAsUncaught()
|
|
|
|
{
|
|
|
|
mWasNotifiedAsUncaught = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool WasNotifiedAsUncaught() const
|
|
|
|
{
|
|
|
|
return mWasNotifiedAsUncaught;
|
|
|
|
}
|
|
|
|
|
2014-09-12 02:18:49 +00:00
|
|
|
private:
|
2014-07-02 04:19:48 +00:00
|
|
|
friend class PromiseDebugging;
|
|
|
|
|
2013-07-11 20:40:36 +00:00
|
|
|
enum PromiseState {
|
2013-06-12 01:41:21 +00:00
|
|
|
Pending,
|
|
|
|
Resolved,
|
|
|
|
Rejected
|
|
|
|
};
|
|
|
|
|
2013-07-11 20:40:36 +00:00
|
|
|
void SetState(PromiseState aState)
|
2013-06-12 01:41:21 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState == Pending);
|
|
|
|
MOZ_ASSERT(aState != Pending);
|
|
|
|
mState = aState;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetResult(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
mResult = aValue;
|
|
|
|
}
|
|
|
|
|
2014-10-28 12:08:19 +00:00
|
|
|
// This method enqueues promise's resolve/reject callbacks with promise's
|
2013-06-12 01:41:21 +00:00
|
|
|
// result. It's executed when the resolver.resolve() or resolver.reject() is
|
2013-07-11 20:40:36 +00:00
|
|
|
// called or when the promise already has a result and new callbacks are
|
2015-08-06 15:18:30 +00:00
|
|
|
// appended by then() or catch().
|
|
|
|
void TriggerPromiseReactions();
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2014-11-17 09:44:00 +00:00
|
|
|
void Settle(JS::Handle<JS::Value> aValue, Promise::PromiseState aState);
|
|
|
|
void MaybeSettle(JS::Handle<JS::Value> aValue, Promise::PromiseState aState);
|
2013-09-11 16:03:04 +00:00
|
|
|
|
2013-07-11 20:40:36 +00:00
|
|
|
void AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback);
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2013-08-29 04:30:06 +00:00
|
|
|
// If we have been rejected and our mResult is a JS exception,
|
|
|
|
// report it to the error console.
|
2014-03-12 14:31:03 +00:00
|
|
|
// Use MaybeReportRejectedOnce() for actual calls.
|
2013-08-29 04:30:06 +00:00
|
|
|
void MaybeReportRejected();
|
|
|
|
|
2014-03-12 14:31:03 +00:00
|
|
|
void MaybeReportRejectedOnce() {
|
|
|
|
MaybeReportRejected();
|
|
|
|
RemoveFeature();
|
2015-01-14 07:59:06 +00:00
|
|
|
mResult.setUndefined();
|
2014-03-12 14:31:03 +00:00
|
|
|
}
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
|
2013-09-26 18:09:16 +00:00
|
|
|
void MaybeResolveInternal(JSContext* aCx,
|
2014-10-28 12:08:19 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-26 18:09:16 +00:00
|
|
|
void MaybeRejectInternal(JSContext* aCx,
|
2014-10-28 12:08:19 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-11 16:03:04 +00:00
|
|
|
|
|
|
|
void ResolveInternal(JSContext* aCx,
|
2014-10-28 12:08:19 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-11 16:03:04 +00:00
|
|
|
void RejectInternal(JSContext* aCx,
|
2014-10-28 12:08:19 +00:00
|
|
|
JS::Handle<JS::Value> aValue);
|
2013-09-11 16:03:04 +00:00
|
|
|
|
2014-02-25 21:34:55 +00:00
|
|
|
template <typename T>
|
|
|
|
void MaybeSomething(T& aArgument, MaybeFunc aFunc) {
|
|
|
|
ThreadsafeAutoJSContext cx;
|
2014-07-19 01:31:11 +00:00
|
|
|
JSObject* wrapper = GetWrapper();
|
|
|
|
MOZ_ASSERT(wrapper); // We preserved it!
|
2014-02-25 21:34:55 +00:00
|
|
|
|
|
|
|
JSAutoCompartment ac(cx, wrapper);
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
2014-04-10 18:57:07 +00:00
|
|
|
if (!ToJSValue(cx, aArgument, &val)) {
|
2014-02-25 21:34:55 +00:00
|
|
|
HandleException(cx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(this->*aFunc)(cx, val);
|
|
|
|
}
|
|
|
|
|
2013-09-11 16:03:04 +00:00
|
|
|
// Static methods for the PromiseInit functions.
|
|
|
|
static bool
|
|
|
|
JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
2014-01-23 18:47:29 +00:00
|
|
|
|
|
|
|
static bool
|
|
|
|
ThenableResolverCommon(JSContext* aCx, uint32_t /* PromiseCallback::Task */ aTask,
|
|
|
|
unsigned aArgc, JS::Value* aVp);
|
|
|
|
static bool
|
|
|
|
JSCallbackThenableResolver(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
|
|
|
static bool
|
|
|
|
JSCallbackThenableRejecter(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
|
|
|
|
|
2013-09-11 16:03:04 +00:00
|
|
|
static JSObject*
|
2015-03-09 16:50:07 +00:00
|
|
|
CreateFunction(JSContext* aCx, Promise* aPromise, int32_t aTask);
|
2013-09-11 16:03:04 +00:00
|
|
|
|
2014-01-23 18:47:29 +00:00
|
|
|
static JSObject*
|
|
|
|
CreateThenableFunction(JSContext* aCx, Promise* aPromise, uint32_t aTask);
|
|
|
|
|
|
|
|
void HandleException(JSContext* aCx);
|
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
void RemoveFeature();
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
2014-03-12 14:31:03 +00:00
|
|
|
|
2014-10-20 02:27:12 +00:00
|
|
|
// Capture the current stack and store it in aTarget. If false is
|
|
|
|
// returned, an exception is presumably pending on aCx.
|
|
|
|
bool CaptureStack(JSContext* aCx, JS::Heap<JSObject*>& aTarget);
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<nsIGlobalObject> mGlobal;
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<PromiseCallback> > mResolveCallbacks;
|
|
|
|
nsTArray<RefPtr<PromiseCallback> > mRejectCallbacks;
|
2013-06-12 01:41:21 +00:00
|
|
|
|
2013-06-18 10:00:37 +00:00
|
|
|
JS::Heap<JS::Value> mResult;
|
2014-10-20 02:27:12 +00:00
|
|
|
// A stack that shows where this promise was allocated, if there was
|
|
|
|
// JS running at the time. Otherwise null.
|
|
|
|
JS::Heap<JSObject*> mAllocationStack;
|
2014-10-20 02:27:12 +00:00
|
|
|
// mRejectionStack is only set when the promise is rejected directly from
|
|
|
|
// script, by calling Promise.reject() or the rejection callback we pass to
|
|
|
|
// the PromiseInit function. Promises that are rejected internally do not
|
|
|
|
// have a rejection stack.
|
|
|
|
JS::Heap<JSObject*> mRejectionStack;
|
2014-10-20 02:27:12 +00:00
|
|
|
// mFullfillmentStack is only set when the promise is fulfilled directly from
|
|
|
|
// script, by calling Promise.resolve() or the fulfillment callback we pass to
|
|
|
|
// the PromiseInit function. Promises that are fulfilled internally do not
|
|
|
|
// have a fulfillment stack.
|
|
|
|
JS::Heap<JSObject*> mFullfillmentStack;
|
2013-07-11 20:40:36 +00:00
|
|
|
PromiseState mState;
|
2014-03-12 14:31:03 +00:00
|
|
|
|
2015-04-10 15:27:57 +00:00
|
|
|
#if defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
|
|
|
bool mHadRejectCallback;
|
2014-11-20 08:51:03 +00:00
|
|
|
|
2014-03-12 14:31:03 +00:00
|
|
|
// If a rejected promise on a worker has no reject callbacks attached, it
|
|
|
|
// needs to know when the worker is shutting down, to report the error on the
|
|
|
|
// console before the worker's context is deleted. This feature is used for
|
|
|
|
// that purpose.
|
|
|
|
nsAutoPtr<PromiseReportRejectFeature> mFeature;
|
2015-04-10 15:27:57 +00:00
|
|
|
#endif // defined(DOM_PROMISE_DEPRECATED_REPORTING)
|
|
|
|
|
|
|
|
bool mTaskPending;
|
|
|
|
bool mResolvePending;
|
|
|
|
|
|
|
|
// `true` if this Promise is the last in the chain, or `false` if
|
|
|
|
// another Promise has been created from this one by a call to
|
|
|
|
// `then`, `all`, `race`, etc.
|
|
|
|
bool mIsLastInChain;
|
|
|
|
|
|
|
|
// `true` if PromiseDebugging has already notified at least one observer that
|
|
|
|
// this promise was left uncaught, `false` otherwise.
|
|
|
|
bool mWasNotifiedAsUncaught;
|
2014-10-20 17:02:21 +00:00
|
|
|
|
|
|
|
// The time when this promise was created.
|
|
|
|
TimeStamp mCreationTimestamp;
|
|
|
|
|
|
|
|
// The time when this promise transitioned out of the pending state.
|
|
|
|
TimeStamp mSettlementTimestamp;
|
2015-04-10 15:27:57 +00:00
|
|
|
|
|
|
|
// Once `GetID()` has been called, a unique-to-the-process identifier for this
|
|
|
|
// promise. Until then, `0`.
|
|
|
|
uint64_t mID;
|
2013-06-12 01:41:21 +00:00
|
|
|
};
|
|
|
|
|
2014-12-26 02:11:20 +00:00
|
|
|
NS_DEFINE_STATIC_IID_ACCESSOR(Promise, NS_PROMISE_IID)
|
|
|
|
|
2013-06-12 01:41:21 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2013-07-11 20:40:36 +00:00
|
|
|
#endif // mozilla_dom_Promise_h
|