nan_maybe_43_inl.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_43_INL_H_
  9. #define NAN_MAYBE_43_INL_H_
  10. template<typename T>
  11. using MaybeLocal = v8::MaybeLocal<T>;
  12. template<typename T>
  13. using Maybe = v8::Maybe<T>;
  14. template<typename T>
  15. inline Maybe<T> Nothing() {
  16. return v8::Nothing<T>();
  17. }
  18. template<typename T>
  19. inline Maybe<T> Just(const T& t) {
  20. return v8::Just<T>(t);
  21. }
  22. v8::Local<v8::Context> GetCurrentContext();
  23. inline
  24. MaybeLocal<v8::String> ToDetailString(v8::Local<v8::Value> val) {
  25. return val->ToDetailString(GetCurrentContext());
  26. }
  27. inline
  28. MaybeLocal<v8::Uint32> ToArrayIndex(v8::Local<v8::Value> val) {
  29. return val->ToArrayIndex(GetCurrentContext());
  30. }
  31. inline
  32. Maybe<bool> Equals(v8::Local<v8::Value> a, v8::Local<v8::Value>(b)) {
  33. return a->Equals(GetCurrentContext(), b);
  34. }
  35. inline
  36. MaybeLocal<v8::Object> NewInstance(v8::Local<v8::Function> h) {
  37. return h->NewInstance(GetCurrentContext());
  38. }
  39. inline
  40. MaybeLocal<v8::Object> NewInstance(
  41. v8::Local<v8::Function> h
  42. , int argc
  43. , v8::Local<v8::Value> argv[]) {
  44. return h->NewInstance(GetCurrentContext(), argc, argv);
  45. }
  46. inline
  47. MaybeLocal<v8::Object> NewInstance(v8::Local<v8::ObjectTemplate> h) {
  48. return h->NewInstance(GetCurrentContext());
  49. }
  50. inline MaybeLocal<v8::Function> GetFunction(
  51. v8::Local<v8::FunctionTemplate> t) {
  52. return t->GetFunction(GetCurrentContext());
  53. }
  54. inline Maybe<bool> Set(
  55. v8::Local<v8::Object> obj
  56. , v8::Local<v8::Value> key
  57. , v8::Local<v8::Value> value) {
  58. return obj->Set(GetCurrentContext(), key, value);
  59. }
  60. inline Maybe<bool> Set(
  61. v8::Local<v8::Object> obj
  62. , uint32_t index
  63. , v8::Local<v8::Value> value) {
  64. return obj->Set(GetCurrentContext(), index, value);
  65. }
  66. inline Maybe<bool> ForceSet(
  67. v8::Local<v8::Object> obj
  68. , v8::Local<v8::Value> key
  69. , v8::Local<v8::Value> value
  70. , v8::PropertyAttribute attribs = v8::None) {
  71. return obj->ForceSet(GetCurrentContext(), key, value, attribs);
  72. }
  73. inline MaybeLocal<v8::Value> Get(
  74. v8::Local<v8::Object> obj
  75. , v8::Local<v8::Value> key) {
  76. return obj->Get(GetCurrentContext(), key);
  77. }
  78. inline
  79. MaybeLocal<v8::Value> Get(v8::Local<v8::Object> obj, uint32_t index) {
  80. return obj->Get(GetCurrentContext(), index);
  81. }
  82. inline v8::PropertyAttribute GetPropertyAttributes(
  83. v8::Local<v8::Object> obj
  84. , v8::Local<v8::Value> key) {
  85. return obj->GetPropertyAttributes(GetCurrentContext(), key).FromJust();
  86. }
  87. inline Maybe<bool> Has(
  88. v8::Local<v8::Object> obj
  89. , v8::Local<v8::String> key) {
  90. return obj->Has(GetCurrentContext(), key);
  91. }
  92. inline Maybe<bool> Has(v8::Local<v8::Object> obj, uint32_t index) {
  93. return obj->Has(GetCurrentContext(), index);
  94. }
  95. inline Maybe<bool> Delete(
  96. v8::Local<v8::Object> obj
  97. , v8::Local<v8::String> key) {
  98. return obj->Delete(GetCurrentContext(), key);
  99. }
  100. inline
  101. Maybe<bool> Delete(v8::Local<v8::Object> obj, uint32_t index) {
  102. return obj->Delete(GetCurrentContext(), index);
  103. }
  104. inline
  105. MaybeLocal<v8::Array> GetPropertyNames(v8::Local<v8::Object> obj) {
  106. return obj->GetPropertyNames(GetCurrentContext());
  107. }
  108. inline
  109. MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Local<v8::Object> obj) {
  110. return obj->GetOwnPropertyNames(GetCurrentContext());
  111. }
  112. inline Maybe<bool> SetPrototype(
  113. v8::Local<v8::Object> obj
  114. , v8::Local<v8::Value> prototype) {
  115. return obj->SetPrototype(GetCurrentContext(), prototype);
  116. }
  117. inline MaybeLocal<v8::String> ObjectProtoToString(
  118. v8::Local<v8::Object> obj) {
  119. return obj->ObjectProtoToString(GetCurrentContext());
  120. }
  121. inline Maybe<bool> HasOwnProperty(
  122. v8::Local<v8::Object> obj
  123. , v8::Local<v8::String> key) {
  124. return obj->HasOwnProperty(GetCurrentContext(), key);
  125. }
  126. inline Maybe<bool> HasRealNamedProperty(
  127. v8::Local<v8::Object> obj
  128. , v8::Local<v8::String> key) {
  129. return obj->HasRealNamedProperty(GetCurrentContext(), key);
  130. }
  131. inline Maybe<bool> HasRealIndexedProperty(
  132. v8::Local<v8::Object> obj
  133. , uint32_t index) {
  134. return obj->HasRealIndexedProperty(GetCurrentContext(), index);
  135. }
  136. inline Maybe<bool> HasRealNamedCallbackProperty(
  137. v8::Local<v8::Object> obj
  138. , v8::Local<v8::String> key) {
  139. return obj->HasRealNamedCallbackProperty(GetCurrentContext(), key);
  140. }
  141. inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
  142. v8::Local<v8::Object> obj
  143. , v8::Local<v8::String> key) {
  144. return obj->GetRealNamedPropertyInPrototypeChain(GetCurrentContext(), key);
  145. }
  146. inline MaybeLocal<v8::Value> GetRealNamedProperty(
  147. v8::Local<v8::Object> obj
  148. , v8::Local<v8::String> key) {
  149. return obj->GetRealNamedProperty(GetCurrentContext(), key);
  150. }
  151. inline MaybeLocal<v8::Value> CallAsFunction(
  152. v8::Local<v8::Object> obj
  153. , v8::Local<v8::Object> recv
  154. , int argc
  155. , v8::Local<v8::Value> argv[]) {
  156. return obj->CallAsFunction(GetCurrentContext(), recv, argc, argv);
  157. }
  158. inline MaybeLocal<v8::Value> CallAsConstructor(
  159. v8::Local<v8::Object> obj
  160. , int argc, v8::Local<v8::Value> argv[]) {
  161. return obj->CallAsConstructor(GetCurrentContext(), argc, argv);
  162. }
  163. inline
  164. MaybeLocal<v8::String> GetSourceLine(v8::Local<v8::Message> msg) {
  165. return msg->GetSourceLine(GetCurrentContext());
  166. }
  167. inline Maybe<int> GetLineNumber(v8::Local<v8::Message> msg) {
  168. return msg->GetLineNumber(GetCurrentContext());
  169. }
  170. inline Maybe<int> GetStartColumn(v8::Local<v8::Message> msg) {
  171. return msg->GetStartColumn(GetCurrentContext());
  172. }
  173. inline Maybe<int> GetEndColumn(v8::Local<v8::Message> msg) {
  174. return msg->GetEndColumn(GetCurrentContext());
  175. }
  176. inline MaybeLocal<v8::Object> CloneElementAt(
  177. v8::Local<v8::Array> array
  178. , uint32_t index) {
  179. #if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  180. v8::EscapableHandleScope handle_scope(v8::Isolate::GetCurrent());
  181. v8::Local<v8::Context> context = GetCurrentContext();
  182. v8::Local<v8::Value> elem;
  183. if (!array->Get(context, index).ToLocal(&elem)) {
  184. return MaybeLocal<v8::Object>();
  185. }
  186. v8::Local<v8::Object> obj;
  187. if (!elem->ToObject(context).ToLocal(&obj)) {
  188. return MaybeLocal<v8::Object>();
  189. }
  190. return MaybeLocal<v8::Object>(handle_scope.Escape(obj->Clone()));
  191. #else
  192. return array->CloneElementAt(GetCurrentContext(), index);
  193. #endif
  194. }
  195. inline MaybeLocal<v8::Value> Call(
  196. v8::Local<v8::Function> fun
  197. , v8::Local<v8::Object> recv
  198. , int argc
  199. , v8::Local<v8::Value> argv[]) {
  200. return fun->Call(GetCurrentContext(), recv, argc, argv);
  201. }
  202. #endif // NAN_MAYBE_43_INL_H_