-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwire.h
455 lines (381 loc) · 14.9 KB
/
wire.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
#pragma once
#if __cplusplus < 201103L
#error Including <emscripten/wire.h> requires building with -std=c++11 or newer!
#else
// A value moving between JavaScript and C++ has three representations:
// - The original JS value: a String
// - The native on-the-wire value: a stack-allocated char*, say
// - The C++ value: std::string
//
// We'll call the on-the-wire type WireType.
#include <stdio.h>
#include <cstdlib>
#include <memory>
#include <string>
#include <string.h>
#define EMSCRIPTEN_ALWAYS_INLINE __attribute__((always_inline))
namespace emscripten {
// #ifndef EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES
// #define EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES 1
// #endif
// #if EMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES
// constexpr bool has_unbound_type_names = true;
// #else
// constexpr bool has_unbound_type_names = false;
// #endif
namespace internal {
typedef const void* TYPEID;
// We don't need the full std::type_info implementation. We
// just need a unique identifier per type and polymorphic type
// identification.
template<typename T>
struct CanonicalizedID {
static char c;
static constexpr TYPEID get() {
return &c;
}
};
template<typename T>
char CanonicalizedID<T>::c;
template<typename T>
struct Canonicalized {
typedef typename std::remove_cv<typename std::remove_reference<T>::type>::type type;
};
template<typename T>
struct LightTypeID {
static constexpr TYPEID get() {
typedef typename Canonicalized<T>::type C;
// if(has_unbound_type_names || std::is_polymorphic<C>::value) {
// #if __has_feature(cxx_rtti)
// return &typeid(T);
// #else
// static_assert(!has_unbound_type_names,
// "Unbound type names are illegal with RTTI disabled. "
// "Either add -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 to or remove -fno-rtti "
// "from the compiler arguments");
// static_assert(!std::is_polymorphic<C>::value,
// "Canonicalized<T>::type being polymorphic is illegal with RTTI disabled");
// #endif
// }
return CanonicalizedID<C>::get();
}
};
template<typename T>
constexpr TYPEID getLightTypeID(const T& value) {
typedef typename Canonicalized<T>::type C;
// if(has_unbound_type_names || std::is_polymorphic<C>::value) {
// #if __has_feature(cxx_rtti)
// return &typeid(value);
// #else
// static_assert(!has_unbound_type_names,
// "Unbound type names are illegal with RTTI disabled. "
// "Either add -DEMSCRIPTEN_HAS_UNBOUND_TYPE_NAMES=0 to or remove -fno-rtti "
// "from the compiler arguments");
// static_assert(!std::is_polymorphic<C>::value,
// "Canonicalized<T>::type being polymorphic is illegal with RTTI disabled");
// #endif
// }
return LightTypeID<T>::get();
}
template<typename T>
struct TypeID {
static constexpr TYPEID get() {
return LightTypeID<T>::get();
}
};
template<typename T>
struct TypeID<std::unique_ptr<T>> {
static constexpr TYPEID get() {
return TypeID<T>::get();
}
};
template<typename T>
struct TypeID<T*> {
static_assert(!std::is_pointer<T*>::value, "Implicitly binding raw pointers is illegal. Specify allow_raw_pointer<arg<?>>");
};
template<typename T>
struct AllowedRawPointer {
};
template<typename T>
struct TypeID<AllowedRawPointer<T>> {
static constexpr TYPEID get() {
return LightTypeID<T*>::get();
}
};
// ExecutePolicies<>
template<typename... Policies>
struct ExecutePolicies;
template<>
struct ExecutePolicies<> {
template<typename T, int Index>
struct With {
typedef T type;
};
};
template<typename Policy, typename... Remaining>
struct ExecutePolicies<Policy, Remaining...> {
template<typename T, int Index>
struct With {
typedef typename Policy::template Transform<
typename ExecutePolicies<Remaining...>::template With<T, Index>::type,
Index
>::type type;
};
};
// TypeList<>
template<typename...>
struct TypeList {};
// Cons :: T, TypeList<types...> -> Cons<T, types...>
template<typename First, typename TypeList>
struct Cons;
template<typename First, typename... Rest>
struct Cons<First, TypeList<Rest...>> {
typedef TypeList<First, Rest...> type;
};
// Apply :: T, TypeList<types...> -> T<types...>
template<template<typename...> class Output, typename TypeList>
struct Apply;
template<template<typename...> class Output, typename... Types>
struct Apply<Output, TypeList<Types...>> {
typedef Output<Types...> type;
};
// MapWithIndex_
template<template<size_t, typename> class Mapper, size_t CurrentIndex, typename... Args>
struct MapWithIndex_;
template<template<size_t, typename> class Mapper, size_t CurrentIndex, typename First, typename... Rest>
struct MapWithIndex_<Mapper, CurrentIndex, First, Rest...> {
typedef typename Cons<
typename Mapper<CurrentIndex, First>::type,
typename MapWithIndex_<Mapper, CurrentIndex + 1, Rest...>::type
>::type type;
};
template<template<size_t, typename> class Mapper, size_t CurrentIndex>
struct MapWithIndex_<Mapper, CurrentIndex> {
typedef TypeList<> type;
};
template<template<typename...> class Output, template<size_t, typename> class Mapper, typename... Args>
struct MapWithIndex {
typedef typename internal::Apply<
Output,
typename MapWithIndex_<Mapper, 0, Args...>::type
>::type type;
};
template<typename ArgList>
struct ArgArrayGetter;
template<typename... Args>
struct ArgArrayGetter<TypeList<Args...>> {
static const TYPEID* get() {
static constexpr TYPEID types[] = { TypeID<Args>::get()... };
return types;
}
};
// WithPolicies<...>::ArgTypeList<...>
template<typename... Policies>
struct WithPolicies {
template<size_t Index, typename T>
struct MapWithPolicies {
typedef typename ExecutePolicies<Policies...>::template With<T, Index>::type type;
};
template<typename... Args>
struct ArgTypeList {
unsigned getCount() const {
return sizeof...(Args);
}
const TYPEID* getTypes() const {
return ArgArrayGetter<
typename MapWithIndex<TypeList, MapWithPolicies, Args...>::type
>::get();
}
};
};
// BindingType<T>
template<typename T>
struct BindingType;
#define EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(type) \
template<> \
struct BindingType<type> { \
typedef type WireType; \
constexpr static WireType toWireType(const type& v) { \
return v; \
} \
constexpr static type fromWireType(WireType v) { \
return v; \
} \
}
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(char);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed char);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned char);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed short);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned short);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed int);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned int);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(signed long);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(unsigned long);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(float);
EMSCRIPTEN_DEFINE_NATIVE_BINDING_TYPE(double);
template<>
struct BindingType<void> {
typedef void WireType;
};
template<>
struct BindingType<bool> {
typedef bool WireType;
static WireType toWireType(bool b) {
return b;
}
static bool fromWireType(WireType wt) {
return wt;
}
};
template<>
struct BindingType<std::string> {
typedef struct {
size_t length;
char data[1]; // trailing data
}* WireType;
static WireType toWireType(const std::string& v) {
WireType wt = (WireType)malloc(sizeof(size_t) + v.length());
wt->length = v.length();
memcpy(wt->data, v.data(), v.length());
return wt;
}
static std::string fromWireType(WireType v) {
return std::string(v->data, v->length);
}
};
template<>
struct BindingType<std::wstring> {
typedef struct {
size_t length;
wchar_t data[1]; // trailing data
}* WireType;
static WireType toWireType(const std::wstring& v) {
WireType wt = (WireType)malloc(sizeof(size_t) + v.length() * sizeof(wchar_t));
wt->length = v.length();
wmemcpy(wt->data, v.data(), v.length());
return wt;
}
static std::wstring fromWireType(WireType v) {
return std::wstring(v->data, v->length);
}
};
template<typename T>
struct BindingType<const T> : public BindingType<T> {
};
template<typename T>
struct BindingType<T&> : public BindingType<T> {
};
template<typename T>
struct BindingType<const T&> : public BindingType<T> {
};
template<typename T>
struct BindingType<T&&> {
typedef typename BindingType<T>::WireType WireType;
static WireType toWireType(const T& v) {
return BindingType<T>::toWireType(v);
}
static T fromWireType(WireType wt) {
return BindingType<T>::fromWireType(wt);
}
};
template<typename T>
struct BindingType<T*> {
typedef T* WireType;
static WireType toWireType(T* p) {
return p;
}
static T* fromWireType(WireType wt) {
return wt;
}
};
template<typename T>
struct GenericBindingType {
typedef typename std::remove_reference<T>::type ActualT;
typedef ActualT* WireType;
static WireType toWireType(const T& v) {
return new T(v);
}
static WireType toWireType(T&& v) {
return new T(std::forward<T>(v));
}
static ActualT& fromWireType(WireType p) {
return *p;
}
};
template<typename T>
struct GenericBindingType<std::unique_ptr<T>> {
typedef typename BindingType<T*>::WireType WireType;
static WireType toWireType(std::unique_ptr<T> p) {
return BindingType<T*>::toWireType(p.release());
}
static std::unique_ptr<T> fromWireType(WireType wt) {
return std::unique_ptr<T>(BindingType<T*>::fromWireType(wt));
}
};
template<typename Enum>
struct EnumBindingType {
typedef Enum WireType;
static WireType toWireType(Enum v) {
return v;
}
static Enum fromWireType(WireType v) {
return v;
}
};
// catch-all generic binding
template<typename T>
struct BindingType : std::conditional<
std::is_enum<T>::value,
EnumBindingType<T>,
GenericBindingType<T> >::type
{};
template<typename T>
auto toWireType(T&& v) -> typename BindingType<T>::WireType {
return BindingType<T>::toWireType(std::forward<T>(v));
}
template<typename T>
constexpr bool typeSupportsMemoryView() {
return (std::is_floating_point<T>::value &&
(sizeof(T) == 4 || sizeof(T) == 8)) ||
(std::is_integral<T>::value &&
(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4));
}
}
template<typename ElementType>
struct memory_view {
memory_view() = delete;
explicit memory_view(size_t size, const ElementType* data)
: size(size)
, data(data)
{}
const size_t size; // in elements, not bytes
const void* const data;
};
// Note that 'data' is marked const just so it can accept both
// const and nonconst pointers. It is certainly possible for
// JavaScript to modify the C heap through the typed array given,
// as it merely aliases the C heap.
template<typename T>
inline memory_view<T> typed_memory_view(size_t size, const T* data) {
static_assert(internal::typeSupportsMemoryView<T>(),
"type of typed_memory_view is invalid");
return memory_view<T>(size, data);
}
namespace internal {
template<typename ElementType>
struct BindingType<memory_view<ElementType>> {
// This non-word-sized WireType only works because I
// happen to know that clang will pass aggregates as
// pointers to stack elements and we never support
// converting JavaScript typed arrays back into
// memory_view. (That is, fromWireType is not implemented
// on the C++ side, nor is toWireType implemented in
// JavaScript.)
typedef memory_view<ElementType> WireType;
static WireType toWireType(const memory_view<ElementType>& mv) {
return mv;
}
};
}
}
#endif // ~C++11 version check