nan_converters.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #ifndef NAN_CONVERTERS_H_
  9. #define NAN_CONVERTERS_H_
  10. namespace imp {
  11. template<typename T> struct ToFactoryBase {
  12. typedef MaybeLocal<T> return_t;
  13. };
  14. template<typename T> struct ValueFactoryBase { typedef Maybe<T> return_t; };
  15. template<typename T> struct ToFactory;
  16. template<>
  17. struct ToFactory<v8::Function> : ToFactoryBase<v8::Function> {
  18. static inline return_t convert(v8::Local<v8::Value> val) {
  19. if (val.IsEmpty() || !val->IsFunction()) return MaybeLocal<v8::Function>();
  20. return MaybeLocal<v8::Function>(val.As<v8::Function>());
  21. }
  22. };
  23. #define X(TYPE) \
  24. template<> \
  25. struct ToFactory<v8::TYPE> : ToFactoryBase<v8::TYPE> { \
  26. static inline return_t convert(v8::Local<v8::Value> val); \
  27. };
  28. X(Boolean)
  29. X(Number)
  30. X(String)
  31. X(Object)
  32. X(Integer)
  33. X(Uint32)
  34. X(Int32)
  35. #undef X
  36. #define X(TYPE) \
  37. template<> \
  38. struct ToFactory<TYPE> : ValueFactoryBase<TYPE> { \
  39. static inline return_t convert(v8::Local<v8::Value> val); \
  40. };
  41. X(bool)
  42. X(double)
  43. X(int64_t)
  44. X(uint32_t)
  45. X(int32_t)
  46. #undef X
  47. } // end of namespace imp
  48. template<typename T>
  49. inline
  50. typename imp::ToFactory<T>::return_t To(v8::Local<v8::Value> val) {
  51. return imp::ToFactory<T>::convert(val);
  52. }
  53. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  54. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  55. # include "nan_converters_43_inl.h"
  56. #else
  57. # include "nan_converters_pre_43_inl.h"
  58. #endif
  59. #endif // NAN_CONVERTERS_H_