-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.h
319 lines (257 loc) · 8.22 KB
/
helpers.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#ifndef TOV8_HELPERS
#define TOV8_HELPERS
#include "type_literal.h" // TYPE_LITERAL macro for making aligned type string for boxing pointers
#if (NODE_MODULE_VERSION > 0x000B)
#define CBIND_UV_ASYNC_ARGS uv_async_t* handle
#else
#define CBIND_UV_ASYNC_ARGS uv_async_t* handle, int status
#endif
class v8_exception : public std::exception
{
std::string msg;
public:
v8_exception(const std::string& _msg) : msg(_msg) {
}
virtual const char* what() const throw() {
return msg.c_str();
}
};
template<class T>
class AsyncFunctionWrap : public NanAsyncWorker {
std::function<T()> func;
public:
AsyncFunctionWrap(std::function<T()> _func) : func(_func) {
}
virtual void Execute() final {
T retValue = func();
}
};
template<class T>
class FunctionWrap : public node::ObjectWrap {
std::function<void(T)> deleter;
T m_handle = nullptr;
FunctionWrap(T handle, std::function<void(T)> _deleter): deleter(_deleter), m_handle(handle) {
}
static void wrap(v8::Local<v8::Object>& obj, T in, std::function<void(T)> deleter = [](T){} ) {
(new FunctionWrap<T>(in, deleter))->Wrap(obj);
}
public:
static T nativeFromV8(v8::Handle<v8::Value> obj, const char* typeId, v8::Persistent<v8::Function>& persistent, T cbNative) {
if(!obj->IsFunction())
throw v8_exception("Argument needs to be a Function");
;
NanAssignPersistent(persistent, obj.As<v8::Function>());
return cbNative;
}
static v8::Handle<v8::Function> create(const char* typeId, T in, NanFunctionCallback cb, std::function<void(T)> deleter = [](T){} ) {
v8::Local<v8::FunctionTemplate> funTpl = NanNew<v8::FunctionTemplate>(cb);
v8::Local<v8::Function> fun = funTpl->GetFunction();
v8::Local<v8::Object> prototype = fun->GetPrototype().As<v8::Object>();
// this can be cached
v8::Local<v8::Function> bind = prototype->Get(NanNew<v8::String>("bind")).As<v8::Function>();
v8::Local<v8::ObjectTemplate> objTpl = v8::ObjectTemplate::New();
objTpl->SetInternalFieldCount(1);
v8::Local<v8::Object> obj = objTpl->NewInstance();
wrap(obj, in, deleter);
v8::Handle<v8::Value> argv[1] = {obj};
// note this can be much improved in terms of performance, for example
// by using internal fields with pointers
fun = NanMakeCallback(fun, bind, 1, argv).As<v8::Function>();
fun->Set(NanNew<v8::String>("__tov8_wrapped_data"), obj);
fun->Set(NanNew<v8::String>("nativeInterface"), NanNew<v8::String>(typeId));
return fun;
}
void clear_free() {
m_handle = nullptr;
deleter = nullptr;
}
virtual ~FunctionWrap() {
if(deleter)
deleter(m_handle);
}
static T Unwrap(v8::Handle<v8::Object> obj, const char* typeId = NULL) {
return node::ObjectWrap::Unwrap<FunctionWrap<T>>(obj)->get();
}
T get() const {
return m_handle;
}
};
template<class T>
class HandleWrap : public node::ObjectWrap {
std::function<void(T)> deleter;
T m_handle;
static void wrap(v8::Local<v8::Object>& obj, T in, std::function<void(T)> deleter = [](T){} ) {
(new HandleWrap<T>(in, deleter))->Wrap(obj);
}
public:
HandleWrap(T handle, std::function<void(T)> _deleter): deleter(_deleter), m_handle(handle) {
}
static v8::Handle<v8::Object> createSafe(const char* typeString, T in, std::function<void(T)> deleter = [](T){} ) {
v8::Local<v8::ObjectTemplate> objTpl = v8::ObjectTemplate::New();
objTpl->SetInternalFieldCount(2);
v8::Local<v8::Object> obj = objTpl->NewInstance();
NanSetInternalFieldPointer(obj, 1, (void*)typeString);
wrap(obj, in, deleter);
return obj;
}
static v8::Handle<v8::Object> create(T in, std::function<void(T)> deleter = [](T){} ) {
return createSafe("", in, deleter);
}
void clear_free() {
m_handle = nullptr;
deleter = nullptr;
}
virtual ~HandleWrap() {
if(deleter)
deleter(m_handle);
}
static T Unwrap(v8::Handle<v8::Object> obj, const char* typeId = NULL) {
void *typeStringPtr = (obj->InternalFieldCount() == 2)?NanGetInternalFieldPointer(obj, 1):nullptr;
if(!typeStringPtr) {
throw v8_exception("Unable to unbox pointer from object, not created by tov8");
}
if(typeId) {
if(strcmp(typeId, (char*)typeStringPtr) != 0) {
throw v8_exception(std::string("Unable to unbox pointer ") + typeId + " from pointer of type " + (const char*)typeStringPtr);
}
}
return node::ObjectWrap::Unwrap<HandleWrap<T>>(obj)->get();
}
T get() const {
return m_handle;
}
};
template<typename T>
struct auto_delete {
T obj;
auto_delete(T _obj) : obj(_obj) {}
operator T() {
return obj;
}
~auto_delete() {
delete obj;
}
};
template<typename T>
struct auto_deleteA {
T obj;
auto_deleteA(T _obj) : obj(_obj) {}
operator T() {
return obj;
}
~auto_deleteA() {
delete[] obj;
}
};
template<typename T>
struct auto_free {
T obj;
auto_free(T _obj) : obj(_obj) {}
operator T() {
return obj;
}
~auto_free() {
free((void*)obj);
}
};
// types
template<typename T>
T getArgument() {
return T();
}
template<typename T>
bool checkArgument(v8::Handle<v8::Value> obj) {
return true;
}
template<typename T>
T getArgument(v8::Handle<v8::Value> obj) {
return T();
}
template<typename T>
v8::Handle<v8::Value> toV8Type(T input) {
return NanUndefined();
}
#define cbind_declare_type(v8_type, type_name, type_identifier) \
template<> \
bool checkArgument<type_name>(v8::Handle<v8::Value> obj) { \
if(!obj->CBIND_CONCAT(Is, v8_type)()) \
return false; \
return true; \
} \
template<> \
type_name getArgument<type_name>(v8::Handle<v8::Value> obj) { \
return obj->CBIND_CONCAT(v8_type, Value)(); \
} \
v8::Handle<v8::Value> CBIND_CONCAT(toV8Type_,type_identifier)(type_name input) { \
return NanNew<v8::v8_type>(input); \
}
#define cbind_declare_type_alias(type_name, type_identifier_from, type_identifier) \
v8::Handle<v8::Value> CBIND_CONCAT(toV8Type_,type_identifier)(type_name input) { \
return CBIND_CONCAT(toV8Type_,type_identifier_from)(input); \
}
cbind_declare_type(Number, double, double)
cbind_declare_type(Number, float, float)
cbind_declare_type(Number, size_t, size_t)
cbind_declare_type(Number, int, int)
cbind_declare_type(Number, unsigned int, unsigned_int)
cbind_declare_type(Boolean, bool, bool)
cbind_declare_type_alias(int, int, int32_t)
cbind_declare_type_alias(unsigned int, unsigned_int, uint32_t)
// type string
template<>
bool checkArgument<std::string>(v8::Handle<v8::Value> obj) {
if(!obj->IsString())
return false;
return true;
}
template<>
std::string getArgument<std::string>(v8::Handle<v8::Value> obj) {
size_t count;
const char* str = NanCString(obj, &count);
std::string out = std::string(str);
delete[] str;
return out;
}
v8::Handle<v8::Value> toV8Type_std__string(std::string input) {
return NanNew<v8::String>(input.c_str());
}
// type const char*
template<>
bool checkArgument<const char*>(v8::Handle<v8::Value> obj) {
if(!obj->IsString())
return false;
return true;
}
template<>
const char* getArgument<const char*>(v8::Handle<v8::Value> obj) {
size_t count;
return NanCString(obj, &count);
}
v8::Handle<v8::Value> toV8Type_const_char_ptr(const char* input, std::function<void(const char*)> deleter = [](const char*){}) {
if(!input) {
return NanNull();
}
auto val = NanNew<v8::String>(input);
deleter(input);
return val;
}
template<>
bool checkArgument<char*>(v8::Handle<v8::Value> obj) {
if(!obj->IsString())
return false;
return true;
}
template<>
char* getArgument<char*>(v8::Handle<v8::Value> obj) {
size_t count;
return NanCString(obj, &count);
}
v8::Handle<v8::Value> toV8Type_char_ptr(char* input, std::function<void(char*)> deleter = [](char*){}) {
if(!input) {
return NanNull();
}
auto val = NanNew<v8::String>(input);
deleter(input);
return val;
}
#endif // TOV8_HELPERS