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