2013-07-11 13:40:36 -07: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/. */
|
|
|
|
|
|
|
|
#include "PromiseCallback.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2013-11-19 10:43:51 -08:00
|
|
|
#include "mozilla/dom/PromiseNativeHandler.h"
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-01-28 10:14:57 -08:00
|
|
|
#include "js/OldDebugAPI.h"
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseCallback)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2013-08-01 18:29:05 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(PromiseCallback)
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
PromiseCallback::PromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
PromiseCallback::~PromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PromiseCallback);
|
|
|
|
}
|
|
|
|
|
2014-04-09 01:08:39 -07:00
|
|
|
// ResolvePromiseCallback
|
2014-04-09 00:32:46 -07:00
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(ResolvePromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(ResolvePromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->mGlobal = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(ResolvePromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ResolvePromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mGlobal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ResolvePromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ResolvePromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ResolvePromiseCallback, PromiseCallback)
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
ResolvePromiseCallback::ResolvePromiseCallback(Promise* aPromise,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
2013-09-11 09:03:04 -07:00
|
|
|
: mPromise(aPromise)
|
2014-04-09 01:30:24 -07:00
|
|
|
, mGlobal(aGlobal)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
2013-09-11 09:03:04 -07:00
|
|
|
MOZ_ASSERT(aPromise);
|
2014-04-09 01:30:24 -07:00
|
|
|
MOZ_ASSERT(aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_COUNT_CTOR(ResolvePromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
HoldJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
ResolvePromiseCallback::~ResolvePromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(ResolvePromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
DropJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-29 14:12:14 -07:00
|
|
|
ResolvePromiseCallback::Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
|
|
|
// Run resolver's algorithm with value and the synchronous flag set.
|
2013-11-24 11:26:07 -08:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
JSAutoCompartment ac(aCx, mGlobal);
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
|
|
|
if (!JS_WrapValue(aCx, &value)) {
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
return;
|
|
|
|
}
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
mPromise->ResolveInternal(aCx, value, Promise::SyncTask);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// RejectPromiseCallback
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(RejectPromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(RejectPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPromise)
|
|
|
|
tmp->mGlobal = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(RejectPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(RejectPromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(RejectPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mGlobal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(RejectPromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(RejectPromiseCallback, PromiseCallback)
|
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
RejectPromiseCallback::RejectPromiseCallback(Promise* aPromise,
|
|
|
|
JS::Handle<JSObject*> aGlobal)
|
2013-09-11 09:03:04 -07:00
|
|
|
: mPromise(aPromise)
|
2014-04-09 01:30:24 -07:00
|
|
|
, mGlobal(aGlobal)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
2013-09-11 09:03:04 -07:00
|
|
|
MOZ_ASSERT(aPromise);
|
2014-04-09 01:30:24 -07:00
|
|
|
MOZ_ASSERT(mGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_COUNT_CTOR(RejectPromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
HoldJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
RejectPromiseCallback::~RejectPromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(RejectPromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
DropJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-29 14:12:14 -07:00
|
|
|
RejectPromiseCallback::Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
|
|
|
// Run resolver's algorithm with value and the synchronous flag set.
|
2013-11-24 11:26:07 -08:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
JSAutoCompartment ac(aCx, mGlobal);
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
|
|
|
if (!JS_WrapValue(aCx, &value)) {
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
return;
|
|
|
|
}
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
mPromise->RejectInternal(aCx, value, Promise::SyncTask);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// WrapperPromiseCallback
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WrapperPromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WrapperPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNextPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCallback)
|
|
|
|
tmp->mGlobal = nullptr;
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WrapperPromiseCallback,
|
|
|
|
PromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNextPromise)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(WrapperPromiseCallback)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mGlobal)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(WrapperPromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(WrapperPromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WrapperPromiseCallback, PromiseCallback)
|
|
|
|
|
2013-09-11 09:03:04 -07:00
|
|
|
WrapperPromiseCallback::WrapperPromiseCallback(Promise* aNextPromise,
|
2014-04-09 01:30:24 -07:00
|
|
|
JS::Handle<JSObject*> aGlobal,
|
2013-07-11 13:40:36 -07:00
|
|
|
AnyCallback* aCallback)
|
2013-09-11 09:03:04 -07:00
|
|
|
: mNextPromise(aNextPromise)
|
2014-04-09 01:30:24 -07:00
|
|
|
, mGlobal(aGlobal)
|
2013-07-11 13:40:36 -07:00
|
|
|
, mCallback(aCallback)
|
|
|
|
{
|
2013-09-11 09:03:04 -07:00
|
|
|
MOZ_ASSERT(aNextPromise);
|
2014-04-09 01:30:24 -07:00
|
|
|
MOZ_ASSERT(aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
MOZ_COUNT_CTOR(WrapperPromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
HoldJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
WrapperPromiseCallback::~WrapperPromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(WrapperPromiseCallback);
|
2014-04-09 01:30:24 -07:00
|
|
|
DropJSObjects(this);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-29 14:12:14 -07:00
|
|
|
WrapperPromiseCallback::Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
2014-04-29 14:12:14 -07:00
|
|
|
JSAutoCompartment ac(aCx, mGlobal);
|
|
|
|
JS::Rooted<JS::Value> value(aCx, aValue);
|
|
|
|
if (!JS_WrapValue(aCx, &value)) {
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
return;
|
|
|
|
}
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
|
|
|
|
// If invoking callback threw an exception, run resolver's reject with the
|
|
|
|
// thrown exception as argument and the synchronous flag set.
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JS::Value> retValue(aCx,
|
2014-04-09 01:30:24 -07:00
|
|
|
mCallback->Call(value, rv, CallbackObject::eRethrowExceptions));
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
rv.WouldReportJSException();
|
|
|
|
|
|
|
|
if (rv.Failed() && rv.IsJSException()) {
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JS::Value> value(aCx);
|
|
|
|
rv.StealJSException(aCx, &value);
|
2013-07-11 13:40:36 -07:00
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
if (!JS_WrapValue(aCx, &value)) {
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
mNextPromise->RejectInternal(aCx, value, Promise::SyncTask);
|
2013-07-11 13:40:36 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-01-28 10:14:57 -08:00
|
|
|
// If the return value is the same as the promise itself, throw TypeError.
|
2014-04-09 01:30:24 -07:00
|
|
|
if (retValue.isObject()) {
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JSObject*> valueObj(aCx, &retValue.toObject());
|
2014-01-28 10:14:57 -08:00
|
|
|
Promise* returnedPromise;
|
|
|
|
nsresult r = UNWRAP_OBJECT(Promise, valueObj, returnedPromise);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(r) && returnedPromise == mNextPromise) {
|
|
|
|
const char* fileName = nullptr;
|
|
|
|
uint32_t lineNumber = 0;
|
|
|
|
|
|
|
|
// Try to get some information about the callback to report a sane error,
|
|
|
|
// but don't try too hard (only deals with scripted functions).
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JSObject*> unwrapped(aCx,
|
2014-01-28 10:14:57 -08:00
|
|
|
js::CheckedUnwrap(mCallback->Callback()));
|
|
|
|
|
|
|
|
if (unwrapped) {
|
2014-04-29 14:12:14 -07:00
|
|
|
JSAutoCompartment ac(aCx, unwrapped);
|
|
|
|
if (JS_ObjectIsFunction(aCx, unwrapped)) {
|
|
|
|
JS::Rooted<JS::Value> asValue(aCx, JS::ObjectValue(*unwrapped));
|
|
|
|
JS::Rooted<JSFunction*> func(aCx, JS_ValueToFunction(aCx, asValue));
|
2014-01-28 10:14:57 -08:00
|
|
|
|
|
|
|
MOZ_ASSERT(func);
|
2014-04-29 14:12:14 -07:00
|
|
|
JSScript* script = JS_GetFunctionScript(aCx, func);
|
2014-01-28 10:14:57 -08:00
|
|
|
if (script) {
|
2014-02-25 07:43:14 -08:00
|
|
|
fileName = JS_GetScriptFilename(script);
|
2014-04-29 14:12:14 -07:00
|
|
|
lineNumber = JS_GetScriptBaseLineNumber(aCx, script);
|
2014-01-28 10:14:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're back in aValue's compartment here.
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JSString*> stack(aCx, JS_GetEmptyString(JS_GetRuntime(aCx)));
|
|
|
|
JS::Rooted<JSString*> fn(aCx, JS_NewStringCopyZ(aCx, fileName));
|
2014-01-28 10:14:57 -08:00
|
|
|
if (!fn) {
|
|
|
|
// Out of memory. Promise will stay unresolved.
|
2014-04-29 14:12:14 -07:00
|
|
|
JS_ClearPendingException(aCx);
|
2014-01-28 10:14:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JSString*> message(aCx,
|
|
|
|
JS_NewStringCopyZ(aCx,
|
2014-01-28 10:14:57 -08:00
|
|
|
"then() cannot return same Promise that it resolves."));
|
|
|
|
if (!message) {
|
|
|
|
// Out of memory. Promise will stay unresolved.
|
2014-04-29 14:12:14 -07:00
|
|
|
JS_ClearPendingException(aCx);
|
2014-01-28 10:14:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
JS::Rooted<JS::Value> typeError(aCx);
|
|
|
|
if (!JS::CreateTypeError(aCx, stack, fn, lineNumber, 0,
|
2014-01-28 10:14:57 -08:00
|
|
|
nullptr, message, &typeError)) {
|
|
|
|
// Out of memory. Promise will stay unresolved.
|
2014-04-29 14:12:14 -07:00
|
|
|
JS_ClearPendingException(aCx);
|
2014-01-28 10:14:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
mNextPromise->RejectInternal(aCx, typeError, Promise::SyncTask);
|
2014-01-28 10:14:57 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
// Otherwise, run resolver's resolve with value and the synchronous flag
|
|
|
|
// set.
|
2014-04-29 14:12:14 -07:00
|
|
|
if (!JS_WrapValue(aCx, &retValue)) {
|
2014-04-09 01:30:24 -07:00
|
|
|
NS_WARNING("Failed to wrap value into the right compartment.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-04-29 14:12:14 -07:00
|
|
|
mNextPromise->ResolveInternal(aCx, retValue, Promise::SyncTask);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
2013-11-19 10:43:51 -08:00
|
|
|
// NativePromiseCallback
|
|
|
|
|
2014-04-25 09:49:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(NativePromiseCallback,
|
|
|
|
PromiseCallback, mHandler)
|
2013-11-19 10:43:51 -08:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(NativePromiseCallback)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(PromiseCallback)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(NativePromiseCallback, PromiseCallback)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(NativePromiseCallback, PromiseCallback)
|
|
|
|
|
|
|
|
NativePromiseCallback::NativePromiseCallback(PromiseNativeHandler* aHandler,
|
|
|
|
Promise::PromiseState aState)
|
|
|
|
: mHandler(aHandler)
|
|
|
|
, mState(aState)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aHandler);
|
|
|
|
MOZ_COUNT_CTOR(NativePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
NativePromiseCallback::~NativePromiseCallback()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(NativePromiseCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-04-29 14:12:14 -07:00
|
|
|
NativePromiseCallback::Call(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aValue)
|
2013-11-19 10:43:51 -08:00
|
|
|
{
|
|
|
|
if (mState == Promise::Resolved) {
|
|
|
|
mHandler->ResolvedCallback(aValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mState == Promise::Rejected) {
|
|
|
|
mHandler->RejectedCallback(aValue);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_NOTREACHED("huh?");
|
|
|
|
}
|
|
|
|
|
2013-07-11 13:40:36 -07:00
|
|
|
/* static */ PromiseCallback*
|
2014-04-09 01:30:24 -07:00
|
|
|
PromiseCallback::Factory(Promise* aNextPromise, JS::Handle<JSObject*> aGlobal,
|
|
|
|
AnyCallback* aCallback, Task aTask)
|
2013-07-11 13:40:36 -07:00
|
|
|
{
|
2013-09-11 09:03:04 -07:00
|
|
|
MOZ_ASSERT(aNextPromise);
|
2013-07-11 13:40:36 -07:00
|
|
|
|
|
|
|
// 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().
|
|
|
|
if (aCallback) {
|
2014-04-09 01:30:24 -07:00
|
|
|
return new WrapperPromiseCallback(aNextPromise, aGlobal, aCallback);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aTask == Resolve) {
|
2014-04-09 01:30:24 -07:00
|
|
|
return new ResolvePromiseCallback(aNextPromise, aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aTask == Reject) {
|
2014-04-09 01:30:24 -07:00
|
|
|
return new RejectPromiseCallback(aNextPromise, aGlobal);
|
2013-07-11 13:40:36 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(false, "This should not happen");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|