Bug 911213 - Implement new promise constructor, r=bz

This commit is contained in:
Andrea Marchesini 2013-09-11 18:03:04 +02:00
parent f697a6c5ed
commit 2b892b82b4
16 changed files with 375 additions and 389 deletions

View File

@ -545,8 +545,8 @@ MozInputContext.prototype = {
getText: function ic_getText(offset, length) { getText: function ic_getText(offset, length) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage('Keyboard:GetText', { cpmm.sendAsyncMessage('Keyboard:GetText', {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,
@ -574,8 +574,8 @@ MozInputContext.prototype = {
setSelectionRange: function ic_setSelectionRange(start, length) { setSelectionRange: function ic_setSelectionRange(start, length) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage("Keyboard:SetSelectionRange", { cpmm.sendAsyncMessage("Keyboard:SetSelectionRange", {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,
@ -603,8 +603,8 @@ MozInputContext.prototype = {
replaceSurroundingText: function ic_replaceSurrText(text, offset, length) { replaceSurroundingText: function ic_replaceSurrText(text, offset, length) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage('Keyboard:ReplaceSurroundingText', { cpmm.sendAsyncMessage('Keyboard:ReplaceSurroundingText', {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,
@ -621,8 +621,8 @@ MozInputContext.prototype = {
sendKey: function ic_sendKey(keyCode, charCode, modifiers) { sendKey: function ic_sendKey(keyCode, charCode, modifiers) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage('Keyboard:SendKey', { cpmm.sendAsyncMessage('Keyboard:SendKey', {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,
@ -635,8 +635,8 @@ MozInputContext.prototype = {
setComposition: function ic_setComposition(text, cursor, clauses) { setComposition: function ic_setComposition(text, cursor, clauses) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage('Keyboard:SetComposition', { cpmm.sendAsyncMessage('Keyboard:SetComposition', {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,
@ -649,8 +649,8 @@ MozInputContext.prototype = {
endComposition: function ic_endComposition(text) { endComposition: function ic_endComposition(text) {
let self = this; let self = this;
return this.createPromise(function(resolver) { return this.createPromise(function(resolve, reject) {
let resolverId = self.getPromiseResolverId(resolver); let resolverId = self.getPromiseResolverId({ resolve: resolve, reject: reject });
cpmm.sendAsyncMessage('Keyboard:EndComposition', { cpmm.sendAsyncMessage('Keyboard:EndComposition', {
contextId: self._contextId, contextId: self._contextId,
requestId: resolverId, requestId: resolverId,

View File

@ -221,7 +221,8 @@ DOMRequestIpcHelper.prototype = {
forEachPromiseResolver: function(aCallback) { forEachPromiseResolver: function(aCallback) {
Object.keys(this._requests).forEach(function(k) { Object.keys(this._requests).forEach(function(k) {
if (this.getPromiseResolver(k) instanceof this._window.PromiseResolver) { if ("resolve" in this.getPromiseResolver(k) &&
"reject" in this.getPromiseResolver(k)) {
aCallback(k); aCallback(k);
} }
}, this); }, this);

View File

@ -30,9 +30,8 @@
function createPromise() { function createPromise() {
ok(Promise, "Promise object should exist"); ok(Promise, "Promise object should exist");
var promise = dummy.createPromise(function(r) { var promise = dummy.createPromise(function(resolve, reject) {
ok(r, "received PromiseResolver"); resolve(true);
r.resolve(true);
}); });
ok(promise instanceof Promise, "returned a Promise"); ok(promise instanceof Promise, "returned a Promise");
promise.then(runTest); promise.then(runTest);
@ -41,7 +40,8 @@
function getResolver() { function getResolver() {
var id; var id;
var resolver; var resolver;
var promise = dummy.createPromise(function(r) { var promise = dummy.createPromise(function(resolve, reject) {
var r = { resolve: resolve, reject: reject };
id = dummy.getPromiseResolverId(r); id = dummy.getPromiseResolverId(r);
resolver = r; resolver = r;
ok(typeof id === "string", "id should be string"); ok(typeof id === "string", "id should be string");
@ -55,7 +55,8 @@
function removeResolver() { function removeResolver() {
var id; var id;
var promise = dummy.createPromise(function(r) { var promise = dummy.createPromise(function(resolve, reject) {
var r = { resolve: resolve, reject: reject };
id = dummy.getPromiseResolverId(r); id = dummy.getPromiseResolverId(r);
ok(typeof id === "string", "id should be string"); ok(typeof id === "string", "id should be string");
@ -74,7 +75,8 @@
function takeResolver() { function takeResolver() {
var id; var id;
var resolver; var resolver;
var promise = dummy.createPromise(function(r) { var promise = dummy.createPromise(function(resolve, reject) {
var r = { resolve: resolve, reject: reject };
id = dummy.getPromiseResolverId(r); id = dummy.getPromiseResolverId(r);
resolver = r; resolver = r;
ok(typeof id === "string", "id should be string"); ok(typeof id === "string", "id should be string");

View File

@ -9,7 +9,6 @@
#include "jsfriendapi.h" #include "jsfriendapi.h"
#include "mozilla/dom/OwningNonNull.h" #include "mozilla/dom/OwningNonNull.h"
#include "mozilla/dom/PromiseBinding.h" #include "mozilla/dom/PromiseBinding.h"
#include "mozilla/dom/PromiseResolver.h"
#include "mozilla/Preferences.h" #include "mozilla/Preferences.h"
#include "PromiseCallback.h" #include "PromiseCallback.h"
#include "nsContentUtils.h" #include "nsContentUtils.h"
@ -52,6 +51,58 @@ private:
nsRefPtr<Promise> mPromise; nsRefPtr<Promise> mPromise;
}; };
// This class processes the promise's callbacks with promise's result.
class PromiseResolverTask MOZ_FINAL : public nsRunnable
{
public:
PromiseResolverTask(Promise* aPromise,
JS::Handle<JS::Value> aValue,
Promise::PromiseState aState)
: mPromise(aPromise)
, mValue(aValue)
, mState(aState)
{
MOZ_ASSERT(aPromise);
MOZ_ASSERT(mState != Promise::Pending);
MOZ_COUNT_CTOR(PromiseResolverTask);
JSContext* cx = nsContentUtils::GetSafeJSContext();
/* It's safe to use unsafeGet() here: the unsafeness comes from the
* possibility of updating the value of mJSObject without triggering the
* barriers. However if the value will always be marked, post barriers
* unnecessary. */
JS_AddNamedValueRootRT(JS_GetRuntime(cx), mValue.unsafeGet(),
"PromiseResolverTask.mValue");
}
~PromiseResolverTask()
{
MOZ_COUNT_DTOR(PromiseResolverTask);
JSContext* cx = nsContentUtils::GetSafeJSContext();
/* It's safe to use unsafeGet() here: the unsafeness comes from the
* possibility of updating the value of mJSObject without triggering the
* barriers. However if the value will always be marked, post barriers
* unnecessary. */
JS_RemoveValueRootRT(JS_GetRuntime(cx), mValue.unsafeGet());
}
NS_IMETHOD Run()
{
mPromise->RunResolveTask(
JS::Handle<JS::Value>::fromMarkedLocation(mValue.address()),
mState, Promise::SyncTask);
return NS_OK;
}
private:
nsRefPtr<Promise> mPromise;
JS::Heap<JS::Value> mValue;
Promise::PromiseState mState;
};
// Promise // Promise
NS_IMPL_CYCLE_COLLECTION_CLASS(Promise) NS_IMPL_CYCLE_COLLECTION_CLASS(Promise)
@ -59,7 +110,6 @@ NS_IMPL_CYCLE_COLLECTION_CLASS(Promise)
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise) NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise)
tmp->MaybeReportRejected(); tmp->MaybeReportRejected();
NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow) NS_IMPL_CYCLE_COLLECTION_UNLINK(mWindow)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolver)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks); NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks);
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks); NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks);
tmp->mResult = JS::UndefinedValue(); tmp->mResult = JS::UndefinedValue();
@ -68,7 +118,6 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise) NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Promise)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow) NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mWindow)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolver)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolveCallbacks); NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolveCallbacks);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRejectCallbacks); NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRejectCallbacks);
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
@ -93,12 +142,11 @@ Promise::Promise(nsPIDOMWindow* aWindow)
, mState(Pending) , mState(Pending)
, mTaskPending(false) , mTaskPending(false)
, mHadRejectCallback(false) , mHadRejectCallback(false)
, mResolvePending(false)
{ {
MOZ_COUNT_CTOR(Promise); MOZ_COUNT_CTOR(Promise);
mozilla::HoldJSObjects(this); mozilla::HoldJSObjects(this);
SetIsDOMBinding(); SetIsDOMBinding();
mResolver = new PromiseResolver(this);
} }
Promise::~Promise() Promise::~Promise()
@ -152,6 +200,67 @@ EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
} }
} }
enum {
SLOT_PROMISE = 0,
SLOT_TASK
};
/* static */ bool
Promise::JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp)
{
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
JS::Rooted<JS::Value> v(aCx,
js::GetFunctionNativeReserved(&args.callee(),
SLOT_PROMISE));
MOZ_ASSERT(v.isObject());
Promise* promise;
if (NS_FAILED(UNWRAP_OBJECT(Promise, aCx, &v.toObject(), promise))) {
return Throw(aCx, NS_ERROR_UNEXPECTED);
}
Optional<JS::Handle<JS::Value> > value(aCx);
if (aArgc) {
value.Value() = args[0];
}
v = js::GetFunctionNativeReserved(&args.callee(), SLOT_TASK);
PromiseCallback::Task task = static_cast<PromiseCallback::Task>(v.toInt32());
if (task == PromiseCallback::Resolve) {
promise->MaybeResolve(aCx, value);
} else {
promise->MaybeReject(aCx, value);
}
return true;
}
/* static */ JSObject*
Promise::CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
int32_t aTask)
{
JSFunction* func = js::NewFunctionWithReserved(aCx, JSCallback,
1 /* nargs */, 0 /* flags */,
aParent, nullptr);
if (!func) {
return nullptr;
}
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
JS::Rooted<JS::Value> promiseObj(aCx);
if (!dom::WrapNewBindingObject(aCx, obj, aPromise, &promiseObj)) {
return nullptr;
}
js::SetFunctionNativeReserved(obj, SLOT_PROMISE, promiseObj);
js::SetFunctionNativeReserved(obj, SLOT_TASK, JS::Int32Value(aTask));
return obj;
}
/* static */ already_AddRefed<Promise> /* static */ already_AddRefed<Promise>
Promise::Constructor(const GlobalObject& aGlobal, Promise::Constructor(const GlobalObject& aGlobal,
PromiseInit& aInit, ErrorResult& aRv) PromiseInit& aInit, ErrorResult& aRv)
@ -165,7 +274,23 @@ Promise::Constructor(const GlobalObject& aGlobal,
nsRefPtr<Promise> promise = new Promise(window); nsRefPtr<Promise> promise = new Promise(window);
aInit.Call(promise, *promise->mResolver, aRv, JS::Rooted<JSObject*> resolveFunc(cx,
CreateFunction(cx, aGlobal.Get(), promise,
PromiseCallback::Resolve));
if (!resolveFunc) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
JS::Rooted<JSObject*> rejectFunc(cx,
CreateFunction(cx, aGlobal.Get(), promise,
PromiseCallback::Reject));
if (!rejectFunc) {
aRv.Throw(NS_ERROR_UNEXPECTED);
return nullptr;
}
aInit.Call(promise, resolveFunc, rejectFunc, aRv,
CallbackObject::eRethrowExceptions); CallbackObject::eRethrowExceptions);
aRv.WouldReportJSException(); aRv.WouldReportJSException();
@ -175,7 +300,7 @@ Promise::Constructor(const GlobalObject& aGlobal,
Maybe<JSAutoCompartment> ac; Maybe<JSAutoCompartment> ac;
EnterCompartment(ac, cx, value); EnterCompartment(ac, cx, value);
promise->mResolver->Reject(cx, value); promise->MaybeReject(cx, value);
} }
return promise.forget(); return promise.forget();
@ -194,7 +319,7 @@ Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
nsRefPtr<Promise> promise = new Promise(window); nsRefPtr<Promise> promise = new Promise(window);
Optional<JS::Handle<JS::Value> > value(aCx, aValue); Optional<JS::Handle<JS::Value> > value(aCx, aValue);
promise->mResolver->Resolve(aCx, value); promise->MaybeResolve(aCx, value);
return promise.forget(); return promise.forget();
} }
@ -211,7 +336,7 @@ Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx,
nsRefPtr<Promise> promise = new Promise(window); nsRefPtr<Promise> promise = new Promise(window);
Optional<JS::Handle<JS::Value> > value(aCx, aValue); Optional<JS::Handle<JS::Value> > value(aCx, aValue);
promise->mResolver->Reject(aCx, value); promise->MaybeReject(aCx, value);
return promise.forget(); return promise.forget();
} }
@ -222,14 +347,14 @@ Promise::Then(const Optional<OwningNonNull<AnyCallback> >& aResolveCallback,
nsRefPtr<Promise> promise = new Promise(GetParentObject()); nsRefPtr<Promise> promise = new Promise(GetParentObject());
nsRefPtr<PromiseCallback> resolveCb = nsRefPtr<PromiseCallback> resolveCb =
PromiseCallback::Factory(promise->mResolver, PromiseCallback::Factory(promise,
aResolveCallback.WasPassed() aResolveCallback.WasPassed()
? &aResolveCallback.Value() ? &aResolveCallback.Value()
: nullptr, : nullptr,
PromiseCallback::Resolve); PromiseCallback::Resolve);
nsRefPtr<PromiseCallback> rejectCb = nsRefPtr<PromiseCallback> rejectCb =
PromiseCallback::Factory(promise->mResolver, PromiseCallback::Factory(promise,
aRejectCallback.WasPassed() aRejectCallback.WasPassed()
? &aRejectCallback.Value() ? &aRejectCallback.Value()
: nullptr, : nullptr,
@ -260,9 +385,9 @@ Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
mRejectCallbacks.AppendElement(aRejectCallback); mRejectCallbacks.AppendElement(aRejectCallback);
} }
// If promise's state is resolved, queue a task to process promise's resolve // If promise's state is resolved, queue a task to process our resolve
// callbacks with promise's result. If promise's state is rejected, queue a task // callbacks with promise's result. If promise's state is rejected, queue a
// to process promise's reject callbacks with promise's result. // task to process our reject callbacks with promise's result.
if (mState != Pending && !mTaskPending) { if (mState != Pending && !mTaskPending) {
nsRefPtr<PromiseTask> task = new PromiseTask(this); nsRefPtr<PromiseTask> task = new PromiseTask(this);
NS_DispatchToCurrentThread(task); NS_DispatchToCurrentThread(task);
@ -315,5 +440,91 @@ Promise::MaybeReportRejected()
win)); win));
} }
void
Promise::MaybeResolve(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
if (mResolvePending) {
return;
}
ResolveInternal(aCx, aValue, aAsynchronous);
}
void
Promise::MaybeReject(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
if (mResolvePending) {
return;
}
RejectInternal(aCx, aValue, aAsynchronous);
}
void
Promise::ResolveInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
mResolvePending = true;
// TODO: Bug 879245 - Then-able objects
if (aValue.WasPassed() && aValue.Value().isObject()) {
JS::Rooted<JSObject*> valueObj(aCx, &aValue.Value().toObject());
Promise* nextPromise;
nsresult rv = UNWRAP_OBJECT(Promise, aCx, valueObj, nextPromise);
if (NS_SUCCEEDED(rv)) {
nsRefPtr<PromiseCallback> resolveCb = new ResolvePromiseCallback(this);
nsRefPtr<PromiseCallback> rejectCb = new RejectPromiseCallback(this);
nextPromise->AppendCallbacks(resolveCb, rejectCb);
return;
}
}
// If the synchronous flag is set, process our resolve callbacks with
// value. Otherwise, the synchronous flag is unset, queue a task to process
// own resolve callbacks with value. Otherwise, the synchronous flag is
// unset, queue a task to process our resolve callbacks with value.
RunResolveTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
Resolved, aAsynchronous);
}
void
Promise::RejectInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
mResolvePending = true;
// If the synchronous flag is set, process our reject callbacks with
// value. Otherwise, the synchronous flag is unset, queue a task to process
// promise's reject callbacks with value.
RunResolveTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
Rejected, aAsynchronous);
}
void
Promise::RunResolveTask(JS::Handle<JS::Value> aValue,
PromiseState aState,
PromiseTaskSync aAsynchronous)
{
// If the synchronous flag is unset, queue a task to process our
// accept callbacks with value.
if (aAsynchronous == AsyncTask) {
nsRefPtr<PromiseResolverTask> task =
new PromiseResolverTask(this, aValue, aState);
NS_DispatchToCurrentThread(task);
return;
}
SetResult(aValue);
SetState(aState);
RunTask();
}
} // namespace dom } // namespace dom
} // namespace mozilla } // namespace mozilla

View File

@ -23,14 +23,15 @@ namespace dom {
class PromiseInit; class PromiseInit;
class PromiseCallback; class PromiseCallback;
class AnyCallback; class AnyCallback;
class PromiseResolver;
class Promise MOZ_FINAL : public nsISupports, class Promise MOZ_FINAL : public nsISupports,
public nsWrapperCache public nsWrapperCache
{ {
friend class PromiseTask; friend class PromiseTask;
friend class PromiseResolver;
friend class PromiseResolverTask; friend class PromiseResolverTask;
friend class ResolvePromiseCallback;
friend class RejectPromiseCallback;
friend class WrapperPromiseCallback;
public: public:
NS_DECL_CYCLE_COLLECTING_ISUPPORTS NS_DECL_CYCLE_COLLECTING_ISUPPORTS
@ -79,6 +80,11 @@ private:
Rejected Rejected
}; };
enum PromiseTaskSync {
SyncTask,
AsyncTask
};
void SetState(PromiseState aState) void SetState(PromiseState aState)
{ {
MOZ_ASSERT(mState == Pending); MOZ_ASSERT(mState == Pending);
@ -97,6 +103,10 @@ private:
// appended by then(), catch() or done(). // appended by then(), catch() or done().
void RunTask(); void RunTask();
void RunResolveTask(JS::Handle<JS::Value> aValue,
Promise::PromiseState aState,
PromiseTaskSync aAsynchronous);
void AppendCallbacks(PromiseCallback* aResolveCallback, void AppendCallbacks(PromiseCallback* aResolveCallback,
PromiseCallback* aRejectCallback); PromiseCallback* aRejectCallback);
@ -104,9 +114,29 @@ private:
// report it to the error console. // report it to the error console.
void MaybeReportRejected(); void MaybeReportRejected();
nsRefPtr<nsPIDOMWindow> mWindow; void MaybeResolve(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
void MaybeReject(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
nsRefPtr<PromiseResolver> mResolver; void ResolveInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
void RejectInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
// Static methods for the PromiseInit functions.
static bool
JSCallback(JSContext *aCx, unsigned aArgc, JS::Value *aVp);
static JSObject*
CreateFunction(JSContext* aCx, JSObject* aParent, Promise* aPromise,
int32_t aTask);
nsRefPtr<nsPIDOMWindow> mWindow;
nsTArray<nsRefPtr<PromiseCallback> > mResolveCallbacks; nsTArray<nsRefPtr<PromiseCallback> > mResolveCallbacks;
nsTArray<nsRefPtr<PromiseCallback> > mRejectCallbacks; nsTArray<nsRefPtr<PromiseCallback> > mRejectCallbacks;
@ -115,6 +145,8 @@ private:
PromiseState mState; PromiseState mState;
bool mTaskPending; bool mTaskPending;
bool mHadRejectCallback; bool mHadRejectCallback;
bool mResolvePending;
}; };
} // namespace dom } // namespace dom

View File

@ -6,7 +6,6 @@
#include "PromiseCallback.h" #include "PromiseCallback.h"
#include "mozilla/dom/Promise.h" #include "mozilla/dom/Promise.h"
#include "mozilla/dom/PromiseResolver.h"
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
@ -51,7 +50,7 @@ EnterCompartment(Maybe<JSAutoCompartment>& aAc, JSContext* aCx,
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(ResolvePromiseCallback, NS_IMPL_CYCLE_COLLECTION_INHERITED_1(ResolvePromiseCallback,
PromiseCallback, PromiseCallback,
mResolver) mPromise)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ResolvePromiseCallback) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ResolvePromiseCallback)
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback) NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
@ -59,10 +58,10 @@ NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
NS_IMPL_ADDREF_INHERITED(ResolvePromiseCallback, PromiseCallback) NS_IMPL_ADDREF_INHERITED(ResolvePromiseCallback, PromiseCallback)
NS_IMPL_RELEASE_INHERITED(ResolvePromiseCallback, PromiseCallback) NS_IMPL_RELEASE_INHERITED(ResolvePromiseCallback, PromiseCallback)
ResolvePromiseCallback::ResolvePromiseCallback(PromiseResolver* aResolver) ResolvePromiseCallback::ResolvePromiseCallback(Promise* aPromise)
: mResolver(aResolver) : mPromise(aPromise)
{ {
MOZ_ASSERT(aResolver); MOZ_ASSERT(aPromise);
MOZ_COUNT_CTOR(ResolvePromiseCallback); MOZ_COUNT_CTOR(ResolvePromiseCallback);
} }
@ -79,14 +78,14 @@ ResolvePromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValue)
Maybe<JSAutoCompartment> ac; Maybe<JSAutoCompartment> ac;
EnterCompartment(ac, cx, aValue); EnterCompartment(ac, cx, aValue);
mResolver->ResolveInternal(cx, aValue, PromiseResolver::SyncTask); mPromise->ResolveInternal(cx, aValue, Promise::SyncTask);
} }
// RejectPromiseCallback // RejectPromiseCallback
NS_IMPL_CYCLE_COLLECTION_INHERITED_1(RejectPromiseCallback, NS_IMPL_CYCLE_COLLECTION_INHERITED_1(RejectPromiseCallback,
PromiseCallback, PromiseCallback,
mResolver) mPromise)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(RejectPromiseCallback) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(RejectPromiseCallback)
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback) NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
@ -94,10 +93,10 @@ NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
NS_IMPL_ADDREF_INHERITED(RejectPromiseCallback, PromiseCallback) NS_IMPL_ADDREF_INHERITED(RejectPromiseCallback, PromiseCallback)
NS_IMPL_RELEASE_INHERITED(RejectPromiseCallback, PromiseCallback) NS_IMPL_RELEASE_INHERITED(RejectPromiseCallback, PromiseCallback)
RejectPromiseCallback::RejectPromiseCallback(PromiseResolver* aResolver) RejectPromiseCallback::RejectPromiseCallback(Promise* aPromise)
: mResolver(aResolver) : mPromise(aPromise)
{ {
MOZ_ASSERT(aResolver); MOZ_ASSERT(aPromise);
MOZ_COUNT_CTOR(RejectPromiseCallback); MOZ_COUNT_CTOR(RejectPromiseCallback);
} }
@ -114,14 +113,14 @@ RejectPromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValue)
Maybe<JSAutoCompartment> ac; Maybe<JSAutoCompartment> ac;
EnterCompartment(ac, cx, aValue); EnterCompartment(ac, cx, aValue);
mResolver->RejectInternal(cx, aValue, PromiseResolver::SyncTask); mPromise->RejectInternal(cx, aValue, Promise::SyncTask);
} }
// WrapperPromiseCallback // WrapperPromiseCallback
NS_IMPL_CYCLE_COLLECTION_INHERITED_2(WrapperPromiseCallback, NS_IMPL_CYCLE_COLLECTION_INHERITED_2(WrapperPromiseCallback,
PromiseCallback, PromiseCallback,
mNextResolver, mCallback) mNextPromise, mCallback)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WrapperPromiseCallback) NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WrapperPromiseCallback)
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback) NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
@ -129,12 +128,12 @@ NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
NS_IMPL_ADDREF_INHERITED(WrapperPromiseCallback, PromiseCallback) NS_IMPL_ADDREF_INHERITED(WrapperPromiseCallback, PromiseCallback)
NS_IMPL_RELEASE_INHERITED(WrapperPromiseCallback, PromiseCallback) NS_IMPL_RELEASE_INHERITED(WrapperPromiseCallback, PromiseCallback)
WrapperPromiseCallback::WrapperPromiseCallback(PromiseResolver* aNextResolver, WrapperPromiseCallback::WrapperPromiseCallback(Promise* aNextPromise,
AnyCallback* aCallback) AnyCallback* aCallback)
: mNextResolver(aNextResolver) : mNextPromise(aNextPromise)
, mCallback(aCallback) , mCallback(aCallback)
{ {
MOZ_ASSERT(aNextResolver); MOZ_ASSERT(aNextPromise);
MOZ_COUNT_CTOR(WrapperPromiseCallback); MOZ_COUNT_CTOR(WrapperPromiseCallback);
} }
@ -155,7 +154,7 @@ WrapperPromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValue)
// If invoking callback threw an exception, run resolver's reject with the // If invoking callback threw an exception, run resolver's reject with the
// thrown exception as argument and the synchronous flag set. // thrown exception as argument and the synchronous flag set.
Optional<JS::Handle<JS::Value> > value(cx, Optional<JS::Handle<JS::Value> > value(cx,
mCallback->Call(mNextResolver->GetParentObject(), aValue, rv, mCallback->Call(mNextPromise->GetParentObject(), aValue, rv,
CallbackObject::eRethrowExceptions)); CallbackObject::eRethrowExceptions));
rv.WouldReportJSException(); rv.WouldReportJSException();
@ -166,7 +165,7 @@ WrapperPromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValue)
Maybe<JSAutoCompartment> ac2; Maybe<JSAutoCompartment> ac2;
EnterCompartment(ac2, cx, value); EnterCompartment(ac2, cx, value);
mNextResolver->RejectInternal(cx, value, PromiseResolver::SyncTask); mNextPromise->RejectInternal(cx, value, Promise::SyncTask);
return; return;
} }
@ -174,7 +173,7 @@ WrapperPromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValue)
// set. // set.
Maybe<JSAutoCompartment> ac2; Maybe<JSAutoCompartment> ac2;
EnterCompartment(ac2, cx, value); EnterCompartment(ac2, cx, value);
mNextResolver->ResolveInternal(cx, value, PromiseResolver::SyncTask); mNextPromise->ResolveInternal(cx, value, Promise::SyncTask);
} }
// SimpleWrapperPromiseCallback // SimpleWrapperPromiseCallback
@ -211,23 +210,23 @@ SimpleWrapperPromiseCallback::Call(const Optional<JS::Handle<JS::Value> >& aValu
} }
/* static */ PromiseCallback* /* static */ PromiseCallback*
PromiseCallback::Factory(PromiseResolver* aNextResolver, PromiseCallback::Factory(Promise* aNextPromise, AnyCallback* aCallback,
AnyCallback* aCallback, Task aTask) Task aTask)
{ {
MOZ_ASSERT(aNextResolver); MOZ_ASSERT(aNextPromise);
// If we have a callback and a next resolver, we have to exec the callback and // If we have a callback and a next resolver, we have to exec the callback and
// then propagate the return value to the next resolver->resolve(). // then propagate the return value to the next resolver->resolve().
if (aCallback) { if (aCallback) {
return new WrapperPromiseCallback(aNextResolver, aCallback); return new WrapperPromiseCallback(aNextPromise, aCallback);
} }
if (aTask == Resolve) { if (aTask == Resolve) {
return new ResolvePromiseCallback(aNextResolver); return new ResolvePromiseCallback(aNextPromise);
} }
if (aTask == Reject) { if (aTask == Reject) {
return new RejectPromiseCallback(aNextResolver); return new RejectPromiseCallback(aNextPromise);
} }
MOZ_ASSERT(false, "This should not happen"); MOZ_ASSERT(false, "This should not happen");

View File

@ -13,8 +13,6 @@
namespace mozilla { namespace mozilla {
namespace dom { namespace dom {
class PromiseResolver;
// This is the base class for any PromiseCallback. // This is the base class for any PromiseCallback.
// It's a logical step in the promise chain of callbacks. // It's a logical step in the promise chain of callbacks.
class PromiseCallback : public nsISupports class PromiseCallback : public nsISupports
@ -35,13 +33,12 @@ public:
// This factory returns a PromiseCallback object with refcount of 0. // This factory returns a PromiseCallback object with refcount of 0.
static PromiseCallback* static PromiseCallback*
Factory(PromiseResolver* aNextResolver, AnyCallback* aCallback, Factory(Promise* aNextPromise, AnyCallback* aCallback, Task aTask);
Task aTask);
}; };
// WrapperPromiseCallback execs a JS Callback with a value, and then the return // WrapperPromiseCallback execs a JS Callback with a value, and then the return
// value is sent to the aNextResolver->resolve() or to aNextResolver->Reject() // value is sent to the aNextPromise->resolveFunction() or to
// if the JS Callback throws. // aNextPromise->RejectFunction() if the JS Callback throws.
class WrapperPromiseCallback MOZ_FINAL : public PromiseCallback class WrapperPromiseCallback MOZ_FINAL : public PromiseCallback
{ {
public: public:
@ -51,12 +48,11 @@ public:
void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE; void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE;
WrapperPromiseCallback(PromiseResolver* aNextResolver, WrapperPromiseCallback(Promise* aNextPromise, AnyCallback* aCallback);
AnyCallback* aCallback);
~WrapperPromiseCallback(); ~WrapperPromiseCallback();
private: private:
nsRefPtr<PromiseResolver> mNextResolver; nsRefPtr<Promise> mNextPromise;
nsRefPtr<AnyCallback> mCallback; nsRefPtr<AnyCallback> mCallback;
}; };
@ -79,8 +75,8 @@ private:
nsRefPtr<AnyCallback> mCallback; nsRefPtr<AnyCallback> mCallback;
}; };
// ResolvePromiseCallback calls aResolver->Resolve() with the value received by // ResolvePromiseCallback calls aPromise->ResolveFunction() with the value
// Call(). // received by Call().
class ResolvePromiseCallback MOZ_FINAL : public PromiseCallback class ResolvePromiseCallback MOZ_FINAL : public PromiseCallback
{ {
public: public:
@ -90,15 +86,15 @@ public:
void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE; void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE;
ResolvePromiseCallback(PromiseResolver* aResolver); ResolvePromiseCallback(Promise* aPromise);
~ResolvePromiseCallback(); ~ResolvePromiseCallback();
private: private:
nsRefPtr<PromiseResolver> mResolver; nsRefPtr<Promise> mPromise;
}; };
// RejectPromiseCallback calls aResolver->Reject() with the value received by // RejectPromiseCallback calls aPromise->RejectFunction() with the value
// Call(). // received by Call().
class RejectPromiseCallback MOZ_FINAL : public PromiseCallback class RejectPromiseCallback MOZ_FINAL : public PromiseCallback
{ {
public: public:
@ -108,11 +104,11 @@ public:
void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE; void Call(const Optional<JS::Handle<JS::Value> >& aValue) MOZ_OVERRIDE;
RejectPromiseCallback(PromiseResolver* aResolver); RejectPromiseCallback(Promise* aPromise);
~RejectPromiseCallback(); ~RejectPromiseCallback();
private: private:
nsRefPtr<PromiseResolver> mResolver; nsRefPtr<Promise> mPromise;
}; };
} // namespace dom } // namespace dom

View File

@ -1,173 +0,0 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#include "mozilla/dom/PromiseResolver.h"
#include "mozilla/dom/PromiseBinding.h"
#include "mozilla/dom/Promise.h"
#include "nsThreadUtils.h"
#include "PromiseCallback.h"
namespace mozilla {
namespace dom {
// PromiseResolverTask
// This class processes the promise's callbacks with promise's result.
class PromiseResolverTask MOZ_FINAL : public nsRunnable
{
public:
PromiseResolverTask(PromiseResolver* aResolver,
const JS::Handle<JS::Value> aValue,
Promise::PromiseState aState)
: mResolver(aResolver)
, mValue(aValue)
, mState(aState)
{
MOZ_ASSERT(aResolver);
MOZ_ASSERT(mState != Promise::Pending);
MOZ_COUNT_CTOR(PromiseResolverTask);
JSContext* cx = nsContentUtils::GetSafeJSContext();
JS_AddNamedValueRootRT(JS_GetRuntime(cx), &mValue,
"PromiseResolverTask.mValue");
}
~PromiseResolverTask()
{
MOZ_COUNT_DTOR(PromiseResolverTask);
JSContext* cx = nsContentUtils::GetSafeJSContext();
JS_RemoveValueRootRT(JS_GetRuntime(cx), &mValue);
}
NS_IMETHOD Run()
{
mResolver->RunTask(JS::Handle<JS::Value>::fromMarkedLocation(&mValue),
mState, PromiseResolver::SyncTask);
return NS_OK;
}
private:
nsRefPtr<PromiseResolver> mResolver;
JS::Value mValue;
Promise::PromiseState mState;
};
// PromiseResolver
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_1(PromiseResolver, mPromise)
NS_IMPL_CYCLE_COLLECTION_ROOT_NATIVE(PromiseResolver, AddRef)
NS_IMPL_CYCLE_COLLECTION_UNROOT_NATIVE(PromiseResolver, Release)
PromiseResolver::PromiseResolver(Promise* aPromise)
: mPromise(aPromise)
, mResolvePending(false)
{
MOZ_COUNT_CTOR(PromiseResolver);
SetIsDOMBinding();
}
PromiseResolver::~PromiseResolver()
{
MOZ_COUNT_DTOR(PromiseResolver);
}
JSObject*
PromiseResolver::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
{
return PromiseResolverBinding::Wrap(aCx, aScope, this);
}
void
PromiseResolver::Resolve(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
if (mResolvePending) {
return;
}
ResolveInternal(aCx, aValue, aAsynchronous);
}
void
PromiseResolver::ResolveInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
mResolvePending = true;
// TODO: Bug 879245 - Then-able objects
if (aValue.WasPassed() && aValue.Value().isObject()) {
JS::Rooted<JSObject*> valueObj(aCx, &aValue.Value().toObject());
Promise* nextPromise;
nsresult rv = UNWRAP_OBJECT(Promise, aCx, valueObj, nextPromise);
if (NS_SUCCEEDED(rv)) {
nsRefPtr<PromiseCallback> resolveCb = new ResolvePromiseCallback(this);
nsRefPtr<PromiseCallback> rejectCb = new RejectPromiseCallback(this);
nextPromise->AppendCallbacks(resolveCb, rejectCb);
return;
}
}
// If the synchronous flag is set, process promise's resolve callbacks with
// value. Otherwise, the synchronous flag is unset, queue a task to process
// promise's resolve callbacks with value. Otherwise, the synchronous flag is
// unset, queue a task to process promise's resolve callbacks with value.
RunTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
Promise::Resolved, aAsynchronous);
}
void
PromiseResolver::Reject(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
if (mResolvePending) {
return;
}
RejectInternal(aCx, aValue, aAsynchronous);
}
void
PromiseResolver::RejectInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aAsynchronous)
{
mResolvePending = true;
// If the synchronous flag is set, process promise's reject callbacks with
// value. Otherwise, the synchronous flag is unset, queue a task to process
// promise's reject callbacks with value.
RunTask(aValue.WasPassed() ? aValue.Value() : JS::UndefinedHandleValue,
Promise::Rejected, aAsynchronous);
}
void
PromiseResolver::RunTask(JS::Handle<JS::Value> aValue,
Promise::PromiseState aState,
PromiseTaskSync aAsynchronous)
{
// If the synchronous flag is unset, queue a task to process promise's
// accept callbacks with value.
if (aAsynchronous == AsyncTask) {
nsRefPtr<PromiseResolverTask> task =
new PromiseResolverTask(this, aValue, aState);
NS_DispatchToCurrentThread(task);
return;
}
mPromise->SetResult(aValue);
mPromise->SetState(aState);
mPromise->RunTask();
mPromise = nullptr;
}
} // namespace dom
} // namespace mozilla

View File

@ -1,74 +0,0 @@
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* 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/. */
#ifndef mozilla_dom_PromiseResolver_h
#define mozilla_dom_PromiseResolver_h
#include "mozilla/dom/Promise.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "nsCycleCollectionParticipant.h"
#include "nsWrapperCache.h"
#include "js/TypeDecls.h"
namespace mozilla {
namespace dom {
class PromiseResolver MOZ_FINAL : public nsWrapperCache
{
friend class PromiseResolverTask;
friend class WrapperPromiseCallback;
friend class ResolvePromiseCallback;
friend class RejectPromiseCallback;
private:
enum PromiseTaskSync {
SyncTask,
AsyncTask
};
public:
NS_INLINE_DECL_CYCLE_COLLECTING_NATIVE_REFCOUNTING(PromiseResolver)
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_NATIVE_CLASS(PromiseResolver)
PromiseResolver(Promise* aPromise);
virtual ~PromiseResolver();
Promise* GetParentObject() const
{
return mPromise;
}
virtual JSObject*
WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope) MOZ_OVERRIDE;
void Resolve(JSContext* aCx, const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
void Reject(JSContext* aCx, const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
private:
void ResolveInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
void RejectInternal(JSContext* aCx,
const Optional<JS::Handle<JS::Value> >& aValue,
PromiseTaskSync aSync = AsyncTask);
void RunTask(JS::Handle<JS::Value> aValue,
Promise::PromiseState aState, PromiseTaskSync aSync);
nsRefPtr<Promise> mPromise;
bool mResolvePending;
};
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_PromiseResolver_h

View File

@ -12,12 +12,10 @@ MODULE = 'dom'
EXPORTS.mozilla.dom += [ EXPORTS.mozilla.dom += [
'Promise.h', 'Promise.h',
'PromiseResolver.h',
] ]
CPP_SOURCES += [ CPP_SOURCES += [
'Promise.cpp', 'Promise.cpp',
'PromiseResolver.cpp',
'PromiseCallback.cpp', 'PromiseCallback.cpp',
] ]

View File

@ -23,10 +23,10 @@ function runTest() {
[{}, {}, {}, {}, {}].reduce(Promise.resolve); [{}, {}, {}, {}, {}].reduce(Promise.resolve);
ok(true, "No leaks with resolve?"); ok(true, "No leaks with resolve?");
[{}, {}, {}, {}, {}].reduce(function(a, b, c, d) { return new Promise(function(r) { throw a; }); }); [{}, {}, {}, {}, {}].reduce(function(a, b, c, d) { return new Promise(function(r1, r2) { throw a; }); });
ok(true, "No leaks with exception?"); ok(true, "No leaks with exception?");
[{}, {}, {}, {}, {}].reduce(function(a, b, c, d) { return new Promise(function(r) { }); }); [{}, {}, {}, {}, {}].reduce(function(a, b, c, d) { return new Promise(function(r1, r2) { }); });
ok(true, "No leaks with empty promise?"); ok(true, "No leaks with empty promise?");
SimpleTest.finish(); SimpleTest.finish();

View File

@ -19,12 +19,11 @@
function promiseResolve() { function promiseResolve() {
ok(Promise, "Promise object should exist"); ok(Promise, "Promise object should exist");
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
ok(resolver, "PromiseResolver exists"); ok(resolve, "Promise.resolve exists");
ok("reject" in resolver, "PromiseResolver.reject exists"); ok(reject, "Promise.reject exists");
ok("resolve" in resolver, "PromiseResolver.resolve exists");
resolver.resolve(42); resolve(42);
}).then(function(what) { }).then(function(what) {
ok(true, "Then - resolveCb has been called"); ok(true, "Then - resolveCb has been called");
is(what, 42, "ResolveCb received 42"); is(what, 42, "ResolveCb received 42");
@ -36,8 +35,8 @@ function promiseResolve() {
} }
function promiseReject() { function promiseReject() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.reject(42); reject(42);
}).then(function(what) { }).then(function(what) {
ok(false, "Then - resolveCb has been called"); ok(false, "Then - resolveCb has been called");
runTest(); runTest();
@ -49,7 +48,7 @@ function promiseReject() {
} }
function promiseException() { function promiseException() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
throw 42; throw 42;
}).then(function(what) { }).then(function(what) {
ok(false, "Then - resolveCb has been called"); ok(false, "Then - resolveCb has been called");
@ -62,9 +61,9 @@ function promiseException() {
} }
function promiseGC() { function promiseGC() {
var resolver; var resolve;
var promise = new Promise(function(r) { var promise = new Promise(function(r1, r2) {
resolver = r; resolve = r1;
}).then(function(what) { }).then(function(what) {
ok(true, "Then - promise is still alive"); ok(true, "Then - promise is still alive");
runTest(); runTest();
@ -76,14 +75,14 @@ function promiseGC() {
SpecialPowers.forceGC(); SpecialPowers.forceGC();
SpecialPowers.forceCC(); SpecialPowers.forceCC();
resolver.resolve(42); resolve(42);
} }
function promiseAsync() { function promiseAsync() {
var global = "foo"; var global = "foo";
var f = new Promise(function(r) { var f = new Promise(function(r1, r2) {
is(global, "foo", "Global should be foo"); is(global, "foo", "Global should be foo");
r.resolve(42); r1(42);
is(global, "foo", "Global should still be foo"); is(global, "foo", "Global should still be foo");
setTimeout(function() { setTimeout(function() {
is(global, "bar", "Global should still be bar!"); is(global, "bar", "Global should still be bar!");
@ -97,8 +96,8 @@ function promiseAsync() {
function promiseDoubleThen() { function promiseDoubleThen() {
var steps = 0; var steps = 0;
var promise = new Promise(function(resolver) { var promise = new Promise(function(r1, r2) {
resolver.resolve(42); r1(42);
}); });
promise.then(function(what) { promise.then(function(what) {
@ -120,8 +119,8 @@ function promiseDoubleThen() {
} }
function promiseThenException() { function promiseThenException() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.resolve(42); resolve(42);
}); });
promise.then(function(what) { promise.then(function(what) {
@ -134,8 +133,8 @@ function promiseThenException() {
} }
function promiseThenCatchThen() { function promiseThenCatchThen() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.resolve(42); resolve(42);
}); });
var promise2 = promise.then(function(what) { var promise2 = promise.then(function(what) {
@ -166,8 +165,8 @@ function promiseThenCatchThen() {
} }
function promiseRejectThenCatchThen() { function promiseRejectThenCatchThen() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.reject(42); reject(42);
}); });
var promise2 = promise.then(function(what) { var promise2 = promise.then(function(what) {
@ -194,8 +193,8 @@ function promiseRejectThenCatchThen() {
} }
function promiseRejectThenCatchThen2() { function promiseRejectThenCatchThen2() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.reject(42); reject(42);
}); });
promise.then(function(what) { promise.then(function(what) {
@ -214,8 +213,8 @@ function promiseRejectThenCatchThen2() {
} }
function promiseRejectThenCatchExceptionThen() { function promiseRejectThenCatchExceptionThen() {
var promise = new Promise(function(resolver) { var promise = new Promise(function(resolve, reject) {
resolver.reject(42); reject(42);
}); });
promise.then(function(what) { promise.then(function(what) {
@ -237,8 +236,8 @@ function promiseRejectThenCatchExceptionThen() {
function promiseThenCatchOrderingResolve() { function promiseThenCatchOrderingResolve() {
var global = 0; var global = 0;
var f = new Promise(function(r) { var f = new Promise(function(r1, r2) {
r.resolve(42); r1(42);
}); });
f.then(function() { f.then(function() {
@ -260,8 +259,8 @@ function promiseThenCatchOrderingResolve() {
function promiseThenCatchOrderingReject() { function promiseThenCatchOrderingReject() {
var global = 0; var global = 0;
var f = new Promise(function(r) { var f = new Promise(function(r1, r2) {
r.reject(42); r2(42);
}) })
f.then(function() {}, function() { f.then(function() {}, function() {
@ -282,10 +281,10 @@ function promiseThenCatchOrderingReject() {
} }
function promiseNestedPromise() { function promiseNestedPromise() {
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.resolve(new Promise(function(r) { resolve(new Promise(function(resolve, reject) {
ok(true, "Nested promise is executed"); ok(true, "Nested promise is executed");
r.resolve(42); resolve(42);
})); }));
}).then(function(value) { }).then(function(value) {
is(value, 42, "Nested promise is executed and then == 42"); is(value, 42, "Nested promise is executed and then == 42");
@ -294,10 +293,10 @@ function promiseNestedPromise() {
} }
function promiseNestedNestedPromise() { function promiseNestedNestedPromise() {
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.resolve(new Promise(function(r) { resolve(new Promise(function(resolve, reject) {
ok(true, "Nested promise is executed"); ok(true, "Nested promise is executed");
r.resolve(42); resolve(42);
}).then(function(what) { return what+1; })); }).then(function(what) { return what+1; }));
}).then(function(value) { }).then(function(value) {
is(value, 43, "Nested promise is executed and then == 43"); is(value, 43, "Nested promise is executed and then == 43");
@ -306,12 +305,12 @@ function promiseNestedNestedPromise() {
} }
function promiseWrongNestedPromise() { function promiseWrongNestedPromise() {
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.resolve(new Promise(function(r) { resolve(new Promise(function(r, r2) {
ok(true, "Nested promise is executed"); ok(true, "Nested promise is executed");
r.resolve(42); r(42);
})); }));
resolver.reject(42); reject(42);
}).then(function(value) { }).then(function(value) {
is(value, 42, "Nested promise is executed and then == 42"); is(value, 42, "Nested promise is executed and then == 42");
runTest(); runTest();
@ -321,12 +320,12 @@ function promiseWrongNestedPromise() {
} }
function promiseLoop() { function promiseLoop() {
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.resolve(new Promise(function(r) { resolve(new Promise(function(r1, r2) {
ok(true, "Nested promise is executed"); ok(true, "Nested promise is executed");
r.resolve(new Promise(function(r) { r1(new Promise(function(r1, r2) {
ok(true, "Nested nested promise is executed"); ok(true, "Nested nested promise is executed");
r.resolve(42); r1(42);
})); }));
})); }));
}).then(function(value) { }).then(function(value) {
@ -356,9 +355,9 @@ function promiseResolve() {
} }
function promiseResolveNestedPromise() { function promiseResolveNestedPromise() {
var promise = Promise.resolve(new Promise(function(r) { var promise = Promise.resolve(new Promise(function(r, r2) {
ok(true, "Nested promise is executed"); ok(true, "Nested promise is executed");
r.resolve(42); r(42);
}, function() { }, function() {
ok(false, "This should not be called"); ok(false, "This should not be called");
})).then(function(what) { })).then(function(what) {

View File

@ -44,13 +44,13 @@ function runTest() {
var test = tests.pop(); var test = tests.pop();
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.resolve(test); resolve(test);
}).then(function(what) { }).then(function(what) {
ok(test === what, "What is: " + what); ok(test === what, "What is: " + what);
}, cbError).then(function() { }, cbError).then(function() {
new Promise(function(resolver) { new Promise(function(resolve, reject) {
resolver.reject(test) reject(test)
}).then(cbError, function(what) { }).then(cbError, function(what) {
ok(test === what, "What is: " + what); ok(test === what, "What is: " + what);
}).then(runTest, cbError); }).then(runTest, cbError);

View File

@ -49,7 +49,7 @@
function nativeCallback() { function nativeCallback() {
SpecialPowers.pushPrefEnv({"set": [["dom.promise.enabled", true]]}, function() { SpecialPowers.pushPrefEnv({"set": [["dom.promise.enabled", true]]}, function() {
new Promise(r => r.resolve(42)).then(console.log); new Promise(function(resolve, reject) { resolve(42); }).then(console.log);
}); });
} }
</script> </script>

View File

@ -383,7 +383,6 @@ var interfaceNamesInGlobalScope =
"ProcessingInstruction", "ProcessingInstruction",
"ProgressEvent", "ProgressEvent",
{name: "Promise", b2g: false, release: false}, {name: "Promise", b2g: false, release: false},
{name: "PromiseResolver", b2g: false, release: false},
"PropertyNodeList", "PropertyNodeList",
"Range", "Range",
"RecordErrorEvent", "RecordErrorEvent",

View File

@ -7,14 +7,10 @@
* http://dom.spec.whatwg.org/#promises * http://dom.spec.whatwg.org/#promises
*/ */
[Func="mozilla::dom::Promise::EnabledForScope"] // TODO We use object instead Function. There is an open issue on WebIDL to
interface PromiseResolver { // have different types for "platform-provided function" and "user-provided
// TODO bug 875289 - void fulfill(optional any value); // function"; for now, we just use "object".
void resolve(optional any value); callback PromiseInit = void (object resolve, object reject);
void reject(optional any value);
};
callback PromiseInit = void (PromiseResolver resolver);
callback AnyCallback = any (optional any value); callback AnyCallback = any (optional any value);
[Func="mozilla::dom::Promise::EnabledForScope", Constructor(PromiseInit init)] [Func="mozilla::dom::Promise::EnabledForScope", Constructor(PromiseInit init)]