nan_maybe_pre_43_inl.h 7.6 KB

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