@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,84 @@
|
|
|
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
|
+
// These must match JavaScript enum StructureType in Type.ts
|
|
9
|
+
|
|
10
|
+
enum class StructureType : unsigned char {
|
|
11
|
+
none = 0,
|
|
12
|
+
constant,
|
|
13
|
+
pointer,
|
|
14
|
+
reference,
|
|
15
|
+
rvalue,
|
|
16
|
+
shared,
|
|
17
|
+
unique,
|
|
18
|
+
vector,
|
|
19
|
+
array,
|
|
20
|
+
callback,
|
|
21
|
+
max
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// Type ID system, based on Embind.
|
|
25
|
+
|
|
26
|
+
typedef const void *TYPEID;
|
|
27
|
+
|
|
28
|
+
// Type ID generator.
|
|
29
|
+
|
|
30
|
+
template<typename ArgType>
|
|
31
|
+
struct Typer {
|
|
32
|
+
// Reserve a single byte of memory to uniquely represent a type.
|
|
33
|
+
// The address of this byte is a unique type-specific constant.
|
|
34
|
+
static const struct SpecType {
|
|
35
|
+
const StructureType placeholderFlag;
|
|
36
|
+
} spec;
|
|
37
|
+
|
|
38
|
+
static NBIND_CONSTEXPR TYPEID makeID() {
|
|
39
|
+
return(&spec.placeholderFlag);
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
// Linkage for placeholder bytes representing types.
|
|
44
|
+
template<typename ArgType>
|
|
45
|
+
const typename Typer<ArgType>::SpecType Typer<ArgType>::spec = {
|
|
46
|
+
StructureType :: none
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Parameterized types
|
|
50
|
+
|
|
51
|
+
struct ParamStructure {
|
|
52
|
+
const StructureType placeholderFlag;
|
|
53
|
+
const TYPEID target;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
#define NBIND_TYPER_PARAM(Type, flag) \
|
|
57
|
+
template<typename ArgType> \
|
|
58
|
+
struct Typer<Type> { \
|
|
59
|
+
static const ParamStructure spec; \
|
|
60
|
+
\
|
|
61
|
+
static NBIND_CONSTEXPR TYPEID makeID() { \
|
|
62
|
+
return(&spec.placeholderFlag); \
|
|
63
|
+
} \
|
|
64
|
+
}; \
|
|
65
|
+
\
|
|
66
|
+
template<typename ArgType> \
|
|
67
|
+
const ParamStructure Typer<Type>::spec = { \
|
|
68
|
+
StructureType :: flag, \
|
|
69
|
+
Typer<ArgType>::makeID() \
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
NBIND_TYPER_PARAM(const ArgType, constant);
|
|
73
|
+
NBIND_TYPER_PARAM(ArgType *, pointer);
|
|
74
|
+
NBIND_TYPER_PARAM(ArgType &, reference);
|
|
75
|
+
|
|
76
|
+
// Convert a list of types in the template argument into an array of type IDs.
|
|
77
|
+
|
|
78
|
+
template<typename... Args>
|
|
79
|
+
const TYPEID *listTypes() {
|
|
80
|
+
static NBIND_CONSTEXPR TYPEID typeList[] = { Typer<Args>::makeID()... };
|
|
81
|
+
return(typeList);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
} // namespace
|
|
@@ -0,0 +1,66 @@
|
|
|
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
|
+
#include <string>
|
|
8
|
+
#include <vector>
|
|
9
|
+
#include <array>
|
|
10
|
+
#include <functional>
|
|
11
|
+
|
|
12
|
+
namespace nbind {
|
|
13
|
+
|
|
14
|
+
NBIND_TYPER_PARAM(std::shared_ptr<ArgType>, shared);
|
|
15
|
+
NBIND_TYPER_PARAM(std::unique_ptr<ArgType>, unique);
|
|
16
|
+
NBIND_TYPER_PARAM(std::vector<ArgType>, vector);
|
|
17
|
+
|
|
18
|
+
typedef struct {
|
|
19
|
+
const StructureType placeholderFlag;
|
|
20
|
+
const TYPEID member;
|
|
21
|
+
const size_t length;
|
|
22
|
+
} ArrayStructure;
|
|
23
|
+
|
|
24
|
+
template<typename MemberType, size_t size>
|
|
25
|
+
struct Typer<std::array<MemberType, size>> {
|
|
26
|
+
static const ArrayStructure spec;
|
|
27
|
+
|
|
28
|
+
static NBIND_CONSTEXPR TYPEID makeID() {
|
|
29
|
+
return(&spec.placeholderFlag);
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
template<typename MemberType, size_t size>
|
|
34
|
+
const ArrayStructure Typer<std::array<MemberType, size>>::spec = {
|
|
35
|
+
StructureType :: array,
|
|
36
|
+
Typer<MemberType>::makeID(),
|
|
37
|
+
size
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
//template<typename ReturnType, typename... Args>
|
|
41
|
+
template <int argCount>
|
|
42
|
+
struct CallbackStructure {
|
|
43
|
+
const StructureType placeholderFlag;
|
|
44
|
+
const int arity;
|
|
45
|
+
const TYPEID returnType;
|
|
46
|
+
const TYPEID argType[argCount];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
template<typename ReturnType, typename... Args>
|
|
50
|
+
struct Typer<std::function<ReturnType (Args...)>> {
|
|
51
|
+
static const CallbackStructure<sizeof...(Args)> spec;
|
|
52
|
+
|
|
53
|
+
static NBIND_CONSTEXPR TYPEID makeID() {
|
|
54
|
+
return(&spec.placeholderFlag);
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
template<typename ReturnType, typename... Args>
|
|
59
|
+
const CallbackStructure<sizeof...(Args)> Typer<std::function<ReturnType (Args...)>>::spec = {
|
|
60
|
+
StructureType :: callback,
|
|
61
|
+
sizeof...(Args),
|
|
62
|
+
Typer<ReturnType>::makeID(),
|
|
63
|
+
{ Typer<Args>::makeID()... }
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
} // namespace
|
|
@@ -0,0 +1,38 @@
|
|
|
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 <typename ArgType> struct BindingType;
|
|
9
|
+
|
|
10
|
+
template<typename ArgType, typename PolicyList = PolicyListType<>>
|
|
11
|
+
struct TypeTransformer {
|
|
12
|
+
typedef BindingType<typename ExecutePolicies<PolicyList>::template Transformed<
|
|
13
|
+
typename DetectPolicies<ArgType>::Type
|
|
14
|
+
>::Type> Binding;
|
|
15
|
+
|
|
16
|
+
# if defined(BUILDING_NODE_EXTENSION)
|
|
17
|
+
typedef v8::Local<v8::Value> WireType;
|
|
18
|
+
# else
|
|
19
|
+
typedef typename Binding::WireType WireType;
|
|
20
|
+
# endif
|
|
21
|
+
|
|
22
|
+
typedef typename Binding::Type Type;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
template <typename ArgType, typename WireType>
|
|
26
|
+
inline typename TypeTransformer<ArgType>::Type convertFromWire(WireType arg) {
|
|
27
|
+
return(TypeTransformer<ArgType>::Binding::fromWireType(arg));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Convert any C++ type to the corresponding JavaScript type.
|
|
31
|
+
// Call correct type converter using perfect forwarding (moving doesn't work).
|
|
32
|
+
|
|
33
|
+
template <typename ReturnType>
|
|
34
|
+
inline typename TypeTransformer<ReturnType>::WireType convertToWire(ReturnType result) {
|
|
35
|
+
return(TypeTransformer<ReturnType>::Binding::toWireType(std::forward<ReturnType>(result)));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
} // namespace
|
|
@@ -0,0 +1,100 @@
|
|
|
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
|
+
// Check for C++11 support in the compiler.
|
|
7
|
+
|
|
8
|
+
#if defined(_MSC_VER)
|
|
9
|
+
// Visual Studio 2015 has good enough support
|
|
10
|
+
// even though it sets __cplusplus to 199711
|
|
11
|
+
# if _MSC_VER < 1900
|
|
12
|
+
# error nbind requires at least Visual Studio 2015.
|
|
13
|
+
# endif
|
|
14
|
+
#elif (__cplusplus < 201103L)
|
|
15
|
+
# error nbind requires a compiler with sufficient C++11 support. Maybe compile with -std=c++11 or similar?
|
|
16
|
+
#endif
|
|
17
|
+
|
|
18
|
+
// Macro to report an error when exceptions are not available.
|
|
19
|
+
|
|
20
|
+
#define NBIND_ERR(message) nbind::Status::setError(message)
|
|
21
|
+
|
|
22
|
+
// Visual Studio 2015 doesn't accept some things as constexpr
|
|
23
|
+
// while GCC and Clang do, so change them to const only if needed.
|
|
24
|
+
|
|
25
|
+
#if defined(_MSC_VER)
|
|
26
|
+
# define NBIND_CONSTEXPR const
|
|
27
|
+
#else
|
|
28
|
+
# define NBIND_CONSTEXPR constexpr
|
|
29
|
+
#endif
|
|
30
|
+
|
|
31
|
+
#include <utility>
|
|
32
|
+
#include <cstdint>
|
|
33
|
+
#include <cstring>
|
|
34
|
+
|
|
35
|
+
namespace nbind {
|
|
36
|
+
|
|
37
|
+
typedef void (*funcPtr)();
|
|
38
|
+
|
|
39
|
+
class Status {
|
|
40
|
+
|
|
41
|
+
public:
|
|
42
|
+
|
|
43
|
+
static inline const char *getError() { return(message); }
|
|
44
|
+
|
|
45
|
+
static inline void clearError() {
|
|
46
|
+
if(Status::message) delete Status::message;
|
|
47
|
+
Status::message = nullptr;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
static inline void setError(const char *message) {
|
|
51
|
+
if(!Status::message) {
|
|
52
|
+
Status::message = new char[strlen(message) + 1];
|
|
53
|
+
if(Status::message) strcpy(Status::message, message);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
private:
|
|
58
|
+
|
|
59
|
+
static char *message;
|
|
60
|
+
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
} // namespace
|
|
64
|
+
|
|
65
|
+
#include "TypeID.h"
|
|
66
|
+
#include "TypeStd.h"
|
|
67
|
+
#include "Policy.h"
|
|
68
|
+
|
|
69
|
+
#if defined(BUILDING_NODE_EXTENSION)
|
|
70
|
+
|
|
71
|
+
# include "v8/BindWrapper.h"
|
|
72
|
+
# include "v8/BindingType.h"
|
|
73
|
+
# include "TypeTransformer.h"
|
|
74
|
+
# include "v8/NBind.h"
|
|
75
|
+
# include "v8/Output.h"
|
|
76
|
+
# include "v8/Convert.h"
|
|
77
|
+
# include "v8/ArgFromWire.h"
|
|
78
|
+
# include "v8/External.h"
|
|
79
|
+
# include "v8/Callback.h"
|
|
80
|
+
# include "v8/BindingStd.h"
|
|
81
|
+
# include "v8/StdFunction.h"
|
|
82
|
+
# include "Buffer.h"
|
|
83
|
+
# include "v8/Buffer.h"
|
|
84
|
+
|
|
85
|
+
#elif defined(__EMSCRIPTEN__)
|
|
86
|
+
|
|
87
|
+
# include "em/NBind.h"
|
|
88
|
+
# include "em/BindingType.h"
|
|
89
|
+
# include "TypeTransformer.h"
|
|
90
|
+
# include "em/ArgFromWire.h"
|
|
91
|
+
# include "signature/CallbackSignature.h"
|
|
92
|
+
# include "em/Pool.h"
|
|
93
|
+
# include "em/External.h"
|
|
94
|
+
# include "em/Callback.h"
|
|
95
|
+
# include "em/BindingStd.h"
|
|
96
|
+
# include "em/StdFunction.h"
|
|
97
|
+
# include "Buffer.h"
|
|
98
|
+
# include "em/Buffer.h"
|
|
99
|
+
|
|
100
|
+
#endif
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
std::forward_list<BindClassBase *> &getClassList(void);
|
|
9
|
+
std::forward_list<MethodDef> &getFunctionList(void);
|
|
10
|
+
const void **getPrimitiveList(void);
|
|
11
|
+
const void **getNamedTypeList(void);
|
|
12
|
+
|
|
13
|
+
void registerClass(BindClassBase &bindClass);
|
|
14
|
+
void registerFunction(
|
|
15
|
+
const char *name,
|
|
16
|
+
funcPtr ptr,
|
|
17
|
+
unsigned int num,
|
|
18
|
+
BaseSignature *signature,
|
|
19
|
+
TypeFlags flags
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
} // namespace
|
|
@@ -0,0 +1,31 @@
|
|
|
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<typename PolicyList, typename ArgType>
|
|
9
|
+
struct ArgFromWire {
|
|
10
|
+
|
|
11
|
+
typedef TypeTransformer<ArgType, PolicyList> Transformed;
|
|
12
|
+
typedef typename Transformed::WireType WireType;
|
|
13
|
+
|
|
14
|
+
explicit ArgFromWire(WireType arg) {}
|
|
15
|
+
|
|
16
|
+
inline typename Transformed::Type get(WireType arg) const {
|
|
17
|
+
return(Transformed::Binding::fromWireType(arg));
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
template<typename PolicyList>
|
|
23
|
+
struct ArgFromWire<PolicyList, void> {
|
|
24
|
+
|
|
25
|
+
explicit ArgFromWire() {}
|
|
26
|
+
|
|
27
|
+
inline void get() const {}
|
|
28
|
+
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
} // namespace
|
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
// This file is part of nbind, copyright (C) 2014-2016 BusFaster Ltd.
|
|
2
|
+
// Released under the MIT license, see LICENSE.
|
|
3
|
+
|
|
4
|
+
// This file handles type conversion of C++ standard library types
|
|
5
|
+
// to / from JavaScript.
|
|
6
|
+
|
|
7
|
+
#pragma once
|
|
8
|
+
|
|
9
|
+
#include <string>
|
|
10
|
+
#include <vector>
|
|
11
|
+
#include <array>
|
|
12
|
+
|
|
13
|
+
namespace nbind {
|
|
14
|
+
|
|
15
|
+
// Shared pointer.
|
|
16
|
+
|
|
17
|
+
template <typename ArgType>
|
|
18
|
+
struct BindingType<std::shared_ptr<ArgType>> {
|
|
19
|
+
|
|
20
|
+
typedef std::shared_ptr<ArgType> Type;
|
|
21
|
+
typedef typename BindingType<ArgType>::WireType WireType;
|
|
22
|
+
|
|
23
|
+
static inline Type fromWireType(WireType arg) {
|
|
24
|
+
// Hack: JS side sends Type * instead of WireType, since C++ side can
|
|
25
|
+
// easily unwrap the shared_ptr anyway.
|
|
26
|
+
|
|
27
|
+
return(*reinterpret_cast<Type *>(arg));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
template <typename SmartType>
|
|
31
|
+
static inline WireType toWireType(SmartType &&arg) {
|
|
32
|
+
WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val)));
|
|
33
|
+
|
|
34
|
+
val->boundUnsafe = arg.get();
|
|
35
|
+
val->boundShared = new std::shared_ptr<ArgType>(std::move(arg));
|
|
36
|
+
|
|
37
|
+
return(val);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
// Unique pointer.
|
|
43
|
+
|
|
44
|
+
template <typename ArgType>
|
|
45
|
+
struct BindingType<std::unique_ptr<ArgType>> {
|
|
46
|
+
|
|
47
|
+
typedef std::unique_ptr<ArgType> Type;
|
|
48
|
+
typedef typename BindingType<ArgType>::WireType WireType;
|
|
49
|
+
|
|
50
|
+
static inline WireType toWireType(Type &&arg) {
|
|
51
|
+
return(BindingType<std::shared_ptr<ArgType>>::toWireType(std::move(arg)));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Array.
|
|
57
|
+
|
|
58
|
+
template <typename MemberType, size_t size>
|
|
59
|
+
struct BindingType<std::array<MemberType, size>> {
|
|
60
|
+
|
|
61
|
+
typedef typename TypeTransformer<MemberType>::Binding MemberBinding;
|
|
62
|
+
|
|
63
|
+
typedef std::array<MemberType, size> Type;
|
|
64
|
+
|
|
65
|
+
/** Temporary in-memory serialization format for the array,
|
|
66
|
+
* easily read and written by both C++ and JavaScript. */
|
|
67
|
+
|
|
68
|
+
typedef struct {
|
|
69
|
+
/** JavaScript and C++ already know the length based on the type,
|
|
70
|
+
* but we duplicate it here to use the same wire type as vectors
|
|
71
|
+
* and share code between them. */
|
|
72
|
+
uint32_t length;
|
|
73
|
+
/** The contents continue past the struct. Use "struct hack"
|
|
74
|
+
* because C++ doesn't have flexible array members like C. */
|
|
75
|
+
typename MemberBinding::WireType data[1];
|
|
76
|
+
} *WireType;
|
|
77
|
+
|
|
78
|
+
static inline Type fromWireType(WireType arg) {
|
|
79
|
+
Type val;
|
|
80
|
+
|
|
81
|
+
for(uint32_t num = 0; num < size; ++num) {
|
|
82
|
+
val[num] = MemberBinding::fromWireType(arg->data[num]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return(val);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
static inline WireType toWireType(Type &&arg) {
|
|
89
|
+
/** Allocate space for wire type (which includes 1 array member) and
|
|
90
|
+
* the other size-1 members, in temporary lalloc "stack frame". */
|
|
91
|
+
WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val) + (size - 1) * sizeof(*val->data)));
|
|
92
|
+
|
|
93
|
+
val->length = size;
|
|
94
|
+
|
|
95
|
+
for(uint32_t num = 0; num < size; ++num) {
|
|
96
|
+
val->data[num] = MemberBinding::toWireType(std::forward<MemberType>(arg[num]));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
return(val);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
// Vector.
|
|
105
|
+
|
|
106
|
+
template <typename MemberType>
|
|
107
|
+
struct BindingType<std::vector<MemberType>> {
|
|
108
|
+
|
|
109
|
+
typedef typename TypeTransformer<MemberType>::Binding MemberBinding;
|
|
110
|
+
|
|
111
|
+
typedef std::vector<MemberType> Type;
|
|
112
|
+
|
|
113
|
+
/** Temporary in-memory serialization format for the vector,
|
|
114
|
+
* easily read and written by both C++ and JavaScript. */
|
|
115
|
+
|
|
116
|
+
typedef struct {
|
|
117
|
+
uint32_t length;
|
|
118
|
+
/** The contents continue past the struct. Use "struct hack"
|
|
119
|
+
* because C++ doesn't have flexible array members like C. */
|
|
120
|
+
typename MemberBinding::WireType data[1];
|
|
121
|
+
} *WireType;
|
|
122
|
+
|
|
123
|
+
static inline Type fromWireType(WireType arg) {
|
|
124
|
+
uint32_t size = arg->length;
|
|
125
|
+
Type val;
|
|
126
|
+
|
|
127
|
+
val.reserve(size);
|
|
128
|
+
|
|
129
|
+
for(uint32_t num = 0; num < size; ++num) {
|
|
130
|
+
val.push_back(MemberBinding::fromWireType(arg->data[num]));
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return(val);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
static inline WireType toWireType(Type &&arg) {
|
|
137
|
+
size_t size = arg.size();
|
|
138
|
+
/** Allocate space for wire type (which includes 1 vector member) and
|
|
139
|
+
* the other size-1 members, in temporary lalloc "stack frame". */
|
|
140
|
+
WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val) + (size - 1) * sizeof(*val->data)));
|
|
141
|
+
|
|
142
|
+
val->length = size;
|
|
143
|
+
|
|
144
|
+
for(uint32_t num = 0; num < size; ++num) {
|
|
145
|
+
val->data[num] = MemberBinding::toWireType(std::forward<MemberType>(arg[num]));
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return(val);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
// String.
|
|
154
|
+
|
|
155
|
+
template<> struct BindingType<std::string> {
|
|
156
|
+
|
|
157
|
+
typedef std::string Type;
|
|
158
|
+
|
|
159
|
+
/** Temporary in-memory serialization format for the string,
|
|
160
|
+
* easily read and written by both C++ and JavaScript. */
|
|
161
|
+
|
|
162
|
+
typedef struct {
|
|
163
|
+
uint32_t length;
|
|
164
|
+
/** The contents continue past the struct. Use "struct hack"
|
|
165
|
+
* because C++ doesn't have flexible array members like C. */
|
|
166
|
+
char data[1];
|
|
167
|
+
} *WireType;
|
|
168
|
+
|
|
169
|
+
static inline Type fromWireType(WireType arg) {
|
|
170
|
+
return(std::string(arg->data, arg->length));
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
static inline WireType toWireType(Type arg) {
|
|
174
|
+
size_t length = arg.length();
|
|
175
|
+
/** Allocate space for wire type (which includes 1 character) and
|
|
176
|
+
* the other size-1 characters, in temporary lalloc "stack frame". */
|
|
177
|
+
WireType val = reinterpret_cast<WireType>(NBind::lalloc(sizeof(*val) + length - 1));
|
|
178
|
+
|
|
179
|
+
val->length = length;
|
|
180
|
+
std::copy(arg.begin(), arg.end(), val->data);
|
|
181
|
+
|
|
182
|
+
return(val);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
template <> struct BindingType<StrictType<std::string>> : public BindingType<std::string> {};
|
|
188
|
+
|
|
189
|
+
// String reference.
|
|
190
|
+
|
|
191
|
+
template<> struct BindingType<const std::string &> {
|
|
192
|
+
|
|
193
|
+
typedef std::string Type;
|
|
194
|
+
|
|
195
|
+
typedef struct {
|
|
196
|
+
uint32_t length;
|
|
197
|
+
char data[1];
|
|
198
|
+
} *WireType;
|
|
199
|
+
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
template <> struct BindingType<StrictType<const std::string &>> : public BindingType<const std::string &> {};
|
|
203
|
+
|
|
204
|
+
template<typename PolicyList>
|
|
205
|
+
struct ArgFromWire<PolicyList, const std::string &> {
|
|
206
|
+
|
|
207
|
+
typedef typename BindingType<const std::string &>::WireType WireType;
|
|
208
|
+
|
|
209
|
+
explicit ArgFromWire(WireType arg) : val(arg->data, arg->length) {}
|
|
210
|
+
|
|
211
|
+
inline const std::string &get(WireType arg) const {
|
|
212
|
+
return(val);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// RAII style storage for the string data.
|
|
216
|
+
|
|
217
|
+
std::string val;
|
|
218
|
+
|
|
219
|
+
};
|
|
220
|
+
|
|
221
|
+
} // namespace
|