1 |
3a515b92
|
cagy
|
/*********************************************************************
|
2 |
|
|
* NAN - Native Abstractions for Node.js
|
3 |
|
|
*
|
4 |
|
|
* Copyright (c) 2018 NAN contributors
|
5 |
|
|
*
|
6 |
|
|
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
|
7 |
|
|
********************************************************************/
|
8 |
|
|
|
9 |
|
|
#ifndef NAN_NEW_H_
|
10 |
|
|
#define NAN_NEW_H_
|
11 |
|
|
|
12 |
|
|
namespace imp { // scnr
|
13 |
|
|
|
14 |
|
|
// TODO(agnat): Generalize
|
15 |
|
|
template <typename T> v8::Local<T> To(v8::Local<v8::Integer> i);
|
16 |
|
|
|
17 |
|
|
template <>
|
18 |
|
|
inline
|
19 |
|
|
v8::Local<v8::Integer>
|
20 |
|
|
To<v8::Integer>(v8::Local<v8::Integer> i) {
|
21 |
|
|
return Nan::To<v8::Integer>(i).ToLocalChecked();
|
22 |
|
|
}
|
23 |
|
|
|
24 |
|
|
template <>
|
25 |
|
|
inline
|
26 |
|
|
v8::Local<v8::Int32>
|
27 |
|
|
To<v8::Int32>(v8::Local<v8::Integer> i) {
|
28 |
|
|
return Nan::To<v8::Int32>(i).ToLocalChecked();
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
template <>
|
32 |
|
|
inline
|
33 |
|
|
v8::Local<v8::Uint32>
|
34 |
|
|
To<v8::Uint32>(v8::Local<v8::Integer> i) {
|
35 |
|
|
return Nan::To<v8::Uint32>(i).ToLocalChecked();
|
36 |
|
|
}
|
37 |
|
|
|
38 |
|
|
template <typename T> struct FactoryBase {
|
39 |
|
|
typedef v8::Local<T> return_t;
|
40 |
|
|
};
|
41 |
|
|
|
42 |
|
|
template <typename T> struct MaybeFactoryBase {
|
43 |
|
|
typedef MaybeLocal<T> return_t;
|
44 |
|
|
};
|
45 |
|
|
|
46 |
|
|
template <typename T> struct Factory;
|
47 |
|
|
|
48 |
|
|
template <>
|
49 |
|
|
struct Factory<v8::Array> : FactoryBase<v8::Array> {
|
50 |
|
|
static inline return_t New();
|
51 |
|
|
static inline return_t New(int length);
|
52 |
|
|
};
|
53 |
|
|
|
54 |
|
|
template <>
|
55 |
|
|
struct Factory<v8::Boolean> : FactoryBase<v8::Boolean> {
|
56 |
|
|
static inline return_t New(bool value);
|
57 |
|
|
};
|
58 |
|
|
|
59 |
|
|
template <>
|
60 |
|
|
struct Factory<v8::BooleanObject> : FactoryBase<v8::BooleanObject> {
|
61 |
|
|
static inline return_t New(bool value);
|
62 |
|
|
};
|
63 |
|
|
|
64 |
|
|
template <>
|
65 |
|
|
struct Factory<v8::Context> : FactoryBase<v8::Context> {
|
66 |
|
|
static inline
|
67 |
|
|
return_t
|
68 |
|
|
New( v8::ExtensionConfiguration* extensions = NULL
|
69 |
|
|
, v8::Local<v8::ObjectTemplate> tmpl = v8::Local<v8::ObjectTemplate>()
|
70 |
|
|
, v8::Local<v8::Value> obj = v8::Local<v8::Value>());
|
71 |
|
|
};
|
72 |
|
|
|
73 |
|
|
template <>
|
74 |
|
|
struct Factory<v8::Date> : MaybeFactoryBase<v8::Date> {
|
75 |
|
|
static inline return_t New(double value);
|
76 |
|
|
};
|
77 |
|
|
|
78 |
|
|
template <>
|
79 |
|
|
struct Factory<v8::External> : FactoryBase<v8::External> {
|
80 |
|
|
static inline return_t New(void *value);
|
81 |
|
|
};
|
82 |
|
|
|
83 |
|
|
template <>
|
84 |
|
|
struct Factory<v8::Function> : FactoryBase<v8::Function> {
|
85 |
|
|
static inline
|
86 |
|
|
return_t
|
87 |
|
|
New( FunctionCallback callback
|
88 |
|
|
, v8::Local<v8::Value> data = v8::Local<v8::Value>());
|
89 |
|
|
};
|
90 |
|
|
|
91 |
|
|
template <>
|
92 |
|
|
struct Factory<v8::FunctionTemplate> : FactoryBase<v8::FunctionTemplate> {
|
93 |
|
|
static inline
|
94 |
|
|
return_t
|
95 |
|
|
New( FunctionCallback callback = NULL
|
96 |
|
|
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
|
97 |
|
|
, v8::Local<v8::Signature> signature = v8::Local<v8::Signature>());
|
98 |
|
|
};
|
99 |
|
|
|
100 |
|
|
template <>
|
101 |
|
|
struct Factory<v8::Number> : FactoryBase<v8::Number> {
|
102 |
|
|
static inline return_t New(double value);
|
103 |
|
|
};
|
104 |
|
|
|
105 |
|
|
template <>
|
106 |
|
|
struct Factory<v8::NumberObject> : FactoryBase<v8::NumberObject> {
|
107 |
|
|
static inline return_t New(double value);
|
108 |
|
|
};
|
109 |
|
|
|
110 |
|
|
template <typename T>
|
111 |
|
|
struct IntegerFactory : FactoryBase<T> {
|
112 |
|
|
typedef typename FactoryBase<T>::return_t return_t;
|
113 |
|
|
static inline return_t New(int32_t value);
|
114 |
|
|
static inline return_t New(uint32_t value);
|
115 |
|
|
};
|
116 |
|
|
|
117 |
|
|
template <>
|
118 |
|
|
struct Factory<v8::Integer> : IntegerFactory<v8::Integer> {};
|
119 |
|
|
|
120 |
|
|
template <>
|
121 |
|
|
struct Factory<v8::Int32> : IntegerFactory<v8::Int32> {};
|
122 |
|
|
|
123 |
|
|
template <>
|
124 |
|
|
struct Factory<v8::Uint32> : FactoryBase<v8::Uint32> {
|
125 |
|
|
static inline return_t New(int32_t value);
|
126 |
|
|
static inline return_t New(uint32_t value);
|
127 |
|
|
};
|
128 |
|
|
|
129 |
|
|
template <>
|
130 |
|
|
struct Factory<v8::Object> : FactoryBase<v8::Object> {
|
131 |
|
|
static inline return_t New();
|
132 |
|
|
};
|
133 |
|
|
|
134 |
|
|
template <>
|
135 |
|
|
struct Factory<v8::ObjectTemplate> : FactoryBase<v8::ObjectTemplate> {
|
136 |
|
|
static inline return_t New();
|
137 |
|
|
};
|
138 |
|
|
|
139 |
|
|
template <>
|
140 |
|
|
struct Factory<v8::RegExp> : MaybeFactoryBase<v8::RegExp> {
|
141 |
|
|
static inline return_t New(
|
142 |
|
|
v8::Local<v8::String> pattern, v8::RegExp::Flags flags);
|
143 |
|
|
};
|
144 |
|
|
|
145 |
|
|
template <>
|
146 |
|
|
struct Factory<v8::Script> : MaybeFactoryBase<v8::Script> {
|
147 |
|
|
static inline return_t New( v8::Local<v8::String> source);
|
148 |
|
|
static inline return_t New( v8::Local<v8::String> source
|
149 |
|
|
, v8::ScriptOrigin const& origin);
|
150 |
|
|
};
|
151 |
|
|
|
152 |
|
|
template <>
|
153 |
|
|
struct Factory<v8::Signature> : FactoryBase<v8::Signature> {
|
154 |
|
|
typedef v8::Local<v8::FunctionTemplate> FTH;
|
155 |
|
|
static inline return_t New(FTH receiver = FTH());
|
156 |
|
|
};
|
157 |
|
|
|
158 |
|
|
template <>
|
159 |
|
|
struct Factory<v8::String> : MaybeFactoryBase<v8::String> {
|
160 |
|
|
static inline return_t New();
|
161 |
|
|
static inline return_t New(const char *value, int length = -1);
|
162 |
|
|
static inline return_t New(const uint16_t *value, int length = -1);
|
163 |
|
|
static inline return_t New(std::string const& value);
|
164 |
|
|
|
165 |
|
|
static inline return_t New(v8::String::ExternalStringResource * value);
|
166 |
|
|
static inline return_t New(ExternalOneByteStringResource * value);
|
167 |
|
|
};
|
168 |
|
|
|
169 |
|
|
template <>
|
170 |
|
|
struct Factory<v8::StringObject> : FactoryBase<v8::StringObject> {
|
171 |
|
|
static inline return_t New(v8::Local<v8::String> value);
|
172 |
|
|
};
|
173 |
|
|
|
174 |
|
|
} // end of namespace imp
|
175 |
|
|
|
176 |
|
|
#if (NODE_MODULE_VERSION >= 12)
|
177 |
|
|
|
178 |
|
|
namespace imp {
|
179 |
|
|
|
180 |
|
|
template <>
|
181 |
|
|
struct Factory<v8::UnboundScript> : MaybeFactoryBase<v8::UnboundScript> {
|
182 |
|
|
static inline return_t New( v8::Local<v8::String> source);
|
183 |
|
|
static inline return_t New( v8::Local<v8::String> source
|
184 |
|
|
, v8::ScriptOrigin const& origin);
|
185 |
|
|
};
|
186 |
|
|
|
187 |
|
|
} // end of namespace imp
|
188 |
|
|
|
189 |
|
|
# include "nan_implementation_12_inl.h"
|
190 |
|
|
|
191 |
|
|
#else // NODE_MODULE_VERSION >= 12
|
192 |
|
|
|
193 |
|
|
# include "nan_implementation_pre_12_inl.h"
|
194 |
|
|
|
195 |
|
|
#endif
|
196 |
|
|
|
197 |
|
|
//=== API ======================================================================
|
198 |
|
|
|
199 |
|
|
template <typename T>
|
200 |
|
|
typename imp::Factory<T>::return_t
|
201 |
|
|
New() {
|
202 |
|
|
return imp::Factory<T>::New();
|
203 |
|
|
}
|
204 |
|
|
|
205 |
|
|
template <typename T, typename A0>
|
206 |
|
|
typename imp::Factory<T>::return_t
|
207 |
|
|
New(A0 arg0) {
|
208 |
|
|
return imp::Factory<T>::New(arg0);
|
209 |
|
|
}
|
210 |
|
|
|
211 |
|
|
template <typename T, typename A0, typename A1>
|
212 |
|
|
typename imp::Factory<T>::return_t
|
213 |
|
|
New(A0 arg0, A1 arg1) {
|
214 |
|
|
return imp::Factory<T>::New(arg0, arg1);
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
template <typename T, typename A0, typename A1, typename A2>
|
218 |
|
|
typename imp::Factory<T>::return_t
|
219 |
|
|
New(A0 arg0, A1 arg1, A2 arg2) {
|
220 |
|
|
return imp::Factory<T>::New(arg0, arg1, arg2);
|
221 |
|
|
}
|
222 |
|
|
|
223 |
|
|
template <typename T, typename A0, typename A1, typename A2, typename A3>
|
224 |
|
|
typename imp::Factory<T>::return_t
|
225 |
|
|
New(A0 arg0, A1 arg1, A2 arg2, A3 arg3) {
|
226 |
|
|
return imp::Factory<T>::New(arg0, arg1, arg2, arg3);
|
227 |
|
|
}
|
228 |
|
|
|
229 |
|
|
// Note(agnat): When passing overloaded function pointers to template functions
|
230 |
|
|
// as generic arguments the compiler needs help in picking the right overload.
|
231 |
|
|
// These two functions handle New<Function> and New<FunctionTemplate> with
|
232 |
|
|
// all argument variations.
|
233 |
|
|
|
234 |
|
|
// v8::Function and v8::FunctionTemplate with one or two arguments
|
235 |
|
|
template <typename T>
|
236 |
|
|
typename imp::Factory<T>::return_t
|
237 |
|
|
New( FunctionCallback callback
|
238 |
|
|
, v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
|
239 |
|
|
return imp::Factory<T>::New(callback, data);
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
// v8::Function and v8::FunctionTemplate with three arguments
|
243 |
|
|
template <typename T, typename A2>
|
244 |
|
|
typename imp::Factory<T>::return_t
|
245 |
|
|
New( FunctionCallback callback
|
246 |
|
|
, v8::Local<v8::Value> data = v8::Local<v8::Value>()
|
247 |
|
|
, A2 a2 = A2()) {
|
248 |
|
|
return imp::Factory<T>::New(callback, data, a2);
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
// Convenience
|
252 |
|
|
|
253 |
|
|
#if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
|
254 |
|
|
template <typename T> inline v8::Local<T> New(v8::Handle<T> h);
|
255 |
|
|
#endif
|
256 |
|
|
|
257 |
|
|
#if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
|
258 |
|
|
template <typename T, typename M>
|
259 |
|
|
inline v8::Local<T> New(v8::Persistent<T, M> const& p);
|
260 |
|
|
#else
|
261 |
|
|
template <typename T> inline v8::Local<T> New(v8::Persistent<T> const& p);
|
262 |
|
|
#endif
|
263 |
|
|
template <typename T, typename M>
|
264 |
|
|
inline v8::Local<T> New(Persistent<T, M> const& p);
|
265 |
|
|
template <typename T>
|
266 |
|
|
inline v8::Local<T> New(Global<T> const& p);
|
267 |
|
|
|
268 |
|
|
inline
|
269 |
|
|
imp::Factory<v8::Boolean>::return_t
|
270 |
|
|
New(bool value) {
|
271 |
|
|
return New<v8::Boolean>(value);
|
272 |
|
|
}
|
273 |
|
|
|
274 |
|
|
inline
|
275 |
|
|
imp::Factory<v8::Int32>::return_t
|
276 |
|
|
New(int32_t value) {
|
277 |
|
|
return New<v8::Int32>(value);
|
278 |
|
|
}
|
279 |
|
|
|
280 |
|
|
inline
|
281 |
|
|
imp::Factory<v8::Uint32>::return_t
|
282 |
|
|
New(uint32_t value) {
|
283 |
|
|
return New<v8::Uint32>(value);
|
284 |
|
|
}
|
285 |
|
|
|
286 |
|
|
inline
|
287 |
|
|
imp::Factory<v8::Number>::return_t
|
288 |
|
|
New(double value) {
|
289 |
|
|
return New<v8::Number>(value);
|
290 |
|
|
}
|
291 |
|
|
|
292 |
|
|
inline
|
293 |
|
|
imp::Factory<v8::String>::return_t
|
294 |
|
|
New(std::string const& value) { // NOLINT(build/include_what_you_use)
|
295 |
|
|
return New<v8::String>(value);
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
inline
|
299 |
|
|
imp::Factory<v8::String>::return_t
|
300 |
|
|
New(const char * value, int length) {
|
301 |
|
|
return New<v8::String>(value, length);
|
302 |
|
|
}
|
303 |
|
|
|
304 |
|
|
inline
|
305 |
|
|
imp::Factory<v8::String>::return_t
|
306 |
|
|
New(const uint16_t * value, int length) {
|
307 |
|
|
return New<v8::String>(value, length);
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
inline
|
311 |
|
|
imp::Factory<v8::String>::return_t
|
312 |
|
|
New(const char * value) {
|
313 |
|
|
return New<v8::String>(value);
|
314 |
|
|
}
|
315 |
|
|
|
316 |
|
|
inline
|
317 |
|
|
imp::Factory<v8::String>::return_t
|
318 |
|
|
New(const uint16_t * value) {
|
319 |
|
|
return New<v8::String>(value);
|
320 |
|
|
}
|
321 |
|
|
|
322 |
|
|
inline
|
323 |
|
|
imp::Factory<v8::String>::return_t
|
324 |
|
|
New(v8::String::ExternalStringResource * value) {
|
325 |
|
|
return New<v8::String>(value);
|
326 |
|
|
}
|
327 |
|
|
|
328 |
|
|
inline
|
329 |
|
|
imp::Factory<v8::String>::return_t
|
330 |
|
|
New(ExternalOneByteStringResource * value) {
|
331 |
|
|
return New<v8::String>(value);
|
332 |
|
|
}
|
333 |
|
|
|
334 |
|
|
inline
|
335 |
|
|
imp::Factory<v8::RegExp>::return_t
|
336 |
|
|
New(v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
|
337 |
|
|
return New<v8::RegExp>(pattern, flags);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
#endif // NAN_NEW_H_
|