nan_maybe_pre_43_inl.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2017 NAN contributors
  5. *
  6. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  7. ********************************************************************/
  8. #ifndef NAN_MAYBE_PRE_43_INL_H_
  9. #define NAN_MAYBE_PRE_43_INL_H_
  10. template<typename T>
  11. class MaybeLocal {
  12. public:
  13. inline MaybeLocal() : val_(v8::Local<T>()) {}
  14. template<typename S>
  15. # if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
  16. inline
  17. MaybeLocal(v8::Local<S> that) : val_(that) {} // NOLINT(runtime/explicit)
  18. # else
  19. inline
  20. MaybeLocal(v8::Local<S> that) : // NOLINT(runtime/explicit)
  21. val_(*reinterpret_cast<v8::Local<T>*>(&that)) {}
  22. # endif
  23. inline bool IsEmpty() const { return val_.IsEmpty(); }
  24. template<typename S>
  25. inline bool ToLocal(v8::Local<S> *out) const {
  26. *out = val_;
  27. return !IsEmpty();
  28. }
  29. inline v8::Local<T> ToLocalChecked() const {
  30. #if defined(V8_ENABLE_CHECKS)
  31. assert(!IsEmpty() && "ToLocalChecked is Empty");
  32. #endif // V8_ENABLE_CHECKS
  33. return val_;
  34. }
  35. template<typename S>
  36. inline v8::Local<S> FromMaybe(v8::Local<S> default_value) const {
  37. return IsEmpty() ? default_value : val_;
  38. }
  39. private:
  40. v8::Local<T> val_;
  41. };
  42. template<typename T>
  43. class Maybe {
  44. public:
  45. inline bool IsNothing() const { return !has_value_; }
  46. inline bool IsJust() const { return has_value_; }
  47. inline T FromJust() const {
  48. #if defined(V8_ENABLE_CHECKS)
  49. assert(IsJust() && "FromJust is Nothing");
  50. #endif // V8_ENABLE_CHECKS
  51. return value_;
  52. }
  53. inline T FromMaybe(const T& default_value) const {
  54. return has_value_ ? value_ : default_value;
  55. }
  56. inline bool operator==(const Maybe &other) const {
  57. return (IsJust() == other.IsJust()) &&
  58. (!IsJust() || FromJust() == other.FromJust());
  59. }
  60. inline bool operator!=(const Maybe &other) const {
  61. return !operator==(other);
  62. }
  63. private:
  64. Maybe() : has_value_(false) {}
  65. explicit Maybe(const T& t) : has_value_(true), value_(t) {}
  66. bool has_value_;
  67. T value_;
  68. template<typename U>
  69. friend Maybe<U> Nothing();
  70. template<typename U>
  71. friend Maybe<U> Just(const U& u);
  72. };
  73. template<typename T>
  74. inline Maybe<T> Nothing() {
  75. return Maybe<T>();
  76. }
  77. template<typename T>
  78. inline Maybe<T> Just(const T& t) {
  79. return Maybe<T>(t);
  80. }
  81. inline
  82. MaybeLocal<v8::String> ToDetailString(v8::Handle<v8::Value> val) {
  83. return MaybeLocal<v8::String>(val->ToDetailString());
  84. }
  85. inline
  86. MaybeLocal<v8::Uint32> ToArrayIndex(v8::Handle<v8::Value> val) {
  87. return MaybeLocal<v8::Uint32>(val->ToArrayIndex());
  88. }
  89. inline
  90. Maybe<bool> Equals(v8::Handle<v8::Value> a, v8::Handle<v8::Value>(b)) {
  91. return Just<bool>(a->Equals(b));
  92. }
  93. inline
  94. MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::Function> h) {
  95. return MaybeLocal<v8::Object>(h->NewInstance());
  96. }
  97. inline
  98. MaybeLocal<v8::Object> NewInstance(
  99. v8::Local<v8::Function> h
  100. , int argc
  101. , v8::Local<v8::Value> argv[]) {
  102. return MaybeLocal<v8::Object>(h->NewInstance(argc, argv));
  103. }
  104. inline
  105. MaybeLocal<v8::Object> NewInstance(v8::Handle<v8::ObjectTemplate> h) {
  106. return MaybeLocal<v8::Object>(h->NewInstance());
  107. }
  108. inline
  109. MaybeLocal<v8::Function> GetFunction(v8::Handle<v8::FunctionTemplate> t) {
  110. return MaybeLocal<v8::Function>(t->GetFunction());
  111. }
  112. inline Maybe<bool> Set(
  113. v8::Handle<v8::Object> obj
  114. , v8::Handle<v8::Value> key
  115. , v8::Handle<v8::Value> value) {
  116. return Just<bool>(obj->Set(key, value));
  117. }
  118. inline Maybe<bool> Set(
  119. v8::Handle<v8::Object> obj
  120. , uint32_t index
  121. , v8::Handle<v8::Value> value) {
  122. return Just<bool>(obj->Set(index, value));
  123. }
  124. inline Maybe<bool> ForceSet(
  125. v8::Handle<v8::Object> obj
  126. , v8::Handle<v8::Value> key
  127. , v8::Handle<v8::Value> value
  128. , v8::PropertyAttribute attribs = v8::None) {
  129. return Just<bool>(obj->ForceSet(key, value, attribs));
  130. }
  131. inline MaybeLocal<v8::Value> Get(
  132. v8::Handle<v8::Object> obj
  133. , v8::Handle<v8::Value> key) {
  134. return MaybeLocal<v8::Value>(obj->Get(key));
  135. }
  136. inline MaybeLocal<v8::Value> Get(
  137. v8::Handle<v8::Object> obj
  138. , uint32_t index) {
  139. return MaybeLocal<v8::Value>(obj->Get(index));
  140. }
  141. inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
  142. v8::Handle<v8::Object> obj
  143. , v8::Handle<v8::Value> key) {
  144. return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
  145. }
  146. inline Maybe<bool> Has(
  147. v8::Handle<v8::Object> obj
  148. , v8::Handle<v8::String> key) {
  149. return Just<bool>(obj->Has(key));
  150. }
  151. inline Maybe<bool> Has(
  152. v8::Handle<v8::Object> obj
  153. , uint32_t index) {
  154. return Just<bool>(obj->Has(index));
  155. }
  156. inline Maybe<bool> Delete(
  157. v8::Handle<v8::Object> obj
  158. , v8::Handle<v8::String> key) {
  159. return Just<bool>(obj->Delete(key));
  160. }
  161. inline Maybe<bool> Delete(
  162. v8::Handle<v8::Object> obj
  163. , uint32_t index) {
  164. return Just<bool>(obj->Delete(index));
  165. }
  166. inline
  167. MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
  168. return MaybeLocal<v8::Array>(obj->GetPropertyNames());
  169. }
  170. inline
  171. MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
  172. return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
  173. }
  174. inline Maybe<bool> SetPrototype(
  175. v8::Handle<v8::Object> obj
  176. , v8::Handle<v8::Value> prototype) {
  177. return Just<bool>(obj->SetPrototype(prototype));
  178. }
  179. inline MaybeLocal<v8::String> ObjectProtoToString(
  180. v8::Handle<v8::Object> obj) {
  181. return MaybeLocal<v8::String>(obj->ObjectProtoToString());
  182. }
  183. inline Maybe<bool> HasOwnProperty(
  184. v8::Handle<v8::Object> obj
  185. , v8::Handle<v8::String> key) {
  186. return Just<bool>(obj->HasOwnProperty(key));
  187. }
  188. inline Maybe<bool> HasRealNamedProperty(
  189. v8::Handle<v8::Object> obj
  190. , v8::Handle<v8::String> key) {
  191. return Just<bool>(obj->HasRealNamedProperty(key));
  192. }
  193. inline Maybe<bool> HasRealIndexedProperty(
  194. v8::Handle<v8::Object> obj
  195. , uint32_t index) {
  196. return Just<bool>(obj->HasRealIndexedProperty(index));
  197. }
  198. inline Maybe<bool> HasRealNamedCallbackProperty(
  199. v8::Handle<v8::Object> obj
  200. , v8::Handle<v8::String> key) {
  201. return Just<bool>(obj->HasRealNamedCallbackProperty(key));
  202. }
  203. inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
  204. v8::Handle<v8::Object> obj
  205. , v8::Handle<v8::String> key) {
  206. return MaybeLocal<v8::Value>(
  207. obj->GetRealNamedPropertyInPrototypeChain(key));
  208. }
  209. inline MaybeLocal<v8::Value> GetRealNamedProperty(
  210. v8::Handle<v8::Object> obj
  211. , v8::Handle<v8::String> key) {
  212. return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
  213. }
  214. inline MaybeLocal<v8::Value> CallAsFunction(
  215. v8::Handle<v8::Object> obj
  216. , v8::Handle<v8::Object> recv
  217. , int argc
  218. , v8::Handle<v8::Value> argv[]) {
  219. return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
  220. }
  221. inline MaybeLocal<v8::Value> CallAsConstructor(
  222. v8::Handle<v8::Object> obj
  223. , int argc
  224. , v8::Local<v8::Value> argv[]) {
  225. return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
  226. }
  227. inline
  228. MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
  229. return MaybeLocal<v8::String>(msg->GetSourceLine());
  230. }
  231. inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
  232. return Just<int>(msg->GetLineNumber());
  233. }
  234. inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
  235. return Just<int>(msg->GetStartColumn());
  236. }
  237. inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
  238. return Just<int>(msg->GetEndColumn());
  239. }
  240. inline MaybeLocal<v8::Object> CloneElementAt(
  241. v8::Handle<v8::Array> array
  242. , uint32_t index) {
  243. return MaybeLocal<v8::Object>(array->CloneElementAt(index));
  244. }
  245. inline MaybeLocal<v8::Value> Call(
  246. v8::Local<v8::Function> fun
  247. , v8::Local<v8::Object> recv
  248. , int argc
  249. , v8::Local<v8::Value> argv[]) {
  250. return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
  251. }
  252. #endif // NAN_MAYBE_PRE_43_INL_H_