@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,63 @@
|
|
|
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 is very similar to MethodSignature.h and AccessorSignature.h
|
|
5
|
+
// so modify them together.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "BaseSignature.h"
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
// Wrapper for all C++ functions with matching argument and return types.
|
|
14
|
+
|
|
15
|
+
template <typename PtrType, class Bound, typename PolicyList, typename ReturnType, typename... Args>
|
|
16
|
+
class FunctionSignature : public TemplatedBaseSignature<FunctionSignature<PtrType, Bound, PolicyList, ReturnType, Args...>, PolicyList, ReturnType, Args...> {
|
|
17
|
+
|
|
18
|
+
public:
|
|
19
|
+
|
|
20
|
+
typedef PtrType MethodType;
|
|
21
|
+
|
|
22
|
+
typedef TemplatedBaseSignature<FunctionSignature, PolicyList, ReturnType, Args...> Parent;
|
|
23
|
+
|
|
24
|
+
static constexpr auto typeExpr = BaseSignature :: SignatureType :: func;
|
|
25
|
+
|
|
26
|
+
static inline funcPtr getDirect(MethodType func) {
|
|
27
|
+
return(reinterpret_cast<funcPtr>(func));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
31
|
+
|
|
32
|
+
template <typename V8Args, typename NanArgs>
|
|
33
|
+
static void callInner(const typename Parent::MethodInfo &method, V8Args &args, NanArgs &nanArgs, void *) {
|
|
34
|
+
nanArgs.GetReturnValue().Set(Parent::CallWrapper::callFunction(
|
|
35
|
+
method.func,
|
|
36
|
+
args
|
|
37
|
+
));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
static void call(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
41
|
+
Parent::template callInnerSafely<void>(
|
|
42
|
+
args,
|
|
43
|
+
args,
|
|
44
|
+
SignatureParam::get(args)->methodNum
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
#elif defined(__EMSCRIPTEN__)
|
|
49
|
+
|
|
50
|
+
static typename TypeTransformer<ReturnType, PolicyList>::Binding::WireType call(
|
|
51
|
+
uint32_t num,
|
|
52
|
+
typename TypeTransformer<Args, PolicyList>::Binding::WireType... args
|
|
53
|
+
) {
|
|
54
|
+
auto func = Parent::getMethod(num).func;
|
|
55
|
+
|
|
56
|
+
return(Caller<PolicyList, ReturnType, Args...>::callFunction(func, args...));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#endif // BUILDING_NODE_EXTENSION, __EMSCRIPTEN__
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
} // namespace
|
|
@@ -0,0 +1,62 @@
|
|
|
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 is very similar to FunctionSignature.h and MethodSignature.h
|
|
5
|
+
// so modify them together.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "BaseSignature.h"
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
// Wrapper for all C++ getters and setters with matching class and data types.
|
|
14
|
+
|
|
15
|
+
template <typename PtrType, class Bound, typename PolicyList, typename ReturnType, typename... Args>
|
|
16
|
+
class GetterSignature : public TemplatedBaseSignature<GetterSignature<PtrType, Bound, PolicyList, ReturnType, Args...>, PolicyList, ReturnType, Args...> {
|
|
17
|
+
|
|
18
|
+
public:
|
|
19
|
+
|
|
20
|
+
typedef PtrType MethodType;
|
|
21
|
+
|
|
22
|
+
typedef TemplatedBaseSignature<GetterSignature, PolicyList, ReturnType, Args...> Parent;
|
|
23
|
+
|
|
24
|
+
static constexpr auto typeExpr = BaseSignature :: SignatureType :: getter;
|
|
25
|
+
|
|
26
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
27
|
+
|
|
28
|
+
template <typename V8Args, typename NanArgs>
|
|
29
|
+
static void callInner(const typename Parent::MethodInfo &method, V8Args &args, NanArgs &nanArgs, Bound *target) {
|
|
30
|
+
nanArgs.GetReturnValue().Set(Parent::CallWrapper::callMethod(
|
|
31
|
+
*target,
|
|
32
|
+
method.func,
|
|
33
|
+
args
|
|
34
|
+
));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static void call(v8::Local<v8::String> property, const Nan::PropertyCallbackInfo<v8::Value> &args) {
|
|
38
|
+
// Note: this may do useless arity checks...
|
|
39
|
+
Parent::template callInnerSafely<Bound>(
|
|
40
|
+
args,
|
|
41
|
+
args,
|
|
42
|
+
SignatureParam::get(args)->getterNum
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
#elif defined(__EMSCRIPTEN__)
|
|
47
|
+
|
|
48
|
+
static typename TypeTransformer<ReturnType, PolicyList>::Binding::WireType call(
|
|
49
|
+
uint32_t num,
|
|
50
|
+
Bound *target,
|
|
51
|
+
typename TypeTransformer<Args, PolicyList>::Binding::WireType... args
|
|
52
|
+
) {
|
|
53
|
+
auto method = Parent::getMethod(num).func;
|
|
54
|
+
|
|
55
|
+
return(Caller<PolicyList, ReturnType, Args...>::callMethod(*target, method, args...));
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
#endif // BUILDING_NODE_EXTENSION, __EMSCRIPTEN__
|
|
59
|
+
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
} // namespace
|
|
@@ -0,0 +1,61 @@
|
|
|
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 is very similar to FunctionSignature.h and AccessorSignature.h
|
|
5
|
+
// so modify them together.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "BaseSignature.h"
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
// Wrapper for all C++ methods with matching class, argument and return types.
|
|
14
|
+
|
|
15
|
+
template <typename PtrType, class Bound, typename PolicyList, typename ReturnType, typename... Args>
|
|
16
|
+
class MethodSignature : public TemplatedBaseSignature<MethodSignature<PtrType, Bound, PolicyList, ReturnType, Args...>, PolicyList, ReturnType, Args...> {
|
|
17
|
+
|
|
18
|
+
public:
|
|
19
|
+
|
|
20
|
+
typedef PtrType MethodType;
|
|
21
|
+
|
|
22
|
+
typedef TemplatedBaseSignature<MethodSignature, PolicyList, ReturnType, Args...> Parent;
|
|
23
|
+
|
|
24
|
+
static constexpr auto typeExpr = BaseSignature :: SignatureType :: method;
|
|
25
|
+
|
|
26
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
27
|
+
|
|
28
|
+
template <typename V8Args, typename NanArgs>
|
|
29
|
+
static void callInner(const typename Parent::MethodInfo &method, V8Args &args, NanArgs &nanArgs, Bound *target) {
|
|
30
|
+
nanArgs.GetReturnValue().Set(Parent::CallWrapper::callMethod(
|
|
31
|
+
*target,
|
|
32
|
+
method.func,
|
|
33
|
+
args
|
|
34
|
+
));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static void call(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
38
|
+
Parent::template callInnerSafely<Bound>(
|
|
39
|
+
args,
|
|
40
|
+
args,
|
|
41
|
+
SignatureParam::get(args)->methodNum
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
#elif defined(__EMSCRIPTEN__)
|
|
46
|
+
|
|
47
|
+
static typename TypeTransformer<ReturnType, PolicyList>::Binding::WireType call(
|
|
48
|
+
uint32_t num,
|
|
49
|
+
Bound *target,
|
|
50
|
+
typename TypeTransformer<Args, PolicyList>::Binding::WireType... args
|
|
51
|
+
) {
|
|
52
|
+
auto method = Parent::getMethod(num).func;
|
|
53
|
+
|
|
54
|
+
return(Caller<PolicyList, ReturnType, Args...>::callMethod(*target, method, args...));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
#endif // BUILDING_NODE_EXTENSION, __EMSCRIPTEN__
|
|
58
|
+
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
} // 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
|
+
// This file is very similar to FunctionSignature.h and MethodSignature.h
|
|
5
|
+
// so modify them together.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include "BaseSignature.h"
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
// Wrapper for all C++ getters and setters with matching class and data types.
|
|
14
|
+
|
|
15
|
+
template <typename PtrType, class Bound, typename PolicyList, typename ReturnType, typename... Args>
|
|
16
|
+
class SetterSignature : public TemplatedBaseSignature<SetterSignature<PtrType, Bound, PolicyList, ReturnType, Args...>, PolicyList, ReturnType, Args...> {
|
|
17
|
+
|
|
18
|
+
public:
|
|
19
|
+
|
|
20
|
+
typedef PtrType MethodType;
|
|
21
|
+
|
|
22
|
+
typedef TemplatedBaseSignature<SetterSignature, PolicyList, ReturnType, Args...> Parent;
|
|
23
|
+
|
|
24
|
+
static constexpr auto typeExpr = BaseSignature :: SignatureType :: setter;
|
|
25
|
+
|
|
26
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
27
|
+
|
|
28
|
+
template <typename V8Args, typename NanArgs>
|
|
29
|
+
static void callInner(const typename Parent::MethodInfo &method, V8Args &args, NanArgs &nanArgs, Bound *target) {
|
|
30
|
+
Parent::CallWrapper::callMethod(
|
|
31
|
+
*target,
|
|
32
|
+
method.func,
|
|
33
|
+
args
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
static void call(v8::Local<v8::String> property, v8::Local<v8::Value> value, const Nan::PropertyCallbackInfo<void> &args) {
|
|
38
|
+
auto *valuePtr = &value;
|
|
39
|
+
|
|
40
|
+
Parent::template callInnerSafely<Bound>(
|
|
41
|
+
valuePtr,
|
|
42
|
+
args,
|
|
43
|
+
SignatureParam::get(args)->setterNum
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
#elif defined(__EMSCRIPTEN__)
|
|
48
|
+
|
|
49
|
+
static typename TypeTransformer<ReturnType, PolicyList>::Binding::WireType call(
|
|
50
|
+
uint32_t num,
|
|
51
|
+
Bound *target,
|
|
52
|
+
typename TypeTransformer<Args, PolicyList>::Binding::WireType... args
|
|
53
|
+
) {
|
|
54
|
+
auto method = Parent::getMethod(num).func;
|
|
55
|
+
|
|
56
|
+
return(Caller<PolicyList, ReturnType, Args...>::callMethod(*target, method, args...));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
#endif // BUILDING_NODE_EXTENSION, __EMSCRIPTEN__
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
} // namespace
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
9
|
+
|
|
10
|
+
struct SignatureParam {
|
|
11
|
+
|
|
12
|
+
template <typename InfoType>
|
|
13
|
+
static SignatureParam *get(const InfoType &args) {
|
|
14
|
+
return(
|
|
15
|
+
static_cast<SignatureParam *>(
|
|
16
|
+
v8::Local<v8::External>::Cast(
|
|
17
|
+
args.Data()
|
|
18
|
+
)->Value()
|
|
19
|
+
)
|
|
20
|
+
);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
union {
|
|
24
|
+
// Distinguish methods with identical signatures.
|
|
25
|
+
unsigned int methodNum = 0;
|
|
26
|
+
unsigned int getterNum;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
union {
|
|
30
|
+
// Distinguish overloaded method signatures with matching names.
|
|
31
|
+
unsigned int overloadNum = 0;
|
|
32
|
+
unsigned int setterNum;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
#endif // BUILDING_NODE_EXTENSION
|
|
38
|
+
|
|
39
|
+
} // namespace
|
|
@@ -0,0 +1,69 @@
|
|
|
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
|
+
// ArgFromWire converts JavaScript types into C++ types, usually with BindingType<>::fromWireType
|
|
9
|
+
// but some types require additional temporary storage, such as a string converted to C style.
|
|
10
|
+
// FromWire is a struct, so wrappers for all objects can be constructed as function arguments,
|
|
11
|
+
// and their actual values passed to the called function are returned by the get() function.
|
|
12
|
+
// The wrappers go out of scope and are destroyed at the end of the function call.
|
|
13
|
+
|
|
14
|
+
// Handle most C++ types.
|
|
15
|
+
|
|
16
|
+
template<typename PolicyList, size_t Index, typename ArgType>
|
|
17
|
+
struct ArgFromWire {
|
|
18
|
+
|
|
19
|
+
typedef TypeTransformer<ArgType, PolicyList> Transformed;
|
|
20
|
+
|
|
21
|
+
template <typename NanArgs>
|
|
22
|
+
ArgFromWire(const NanArgs &args) {}
|
|
23
|
+
|
|
24
|
+
template <typename NanArgs>
|
|
25
|
+
inline typename Transformed::Type get(const NanArgs &args) noexcept(false) {
|
|
26
|
+
return(Transformed::Binding::fromWireType(args[Index]));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Handle char pointers, which will receive a C string representation of any JavaScript value.
|
|
32
|
+
|
|
33
|
+
template<typename PolicyList, size_t Index>
|
|
34
|
+
struct ArgFromWire<PolicyList, Index, const char *> {
|
|
35
|
+
|
|
36
|
+
template <typename NanArgs>
|
|
37
|
+
ArgFromWire(const NanArgs &args) : val(args[Index]) {}
|
|
38
|
+
|
|
39
|
+
template <typename NanArgs>
|
|
40
|
+
inline const char *get(const NanArgs &args) {
|
|
41
|
+
return(*val);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// RAII style storage for the string data.
|
|
45
|
+
|
|
46
|
+
Nan::Utf8String val;
|
|
47
|
+
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
// Automatically cast char to unsigned if the C++ function expects it.
|
|
51
|
+
|
|
52
|
+
template<typename PolicyList, size_t Index>
|
|
53
|
+
struct ArgFromWire<PolicyList, Index, const unsigned char *> {
|
|
54
|
+
|
|
55
|
+
template <typename NanArgs>
|
|
56
|
+
ArgFromWire(const NanArgs &args) : val(args[Index]) {}
|
|
57
|
+
|
|
58
|
+
template <typename NanArgs>
|
|
59
|
+
inline const unsigned char *get(const NanArgs &args) {
|
|
60
|
+
return(reinterpret_cast<const unsigned char *>(*val));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// RAII style storage for the string data.
|
|
64
|
+
|
|
65
|
+
Nan::Utf8String val;
|
|
66
|
+
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
} // namespace
|
|
@@ -0,0 +1,295 @@
|
|
|
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
|
+
#include <memory>
|
|
7
|
+
|
|
8
|
+
#if !defined(NBIND_DUPLICATE_POINTERS)
|
|
9
|
+
|
|
10
|
+
#include <unordered_map>
|
|
11
|
+
|
|
12
|
+
namespace nbind {
|
|
13
|
+
|
|
14
|
+
template <typename A, typename B>
|
|
15
|
+
class HashablePair : public std::pair<A, B> {
|
|
16
|
+
|
|
17
|
+
public:
|
|
18
|
+
|
|
19
|
+
HashablePair(A a, B b) : std::pair<A, B>(a, b) {}
|
|
20
|
+
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
namespace std {
|
|
26
|
+
|
|
27
|
+
// Add hash functions for HashablePair and TypeFlags so they can be used
|
|
28
|
+
// as keys in std::unordered_map.
|
|
29
|
+
|
|
30
|
+
template<typename A, typename B> struct hash<nbind::HashablePair<A, B>> {
|
|
31
|
+
inline size_t operator()(const nbind::HashablePair<A, B> &obj) const {
|
|
32
|
+
size_t result = hash<A>()(obj.first) + 0x9e3779b9;
|
|
33
|
+
|
|
34
|
+
result = (result << 6) + (result >> 2);
|
|
35
|
+
result += hash<B>()(obj.second) + 0x9e3779b9;
|
|
36
|
+
|
|
37
|
+
return(result);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
template<>
|
|
42
|
+
struct hash<nbind::TypeFlags> {
|
|
43
|
+
inline size_t operator()(nbind::TypeFlags flags) const {
|
|
44
|
+
return(hash<uint32_t>()(static_cast<uint32_t>(flags)));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
#endif // NBIND_DUPLICATE_POINTERS
|
|
51
|
+
|
|
52
|
+
#include <v8.h>
|
|
53
|
+
#include <node.h>
|
|
54
|
+
#include <node_buffer.h>
|
|
55
|
+
#include <nan.h>
|
|
56
|
+
|
|
57
|
+
namespace nbind {
|
|
58
|
+
|
|
59
|
+
class BindClassBase;
|
|
60
|
+
|
|
61
|
+
class BindWrapperBase : public node::ObjectWrap {
|
|
62
|
+
|
|
63
|
+
public:
|
|
64
|
+
|
|
65
|
+
BindWrapperBase(void *bound, TypeFlags flags, BindClassBase &bindClass) :
|
|
66
|
+
boundUnsafe(bound), flags(flags), bindClass(bindClass) {}
|
|
67
|
+
|
|
68
|
+
inline void testTarget(TypeFlags argFlags) {
|
|
69
|
+
if(!!(flags & TypeFlags::isConst) && !(argFlags & TypeFlags::isConst)) {
|
|
70
|
+
throw(std::runtime_error(
|
|
71
|
+
!!(argFlags & TypeFlags::isMethod) ?
|
|
72
|
+
"Calling a non-const method on a const object" :
|
|
73
|
+
"Passing a const value as a non-const argument"
|
|
74
|
+
));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if(boundUnsafe == nullptr) {
|
|
78
|
+
throw(std::runtime_error("Attempt to access deleted object"));
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
#if !defined(NBIND_DUPLICATE_POINTERS)
|
|
83
|
+
|
|
84
|
+
static Nan::Persistent<v8::Object> *findInstance(const void *ptr, TypeFlags flags) {
|
|
85
|
+
// This will insert a null pointer (to a non-existent wrapper)
|
|
86
|
+
// in the map if the object pointer is not found yet.
|
|
87
|
+
|
|
88
|
+
return(&getInstanceTbl()[HashablePair<const void *, TypeFlags>(ptr, flags)]);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
#endif // NBIND_DUPLICATE_POINTERS
|
|
92
|
+
|
|
93
|
+
BindClassBase &getClass() { return(bindClass); }
|
|
94
|
+
|
|
95
|
+
template <class Bound>
|
|
96
|
+
Bound *upcast();
|
|
97
|
+
|
|
98
|
+
protected:
|
|
99
|
+
|
|
100
|
+
void wrapThis(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
101
|
+
|
|
102
|
+
# if !defined(NBIND_DUPLICATE_POINTERS)
|
|
103
|
+
|
|
104
|
+
addInstance(args.This());
|
|
105
|
+
|
|
106
|
+
# endif // NBIND_DUPLICATE_POINTERS
|
|
107
|
+
|
|
108
|
+
this->Wrap(args.This());
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
#if !defined(NBIND_DUPLICATE_POINTERS)
|
|
112
|
+
|
|
113
|
+
// If the GC wants to free the wrapper object, get rid of our reference to it.
|
|
114
|
+
|
|
115
|
+
static void weakCallback(const Nan::WeakCallbackInfo<Nan::Persistent<v8::Object>> &data) {
|
|
116
|
+
// This causes a crash. Maybe it's a reference counter that already got decremented?
|
|
117
|
+
// data.GetParameter()->Reset();
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Add a mapping from a pointer to a wrapper object,
|
|
121
|
+
// to re-use the same wrapper for duplicates of the same pointer.
|
|
122
|
+
|
|
123
|
+
void addInstance(v8::Local<v8::Object> obj) {
|
|
124
|
+
Nan::Persistent<v8::Object> *ref = &getInstanceTbl()[
|
|
125
|
+
HashablePair<const void *, TypeFlags>(
|
|
126
|
+
boundUnsafe,
|
|
127
|
+
flags
|
|
128
|
+
)
|
|
129
|
+
];
|
|
130
|
+
|
|
131
|
+
ref->Reset(obj);
|
|
132
|
+
|
|
133
|
+
// Mark the reference to the wrapper weak, so it can be
|
|
134
|
+
// garbage collected when no copies remain.
|
|
135
|
+
// If the same pointer needs wrapping later, we can just
|
|
136
|
+
// create a new wrapper again.
|
|
137
|
+
|
|
138
|
+
ref->SetWeak(ref, weakCallback, Nan::WeakCallbackType::kParameter);
|
|
139
|
+
// PersistentBase::MarkIndependent was removed in modern V8.
|
|
140
|
+
// The weak reference set above plus removeInstance() called from the
|
|
141
|
+
// wrapper destructor are enough for garbage collection bookkeeping:
|
|
142
|
+
// once the wrapper is collected or destroyed, a new wrapper can be
|
|
143
|
+
// created again for the same C++ pointer.
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
void removeInstance() {
|
|
147
|
+
// This causes a crash. Maybe it's a reference counter that already got decremented?
|
|
148
|
+
// auto iter = getInstanceTbl().find(bound.get());
|
|
149
|
+
// (*iter).second.Reset();
|
|
150
|
+
|
|
151
|
+
getInstanceTbl().erase(
|
|
152
|
+
HashablePair<const void *, TypeFlags>(
|
|
153
|
+
boundUnsafe,
|
|
154
|
+
flags
|
|
155
|
+
)
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// This is effectively a map from C++ instance pointers
|
|
160
|
+
// to weak references to JavaScript objects wrapping them.
|
|
161
|
+
|
|
162
|
+
static std::unordered_map<
|
|
163
|
+
HashablePair<const void *, TypeFlags>,
|
|
164
|
+
Nan::Persistent<v8::Object>
|
|
165
|
+
> &getInstanceTbl() {
|
|
166
|
+
static std::unordered_map<
|
|
167
|
+
HashablePair<const void *, TypeFlags>,
|
|
168
|
+
Nan::Persistent<v8::Object>
|
|
169
|
+
> instanceTbl;
|
|
170
|
+
|
|
171
|
+
return(instanceTbl);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#endif // NBIND_DUPLICATE_POINTERS
|
|
175
|
+
|
|
176
|
+
void *boundUnsafe;
|
|
177
|
+
TypeFlags flags;
|
|
178
|
+
|
|
179
|
+
BindClassBase &bindClass;
|
|
180
|
+
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
// BindWrapper encapsulates a C++ object created in Node.js.
|
|
184
|
+
|
|
185
|
+
template <class Bound>
|
|
186
|
+
class BindWrapper : public BindWrapperBase {
|
|
187
|
+
|
|
188
|
+
public:
|
|
189
|
+
|
|
190
|
+
BindWrapper(Bound *bound, TypeFlags flags) :
|
|
191
|
+
BindWrapperBase(bound, flags, getBindClass()) {}
|
|
192
|
+
|
|
193
|
+
BindWrapper(std::shared_ptr<Bound> bound, TypeFlags flags) :
|
|
194
|
+
BindWrapperBase(bound.get(), flags, getBindClass()), boundShared(bound) {}
|
|
195
|
+
|
|
196
|
+
// This destructor is called automatically by the JavaScript garbage collector.
|
|
197
|
+
|
|
198
|
+
~BindWrapper() {
|
|
199
|
+
destroy();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
// Calls BindClass<Bound>::getInstance();
|
|
203
|
+
// We don't want to depend on BindClass.h here.
|
|
204
|
+
|
|
205
|
+
static BindClassBase &getBindClass();
|
|
206
|
+
|
|
207
|
+
// Pass any constructor arguments to wrapped class.
|
|
208
|
+
template<typename... Args>
|
|
209
|
+
static void createObj(
|
|
210
|
+
const Nan::FunctionCallbackInfo<v8::Value> &nanArgs,
|
|
211
|
+
Args&&... args
|
|
212
|
+
) {
|
|
213
|
+
(new BindWrapper(
|
|
214
|
+
std::make_shared<Bound>(args...),
|
|
215
|
+
TypeFlags::isSharedPtr
|
|
216
|
+
))->wrapThis(nanArgs);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
static void wrapPtr(const Nan::FunctionCallbackInfo<v8::Value> &nanArgs) {
|
|
220
|
+
auto flags = static_cast<TypeFlags>(Nan::To<uint32_t>(nanArgs[1]).FromJust());
|
|
221
|
+
void *ptr = v8::Local<v8::External>::Cast(nanArgs[0])->Value();
|
|
222
|
+
|
|
223
|
+
if((flags & TypeFlags::refMask) == TypeFlags::isSharedPtr) {
|
|
224
|
+
auto *ptrShared = static_cast<std::shared_ptr<Bound> *>(ptr);
|
|
225
|
+
|
|
226
|
+
(new BindWrapper(*ptrShared, flags))->wrapThis(nanArgs);
|
|
227
|
+
|
|
228
|
+
// Delete temporary shared pointer after re-referencing target object.
|
|
229
|
+
delete ptrShared;
|
|
230
|
+
} else {
|
|
231
|
+
auto *ptrUnsafe = static_cast<Bound *>(ptr);
|
|
232
|
+
|
|
233
|
+
(new BindWrapper(ptrUnsafe, flags))->wrapThis(nanArgs);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
void destroy() {
|
|
238
|
+
|
|
239
|
+
// Avoid freeing the object twice.
|
|
240
|
+
if(!boundUnsafe) return;
|
|
241
|
+
|
|
242
|
+
# if !defined(NBIND_DUPLICATE_POINTERS)
|
|
243
|
+
|
|
244
|
+
// JavaScript side no longer holds any references to the object,
|
|
245
|
+
// so remove our weak pointer to the wrapper.
|
|
246
|
+
|
|
247
|
+
removeInstance();
|
|
248
|
+
|
|
249
|
+
# endif // NBIND_DUPLICATE_POINTERS
|
|
250
|
+
|
|
251
|
+
// Delete the bound object if the C++ side isn't holding onto it.
|
|
252
|
+
// The weak pointer must be removed first,
|
|
253
|
+
// because resetting changes the hash key.
|
|
254
|
+
|
|
255
|
+
boundUnsafe = nullptr;
|
|
256
|
+
boundShared.reset();
|
|
257
|
+
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
static void testInstance(v8::Local<v8::Object> arg);
|
|
261
|
+
|
|
262
|
+
static Bound *getBound(v8::Local<v8::Object> arg, TypeFlags argFlags) {
|
|
263
|
+
BindWrapper::testInstance(arg);
|
|
264
|
+
|
|
265
|
+
BindWrapperBase *wrapper = node::ObjectWrap::Unwrap<BindWrapperBase>(arg);
|
|
266
|
+
|
|
267
|
+
wrapper->testTarget(argFlags);
|
|
268
|
+
|
|
269
|
+
Bound *ptr = wrapper->upcast<Bound>();
|
|
270
|
+
|
|
271
|
+
if(!ptr) throw(std::runtime_error("Type mismatch"));
|
|
272
|
+
|
|
273
|
+
return(ptr);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
// TODO: this should throw or never get called if bound is not shared!
|
|
277
|
+
|
|
278
|
+
static std::shared_ptr<Bound> getShared(v8::Local<v8::Object> arg, TypeFlags argFlags) {
|
|
279
|
+
BindWrapper::testInstance(arg);
|
|
280
|
+
|
|
281
|
+
BindWrapper<Bound> *wrapper = node::ObjectWrap::Unwrap<BindWrapper<Bound>>(arg);
|
|
282
|
+
|
|
283
|
+
wrapper->testTarget(argFlags);
|
|
284
|
+
|
|
285
|
+
// TODO: upcast!
|
|
286
|
+
return(wrapper->boundShared);
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
private:
|
|
290
|
+
|
|
291
|
+
std::shared_ptr<Bound> boundShared;
|
|
292
|
+
|
|
293
|
+
};
|
|
294
|
+
|
|
295
|
+
} // namespace
|