UNPKG

2.11 kBtext/x-cView Raw
1/*********************************************************************
2 * NAN - Native Abstractions for Node.js
3 *
4 * Copyright (c) 2018 NAN contributors
5 *
6 * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
7 ********************************************************************/
8
9#ifndef NAN_CONVERTERS_H_
10#define NAN_CONVERTERS_H_
11
12namespace imp {
13template<typename T> struct ToFactoryBase {
14 typedef MaybeLocal<T> return_t;
15};
16template<typename T> struct ValueFactoryBase { typedef Maybe<T> return_t; };
17
18template<typename T> struct ToFactory;
19
20template<>
21struct ToFactory<v8::Function> : ToFactoryBase<v8::Function> {
22 static inline return_t convert(v8::Local<v8::Value> val) {
23 if (val.IsEmpty() || !val->IsFunction()) return MaybeLocal<v8::Function>();
24 return MaybeLocal<v8::Function>(val.As<v8::Function>());
25 }
26};
27
28#define X(TYPE) \
29 template<> \
30 struct ToFactory<v8::TYPE> : ToFactoryBase<v8::TYPE> { \
31 static inline return_t convert(v8::Local<v8::Value> val); \
32 };
33
34X(Boolean)
35X(Number)
36X(String)
37X(Object)
38X(Integer)
39X(Uint32)
40X(Int32)
41
42#undef X
43
44#define X(TYPE) \
45 template<> \
46 struct ToFactory<TYPE> : ValueFactoryBase<TYPE> { \
47 static inline return_t convert(v8::Local<v8::Value> val); \
48 };
49
50X(bool)
51X(double)
52X(int64_t)
53X(uint32_t)
54X(int32_t)
55
56#undef X
57} // end of namespace imp
58
59template<typename T>
60inline
61typename imp::ToFactory<T>::return_t To(v8::Local<v8::Value> val) {
62 return imp::ToFactory<T>::convert(val);
63}
64
65#if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
66 (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
67# include "nan_converters_43_inl.h"
68#else
69# include "nan_converters_pre_43_inl.h"
70#endif
71
72#endif // NAN_CONVERTERS_H_