@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
|
+
#pragma once
|
|
5
|
+
|
|
6
|
+
namespace nbind {
|
|
7
|
+
|
|
8
|
+
class cbOutput;
|
|
9
|
+
|
|
10
|
+
class NBindID {
|
|
11
|
+
|
|
12
|
+
public:
|
|
13
|
+
|
|
14
|
+
explicit NBindID(TYPEID id);
|
|
15
|
+
explicit NBindID(uintptr_t ptr);
|
|
16
|
+
|
|
17
|
+
const void *getStructure() const;
|
|
18
|
+
StructureType getStructureType() const;
|
|
19
|
+
void toJS(cbOutput output) const;
|
|
20
|
+
|
|
21
|
+
private:
|
|
22
|
+
|
|
23
|
+
union {
|
|
24
|
+
TYPEID id;
|
|
25
|
+
const void *structure;
|
|
26
|
+
const StructureType *structureType;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
class External;
|
|
32
|
+
|
|
33
|
+
template <typename DefaultReturnType>
|
|
34
|
+
class cbWrapper;
|
|
35
|
+
|
|
36
|
+
typedef cbWrapper<void> cbFunction;
|
|
37
|
+
|
|
38
|
+
class NBind {
|
|
39
|
+
|
|
40
|
+
public:
|
|
41
|
+
|
|
42
|
+
static void bind_value(const char *name, cbFunction &func);
|
|
43
|
+
|
|
44
|
+
static void reflect(
|
|
45
|
+
cbFunction &outPrimitive,
|
|
46
|
+
cbFunction &outType,
|
|
47
|
+
cbFunction &outClass,
|
|
48
|
+
cbFunction &outSuper,
|
|
49
|
+
cbFunction &outMethod
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
static External queryType(NBindID type, cbFunction &outTypeDetail);
|
|
53
|
+
|
|
54
|
+
static uintptr_t lalloc(size_t size);
|
|
55
|
+
static void lreset(unsigned int used, uintptr_t page);
|
|
56
|
+
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
} // namespace
|
|
60
|
+
|
|
61
|
+
extern "C" {
|
|
62
|
+
extern void nbind_debug(void);
|
|
63
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
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 Pool {
|
|
9
|
+
|
|
10
|
+
public:
|
|
11
|
+
|
|
12
|
+
// TODO: testbench should try values 8 and 65536 here.
|
|
13
|
+
static const unsigned int pageSize = 65536;
|
|
14
|
+
static unsigned int used;
|
|
15
|
+
static unsigned char *rootPage;
|
|
16
|
+
static unsigned char *page;
|
|
17
|
+
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
class PoolRestore {
|
|
21
|
+
|
|
22
|
+
public:
|
|
23
|
+
|
|
24
|
+
PoolRestore();
|
|
25
|
+
~PoolRestore();
|
|
26
|
+
|
|
27
|
+
private:
|
|
28
|
+
|
|
29
|
+
unsigned int used;
|
|
30
|
+
unsigned char *page;
|
|
31
|
+
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
} // namespace
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// This file is part of nbind, copyright (C) 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 <functional>
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
#define NBIND_DEFINE_CALLBACK_TYPE(ArgType) \
|
|
14
|
+
template <typename ReturnType, typename... Args> \
|
|
15
|
+
struct BindingType<ArgType> { \
|
|
16
|
+
\
|
|
17
|
+
typedef ArgType Type; \
|
|
18
|
+
\
|
|
19
|
+
typedef unsigned int WireType; \
|
|
20
|
+
\
|
|
21
|
+
}; \
|
|
22
|
+
\
|
|
23
|
+
template<typename PolicyList, typename ReturnType, typename... Args> \
|
|
24
|
+
struct ArgFromWire<PolicyList, ArgType> { \
|
|
25
|
+
\
|
|
26
|
+
explicit ArgFromWire(unsigned int num) : val(cbWrapper<ReturnType>(num)) {} \
|
|
27
|
+
\
|
|
28
|
+
inline ArgType get(unsigned int num) { \
|
|
29
|
+
return(val); \
|
|
30
|
+
} \
|
|
31
|
+
\
|
|
32
|
+
std::function<ReturnType (Args...)> val; \
|
|
33
|
+
\
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
NBIND_DEFINE_CALLBACK_TYPE(std::function<ReturnType (Args...)>);
|
|
37
|
+
NBIND_DEFINE_CALLBACK_TYPE(std::function<ReturnType (Args...)> &);
|
|
38
|
+
NBIND_DEFINE_CALLBACK_TYPE(std::function<ReturnType (Args...)> const &);
|
|
39
|
+
|
|
40
|
+
} // namespace
|
|
@@ -0,0 +1,54 @@
|
|
|
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 value objects, which are represented by equivalent C++ and
|
|
5
|
+
// JavaScript classes, with toJS and fromJS methods calling each others'
|
|
6
|
+
// constructors to marshal the class between languages and providing a similar
|
|
7
|
+
// API in both.
|
|
8
|
+
|
|
9
|
+
#pragma once
|
|
10
|
+
|
|
11
|
+
namespace nbind {
|
|
12
|
+
|
|
13
|
+
extern "C" {
|
|
14
|
+
extern unsigned int _nbind_get_value_object(unsigned int index, ArgStorage *storage);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
template<> struct BindingType<cbOutput::CreateValue> {
|
|
18
|
+
|
|
19
|
+
typedef int Type;
|
|
20
|
+
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
template <typename ArgType>
|
|
24
|
+
inline auto BindingType<ValueType<ArgType>>::toWireType(ArgType &&arg) -> WireType {
|
|
25
|
+
cbFunction *jsConstructor = BindClass<
|
|
26
|
+
typename std::remove_const<ObjType>::type
|
|
27
|
+
>::getInstance().getValueConstructorJS();
|
|
28
|
+
|
|
29
|
+
if(jsConstructor != nullptr) {
|
|
30
|
+
cbOutput construct(*jsConstructor);
|
|
31
|
+
|
|
32
|
+
arg.toJS(construct);
|
|
33
|
+
|
|
34
|
+
return(reinterpret_cast<WireType>(construct.getSlot()));
|
|
35
|
+
} else {
|
|
36
|
+
// Value type JavaScript class is missing or not registered.
|
|
37
|
+
return(BindingType<ObjType>::toWireType(std::forward<ArgType>(arg)));
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
template <typename ArgType>
|
|
42
|
+
inline ArgType BindingType<ValueType<ArgType>>::fromWireType(WireType arg) {
|
|
43
|
+
uintptr_t index = reinterpret_cast<int>(arg);
|
|
44
|
+
if(index & 1) {
|
|
45
|
+
// Constructor argument is an unused dummy value.
|
|
46
|
+
TemplatedArgStorage<ArgType> storage(0);
|
|
47
|
+
|
|
48
|
+
_nbind_get_value_object(index, &storage);
|
|
49
|
+
|
|
50
|
+
return(storage.getBound());
|
|
51
|
+
} else return(*reinterpret_cast<Type *>(arg));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
} // namespace
|
|
@@ -0,0 +1,19 @@
|
|
|
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
|
+
#define NBIND_MULTIMETHOD(name, args, ...) definer.overloaded args ().method(name, ## __VA_ARGS__)
|
|
7
|
+
|
|
8
|
+
#include "noconflict.h"
|
|
9
|
+
|
|
10
|
+
#define function NBIND_FUNCTION
|
|
11
|
+
#define multifunction NBIND_MULTIFUNCTION
|
|
12
|
+
#define method(...) NBIND_EXPAND(NBIND_METHOD(__VA_ARGS__))
|
|
13
|
+
#define inherit(...) NBIND_INHERIT(__VA_ARGS__)
|
|
14
|
+
#define args NBIND_ARGS
|
|
15
|
+
#define multimethod NBIND_MULTIMETHOD
|
|
16
|
+
#define construct NBIND_CONSTRUCT
|
|
17
|
+
#define field(...) NBIND_FIELD(__VA_ARGS__)
|
|
18
|
+
#define getter NBIND_GETTER
|
|
19
|
+
#define getset NBIND_GETSET
|
|
@@ -0,0 +1,91 @@
|
|
|
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
|
+
#if defined(BUILDING_NODE_EXTENSION) || defined(__EMSCRIPTEN__)
|
|
7
|
+
|
|
8
|
+
#include "BindDefiner.h"
|
|
9
|
+
#include "FunctionDefiner.h"
|
|
10
|
+
|
|
11
|
+
// Support overloading macros by number of arguments.
|
|
12
|
+
// See http://stackoverflow.com/a/16683147/16509
|
|
13
|
+
|
|
14
|
+
// VS C++ workaround.
|
|
15
|
+
#define NBIND_EXPAND(args) args
|
|
16
|
+
|
|
17
|
+
// _1, _2... are dummy parameters to discard original macro arguments.
|
|
18
|
+
#define NBIND_SIZE_HELPER(_6, _5, _4, _3, _2, _1, ARGC, ...) ARGC
|
|
19
|
+
#define NBIND_SIZE(...) NBIND_EXPAND(NBIND_SIZE_HELPER(__VA_ARGS__, n, n, n, n, n, 1))
|
|
20
|
+
|
|
21
|
+
#define NBIND_CONCAT(a, b) a ## b
|
|
22
|
+
|
|
23
|
+
#define NBIND_SELECT_HELPER(name, argc) NBIND_CONCAT(name ## _, argc)
|
|
24
|
+
#define NBIND_SELECT(name, ...) NBIND_SELECT_HELPER(name, NBIND_EXPAND(NBIND_SIZE(__VA_ARGS__)))(__VA_ARGS__)
|
|
25
|
+
|
|
26
|
+
#define NBIND_UNIQUE(name, line) NBIND_CONCAT(name, line)
|
|
27
|
+
|
|
28
|
+
#define NBIND_GLOBAL() namespace
|
|
29
|
+
|
|
30
|
+
#define NBIND_FUNCTION(name, ...) nbind::FunctionDefiner NBIND_UNIQUE(definer, __LINE__)(#name, &name, ## __VA_ARGS__)
|
|
31
|
+
|
|
32
|
+
#define NBIND_MULTIFUNCTION(name, args, ...) nbind::FunctionDefiner :: template Overloaded args NBIND_UNIQUE(definer, __LINE__)(#name, &name, ## __VA_ARGS__)
|
|
33
|
+
|
|
34
|
+
// Define bindings for a C++ class using a syntax that looks like a function definition.
|
|
35
|
+
|
|
36
|
+
#define NBIND_CLASS_1(Name) \
|
|
37
|
+
template<class Bound> struct BindInvoker##Name { \
|
|
38
|
+
BindInvoker##Name(); \
|
|
39
|
+
nbind::BindDefiner<Name> definer; \
|
|
40
|
+
}; \
|
|
41
|
+
static struct BindInvoker##Name<Name> bindInvoker##Name; \
|
|
42
|
+
template<class Bound> BindInvoker##Name<Bound>::BindInvoker##Name():definer(#Name)
|
|
43
|
+
|
|
44
|
+
#define NBIND_CLASS_n(Name, visible) \
|
|
45
|
+
template<class Bound> struct BindInvoker##visible { \
|
|
46
|
+
BindInvoker##visible(); \
|
|
47
|
+
nbind::BindDefiner<Name> definer; \
|
|
48
|
+
}; \
|
|
49
|
+
static struct BindInvoker##visible<Name> bindInvoker##visible; \
|
|
50
|
+
template<class Bound> BindInvoker##visible<Bound>::BindInvoker##visible():definer(#visible)
|
|
51
|
+
|
|
52
|
+
#define NBIND_CLASS(...) NBIND_SELECT(NBIND_CLASS, __VA_ARGS__)
|
|
53
|
+
|
|
54
|
+
// Define a method by name in C++, and optionally a different name visible to JavaScript.
|
|
55
|
+
|
|
56
|
+
#define NBIND_METHOD(name, ...) definer.method(#name, &Bound::name, ## __VA_ARGS__)
|
|
57
|
+
|
|
58
|
+
#define NBIND_INHERIT(Name) definer.inherit<Name>()
|
|
59
|
+
|
|
60
|
+
#define NBIND_ARGS(...) <__VA_ARGS__>
|
|
61
|
+
|
|
62
|
+
#ifndef NBIND_MULTIMETHOD
|
|
63
|
+
#define NBIND_MULTIMETHOD(name, args, ...) definer.overloaded args ().method(#name, &Bound::name, ## __VA_ARGS__)
|
|
64
|
+
#endif
|
|
65
|
+
|
|
66
|
+
// Define a constructor.
|
|
67
|
+
// Constructor argument types must be appended as template arguments like:
|
|
68
|
+
// construct<int, int>()
|
|
69
|
+
|
|
70
|
+
#define NBIND_CONSTRUCT definer.constructor
|
|
71
|
+
|
|
72
|
+
#define NBIND_FIELD(name) definer.field(#name, &Bound::name)
|
|
73
|
+
#define NBIND_GETTER(name, ...) definer.property(#name, &Bound::name, ## __VA_ARGS__)
|
|
74
|
+
#define NBIND_GETSET(getName, setName, ...) \
|
|
75
|
+
definer.property(#getName, &Bound::getName, &Bound::setName, ## __VA_ARGS__)
|
|
76
|
+
|
|
77
|
+
#define NBIND_ALIAS(Name, base) \
|
|
78
|
+
namespace nbind { \
|
|
79
|
+
template<> struct BindingType<Name> : BindingType<base> { \
|
|
80
|
+
static inline Name fromWireType(WireType arg) { \
|
|
81
|
+
return(static_cast<Name>(BindingType<Type>::fromWireType(arg))); \
|
|
82
|
+
} \
|
|
83
|
+
static inline WireType toWireType(Name arg) { \
|
|
84
|
+
return(BindingType<Type>::toWireType(static_cast<Type>(arg))); \
|
|
85
|
+
} \
|
|
86
|
+
}; \
|
|
87
|
+
template<> struct Typer<Name> : Typer<base> {}; \
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
#endif // BUILDING_NODE_EXTENSION || __EMSCRIPTEN__
|
|
@@ -0,0 +1,238 @@
|
|
|
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
|
+
// Base class for signatures of all constructors, functions and methods.
|
|
9
|
+
// A signature represents a unique set of argument and return types,
|
|
10
|
+
// and the invoker functions needed to deal with such types.
|
|
11
|
+
|
|
12
|
+
class BaseSignature {
|
|
13
|
+
|
|
14
|
+
public:
|
|
15
|
+
|
|
16
|
+
// Type of the signature.
|
|
17
|
+
// Determines the actual signature class of each instance.
|
|
18
|
+
// These must match JavaScript enum SignatureType in common.ts
|
|
19
|
+
|
|
20
|
+
enum class SignatureType: unsigned int {
|
|
21
|
+
none = 0,
|
|
22
|
+
func,
|
|
23
|
+
method,
|
|
24
|
+
getter,
|
|
25
|
+
setter,
|
|
26
|
+
construct
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
BaseSignature(
|
|
30
|
+
SignatureType type,
|
|
31
|
+
funcPtr caller,
|
|
32
|
+
const char **policyNameList,
|
|
33
|
+
const TYPEID *typeList,
|
|
34
|
+
unsigned int arity
|
|
35
|
+
) : type(type), caller(caller), policyNameList(policyNameList), typeList(typeList), arity(arity) {}
|
|
36
|
+
|
|
37
|
+
SignatureType getType() const { return(type); }
|
|
38
|
+
funcPtr getCaller() const { return(caller); }
|
|
39
|
+
|
|
40
|
+
// Type list is one item longer than arity,
|
|
41
|
+
// because it starts with the return type (not counted in arity).
|
|
42
|
+
|
|
43
|
+
const TYPEID *getTypeList() const { return(typeList); }
|
|
44
|
+
unsigned int getArity() const { return(arity); }
|
|
45
|
+
|
|
46
|
+
const char **getPolicies() const {
|
|
47
|
+
return(policyNameList);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// A value constructor pointer is included in each signature,
|
|
51
|
+
// but only used for constructors.
|
|
52
|
+
|
|
53
|
+
funcPtr getValueConstructor() const { return(valueConstructor); }
|
|
54
|
+
void setValueConstructor(funcPtr valueConstructor) {
|
|
55
|
+
this->valueConstructor = valueConstructor;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
template<typename MethodType>
|
|
59
|
+
static inline funcPtr getDirect(MethodType func) { return(nullptr); }
|
|
60
|
+
|
|
61
|
+
private:
|
|
62
|
+
|
|
63
|
+
const SignatureType type;
|
|
64
|
+
const funcPtr caller;
|
|
65
|
+
const char **policyNameList;
|
|
66
|
+
const TYPEID *typeList;
|
|
67
|
+
const unsigned int arity;
|
|
68
|
+
funcPtr valueConstructor;
|
|
69
|
+
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
// Templated static class for each different function call signature exposed by the
|
|
73
|
+
// Node.js plugin. Used to pass arguments and return values between C++ and Node.js.
|
|
74
|
+
// Everything must be static because the V8 JavaScript engine wants a single
|
|
75
|
+
// function pointer to call, so each template variant is a singleton class.
|
|
76
|
+
|
|
77
|
+
template <class Signature, typename PolicyList, typename ReturnType, typename... Args>
|
|
78
|
+
class TemplatedBaseSignature : public BaseSignature {
|
|
79
|
+
|
|
80
|
+
public:
|
|
81
|
+
|
|
82
|
+
TemplatedBaseSignature() : BaseSignature(
|
|
83
|
+
Signature::typeExpr,
|
|
84
|
+
reinterpret_cast<funcPtr>(Signature::call),
|
|
85
|
+
PolicyLister<PolicyList>::getNameList(),
|
|
86
|
+
listTypes<ReturnType, Args...>(),
|
|
87
|
+
sizeof...(Args)
|
|
88
|
+
) {}
|
|
89
|
+
|
|
90
|
+
// Making the instance a direct class member fails on some platforms.
|
|
91
|
+
// Maybe it registers the signature too early.
|
|
92
|
+
|
|
93
|
+
static Signature &getInstance() {
|
|
94
|
+
// Linkage for a singleton instance of each templated class.
|
|
95
|
+
static Signature instance;
|
|
96
|
+
|
|
97
|
+
// The constructor should have been called, but sometimes it wasn't!
|
|
98
|
+
if(instance.getType() == SignatureType :: none) new (&instance) Signature();
|
|
99
|
+
|
|
100
|
+
return(instance);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Information about a single named function.
|
|
104
|
+
// This wrapper around Signature::MethodType is needed because TemplatedBaseSignature itself cannot see the type directly.
|
|
105
|
+
// It's passed as a CRTP argument and is not fully defined here, but inside an inner class that doesn't matter.
|
|
106
|
+
|
|
107
|
+
struct MethodInfo {
|
|
108
|
+
|
|
109
|
+
typedef typename Signature::MethodType MethodType;
|
|
110
|
+
|
|
111
|
+
MethodInfo(MethodType func, TypeFlags flags) : func(func), flags(flags) {}
|
|
112
|
+
|
|
113
|
+
const MethodType func;
|
|
114
|
+
const TypeFlags flags;
|
|
115
|
+
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
static const MethodInfo &getMethod(unsigned int num) {
|
|
119
|
+
return(getInstance().funcVect[num]);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
template <typename MethodType>
|
|
123
|
+
static unsigned int addMethod(MethodType func, TypeFlags flags) {
|
|
124
|
+
auto &funcVect = getInstance().funcVect;
|
|
125
|
+
|
|
126
|
+
funcVect.emplace_back(func, flags | TypeFlags::isMethod);
|
|
127
|
+
|
|
128
|
+
return(funcVect.size() - 1);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
132
|
+
|
|
133
|
+
// Specialize static caller functions defined in Caller.h.
|
|
134
|
+
|
|
135
|
+
typedef Caller<
|
|
136
|
+
ReturnType,
|
|
137
|
+
typename emscripten::internal::MapWithIndex<
|
|
138
|
+
PolicyList,
|
|
139
|
+
TypeList,
|
|
140
|
+
ArgFromWire,
|
|
141
|
+
Args...
|
|
142
|
+
>::type
|
|
143
|
+
> CallWrapper;
|
|
144
|
+
|
|
145
|
+
typedef Checker<
|
|
146
|
+
typename emscripten::internal::MapWithIndex<
|
|
147
|
+
PolicyList,
|
|
148
|
+
TypeList,
|
|
149
|
+
CheckWire,
|
|
150
|
+
Args...
|
|
151
|
+
>::type
|
|
152
|
+
> CheckWrapper;
|
|
153
|
+
|
|
154
|
+
template <typename NanArgs>
|
|
155
|
+
static bool typesAreValid(NanArgs &args) {
|
|
156
|
+
return(CheckWrapper::typesAreValid(args));
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
template <typename NanArgs>
|
|
160
|
+
static v8::Local<v8::Value> getTypeError(NanArgs &args) {
|
|
161
|
+
return(CheckWrapper::getTypeError(args, getInstance().getTypeList()));
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
static bool arityIsValid(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
165
|
+
static constexpr decltype(args.Length()) arity = sizeof...(Args);
|
|
166
|
+
return(args.Length() == arity);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
template <typename Value>
|
|
170
|
+
static bool arityIsValid(const Nan::PropertyCallbackInfo<Value> &args) {
|
|
171
|
+
return(true);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
template <typename NanArgs, typename Bound>
|
|
175
|
+
static Bound *getTargetSafely(
|
|
176
|
+
NanArgs &nanArgs,
|
|
177
|
+
Bound *target,
|
|
178
|
+
TypeFlags flags
|
|
179
|
+
) {
|
|
180
|
+
return(BindWrapper<Bound>::getBound(nanArgs.This(), flags));
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Overload second argument, effectively a partial specialization of the function template above.
|
|
184
|
+
template <typename NanArgs>
|
|
185
|
+
static void *getTargetSafely(
|
|
186
|
+
NanArgs &nanArgs,
|
|
187
|
+
void *target,
|
|
188
|
+
TypeFlags flags
|
|
189
|
+
) {
|
|
190
|
+
return(nullptr);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
template <typename Bound, typename V8Args, typename NanArgs>
|
|
194
|
+
static void callInnerSafely(V8Args &args, NanArgs &nanArgs, unsigned int methodNum) {
|
|
195
|
+
Bound *target = nullptr;
|
|
196
|
+
|
|
197
|
+
if(!arityIsValid(nanArgs)) {
|
|
198
|
+
// TODO: When function is overloaded, this test could be skipped...
|
|
199
|
+
|
|
200
|
+
std::string msg = "Wrong number of arguments, expected " + std::to_string(sizeof...(Args));
|
|
201
|
+
Nan::ThrowError(msg.c_str());
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if(!Signature::typesAreValid(args)) {
|
|
206
|
+
Nan::ThrowError(Signature::getTypeError(args));
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
const MethodInfo &method = getMethod(methodNum);
|
|
211
|
+
|
|
212
|
+
try {
|
|
213
|
+
target = getTargetSafely(nanArgs, target, method.flags);
|
|
214
|
+
|
|
215
|
+
Status::clearError();
|
|
216
|
+
Signature::callInner(method, args, nanArgs, target);
|
|
217
|
+
if(Status::getError() != nullptr) Nan::ThrowError(Status::getError());
|
|
218
|
+
} catch(const cbException &ex) {
|
|
219
|
+
// A JavaScript exception is already heading up the stack.
|
|
220
|
+
} catch(const std::exception &ex) {
|
|
221
|
+
const char *message = Status::getError();
|
|
222
|
+
|
|
223
|
+
if(message == nullptr) message = ex.what();
|
|
224
|
+
|
|
225
|
+
Nan::ThrowError(message);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
#endif // BUILDING_NODE_EXTENSION
|
|
230
|
+
|
|
231
|
+
// The funcVect vector cannot be moved to BaseSignature because it can contain pointers to
|
|
232
|
+
// functions or class methods, and there isn't a single pointer type able to hold both.
|
|
233
|
+
|
|
234
|
+
std::vector<struct MethodInfo> funcVect;
|
|
235
|
+
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
} // namespace
|
|
@@ -0,0 +1,59 @@
|
|
|
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_register_callback_signature(const TYPEID *types, unsigned int typeCount);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
class BaseCallbackSignature {
|
|
13
|
+
|
|
14
|
+
public:
|
|
15
|
+
|
|
16
|
+
BaseCallbackSignature(const TYPEID *typeList, unsigned int arity) :
|
|
17
|
+
typeList(typeList),
|
|
18
|
+
arity(arity),
|
|
19
|
+
num(_nbind_register_callback_signature(
|
|
20
|
+
typeList,
|
|
21
|
+
arity + 1
|
|
22
|
+
)) {}
|
|
23
|
+
|
|
24
|
+
const TYPEID *getTypeList() const { return(typeList); }
|
|
25
|
+
unsigned int getArity() const { return(arity); }
|
|
26
|
+
unsigned int getNum() const { return(num); }
|
|
27
|
+
|
|
28
|
+
private:
|
|
29
|
+
|
|
30
|
+
const TYPEID *typeList;
|
|
31
|
+
const unsigned int arity;
|
|
32
|
+
const unsigned int num;
|
|
33
|
+
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
template <typename ReturnType, typename... Args>
|
|
37
|
+
class CallbackSignature : public BaseCallbackSignature {
|
|
38
|
+
|
|
39
|
+
public:
|
|
40
|
+
|
|
41
|
+
typedef ReturnType(*MethodType)(Args...);
|
|
42
|
+
|
|
43
|
+
CallbackSignature() : BaseCallbackSignature(
|
|
44
|
+
listTypes<ReturnType, Args...>(),
|
|
45
|
+
sizeof...(Args)
|
|
46
|
+
) {}
|
|
47
|
+
|
|
48
|
+
// Making the instance a direct class member fails on some platforms.
|
|
49
|
+
// Maybe it registers the signature too early.
|
|
50
|
+
|
|
51
|
+
static CallbackSignature &getInstance() {
|
|
52
|
+
// Linkage for a singleton instance of each templated class.
|
|
53
|
+
static CallbackSignature instance;
|
|
54
|
+
return(instance);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
} // namespace
|
|
@@ -0,0 +1,76 @@
|
|
|
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 "BaseSignature.h"
|
|
7
|
+
|
|
8
|
+
namespace nbind {
|
|
9
|
+
|
|
10
|
+
// Constructor. Call() creates an instance of a bound C++ class, while
|
|
11
|
+
// createValue() constructs it in place, directly in the stack for passing by value.
|
|
12
|
+
|
|
13
|
+
template <class Bound, typename PolicyList, typename... Args>
|
|
14
|
+
class ConstructorSignature : public TemplatedBaseSignature<ConstructorSignature<Bound, PolicyList, Args...>, PolicyList, Bound *, Args...> {
|
|
15
|
+
|
|
16
|
+
public:
|
|
17
|
+
|
|
18
|
+
ConstructorSignature() {
|
|
19
|
+
this->setValueConstructor(reinterpret_cast<funcPtr>(createValue));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Unused dummy type.
|
|
23
|
+
typedef void *MethodType;
|
|
24
|
+
|
|
25
|
+
typedef TemplatedBaseSignature<ConstructorSignature, PolicyList, Bound *, Args...> Parent;
|
|
26
|
+
|
|
27
|
+
static constexpr auto typeExpr = BaseSignature :: SignatureType :: construct;
|
|
28
|
+
|
|
29
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
30
|
+
|
|
31
|
+
typedef Creator<
|
|
32
|
+
Bound,
|
|
33
|
+
typename emscripten::internal::MapWithIndex<
|
|
34
|
+
PolicyList,
|
|
35
|
+
TypeList,
|
|
36
|
+
ArgFromWire,
|
|
37
|
+
Args...
|
|
38
|
+
>::type
|
|
39
|
+
> ConstructWrapper;
|
|
40
|
+
|
|
41
|
+
static void call(const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
42
|
+
ConstructWrapper::create(args);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static void createValue(ArgStorage &storage, const Nan::FunctionCallbackInfo<v8::Value> &args) {
|
|
46
|
+
ConstructWrapper::createValue(storage, args);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
#elif defined(__EMSCRIPTEN__)
|
|
50
|
+
|
|
51
|
+
typedef Creator<
|
|
52
|
+
PolicyList,
|
|
53
|
+
Bound,
|
|
54
|
+
Args...
|
|
55
|
+
> ConstructWrapper;
|
|
56
|
+
|
|
57
|
+
// Args are wire types! They must be received by value.
|
|
58
|
+
|
|
59
|
+
static typename ConstructWrapper::WireType call(
|
|
60
|
+
typename BindingType<Args>::WireType... args
|
|
61
|
+
) {
|
|
62
|
+
return(ConstructWrapper::create(args...));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
static void createValue(
|
|
66
|
+
ArgStorage &storage,
|
|
67
|
+
typename BindingType<Args>::WireType... args
|
|
68
|
+
) {
|
|
69
|
+
ConstructWrapper::createValue(storage, args...);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
#endif // BUILDING_NODE_EXTENSION, __EMSCRIPTEN__
|
|
73
|
+
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
} // namespace
|