Repository navigation
Expand file tree
/
Copy pathv8serialize.h
More file actions
570 lines (522 loc) · 13.1 KB
/
Copy pathv8serialize.h
File metadata and controls
570 lines (522 loc) · 13.1 KB
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
/*
* v8serialize.h
*
* Created on: Oct 8, 2011
* Author: migimunz
*/
#ifndef V8SERIALIZE_H_
#define V8SERIALIZE_H_
#include <v8.h>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <exception>
#ifndef V8SERIALIZE_NO_BOOST
///Planning to include v8::Function to boost::function conversion.
#include <boost/function.hpp>
#include <boost/shared_ptr.hpp>
#endif /*V8SERIALIZE_NO_BOOST*/
namespace v8s
{
typedef v8::Handle<v8::Object> ObjectHandle;
typedef v8::Handle<v8::Value> ValueHandle;
/**
* \class bad_conversion_exception
* Bad conversion exception
*
* Thrown when a value can't be converted to js or from js
*/
class bad_conversion_exception : public std::exception
{
const char *message;
public:
bad_conversion_exception()
:message("Conversion failed")
{
}
bad_conversion_exception(const char *message)
:message(message)
{
}
virtual const char* what() const throw()
{
return message;
}
};
class load_info;
class save_info;
/**
* \class convert
* Class template used to convert values from and to v8 values
*
* Every every specialization of this template class defines a conversion from and to a type.
* Specialized templates are defined within this file and are used to convert primitives and
* some STL types, as well as shared pointers.
*
* User types are converted in the unspecialized template by calling T::load and T::save to
* convert js->C++ and C++->js respectively.
*/
template<typename T>
struct convert
{
/**
* Converts a v8::Value to T
* @param data the v8::Value to convert
* @param result
*/
void from_js(const ValueHandle &data, T &result)
{
using namespace v8;
ObjectHandle object = ObjectHandle::Cast(data);
if(object.IsEmpty())
throw bad_conversion_exception("Cannot convert non-objects to user types");
load_info info(object);
T::load(info, result);
}
/**
* Converts T to a v8::Value
* @param value The value to convert
* @return v8::Value
*/
ValueHandle to_js(const T &value)
{
using namespace v8;
ObjectHandle ret = Object::New();
save_info info(ret);
T::save(info, value);
return ret;
}
};
/**
* \class load_info
* Helper object passed to T::load when performing user-defined conversions
* The class is a thin wrapper for a value handle, and should olny be used by
* calling load_info::get.
*
* A user doesn't need to create this object, they'll always be passed by reference to T::load
*/
struct load_info
{
ObjectHandle object;
/**
* load_info constructor
* @param value a value handle
*/
explicit load_info(const ObjectHandle &object)
:object(object) {}
/**
* Converts a property of an object named \c name to T, assigns the result of conversion to \c result,
* throws a \c bad_conversion_exception on failure.
* @param name name of the property
* @param result result of the conversion (assigned to the reference)
*/
template<typename T>
void get(const std::string &name, T &result)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
ValueHandle child = object->Get(String::New(name.c_str()));
if(try_catch.HasCaught() || child->IsUndefined())
throw bad_conversion_exception();
convert<T>().from_js(child, result);
}
/**
* Converts a property of an object named \c name to T, assigns the result of conversion to \c result,
* returns a default value \c def on failure.
* @param name name of the property
* @param result result result of the conversion (assigned to the reference)
* @param def default value to return if the conversion fails
*/
template<typename T>
void get(const std::string &name, T &result, const T &def)
{
using namespace v8;
try
{
get(name, result);
}
catch(bad_conversion_exception &e)
{
result = def;
}
}
};
/**
* \class save_info
* Helper object passed to T::save when performing user-defined conversions
* The class is a thin wrapper for an object handle, and should olny be used by
* calling load_info::set.
*
* A user doesn't need to create this object, they'll always be passed by reference to T::save
*/
struct save_info
{
ObjectHandle object;
/**
* save_info constructor
* @param object object handle
*/
save_info(const ObjectHandle &object)
:object(object)
{
}
/**
* Converts T to a v8::Value and assigns it to property named \c of it's internal v8::Object.
* If the conversion fails, /c bad_conversion_exception is thrown
* @param name name of the property
* @param svalue value to convert
*/
template<typename T>
void set(const std::string &name, const T &svalue)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
if(object.IsEmpty()) //if handle is not bound to an object
throw bad_conversion_exception();
object->Set(String::New(name.c_str()), convert<T>().to_js(svalue));
if(try_catch.HasCaught())
throw bad_conversion_exception();
}
};
/**
* Serializes a v8::Value to a C++ value using defined conversion methods.
* @param value value to convert
* @param result result of the conversion is assigned to this
*/
template<typename T>
void from_js(const ValueHandle &value, T &result)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
convert<T>().from_js(value, result);
if(try_catch.HasCaught())
throw bad_conversion_exception();
}
/**
* Serializes a C++ type to v8::Value using defined conversion methods.
* @param value C++ value to serialize to v8::Value
* @return resulting v8::Value
*/
template<typename T>
ValueHandle to_js(const T &value)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
ValueHandle ret = convert<T>().to_js(value);
if(try_catch.HasCaught())
throw bad_conversion_exception();
else
return scope.Close(ret);
}
//Primitive conversions
/**
* A specialization of \c convert class template used to convert int16_t to and from v8::Value
*/
template<>
struct convert< int16_t >
{
void from_js(const ValueHandle &data, int16_t &result)
{
if(data->IsNumber())
result = data->Int32Value();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const int16_t &value)
{
return v8::Integer::New(value);
}
};
/**
* A specialization of \c convert class template used to convert uint16_t to and from v8::Value
*/
template<>
struct convert< uint16_t >
{
void from_js(const ValueHandle &data, uint16_t &result)
{
if(data->IsNumber())
result = data->Uint32Value();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const uint16_t &value)
{
return v8::Integer::NewFromUnsigned(value);
}
};
/**
* A specialization of \c convert class template used to convert int32_t to and from v8::Value
*/
template<>
struct convert< int32_t >
{
void from_js(const ValueHandle &data, int32_t &result)
{
if(data->IsNumber())
result = data->Int32Value();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const int32_t &value)
{
return v8::Integer::New(value);
}
};
/**
* A specialization of \c convert class template used to convert uint32_t to and from v8::Value
*/
template<>
struct convert< uint32_t >
{
void from_js(const ValueHandle &data, uint32_t &result)
{
if(data->IsNumber())
result = data->Uint32Value();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const uint32_t &value)
{
return v8::Integer::NewFromUnsigned(value);
}
};
/**
* A specialization of \c convert class template used to convert int64_t to and from v8::Value
*/
template<>
struct convert< int64_t >
{
void from_js(const ValueHandle &data, int64_t &result)
{
if(data->IsNumber())
result = data->IntegerValue();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const int64_t &value)
{
return v8::Number::New(static_cast<double>(value));
}
};
/**
* A specialization of \c convert class template used to convert uint64_t to and from v8::Value
*/
template<>
struct convert< uint64_t >
{
void from_js(const ValueHandle &data, uint64_t &result)
{
if(data->IsNumber())
result = data->IntegerValue();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const uint64_t &value)
{
return v8::Number::New(static_cast<double>(value));
}
};
/**
* A specialization of \c convert class template used to convert double to and from v8::Value
*/
template<>
struct convert< double >
{
void from_js(const ValueHandle &data, double &result)
{
if(data->IsNumber())
result = data->NumberValue();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const double &value)
{
return v8::Number::New(value);
}
};
/**
* A specialization of \c convert class template used to convert float to and from v8::Value
*/
template<>
struct convert< float >
{
void from_js(const ValueHandle &data, float &result)
{
if(data->IsNumber())
result = data->NumberValue();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const float &value)
{
return v8::Number::New(value);
}
};
/**
* A specialization of \c convert class template used to convert bool to and from v8::Value
*/
template<>
struct convert< bool >
{
void from_js(const ValueHandle &data, bool &result)
{
if(data->IsBoolean())
result = data->BooleanValue();
else
throw bad_conversion_exception();
}
ValueHandle to_js(const bool &value)
{
return v8::Boolean::New(value);
}
};
/**
* A specialization of \c convert class template used to convert std::string to and from v8::Value
*/
template<>
struct convert< std::string >
{
void from_js(const ValueHandle &data, std::string &result)
{
using namespace v8;
if(data->IsString())
{
String::Utf8Value utf8_value(Handle<String>::Cast(data));
result = *utf8_value;
}
else
throw bad_conversion_exception();
}
ValueHandle to_js(const std::string &value)
{
return v8::String::New(value.c_str());
}
};
/**
* A specialization of \c convert class template used to convert std::map<std::string, T> to and from v8::Value.
* This specializations converts a std::map to and from \c v8::Object.
*/
template<typename T>
struct convert< std::map<std::string, T> >
{
void from_js(const ValueHandle &data, std::map<std::string, T> &result)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
Handle<Object> object = Handle<Object>::Cast(data);
if(object.IsEmpty() || try_catch.HasCaught())
throw bad_conversion_exception();
Handle<Array> names = object->GetOwnPropertyNames();
uint32_t length = names->Length();
for(uint32_t i = 0; i < length; ++i)
{
Handle<String> js_name = Handle<String>::Cast(names->Get(i));
String::Utf8Value name(js_name);
std::string first(*name);
ValueHandle value = object->Get(js_name);
T second;
convert<T>().from_js(value, second);
result[first] = second;
}
}
ValueHandle to_js(const std::map<std::string, T> &value)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
ObjectHandle object = Object::New();
typename std::map<std::string, T>::const_iterator iter;
for(iter = value.begin(); iter != value.end(); iter++)
{
ValueHandle ret = convert<T>().to_js(iter->second);
object->Set(String::New(iter->first.c_str()), ret);
}
return scope.Close(object);
}
};
/**
* \c convert_list
* A helper class template used to convert any collection that exposes a forward iterator with
* \c begin() and \c end() and methods \c size() and \c push_back(). Currently used to convert
* \c std::vector and \c std::list to and from \c v8::Value
*/
template<typename C, typename T>
struct convert_list
{
void from_js(const ValueHandle &data, C &result)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
ValueHandle value = data;
Handle<Array> array = Handle<Array>::Cast(value);
if(array.IsEmpty() || try_catch.HasCaught())
throw bad_conversion_exception();
uint32_t length = array->Length();
for(uint32_t i = 0; i < length; ++i)
{
T element;
convert<T>().from_js(array->Get(i), element);
result.push_back(element);
}
}
ValueHandle to_js(const C &value)
{
using namespace v8;
HandleScope scope;
TryCatch try_catch;
Handle<Array> array = Array::New(value.size());
typename C::const_iterator iter;
uint32_t i = 0;
for(iter = value.begin(); iter != value.end(); iter++)
{
array->Set(i, convert<T>().to_js(*iter));
i++;
}
return scope.Close(array);
}
};
/**
* A specialization of \c convert class template used to convert std::list<T> to and from v8::Value
*/
template<typename T>
struct convert< std::list<T> > : public convert_list< std::list<T>, T >
{
};
/**
* A specialization of \c convert class template used to convert std::vector<T> to and from v8::Value
*/
template<typename T>
struct convert< std::vector<T> > : public convert_list< std::vector<T>, T >
{
};
#ifndef V8SERIALIZE_NO_BOOST
/**
* A specialization of \c convert class template used to convert a \c boost::shared_ptr<T> to and from a \c v8::Value .
* When deserializing (converting from js to C++ types), an object of type T will be allocated using \c new , and assigned to
* \c result.
*/
template<typename T>
struct convert< boost::shared_ptr<T> >
{
void from_js(const ValueHandle &data, boost::shared_ptr<T> &result)
{
using boost::shared_ptr;
result = shared_ptr<T>(new T());
convert<T>().from_js(data, *result);
}
ValueHandle to_js(const boost::shared_ptr<T> &value)
{
return convert<T>().to_js(*value);
}
};
#endif /*V8SERIALIZE_NO_BOOST*/
}
#endif /* V8SERIALIZE_H_ */