@ohos-ports/nbind 0.0.0-stage → 0.3.15-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. package/.editorconfig +7 -0
  2. package/LICENSE +22 -0
  3. package/README.md +1393 -2
  4. package/autogypi.json +11 -0
  5. package/bin/ci-trigger +40 -0
  6. package/bin/copyasm.js +77 -0
  7. package/bin/emcc +21 -0
  8. package/bin/emcc-path.js +7 -0
  9. package/bin/ndts.js +22 -0
  10. package/config.js +7 -0
  11. package/dist/Type.d.ts +92 -0
  12. package/dist/Type.js +180 -0
  13. package/dist/common.d.ts +9 -0
  14. package/dist/common.js +10 -0
  15. package/dist/em-api.js +2353 -0
  16. package/dist/int64.d.ts +9 -0
  17. package/dist/int64.js +64 -0
  18. package/dist/nbind.d.ts +49 -0
  19. package/dist/nbind.js +154 -0
  20. package/dist/reflect.d.ts +69 -0
  21. package/dist/reflect.js +261 -0
  22. package/dist/shim.d.ts +1 -0
  23. package/dist/shim.js +2 -0
  24. package/dist/todts.d.ts +5 -0
  25. package/dist/todts.js +146 -0
  26. package/include/nbind/ArgStorage.h +67 -0
  27. package/include/nbind/BindClass.h +290 -0
  28. package/include/nbind/BindDefiner.h +295 -0
  29. package/include/nbind/Buffer.h +50 -0
  30. package/include/nbind/FunctionDefiner.h +51 -0
  31. package/include/nbind/MethodDef.h +50 -0
  32. package/include/nbind/Policy.h +255 -0
  33. package/include/nbind/TypeID.h +84 -0
  34. package/include/nbind/TypeStd.h +66 -0
  35. package/include/nbind/TypeTransformer.h +38 -0
  36. package/include/nbind/api.h +100 -0
  37. package/include/nbind/common.h +22 -0
  38. package/include/nbind/em/ArgFromWire.h +31 -0
  39. package/include/nbind/em/BindingStd.h +221 -0
  40. package/include/nbind/em/BindingType.h +204 -0
  41. package/include/nbind/em/Buffer.h +37 -0
  42. package/include/nbind/em/Callback.h +186 -0
  43. package/include/nbind/em/Caller.h +61 -0
  44. package/include/nbind/em/Creator.h +30 -0
  45. package/include/nbind/em/External.h +63 -0
  46. package/include/nbind/em/Int64.h +131 -0
  47. package/include/nbind/em/NBind.h +63 -0
  48. package/include/nbind/em/Pool.h +34 -0
  49. package/include/nbind/em/StdFunction.h +40 -0
  50. package/include/nbind/em/ValueObj.h +54 -0
  51. package/include/nbind/nbind.h +19 -0
  52. package/include/nbind/noconflict.h +91 -0
  53. package/include/nbind/signature/BaseSignature.h +238 -0
  54. package/include/nbind/signature/CallbackSignature.h +59 -0
  55. package/include/nbind/signature/ConstructorSignature.h +76 -0
  56. package/include/nbind/signature/FunctionSignature.h +63 -0
  57. package/include/nbind/signature/GetterSignature.h +62 -0
  58. package/include/nbind/signature/MethodSignature.h +61 -0
  59. package/include/nbind/signature/SetterSignature.h +63 -0
  60. package/include/nbind/signature/SignatureParam.h +39 -0
  61. package/include/nbind/v8/ArgFromWire.h +69 -0
  62. package/include/nbind/v8/BindWrapper.h +295 -0
  63. package/include/nbind/v8/BindingStd.h +180 -0
  64. package/include/nbind/v8/BindingType.h +241 -0
  65. package/include/nbind/v8/Buffer.h +59 -0
  66. package/include/nbind/v8/Callback.h +165 -0
  67. package/include/nbind/v8/Caller.h +140 -0
  68. package/include/nbind/v8/Convert.h +80 -0
  69. package/include/nbind/v8/Creator.h +26 -0
  70. package/include/nbind/v8/External.h +119 -0
  71. package/include/nbind/v8/Int64.h +195 -0
  72. package/include/nbind/v8/NBind.h +63 -0
  73. package/include/nbind/v8/Output.h +36 -0
  74. package/include/nbind/v8/Overloader.h +215 -0
  75. package/include/nbind/v8/StdFunction.h +44 -0
  76. package/include/nbind/v8/ValueObj.h +137 -0
  77. package/include/nbind/wire.h +79 -0
  78. package/package.json +69 -4
  79. package/src/checkver.js +31 -0
  80. package/src/common.cc +129 -0
  81. package/src/em/Binding.cc +313 -0
  82. package/src/em/tsconfig.json +22 -0
  83. package/src/nbind-common.gypi +14 -0
  84. package/src/nbind.gypi +98 -0
  85. package/src/post.js +2 -0
  86. package/src/pre.js +48 -0
  87. package/src/reflect.cc +170 -0
  88. package/src/symbols.txt +1 -0
  89. package/src/tsconfig.json +23 -0
  90. package/src/tslint.json +86 -0
  91. package/src/v8/Binding.cc +391 -0
  92. package/src/v8/Buffer.cc +73 -0
