Skip to content

Commit 209151e

Browse files
dave-365vasudevanlegendecas
authored
feat!: enforce callback signatures with C++ Concepts (#1762)
Co-authored-by: vasudevan <vasudevan@syntrio.in> Co-authored-by: Chengzhong Wu <cwu631@bloomberg.net>
1 parent 82d1a35 commit 209151e

7 files changed

Lines changed: 635 additions & 263 deletions

File tree

‎napi-inl.deprecated.h‎

Lines changed: 71 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
////////////////////////////////////////////////////////////////////////////////
77

88
template <typename Getter>
9-
inline PropertyDescriptor PropertyDescriptor::Accessor(
10-
const char* utf8name,
11-
Getter getter,
12-
napi_property_attributes attributes,
13-
void* /*data*/) {
9+
NAPI_REQUIRES(concepts::GetterCallable<Getter>)
10+
inline PropertyDescriptor
11+
PropertyDescriptor::Accessor(const char* utf8name,
12+
Getter getter,
13+
napi_property_attributes attributes,
14+
void* /*data*/) {
1415
using CbData = details::CallbackData<Getter, Napi::Value>;
1516
// TODO: Delete when the function is destroyed
1617
auto callbackData = new CbData({getter, nullptr});
@@ -26,20 +27,22 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(
2627
}
2728

2829
template <typename Getter>
29-
inline PropertyDescriptor PropertyDescriptor::Accessor(
30-
const std::string& utf8name,
31-
Getter getter,
32-
napi_property_attributes attributes,
33-
void* data) {
30+
NAPI_REQUIRES(concepts::GetterCallable<Getter>)
31+
inline PropertyDescriptor
32+
PropertyDescriptor::Accessor(const std::string& utf8name,
33+
Getter getter,
34+
napi_property_attributes attributes,
35+
void* data) {
3436
return Accessor(utf8name.c_str(), getter, attributes, data);
3537
}
3638

3739
template <typename Getter>
38-
inline PropertyDescriptor PropertyDescriptor::Accessor(
39-
napi_value name,
40-
Getter getter,
41-
napi_property_attributes attributes,
42-
void* /*data*/) {
40+
NAPI_REQUIRES(concepts::GetterCallable<Getter>)
41+
inline PropertyDescriptor
42+
PropertyDescriptor::Accessor(napi_value name,
43+
Getter getter,
44+
napi_property_attributes attributes,
45+
void* /*data*/) {
4346
using CbData = details::CallbackData<Getter, Napi::Value>;
4447
// TODO: Delete when the function is destroyed
4548
auto callbackData = new CbData({getter, nullptr});
@@ -55,19 +58,22 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(
5558
}
5659

5760
template <typename Getter>
61+
NAPI_REQUIRES(concepts::GetterCallable<Getter>)
5862
inline PropertyDescriptor PropertyDescriptor::Accessor(
5963
Name name, Getter getter, napi_property_attributes attributes, void* data) {
6064
napi_value nameValue = name;
6165
return PropertyDescriptor::Accessor(nameValue, getter, attributes, data);
6266
}
6367

6468
template <typename Getter, typename Setter>
65-
inline PropertyDescriptor PropertyDescriptor::Accessor(
66-
const char* utf8name,
67-
Getter getter,
68-
Setter setter,
69-
napi_property_attributes attributes,
70-
void* /*data*/) {
69+
NAPI_REQUIRES(
70+
concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>)
71+
inline PropertyDescriptor
72+
PropertyDescriptor::Accessor(const char* utf8name,
73+
Getter getter,
74+
Setter setter,
75+
napi_property_attributes attributes,
76+
void* /*data*/) {
7177
using CbData = details::AccessorCallbackData<Getter, Setter>;
7278
// TODO: Delete when the function is destroyed
7379
auto callbackData = new CbData({getter, setter, nullptr});
@@ -83,22 +89,26 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(
8389
}
8490

8591
template <typename Getter, typename Setter>
86-
inline PropertyDescriptor PropertyDescriptor::Accessor(
87-
const std::string& utf8name,
88-
Getter getter,
89-
Setter setter,
90-
napi_property_attributes attributes,
91-
void* data) {
92+
NAPI_REQUIRES(
93+
concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>)
94+
inline PropertyDescriptor
95+
PropertyDescriptor::Accessor(const std::string& utf8name,
96+
Getter getter,
97+
Setter setter,
98+
napi_property_attributes attributes,
99+
void* data) {
92100
return Accessor(utf8name.c_str(), getter, setter, attributes, data);
93101
}
94102

95103
template <typename Getter, typename Setter>
96-
inline PropertyDescriptor PropertyDescriptor::Accessor(
97-
napi_value name,
98-
Getter getter,
99-
Setter setter,
100-
napi_property_attributes attributes,
101-
void* /*data*/) {
104+
NAPI_REQUIRES(
105+
concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>)
106+
inline PropertyDescriptor
107+
PropertyDescriptor::Accessor(napi_value name,
108+
Getter getter,
109+
Setter setter,
110+
napi_property_attributes attributes,
111+
void* /*data*/) {
102112
using CbData = details::AccessorCallbackData<Getter, Setter>;
103113
// TODO: Delete when the function is destroyed
104114
auto callbackData = new CbData({getter, setter, nullptr});
@@ -114,23 +124,26 @@ inline PropertyDescriptor PropertyDescriptor::Accessor(
114124
}
115125

116126
template <typename Getter, typename Setter>
117-
inline PropertyDescriptor PropertyDescriptor::Accessor(
118-
Name name,
119-
Getter getter,
120-
Setter setter,
121-
napi_property_attributes attributes,
122-
void* data) {
127+
NAPI_REQUIRES(
128+
concepts::GetterCallable<Getter>&& concepts::SetterCallable<Setter>)
129+
inline PropertyDescriptor
130+
PropertyDescriptor::Accessor(Name name,
131+
Getter getter,
132+
Setter setter,
133+
napi_property_attributes attributes,
134+
void* data) {
123135
napi_value nameValue = name;
124136
return PropertyDescriptor::Accessor(
125137
nameValue, getter, setter, attributes, data);
126138
}
127139

128140
template <typename Callable>
129-
inline PropertyDescriptor PropertyDescriptor::Function(
130-
const char* utf8name,
131-
Callable cb,
132-
napi_property_attributes attributes,
133-
void* /*data*/) {
141+
NAPI_REQUIRES(concepts::CallbackCallable<Callable>)
142+
inline PropertyDescriptor
143+
PropertyDescriptor::Function(const char* utf8name,
144+
Callable cb,
145+
napi_property_attributes attributes,
146+
void* /*data*/) {
134147
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
135148
using CbData = details::CallbackData<Callable, ReturnType>;
136149
// TODO: Delete when the function is destroyed
@@ -147,20 +160,22 @@ inline PropertyDescriptor PropertyDescriptor::Function(
147160
}
148161

149162
template <typename Callable>
150-
inline PropertyDescriptor PropertyDescriptor::Function(
151-
const std::string& utf8name,
152-
Callable cb,
153-
napi_property_attributes attributes,
154-
void* data) {
163+
NAPI_REQUIRES(concepts::CallbackCallable<Callable>)
164+
inline PropertyDescriptor
165+
PropertyDescriptor::Function(const std::string& utf8name,
166+
Callable cb,
167+
napi_property_attributes attributes,
168+
void* data) {
155169
return Function(utf8name.c_str(), cb, attributes, data);
156170
}
157171

158172
template <typename Callable>
159-
inline PropertyDescriptor PropertyDescriptor::Function(
160-
napi_value name,
161-
Callable cb,
162-
napi_property_attributes attributes,
163-
void* /*data*/) {
173+
NAPI_REQUIRES(concepts::CallbackCallable<Callable>)
174+
inline PropertyDescriptor
175+
PropertyDescriptor::Function(napi_value name,
176+
Callable cb,
177+
napi_property_attributes attributes,
178+
void* /*data*/) {
164179
using ReturnType = decltype(cb(CallbackInfo(nullptr, nullptr)));
165180
using CbData = details::CallbackData<Callable, ReturnType>;
166181
// TODO: Delete when the function is destroyed
@@ -177,10 +192,11 @@ inline PropertyDescriptor PropertyDescriptor::Function(
177192
}
178193

179194
template <typename Callable>
195+
NAPI_REQUIRES(concepts::CallbackCallable<Callable>)
180196
inline PropertyDescriptor PropertyDescriptor::Function(
181197
Name name, Callable cb, napi_property_attributes attributes, void* data) {
182198
napi_value nameValue = name;
183199
return PropertyDescriptor::Function(nameValue, cb, attributes, data);
184200
}
185201

186-
#endif // !SRC_NAPI_INL_DEPRECATED_H_
202+
#endif // SRC_NAPI_INL_DEPRECATED_H_

0 commit comments

Comments
 (0)