-
Notifications
You must be signed in to change notification settings - Fork 2
/
nan-marshal.h
597 lines (497 loc) · 16.8 KB
/
nan-marshal.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*********************************************************************
* NAN Marshal - Data type marshalling for NAN module
*
* Copyright (c) 2015 NAN-Marshal contributors:
* - Ievgen Khvedchenia <https://github.com/BloodAxe>
*
* MIT License <https://github.com/BloodAxe/nan-Marshal/blob/master/LICENSE.md>
*
* Version 0.0.1: current Node 4.0.0, Node 12: 0.12.7, Node 10: 0.10.40, iojs: 3.2.0
*
* See https://github.com/BloodAxe/nan-marshal for the latest update to this file
**********************************************************************************/
#pragma once
#include <nan.h>
#include <node_buffer.h>
#include <complex>
#include <functional>
#include <iostream>
#include <sstream>
#include <array>
#include <memory>
#include <map>
#include <stdexcept>
#if _MSC_VER
#define NANMARSHAL_NOTHROW _THROW0()
#else
#define NANMARSHAL_NOTHROW noexcept
#endif
namespace Nan
{
typedef v8::Local<v8::Value> V8Result;
template <typename T>
T Marshal(V8Result val);
template <typename T>
V8Result Marshal(const T& val);
class MarshalException : public std::runtime_error
{
public:
const char * what() const NANMARSHAL_NOTHROW override
{
return m_message.c_str();
}
inline MarshalException(const char * message)
: std::runtime_error("MarshalTypeMismatchException")
, m_message(message)
{
}
private:
std::string m_message;
};
namespace marshal
{
struct access
{
template<typename Archive, typename T>
static inline void serialize(Archive& ar, T& type)
{
type.serialize(ar);
}
};
template <typename T>
struct nvp_struct
{
const char * name;
T& value;
explicit inline nvp_struct(const char * name_, T & value_)
: name(name_)
, value(value_)
{}
inline nvp_struct(const nvp_struct & rhs)
: name(rhs.name)
, value(rhs.value)
{}
};
template <typename T>
inline nvp_struct<T> make_nvp(const char * name, T& val)
{
return nvp_struct<T>(name, val);
}
template <typename T>
inline nvp_struct<T> make_nvp(const char * name, const T& val)
{
return nvp_struct<T>(name, const_cast<T&>(val));
}
template <typename T>
inline nvp_struct<T> make_nvp(const char * name, T* val) = delete;
template <typename T>
inline nvp_struct<T> make_nvp(const char * name, const T* val) = delete;
template<typename Archive, typename T>
inline void serialize(Archive& ar, T& type)
{
access::serialize(ar, type);
}
template<typename T>
struct Serializer
{
template<typename InputArchive>
static inline void load(InputArchive& ar, T& val)
{
marshal::serialize(ar, val);
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const T& val)
{
marshal::serialize(ar, const_cast<T&>(val));
}
};
#define BASIC_TYPE_SERIALIZER(type)\
template<> \
struct Serializer<type> \
{\
template<typename InputArchive>\
static inline void load(InputArchive& ar, type& val)\
{\
ar.load(val); \
}\
template<typename OutputArchive>\
static inline void save(OutputArchive& ar, const type& val)\
{\
ar.save(val); \
}\
}
#define ENUM_SERIALIZER(type)\
template<>\
struct Serializer<type>\
{\
template<typename InputArchive>\
static inline void load(InputArchive& ar, type& val)\
{\
int int_val; \
ar & int_val; \
val = (type)int_val; \
}\
template<typename OutputArchive>\
static inline void save(OutputArchive& ar, const type& val)\
{\
int int_val = (int)val; \
ar & int_val; \
}\
}
// declare serializers for simple types
BASIC_TYPE_SERIALIZER(char);
BASIC_TYPE_SERIALIZER(unsigned char);
BASIC_TYPE_SERIALIZER(short);
BASIC_TYPE_SERIALIZER(unsigned short);
BASIC_TYPE_SERIALIZER(int);
BASIC_TYPE_SERIALIZER(unsigned int);
BASIC_TYPE_SERIALIZER(long);
BASIC_TYPE_SERIALIZER(unsigned long);
BASIC_TYPE_SERIALIZER(unsigned long long);
BASIC_TYPE_SERIALIZER(float);
BASIC_TYPE_SERIALIZER(double);
BASIC_TYPE_SERIALIZER(bool);
template<typename T>
struct Serializer< std::shared_ptr<T> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::shared_ptr<T>& val)
{
val.reset(new T);
T& _val = *val.get();
ar & _val;
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::shared_ptr<T>& val)
{
const T& _val = *val.get();
ar & _val;
}
};
// serializer for std::vector
template<typename T>
struct Serializer < std::vector<T> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::vector<T>& val)
{
int N = ar.template As<v8::Array>()->Length();
val.resize(N);
for (int i = 0; i < N; i++)
{
V8Result item = ar.template As<v8::Array>()->Get(i);
val[i] = Marshal<T>(item);
}
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::vector<T>& val)
{
auto result = Nan::New<v8::Array>((int)val.size());
for (uint32_t i = 0; i < val.size(); i++)
{
const T& item = val[i];
result->Set(i, Marshal(item));
}
ar = result;
}
};
// serializer for std::pair
template<typename K, typename V>
struct Serializer < std::pair<K, V> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::pair<K, V>& val)
{
ar & make_nvp("key", val.first);
ar & make_nvp("value", val.second);
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::pair<K, V>& val)
{
ar & make_nvp("key", val.first);
ar & make_nvp("value", val.second);
}
};
template<>
struct Serializer < std::string >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::string& val)
{
size_t len = Nan::DecodeBytes(ar, Nan::ASCII);
val.resize(len);
Nan::DecodeWrite(const_cast<char*>(val.data()), len, ar, Nan::ASCII);
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::string& val)
{
ar = Nan::New<v8::String>(val).ToLocalChecked();
}
};
// serializer for std::map
template<typename K, typename V>
struct Serializer < std::map<K, V> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::map<K, V>& map_val)
{
int N = ar.template As<v8::Array>()->Length();
for (int i = 0; i < N; i++)
{
V8Result item = ar.template As<v8::Array>()->Get(i);
map_val.insert(Marshal< std::pair<K, V> >(item));
}
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::map<K, V>& map_val)
{
v8::Local<v8::Array> result = Nan::New<v8::Array>();
uint32_t idx = 0;
for (typename std::map<K, V>::const_iterator i = map_val.begin(); i != map_val.end(); ++i, ++idx)
{
result->Set(idx, Marshal(*i));
}
ar = result;
}
};
// serializer for std::complex
template<typename T>
struct Serializer < std::complex<T> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::complex<T>& val)
{
T real, imag;
ar & make_nvp("real", real);
ar & make_nvp("imag", imag);
val = std::complex<T>(real, imag);
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::complex<T>& val)
{
T real = val.real();
T imag = val.imag();
ar & make_nvp("real", real);
ar & make_nvp("imag", imag);
}
};
template<typename T>
struct Serializer < nvp_struct<T> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, nvp_struct<T>& val)
{
ar.load(val);
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const nvp_struct<T>& val)
{
ar.save(val);
}
};
template<typename T, int N>
struct Serializer < T[N] >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, T(&val)[N])
{
for (int i = 0; i < N; i++)
{
V8Result item = ar.template As<v8::Array>()->Get(i);
val[i] = Marshal<T>(item);
}
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, T const (&val)[N])
{
v8::Local<v8::Array> result = Nan::New<v8::Array>(N);
for (uint32_t i = 0; i < N; i++)
{
const T& item = val[i];
result->Set(i, Marshal(item));
}
ar = result;
}
};
template<typename T, std::size_t N>
struct Serializer < std::array<T, N> >
{
template<typename InputArchive>
static inline void load(InputArchive& ar, std::array<T, N>& val)
{
for (int i = 0; i < N; i++)
{
V8Result item = ar.template As<v8::Array>()->Get(i);
val[i] = Marshal<T>(item);
}
}
template<typename OutputArchive>
static inline void save(OutputArchive& ar, const std::array<T, N>& val)
{
v8::Local<v8::Array> result = Nan::New<v8::Array>(static_cast<int>(N));
for (uint32_t i = 0; i < N; i++)
{
const T& item = val[i];
result->Set(i, Marshal(item));
}
ar = result;
}
};
template <bool C_>
struct bool_ {
static const bool value = C_;
typedef bool value_type;
};
class SaveArchive
{
public:
typedef v8::Local<v8::Value> V8Result;
inline SaveArchive()
{
}
inline SaveArchive(V8Result& dst) : _dst(dst)
{
}
inline ~SaveArchive()
{
}
typedef bool_<false> is_loading;
typedef bool_<true> is_saving;
template<typename T>
inline SaveArchive& operator& (const T& val)
{
Serializer<T>::save(*this, val);
return *this;
}
template<typename T>
inline void save(const T& val)
{
_dst = Nan::New(val);
}
template<typename T>
inline void save(const nvp_struct<T>& val)
{
if (_dst.IsEmpty() || !_dst->IsObject())
{
_dst = Nan::New<v8::Object>();
}
_dst->ToObject()->Set(Nan::New<v8::String>(val.name).ToLocalChecked(), Marshal(val.value));
}
template<typename T>
void save(T* const& val) = delete;
inline SaveArchive& operator=(V8Result newVal)
{
_dst = newVal;
return *this;
}
inline operator V8Result()
{
return _dst;
}
V8Result _dst;
private:
};
class LoadArchive
{
public:
inline LoadArchive(V8Result src)
: _src(src)
{
}
inline ~LoadArchive()
{
}
typedef bool_<true> is_loading;
typedef bool_<false> is_saving;
inline V8Result target() const
{
return _src;
}
template<typename T>
inline LoadArchive& operator& (const T& val)
{
Serializer<T>::load(*this, const_cast<T&>(val));
return *this;
}
template<typename T>
void load(T& val);
template<typename T>
void load(T*& val) = delete;
template<typename T>
inline void load(nvp_struct<T>& val)
{
if (!_src->IsObject())
{
throw MarshalException("Underlying instance is not an object");
}
auto prop = _src->ToObject()->Get(Nan::New<v8::String>(val.name).ToLocalChecked());
if (prop.IsEmpty())
{
throw MarshalException("Object does not contains property");
}
val.value = Marshal<T>(prop);
}
template <typename T>
inline v8::Local<T> As()
{
return _src.template As<T>();
}
inline operator V8Result()
{
return _src;
}
private:
V8Result _src;
};
template<>
inline void LoadArchive::load(bool& val)
{
if (!_src->IsBoolean())
throw MarshalException("Argument is not a boolean");
val = _src->BooleanValue();
}
template<>
inline void LoadArchive::load(int& val)
{
if (!_src->IsInt32())
throw MarshalException("Argument is not a number");
val = _src->Int32Value();
}
template<>
inline void LoadArchive::load(float& val)
{
if (!_src->IsNumber())
throw MarshalException("Argument is not a number");
val = (float)_src->NumberValue();
}
template<>
inline void LoadArchive::load(double& val)
{
if (!_src->IsNumber())
throw MarshalException("Argument is not a number");
val = (double)_src->NumberValue();
}
template<>
inline void LoadArchive::load(uint32_t& val)
{
val = (uint32_t)_src->Uint32Value();
}
}
// Marshal functions implementation
template <typename T>
inline T Marshal(V8Result val)
{
marshal::LoadArchive ia(val);
T loaded = T();
ia & loaded;
return std::move(loaded);
}
template <typename T>
inline V8Result Marshal(const T& val)
{
Nan::EscapableHandleScope scope;
marshal::SaveArchive oa;
oa & val;
return scope.Escape(oa._dst);
}
}