package/src/reflect.cc ADDED
@@ -0,0 +1,170 @@
1
+ // This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ #include "nbind/nbind.h"
5
+
6
+ using namespace nbind;
7
+
8
+ typedef BaseSignature :: SignatureType SignatureType;
9
+
10
+ void listMethods(NBindID classType, std::forward_list<MethodDef> &methodList, cbFunction &outMethod) {
11
+ for(auto &func : methodList) {
12
+ const BaseSignature *signature = func.getSignature();
13
+
14
+ if(signature == nullptr) {
15
+ continue;
16
+ }
17
+
18
+ auto rawTypePtr = signature->getTypeList();
19
+ std::vector<NBindID> typeIdList;
20
+
21
+ unsigned int arity = signature->getArity() + 1;
22
+ typeIdList.reserve(arity);
23
+
24
+ while(arity--) {
25
+ typeIdList.push_back(NBindID(*rawTypePtr));
26
+ ++rawTypePtr;
27
+ }
28
+
29
+ const char **rawPolicyPtr = signature->getPolicies();
30
+ std::vector<const char *> policyNameList;
31
+
32
+ while(*rawPolicyPtr) {
33
+ policyNameList.push_back(*rawPolicyPtr);
34
+ ++rawPolicyPtr;
35
+ }
36
+
37
+ outMethod(
38
+ classType,
39
+ func.getName(),
40
+ static_cast<unsigned int>(signature->getType()),
41
+ typeIdList,
42
+ policyNameList
43
+ );
44
+ }
45
+ }
46
+
47
+ void NBind :: reflect(
48
+ cbFunction &outPrimitive,
49
+ cbFunction &outType,
50
+ cbFunction &outClass,
51
+ cbFunction &outSuper,
52
+ cbFunction &outMethod
53
+ ) {
54
+
55
+ const void **primitiveData = getPrimitiveList();
56
+ const uint8_t *sizePtr = static_cast<const uint8_t *>(primitiveData[1]);
57
+ const uint8_t *flagPtr = static_cast<const uint8_t *>(primitiveData[2]);
58
+
59
+ for(const TYPEID *type = static_cast<const TYPEID *>(primitiveData[0]); *type; ++type) {
60
+ outPrimitive(
61
+ NBindID(*type),
62
+ *(sizePtr++),
63
+ *(flagPtr++)
64
+ );
65
+ }
66
+
67
+ for(const void **type = getNamedTypeList(); *type; type += 2) {
68
+ outType(
69
+ NBindID(type[0]),
70
+ static_cast<const char *>(type[1])
71
+ );
72
+ }
73
+
74
+ for(BindClassBase *bindClass : getClassList()) {
75
+ NBindID classType{bindClass->getTypes()[0]};
76
+
77
+ outClass(
78
+ classType,
79
+ bindClass->getName()
80
+ );
81
+ }
82
+
83
+ listMethods(NBindID(nullptr), getFunctionList(), outMethod);
84
+
85
+ for(BindClassBase *bindClass : getClassList()) {
86
+ NBindID classType{bindClass->getTypes()[0]};
87
+ auto superClassList = bindClass->getSuperClassList();
88
+
89
+ if(!superClassList.empty()) {
90
+ std::vector<NBindID> superIdList;
91
+
92
+ for(auto &spec : superClassList) {
93
+ superIdList.push_back(NBindID(spec.superClass.getTypes()[0]));
94
+ }
95
+
96
+ outSuper(classType, superIdList);
97
+ }
98
+
99
+ listMethods(
100
+ classType,
101
+ bindClass->getMethodList(),
102
+ outMethod
103
+ );
104
+ }
105
+ }
106
+
107
+ External NBind :: queryType(
108
+ NBindID id,
109
+ cbFunction &outTypeDetail
110
+ ) {
111
+ const ParamStructure *paramSpec;
112
+ const ArrayStructure *arraySpec;
113
+ const CallbackStructure<1> *callbackSpec;
114
+ External result;
115
+
116
+ StructureType placeholderFlag = id.getStructureType();
117
+ std::vector<NBindID> typeIdList;
118
+ const TYPEID *rawTypePtr;
119
+ unsigned int arity;
120
+
121
+ switch(placeholderFlag) {
122
+ case StructureType :: none:
123
+ result = outTypeDetail.call<External>(
124
+ static_cast<unsigned char>(placeholderFlag)
125
+ );
126
+
127
+ break;
128
+
129
+ case StructureType :: array:
130
+ arraySpec = static_cast<const ArrayStructure *>(id.getStructure());
131
+
132
+ result = outTypeDetail.call<External>(
133
+ static_cast<unsigned char>(placeholderFlag),
134
+ NBindID(arraySpec->member),
135
+ arraySpec->length
136
+ );
137
+
138
+ break;
139
+
140
+ case StructureType :: callback:
141
+ callbackSpec = static_cast<const CallbackStructure<1> *>(id.getStructure());
142
+ rawTypePtr = callbackSpec->argType;
143
+ arity = callbackSpec->arity;
144
+
145
+ while(arity--) {
146
+ typeIdList.push_back(NBindID(*rawTypePtr));
147
+ ++rawTypePtr;
148
+ }
149
+
150
+ result = outTypeDetail.call<External>(
151
+ static_cast<unsigned char>(placeholderFlag),
152
+ NBindID(callbackSpec->returnType),
153
+ typeIdList
154
+ );
155
+
156
+ break;
157
+
158
+ default:
159
+ paramSpec = static_cast<const ParamStructure *>(id.getStructure());
160
+
161
+ result = outTypeDetail.call<External>(
162
+ static_cast<unsigned char>(placeholderFlag),
163
+ NBindID(paramSpec->target)
164
+ );
165
+
166
+ break;
167
+ }
168
+
169
+ return(result);
170
+ }
@@ -0,0 +1 @@
1
+ _nbind_module
@@ -0,0 +1,23 @@
1
+ {
2
+ "compileOnSave": true,
3
+ "compilerOptions": {
4
+ "declaration": true,
5
+ "lib": ["es5", "es2015.collection"],
6
+ "module": "commonjs",
7
+ "moduleResolution": "node",
8
+ "noImplicitAny": true,
9
+ "noImplicitThis": true,
10
+ "outDir": "../dist",
11
+ "removeComments": false,
12
+ "sourceMap": false,
13
+ "strictNullChecks": true,
14
+ "target": "es5"
15
+ },
16
+ "files": [
17
+ "int64.ts",
18
+ "reflect.ts",
19
+ "todts.ts",
20
+ "shim.ts",
21
+ "nbind.ts"
22
+ ]
23
+ }
@@ -0,0 +1,86 @@
1
+ {
2
+ "rules": {
3
+ "adjacent-overload-signatures": true,
4
+ "align": true,
5
+ "ban": false,
6
+ "class-name": true,
7
+ "comment-format": [ true, "check-space" ],
8
+ "curly": false,
9
+ "eofline": true,
10
+ "forin": true,
11
+ "indent": [ true, "tabs" ],
12
+ "interface-name": [ true, "never-prefix" ],
13
+ "jsdoc-format": false,
14
+ "label-position": true,
15
+ "max-line-length": [ true, 96 ],
16
+ "member-access": false,
17
+ "member-ordering": [ true, { "order": [
18
+ "constructor",
19
+
20
+ "private-static-method",
21
+ "public-static-method",
22
+
23
+ "private-instance-method",
24
+ "public-instance-method",
25
+
26
+ "private-instance-field",
27
+ "public-instance-field",
28
+
29
+ "private-static-field",
30
+ "public-static-field"
31
+ ] } ],
32
+ "new-parens": true,
33
+ "no-angle-bracket-type-assertion": true,
34
+ "no-any": false,
35
+ "no-arg": true,
36
+ "no-bitwise": false,
37
+ "no-conditional-assignment": true,
38
+ "no-consecutive-blank-lines": true,
39
+ "no-console": [ true, "log", "warn", "error" ],
40
+ "no-construct": true,
41
+ "no-debugger": false,
42
+ "no-default-export": true,
43
+ "no-duplicate-variable": true,
44
+ "no-empty": true,
45
+ "no-empty-interface": true,
46
+ "no-eval": false,
47
+ "no-import-side-effect": true,
48
+ "no-inferrable-types": true,
49
+ "no-internal-module": true,
50
+ "no-invalid-this": false,
51
+ "no-magic-numbers": false,
52
+ "no-namespace": false,
53
+ "no-non-null-assertion": false,
54
+ "no-null-keyword": false,
55
+ "no-reference": true,
56
+ "no-require-imports": false,
57
+ "no-shadowed-variable": true,
58
+ "no-string-literal": false,
59
+ "no-switch-case-fall-through": true,
60
+ "no-trailing-whitespace": true,
61
+ "no-unused-expression": true,
62
+ "no-use-before-declare": false,
63
+ "no-var-keyword": true,
64
+ "no-var-requires": true,
65
+ "object-literal-sort-keys": true,
66
+ "one-line": [ true, "check-catch", "check-else", "check-finally", "check-open-brace" ],
67
+ "one-variable-per-declaration": true,
68
+ "quotemark": [ true, "single" ],
69
+ "radix": "true",
70
+ "semicolon": [ true, "always" ],
71
+ "switch-default": true,
72
+ "trailing-comma": [ true, {
73
+ "multiline": "never",
74
+ "singleline": "never"
75
+ }],
76
+ "triple-equals": false,
77
+ "typedef": [ true, "parameter", "arrow-parameter", "property-declaration" ],
78
+ "typedef-whitespace": [ true,
79
+ { "call-signature": "nospace", "index-signature": "nospace", "parameter": "nospace", "property-declaration": "nospace", "variable-declaration": "nospace" },
80
+ { "call-signature": "onespace", "index-signature": "onespace", "parameter": "onespace", "property-declaration": "onespace", "variable-declaration": "onespace" }
81
+ ],
82
+ "use-isnan": true,
83
+ "variable-name": [ true, "check-format", "allow-leading-underscore", "allow-pascal-case" ],
84
+ "whitespace": [ true, "check-decl", "check-operator", "check-module", "check-separator", "check-type", "check-typecast" ]
85
+ }
86
+ }
@@ -0,0 +1,391 @@
1
+ // This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ #ifdef BUILDING_NODE_EXTENSION
5
+
6
+ #include <cstring>
7
+ #include <unordered_set>
8
+
9
+ #include "nbind/BindDefiner.h"
10
+
11
+ using namespace v8;
12
+ using namespace nbind;
13
+
14
+ // Convert getter names like "getFoo" into property names like "foo".
15
+ // This could be so much more concisely written with regexps...
16
+ const char *stripGetterPrefix(const char *name, char *&nameBuf) {
17
+ if(
18
+ strlen(name) <= 3 ||
19
+ (name[0] != 'G' && name[0] != 'g') ||
20
+ name[1] != 'e' ||
21
+ name[2] != 't'
22
+ ) return(name);
23
+
24
+ char c = name[3];
25
+
26
+ // "Get_foo", "get_foo" => Remove 4 first characters.
27
+ if(c == '_') return(name + 4);
28
+
29
+ // "Getfoo", "getfoo" => Remove 3 first characters.
30
+ if(c >= 'a' && c <= 'z') return(name + 3);
31
+
32
+ if(c >= 'A' && c <= 'Z') {
33
+ // "GetFOO", "getFOO" => Remove first 3 characters.
34
+ if(name[4] >= 'A' && name[4] <= 'Z') return(name + 3);
35
+ } else return(name);
36
+
37
+ // "GetFoo", "getFoo" => Remove 3 first characters,
38
+ // make a modifiable copy and lowercase first letter.
39
+
40
+ if(nameBuf != nullptr) free(nameBuf);
41
+ nameBuf = strdup(name + 3);
42
+
43
+ if(nameBuf != nullptr) {
44
+ nameBuf[0] = c + ('a' - 'A');
45
+ return(nameBuf);
46
+ }
47
+
48
+ // Memory allocation failed.
49
+ // The world will soon end anyway, so just declare
50
+ // the getter without stripping the "get" prefix.
51
+
52
+ return(name);
53
+ }
54
+
55
+ NBindID :: NBindID(TYPEID id) : id(id), name(nullptr) {}
56
+
57
+ NBindID :: NBindID(const NBindID &other) :
58
+ id(other.id), name(other.name ? strdup(other.name) : nullptr) {}
59
+
60
+ NBindID :: NBindID(NBindID &&other) : id(other.id), name(other.name) {
61
+ other.name = nullptr;
62
+ }
63
+
64
+ NBindID :: ~NBindID() {
65
+ if(name) delete(name);
66
+ name = nullptr;
67
+ }
68
+
69
+ const void *NBindID :: getStructure() const {
70
+ return(structure);
71
+ }
72
+
73
+ StructureType NBindID :: getStructureType() const {
74
+ return(*structureType);
75
+ }
76
+
77
+ const char *NBindID :: toString() {
78
+ if(!name) {
79
+ static const char *alphabet = "0123456789abcdef";
80
+
81
+ char *newName = new char[sizeof(id) * 2 + 1];
82
+ unsigned int pos = sizeof(id) * 2;
83
+ uintptr_t code = reinterpret_cast<uintptr_t>(id);
84
+
85
+ newName[pos] = 0;
86
+
87
+ while(pos--) {
88
+ newName[pos] = alphabet[code & 15];
89
+ code >>= 4;
90
+ }
91
+
92
+ name = newName;
93
+ }
94
+
95
+ return(name);
96
+ }
97
+
98
+ typedef BaseSignature :: SignatureType SignatureType;
99
+
100
+ static void registerMethods(
101
+ BindClassBase &bindClass,
102
+ Local<FunctionTemplate> constructorTemplate,
103
+ bool staticOnly
104
+ ) {
105
+ Local<ObjectTemplate> proto = constructorTemplate->PrototypeTemplate();
106
+ char *nameBuf = nullptr;
107
+
108
+ funcPtr setter = nullptr;
109
+ funcPtr getter = nullptr;
110
+ // unsigned int getterNum = 0; unused for now.
111
+ unsigned int setterNum = 0;
112
+ SignatureParam *param;
113
+
114
+ for(auto &func : bindClass.getMethodList()) {
115
+ // TODO: Support for function overloading goes here.
116
+
117
+ const BaseSignature *signature = func.getSignature();
118
+
119
+ if(signature == nullptr) {
120
+
121
+ /*
122
+ Currently properties must have getters so this is unused.
123
+ if(func.getName() == emptyGetter) {
124
+ getter = nullptr;
125
+ getterNum = 0;
126
+ }
127
+ */
128
+
129
+ if(func.getName() == emptySetter) {
130
+ setter = nullptr;
131
+ setterNum = 0;
132
+ }
133
+
134
+ continue;
135
+ }
136
+
137
+ if(staticOnly && signature->getType() != SignatureType :: func) continue;
138
+
139
+ param = new SignatureParam();
140
+
141
+ switch(signature->getType()) {
142
+ case SignatureType :: none:
143
+
144
+ // Impossible!
145
+ abort();
146
+
147
+ case SignatureType :: method:
148
+ param->methodNum = func.getNum();
149
+ Nan::SetPrototypeTemplate(constructorTemplate, func.getName(),
150
+ Nan::New<FunctionTemplate>(
151
+ reinterpret_cast<BindClassBase::jsMethod *>(signature->getCaller()),
152
+ Nan::New<v8::External>(param)
153
+ )
154
+ );
155
+
156
+ break;
157
+
158
+ case SignatureType :: func:
159
+ param->methodNum = func.getNum();
160
+ Nan::SetTemplate(constructorTemplate, func.getName(),
161
+ Nan::New<FunctionTemplate>(
162
+ reinterpret_cast<BindClassBase::jsMethod *>(signature->getCaller()),
163
+ Nan::New<v8::External>(param)
164
+ )
165
+ );
166
+
167
+ break;
168
+
169
+ case SignatureType :: setter:
170
+ setter = signature->getCaller();
171
+ setterNum = func.getNum();
172
+
173
+ break;
174
+
175
+ case SignatureType :: getter:
176
+ getter = signature->getCaller();
177
+
178
+ param->setterNum = setterNum;
179
+ param->methodNum = func.getNum();
180
+ Nan::SetAccessor(
181
+ proto,
182
+ Nan::New<String>(stripGetterPrefix(func.getName(), nameBuf)).ToLocalChecked(),
183
+ reinterpret_cast<BindClassBase::jsGetter *>(getter),
184
+ reinterpret_cast<BindClassBase::jsSetter *>(setter),
185
+ Nan::New<v8::External>(param)
186
+ );
187
+
188
+ break;
189
+
190
+ case SignatureType :: construct:
191
+
192
+ // Constructors in method list are ignored.
193
+ // They're handled by overloaders for wrappers and values.
194
+
195
+ break;
196
+ }
197
+ }
198
+
199
+ if(nameBuf != nullptr) free(nameBuf);
200
+ }
201
+
202
+ /** Register class members and simulate multiple inheritance. */
203
+
204
+ static void registerSuperMethods(
205
+ BindClassBase &bindClass,
206
+ /** Index of first superclass not natively inherited,
207
+ * or -1 to recursively ignore all members. */
208
+ signed int firstSuper,
209
+ Local<FunctionTemplate> constructorTemplate,
210
+ std::unordered_set<BindClassBase *> &visitTbl
211
+ ) {
212
+ // Stop if this class has already been visited.
213
+ if(visitTbl.find(&bindClass) != visitTbl.end()) return;
214
+
215
+ // Mark this class visited.
216
+ visitTbl.insert(&bindClass);
217
+
218
+ signed int superNum = 0;
219
+ signed int nextFirst;
220
+
221
+ for(auto &spec : bindClass.getSuperClassList()) {
222
+ if(superNum++ < firstSuper || firstSuper < 0) {
223
+ // Non-static contents of the initial first superclass and all of its
224
+ // superclasses have already been inherited through the prototype
225
+ // chain. Mark them visited recursively and inherit static methods.
226
+ nextFirst = -1;
227
+ } else {
228
+ // Complete contents of all other superclasses must be included
229
+ // recursively.
230
+ nextFirst = 0;
231
+ }
232
+
233
+ registerSuperMethods(spec.superClass, nextFirst, constructorTemplate, visitTbl);
234
+ }
235
+
236
+ // Include (possibly only static) contents in class constructor template.
237
+ registerMethods(bindClass, constructorTemplate, firstSuper < 0);
238
+ }
239
+
240
+ static void nop(const Nan::FunctionCallbackInfo<v8::Value> &args) {
241
+ args.GetReturnValue().Set(Nan::Undefined());
242
+ }
243
+
244
+ static void initModule(Local<Object> exports) {
245
+ SignatureParam *param;
246
+
247
+ for(auto &func : getFunctionList()) {
248
+ const BaseSignature *signature = func.getSignature();
249
+
250
+ param = new SignatureParam();
251
+ param->methodNum = func.getNum();
252
+
253
+ Local<FunctionTemplate> functionTemplate = Nan::New<FunctionTemplate>(
254
+ reinterpret_cast<BindClassBase::jsMethod *>(signature->getCaller()),
255
+ Nan::New<v8::External>(param)
256
+ );
257
+
258
+ Local<v8::Function> jsFunction = Nan::GetFunction(functionTemplate).ToLocalChecked();
259
+
260
+ Nan::Set(
261
+ exports,
262
+ Nan::New<String>(func.getName()).ToLocalChecked(),
263
+ jsFunction
264
+ );
265
+ }
266
+
267
+ // Base class for all C++ objects.
268
+
269
+ Local<FunctionTemplate> superTemplate = Nan::New<FunctionTemplate>(nop);
270
+
271
+ // Wrapper for temporary data pointer when passing objects by value.
272
+
273
+ auto storageTemplate = Nan::New<ObjectTemplate>();
274
+ storageTemplate->SetInternalFieldCount(1);
275
+ Nan::SetCallAsFunctionHandler(storageTemplate, Overloader::createValue);
276
+
277
+ // Create all class constructor templates.
278
+
279
+ auto &classList = getClassList();
280
+
281
+ for(auto *bindClass : classList) bindClass->unvisit();
282
+
283
+ auto posPrev = classList.before_begin();
284
+ auto pos = classList.begin();
285
+
286
+ while(pos != classList.end()) {
287
+ auto *bindClass = *pos++;
288
+
289
+ // Avoid registering the same class twice.
290
+ if(bindClass->isVisited()) {
291
+ classList.erase_after(posPrev);
292
+ continue;
293
+ }
294
+
295
+ bindClass->visit();
296
+ ++posPrev;
297
+
298
+ if(bindClass->isReady()) continue;
299
+
300
+ param = new SignatureParam();
301
+ param->overloadNum = bindClass->wrapperConstructorNum;
302
+
303
+ auto constructorTemplate = Nan::New<FunctionTemplate>(
304
+ Overloader::create,
305
+ Nan::New<v8::External>(param)
306
+ );
307
+
308
+ constructorTemplate->SetClassName(Nan::New<String>(bindClass->getName()).ToLocalChecked());
309
+ constructorTemplate->InstanceTemplate()->SetInternalFieldCount(1);
310
+
311
+ bindClass->constructorTemplate.Reset(constructorTemplate);
312
+ bindClass->superTemplate.Reset(superTemplate);
313
+ bindClass->storageTemplate.Reset(storageTemplate);
314
+ }
315
+
316
+ // Add NBind reference to base class to enforce its visibility.
317
+
318
+ Nan::SetTemplate(
319
+ superTemplate,
320
+ "NBind",
321
+ Nan::New(BindClass<NBind>::getInstance().constructorTemplate)
322
+ );
323
+
324
+ // Define inheritance between class constructor templates and add methods.
325
+
326
+ for(auto *bindClass : classList) {
327
+ if(bindClass->isReady()) continue;
328
+
329
+ Local<FunctionTemplate> constructorTemplate = Nan::New(bindClass->constructorTemplate);
330
+
331
+ auto superClassList = bindClass->getSuperClassList();
332
+
333
+ if(!superClassList.empty()) {
334
+ // This fails if the constructor template of any other child class
335
+ // has already been instantiated!
336
+ // TODO: Is this correct? What the superclass inherits may still
337
+ // be undefined. Should this be in registerSuperMethods instead?
338
+ constructorTemplate->Inherit(Nan::New(superClassList.front().superClass.constructorTemplate));
339
+ } else {
340
+ constructorTemplate->Inherit(superTemplate);
341
+ }
342
+
343
+ // Add methods of the class and all its superclasses not inherited
344
+ // through the prototype chain.
345
+
346
+ std::unordered_set<BindClassBase *> visitTbl;
347
+ registerSuperMethods(*bindClass, 1, constructorTemplate, visitTbl);
348
+
349
+ Nan::SetPrototypeTemplate(constructorTemplate, "free",
350
+ Nan::New<FunctionTemplate>(
351
+ bindClass->getDeleter()
352
+ )
353
+ );
354
+ }
355
+
356
+ // Instantiate and export class constructor templates.
357
+
358
+ for(auto *bindClass : classList) {
359
+ // Instantiate the constructor template.
360
+ Local<Function> jsConstructor = Nan::GetFunction(Nan::New(bindClass->constructorTemplate)).ToLocalChecked();
361
+
362
+ Overloader::setConstructorJS(bindClass->wrapperConstructorNum, jsConstructor);
363
+ Overloader::setPtrWrapper(bindClass->wrapperConstructorNum, bindClass->wrapPtr);
364
+
365
+ Nan::Set(
366
+ exports,
367
+ Nan::New<String>(bindClass->getName()).ToLocalChecked(),
368
+ jsConstructor
369
+ );
370
+
371
+ bindClass->setReady();
372
+ }
373
+ }
374
+
375
+ #include "nbind/nbind.h"
376
+
377
+ NBIND_CLASS(NBind) {
378
+ construct<>();
379
+
380
+ method(bind_value);
381
+ method(reflect);
382
+ method(queryType);
383
+ }
384
+
385
+ NBIND_CLASS(NBindID) {
386
+ method(toString);
387
+ }
388
+
389
+ NODE_MODULE(nbind, initModule)
390
+
391
+ #endif