2013-06-12 05:41:21 +04:00
|
|
|
/* -*- 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/. */
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
|
|
|
|
#include "jsfriendapi.h"
|
2013-09-10 11:03:37 +04:00
|
|
|
#include "mozilla/dom/OwningNonNull.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "mozilla/dom/PromiseBinding.h"
|
2014-03-04 22:05:08 +04:00
|
|
|
#include "mozilla/CycleCollectedJSRuntime.h"
|
2013-06-12 17:44:56 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-07-12 00:40:36 +04:00
|
|
|
#include "PromiseCallback.h"
|
2013-11-19 22:43:51 +04:00
|
|
|
#include "PromiseNativeHandler.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-08-08 01:40:20 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2013-08-08 01:40:20 +04:00
|
|
|
#include "nsJSPrincipals.h"
|
2013-08-29 08:30:06 +04:00
|
|
|
#include "nsJSUtils.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsJSEnvironment.h"
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
using namespace workers;
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
NS_IMPL_ISUPPORTS0(PromiseNativeHandler)
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// PromiseTask
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// This class processes the promise's callbacks with promise's result.
|
|
|
|
class PromiseTask MOZ_FINAL : public nsRunnable
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseTask(Promise* aPromise)
|
|
|
|
: mPromise(aPromise)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(PromiseTask);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
~PromiseTask()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
MOZ_COUNT_DTOR(PromiseTask);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
mPromise->mTaskPending = false;
|
|
|
|
mPromise->RunTask();
|
2013-06-12 05:41:21 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> mPromise;
|
2013-06-12 05:41:21 +04:00
|
|
|
};
|
|
|
|
|
2014-02-17 10:54:36 +04:00
|
|
|
class WorkerPromiseTask MOZ_FINAL : public WorkerSameThreadRunnable
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
public:
|
2013-11-24 23:26:07 +04:00
|
|
|
WorkerPromiseTask(WorkerPrivate* aWorkerPrivate, Promise* aPromise)
|
2014-02-17 10:54:36 +04:00
|
|
|
: WorkerSameThreadRunnable(aWorkerPrivate)
|
2013-11-24 23:26:07 +04:00
|
|
|
, mPromise(aPromise)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_COUNT_CTOR(WorkerPromiseTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
~WorkerPromiseTask()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WorkerPromiseTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mPromise->mTaskPending = false;
|
|
|
|
mPromise->RunTask();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
};
|
|
|
|
|
|
|
|
class PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PromiseResolverMixin(Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
2013-09-11 20:03:04 +04:00
|
|
|
: mPromise(aPromise)
|
2014-04-16 12:47:53 +04:00
|
|
|
, mValue(CycleCollectedJSRuntime::Get()->Runtime(), aValue)
|
2013-09-11 20:03:04 +04:00
|
|
|
, mState(aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_ASSERT(mState != Promise::Pending);
|
2013-11-24 23:26:07 +04:00
|
|
|
MOZ_COUNT_CTOR(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
virtual ~PromiseResolverMixin()
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(PromiseResolverMixin);
|
|
|
|
MOZ_COUNT_DTOR(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
protected:
|
|
|
|
void
|
|
|
|
RunInternal()
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_ASSERT_OWNINGTHREAD(PromiseResolverMixin);
|
2013-09-11 20:03:04 +04:00
|
|
|
mPromise->RunResolveTask(
|
|
|
|
JS::Handle<JS::Value>::fromMarkedLocation(mValue.address()),
|
|
|
|
mState, Promise::SyncTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<Promise> mPromise;
|
2014-04-16 12:47:53 +04:00
|
|
|
JS::PersistentRooted<JS::Value> mValue;
|
2013-09-11 20:03:04 +04:00
|
|
|
Promise::PromiseState mState;
|
2013-11-24 23:26:07 +04:00
|
|
|
NS_DECL_OWNINGTHREAD;
|
|
|
|
};
|
|
|
|
|
|
|
|
// This class processes the promise's callbacks with promise's result.
|
|
|
|
class PromiseResolverTask MOZ_FINAL : public nsRunnable,
|
|
|
|
public PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
PromiseResolverTask(Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
|
|
|
: PromiseResolverMixin(aPromise, aValue, aState)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~PromiseResolverTask()
|
|
|
|
{}
|
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
|
|
|
RunInternal();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-02-17 10:54:36 +04:00
|
|
|
class WorkerPromiseResolverTask MOZ_FINAL : public WorkerSameThreadRunnable,
|
2013-11-24 23:26:07 +04:00
|
|
|
public PromiseResolverMixin
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
WorkerPromiseResolverTask(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aPromise,
|
|
|
|
JS::Handle<JS::Value> aValue,
|
|
|
|
Promise::PromiseState aState)
|
2014-02-17 10:54:36 +04:00
|
|
|
: WorkerSameThreadRunnable(aWorkerPrivate),
|
2013-11-24 23:26:07 +04:00
|
|
|
PromiseResolverMixin(aPromise, aValue, aState)
|
|
|
|
{}
|
|
|
|
|
|
|
|
~WorkerPromiseResolverTask()
|
|
|
|
{}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
RunInternal();
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
};
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
// Promise
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Promise)
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Promise)
|
2014-03-12 18:31:03 +04:00
|
|
|
tmp->MaybeReportRejectedOnce();
|
2014-02-26 01:34:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mGlobal)
|
2014-04-09 12:30:24 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mResolveCallbacks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRejectCallbacks)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
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)
|
2014-04-09 12:30:24 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResolveCallbacks)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRejectCallbacks)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JSVAL_MEMBER_CALLBACK(mResult)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(Promise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Promise)
|
2013-06-12 05:41:21 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
Promise::Promise(nsIGlobalObject* aGlobal)
|
|
|
|
: mGlobal(aGlobal)
|
2013-06-12 05:41:21 +04:00
|
|
|
, mResult(JS::UndefinedValue())
|
|
|
|
, mState(Pending)
|
|
|
|
, mTaskPending(false)
|
2013-08-29 08:30:06 +04:00
|
|
|
, mHadRejectCallback(false)
|
2013-09-11 20:03:04 +04:00
|
|
|
, mResolvePending(false)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
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
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::~Promise()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2014-03-12 18:31:03 +04:00
|
|
|
MaybeReportRejectedOnce();
|
2013-08-17 00:10:17 +04:00
|
|
|
mozilla::DropJSObjects(this);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
Promise::WrapObject(JSContext* aCx)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-09 02:27:17 +04:00
|
|
|
return PromiseBinding::Wrap(aCx, this);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
JSObject*
|
|
|
|
Promise::GetOrCreateWrapper(JSContext* aCx)
|
|
|
|
{
|
|
|
|
if (JSObject* wrapper = GetWrapper()) {
|
|
|
|
return wrapper;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject* global = GetParentObject();
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> scope(aCx, global->GetGlobalJSObject());
|
|
|
|
if (!scope) {
|
|
|
|
JS_ReportError(aCx, "can't get scope");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-04-04 07:32:11 +04:00
|
|
|
JSAutoCompartment ac(aCx, scope);
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
JS::Rooted<JS::Value> val(aCx);
|
2014-04-09 02:27:19 +04:00
|
|
|
if (!WrapNewBindingObject(aCx, this, &val)) {
|
2014-02-26 01:34:55 +04:00
|
|
|
MOZ_ASSERT(JS_IsExceptionPending(aCx));
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetWrapper();
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:16 +04:00
|
|
|
void
|
|
|
|
Promise::MaybeResolve(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-09-26 22:09:16 +04:00
|
|
|
{
|
|
|
|
MaybeResolveInternal(aCx, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::MaybeReject(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-09-26 22:09:16 +04:00
|
|
|
{
|
|
|
|
MaybeRejectInternal(aCx, aValue);
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
enum {
|
|
|
|
SLOT_PROMISE = 0,
|
2014-01-23 22:47:29 +04:00
|
|
|
SLOT_DATA
|
2013-09-11 20:03:04 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/* static */ bool
|
2014-01-23 22:47:29 +04:00
|
|
|
Promise::JSCallback(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> v(aCx,
|
|
|
|
js::GetFunctionNativeReserved(&args.callee(),
|
|
|
|
SLOT_PROMISE));
|
|
|
|
MOZ_ASSERT(v.isObject());
|
|
|
|
|
|
|
|
Promise* promise;
|
2013-11-21 16:51:16 +04:00
|
|
|
if (NS_FAILED(UNWRAP_OBJECT(Promise, &v.toObject(), promise))) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return Throw(aCx, NS_ERROR_UNEXPECTED);
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:47:29 +04:00
|
|
|
v = js::GetFunctionNativeReserved(&args.callee(), SLOT_DATA);
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseCallback::Task task = static_cast<PromiseCallback::Task>(v.toInt32());
|
|
|
|
|
|
|
|
if (task == PromiseCallback::Resolve) {
|
2013-11-19 22:39:51 +04:00
|
|
|
promise->MaybeResolveInternal(aCx, args.get(0));
|
2013-09-11 20:03:04 +04:00
|
|
|
} else {
|
2013-11-19 22:39:51 +04:00
|
|
|
promise->MaybeRejectInternal(aCx, args.get(0));
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:47:29 +04:00
|
|
|
/*
|
|
|
|
* Utilities for thenable callbacks.
|
|
|
|
*
|
|
|
|
* A thenable is a { then: function(resolve, reject) { } }.
|
|
|
|
* `then` is called with a resolve and reject callback pair.
|
|
|
|
* Since only one of these should be called at most once (first call wins), the
|
|
|
|
* two keep a reference to each other in SLOT_DATA. When either of them is
|
|
|
|
* called, the references are cleared. Further calls are ignored.
|
|
|
|
*/
|
|
|
|
namespace {
|
|
|
|
void
|
|
|
|
LinkThenableCallables(JSContext* aCx, JS::Handle<JSObject*> aResolveFunc,
|
|
|
|
JS::Handle<JSObject*> aRejectFunc)
|
|
|
|
{
|
|
|
|
js::SetFunctionNativeReserved(aResolveFunc, SLOT_DATA,
|
|
|
|
JS::ObjectValue(*aRejectFunc));
|
|
|
|
js::SetFunctionNativeReserved(aRejectFunc, SLOT_DATA,
|
|
|
|
JS::ObjectValue(*aResolveFunc));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns false if callback was already called before, otherwise breaks the
|
|
|
|
* links and returns true.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
MarkAsCalledIfNotCalledBefore(JSContext* aCx, JS::Handle<JSObject*> aFunc)
|
|
|
|
{
|
|
|
|
JS::Value otherFuncVal = js::GetFunctionNativeReserved(aFunc, SLOT_DATA);
|
|
|
|
|
|
|
|
if (!otherFuncVal.isObject()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject* otherFuncObj = &otherFuncVal.toObject();
|
|
|
|
MOZ_ASSERT(js::GetFunctionNativeReserved(otherFuncObj, SLOT_DATA).isObject());
|
|
|
|
|
|
|
|
// Break both references.
|
|
|
|
js::SetFunctionNativeReserved(aFunc, SLOT_DATA, JS::UndefinedValue());
|
|
|
|
js::SetFunctionNativeReserved(otherFuncObj, SLOT_DATA, JS::UndefinedValue());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
|
|
|
GetPromise(JSContext* aCx, JS::Handle<JSObject*> aFunc)
|
|
|
|
{
|
|
|
|
JS::Value promiseVal = js::GetFunctionNativeReserved(aFunc, SLOT_PROMISE);
|
|
|
|
|
|
|
|
MOZ_ASSERT(promiseVal.isObject());
|
|
|
|
|
|
|
|
Promise* promise;
|
|
|
|
UNWRAP_OBJECT(Promise, &promiseVal.toObject(), promise);
|
|
|
|
return promise;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Common bits of (JSCallbackThenableResolver/JSCallbackThenableRejecter).
|
|
|
|
* Resolves/rejects the Promise if it is ok to do so, based on whether either of
|
|
|
|
* the callbacks have been called before or not.
|
|
|
|
*/
|
|
|
|
/* static */ bool
|
|
|
|
Promise::ThenableResolverCommon(JSContext* aCx, uint32_t aTask,
|
|
|
|
unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS::CallArgs args = CallArgsFromVp(aArgc, aVp);
|
|
|
|
JS::Rooted<JSObject*> thisFunc(aCx, &args.callee());
|
|
|
|
if (!MarkAsCalledIfNotCalledBefore(aCx, thisFunc)) {
|
|
|
|
// A function from this pair has been called before.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise* promise = GetPromise(aCx, thisFunc);
|
|
|
|
MOZ_ASSERT(promise);
|
|
|
|
|
|
|
|
if (aTask == PromiseCallback::Resolve) {
|
|
|
|
promise->ResolveInternal(aCx, args.get(0), SyncTask);
|
|
|
|
} else {
|
|
|
|
promise->RejectInternal(aCx, args.get(0), SyncTask);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
Promise::JSCallbackThenableResolver(JSContext* aCx,
|
|
|
|
unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
return ThenableResolverCommon(aCx, PromiseCallback::Resolve, aArgc, aVp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool
|
|
|
|
Promise::JSCallbackThenableRejecter(JSContext* aCx,
|
|
|
|
unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
return ThenableResolverCommon(aCx, PromiseCallback::Reject, aArgc, aVp);
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
/* 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);
|
2014-04-09 02:27:19 +04:00
|
|
|
if (!dom::WrapNewBindingObject(aCx, aPromise, &promiseObj)) {
|
2013-09-11 20:03:04 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_PROMISE, promiseObj);
|
2014-01-23 22:47:29 +04:00
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_DATA, JS::Int32Value(aTask));
|
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ JSObject*
|
|
|
|
Promise::CreateThenableFunction(JSContext* aCx, Promise* aPromise, uint32_t aTask)
|
|
|
|
{
|
|
|
|
JSNative whichFunc =
|
|
|
|
aTask == PromiseCallback::Resolve ? JSCallbackThenableResolver :
|
|
|
|
JSCallbackThenableRejecter ;
|
|
|
|
|
|
|
|
JSFunction* func = js::NewFunctionWithReserved(aCx, whichFunc,
|
|
|
|
1 /* nargs */, 0 /* flags */,
|
|
|
|
nullptr, nullptr);
|
|
|
|
if (!func) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS_GetFunctionObject(func));
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> promiseObj(aCx);
|
2014-04-09 02:27:19 +04:00
|
|
|
if (!dom::WrapNewBindingObject(aCx, aPromise, &promiseObj)) {
|
2014-01-23 22:47:29 +04:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
js::SetFunctionNativeReserved(obj, SLOT_PROMISE, promiseObj);
|
2013-09-11 20:03:04 +04:00
|
|
|
|
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
2013-08-23 09:17:08 +04:00
|
|
|
Promise::Constructor(const GlobalObject& aGlobal,
|
2013-07-12 00:40:36 +04:00
|
|
|
PromiseInit& aInit, ErrorResult& aRv)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-08-23 09:17:08 +04:00
|
|
|
JSContext* cx = aGlobal.GetContext();
|
2013-11-24 23:26:07 +04:00
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global;
|
|
|
|
global = do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(global);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-02-19 22:59:23 +04:00
|
|
|
aInit.Call(resolveFunc, rejectFunc, aRv, CallbackObject::eRethrowExceptions);
|
2013-06-12 05:41:21 +04:00
|
|
|
aRv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (aRv.IsJSException()) {
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx);
|
|
|
|
aRv.StealJSException(cx, &value);
|
2013-06-26 17:44:21 +04:00
|
|
|
|
2014-04-09 12:30:24 +04:00
|
|
|
// we want the same behavior as this JS implementation:
|
|
|
|
// function Promise(arg) { try { arg(a, b); } catch (e) { this.reject(e); }}
|
|
|
|
if (!JS_WrapValue(cx, &value)) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-09-26 22:09:16 +04:00
|
|
|
promise->MaybeRejectInternal(cx, value);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Resolve(const GlobalObject& aGlobal, JSContext* aCx,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2014-02-10 21:27:02 +04:00
|
|
|
// If a Promise was passed, just return it.
|
2014-02-19 19:13:38 +04:00
|
|
|
if (aValue.isObject()) {
|
|
|
|
JS::Rooted<JSObject*> valueObj(aCx, &aValue.toObject());
|
2014-02-10 21:27:02 +04:00
|
|
|
Promise* nextPromise;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(Promise, valueObj, nextPromise);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsRefPtr<Promise> addRefed = nextPromise;
|
|
|
|
return addRefed.forget();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
return Resolve(global, aCx, aValue, aRv);
|
2014-01-14 02:36:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Promise>
|
2014-02-26 01:34:55 +04:00
|
|
|
Promise::Resolve(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-02-10 21:27:02 +04:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2014-01-14 02:36:03 +04:00
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(aGlobal);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2014-01-14 02:36:03 +04:00
|
|
|
promise->MaybeResolveInternal(aCx, aValue);
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Reject(const GlobalObject& aGlobal, JSContext* aCx,
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
return Reject(global, aCx, aValue, aRv);
|
2014-01-14 02:36:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Promise>
|
2014-02-26 01:34:55 +04:00
|
|
|
Promise::Reject(nsIGlobalObject* aGlobal, JSContext* aCx,
|
2014-01-14 02:36:03 +04:00
|
|
|
JS::Handle<JS::Value> aValue, ErrorResult& aRv)
|
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(aGlobal);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2014-01-14 02:36:03 +04:00
|
|
|
promise->MaybeRejectInternal(aCx, aValue);
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-04-09 12:30:24 +04:00
|
|
|
Promise::Then(JSContext* aCx, AnyCallback* aResolveCallback,
|
|
|
|
AnyCallback* aRejectCallback)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(GetParentObject());
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2014-04-09 12:30:24 +04:00
|
|
|
JS::Rooted<JSObject*> global(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
2014-04-09 12:30:24 +04:00
|
|
|
PromiseCallback::Factory(promise, global, aResolveCallback,
|
|
|
|
PromiseCallback::Resolve);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
nsRefPtr<PromiseCallback> rejectCb =
|
2014-04-09 12:30:24 +04:00
|
|
|
PromiseCallback::Factory(promise, global, aRejectCallback,
|
|
|
|
PromiseCallback::Reject);
|
2013-06-12 05:41:22 +04:00
|
|
|
|
|
|
|
AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
return promise.forget();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 00:40:36 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-04-09 12:30:24 +04:00
|
|
|
Promise::Catch(JSContext* aCx, AnyCallback* aRejectCallback)
|
2013-06-12 05:41:22 +04:00
|
|
|
{
|
2014-01-28 22:14:57 +04:00
|
|
|
nsRefPtr<AnyCallback> resolveCb;
|
2014-04-09 12:30:24 +04:00
|
|
|
return Then(aCx, resolveCb, aRejectCallback);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
|
2013-11-20 01:53:00 +04:00
|
|
|
/**
|
|
|
|
* The CountdownHolder class encapsulates Promise.all countdown functions and
|
|
|
|
* the countdown holder parts of the Promises spec. It maintains the result
|
|
|
|
* array and AllResolveHandlers use SetValue() to set the array indices.
|
|
|
|
*/
|
|
|
|
class CountdownHolder MOZ_FINAL : public nsISupports
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(CountdownHolder)
|
|
|
|
|
2014-04-09 12:30:24 +04:00
|
|
|
CountdownHolder(const GlobalObject& aGlobal, Promise* aPromise,
|
|
|
|
uint32_t aCountdown)
|
2013-11-20 01:53:00 +04:00
|
|
|
: mPromise(aPromise), mCountdown(aCountdown)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aCountdown != 0);
|
|
|
|
JSContext* cx = aGlobal.GetContext();
|
2014-04-09 12:30:24 +04:00
|
|
|
|
|
|
|
// The only time aGlobal.GetContext() and aGlobal.Get() are not
|
|
|
|
// same-compartment is when we're called via Xrays, and in that situation we
|
|
|
|
// in fact want to create the array in the callee compartment
|
|
|
|
|
2013-11-20 01:53:00 +04:00
|
|
|
JSAutoCompartment ac(cx, aGlobal.Get());
|
2014-02-12 14:50:46 +04:00
|
|
|
mValues = JS_NewArrayObject(cx, aCountdown);
|
2013-11-20 01:53:00 +04:00
|
|
|
mozilla::HoldJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
~CountdownHolder()
|
|
|
|
{
|
|
|
|
mozilla::DropJSObjects(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetValue(uint32_t index, const JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCountdown > 0);
|
|
|
|
|
2014-03-04 22:05:08 +04:00
|
|
|
ThreadsafeAutoSafeJSContext cx;
|
2013-11-20 01:53:00 +04:00
|
|
|
JSAutoCompartment ac(cx, mValues);
|
2014-04-09 12:08:39 +04:00
|
|
|
{
|
2014-04-09 12:30:24 +04:00
|
|
|
|
2013-11-20 01:53:00 +04:00
|
|
|
AutoDontReportUncaught silenceReporting(cx);
|
2014-04-09 12:30:24 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx, aValue);
|
|
|
|
if (!JS_WrapValue(cx, &value) ||
|
|
|
|
!JS_DefineElement(cx, mValues, index, value, nullptr, nullptr,
|
|
|
|
JSPROP_ENUMERATE)) {
|
2013-11-20 01:53:00 +04:00
|
|
|
MOZ_ASSERT(JS_IsExceptionPending(cx));
|
|
|
|
JS::Rooted<JS::Value> exn(cx);
|
|
|
|
JS_GetPendingException(cx, &exn);
|
|
|
|
|
|
|
|
mPromise->MaybeReject(cx, exn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
--mCountdown;
|
|
|
|
if (mCountdown == 0) {
|
|
|
|
JS::Rooted<JS::Value> result(cx, JS::ObjectValue(*mValues));
|
|
|
|
mPromise->MaybeResolve(cx, result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
uint32_t mCountdown;
|
|
|
|
JS::Heap<JSObject*> mValues;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(CountdownHolder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(CountdownHolder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(CountdownHolder)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(CountdownHolder)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(CountdownHolder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mValues)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(CountdownHolder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(CountdownHolder)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->mValues = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An AllResolveHandler is the per-promise part of the Promise.all() algorithm.
|
|
|
|
* Every Promise in the handler is handed an instance of this as a resolution
|
|
|
|
* handler and it sets the relevant index in the CountdownHolder.
|
|
|
|
*/
|
|
|
|
class AllResolveHandler MOZ_FINAL : public PromiseNativeHandler
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS(AllResolveHandler)
|
|
|
|
|
|
|
|
AllResolveHandler(CountdownHolder* aHolder, uint32_t aIndex)
|
|
|
|
: mCountdownHolder(aHolder), mIndex(aIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aHolder);
|
|
|
|
}
|
|
|
|
|
|
|
|
~AllResolveHandler()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ResolvedCallback(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
mCountdownHolder->SetValue(mIndex, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RejectedCallback(JS::Handle<JS::Value> aValue)
|
|
|
|
{
|
|
|
|
// Should never be attached to Promise as a reject handler.
|
|
|
|
MOZ_ASSERT(false, "AllResolveHandler should never be attached to a Promise's reject handler!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<CountdownHolder> mCountdownHolder;
|
|
|
|
uint32_t mIndex;
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AllResolveHandler)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AllResolveHandler)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AllResolveHandler)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseNativeHandler)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_1(AllResolveHandler, mCountdownHolder)
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::All(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv)
|
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2013-11-20 01:53:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aIterable.Length() == 0) {
|
2014-02-12 14:50:46 +04:00
|
|
|
JS::Rooted<JSObject*> empty(aCx, JS_NewArrayObject(aCx, 0));
|
2013-11-20 01:53:00 +04:00
|
|
|
if (!empty) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::ObjectValue(*empty));
|
|
|
|
return Promise::Resolve(aGlobal, aCx, value, aRv);
|
2013-11-20 01:53:00 +04:00
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(global);
|
2013-11-20 01:53:00 +04:00
|
|
|
nsRefPtr<CountdownHolder> holder =
|
|
|
|
new CountdownHolder(aGlobal, promise, aIterable.Length());
|
|
|
|
|
2014-04-09 12:30:24 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!obj) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> rejectCb = new RejectPromiseCallback(promise, obj);
|
2013-11-20 01:53:00 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aIterable.Length(); ++i) {
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx, aIterable.ElementAt(i));
|
|
|
|
nsRefPtr<Promise> nextPromise = Promise::Resolve(aGlobal, aCx, value, aRv);
|
2013-11-20 01:53:00 +04:00
|
|
|
|
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
|
|
|
|
|
|
|
nsRefPtr<PromiseNativeHandler> resolveHandler =
|
|
|
|
new AllResolveHandler(holder, i);
|
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
|
|
|
new NativePromiseCallback(resolveHandler, Resolved);
|
|
|
|
// Every promise gets its own resolve callback, which will set the right
|
|
|
|
// index in the array to the resolution value.
|
|
|
|
nextPromise->AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ already_AddRefed<Promise>
|
|
|
|
Promise::Race(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
const Sequence<JS::Value>& aIterable, ErrorResult& aRv)
|
|
|
|
{
|
2014-02-26 01:34:55 +04:00
|
|
|
nsCOMPtr<nsIGlobalObject> global =
|
|
|
|
do_QueryInterface(aGlobal.GetAsSupports());
|
|
|
|
if (!global) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
2013-11-20 01:53:00 +04:00
|
|
|
}
|
|
|
|
|
2014-04-09 12:30:24 +04:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, JS::CurrentGlobalOrNull(aCx));
|
|
|
|
if (!obj) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-26 01:34:55 +04:00
|
|
|
nsRefPtr<Promise> promise = new Promise(global);
|
2014-04-09 12:30:24 +04:00
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
|
|
|
new ResolvePromiseCallback(promise, obj);
|
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> rejectCb = new RejectPromiseCallback(promise, obj);
|
2013-11-20 01:53:00 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < aIterable.Length(); ++i) {
|
2014-02-19 19:13:38 +04:00
|
|
|
JS::Rooted<JS::Value> value(aCx, aIterable.ElementAt(i));
|
|
|
|
nsRefPtr<Promise> nextPromise = Promise::Resolve(aGlobal, aCx, value, aRv);
|
2014-02-10 21:27:02 +04:00
|
|
|
// According to spec, Resolve can throw, but our implementation never does.
|
|
|
|
// Well it does when window isn't passed on the main thread, but that is an
|
|
|
|
// implementation detail which should never be reached since we are checking
|
|
|
|
// for window above. Remove this when subclassing is supported.
|
2013-11-20 01:53:00 +04:00
|
|
|
MOZ_ASSERT(!aRv.Failed());
|
|
|
|
nextPromise->AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2013-11-19 22:43:51 +04:00
|
|
|
void
|
|
|
|
Promise::AppendNativeHandler(PromiseNativeHandler* aRunnable)
|
|
|
|
{
|
|
|
|
nsRefPtr<PromiseCallback> resolveCb =
|
2013-11-20 01:53:00 +04:00
|
|
|
new NativePromiseCallback(aRunnable, Resolved);
|
2013-11-19 22:43:51 +04:00
|
|
|
|
|
|
|
nsRefPtr<PromiseCallback> rejectCb =
|
2013-11-20 01:53:00 +04:00
|
|
|
new NativePromiseCallback(aRunnable, Rejected);
|
2013-11-19 22:43:51 +04:00
|
|
|
|
|
|
|
AppendCallbacks(resolveCb, rejectCb);
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
void
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::AppendCallbacks(PromiseCallback* aResolveCallback,
|
|
|
|
PromiseCallback* aRejectCallback)
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
2013-06-12 05:41:22 +04:00
|
|
|
if (aResolveCallback) {
|
|
|
|
mResolveCallbacks.AppendElement(aResolveCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aRejectCallback) {
|
2013-08-29 08:30:06 +04:00
|
|
|
mHadRejectCallback = true;
|
2013-06-12 05:41:22 +04:00
|
|
|
mRejectCallbacks.AppendElement(aRejectCallback);
|
2014-03-12 18:31:03 +04:00
|
|
|
|
|
|
|
// Now that there is a callback, we don't need to report anymore.
|
|
|
|
RemoveFeature();
|
2013-06-12 05:41:22 +04:00
|
|
|
}
|
2013-06-12 05:41:21 +04:00
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
// 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 to process our reject callbacks with promise's result.
|
2013-06-12 05:41:21 +04:00
|
|
|
if (mState != Pending && !mTaskPending) {
|
2013-11-24 23:26:07 +04:00
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
nsRefPtr<PromiseTask> task = new PromiseTask(this);
|
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
nsRefPtr<WorkerPromiseTask> task = new WorkerPromiseTask(worker, this);
|
2014-02-17 10:54:36 +04:00
|
|
|
task->Dispatch(worker->GetJSContext());
|
2013-11-24 23:26:07 +04:00
|
|
|
}
|
2013-06-12 05:41:21 +04:00
|
|
|
mTaskPending = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-07-12 00:40:36 +04:00
|
|
|
Promise::RunTask()
|
2013-06-12 05:41:21 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mState != Pending);
|
|
|
|
|
2013-11-20 01:53:00 +04:00
|
|
|
nsTArray<nsRefPtr<PromiseCallback>> callbacks;
|
2013-06-12 05:41:21 +04:00
|
|
|
callbacks.SwapElements(mState == Resolved ? mResolveCallbacks
|
|
|
|
: mRejectCallbacks);
|
|
|
|
mResolveCallbacks.Clear();
|
|
|
|
mRejectCallbacks.Clear();
|
|
|
|
|
2014-03-04 22:05:08 +04:00
|
|
|
ThreadsafeAutoJSContext cx; // Just for rooting.
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JS::Value> value(cx, mResult);
|
2013-06-12 05:41:21 +04:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < callbacks.Length(); ++i) {
|
2013-06-12 05:41:22 +04:00
|
|
|
callbacks[i]->Call(value);
|
2013-06-12 05:41:21 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-29 08:30:06 +04:00
|
|
|
void
|
|
|
|
Promise::MaybeReportRejected()
|
|
|
|
{
|
|
|
|
if (mState != Rejected || mHadRejectCallback || mResult.isUndefined()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-30 21:30:29 +04:00
|
|
|
if (!mResult.isObject()) {
|
|
|
|
return;
|
|
|
|
}
|
2014-03-04 22:05:08 +04:00
|
|
|
ThreadsafeAutoJSContext cx;
|
2014-01-30 21:30:29 +04:00
|
|
|
JS::Rooted<JSObject*> obj(cx, &mResult.toObject());
|
|
|
|
JSAutoCompartment ac(cx, obj);
|
|
|
|
JSErrorReport* report = JS_ErrorFromException(cx, obj);
|
2013-08-29 08:30:06 +04:00
|
|
|
if (!report) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-24 23:26:07 +04:00
|
|
|
// Remains null in case of worker.
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win;
|
|
|
|
bool isChromeError = false;
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
win =
|
2014-01-30 21:30:29 +04:00
|
|
|
do_QueryInterface(nsJSUtils::GetStaticScriptGlobal(obj));
|
|
|
|
nsIPrincipal* principal = nsContentUtils::GetObjectPrincipal(obj);
|
2013-11-24 23:26:07 +04:00
|
|
|
isChromeError = nsContentUtils::IsSystemPrincipal(principal);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
isChromeError = worker->IsChromeWorker();
|
|
|
|
}
|
2013-08-29 08:30:06 +04:00
|
|
|
|
|
|
|
// Now post an event to do the real reporting async
|
2014-01-11 02:07:46 +04:00
|
|
|
// Since Promises preserve their wrapper, it is essential to nsRefPtr<> the
|
|
|
|
// AsyncErrorReporter, otherwise if the call to DispatchToMainThread fails, it
|
|
|
|
// will leak. See Bug 958684.
|
|
|
|
nsRefPtr<AsyncErrorReporter> r =
|
2014-01-30 21:30:29 +04:00
|
|
|
new AsyncErrorReporter(JS_GetObjectRuntime(obj),
|
2013-08-29 08:30:06 +04:00
|
|
|
report,
|
|
|
|
nullptr,
|
2013-11-24 23:26:07 +04:00
|
|
|
isChromeError,
|
2014-01-11 02:07:46 +04:00
|
|
|
win);
|
|
|
|
NS_DispatchToMainThread(r);
|
2013-08-29 08:30:06 +04:00
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
void
|
2013-09-26 22:09:16 +04:00
|
|
|
Promise::MaybeResolveInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 22:09:16 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
if (mResolvePending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ResolveInternal(aCx, aValue, aAsynchronous);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-26 22:09:16 +04:00
|
|
|
Promise::MaybeRejectInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-26 22:09:16 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
2013-09-11 20:03:04 +04:00
|
|
|
{
|
|
|
|
if (mResolvePending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RejectInternal(aCx, aValue, aAsynchronous);
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:47:29 +04:00
|
|
|
void
|
|
|
|
Promise::HandleException(JSContext* aCx)
|
|
|
|
{
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
if (JS_GetPendingException(aCx, &exn)) {
|
|
|
|
JS_ClearPendingException(aCx);
|
|
|
|
RejectInternal(aCx, exn, SyncTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
void
|
|
|
|
Promise::ResolveInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 20:03:04 +04:00
|
|
|
PromiseTaskSync aAsynchronous)
|
|
|
|
{
|
|
|
|
mResolvePending = true;
|
|
|
|
|
2013-11-19 22:39:51 +04:00
|
|
|
if (aValue.isObject()) {
|
2013-11-20 01:53:00 +04:00
|
|
|
AutoDontReportUncaught silenceReporting(aCx);
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Rooted<JSObject*> valueObj(aCx, &aValue.toObject());
|
2013-09-11 20:03:04 +04:00
|
|
|
|
2014-01-23 22:47:29 +04:00
|
|
|
// Thenables.
|
|
|
|
JS::Rooted<JS::Value> then(aCx);
|
|
|
|
if (!JS_GetProperty(aCx, valueObj, "then", &then)) {
|
|
|
|
HandleException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (then.isObject() && JS_ObjectIsCallable(aCx, &then.toObject())) {
|
|
|
|
JS::Rooted<JSObject*> resolveFunc(aCx,
|
|
|
|
CreateThenableFunction(aCx, this, PromiseCallback::Resolve));
|
|
|
|
|
|
|
|
if (!resolveFunc) {
|
|
|
|
HandleException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> rejectFunc(aCx,
|
|
|
|
CreateThenableFunction(aCx, this, PromiseCallback::Reject));
|
|
|
|
if (!rejectFunc) {
|
|
|
|
HandleException(aCx);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LinkThenableCallables(aCx, resolveFunc, rejectFunc);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> thenObj(aCx, &then.toObject());
|
|
|
|
nsRefPtr<PromiseInit> thenCallback =
|
|
|
|
new PromiseInit(thenObj, mozilla::dom::GetIncumbentGlobal());
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
thenCallback->Call(valueObj, resolveFunc, rejectFunc,
|
|
|
|
rv, CallbackObject::eRethrowExceptions);
|
|
|
|
rv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (rv.IsJSException()) {
|
|
|
|
JS::Rooted<JS::Value> exn(aCx);
|
|
|
|
rv.StealJSException(aCx, &exn);
|
|
|
|
|
|
|
|
bool couldMarkAsCalled = MarkAsCalledIfNotCalledBefore(aCx, resolveFunc);
|
|
|
|
|
|
|
|
// If we could mark as called, neither of the callbacks had been called
|
|
|
|
// when the exception was thrown. So we can reject the Promise.
|
|
|
|
if (couldMarkAsCalled) {
|
2014-04-09 12:30:24 +04:00
|
|
|
bool ok = JS_WrapValue(aCx, &exn);
|
|
|
|
MOZ_ASSERT(ok);
|
|
|
|
if (!ok) {
|
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
}
|
|
|
|
|
2014-01-23 22:47:29 +04:00
|
|
|
RejectInternal(aCx, exn, Promise::SyncTask);
|
|
|
|
}
|
|
|
|
// At least one of resolveFunc or rejectFunc have been called, so ignore
|
|
|
|
// the exception. FIXME(nsm): This should be reported to the error
|
|
|
|
// console though, for debugging.
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
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.
|
2013-11-19 22:39:51 +04:00
|
|
|
RunResolveTask(aValue, Resolved, aAsynchronous);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Promise::RejectInternal(JSContext* aCx,
|
2013-11-19 22:39:51 +04:00
|
|
|
JS::Handle<JS::Value> aValue,
|
2013-09-11 20:03:04 +04:00
|
|
|
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.
|
2013-11-19 22:39:51 +04:00
|
|
|
RunResolveTask(aValue, Rejected, aAsynchronous);
|
2013-09-11 20:03:04 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2013-11-24 23:26:07 +04:00
|
|
|
if (MOZ_LIKELY(NS_IsMainThread())) {
|
|
|
|
nsRefPtr<PromiseResolverTask> task =
|
|
|
|
new PromiseResolverTask(this, aValue, aState);
|
|
|
|
NS_DispatchToCurrentThread(task);
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
nsRefPtr<WorkerPromiseResolverTask> task =
|
|
|
|
new WorkerPromiseResolverTask(worker, this, aValue, aState);
|
2014-02-17 10:54:36 +04:00
|
|
|
task->Dispatch(worker->GetJSContext());
|
2013-11-24 23:26:07 +04:00
|
|
|
}
|
2013-09-11 20:03:04 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-20 01:53:00 +04:00
|
|
|
// Promise.all() or Promise.race() implementations will repeatedly call
|
|
|
|
// Resolve/RejectInternal rather than using the Maybe... forms. Stop SetState
|
|
|
|
// from asserting.
|
|
|
|
if (mState != Pending) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
SetResult(aValue);
|
|
|
|
SetState(aState);
|
2014-03-12 18:31:03 +04:00
|
|
|
|
|
|
|
// If the Promise was rejected, and there is no reject handler already setup,
|
|
|
|
// watch for thread shutdown.
|
|
|
|
if (aState == PromiseState::Rejected &&
|
|
|
|
!mHadRejectCallback &&
|
|
|
|
!NS_IsMainThread()) {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
mFeature = new PromiseReportRejectFeature(this);
|
|
|
|
if (NS_WARN_IF(!worker->AddFeature(worker->GetJSContext(), mFeature))) {
|
|
|
|
// Worker is shutting down, report rejection immediately since it is
|
|
|
|
// unlikely that reject callbacks will be added after this point.
|
|
|
|
MaybeReportRejected();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-11 20:03:04 +04:00
|
|
|
RunTask();
|
|
|
|
}
|
|
|
|
|
2014-03-12 18:31:03 +04:00
|
|
|
void
|
|
|
|
Promise::RemoveFeature()
|
|
|
|
{
|
|
|
|
if (mFeature) {
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->RemoveFeature(worker->GetJSContext(), mFeature);
|
|
|
|
mFeature = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
PromiseReportRejectFeature::Notify(JSContext* aCx, workers::Status aStatus)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
|
|
|
mPromise->MaybeReportRejectedOnce();
|
|
|
|
// After this point, `this` has been deleted by RemoveFeature!
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-06-12 05:41:21 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|