forked from mozilla/gecko-dev
-
Notifications
You must be signed in to change notification settings - Fork 2
/
PromiseResolver.cpp
171 lines (143 loc) · 5.04 KB
/
PromiseResolver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/* -*- 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 "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_COLLECTING_ADDREF(PromiseResolver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(PromiseResolver)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(PromiseResolver)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
PromiseResolver::PromiseResolver(Promise* aPromise)
: mPromise(aPromise)
, mResolvePending(false)
{
SetIsDOMBinding();
}
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 = UnwrapObject<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