-
Notifications
You must be signed in to change notification settings - Fork 0
/
nan_object_wrap_template.h
125 lines (106 loc) · 4.29 KB
/
nan_object_wrap_template.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
/*********************************************************************
* Derived from nan_object_wrap.h from
* NAN - Native Abstractions for Node.js
*
* Copyright (c) 2015 Christopher J. Brody
* Copyright (c) 2015 NAN contributors
*
* MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
********************************************************************/
#include <nan.h>
#ifndef NAN_OBJECT_WRAP_TEMPLATE_H_
#define NAN_OBJECT_WRAP_TEMPLATE_H_
template <typename T>
class ObjectWrapTemplateBase : public Nan::ObjectWrap {
protected:
static inline v8::Local<v8::FunctionTemplate>
NewConstructorFunctionTemplate(const char * object_class_name,
int internal_field_count) {
v8::Local<v8::FunctionTemplate> tpl = Nan::New<v8::FunctionTemplate>(New);
tpl->SetClassName(Nan::New(object_class_name).ToLocalChecked());
tpl->InstanceTemplate()->SetInternalFieldCount(1);
return tpl;
}
static inline void
SetNewFunction(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target,
v8::Local<v8::FunctionTemplate> tpl,
const char * object_class_name) {
Nan::Set(target, Nan::New(object_class_name).ToLocalChecked(), tpl->GetFunction());
}
ObjectWrapTemplateBase() {}
~ObjectWrapTemplateBase() {}
// Used by NewConstructorFunctionTemplate():
// (same as static NAN_METHOD(New) { ... })
static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
if (info.IsConstructCall()) { /* should always be the case here */
T *obj = new T(info);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
/* else do nothing */
}
// ref: https://nodejs.org/api/addons.html#addons_factory_of_wrapped_objects
static inline void NewInstanceMethod(v8::Local<v8::Function> cons, Nan::NAN_METHOD_ARGS_TYPE info) {
const int argc = info.Length();
std::vector<v8::Local<v8::Value> > argv;
for (int i=0; i<argc; ++i) argv.push_back(info[i]);
info.GetReturnValue().Set(Nan::NewInstance(cons, argc, &argv[0]).ToLocalChecked());
}
static inline v8::Local<v8::Value> NewInstanceMethod(v8::Local<v8::Function> cons, int argc, v8::Local<v8::Value> argv[]) {
return cons->NewInstance(argc, argv);
}
static inline T * ObjectFromMethodArgsInfo(Nan::NAN_METHOD_ARGS_TYPE info) {
return ObjectWrap::Unwrap<T>(info.This());
}
};
template <typename T>
class ObjectWrapTemplate : public ObjectWrapTemplateBase<T> {
protected:
static inline void
SetConstructorFunctionTemplate(v8::Local<v8::FunctionTemplate> tpl) {
constructor_tpl().Reset(tpl);
constructor().Reset(Nan::GetFunction(tpl).ToLocalChecked());
}
static inline v8::Local<v8::FunctionTemplate>
GetConstructorFunctionTemplate() {
return Nan::New(constructor_tpl());
}
ObjectWrapTemplate() {}
~ObjectWrapTemplate() {}
// Used by NewConstructorFunctionTemplate():
// (same as static NAN_METHOD(New) { ... })
static void New(Nan::NAN_METHOD_ARGS_TYPE info) {
if (info.IsConstructCall()) {
T *obj = new T(info);
obj->Wrap(info.This());
info.GetReturnValue().Set(info.This());
} else {
NewInstanceMethod(info);
}
}
// (same as static NAN_METHOD(NewInstanceMethod) { ... })
static inline void NewInstanceMethod(Nan::NAN_METHOD_ARGS_TYPE info) {
v8::Local<v8::Function> cons = Nan::New(constructor());
NewInstanceMethod(cons, info);
}
static inline v8::Local<v8::Value> NewInstanceMethod(int argc, v8::Local<v8::Value> argv[]) {
v8::Local<v8::Function> cons = Nan::New(constructor());
return cons->NewInstance(argc, argv);
}
static inline void NewInstanceMethod(v8::Local<v8::Function> cons, Nan::NAN_METHOD_ARGS_TYPE info) {
ObjectWrapTemplateBase<T>::NewInstanceMethod(cons, info);
}
static inline v8::Local<v8::Value> NewInstanceMethod(v8::Local<v8::Function> cons, int argc, v8::Local<v8::Value> argv[]) {
ObjectWrapTemplateBase<T>::NewInstanceMethod(cons, argc, argv);
}
private:
static inline Nan::Persistent<v8::FunctionTemplate> & constructor_tpl() {
static Nan::Persistent<v8::FunctionTemplate> my_constructor_tpl;
return my_constructor_tpl;
}
static inline Nan::Persistent<v8::Function> & constructor() {
static Nan::Persistent<v8::Function> my_constructor;
return my_constructor;
}
};
#endif // NAN_OBJECT_WRAP_TEMPLATE_H_