@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
|
@@ -0,0 +1,140 @@
|
|
|
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
|
+
// Caller handles the template magic to compose a method call from a class and
|
|
9
|
+
// parts of a method signature extracted from it.
|
|
10
|
+
|
|
11
|
+
template<typename...> struct TypeList {};
|
|
12
|
+
|
|
13
|
+
template <typename NanArgs>
|
|
14
|
+
v8::Local<v8::Value> makeTypeError(
|
|
15
|
+
NanArgs &args,
|
|
16
|
+
uint32_t count,
|
|
17
|
+
const TYPEID *typeList,
|
|
18
|
+
bool *flagList
|
|
19
|
+
) {
|
|
20
|
+
(void)args; // Silence compiler warning about unused parameter.
|
|
21
|
+
|
|
22
|
+
v8::Local<v8::Value> err = Nan::TypeError("Type mismatch");
|
|
23
|
+
v8::Local<v8::Object> errObj = Nan::To<v8::Object>(err).ToLocalChecked();
|
|
24
|
+
// v8::Local<v8::Array> typeArray = Nan::New<v8::Array>(count);
|
|
25
|
+
v8::Local<v8::Array> flagArray = Nan::New<v8::Array>(count);
|
|
26
|
+
|
|
27
|
+
for(uint32_t num = 0; num < count; ++num) {
|
|
28
|
+
Nan::Set(flagArray, num, Nan::New<v8::Boolean>(flagList[num]));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// errObj->Set(Nan::New<v8::String>("types").ToLocalChecked(), typeArray);
|
|
32
|
+
Nan::Set(errObj, Nan::New<v8::String>("flags").ToLocalChecked(), flagArray);
|
|
33
|
+
|
|
34
|
+
return(err);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// CheckWire verifies if the type of a JavaScript handle corresponds to a C++ type.
|
|
38
|
+
|
|
39
|
+
template<typename PolicyList, size_t Index, typename ArgType>
|
|
40
|
+
struct CheckWire {
|
|
41
|
+
|
|
42
|
+
typedef TypeTransformer<ArgType, PolicyList> Transformed;
|
|
43
|
+
|
|
44
|
+
template <typename NanArgs>
|
|
45
|
+
static inline bool checkType(const NanArgs &args) {
|
|
46
|
+
return(Transformed::Binding::checkType(args[Index]));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
template<typename ArgList> struct Checker;
|
|
52
|
+
|
|
53
|
+
template<typename... Args>
|
|
54
|
+
struct Checker<TypeList<Args...>> {
|
|
55
|
+
|
|
56
|
+
static bool booleanAnd(bool flag) {
|
|
57
|
+
return(flag);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
template <typename... Rest>
|
|
61
|
+
static bool booleanAnd(bool flag, Rest... rest) {
|
|
62
|
+
return(flag & booleanAnd(rest...));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
template <typename NanArgs>
|
|
66
|
+
static bool typesAreValid(NanArgs &args) {
|
|
67
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
68
|
+
|
|
69
|
+
return(booleanAnd(Args::checkType(args)..., true));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
template <typename NanArgs>
|
|
73
|
+
static v8::Local<v8::Value> getTypeError(NanArgs &args, const TYPEID *typeList) {
|
|
74
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
75
|
+
|
|
76
|
+
bool flagList[] = { Args::checkType(args)..., true };
|
|
77
|
+
|
|
78
|
+
return(makeTypeError(args, sizeof...(Args), typeList, flagList));
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
template<typename ReturnType, typename ArgList> struct Caller;
|
|
84
|
+
|
|
85
|
+
template<typename ReturnType, typename... Args>
|
|
86
|
+
struct Caller<ReturnType, TypeList<Args...>> {
|
|
87
|
+
|
|
88
|
+
template <class Bound, typename MethodType, typename NanArgs>
|
|
89
|
+
static WireType callMethod(Bound &target, MethodType method, NanArgs &args) noexcept(false) {
|
|
90
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
91
|
+
|
|
92
|
+
// Note that Args().get may throw.
|
|
93
|
+
return(MethodResultConverter<ReturnType>::toWireType(
|
|
94
|
+
(target.*method)(Args(args).get(args)...),
|
|
95
|
+
target,
|
|
96
|
+
0.0
|
|
97
|
+
));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
template <typename Function, typename NanArgs>
|
|
101
|
+
static WireType callFunction(Function func, NanArgs &args) noexcept(false) {
|
|
102
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
103
|
+
|
|
104
|
+
// Note that Args().get may throw.
|
|
105
|
+
return(convertToWire<ReturnType>(
|
|
106
|
+
(*func)(Args(args).get(args)...)
|
|
107
|
+
));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// Specialize Caller for void return type, because toWireType needs a non-void
|
|
113
|
+
// argument.
|
|
114
|
+
|
|
115
|
+
template<typename... Args>
|
|
116
|
+
struct Caller<void, TypeList<Args...>> {
|
|
117
|
+
|
|
118
|
+
template <class Bound, typename MethodType, typename NanArgs>
|
|
119
|
+
static WireType callMethod(Bound &target, MethodType method, NanArgs &args) noexcept(false) {
|
|
120
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
121
|
+
|
|
122
|
+
// Note that Args().get may throw.
|
|
123
|
+
(target.*method)(Args(args).get(args)...);
|
|
124
|
+
|
|
125
|
+
return(Nan::Undefined());
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
template <typename Function, typename NanArgs>
|
|
129
|
+
static WireType callFunction(Function func, NanArgs &args) noexcept(false) {
|
|
130
|
+
(void)args; // Silence possible compiler warning about unused parameter.
|
|
131
|
+
|
|
132
|
+
// Note that Args().get may throw.
|
|
133
|
+
(*func)(Args(args).get(args)...);
|
|
134
|
+
|
|
135
|
+
return(Nan::Undefined());
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
} // namespace
|
|
@@ -0,0 +1,80 @@
|
|
|
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
|
+
// Calls BindClass<Bound>::getInstance()->getValueConstructorJS();
|
|
9
|
+
// We don't want to depend on BindClass.h here.
|
|
10
|
+
|
|
11
|
+
template <class Bound>
|
|
12
|
+
cbFunction *getValueConstructorJS();
|
|
13
|
+
|
|
14
|
+
// Send value object to JavaScript using toJS method of its C++ class.
|
|
15
|
+
// It was received by value, so it can be moved.
|
|
16
|
+
|
|
17
|
+
template <typename ArgType>
|
|
18
|
+
inline WireType BindingType<ValueType<ArgType>>::toWireType(ArgType &&arg) {
|
|
19
|
+
v8::Local<v8::Value> output = Nan::Undefined();
|
|
20
|
+
cbFunction *jsConstructor = getValueConstructorJS<
|
|
21
|
+
typename std::remove_const<ArgType>::type
|
|
22
|
+
>();
|
|
23
|
+
|
|
24
|
+
if(jsConstructor != nullptr) {
|
|
25
|
+
cbOutput construct(*jsConstructor, &output);
|
|
26
|
+
|
|
27
|
+
arg.toJS(construct);
|
|
28
|
+
} else {
|
|
29
|
+
// Value type JavaScript class is missing or not registered.
|
|
30
|
+
return(BindingType<ArgType *>::toWireType(new ArgType(std::move(arg))));
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return(output);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Handle value types as return values.
|
|
37
|
+
// This converter allows overriding the return type's toJS function
|
|
38
|
+
// with the wrapped object's toJS function.
|
|
39
|
+
|
|
40
|
+
template<typename ReturnType> struct MethodResultConverter {
|
|
41
|
+
|
|
42
|
+
// Call the toJS method of a returned C++ object, to convert it into a JavaScript object.
|
|
43
|
+
// This is used when a C++ function is called from JavaScript.
|
|
44
|
+
// A functor capable of calling the correct JavaScript constructor is passed to toJS,
|
|
45
|
+
// which must call the functor with arguments in the correct order.
|
|
46
|
+
// The functor calls the JavaScript constructor and writes a pointer to the resulting object
|
|
47
|
+
// directly into a local handle called "output" which is returned to JavaScript.
|
|
48
|
+
|
|
49
|
+
template <typename Bound>
|
|
50
|
+
static inline auto toWireType(ReturnType &&result, Bound &target, int dummy) -> typename std::remove_reference<decltype(
|
|
51
|
+
// SFINAE, use this template only if Bound::toJS(ReturnType, cbOutput) exists.
|
|
52
|
+
target.toJS(std::declval<ReturnType>(), *(cbOutput *)nullptr),
|
|
53
|
+
// Actual return type of this function: WireType (decltype adds a reference, which is removed).
|
|
54
|
+
*(WireType *)nullptr
|
|
55
|
+
)>::type {
|
|
56
|
+
v8::Local<v8::Value> output = Nan::Undefined();
|
|
57
|
+
cbFunction *jsConstructor = getValueConstructorJS<ReturnType>();
|
|
58
|
+
|
|
59
|
+
if(jsConstructor != nullptr) {
|
|
60
|
+
cbOutput construct(*jsConstructor, &output);
|
|
61
|
+
|
|
62
|
+
target.toJS(std::move(result), construct);
|
|
63
|
+
} else {
|
|
64
|
+
return(BindingType<ReturnType *>::toWireType(new ReturnType(std::move(result))));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return(output);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// If Bound::toJS(ReturnType, cbOutput) is missing
|
|
71
|
+
// (bound may not even be a class), fall back to ReturnType::toJS(cbOutput).
|
|
72
|
+
|
|
73
|
+
template <typename Bound>
|
|
74
|
+
static inline WireType toWireType(ReturnType &&result, Bound &target, double dummy) {
|
|
75
|
+
return(convertToWire(std::forward<ReturnType>(result)));
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
} // namespace
|
|
@@ -0,0 +1,26 @@
|
|
|
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<class Bound, typename ArgList> struct Creator;
|
|
9
|
+
|
|
10
|
+
template<class Bound, typename... Args>
|
|
11
|
+
struct Creator<Bound, TypeList<Args...>> {
|
|
12
|
+
|
|
13
|
+
public:
|
|
14
|
+
|
|
15
|
+
static void create(const Nan::FunctionCallbackInfo<v8::Value> &args) noexcept(false) {
|
|
16
|
+
// Note that Args().get may throw.
|
|
17
|
+
BindWrapper<Bound>::createObj(args, Args(args).get(args)...);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
static void createValue(ArgStorage &storage, const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
21
|
+
static_cast<TemplatedArgStorage<Bound> &>(storage).init(Args(args).get(args)...);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
} // namespace
|
|
@@ -0,0 +1,119 @@
|
|
|
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
|
+
// Holds a permanent reference to a JavaScript object, protecting it
|
|
9
|
+
// from garbage collection.
|
|
10
|
+
|
|
11
|
+
class External {
|
|
12
|
+
|
|
13
|
+
public:
|
|
14
|
+
|
|
15
|
+
External() {}
|
|
16
|
+
|
|
17
|
+
explicit External(v8::Local<v8::Object> obj) : handle(obj) {}
|
|
18
|
+
|
|
19
|
+
External(const External &other) : handle(Nan::New(other.handle)) {}
|
|
20
|
+
|
|
21
|
+
External(External &&other) : handle(Nan::New(other.handle)) {
|
|
22
|
+
other.handle.Reset();
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
External &operator=(const External &other) {
|
|
26
|
+
handle.Reset(Nan::New(other.handle));
|
|
27
|
+
|
|
28
|
+
return(*this);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
External &operator=(v8::Local<v8::Object> obj) {
|
|
32
|
+
handle.Reset(obj);
|
|
33
|
+
|
|
34
|
+
return(*this);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
External &operator=(External &&other) {
|
|
38
|
+
handle.Reset(Nan::New(other.handle));
|
|
39
|
+
other.handle.Reset();
|
|
40
|
+
|
|
41
|
+
return(*this);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// Call callback with data when external object is garbage collected.
|
|
45
|
+
|
|
46
|
+
template<typename Data>
|
|
47
|
+
void addDestructor(void callback(Data *), Data *data) {
|
|
48
|
+
new Destructor<Data>(callback, data, handle);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
v8::Local<v8::Object> getHandle() {
|
|
52
|
+
return(Nan::New(handle));
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private:
|
|
56
|
+
|
|
57
|
+
// Destructor info attached to an object for detecting when it gets
|
|
58
|
+
// garbage collected.
|
|
59
|
+
|
|
60
|
+
template<typename Data>
|
|
61
|
+
struct Destructor {
|
|
62
|
+
|
|
63
|
+
Destructor(
|
|
64
|
+
void callback(Data *),
|
|
65
|
+
Data *data,
|
|
66
|
+
const Nan::Persistent<
|
|
67
|
+
v8::Object,
|
|
68
|
+
Nan::CopyablePersistentTraits<v8::Object>
|
|
69
|
+
> &handle
|
|
70
|
+
) : callback(callback), data(data), weak(Nan::New(handle)) {
|
|
71
|
+
weak.SetWeak(
|
|
72
|
+
this,
|
|
73
|
+
&gc,
|
|
74
|
+
Nan::WeakCallbackType::kParameter
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
// PersistentBase::MarkIndependent was removed in modern V8.
|
|
78
|
+
// SetWeak alone is enough: once only the weak reference remains,
|
|
79
|
+
// the garbage collector calls gc() below.
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Called by V8, calls callback and deletes the destructor.
|
|
83
|
+
|
|
84
|
+
static void gc(const Nan::WeakCallbackInfo<Destructor<Data>> &data) {
|
|
85
|
+
Destructor<Data> *arg = data.GetParameter();
|
|
86
|
+
|
|
87
|
+
arg->callback(arg->data);
|
|
88
|
+
|
|
89
|
+
delete arg;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// Callback and its arbitrary data for notifying about destruction
|
|
93
|
+
// of watched object.
|
|
94
|
+
void (*callback)(Data *);
|
|
95
|
+
Data *data;
|
|
96
|
+
|
|
97
|
+
// Hold a weak reference to the object, to detect but not prevent its
|
|
98
|
+
// garbage collection.
|
|
99
|
+
Nan::Persistent<v8::Object> weak;
|
|
100
|
+
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
// CopyablePersistentTraits automatically resets handle in the destructor.
|
|
104
|
+
Nan::Persistent<v8::Object, Nan::CopyablePersistentTraits<v8::Object>> handle;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
template<>
|
|
108
|
+
struct BindingType<External> {
|
|
109
|
+
|
|
110
|
+
typedef External Type;
|
|
111
|
+
|
|
112
|
+
static inline bool checkType(WireType arg) { return(arg->IsObject()); }
|
|
113
|
+
|
|
114
|
+
static inline Type fromWireType(WireType arg) { return(External(Nan::To<v8::Object>(arg).ToLocalChecked())); }
|
|
115
|
+
static inline WireType toWireType(Type arg) { return(arg.getHandle()); }
|
|
116
|
+
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
} // namespace
|
|
@@ -0,0 +1,195 @@
|
|
|
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 Int64 {};
|
|
9
|
+
|
|
10
|
+
template <typename ArgType>
|
|
11
|
+
static ArgType int64FromWire(WireType arg, void(init)(const Nan::FunctionCallbackInfo<v8::Value> &args)) {
|
|
12
|
+
Nan::HandleScope();
|
|
13
|
+
|
|
14
|
+
auto target = Nan::To<v8::Object>(arg).ToLocalChecked();
|
|
15
|
+
auto fromJS = Nan::Get(target, Nan::New<v8::String>("fromJS").ToLocalChecked()).ToLocalChecked();
|
|
16
|
+
|
|
17
|
+
if(!fromJS->IsFunction()) throw(std::runtime_error("Type mismatch"));
|
|
18
|
+
|
|
19
|
+
ArgType storage = 0;
|
|
20
|
+
|
|
21
|
+
// TODO: cache this for a speedup (replace init param).
|
|
22
|
+
auto storageTemplate = Nan::New<v8::ObjectTemplate>();
|
|
23
|
+
storageTemplate->SetInternalFieldCount(1);
|
|
24
|
+
Nan::SetCallAsFunctionHandler(storageTemplate, init);
|
|
25
|
+
|
|
26
|
+
auto instance = Nan::NewInstance(storageTemplate).ToLocalChecked();
|
|
27
|
+
Nan::SetInternalFieldPointer(instance, 0, &storage);
|
|
28
|
+
|
|
29
|
+
// TODO: cache this for a speedup.
|
|
30
|
+
cbFunction converter(v8::Local<v8::Function>::Cast(fromJS));
|
|
31
|
+
converter.callMethod<void>(target, instance);
|
|
32
|
+
|
|
33
|
+
return(storage);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Fast code path for 32-bit types.
|
|
37
|
+
|
|
38
|
+
template <int size> struct Int64Converter {
|
|
39
|
+
template <typename ArgType>
|
|
40
|
+
static inline WireType uint64ToWire(ArgType arg) {
|
|
41
|
+
return(Nan::New<v8::Uint32>(static_cast<uint32_t>(arg)));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
template <typename ArgType>
|
|
45
|
+
static inline WireType int64ToWire(ArgType arg) {
|
|
46
|
+
return(Nan::New<v8::Int32>(static_cast<int32_t>(arg)));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
template <typename ArgType>
|
|
50
|
+
static void uint64Init(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
51
|
+
ArgType &storage = *static_cast<ArgType *>(Nan::GetInternalFieldPointer(args.Holder(), 0));
|
|
52
|
+
|
|
53
|
+
unsigned int argc = args.Length();
|
|
54
|
+
if(argc > 0) storage = Nan::To<uint32_t>(args[0]).FromJust();
|
|
55
|
+
|
|
56
|
+
args.GetReturnValue().Set(Nan::Undefined());
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
template <typename ArgType>
|
|
60
|
+
static void int64Init(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
61
|
+
ArgType &storage = *static_cast<ArgType *>(Nan::GetInternalFieldPointer(args.Holder(), 0));
|
|
62
|
+
|
|
63
|
+
unsigned int argc = args.Length();
|
|
64
|
+
if(argc > 0) storage = Nan::To<uint32_t>(args[0]).FromJust();
|
|
65
|
+
if(argc > 2 && Nan::To<bool>(args[2]).FromJust()) storage = -storage;
|
|
66
|
+
|
|
67
|
+
args.GetReturnValue().Set(Nan::Undefined());
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
// Handle 64-bit types.
|
|
72
|
+
|
|
73
|
+
template<> struct Int64Converter<8> {
|
|
74
|
+
template <typename ArgType>
|
|
75
|
+
static inline WireType uint64ToWire(ArgType arg) {
|
|
76
|
+
if(arg <= 0xffffffffU) {
|
|
77
|
+
// Number fits in uint32_t.
|
|
78
|
+
return(Nan::New<v8::Uint32>(static_cast<uint32_t>(arg)));
|
|
79
|
+
} else if(arg <= 0x20000000000000ULL) {
|
|
80
|
+
// Number fits in double.
|
|
81
|
+
return(Nan::New<v8::Number>(static_cast<double>(arg)));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
cbFunction *jsConstructor = BindClass<Int64>::getInstance().getValueConstructorJS();
|
|
85
|
+
|
|
86
|
+
if(jsConstructor != nullptr) {
|
|
87
|
+
// Construct custom bignum object from high and low 32-bit halves.
|
|
88
|
+
v8::Local<v8::Value> output = Nan::Undefined();
|
|
89
|
+
cbOutput construct(*jsConstructor, &output);
|
|
90
|
+
|
|
91
|
+
construct(
|
|
92
|
+
static_cast<uint32_t>(arg),
|
|
93
|
+
static_cast<uint32_t>(arg >> 32),
|
|
94
|
+
false
|
|
95
|
+
);
|
|
96
|
+
|
|
97
|
+
return(output);
|
|
98
|
+
} else {
|
|
99
|
+
// Int64 JavaScript class is missing or not registered.
|
|
100
|
+
// Just cast to double then.
|
|
101
|
+
return(Nan::New<v8::Number>(static_cast<double>(arg)));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
template <typename ArgType>
|
|
106
|
+
static inline WireType int64ToWire(ArgType arg) {
|
|
107
|
+
if(arg >= -0x80000000 && arg <= 0x7fffffff) {
|
|
108
|
+
return(Nan::New<v8::Int32>(static_cast<int32_t>(arg)));
|
|
109
|
+
} else if(arg >= -0x20000000000000LL && arg <= 0x20000000000000LL) {
|
|
110
|
+
return(Nan::New<v8::Number>(static_cast<double>(arg)));
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
cbFunction *jsConstructor = BindClass<Int64>::getInstance().getValueConstructorJS();
|
|
114
|
+
|
|
115
|
+
if(jsConstructor != nullptr) {
|
|
116
|
+
v8::Local<v8::Value> output = Nan::Undefined();
|
|
117
|
+
cbOutput construct(*jsConstructor, &output);
|
|
118
|
+
|
|
119
|
+
bool sign = arg < 0;
|
|
120
|
+
if(sign) arg = -arg;
|
|
121
|
+
|
|
122
|
+
construct(
|
|
123
|
+
static_cast<uint32_t>(arg),
|
|
124
|
+
static_cast<uint32_t>(static_cast<uint64_t>(arg) >> 32),
|
|
125
|
+
sign
|
|
126
|
+
);
|
|
127
|
+
|
|
128
|
+
return(output);
|
|
129
|
+
} else {
|
|
130
|
+
// Int64 JavaScript class is missing or not registered.
|
|
131
|
+
return(Nan::New<v8::Number>(static_cast<double>(arg)));
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
template <typename ArgType>
|
|
136
|
+
static void uint64Init(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
137
|
+
ArgType &storage = *static_cast<ArgType *>(Nan::GetInternalFieldPointer(args.Holder(), 0));
|
|
138
|
+
|
|
139
|
+
unsigned int argc = args.Length();
|
|
140
|
+
if(argc > 0) storage = Nan::To<uint32_t>(args[0]).FromJust();
|
|
141
|
+
if(argc > 1) storage += static_cast<uint64_t>(Nan::To<uint32_t>(args[1]).FromJust()) << 32;
|
|
142
|
+
|
|
143
|
+
args.GetReturnValue().Set(Nan::Undefined());
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
template <typename ArgType>
|
|
147
|
+
static void int64Init(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
148
|
+
ArgType &storage = *static_cast<ArgType *>(Nan::GetInternalFieldPointer(args.Holder(), 0));
|
|
149
|
+
|
|
150
|
+
unsigned int argc = args.Length();
|
|
151
|
+
if(argc > 0) storage = Nan::To<uint32_t>(args[0]).FromJust();
|
|
152
|
+
if(argc > 1) storage += static_cast<uint64_t>(Nan::To<uint32_t>(args[1]).FromJust()) << 32;
|
|
153
|
+
if(argc > 2 && Nan::To<bool>(args[2]).FromJust()) storage = -storage;
|
|
154
|
+
|
|
155
|
+
args.GetReturnValue().Set(Nan::Undefined());
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
#define DEFINE_INT64_BINDING_TYPE(ArgType, encode, decode) \
|
|
160
|
+
template <> struct BindingType<ArgType> { \
|
|
161
|
+
typedef ArgType Type; \
|
|
162
|
+
\
|
|
163
|
+
static inline bool checkType(WireType arg) { \
|
|
164
|
+
return(true); \
|
|
165
|
+
} \
|
|
166
|
+
\
|
|
167
|
+
static inline WireType toWireType(Type arg) { \
|
|
168
|
+
return(Int64Converter<sizeof(Type)>::encode(arg)); \
|
|
169
|
+
} \
|
|
170
|
+
\
|
|
171
|
+
static inline Type fromWireType(WireType arg) { \
|
|
172
|
+
if(arg->IsObject()) { \
|
|
173
|
+
return(int64FromWire<ArgType>(arg, Int64Converter<sizeof(Type)>::decode<ArgType>)); \
|
|
174
|
+
} else { \
|
|
175
|
+
return(static_cast<Type>(Nan::To<double>(arg).FromJust())); \
|
|
176
|
+
} \
|
|
177
|
+
} \
|
|
178
|
+
}; \
|
|
179
|
+
\
|
|
180
|
+
template <> struct BindingType<StrictType<ArgType>> : public BindingType<ArgType> { \
|
|
181
|
+
static inline bool checkType(WireType arg) { \
|
|
182
|
+
/* TODO: check type of object (bound to Int64?) */ \
|
|
183
|
+
return(arg->IsNumber() || arg->IsObject()); \
|
|
184
|
+
} \
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Handle possibly 64-bit types.
|
|
188
|
+
// Types detected to fit in 32 bits automatically use a faster code path.
|
|
189
|
+
|
|
190
|
+
DEFINE_INT64_BINDING_TYPE(unsigned long, uint64ToWire, uint64Init);
|
|
191
|
+
DEFINE_INT64_BINDING_TYPE(unsigned long long, uint64ToWire, uint64Init);
|
|
192
|
+
DEFINE_INT64_BINDING_TYPE(signed long, int64ToWire, int64Init);
|
|
193
|
+
DEFINE_INT64_BINDING_TYPE(signed long long, int64ToWire, int64Init);
|
|
194
|
+
|
|
195
|
+
} // namespace
|
|
@@ -0,0 +1,63 @@
|
|
|
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
|
+
class NBindID {
|
|
9
|
+
|
|
10
|
+
public:
|
|
11
|
+
|
|
12
|
+
explicit NBindID(TYPEID id);
|
|
13
|
+
NBindID (const NBindID &other);
|
|
14
|
+
NBindID (NBindID &&other);
|
|
15
|
+
~NBindID();
|
|
16
|
+
|
|
17
|
+
NBindID &operator=(const NBindID &) = delete;
|
|
18
|
+
NBindID &operator=(NBindID &&) = delete;
|
|
19
|
+
|
|
20
|
+
const void *getStructure() const;
|
|
21
|
+
StructureType getStructureType() const;
|
|
22
|
+
const char *toString();
|
|
23
|
+
|
|
24
|
+
private:
|
|
25
|
+
|
|
26
|
+
union {
|
|
27
|
+
TYPEID id;
|
|
28
|
+
const void *structure;
|
|
29
|
+
const StructureType *structureType;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const char *name;
|
|
33
|
+
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
class External;
|
|
37
|
+
|
|
38
|
+
template <typename DefaultReturnType>
|
|
39
|
+
class cbWrapper;
|
|
40
|
+
|
|
41
|
+
typedef cbWrapper<void> cbFunction;
|
|
42
|
+
|
|
43
|
+
class NBind {
|
|
44
|
+
|
|
45
|
+
public:
|
|
46
|
+
|
|
47
|
+
static void bind_value(const char *name, cbFunction &func);
|
|
48
|
+
|
|
49
|
+
static void reflect(
|
|
50
|
+
cbFunction &outPrimitive,
|
|
51
|
+
cbFunction &outType,
|
|
52
|
+
cbFunction &outClass,
|
|
53
|
+
cbFunction &outSuper,
|
|
54
|
+
cbFunction &outMethod
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
static External queryType(NBindID type, cbFunction &outTypeDetail);
|
|
58
|
+
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
} // namespace
|
|
62
|
+
|
|
63
|
+
#define nbind_debug()
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
class cbOutput {
|
|
11
|
+
|
|
12
|
+
template<typename ArgType>
|
|
13
|
+
friend struct BindingType;
|
|
14
|
+
|
|
15
|
+
public:
|
|
16
|
+
|
|
17
|
+
cbOutput(cbFunction &jsConstructor, v8::Local<v8::Value> *output) :
|
|
18
|
+
jsConstructor(jsConstructor), output(output) {}
|
|
19
|
+
|
|
20
|
+
// This overload is identical to cbFunction.
|
|
21
|
+
template<typename... Args>
|
|
22
|
+
void operator()(Args&&... args) {
|
|
23
|
+
call<void>(args...);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
template <typename ReturnType, typename... Args>
|
|
27
|
+
void call(Args... args);
|
|
28
|
+
|
|
29
|
+
private:
|
|
30
|
+
|
|
31
|
+
cbFunction &jsConstructor;
|
|
32
|
+
v8::Local<v8::Value> *output;
|
|
33
|
+
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
} // namespace
|