UI for Zipcoin Blue

nan_maybe_pre_43_inl.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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_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 : v8::Local<S>(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. #include "nan_define_own_property_helper.h" // NOLINT(build/include)
  125. inline Maybe<bool> DefineOwnProperty(
  126. v8::Handle<v8::Object> obj
  127. , v8::Handle<v8::String> key
  128. , v8::Handle<v8::Value> value
  129. , v8::PropertyAttribute attribs = v8::None) {
  130. v8::PropertyAttribute current = obj->GetPropertyAttributes(key);
  131. return imp::DefineOwnPropertyHelper(current, obj, key, value, attribs);
  132. }
  133. NAN_DEPRECATED inline Maybe<bool> ForceSet(
  134. v8::Handle<v8::Object> obj
  135. , v8::Handle<v8::Value> key
  136. , v8::Handle<v8::Value> value
  137. , v8::PropertyAttribute attribs = v8::None) {
  138. return Just<bool>(obj->ForceSet(key, value, attribs));
  139. }
  140. inline MaybeLocal<v8::Value> Get(
  141. v8::Handle<v8::Object> obj
  142. , v8::Handle<v8::Value> key) {
  143. return MaybeLocal<v8::Value>(obj->Get(key));
  144. }
  145. inline MaybeLocal<v8::Value> Get(
  146. v8::Handle<v8::Object> obj
  147. , uint32_t index) {
  148. return MaybeLocal<v8::Value>(obj->Get(index));
  149. }
  150. inline Maybe<v8::PropertyAttribute> GetPropertyAttributes(
  151. v8::Handle<v8::Object> obj
  152. , v8::Handle<v8::Value> key) {
  153. return Just<v8::PropertyAttribute>(obj->GetPropertyAttributes(key));
  154. }
  155. inline Maybe<bool> Has(
  156. v8::Handle<v8::Object> obj
  157. , v8::Handle<v8::String> key) {
  158. return Just<bool>(obj->Has(key));
  159. }
  160. inline Maybe<bool> Has(
  161. v8::Handle<v8::Object> obj
  162. , uint32_t index) {
  163. return Just<bool>(obj->Has(index));
  164. }
  165. inline Maybe<bool> Delete(
  166. v8::Handle<v8::Object> obj
  167. , v8::Handle<v8::String> key) {
  168. return Just<bool>(obj->Delete(key));
  169. }
  170. inline Maybe<bool> Delete(
  171. v8::Handle<v8::Object> obj
  172. , uint32_t index) {
  173. return Just<bool>(obj->Delete(index));
  174. }
  175. inline
  176. MaybeLocal<v8::Array> GetPropertyNames(v8::Handle<v8::Object> obj) {
  177. return MaybeLocal<v8::Array>(obj->GetPropertyNames());
  178. }
  179. inline
  180. MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Handle<v8::Object> obj) {
  181. return MaybeLocal<v8::Array>(obj->GetOwnPropertyNames());
  182. }
  183. inline Maybe<bool> SetPrototype(
  184. v8::Handle<v8::Object> obj
  185. , v8::Handle<v8::Value> prototype) {
  186. return Just<bool>(obj->SetPrototype(prototype));
  187. }
  188. inline MaybeLocal<v8::String> ObjectProtoToString(
  189. v8::Handle<v8::Object> obj) {
  190. return MaybeLocal<v8::String>(obj->ObjectProtoToString());
  191. }
  192. inline Maybe<bool> HasOwnProperty(
  193. v8::Handle<v8::Object> obj
  194. , v8::Handle<v8::String> key) {
  195. return Just<bool>(obj->HasOwnProperty(key));
  196. }
  197. inline Maybe<bool> HasRealNamedProperty(
  198. v8::Handle<v8::Object> obj
  199. , v8::Handle<v8::String> key) {
  200. return Just<bool>(obj->HasRealNamedProperty(key));
  201. }
  202. inline Maybe<bool> HasRealIndexedProperty(
  203. v8::Handle<v8::Object> obj
  204. , uint32_t index) {
  205. return Just<bool>(obj->HasRealIndexedProperty(index));
  206. }
  207. inline Maybe<bool> HasRealNamedCallbackProperty(
  208. v8::Handle<v8::Object> obj
  209. , v8::Handle<v8::String> key) {
  210. return Just<bool>(obj->HasRealNamedCallbackProperty(key));
  211. }
  212. inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
  213. v8::Handle<v8::Object> obj
  214. , v8::Handle<v8::String> key) {
  215. return MaybeLocal<v8::Value>(
  216. obj->GetRealNamedPropertyInPrototypeChain(key));
  217. }
  218. inline MaybeLocal<v8::Value> GetRealNamedProperty(
  219. v8::Handle<v8::Object> obj
  220. , v8::Handle<v8::String> key) {
  221. return MaybeLocal<v8::Value>(obj->GetRealNamedProperty(key));
  222. }
  223. inline MaybeLocal<v8::Value> CallAsFunction(
  224. v8::Handle<v8::Object> obj
  225. , v8::Handle<v8::Object> recv
  226. , int argc
  227. , v8::Handle<v8::Value> argv[]) {
  228. return MaybeLocal<v8::Value>(obj->CallAsFunction(recv, argc, argv));
  229. }
  230. inline MaybeLocal<v8::Value> CallAsConstructor(
  231. v8::Handle<v8::Object> obj
  232. , int argc
  233. , v8::Local<v8::Value> argv[]) {
  234. return MaybeLocal<v8::Value>(obj->CallAsConstructor(argc, argv));
  235. }
  236. inline
  237. MaybeLocal<v8::String> GetSourceLine(v8::Handle<v8::Message> msg) {
  238. return MaybeLocal<v8::String>(msg->GetSourceLine());
  239. }
  240. inline Maybe<int> GetLineNumber(v8::Handle<v8::Message> msg) {
  241. return Just<int>(msg->GetLineNumber());
  242. }
  243. inline Maybe<int> GetStartColumn(v8::Handle<v8::Message> msg) {
  244. return Just<int>(msg->GetStartColumn());
  245. }
  246. inline Maybe<int> GetEndColumn(v8::Handle<v8::Message> msg) {
  247. return Just<int>(msg->GetEndColumn());
  248. }
  249. inline MaybeLocal<v8::Object> CloneElementAt(
  250. v8::Handle<v8::Array> array
  251. , uint32_t index) {
  252. return MaybeLocal<v8::Object>(array->CloneElementAt(index));
  253. }
  254. inline MaybeLocal<v8::Value> Call(
  255. v8::Local<v8::Function> fun
  256. , v8::Local<v8::Object> recv
  257. , int argc
  258. , v8::Local<v8::Value> argv[]) {
  259. return MaybeLocal<v8::Value>(fun->Call(recv, argc, argv));
  260. }
  261. #endif // NAN_MAYBE_PRE_43_INL_H_