@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,204 @@
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
+ // Following emscripten conventions, the type passed between the two is called
6
+ // WireType.
7
+ // Anything from the standard library is instead in BindingStd.h
8
+
9
+ /*
10
+ This file is very similar to wire.h in Embind, and serves the same purpose.
11
+ The following conditions apply to any identical parts:
12
+
13
+ Copyright (c) 2010-2014 Emscripten authors, see EMSCRIPTEN-AUTHORS file.
14
+
15
+ Permission is hereby granted, free of charge, to any person obtaining a copy
16
+ of this software and associated documentation files (the "Software"), to deal
17
+ in the Software without restriction, including without limitation the rights
18
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
+ copies of the Software, and to permit persons to whom the Software is
20
+ furnished to do so, subject to the following conditions:
21
+
22
+ The above copyright notice and this permission notice shall be included in
23
+ all copies or substantial portions of the Software.
24
+
25
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
31
+ THE SOFTWARE.
32
+ */
33
+
34
+ #pragma once
35
+
36
+ #include <memory>
37
+ #include <string>
38
+ #include <vector>
39
+
40
+ namespace nbind {
41
+
42
+ // Generic C++ object.
43
+
44
+ template <typename ArgType> struct BindingType {
45
+
46
+ typedef ArgType Type;
47
+ typedef struct {
48
+ std::shared_ptr<ArgType> *boundShared;
49
+ ArgType *boundUnsafe;
50
+ } *WireType;
51
+
52
+ static inline Type fromWireType(WireType arg) {
53
+ // Hack: JS side sends Type * instead of WireType, since C++ side can
54
+ // easily unwrap the shared_ptr anyway.
55
+
56
+ return(*reinterpret_cast<Type *>(arg));
57
+ }
58
+
59
+ static inline WireType toWireType(ArgType &&arg) {
60
+ WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val)));
61
+
62
+ val->boundUnsafe = new ArgType(std::move(arg));
63
+ val->boundShared = new std::shared_ptr<ArgType>(val->boundUnsafe);
64
+
65
+ return(val);
66
+ }
67
+
68
+ static inline WireType toWireType(const ArgType &arg) {
69
+ WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val)));
70
+
71
+ val->boundUnsafe = new ArgType(arg);
72
+ val->boundShared = new std::shared_ptr<ArgType>(val->boundUnsafe);
73
+
74
+ return(val);
75
+ }
76
+
77
+ };
78
+
79
+ // Object pointer.
80
+
81
+ template <typename ArgType>
82
+ struct BindingType<ArgType *> {
83
+
84
+ typedef ArgType *Type;
85
+ typedef ArgType *WireType;
86
+
87
+ // checkType is not called on Emscripten target.
88
+ // static inline bool checkType(WireType arg) { return(arg != nullptr); }
89
+
90
+ static inline Type fromWireType(WireType arg) { return(arg); }
91
+ static inline WireType toWireType(Type arg) { return(arg); }
92
+
93
+ };
94
+
95
+ // Object reference.
96
+
97
+ template <typename ArgType>
98
+ struct BindingType<ArgType &> {
99
+
100
+ typedef ArgType &Type;
101
+ typedef ArgType *WireType;
102
+
103
+ static inline Type fromWireType(WireType arg) { return(*arg); }
104
+ static inline WireType toWireType(Type arg) { return(&arg); }
105
+
106
+ };
107
+
108
+ // Object rvalue reference.
109
+
110
+ template <typename ArgType>
111
+ struct BindingType<ArgType &&> {
112
+
113
+ typedef ArgType &&Type;
114
+ typedef ArgType *WireType;
115
+
116
+ static inline Type fromWireType(WireType arg) { return(*arg); }
117
+ static inline WireType toWireType(Type arg) { return(&arg); }
118
+
119
+ };
120
+
121
+ // Nullable type, just calls the non-nullable version.
122
+ // Functional differences are on JS side.
123
+
124
+ template <typename ArgType>
125
+ struct BindingType<NullableType<ArgType>> {
126
+
127
+ typedef typename BindingType<ArgType>::Type Type;
128
+ typedef typename BindingType<ArgType>::WireType WireType;
129
+
130
+ static inline Type fromWireType(WireType arg) { return(arg); }
131
+ static inline WireType toWireType(Type arg) { return(arg); }
132
+
133
+ };
134
+
135
+ template <typename ArgType>
136
+ struct BindingType<ValueType<ArgType>> {
137
+
138
+ typedef ArgType Type;
139
+ typedef typename std::remove_reference<ArgType>::type ObjType;
140
+
141
+ // Pointer or offset (times 2 plus 1 to distinguish from pointers)
142
+ // to a list of constructed objects on the JavaScript side.
143
+
144
+ typedef typename BindingType<ObjType>::WireType WireType;
145
+
146
+ static inline Type fromWireType(WireType arg);
147
+ static inline WireType toWireType(Type &&arg);
148
+
149
+ };
150
+
151
+ #define DEFINE_NATIVE_BINDING_TYPE(ArgType, ArgWireType) \
152
+ template <> struct BindingType<ArgType> { \
153
+ typedef ArgType Type; \
154
+ typedef ArgWireType WireType; \
155
+ static inline Type fromWireType(WireType arg) { return(arg); } \
156
+ static inline WireType toWireType(Type arg) { return(arg); } \
157
+ }; \
158
+ \
159
+ template <> struct BindingType<StrictType<ArgType>> : public BindingType<ArgType> {}
160
+
161
+ DEFINE_NATIVE_BINDING_TYPE(double, double);
162
+ DEFINE_NATIVE_BINDING_TYPE(float, double);
163
+
164
+ DEFINE_NATIVE_BINDING_TYPE(unsigned int, int);
165
+ DEFINE_NATIVE_BINDING_TYPE(unsigned short, int);
166
+ DEFINE_NATIVE_BINDING_TYPE(unsigned char, int);
167
+
168
+ DEFINE_NATIVE_BINDING_TYPE(int, int);
169
+ DEFINE_NATIVE_BINDING_TYPE(short, int);
170
+ DEFINE_NATIVE_BINDING_TYPE(char, int);
171
+
172
+ DEFINE_NATIVE_BINDING_TYPE(unsigned char *, unsigned char *);
173
+ DEFINE_NATIVE_BINDING_TYPE(char *, char *);
174
+ DEFINE_NATIVE_BINDING_TYPE(const unsigned char *, const unsigned char *);
175
+ DEFINE_NATIVE_BINDING_TYPE(const char *, const char *);
176
+
177
+ template<> struct BindingType<bool> {
178
+
179
+ typedef bool Type;
180
+
181
+ typedef int WireType;
182
+
183
+ static inline Type fromWireType(WireType arg) { return(arg != 0); }
184
+
185
+ static inline WireType toWireType(Type arg) { return(arg); }
186
+
187
+ };
188
+
189
+ template <> struct BindingType<StrictType<bool>> : public BindingType<bool> {};
190
+
191
+ template<> struct BindingType<void> {
192
+
193
+ typedef void Type;
194
+
195
+ typedef void WireType;
196
+
197
+ static inline Type fromWireType() { }
198
+
199
+ template <typename... Args>
200
+ static inline WireType toWireType(Args...) { }
201
+
202
+ };
203
+
204
+ } // namespace
@@ -0,0 +1,37 @@
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
+ namespace nbind {
7
+
8
+ template<> struct BindingType<Buffer> {
9
+
10
+ typedef Buffer Type;
11
+
12
+ typedef struct {
13
+ uint32_t length;
14
+ unsigned char *data;
15
+ unsigned int num;
16
+ } *WireType;
17
+
18
+ static inline Type fromWireType(WireType arg) {
19
+ return(Buffer(
20
+ arg->data,
21
+ arg->length,
22
+ arg->num
23
+ ));
24
+ }
25
+
26
+ static inline WireType toWireType(Type arg) { return(nullptr); }
27
+
28
+ };
29
+
30
+ inline void Buffer :: commit() {
31
+ EM_ASM_ARGS(
32
+ {_nbind.commitBuffer($0,$1,$2);},
33
+ handle.getNum(), ptr, len
34
+ );
35
+ }
36
+
37
+ } // namespace
@@ -0,0 +1,186 @@
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
+ #include <emscripten.h>
9
+
10
+ namespace nbind {
11
+
12
+ // Wrapper class to specialize call function for different return types,
13
+ // since function template partial specialization is forbidden.
14
+ template <typename ReturnType>
15
+ struct cbCaller {
16
+ typedef typename TypeTransformer<ReturnType>::Binding ReturnBindingType;
17
+
18
+ template <typename... Args>
19
+ static ReturnType call(unsigned int num, Args... args);
20
+ };
21
+
22
+ template <typename DefaultReturnType>
23
+ class cbWrapper {
24
+
25
+ public:
26
+
27
+ explicit cbWrapper(unsigned int num = 0) : handle(num) {}
28
+
29
+ template<typename... Args>
30
+ DefaultReturnType operator()(Args&&... args) {
31
+ return(call<DefaultReturnType>(std::forward<Args>(args)...));
32
+ }
33
+
34
+ template <typename ReturnType, typename... Args>
35
+ typename BindingType<ReturnType>::Type call(Args&&... args) const {
36
+ // Restore linear allocator state in RAII style when done.
37
+ PoolRestore restore;
38
+
39
+ return(cbCaller<ReturnType>::call(handle.getNum(), std::forward<Args>(args)...));
40
+ }
41
+
42
+ private:
43
+
44
+ External handle;
45
+
46
+ };
47
+
48
+ template<> template<typename... Args>
49
+ void cbWrapper<void> :: operator()(Args&&... args) {
50
+ call<void>(std::forward<Args>(args)...);
51
+ }
52
+
53
+ class cbOutput {
54
+
55
+ template<typename ArgType>
56
+ friend struct BindingType;
57
+
58
+ public:
59
+
60
+ struct CreateValue {};
61
+
62
+ explicit cbOutput(cbFunction &jsConstructor) :
63
+ jsConstructor(jsConstructor), original(*this) {}
64
+
65
+ cbOutput(const cbOutput &other) :
66
+ jsConstructor(other.jsConstructor), original(other.original) {}
67
+
68
+ cbOutput &operator=(const cbOutput &) = delete;
69
+
70
+ template<typename... Args>
71
+ inline int operator()(Args&&... args) {
72
+ original.slot = jsConstructor.call<CreateValue>(args...);
73
+
74
+ return(original.slot);
75
+ }
76
+
77
+ int getSlot() { return(original.slot); }
78
+
79
+ private:
80
+
81
+ cbFunction &jsConstructor;
82
+ cbOutput &original;
83
+ int slot = 0;
84
+
85
+ };
86
+
87
+ template <typename... Args>
88
+ double callDouble(unsigned int num, Args... args) {
89
+ return(EM_ASM_DOUBLE(
90
+ {return(_nbind.callbackSignatureList[$0].apply(this,arguments));},
91
+ CallbackSignature<double, Args...>::getInstance().getNum(),
92
+ num,
93
+ convertToWire(std::forward<Args>(args))...
94
+ ));
95
+ }
96
+
97
+ template <typename ReturnType> template <typename... Args>
98
+ ReturnType cbCaller<ReturnType>::call(unsigned int num, Args... args) {
99
+ return(ReturnBindingType::fromWireType(reinterpret_cast<typename ReturnBindingType::WireType>(
100
+ EM_ASM_INT(
101
+ {return(_nbind.callbackSignatureList[$0].apply(this,arguments));},
102
+ CallbackSignature<ReturnType, Args...>::getInstance().getNum(),
103
+ num,
104
+ convertToWire(std::forward<Args>(args))...
105
+ )
106
+ )));
107
+ }
108
+
109
+ template<> struct cbCaller<void> {
110
+
111
+ template <typename... Args>
112
+ static void call(unsigned int num, Args... args) {
113
+ EM_ASM_ARGS(
114
+ {return(_nbind.callbackSignatureList[$0].apply(this,arguments));},
115
+ CallbackSignature<void, Args...>::getInstance().getNum(),
116
+ num,
117
+ convertToWire(std::forward<Args>(args))...
118
+ );
119
+ }
120
+
121
+ };
122
+
123
+ template<> struct cbCaller<double> {
124
+
125
+ template <typename... Args>
126
+ static double call(unsigned int num, Args... args) {
127
+ return(callDouble(num, args...));
128
+ }
129
+
130
+ };
131
+
132
+ template<> struct cbCaller<float> {
133
+
134
+ template <typename... Args>
135
+ static float call(unsigned int num, Args... args) {
136
+ return(callDouble(num, args...));
137
+ }
138
+
139
+ };
140
+
141
+ template<> struct cbCaller<cbOutput::CreateValue> {
142
+
143
+ template <typename... Args>
144
+ static int call(unsigned int num, Args... args) {
145
+ return(EM_ASM_ARGS({return(_nbind.callbackSignatureList[$0].apply(this,arguments));},
146
+ CallbackSignature<cbOutput::CreateValue, Args...>::getInstance().getNum(),
147
+ num,
148
+ convertToWire(std::forward<Args>(args))...
149
+ ));
150
+ }
151
+
152
+ };
153
+
154
+ template <> struct BindingType<const cbFunction &> {
155
+
156
+ typedef const cbFunction &Type;
157
+
158
+ typedef unsigned int WireType;
159
+
160
+ };
161
+
162
+ template <> struct BindingType<cbFunction &> : public BindingType<const cbFunction &> {};
163
+
164
+ template<typename PolicyList>
165
+ struct ArgFromWire<PolicyList, const cbFunction &> {
166
+
167
+ explicit ArgFromWire(unsigned int num) : val(num) {}
168
+
169
+ inline cbFunction &get(unsigned int num) { return(val); }
170
+
171
+ cbFunction val;
172
+
173
+ };
174
+
175
+ template<typename PolicyList>
176
+ struct ArgFromWire<PolicyList, cbFunction &> {
177
+
178
+ explicit ArgFromWire(unsigned int num) : val(num) {}
179
+
180
+ inline cbFunction &get(unsigned int num) { return(val); }
181
+
182
+ cbFunction val;
183
+
184
+ };
185
+
186
+ } // namespace
@@ -0,0 +1,61 @@
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
+ template<typename PolicyList, typename ReturnType, typename... Args>
9
+ struct Caller {
10
+
11
+ typedef typename TypeTransformer<ReturnType, PolicyList>::Binding ReturnBindingType;
12
+
13
+ template<typename MethodType, class Bound>
14
+ static typename ReturnBindingType::WireType callMethod(
15
+ Bound &target,
16
+ MethodType method,
17
+ typename TypeTransformer<Args, PolicyList>::WireType... args
18
+ ) {
19
+ return(ReturnBindingType::toWireType(
20
+ (target.*method)(ArgFromWire<PolicyList, Args>(args).get(args)...)
21
+ ));
22
+ }
23
+
24
+ template<typename FunctionType>
25
+ static typename ReturnBindingType::WireType callFunction(
26
+ FunctionType func,
27
+ typename TypeTransformer<Args, PolicyList>::WireType... args
28
+ ) {
29
+ return(ReturnBindingType::toWireType(
30
+ (*func)(ArgFromWire<PolicyList, Args>(args).get(args)...)
31
+ ));
32
+ }
33
+
34
+ };
35
+
36
+ // Specialize Caller for void return type, because toWireType needs a non-void
37
+ // argument.
38
+
39
+ template<typename PolicyList, typename... Args>
40
+ struct Caller<PolicyList, void, Args...> {
41
+
42
+ template<typename MethodType, class Bound>
43
+ static void callMethod(
44
+ Bound &target,
45
+ MethodType method,
46
+ typename TypeTransformer<Args, PolicyList>::WireType... args
47
+ ) {
48
+ (target.*method)(ArgFromWire<PolicyList, Args>(args).get(args)...);
49
+ }
50
+
51
+ template<typename FunctionType>
52
+ static void callFunction(
53
+ FunctionType func,
54
+ typename TypeTransformer<Args, PolicyList>::WireType... args
55
+ ) {
56
+ (*func)(ArgFromWire<PolicyList, Args>(args).get(args)...);
57
+ }
58
+
59
+ };
60
+
61
+ } // namespace
@@ -0,0 +1,30 @@
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
+ template<typename PolicyList, class Bound, typename... Args>
9
+ struct Creator {
10
+
11
+ typedef typename BindingType<Bound>::WireType WireType;
12
+
13
+ static inline WireType create(typename BindingType<Args>::WireType... args) {
14
+ // return(new Bound(ArgFromWire<PolicyList, Args>(args).get(args)...));
15
+
16
+ WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val)));
17
+
18
+ val->boundUnsafe = new Bound(ArgFromWire<PolicyList, Args>(args).get(args)...);
19
+ val->boundShared = new std::shared_ptr<Bound>(val->boundUnsafe);
20
+
21
+ return(val);
22
+ }
23
+
24
+ static void createValue(ArgStorage &storage, typename BindingType<Args>::WireType... args) {
25
+ static_cast<TemplatedArgStorage<Bound> &>(storage).init(ArgFromWire<PolicyList, Args>(args).get(args)...);
26
+ }
27
+
28
+ };
29
+
30
+ } // namespace
@@ -0,0 +1,63 @@
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
+ extern "C" {
9
+ extern void _nbind_reference_external(unsigned int num);
10
+ extern void _nbind_free_external(unsigned int num);
11
+ }
12
+
13
+ class External {
14
+
15
+ public:
16
+
17
+ explicit External(unsigned int num = 0) : num(num) {}
18
+
19
+ External(const External &other) : num(other.num) {
20
+ _nbind_reference_external(num);
21
+ }
22
+
23
+ External (External &&other) : num(other.num) { other.num = 0; }
24
+
25
+ External &operator=(const External &other) {
26
+ if(num) _nbind_free_external(num);
27
+ num = other.num;
28
+ _nbind_reference_external(num);
29
+
30
+ return(*this);
31
+ }
32
+
33
+ External &operator=(External &&other) {
34
+ if(num) _nbind_free_external(num);
35
+ num = other.num;
36
+ other.num = 0;
37
+
38
+ return(*this);
39
+ }
40
+
41
+ ~External() {
42
+ if(num) _nbind_free_external(num);
43
+ }
44
+
45
+ inline unsigned int getNum() const { return(num); }
46
+
47
+ private:
48
+
49
+ unsigned int num;
50
+ };
51
+
52
+ template<>
53
+ struct BindingType<External> {
54
+
55
+ typedef External Type;
56
+ typedef int WireType;
57
+
58
+ static inline Type fromWireType(WireType arg) { return(External(arg)); }
59
+ static inline WireType toWireType(Type arg) { return(arg.getNum()); }
60
+
61
+ };
62
+
63
+ } // namespace
@@ -0,0 +1,131 @@
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
+ extern "C" {
9
+ extern unsigned int _nbind_get_int_64(unsigned int index, uint32_t *storage);
10
+ }
11
+
12
+ class Int64 {};
13
+
14
+ // 2^64, first integer not representable with uint64_t.
15
+ // Start of range used for other flags.
16
+ const double valueBase = 18446744073709551616.0;
17
+
18
+ template <int size> struct Int64Converter {
19
+ typedef int WireType;
20
+
21
+ template <typename ArgType>
22
+ static inline WireType uint64ToWire(ArgType arg) {
23
+ return(static_cast<int>(arg));
24
+ }
25
+
26
+ template <typename ArgType>
27
+ static inline WireType int64ToWire(ArgType arg) {
28
+ return(static_cast<int>(arg));
29
+ }
30
+
31
+ template <typename ArgType>
32
+ static inline ArgType fromWire(WireType arg) {
33
+ return(arg);
34
+ }
35
+ };
36
+
37
+ template<> struct Int64Converter<8> {
38
+ typedef double WireType;
39
+
40
+ template <typename ArgType>
41
+ static inline WireType uint64ToWire(ArgType arg) {
42
+ if(arg <= 0x20000000000000ULL) {
43
+ // Number fits in double.
44
+ return(static_cast<double>(arg));
45
+ }
46
+
47
+ cbFunction *jsConstructor = BindClass<Int64>::getInstance().getValueConstructorJS();
48
+
49
+ if(jsConstructor != nullptr) {
50
+ // Construct custom bignum object from high and low 32-bit halves.
51
+ cbOutput construct(*jsConstructor);
52
+
53
+ return(
54
+ construct(
55
+ static_cast<uint32_t>(arg),
56
+ static_cast<uint32_t>(arg >> 32),
57
+ false
58
+ ) * 4096 + valueBase
59
+ );
60
+ } else {
61
+ // Int64 JavaScript class is missing or not registered.
62
+ // Just cast to double then.
63
+ return(static_cast<double>(arg));
64
+ }
65
+ }
66
+
67
+ template <typename ArgType>
68
+ static inline WireType int64ToWire(ArgType arg) {
69
+ if(arg >= -0x20000000000000LL && arg <= 0x20000000000000LL) {
70
+ return(static_cast<double>(arg));
71
+ }
72
+
73
+ cbFunction *jsConstructor = BindClass<Int64>::getInstance().getValueConstructorJS();
74
+
75
+ if(jsConstructor != nullptr) {
76
+ cbOutput construct(*jsConstructor);
77
+
78
+ bool sign = arg < 0;
79
+ if(sign) arg = -arg;
80
+
81
+ return(
82
+ construct(
83
+ static_cast<uint32_t>(arg),
84
+ static_cast<uint32_t>(static_cast<uint64_t>(arg) >> 32),
85
+ sign
86
+ ) * 4096 + valueBase
87
+ );
88
+ } else {
89
+ // Int64 JavaScript class is missing or not registered.
90
+ return(static_cast<double>(arg));
91
+ }
92
+ }
93
+
94
+ template <typename ArgType>
95
+ static inline ArgType fromWire(WireType arg) {
96
+ if(arg <= valueBase) return(arg);
97
+
98
+ unsigned int index = (arg - valueBase) / 4096;
99
+ ArgType storage = 0;
100
+
101
+ _nbind_get_int_64(index, reinterpret_cast<uint32_t *>(&storage));
102
+
103
+ return(storage);
104
+ }
105
+ };
106
+
107
+ #define DEFINE_INT64_BINDING_TYPE(ArgType, encode) \
108
+ template <> struct BindingType<ArgType> { \
109
+ typedef ArgType Type; \
110
+ typedef typename Int64Converter<sizeof(Type)>::WireType WireType; \
111
+ \
112
+ static inline WireType toWireType(Type arg) { \
113
+ return(Int64Converter<sizeof(Type)>::encode(arg)); \
114
+ } \
115
+ \
116
+ static inline Type fromWireType(WireType arg) { \
117
+ return(Int64Converter<sizeof(Type)>::fromWire<Type>(arg)); \
118
+ } \
119
+ }; \
120
+ \
121
+ template <> struct BindingType<StrictType<ArgType>> : public BindingType<ArgType> {}
122
+
123
+ // Handle possibly 64-bit types.
124
+ // Types detected to fit in 32 bits automatically use a faster code path.
125
+
126
+ DEFINE_INT64_BINDING_TYPE(unsigned long, uint64ToWire);
127
+ DEFINE_INT64_BINDING_TYPE(unsigned long long, uint64ToWire);
128
+ DEFINE_INT64_BINDING_TYPE(signed long, int64ToWire);
129
+ DEFINE_INT64_BINDING_TYPE(signed long long, int64ToWire);
130
+
131
+ } // namespace