-
Notifications
You must be signed in to change notification settings - Fork 54
/
Promise-inl.h
164 lines (127 loc) · 5.07 KB
/
Promise-inl.h
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=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_Promise_inl_h
#define mozilla_dom_Promise_inl_h
#include "mozilla/TupleCycleCollection.h"
#include "mozilla/TypeTraits.h"
#include "mozilla/ResultExtensions.h"
#include "mozilla/dom/PromiseNativeHandler.h"
namespace mozilla {
namespace dom {
class PromiseNativeThenHandlerBase : public PromiseNativeHandler {
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_CLASS(PromiseNativeThenHandlerBase)
PromiseNativeThenHandlerBase(Promise& aPromise) : mPromise(&aPromise) {}
void ResolvedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
void RejectedCallback(JSContext* aCx, JS::Handle<JS::Value> aValue) override;
protected:
virtual ~PromiseNativeThenHandlerBase() = default;
virtual already_AddRefed<Promise> CallResolveCallback(
JSContext* aCx, JS::Handle<JS::Value> aValue) = 0;
virtual void Traverse(nsCycleCollectionTraversalCallback&) = 0;
virtual void Unlink() = 0;
RefPtr<Promise> mPromise;
};
namespace {
template <typename T,
bool = IsRefcounted<typename RemovePointer<T>::Type>::value,
bool = (IsConvertible<T, nsISupports*>::value ||
IsConvertible<T*, nsISupports*>::value)>
struct StorageTypeHelper {
using Type = T;
};
template <typename T>
struct StorageTypeHelper<T, true, true> {
using Type = nsCOMPtr<T>;
};
template <typename T>
struct StorageTypeHelper<nsCOMPtr<T>, true, true> {
using Type = nsCOMPtr<T>;
};
template <typename T>
struct StorageTypeHelper<T*, true, false> {
using Type = RefPtr<T>;
};
template <template <typename> class SmartPtr, typename T>
struct StorageTypeHelper<SmartPtr<T>, true, false>
: EnableIf<IsConvertible<SmartPtr<T>, T*>::value, RefPtr<T>> {};
template <typename T>
using StorageType = typename StorageTypeHelper<typename Decay<T>::Type>::Type;
// Helpers to choose the correct argument type based on the storage type. Smart
// pointers are converted to the corresponding raw pointer type. Everything else
// is passed by move reference.
//
// Note: We can't just use std::forward for this because the input type may be a
// raw pointer which does not match the argument type, and while the
// spec-compliant behavior there should still give us the expected results, MSVC
// considers it an illegal use of std::forward.
template <template <typename> class SmartPtr, typename T>
decltype(DeclVal<SmartPtr<T>>().get()) ArgType(SmartPtr<T>& aVal) {
return aVal.get();
}
template <typename T>
T&& ArgType(T& aVal) {
return std::move(aVal);
}
using ::ImplCycleCollectionUnlink;
template <typename Callback, typename... Args>
class NativeThenHandler final : public PromiseNativeThenHandlerBase {
public:
NativeThenHandler(Promise& aPromise, Callback&& aOnResolve, Args&&... aArgs)
: PromiseNativeThenHandlerBase(aPromise),
mOnResolve(std::forward<Callback>(aOnResolve)),
mArgs(std::forward<Args>(aArgs)...) {}
protected:
void Traverse(nsCycleCollectionTraversalCallback& cb) override {
auto* tmp = this;
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mArgs)
}
void Unlink() override {
auto* tmp = this;
NS_IMPL_CYCLE_COLLECTION_UNLINK(mArgs)
}
already_AddRefed<Promise> CallResolveCallback(
JSContext* aCx, JS::Handle<JS::Value> aValue) override {
return CallCallback(aCx, mOnResolve, aValue);
}
template <size_t... Indices>
already_AddRefed<Promise> CallCallback(JSContext* aCx,
const Callback& aHandler,
JS::Handle<JS::Value> aValue,
std::index_sequence<Indices...>) {
return mOnResolve(aCx, aValue, ArgType(Get<Indices>(mArgs))...);
}
already_AddRefed<Promise> CallCallback(JSContext* aCx,
const Callback& aHandler,
JS::Handle<JS::Value> aValue) {
return CallCallback(aCx, aHandler, aValue,
std::index_sequence_for<Args...>{});
}
Callback mOnResolve;
Tuple<StorageType<Args>...> mArgs;
};
} // anonymous namespace
template <typename Callback, typename... Args>
Promise::ThenResult<Callback, Args...> Promise::ThenWithCycleCollectedArgs(
Callback&& aOnResolve, Args&&... aArgs) {
using HandlerType = NativeThenHandler<Callback, Args...>;
ErrorResult rv;
RefPtr<Promise> promise = Promise::Create(GetParentObject(), rv);
if (rv.Failed()) {
return Err(rv.StealNSResult());
}
auto* handler =
new (fallible) HandlerType(*promise, std::forward<Callback>(aOnResolve),
std::forward<Args>(aArgs)...);
if (!handler) {
return Err(NS_ERROR_OUT_OF_MEMORY);
}
AppendNativeHandler(handler);
return std::move(promise);
}
} // namespace dom
} // namespace mozilla
#endif // mozilla_dom_Promise_inl_h