-
Notifications
You must be signed in to change notification settings - Fork 121
/
Binding.cc
217 lines (160 loc) · 5.53 KB
/
Binding.cc
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
// This file is part of nbind, copyright (C) 2014-2015 BusFaster Ltd.
// Released under the MIT license, see LICENSE.
#ifdef BUILDING_NODE_EXTENSION
#include <cstring>
#include "nbind/BindDefiner.h"
using namespace v8;
using namespace nbind;
const char *nbind :: emptyGetter = "";
const char *nbind :: emptySetter = "";
class NBind {
public:
static void bind(const char *name, cbFunction &func) {
Bindings::setValueConstructorByName(name, func);
}
};
void Bindings :: setValueConstructorByName(const char *name, cbFunction &func) {
for(auto *bindClass : getClassList()) {
if(strcmp(bindClass->getName(), name) == 0) {
bindClass->setValueConstructorJS(func);
break;
}
}
}
// Linkage for module-wide error message.
const char *Status :: message;
void Bindings :: registerClass(BindClassBase &bindClass) {
getClassList().emplace_front(&bindClass);
}
// Convert getter names like "getFoo" into property names like "foo".
// This could be so much more concisely written with regexps...
const char *stripGetterPrefix(const char *name, char *&nameBuf) {
if((name[0] == 'G' || name[0] == 'g') && name[1] == 'e' && name[2] == 't') {
char c = name[3];
if(c == '_') {
// "Get_foo", "get_foo" => Remove 4 first characters.
name += 4;
} else if(c >= 'a' && c <= 'z') {
// "Getfoo", "getfoo" => Remove 3 first characters.
name += 3;
} else if(c >= 'A' && c <= 'Z') {
// "GetFoo", "getFoo" => Remove 3 first characters,
// make a modifiable copy and lowercase first letter.
if(nameBuf != nullptr) free(nameBuf);
nameBuf = strdup(name + 3);
if(nameBuf != nullptr) {
nameBuf[0] = c + ('a' - 'A');
name = nameBuf;
} else {
// Memory allocation failed.
// The world will soon end anyway, so just declare
// the getter without stripping the "get" prefix.
}
}
}
return(name);
}
typedef BaseSignature::Type SigType;
void Bindings :: initModule(Handle<Object> exports) {
// Register NBind a second time to make sure it's first on the list
// of classes and gets defined first, so pointers to it can be added
// to other classes to enforce its visibility in npm exports.
registerClass(BindClass<NBind>::getInstance());
Local<Function> nBindConstructor;
for(auto *bindClass : getClassList()) {
// Avoid registering the same class twice.
if(bindClass->isReady()) continue;
bindClass->init();
// Local<FunctionTemplate> constructorTemplate = Nan::New<FunctionTemplate>(bindClass->createPtr);
Local<FunctionTemplate> constructorTemplate = Nan::New<FunctionTemplate>(
Overloader::create,
Nan::New<Number>(bindClass->wrapperConstructorNum << overloadShift)
);
constructorTemplate->SetClassName(Nan::New<String>(bindClass->getName()).ToLocalChecked());
constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
Nan::SetPrototypeTemplate(constructorTemplate, "free",
Nan::New<FunctionTemplate>(
bindClass->getDeleter()
)->GetFunction()
);
Local<ObjectTemplate> proto = constructorTemplate->PrototypeTemplate();
char *nameBuf = nullptr;
funcPtr setter = nullptr;
funcPtr getter = nullptr;
unsigned int getterNum = 0;
unsigned int setterNum = 0;
for(auto &func : bindClass->getMethodList()) {
// TODO: Support for function overloading goes here.
BaseSignature *signature = func.getSignature();
if(signature == nullptr) {
if(func.getName() == emptyGetter) {
getter = nullptr;
getterNum = 0;
}
if(func.getName() == emptySetter) {
setter = nullptr;
setterNum = 0;
}
continue;
}
switch(signature->getType()) {
case SigType::method:
Nan::SetPrototypeTemplate(constructorTemplate, func.getName(),
Nan::New<FunctionTemplate>(
reinterpret_cast<BindClassBase::jsMethod *>(signature->getCaller()),
Nan::New<Number>(func.getNum())
)->GetFunction()
);
break;
case SigType::function:
Nan::SetTemplate(constructorTemplate, func.getName(),
Nan::New<FunctionTemplate>(
reinterpret_cast<BindClassBase::jsMethod *>(signature->getCaller()),
Nan::New<Number>(func.getNum())
)->GetFunction()
);
break;
case SigType::setter:
setter = signature->getCaller();
setterNum = func.getNum();
break;
case SigType::getter:
getter = signature->getCaller();
getterNum = func.getNum();
Nan::SetAccessor(
proto,
Nan::New<String>(stripGetterPrefix(func.getName(), nameBuf)).ToLocalChecked(),
reinterpret_cast<BindClassBase::jsGetter *>(getter),
reinterpret_cast<BindClassBase::jsSetter *>(setter),
Nan::New<Number>((setterNum << accessorSetterShift) | getterNum)
);
break;
case SigType::constructor:
// Impossible! Constructors are not added to the method list.
// They're handled by overloaders for wrappers and values.
break;
}
}
if(nameBuf != nullptr) free(nameBuf);
// Add NBind references to other classes to enforce visibility.
if(bindClass == &BindClass<NBind>::getInstance()) {
nBindConstructor = constructorTemplate->GetFunction();
} else {
Nan::SetTemplate(constructorTemplate, "NBind", nBindConstructor);
}
Local<v8::Function> jsConstructor = constructorTemplate->GetFunction();
// bindClass->setConstructorHandle(jsConstructor);
Overloader::setConstructorJS(bindClass->wrapperConstructorNum, jsConstructor);
exports->Set(
Nan::New<String>(bindClass->getName()).ToLocalChecked(),
jsConstructor
);
}
}
#include "nbind/BindingShort.h"
NBIND_CLASS(NBind) {
construct<>();
method(bind);
}
NODE_MODULE(nbind, nbind::Bindings::initModule)
#endif