nan_maybe_43_inl.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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_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. inline
  23. MaybeLocal<v8::String> ToDetailString(v8::Local<v8::Value> val) {
  24. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  25. v8::EscapableHandleScope scope(isolate);
  26. return scope.Escape(val->ToDetailString(isolate->GetCurrentContext())
  27. .FromMaybe(v8::Local<v8::String>()));
  28. }
  29. inline
  30. MaybeLocal<v8::Uint32> ToArrayIndex(v8::Local<v8::Value> val) {
  31. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  32. v8::EscapableHandleScope scope(isolate);
  33. return scope.Escape(val->ToArrayIndex(isolate->GetCurrentContext())
  34. .FromMaybe(v8::Local<v8::Uint32>()));
  35. }
  36. inline
  37. Maybe<bool> Equals(v8::Local<v8::Value> a, v8::Local<v8::Value>(b)) {
  38. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  39. v8::HandleScope scope(isolate);
  40. return a->Equals(isolate->GetCurrentContext(), b);
  41. }
  42. inline
  43. MaybeLocal<v8::Object> NewInstance(v8::Local<v8::Function> h) {
  44. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  45. v8::EscapableHandleScope scope(isolate);
  46. return scope.Escape(h->NewInstance(isolate->GetCurrentContext())
  47. .FromMaybe(v8::Local<v8::Object>()));
  48. }
  49. inline
  50. MaybeLocal<v8::Object> NewInstance(
  51. v8::Local<v8::Function> h
  52. , int argc
  53. , v8::Local<v8::Value> argv[]) {
  54. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  55. v8::EscapableHandleScope scope(isolate);
  56. return scope.Escape(h->NewInstance(isolate->GetCurrentContext(), argc, argv)
  57. .FromMaybe(v8::Local<v8::Object>()));
  58. }
  59. inline
  60. MaybeLocal<v8::Object> NewInstance(v8::Local<v8::ObjectTemplate> h) {
  61. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  62. v8::EscapableHandleScope scope(isolate);
  63. return scope.Escape(h->NewInstance(isolate->GetCurrentContext())
  64. .FromMaybe(v8::Local<v8::Object>()));
  65. }
  66. inline MaybeLocal<v8::Function> GetFunction(
  67. v8::Local<v8::FunctionTemplate> t) {
  68. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  69. v8::EscapableHandleScope scope(isolate);
  70. return scope.Escape(t->GetFunction(isolate->GetCurrentContext())
  71. .FromMaybe(v8::Local<v8::Function>()));
  72. }
  73. inline Maybe<bool> Set(
  74. v8::Local<v8::Object> obj
  75. , v8::Local<v8::Value> key
  76. , v8::Local<v8::Value> value) {
  77. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  78. v8::HandleScope scope(isolate);
  79. return obj->Set(isolate->GetCurrentContext(), key, value);
  80. }
  81. inline Maybe<bool> Set(
  82. v8::Local<v8::Object> obj
  83. , uint32_t index
  84. , v8::Local<v8::Value> value) {
  85. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  86. v8::HandleScope scope(isolate);
  87. return obj->Set(isolate->GetCurrentContext(), index, value);
  88. }
  89. inline Maybe<bool> ForceSet(
  90. v8::Local<v8::Object> obj
  91. , v8::Local<v8::Value> key
  92. , v8::Local<v8::Value> value
  93. , v8::PropertyAttribute attribs = v8::None) {
  94. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  95. v8::HandleScope scope(isolate);
  96. return obj->ForceSet(isolate->GetCurrentContext(), key, value, attribs);
  97. }
  98. inline MaybeLocal<v8::Value> Get(
  99. v8::Local<v8::Object> obj
  100. , v8::Local<v8::Value> key) {
  101. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  102. v8::EscapableHandleScope scope(isolate);
  103. return scope.Escape(obj->Get(isolate->GetCurrentContext(), key)
  104. .FromMaybe(v8::Local<v8::Value>()));
  105. }
  106. inline
  107. MaybeLocal<v8::Value> Get(v8::Local<v8::Object> obj, uint32_t index) {
  108. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  109. v8::EscapableHandleScope scope(isolate);
  110. return scope.Escape(obj->Get(isolate->GetCurrentContext(), index)
  111. .FromMaybe(v8::Local<v8::Value>()));
  112. }
  113. inline v8::PropertyAttribute GetPropertyAttributes(
  114. v8::Local<v8::Object> obj
  115. , v8::Local<v8::Value> key) {
  116. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  117. v8::HandleScope scope(isolate);
  118. return obj->GetPropertyAttributes(isolate->GetCurrentContext(), key)
  119. .FromJust();
  120. }
  121. inline Maybe<bool> Has(
  122. v8::Local<v8::Object> obj
  123. , v8::Local<v8::String> key) {
  124. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  125. v8::HandleScope scope(isolate);
  126. return obj->Has(isolate->GetCurrentContext(), key);
  127. }
  128. inline Maybe<bool> Has(v8::Local<v8::Object> obj, uint32_t index) {
  129. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  130. v8::HandleScope scope(isolate);
  131. return obj->Has(isolate->GetCurrentContext(), index);
  132. }
  133. inline Maybe<bool> Delete(
  134. v8::Local<v8::Object> obj
  135. , v8::Local<v8::String> key) {
  136. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  137. v8::HandleScope scope(isolate);
  138. return obj->Delete(isolate->GetCurrentContext(), key);
  139. }
  140. inline
  141. Maybe<bool> Delete(v8::Local<v8::Object> obj, uint32_t index) {
  142. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  143. v8::HandleScope scope(isolate);
  144. return obj->Delete(isolate->GetCurrentContext(), index);
  145. }
  146. inline
  147. MaybeLocal<v8::Array> GetPropertyNames(v8::Local<v8::Object> obj) {
  148. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  149. v8::EscapableHandleScope scope(isolate);
  150. return scope.Escape(obj->GetPropertyNames(isolate->GetCurrentContext())
  151. .FromMaybe(v8::Local<v8::Array>()));
  152. }
  153. inline
  154. MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Local<v8::Object> obj) {
  155. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  156. v8::EscapableHandleScope scope(isolate);
  157. return scope.Escape(obj->GetOwnPropertyNames(isolate->GetCurrentContext())
  158. .FromMaybe(v8::Local<v8::Array>()));
  159. }
  160. inline Maybe<bool> SetPrototype(
  161. v8::Local<v8::Object> obj
  162. , v8::Local<v8::Value> prototype) {
  163. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  164. v8::HandleScope scope(isolate);
  165. return obj->SetPrototype(isolate->GetCurrentContext(), prototype);
  166. }
  167. inline MaybeLocal<v8::String> ObjectProtoToString(
  168. v8::Local<v8::Object> obj) {
  169. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  170. v8::EscapableHandleScope scope(isolate);
  171. return scope.Escape(obj->ObjectProtoToString(isolate->GetCurrentContext())
  172. .FromMaybe(v8::Local<v8::String>()));
  173. }
  174. inline Maybe<bool> HasOwnProperty(
  175. v8::Local<v8::Object> obj
  176. , v8::Local<v8::String> key) {
  177. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  178. v8::HandleScope scope(isolate);
  179. return obj->HasOwnProperty(isolate->GetCurrentContext(), key);
  180. }
  181. inline Maybe<bool> HasRealNamedProperty(
  182. v8::Local<v8::Object> obj
  183. , v8::Local<v8::String> key) {
  184. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  185. v8::HandleScope scope(isolate);
  186. return obj->HasRealNamedProperty(isolate->GetCurrentContext(), key);
  187. }
  188. inline Maybe<bool> HasRealIndexedProperty(
  189. v8::Local<v8::Object> obj
  190. , uint32_t index) {
  191. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  192. v8::HandleScope scope(isolate);
  193. return obj->HasRealIndexedProperty(isolate->GetCurrentContext(), index);
  194. }
  195. inline Maybe<bool> HasRealNamedCallbackProperty(
  196. v8::Local<v8::Object> obj
  197. , v8::Local<v8::String> key) {
  198. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  199. v8::HandleScope scope(isolate);
  200. return obj->HasRealNamedCallbackProperty(isolate->GetCurrentContext(), key);
  201. }
  202. inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
  203. v8::Local<v8::Object> obj
  204. , v8::Local<v8::String> key) {
  205. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  206. v8::EscapableHandleScope scope(isolate);
  207. return scope.Escape(obj->GetRealNamedPropertyInPrototypeChain(
  208. isolate->GetCurrentContext(), key)
  209. .FromMaybe(v8::Local<v8::Value>()));
  210. }
  211. inline MaybeLocal<v8::Value> GetRealNamedProperty(
  212. v8::Local<v8::Object> obj
  213. , v8::Local<v8::String> key) {
  214. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  215. v8::EscapableHandleScope scope(isolate);
  216. return scope.Escape(
  217. obj->GetRealNamedProperty(isolate->GetCurrentContext(), key)
  218. .FromMaybe(v8::Local<v8::Value>()));
  219. }
  220. inline MaybeLocal<v8::Value> CallAsFunction(
  221. v8::Local<v8::Object> obj
  222. , v8::Local<v8::Object> recv
  223. , int argc
  224. , v8::Local<v8::Value> argv[]) {
  225. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  226. v8::EscapableHandleScope scope(isolate);
  227. return scope.Escape(
  228. obj->CallAsFunction(isolate->GetCurrentContext(), recv, argc, argv)
  229. .FromMaybe(v8::Local<v8::Value>()));
  230. }
  231. inline MaybeLocal<v8::Value> CallAsConstructor(
  232. v8::Local<v8::Object> obj
  233. , int argc, v8::Local<v8::Value> argv[]) {
  234. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  235. v8::EscapableHandleScope scope(isolate);
  236. return scope.Escape(
  237. obj->CallAsConstructor(isolate->GetCurrentContext(), argc, argv)
  238. .FromMaybe(v8::Local<v8::Value>()));
  239. }
  240. inline
  241. MaybeLocal<v8::String> GetSourceLine(v8::Local<v8::Message> msg) {
  242. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  243. v8::EscapableHandleScope scope(isolate);
  244. return scope.Escape(msg->GetSourceLine(isolate->GetCurrentContext())
  245. .FromMaybe(v8::Local<v8::String>()));
  246. }
  247. inline Maybe<int> GetLineNumber(v8::Local<v8::Message> msg) {
  248. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  249. v8::HandleScope scope(isolate);
  250. return msg->GetLineNumber(isolate->GetCurrentContext());
  251. }
  252. inline Maybe<int> GetStartColumn(v8::Local<v8::Message> msg) {
  253. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  254. v8::HandleScope scope(isolate);
  255. return msg->GetStartColumn(isolate->GetCurrentContext());
  256. }
  257. inline Maybe<int> GetEndColumn(v8::Local<v8::Message> msg) {
  258. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  259. v8::HandleScope scope(isolate);
  260. return msg->GetEndColumn(isolate->GetCurrentContext());
  261. }
  262. inline MaybeLocal<v8::Object> CloneElementAt(
  263. v8::Local<v8::Array> array
  264. , uint32_t index) {
  265. #if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  266. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  267. v8::EscapableHandleScope scope(isolate);
  268. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  269. v8::Local<v8::Value> elem;
  270. v8::Local<v8::Object> obj;
  271. if (!array->Get(context, index).ToLocal(&elem)) {
  272. return scope.Escape(obj);
  273. }
  274. if (!elem->ToObject(context).ToLocal(&obj)) {
  275. return scope.Escape(v8::Local<v8::Object>());
  276. }
  277. return scope.Escape(obj->Clone());
  278. #else
  279. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  280. v8::EscapableHandleScope scope(isolate);
  281. return scope.Escape(array->CloneElementAt(isolate->GetCurrentContext(), index)
  282. .FromMaybe(v8::Local<v8::Object>()));
  283. #endif
  284. }
  285. inline MaybeLocal<v8::Value> Call(
  286. v8::Local<v8::Function> fun
  287. , v8::Local<v8::Object> recv
  288. , int argc
  289. , v8::Local<v8::Value> argv[]) {
  290. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  291. v8::EscapableHandleScope scope(isolate);
  292. return scope.Escape(fun->Call(isolate->GetCurrentContext(), recv, argc, argv)
  293. .FromMaybe(v8::Local<v8::Value>()));
  294. }
  295. #endif // NAN_MAYBE_43_INL_H_