@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
@@ -0,0 +1,180 @@
1
+ // This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ // This file handles type conversion of C++ standard library types
5
+ // to / from JavaScript.
6
+
7
+ #pragma once
8
+
9
+ #include <string>
10
+ #include <vector>
11
+ #include <array>
12
+
13
+ namespace nbind {
14
+
15
+ // Array.
16
+
17
+ template <typename ArgType, size_t size>
18
+ struct BindingType<std::array<ArgType, size>> {
19
+
20
+ typedef std::array<ArgType, size> Type;
21
+
22
+ static inline bool checkType(WireType arg) {
23
+ if(!arg->IsArray()) return(false);
24
+
25
+ v8::Local<v8::Array> arr = arg.template As<v8::Array>();
26
+
27
+ return(arr->Length() >= size);
28
+ }
29
+
30
+ static inline Type fromWireType(WireType arg) {
31
+ // TODO: Don't convert sparse arrays.
32
+
33
+ v8::Local<v8::Array> arr = arg.template As<v8::Array>();
34
+
35
+ Type val;
36
+
37
+ // Length of arr is checked in checkType().
38
+ for(uint32_t num = 0; num < size; ++num) {
39
+ v8::Local<v8::Value> item;
40
+
41
+ if(
42
+ Nan::Get(arr, num).ToLocal(&item) &&
43
+ BindingType<ArgType>::checkType(item)
44
+ ) {
45
+ val[num] = convertFromWire<ArgType>(item);
46
+ } else {
47
+ throw(std::runtime_error("Error converting array element"));
48
+ }
49
+ }
50
+
51
+ return(val);
52
+ }
53
+
54
+ static inline WireType toWireType(Type &&arg) {
55
+ v8::Local<v8::Array> arr = Nan::New<v8::Array>(size);
56
+
57
+ for(uint32_t num = 0; num < size; ++num) {
58
+ Nan::Set(arr, num, convertToWire(std::forward<ArgType>(arg[num])));
59
+ }
60
+
61
+ return(arr);
62
+ }
63
+
64
+ };
65
+
66
+ // Vector.
67
+
68
+ template <typename ArgType>
69
+ struct BindingType<std::vector<ArgType>> {
70
+
71
+ typedef std::vector<ArgType> Type;
72
+
73
+ static inline bool checkType(WireType arg) {
74
+ return(arg->IsArray());
75
+ }
76
+
77
+ static inline Type fromWireType(WireType arg) {
78
+ // TODO: Don't convert sparse arrays.
79
+
80
+ v8::Local<v8::Array> arr = arg.template As<v8::Array>();
81
+ uint32_t count = arr->Length();
82
+
83
+ // We know the length, so it's faster to preallocate the vector.
84
+
85
+ Type val;
86
+ val.reserve(count);
87
+
88
+ for(uint32_t num = 0; num < count; ++num) {
89
+ v8::Local<v8::Value> item;
90
+
91
+ if(
92
+ Nan::Get(arr, num).ToLocal(&item) &&
93
+ BindingType<ArgType>::checkType(item)
94
+ ) {
95
+ val.push_back(convertFromWire<ArgType>(item));
96
+ } else {
97
+ throw(std::runtime_error("Error converting array element"));
98
+ }
99
+ }
100
+
101
+ return(val);
102
+ }
103
+
104
+ static inline WireType toWireType(Type &&arg) {
105
+ uint32_t count = arg.size();
106
+ v8::Local<v8::Array> arr = Nan::New<v8::Array>(count);
107
+
108
+ for(uint32_t num = 0; num < count; ++num) {
109
+ Nan::Set(arr, num, convertToWire(std::forward<ArgType>(arg[num])));
110
+ }
111
+
112
+ return(arr);
113
+ }
114
+
115
+ };
116
+
117
+ // String.
118
+
119
+ template <> struct BindingType<std::string> {
120
+
121
+ typedef std::string Type;
122
+
123
+ static inline bool checkType(WireType arg) {
124
+ return(true);
125
+ }
126
+
127
+ static inline Type fromWireType(WireType arg) {
128
+ Nan::Utf8String val(arg);
129
+ return(std::string(*val, val.length()));
130
+ }
131
+
132
+ static inline WireType toWireType(Type arg) {
133
+ return(Nan::New<v8::String>(arg.c_str(), arg.length()).ToLocalChecked());
134
+ }
135
+
136
+ };
137
+
138
+ template <> struct BindingType<StrictType<std::string>> : public BindingType<std::string> {
139
+ static inline bool checkType(WireType arg) {
140
+ return(arg->IsString());
141
+ }
142
+ };
143
+
144
+ // String reference.
145
+
146
+ template <> struct BindingType<const std::string &> {
147
+
148
+ typedef std::string Type;
149
+
150
+ static inline bool checkType(WireType arg) {
151
+ return(true);
152
+ }
153
+
154
+ };
155
+
156
+ template <> struct BindingType<StrictType<const std::string &>> : public BindingType<const std::string &> {
157
+ static inline bool checkType(WireType arg) {
158
+ return(arg->IsString());
159
+ }
160
+ };
161
+
162
+ template<typename PolicyList, size_t Index>
163
+ struct ArgFromWire<PolicyList, Index, const std::string &> {
164
+
165
+ // TODO: Get string length from JS to support zero bytes?
166
+ template <typename NanArgs>
167
+ ArgFromWire(const NanArgs &args) : val(*Nan::Utf8String(args[Index])) {}
168
+
169
+ template <typename NanArgs>
170
+ inline const std::string &get(const NanArgs &args) {
171
+ return(val);
172
+ }
173
+
174
+ // RAII style storage for the string data.
175
+
176
+ std::string val;
177
+
178
+ };
179
+
180
+ } // namespace
@@ -0,0 +1,241 @@
1
+ // This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ // This file handles conversion of C++ primitive types to / from JavaScript
5
+ // types in V8 internal representation. Following emscripten conventions,
6
+ // the type passed between the two is called WireType.
7
+ // Anything from the standard library is instead in BindingStd.h
8
+
9
+ #pragma once
10
+
11
+ namespace nbind {
12
+
13
+ typedef v8::Local<v8::Value> WireType;
14
+
15
+ // Generic C++ object.
16
+
17
+ template <typename ArgType> struct BindingType {
18
+
19
+ typedef ArgType Type;
20
+
21
+ static inline bool checkType(WireType arg) {
22
+ return(BindingType<ArgType *>::checkType(arg));
23
+ }
24
+
25
+ static inline Type fromWireType(WireType arg) {
26
+ return(*BindingType<ArgType *>::fromWireType(arg));
27
+ }
28
+
29
+ static inline WireType toWireType(ArgType &&arg) {
30
+ return(BindingType<std::shared_ptr<ArgType>>::toWireType(
31
+ // Move construct from stack to heap.
32
+ std::make_shared<ArgType>(std::move(arg))
33
+ ));
34
+ }
35
+
36
+ };
37
+
38
+ // Object pointer.
39
+
40
+ template <typename ArgType>
41
+ struct BindingType<ArgType *> {
42
+
43
+ typedef ArgType *Type;
44
+ typedef typename std::remove_const<ArgType>::type BaseType;
45
+
46
+ static inline bool checkType(WireType arg) {
47
+ return(arg->IsObject());
48
+ }
49
+
50
+ static inline Type fromWireType(WireType arg) {
51
+ return(BindWrapper<BaseType>::getBound(
52
+ Nan::To<v8::Object>(arg).ToLocalChecked(),
53
+ std::is_const<ArgType>::value ?
54
+ TypeFlags::isConst :
55
+ TypeFlags::none
56
+ ));
57
+ }
58
+
59
+ static inline WireType toWireType(Type arg);
60
+
61
+ };
62
+
63
+ // Object reference.
64
+
65
+ template <typename ArgType>
66
+ struct BindingType<ArgType &> {
67
+
68
+ typedef ArgType &Type;
69
+
70
+ static inline bool checkType(WireType arg) {
71
+ return(arg->IsObject());
72
+ }
73
+
74
+ static inline Type fromWireType(WireType arg) {
75
+ return(*BindingType<ArgType *>::fromWireType(arg));
76
+ }
77
+
78
+ static inline WireType toWireType(Type arg) {
79
+ return(BindingType<ArgType *>::toWireType(&arg));
80
+ }
81
+
82
+ };
83
+
84
+ template <typename ArgType>
85
+ struct BindingType<std::shared_ptr<ArgType>> {
86
+
87
+ typedef std::shared_ptr<ArgType> Type;
88
+ typedef typename std::remove_const<ArgType>::type BaseType;
89
+
90
+ static inline bool checkType(WireType arg) {
91
+ return(arg->IsObject());
92
+ }
93
+
94
+ static inline Type fromWireType(WireType arg) {
95
+ return(BindWrapper<BaseType>::getShared(
96
+ Nan::To<v8::Object>(arg).ToLocalChecked(),
97
+ std::is_const<ArgType>::value ?
98
+ TypeFlags::isConst :
99
+ TypeFlags::none
100
+ ));
101
+ }
102
+
103
+ static inline WireType toWireType(Type &&arg);
104
+
105
+ };
106
+
107
+ template <typename ArgType>
108
+ struct BindingType<std::unique_ptr<ArgType>> {
109
+
110
+ typedef std::unique_ptr<ArgType> Type;
111
+ typedef typename std::remove_const<ArgType>::type BaseType;
112
+
113
+ // checkType and fromWireType not supported! C++ objects are wrapped in
114
+ // shared_ptr which would become invalid if passed back as unique_ptr.
115
+
116
+ static inline WireType toWireType(Type &&arg);
117
+
118
+ // Alternative toWireType definition:
119
+ // return(BindingType<std::shared_ptr<ArgType>>::toWireType(
120
+ // std::move(std::shared_ptr<ArgType>(std::move(arg)))
121
+ // ));
122
+
123
+ };
124
+
125
+ template <typename ArgType>
126
+ struct BindingType<NullableType<ArgType>> {
127
+
128
+ typedef typename BindingType<ArgType>::Type Type;
129
+
130
+ static inline bool checkType(WireType arg) {
131
+ return(arg->IsNull() || arg->IsUndefined() || BindingType<ArgType>::checkType(arg));
132
+ }
133
+
134
+ static inline Type fromWireType(WireType arg) {
135
+ if(arg->IsNull() || arg->IsUndefined()) return(nullptr);
136
+ return(BindingType<ArgType>::fromWireType(arg));
137
+ }
138
+
139
+ static inline WireType toWireType(Type arg) {
140
+ if(arg == nullptr) return(Nan::Null());
141
+ return(BindingType<ArgType>::toWireType(std::forward<Type>(arg)));
142
+ }
143
+
144
+ };
145
+
146
+ template <typename ArgType>
147
+ struct BindingType<ValueType<ArgType>> {
148
+
149
+ typedef ArgType Type;
150
+
151
+ static inline bool checkType(WireType arg) {
152
+ return(BindingType<ArgType *>::checkType(arg));
153
+ }
154
+
155
+ static inline Type fromWireType(WireType arg) noexcept(false);
156
+
157
+ static inline WireType toWireType(Type &&arg);
158
+
159
+ };
160
+
161
+ // Numeric and boolean types.
162
+ // The static cast silences a compiler warning in Visual Studio.
163
+ // NOTE: third macro parameter (decode) is now a Nan::To target type
164
+ // instead of the removed context-less V8 value getter method.
165
+
166
+ #define DEFINE_NATIVE_BINDING_TYPE(ArgType, checker, decode, jsClass) \
167
+ template <> struct BindingType<ArgType> { \
168
+ typedef ArgType Type; \
169
+ \
170
+ static inline bool checkType(WireType arg) { \
171
+ return(true); \
172
+ } \
173
+ \
174
+ static inline Type fromWireType(WireType arg) { \
175
+ return(static_cast<Type>(Nan::To<decode>(arg).FromJust())); \
176
+ } \
177
+ \
178
+ static inline WireType toWireType(Type arg) { \
179
+ return(Nan::New<jsClass>(arg)); \
180
+ } \
181
+ }; \
182
+ \
183
+ template <> struct BindingType<StrictType<ArgType>> : public BindingType<ArgType> { \
184
+ static inline bool checkType(WireType arg) { \
185
+ return(arg->checker()); \
186
+ } \
187
+ }
188
+
189
+ DEFINE_NATIVE_BINDING_TYPE(bool, IsBoolean, bool, v8::Boolean);
190
+
191
+ DEFINE_NATIVE_BINDING_TYPE(double, IsNumber, double, v8::Number);
192
+ DEFINE_NATIVE_BINDING_TYPE(float, IsNumber, double, v8::Number);
193
+
194
+ DEFINE_NATIVE_BINDING_TYPE(unsigned int, IsNumber, uint32_t, v8::Uint32);
195
+ DEFINE_NATIVE_BINDING_TYPE(unsigned short, IsNumber, uint32_t, v8::Uint32);
196
+ DEFINE_NATIVE_BINDING_TYPE(unsigned char, IsNumber, uint32_t, v8::Uint32);
197
+
198
+ DEFINE_NATIVE_BINDING_TYPE(signed int, IsNumber, int32_t, v8::Int32);
199
+ DEFINE_NATIVE_BINDING_TYPE(signed short, IsNumber, int32_t, v8::Int32);
200
+ DEFINE_NATIVE_BINDING_TYPE(signed char, IsNumber, int32_t, v8::Int32);
201
+
202
+ DEFINE_NATIVE_BINDING_TYPE(char, IsNumber, int32_t, v8::Int32);
203
+
204
+ #define DEFINE_STRING_BINDING_TYPE(ArgType) \
205
+ template <> struct BindingType<ArgType> { \
206
+ typedef ArgType Type; \
207
+ \
208
+ static inline bool checkType(WireType arg) { \
209
+ return(true); \
210
+ } \
211
+ \
212
+ static inline WireType toWireType(Type arg) { \
213
+ const char *buf = (arg == nullptr) ? "" : reinterpret_cast<const char *>(arg); \
214
+ return(Nan::New<v8::String>(buf, strlen(buf)).ToLocalChecked()); \
215
+ } \
216
+ }; \
217
+ \
218
+ template <> struct BindingType<StrictType<ArgType>> : public BindingType<ArgType> { \
219
+ static inline bool checkType(WireType arg) { \
220
+ return(arg->IsString()); \
221
+ } \
222
+ }
223
+
224
+ DEFINE_STRING_BINDING_TYPE(unsigned char *);
225
+ DEFINE_STRING_BINDING_TYPE(char *);
226
+ DEFINE_STRING_BINDING_TYPE(const unsigned char *);
227
+ DEFINE_STRING_BINDING_TYPE(const char *);
228
+
229
+ // void return values are passed to toWireType as null pointers.
230
+
231
+ template <> struct BindingType<void> {
232
+
233
+ typedef std::nullptr_t Type;
234
+
235
+ static inline Type fromWireType(WireType arg) { return(nullptr); }
236
+
237
+ static inline WireType toWireType(Type arg) { return(Nan::Undefined()); }
238
+
239
+ };
240
+
241
+ } // namespace
@@ -0,0 +1,59 @@
1
+ // This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ #pragma once
5
+
6
+ #include <node_version.h>
7
+
8
+ namespace nbind {
9
+
10
+ template<> struct BindingType<Buffer> {
11
+
12
+ typedef Buffer Type;
13
+
14
+ #if NODE_MODULE_VERSION >= 14 // >= Node.js 0.12
15
+
16
+ static void initFromArray(WireType arg, Buffer &result);
17
+
18
+ #endif // >= Node.js 0.12
19
+
20
+ static inline bool checkType(WireType arg) {
21
+ # if NODE_MODULE_VERSION >= 14 // Node.js 0.12
22
+ if(arg->IsArrayBuffer() || arg->IsArrayBufferView()) return(true);
23
+ # endif
24
+
25
+ # if NODE_MODULE_VERSION < 45 // IO.js 3.0
26
+ if(node::Buffer::HasInstance(arg)) return(true);
27
+ # endif
28
+
29
+ return(false);
30
+ }
31
+
32
+ static inline Type fromWireType(WireType arg) {
33
+ Buffer result(nullptr, 0, Nan::To<v8::Object>(arg).ToLocalChecked());
34
+
35
+ # if NODE_MODULE_VERSION >= 14 // Node.js 0.12
36
+ if(arg->IsArrayBuffer() || arg->IsArrayBufferView()) {
37
+ initFromArray(arg, result);
38
+ }
39
+ # endif
40
+
41
+ # if NODE_MODULE_VERSION < 45 // IO.js 3.0
42
+ if(node::Buffer::HasInstance(arg)) {
43
+ result.ptr = reinterpret_cast<unsigned char *>(node::Buffer::Data(arg));
44
+ result.len = node::Buffer::Length(arg);
45
+ }
46
+ # endif
47
+
48
+ return(result);
49
+ }
50
+
51
+ static inline WireType toWireType(Type arg) {
52
+ return(Nan::Null());
53
+ }
54
+
55
+ };
56
+
57
+ inline void Buffer :: commit() {}
58
+
59
+ } // namespace
@@ -0,0 +1,165 @@
1
+ // This file is part of nbind, copyright (C) 2014-2015 BusFaster Ltd.
2
+ // Released under the MIT license, see LICENSE.
3
+
4
+ // This file handles JavaScript callback functions accessible from C++.
5
+
6
+ #pragma once
7
+
8
+ namespace nbind {
9
+
10
+ // Exception signaling a JavaScript callback has thrown, and an exception is
11
+ // now bubbling up the JavaScript stack.
12
+
13
+ class cbException : public std::exception {};
14
+
15
+ // cbFunction is a functor that can be called with any number of arguments of any type
16
+ // compatible with JavaScript. Types are autodetected from a parameter pack.
17
+ // Normally the function returns nothing when called, but it has a templated
18
+ // call<ReturnType>() method that accepts the expected return type as a template
19
+ // parameter, and handles conversion automatically.
20
+
21
+ template <typename DefaultReturnType>
22
+ class cbWrapper {
23
+
24
+ template<typename PolicyList, size_t Index, typename ArgType>
25
+ friend struct ArgFromWire;
26
+
27
+ public:
28
+
29
+ explicit cbWrapper(const v8::Local<v8::Function> &func) : func(func) {}
30
+
31
+ cbWrapper(const cbWrapper &func) : func(func.getJsFunction()) {}
32
+
33
+ ~cbWrapper() {
34
+ func.Reset();
35
+ }
36
+
37
+ void reset() {
38
+ func.Reset();
39
+ }
40
+
41
+ template<typename... Args>
42
+ DefaultReturnType operator()(Args&&... args) {
43
+ return(call<DefaultReturnType>(std::move(args)...));
44
+ }
45
+
46
+ template <typename ReturnType, typename... Args>
47
+ typename TypeTransformer<ReturnType>::Type call(Args&&... args) const {
48
+ v8::Local<v8::Value> argv[] = {
49
+ (convertToWire(std::move(args)))...,
50
+ // Avoid error C2466: cannot allocate an array of constant size 0.
51
+ Nan::Null()
52
+ };
53
+
54
+ Nan::MaybeLocal<v8::Value> result = Nan::Call(func, Nan::GetCurrentContext()->Global(), sizeof...(Args), argv);
55
+
56
+ if(result.IsEmpty()) throw(cbException());
57
+
58
+ return(convertFromWire<ReturnType>(result.ToLocalChecked()));
59
+ }
60
+
61
+ template <typename ReturnType, typename... Args>
62
+ typename TypeTransformer<ReturnType>::Type callMethod(
63
+ v8::Local<v8::Object> target,
64
+ Args&&... args
65
+ ) const {
66
+ v8::Local<v8::Value> argv[] = {
67
+ (convertToWire(std::move(args)))...,
68
+ // Avoid error C2466: cannot allocate an array of constant size 0.
69
+ Nan::Null()
70
+ };
71
+
72
+ Nan::MaybeLocal<v8::Value> result = Nan::Call(func, target, sizeof...(Args), argv);
73
+
74
+ if(result.IsEmpty()) throw(cbException());
75
+
76
+ return(convertFromWire<ReturnType>(result.ToLocalChecked()));
77
+ }
78
+
79
+ v8::Local<v8::Function> getJsFunction() const { return(func.GetFunction()); }
80
+
81
+ private:
82
+
83
+ Nan::Callback func;
84
+
85
+ };
86
+
87
+ template <> template<typename... Args>
88
+ void cbWrapper<void> :: operator()(Args&&... args) {
89
+ call<void>(std::move(args)...);
90
+ }
91
+
92
+ // Note: passing cbFunction by value on asm.js doesn't work.
93
+
94
+ template <> struct BindingType<cbFunction> {
95
+
96
+ typedef const cbFunction Type;
97
+
98
+ static inline bool checkType(WireType arg) {
99
+ return(arg->IsFunction());
100
+ }
101
+
102
+ static inline Type fromWireType(WireType arg) {
103
+ return(cbFunction(arg.As<v8::Function>()));
104
+ }
105
+
106
+ };
107
+
108
+ template <> struct BindingType<const cbFunction &> {
109
+
110
+ typedef const cbFunction &Type;
111
+
112
+ static inline bool checkType(WireType arg) {
113
+ return(arg->IsFunction());
114
+ }
115
+
116
+ };
117
+
118
+ template <> struct BindingType<cbFunction &> : public BindingType<const cbFunction &> {};
119
+
120
+ // Handle callback functions. They are converted to a functor of type cbFunction,
121
+ // which can be called directly from C++ with arguments of any type.
122
+
123
+ template<typename PolicyList, size_t Index>
124
+ struct ArgFromWire<PolicyList, Index, const cbFunction &> {
125
+
126
+ template <typename NanArgs>
127
+ ArgFromWire(const NanArgs &args) : val(args[Index].template As<v8::Function>()) {}
128
+
129
+ template <typename NanArgs>
130
+ inline const cbFunction &get(const NanArgs &args) {
131
+ return(val);
132
+ }
133
+
134
+ cbFunction val;
135
+
136
+ };
137
+
138
+ template<typename PolicyList, size_t Index>
139
+ struct ArgFromWire<PolicyList, Index, cbFunction &> {
140
+
141
+ template <typename NanArgs>
142
+ ArgFromWire(const NanArgs &args) : val(args[Index].template As<v8::Function>()) {}
143
+
144
+ template <typename NanArgs>
145
+ inline cbFunction &get(const NanArgs &args) {
146
+ return(val);
147
+ }
148
+
149
+ cbFunction val;
150
+
151
+ };
152
+
153
+ template <typename ReturnType, typename... Args>
154
+ void cbOutput :: call(Args... args) {
155
+ v8::Local<v8::Value> argv[] = {
156
+ (BindingType<Args>::toWireType(std::forward<Args>(args)))...
157
+ };
158
+
159
+ auto result = Nan::NewInstance(jsConstructor.getJsFunction(), sizeof...(Args), argv);
160
+
161
+ if(result.IsEmpty()) *output = Nan::Undefined();
162
+ else *output = result.ToLocalChecked();
163
+ }
164
+
165
+ } // namespace