@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.
- package/.editorconfig +7 -0
- package/LICENSE +22 -0
- package/README.md +1393 -2
- package/autogypi.json +11 -0
- package/bin/ci-trigger +40 -0
- package/bin/copyasm.js +77 -0
- package/bin/emcc +21 -0
- package/bin/emcc-path.js +7 -0
- package/bin/ndts.js +22 -0
- package/config.js +7 -0
- package/dist/Type.d.ts +92 -0
- package/dist/Type.js +180 -0
- package/dist/common.d.ts +9 -0
- package/dist/common.js +10 -0
- package/dist/em-api.js +2353 -0
- package/dist/int64.d.ts +9 -0
- package/dist/int64.js +64 -0
- package/dist/nbind.d.ts +49 -0
- package/dist/nbind.js +154 -0
- package/dist/reflect.d.ts +69 -0
- package/dist/reflect.js +261 -0
- package/dist/shim.d.ts +1 -0
- package/dist/shim.js +2 -0
- package/dist/todts.d.ts +5 -0
- package/dist/todts.js +146 -0
- package/include/nbind/ArgStorage.h +67 -0
- package/include/nbind/BindClass.h +290 -0
- package/include/nbind/BindDefiner.h +295 -0
- package/include/nbind/Buffer.h +50 -0
- package/include/nbind/FunctionDefiner.h +51 -0
- package/include/nbind/MethodDef.h +50 -0
- package/include/nbind/Policy.h +255 -0
- package/include/nbind/TypeID.h +84 -0
- package/include/nbind/TypeStd.h +66 -0
- package/include/nbind/TypeTransformer.h +38 -0
- package/include/nbind/api.h +100 -0
- package/include/nbind/common.h +22 -0
- package/include/nbind/em/ArgFromWire.h +31 -0
- package/include/nbind/em/BindingStd.h +221 -0
- package/include/nbind/em/BindingType.h +204 -0
- package/include/nbind/em/Buffer.h +37 -0
- package/include/nbind/em/Callback.h +186 -0
- package/include/nbind/em/Caller.h +61 -0
- package/include/nbind/em/Creator.h +30 -0
- package/include/nbind/em/External.h +63 -0
- package/include/nbind/em/Int64.h +131 -0
- package/include/nbind/em/NBind.h +63 -0
- package/include/nbind/em/Pool.h +34 -0
- package/include/nbind/em/StdFunction.h +40 -0
- package/include/nbind/em/ValueObj.h +54 -0
- package/include/nbind/nbind.h +19 -0
- package/include/nbind/noconflict.h +91 -0
- package/include/nbind/signature/BaseSignature.h +238 -0
- package/include/nbind/signature/CallbackSignature.h +59 -0
- package/include/nbind/signature/ConstructorSignature.h +76 -0
- package/include/nbind/signature/FunctionSignature.h +63 -0
- package/include/nbind/signature/GetterSignature.h +62 -0
- package/include/nbind/signature/MethodSignature.h +61 -0
- package/include/nbind/signature/SetterSignature.h +63 -0
- package/include/nbind/signature/SignatureParam.h +39 -0
- package/include/nbind/v8/ArgFromWire.h +69 -0
- package/include/nbind/v8/BindWrapper.h +295 -0
- package/include/nbind/v8/BindingStd.h +180 -0
- package/include/nbind/v8/BindingType.h +241 -0
- package/include/nbind/v8/Buffer.h +59 -0
- package/include/nbind/v8/Callback.h +165 -0
- package/include/nbind/v8/Caller.h +140 -0
- package/include/nbind/v8/Convert.h +80 -0
- package/include/nbind/v8/Creator.h +26 -0
- package/include/nbind/v8/External.h +119 -0
- package/include/nbind/v8/Int64.h +195 -0
- package/include/nbind/v8/NBind.h +63 -0
- package/include/nbind/v8/Output.h +36 -0
- package/include/nbind/v8/Overloader.h +215 -0
- package/include/nbind/v8/StdFunction.h +44 -0
- package/include/nbind/v8/ValueObj.h +137 -0
- package/include/nbind/wire.h +79 -0
- package/package.json +69 -4
- package/src/checkver.js +31 -0
- package/src/common.cc +129 -0
- package/src/em/Binding.cc +313 -0
- package/src/em/tsconfig.json +22 -0
- package/src/nbind-common.gypi +14 -0
- package/src/nbind.gypi +98 -0
- package/src/post.js +2 -0
- package/src/pre.js +48 -0
- package/src/reflect.cc +170 -0
- package/src/symbols.txt +1 -0
- package/src/tsconfig.json +23 -0
- package/src/tslint.json +86 -0
- package/src/v8/Binding.cc +391 -0
- package/src/v8/Buffer.cc +73 -0
package/dist/todts.js
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
|
|
3
|
+
// Released under the MIT license, see LICENSE.
|
|
4
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
+
// TypeScript alternatives to named C++ types,
|
|
6
|
+
// and a flag whether they need surrounding parentheses when nested.
|
|
7
|
+
var nameTbl = {
|
|
8
|
+
'Buffer': ['number[] | ArrayBuffer | DataView | Uint8Array | Buffer', true],
|
|
9
|
+
'External': ['any', false],
|
|
10
|
+
'Int64': ['number', false],
|
|
11
|
+
'bool': ['boolean', false],
|
|
12
|
+
'cbFunction &': ['(...args: any[]) => any', true],
|
|
13
|
+
'std::string': ['string', false],
|
|
14
|
+
'void': ['void', false]
|
|
15
|
+
};
|
|
16
|
+
// tslint:disable-next-line:typedef
|
|
17
|
+
function formatType(bindType, policyTbl, needParens) {
|
|
18
|
+
if (policyTbl === void 0) { policyTbl = {}; }
|
|
19
|
+
if (needParens === void 0) { needParens = false; }
|
|
20
|
+
var flags = bindType.flags;
|
|
21
|
+
var kind = flags & 15360 /* kindMask */;
|
|
22
|
+
var refKind = flags & 896 /* refMask */;
|
|
23
|
+
// tslint:disable-next-line:typedef
|
|
24
|
+
function formatSubType(needsParens, num) {
|
|
25
|
+
if (num === void 0) { num = 0; }
|
|
26
|
+
return (formatType(bindType.spec.paramList[num], policyTbl, needsParens));
|
|
27
|
+
}
|
|
28
|
+
function addParens(name) {
|
|
29
|
+
return (needParens ? '(' + name + ')' : name);
|
|
30
|
+
}
|
|
31
|
+
if (flags & 1 /* isConst */)
|
|
32
|
+
return (formatSubType(needParens));
|
|
33
|
+
var isNullable = policyTbl['Nullable'];
|
|
34
|
+
var argList = [];
|
|
35
|
+
switch (kind) {
|
|
36
|
+
case 1024 /* isArithmetic */:
|
|
37
|
+
return ('number');
|
|
38
|
+
case 2048 /* isClass */:
|
|
39
|
+
// Objects passed by pointer (not value or reference)
|
|
40
|
+
// may be nullable.
|
|
41
|
+
isNullable = isNullable && (refKind == 128 /* isPointer */ ||
|
|
42
|
+
refKind == 512 /* isSharedPtr */ ||
|
|
43
|
+
refKind == 640 /* isUniquePtr */);
|
|
44
|
+
needParens = needParens && isNullable;
|
|
45
|
+
return (addParens((refKind ? formatSubType(isNullable) : bindType.name) +
|
|
46
|
+
(isNullable ? ' | null' : '')));
|
|
47
|
+
case 5120 /* isVector */:
|
|
48
|
+
case 6144 /* isArray */:
|
|
49
|
+
return (addParens(formatSubType(true) + '[]'));
|
|
50
|
+
case 7168 /* isCString */:
|
|
51
|
+
return (isNullable ? addParens('string | null') : 'string');
|
|
52
|
+
case 8192 /* isString */:
|
|
53
|
+
return ('string');
|
|
54
|
+
case 9216 /* isCallback */:
|
|
55
|
+
for (var num = 1; num < bindType.spec.paramList.length; ++num) {
|
|
56
|
+
argList.push('p' + (num - 1) + ': ' + formatSubType(false, num));
|
|
57
|
+
}
|
|
58
|
+
return (addParens('(' + argList.join(', ') + ') => ' + formatSubType(true)));
|
|
59
|
+
case 10240 /* isOther */:
|
|
60
|
+
var spec = nameTbl[bindType.name];
|
|
61
|
+
return (spec ? (spec[1] ? addParens(spec[0]) : spec[0]) : 'any');
|
|
62
|
+
default:
|
|
63
|
+
return ('any');
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
function formatMethod(method) {
|
|
67
|
+
var policyTbl = {};
|
|
68
|
+
for (var _i = 0, _a = method.policyList; _i < _a.length; _i++) {
|
|
69
|
+
var key = _a[_i];
|
|
70
|
+
policyTbl[key] = true;
|
|
71
|
+
}
|
|
72
|
+
var args = ('(' + method.argTypeList.map(function (bindType, num) { return 'p' + num + ': ' + formatType(bindType, policyTbl); }).join(', ') + ')');
|
|
73
|
+
if (method.name) {
|
|
74
|
+
// Most return types may be null.
|
|
75
|
+
return (method.name + args + ': ' + formatType(method.returnType, { 'Nullable': true }) + ';');
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
return ('constructor' + args + ';');
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
function formatProperty(prop) {
|
|
82
|
+
return (prop.name + ': ' + formatType(prop.bindType) + ';');
|
|
83
|
+
}
|
|
84
|
+
function dump(options) {
|
|
85
|
+
var classCodeList = [];
|
|
86
|
+
var indent;
|
|
87
|
+
var staticPrefixCC;
|
|
88
|
+
var staticPrefixJS;
|
|
89
|
+
var classList = options.reflect.classList;
|
|
90
|
+
if (options.shim) {
|
|
91
|
+
classCodeList.push('import { Buffer } from "nbind/dist/shim";');
|
|
92
|
+
}
|
|
93
|
+
classCodeList.push('export class NBindBase { free?(): void }');
|
|
94
|
+
if (options.reflect.globalScope) {
|
|
95
|
+
classList = classList.concat([options.reflect.globalScope]);
|
|
96
|
+
}
|
|
97
|
+
for (var _i = 0, classList_1 = classList; _i < classList_1.length; _i++) {
|
|
98
|
+
var bindClass = classList_1[_i];
|
|
99
|
+
if (bindClass.isClass) {
|
|
100
|
+
indent = '\t';
|
|
101
|
+
staticPrefixCC = 'static ';
|
|
102
|
+
staticPrefixJS = 'static ';
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
indent = '';
|
|
106
|
+
staticPrefixCC = '';
|
|
107
|
+
staticPrefixJS = 'export function ';
|
|
108
|
+
}
|
|
109
|
+
var methodBlock = bindClass.methodList.map(function (method) { return (indent +
|
|
110
|
+
'/** ' + (method.name && method.isStatic ? staticPrefixCC : '') +
|
|
111
|
+
method + ';' + (method.policyList.length ?
|
|
112
|
+
' -- ' + method.policyList.join(', ') :
|
|
113
|
+
'') +
|
|
114
|
+
' */\n' +
|
|
115
|
+
indent + (method.name && method.isStatic ? staticPrefixJS : '') +
|
|
116
|
+
formatMethod(method)); }).join('\n\n');
|
|
117
|
+
var propertyBlock = bindClass.propertyList.map(function (property) { return (indent +
|
|
118
|
+
'/** ' + property + ';' +
|
|
119
|
+
(!(property.isReadable && property.isWritable) ?
|
|
120
|
+
' -- ' + (property.isReadable ? 'Read-only' : 'Write-only') :
|
|
121
|
+
'') +
|
|
122
|
+
' */\n' +
|
|
123
|
+
indent +
|
|
124
|
+
formatProperty(property)); }).join('\n\n');
|
|
125
|
+
var classCode = (methodBlock +
|
|
126
|
+
(methodBlock && propertyBlock ? '\n\n' : '') +
|
|
127
|
+
propertyBlock);
|
|
128
|
+
var superClass = 'NBindBase';
|
|
129
|
+
if (bindClass.superList.length == 1) {
|
|
130
|
+
superClass = bindClass.superList[0].name;
|
|
131
|
+
}
|
|
132
|
+
else if (bindClass.superList.length > 1) {
|
|
133
|
+
superClass = '_' + bindClass.name;
|
|
134
|
+
classCodeList.push('export interface ' + superClass + ' extends ' + bindClass.superList.map(function (superClassEntry) { return superClassEntry.name; }).join(', ') + ' {}\n' +
|
|
135
|
+
'export var ' + superClass + ': { new(): ' + superClass + ' };');
|
|
136
|
+
}
|
|
137
|
+
if (indent) {
|
|
138
|
+
classCode = ('export class ' + bindClass.name + ' extends ' + superClass + ' {' +
|
|
139
|
+
(classCode ? '\n' + classCode + '\n' : '') +
|
|
140
|
+
'}');
|
|
141
|
+
}
|
|
142
|
+
classCodeList.push(classCode);
|
|
143
|
+
}
|
|
144
|
+
return (classCodeList.join('\n\n') + '\n');
|
|
145
|
+
}
|
|
146
|
+
exports.dump = dump;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
// This file is part of nbind, copyright (C) 2014-2015 BusFaster Ltd.
|
|
2
|
+
// Released under the MIT license, see LICENSE.
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
namespace nbind {
|
|
7
|
+
|
|
8
|
+
// Wrapper for C++ objects converted from corresponding JavaScript objects.
|
|
9
|
+
// Needed for allocating an empty placeholder object before JavaScript calls
|
|
10
|
+
// its constructor. See:
|
|
11
|
+
// http://stackoverflow.com/questions/31091223/placement-new-return-by-value-and-safely-dispose-temporary-copies
|
|
12
|
+
|
|
13
|
+
class ArgStorage {
|
|
14
|
+
|
|
15
|
+
public:
|
|
16
|
+
|
|
17
|
+
ArgStorage(unsigned int overloadNum) :
|
|
18
|
+
overloadNum(overloadNum) {}
|
|
19
|
+
|
|
20
|
+
unsigned int getOverloadNum() const { return(overloadNum); }
|
|
21
|
+
|
|
22
|
+
private:
|
|
23
|
+
|
|
24
|
+
const unsigned int overloadNum;
|
|
25
|
+
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
template <typename Bound>
|
|
29
|
+
class TemplatedArgStorage : public ArgStorage {
|
|
30
|
+
|
|
31
|
+
public:
|
|
32
|
+
|
|
33
|
+
TemplatedArgStorage(unsigned int overloadNum) :
|
|
34
|
+
ArgStorage(overloadNum), dummy(0) {}
|
|
35
|
+
|
|
36
|
+
~TemplatedArgStorage() {
|
|
37
|
+
if(isDataValid) data.~Bound();
|
|
38
|
+
isDataValid = false;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
template <typename... Args>
|
|
42
|
+
void init(Args&&... args) {
|
|
43
|
+
::new(&data) Bound(std::forward<Args>(args)...);
|
|
44
|
+
isDataValid = true;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
Bound getBound() {
|
|
48
|
+
return(std::move(data));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
private:
|
|
52
|
+
|
|
53
|
+
// Union with a dummy value allows initializing the class
|
|
54
|
+
// without constructing the data member object.
|
|
55
|
+
|
|
56
|
+
union {
|
|
57
|
+
int dummy;
|
|
58
|
+
Bound data;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Flag tells if the union no longer holds a dummy value.
|
|
62
|
+
|
|
63
|
+
bool isDataValid = false;
|
|
64
|
+
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
} // namespace
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
// This file is part of nbind, copyright (C) 2014-2015 BusFaster Ltd.
|
|
2
|
+
// Released under the MIT license, see LICENSE.
|
|
3
|
+
|
|
4
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
namespace nbind {
|
|
7
|
+
|
|
8
|
+
class BindClassBase;
|
|
9
|
+
|
|
10
|
+
struct SuperClassSpec {
|
|
11
|
+
explicit SuperClassSpec(
|
|
12
|
+
BindClassBase &superClass,
|
|
13
|
+
void *(&upcast)(void *)
|
|
14
|
+
) : superClass(superClass), upcast(upcast) {}
|
|
15
|
+
|
|
16
|
+
BindClassBase &superClass;
|
|
17
|
+
void *(&upcast)(void *);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
// Templated singleton class for each C++ class accessible from Node.js.
|
|
21
|
+
// Stores their information defined in static constructors, until the Node.js
|
|
22
|
+
// plugin is initialized.
|
|
23
|
+
|
|
24
|
+
class BindClassBase {
|
|
25
|
+
|
|
26
|
+
public:
|
|
27
|
+
|
|
28
|
+
// Get type of method definitions to use in function pointers.
|
|
29
|
+
|
|
30
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
31
|
+
|
|
32
|
+
typedef std::remove_pointer<Nan::FunctionCallback>::type jsMethod;
|
|
33
|
+
typedef std::remove_pointer<Nan::GetterCallback>::type jsGetter;
|
|
34
|
+
typedef std::remove_pointer<Nan::SetterCallback>::type jsSetter;
|
|
35
|
+
|
|
36
|
+
#else
|
|
37
|
+
|
|
38
|
+
typedef void (jsMethod)();
|
|
39
|
+
typedef void (jsGetter)();
|
|
40
|
+
typedef void (jsSetter)();
|
|
41
|
+
|
|
42
|
+
#endif // BUILDING_NODE_EXTENSION
|
|
43
|
+
|
|
44
|
+
const TYPEID *getTypes() const { return(idList); }
|
|
45
|
+
|
|
46
|
+
const char **getPolicies() const { return(policyNameList); }
|
|
47
|
+
|
|
48
|
+
const char *getName() const { return(name); }
|
|
49
|
+
|
|
50
|
+
std::forward_list<SuperClassSpec> &getSuperClassList() {
|
|
51
|
+
return(superClassList);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
unsigned int getSuperClassCount() { return(superClassCount); }
|
|
55
|
+
|
|
56
|
+
void addConstructor(BaseSignature *signature) {
|
|
57
|
+
|
|
58
|
+
# if defined(BUILDING_NODE_EXTENSION)
|
|
59
|
+
|
|
60
|
+
Overloader::addMethod(
|
|
61
|
+
wrapperConstructorNum,
|
|
62
|
+
signature->getArity(),
|
|
63
|
+
signature->getCaller()
|
|
64
|
+
);
|
|
65
|
+
|
|
66
|
+
Overloader::addMethod(
|
|
67
|
+
valueConstructorNum,
|
|
68
|
+
signature->getArity(),
|
|
69
|
+
signature->getValueConstructor()
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
# endif // BUILDING_NODE_EXTENSION
|
|
73
|
+
|
|
74
|
+
addMethod(nullptr, signature);
|
|
75
|
+
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Functions called when defining an nbind wrapper for a C++ class.
|
|
79
|
+
// They are called from the constructor of a static variable,
|
|
80
|
+
// meaning they run before any other code when the compiled library is loaded.
|
|
81
|
+
// They add pointers to the wrapped C++ functions, which will be bound to
|
|
82
|
+
// JavaScript prototype objects by the library's initModule function.
|
|
83
|
+
|
|
84
|
+
// Add a method to the class.
|
|
85
|
+
|
|
86
|
+
void addMethod(
|
|
87
|
+
const char *name,
|
|
88
|
+
BaseSignature *signature = nullptr,
|
|
89
|
+
funcPtr ptr = nullptr,
|
|
90
|
+
unsigned int num = 0,
|
|
91
|
+
TypeFlags flags = TypeFlags::none
|
|
92
|
+
) {
|
|
93
|
+
methodList.emplace_after(methodLast, name, ptr, num, signature, flags);
|
|
94
|
+
++methodLast;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
std::forward_list<MethodDef> &getMethodList() { return(methodList); }
|
|
98
|
+
|
|
99
|
+
jsMethod *getDeleter() const { return(deleter); }
|
|
100
|
+
|
|
101
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
102
|
+
|
|
103
|
+
unsigned int wrapperConstructorNum = Overloader::addGroup();
|
|
104
|
+
unsigned int valueConstructorNum = Overloader::addGroup();
|
|
105
|
+
|
|
106
|
+
/** Handler to wrap object pointers instantiated in C++ for use in JavaScript. */
|
|
107
|
+
jsMethod *wrapPtr;
|
|
108
|
+
|
|
109
|
+
Nan::Persistent<v8::FunctionTemplate> constructorTemplate;
|
|
110
|
+
Nan::Persistent<v8::FunctionTemplate> superTemplate;
|
|
111
|
+
Nan::Persistent<v8::ObjectTemplate> storageTemplate;
|
|
112
|
+
|
|
113
|
+
#endif // BUILDING_NODE_EXTENSION
|
|
114
|
+
|
|
115
|
+
// A JavaScript "value constructor" creates a JavaScript object with
|
|
116
|
+
// the same data as the equivalent C++ object. This is used for small
|
|
117
|
+
// objects that should work like primitives, such as coordinate pairs.
|
|
118
|
+
|
|
119
|
+
// Calling C++ accessor methods would be much slower than reading
|
|
120
|
+
// directly from a JavaScript object. Also, using a JIT-compiled
|
|
121
|
+
// constructor function directly created an object with compact and
|
|
122
|
+
// fast, in-object properties:
|
|
123
|
+
// http://jayconrod.com/posts/52/a-tour-of-v8-object-representation
|
|
124
|
+
|
|
125
|
+
void setValueConstructorJS(cbFunction &func) {
|
|
126
|
+
if(valueConstructorJS != nullptr) delete(valueConstructorJS);
|
|
127
|
+
valueConstructorJS = new cbFunction(func);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
cbFunction *getValueConstructorJS() const { return(valueConstructorJS); }
|
|
131
|
+
|
|
132
|
+
void *upcastStep(BindClassBase &dst, void *ptr) {
|
|
133
|
+
if(&dst == this) return(ptr);
|
|
134
|
+
|
|
135
|
+
for(auto &spec : superClassList) {
|
|
136
|
+
void *superPtr = spec.superClass.upcastStep(dst, spec.upcast(ptr));
|
|
137
|
+
if(superPtr) return(superPtr);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return(nullptr);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
bool isVisited() { return(visited); }
|
|
144
|
+
void visit() { visited = true; }
|
|
145
|
+
void unvisit() { visited = false; }
|
|
146
|
+
|
|
147
|
+
bool isReady() { return(ready); }
|
|
148
|
+
void setReady() { ready = true; }
|
|
149
|
+
|
|
150
|
+
protected:
|
|
151
|
+
|
|
152
|
+
TYPEID idList[2];
|
|
153
|
+
|
|
154
|
+
const char **policyNameList;
|
|
155
|
+
|
|
156
|
+
const char *name;
|
|
157
|
+
|
|
158
|
+
std::forward_list<SuperClassSpec> superClassList;
|
|
159
|
+
unsigned int superClassCount = 0;
|
|
160
|
+
|
|
161
|
+
std::forward_list<MethodDef> methodList;
|
|
162
|
+
decltype(methodList.before_begin()) methodLast = methodList.before_begin();
|
|
163
|
+
|
|
164
|
+
jsMethod *deleter;
|
|
165
|
+
|
|
166
|
+
// This has to be a pointer instead of a member object so the
|
|
167
|
+
// destructor won't get called. Otherwise NanCallback's destructor
|
|
168
|
+
// segfaults when freeing V8 resources because the surrounding
|
|
169
|
+
// object gets destroyed after the V8 engine.
|
|
170
|
+
|
|
171
|
+
// Suitable JavaScript constructor called by a toJS C++ function
|
|
172
|
+
// when converting this object into a plain JavaScript object,
|
|
173
|
+
// if possible.
|
|
174
|
+
|
|
175
|
+
cbFunction *valueConstructorJS = nullptr;
|
|
176
|
+
|
|
177
|
+
bool visited = false;
|
|
178
|
+
bool ready = false;
|
|
179
|
+
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
// Templated singleton class used for storing the definitions of a single
|
|
183
|
+
// wrapped C++ class.
|
|
184
|
+
|
|
185
|
+
template <class Bound> class BindClass : public BindClassBase {
|
|
186
|
+
|
|
187
|
+
public:
|
|
188
|
+
|
|
189
|
+
void init(const char *name) {
|
|
190
|
+
idList[0] = Typer<Bound>::makeID();
|
|
191
|
+
idList[1] = Typer<Bound *>::makeID();
|
|
192
|
+
|
|
193
|
+
this->name = name;
|
|
194
|
+
this->policyNameList = DetectPolicies<Bound>::getPolicies();
|
|
195
|
+
this->deleter = reinterpret_cast<jsMethod *>(&BindClass::destroy);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// Making the instance a direct class member might fail on some platforms.
|
|
199
|
+
|
|
200
|
+
static BindClass &getInstance() {
|
|
201
|
+
// Linkage for a singleton instance of each templated class.
|
|
202
|
+
static BindClass instance;
|
|
203
|
+
|
|
204
|
+
return(instance);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
template <class SuperType>
|
|
208
|
+
inline void addSuperClass();
|
|
209
|
+
|
|
210
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
211
|
+
|
|
212
|
+
static void destroy(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
213
|
+
v8::Local<v8::Object> targetWrapped = args.This();
|
|
214
|
+
auto wrapper = node::ObjectWrap::Unwrap<BindWrapper<Bound>>(targetWrapped);
|
|
215
|
+
|
|
216
|
+
wrapper->destroy();
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
#elif defined(__EMSCRIPTEN__)
|
|
220
|
+
|
|
221
|
+
static void destroy(uint32_t, void *ptr, void *shared, TypeFlags flags) {
|
|
222
|
+
if((flags & TypeFlags::refMask) == TypeFlags::isSharedPtr) {
|
|
223
|
+
delete(static_cast<std::shared_ptr<Bound> *>(shared));
|
|
224
|
+
} else {
|
|
225
|
+
delete(static_cast<Bound *>(ptr));
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#endif
|
|
230
|
+
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
234
|
+
|
|
235
|
+
template <class Bound>
|
|
236
|
+
BindClassBase &BindWrapper<Bound> :: getBindClass() {
|
|
237
|
+
return(BindClass<Bound>::getInstance());
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Test that a JavaScript object is an instance of a class from this addon
|
|
241
|
+
* and has some data attached (hopefully a wrapped C++ object). */
|
|
242
|
+
|
|
243
|
+
template <class Bound>
|
|
244
|
+
void BindWrapper<Bound> :: testInstance(v8::Local<v8::Object> arg) {
|
|
245
|
+
BindClassBase &bindClass = getBindClass();
|
|
246
|
+
|
|
247
|
+
if(bindClass.superTemplate.IsEmpty()) {
|
|
248
|
+
throw(std::runtime_error("Unbound type"));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
if(
|
|
252
|
+
!Nan::New(bindClass.superTemplate)->HasInstance(arg) ||
|
|
253
|
+
arg->InternalFieldCount() != 1
|
|
254
|
+
) {
|
|
255
|
+
throw(std::runtime_error("Type mismatch"));
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
template <class Bound>
|
|
260
|
+
Bound *BindWrapperBase :: upcast() {
|
|
261
|
+
BindClassBase &src = getClass();
|
|
262
|
+
BindClassBase &dst = BindClass<Bound>::getInstance();
|
|
263
|
+
|
|
264
|
+
if(&src == &dst) return(static_cast<Bound *>(boundUnsafe));
|
|
265
|
+
|
|
266
|
+
return(static_cast<Bound *>(src.upcastStep(dst, boundUnsafe)));
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
#endif
|
|
270
|
+
|
|
271
|
+
template <class Bound>
|
|
272
|
+
cbFunction *getValueConstructorJS() {
|
|
273
|
+
return(BindClass<Bound>::getInstance().getValueConstructorJS());
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
template <class Bound, class SuperType>
|
|
277
|
+
void *upcast(void *arg) {
|
|
278
|
+
return(static_cast<SuperType *>(static_cast<Bound *>(arg)));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
template <class Bound> template <class SuperType>
|
|
282
|
+
inline void BindClass<Bound> :: addSuperClass() {
|
|
283
|
+
superClassList.emplace_front(
|
|
284
|
+
BindClass<SuperType>::getInstance(),
|
|
285
|
+
upcast<Bound, SuperType>
|
|
286
|
+
);
|
|
287
|
+
++superClassCount;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
} // namespace
|