nan_maybe_43_inl.h 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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_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. #if NODE_MODULE_VERSION < NODE_4_0_MODULE_VERSION
  90. #include "nan_define_own_property_helper.h" // NOLINT(build/include)
  91. #endif
  92. inline Maybe<bool> DefineOwnProperty(
  93. v8::Local<v8::Object> obj
  94. , v8::Local<v8::String> key
  95. , v8::Local<v8::Value> value
  96. , v8::PropertyAttribute attribs = v8::None) {
  97. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  98. v8::HandleScope scope(isolate);
  99. #if NODE_MODULE_VERSION >= NODE_4_0_MODULE_VERSION
  100. return obj->DefineOwnProperty(isolate->GetCurrentContext(), key, value,
  101. attribs);
  102. #else
  103. Maybe<v8::PropertyAttribute> maybeCurrent =
  104. obj->GetPropertyAttributes(isolate->GetCurrentContext(), key);
  105. if (maybeCurrent.IsNothing()) {
  106. return Nothing<bool>();
  107. }
  108. v8::PropertyAttribute current = maybeCurrent.FromJust();
  109. return imp::DefineOwnPropertyHelper(current, obj, key, value, attribs);
  110. #endif
  111. }
  112. NAN_DEPRECATED inline Maybe<bool> ForceSet(
  113. v8::Local<v8::Object> obj
  114. , v8::Local<v8::Value> key
  115. , v8::Local<v8::Value> value
  116. , v8::PropertyAttribute attribs = v8::None) {
  117. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  118. v8::HandleScope scope(isolate);
  119. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  120. return key->IsName()
  121. ? obj->DefineOwnProperty(isolate->GetCurrentContext(),
  122. key.As<v8::Name>(), value, attribs)
  123. : Nothing<bool>();
  124. #else
  125. return obj->ForceSet(isolate->GetCurrentContext(), key, value, attribs);
  126. #endif
  127. }
  128. inline MaybeLocal<v8::Value> Get(
  129. v8::Local<v8::Object> obj
  130. , v8::Local<v8::Value> key) {
  131. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  132. v8::EscapableHandleScope scope(isolate);
  133. return scope.Escape(obj->Get(isolate->GetCurrentContext(), key)
  134. .FromMaybe(v8::Local<v8::Value>()));
  135. }
  136. inline
  137. MaybeLocal<v8::Value> Get(v8::Local<v8::Object> obj, uint32_t index) {
  138. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  139. v8::EscapableHandleScope scope(isolate);
  140. return scope.Escape(obj->Get(isolate->GetCurrentContext(), index)
  141. .FromMaybe(v8::Local<v8::Value>()));
  142. }
  143. inline v8::PropertyAttribute GetPropertyAttributes(
  144. v8::Local<v8::Object> obj
  145. , v8::Local<v8::Value> key) {
  146. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  147. v8::HandleScope scope(isolate);
  148. return obj->GetPropertyAttributes(isolate->GetCurrentContext(), key)
  149. .FromJust();
  150. }
  151. inline Maybe<bool> Has(
  152. v8::Local<v8::Object> obj
  153. , v8::Local<v8::String> key) {
  154. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  155. v8::HandleScope scope(isolate);
  156. return obj->Has(isolate->GetCurrentContext(), key);
  157. }
  158. inline Maybe<bool> Has(v8::Local<v8::Object> obj, uint32_t index) {
  159. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  160. v8::HandleScope scope(isolate);
  161. return obj->Has(isolate->GetCurrentContext(), index);
  162. }
  163. inline Maybe<bool> Delete(
  164. v8::Local<v8::Object> obj
  165. , v8::Local<v8::String> key) {
  166. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  167. v8::HandleScope scope(isolate);
  168. return obj->Delete(isolate->GetCurrentContext(), key);
  169. }
  170. inline
  171. Maybe<bool> Delete(v8::Local<v8::Object> obj, uint32_t index) {
  172. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  173. v8::HandleScope scope(isolate);
  174. return obj->Delete(isolate->GetCurrentContext(), index);
  175. }
  176. inline
  177. MaybeLocal<v8::Array> GetPropertyNames(v8::Local<v8::Object> obj) {
  178. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  179. v8::EscapableHandleScope scope(isolate);
  180. return scope.Escape(obj->GetPropertyNames(isolate->GetCurrentContext())
  181. .FromMaybe(v8::Local<v8::Array>()));
  182. }
  183. inline
  184. MaybeLocal<v8::Array> GetOwnPropertyNames(v8::Local<v8::Object> obj) {
  185. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  186. v8::EscapableHandleScope scope(isolate);
  187. return scope.Escape(obj->GetOwnPropertyNames(isolate->GetCurrentContext())
  188. .FromMaybe(v8::Local<v8::Array>()));
  189. }
  190. inline Maybe<bool> SetPrototype(
  191. v8::Local<v8::Object> obj
  192. , v8::Local<v8::Value> prototype) {
  193. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  194. v8::HandleScope scope(isolate);
  195. return obj->SetPrototype(isolate->GetCurrentContext(), prototype);
  196. }
  197. inline MaybeLocal<v8::String> ObjectProtoToString(
  198. v8::Local<v8::Object> obj) {
  199. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  200. v8::EscapableHandleScope scope(isolate);
  201. return scope.Escape(obj->ObjectProtoToString(isolate->GetCurrentContext())
  202. .FromMaybe(v8::Local<v8::String>()));
  203. }
  204. inline Maybe<bool> HasOwnProperty(
  205. v8::Local<v8::Object> obj
  206. , v8::Local<v8::String> key) {
  207. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  208. v8::HandleScope scope(isolate);
  209. return obj->HasOwnProperty(isolate->GetCurrentContext(), key);
  210. }
  211. inline Maybe<bool> HasRealNamedProperty(
  212. v8::Local<v8::Object> obj
  213. , v8::Local<v8::String> key) {
  214. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  215. v8::HandleScope scope(isolate);
  216. return obj->HasRealNamedProperty(isolate->GetCurrentContext(), key);
  217. }
  218. inline Maybe<bool> HasRealIndexedProperty(
  219. v8::Local<v8::Object> obj
  220. , uint32_t index) {
  221. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  222. v8::HandleScope scope(isolate);
  223. return obj->HasRealIndexedProperty(isolate->GetCurrentContext(), index);
  224. }
  225. inline Maybe<bool> HasRealNamedCallbackProperty(
  226. v8::Local<v8::Object> obj
  227. , v8::Local<v8::String> key) {
  228. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  229. v8::HandleScope scope(isolate);
  230. return obj->HasRealNamedCallbackProperty(isolate->GetCurrentContext(), key);
  231. }
  232. inline MaybeLocal<v8::Value> GetRealNamedPropertyInPrototypeChain(
  233. v8::Local<v8::Object> obj
  234. , v8::Local<v8::String> key) {
  235. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  236. v8::EscapableHandleScope scope(isolate);
  237. return scope.Escape(obj->GetRealNamedPropertyInPrototypeChain(
  238. isolate->GetCurrentContext(), key)
  239. .FromMaybe(v8::Local<v8::Value>()));
  240. }
  241. inline MaybeLocal<v8::Value> GetRealNamedProperty(
  242. v8::Local<v8::Object> obj
  243. , v8::Local<v8::String> key) {
  244. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  245. v8::EscapableHandleScope scope(isolate);
  246. return scope.Escape(
  247. obj->GetRealNamedProperty(isolate->GetCurrentContext(), key)
  248. .FromMaybe(v8::Local<v8::Value>()));
  249. }
  250. inline MaybeLocal<v8::Value> CallAsFunction(
  251. v8::Local<v8::Object> obj
  252. , v8::Local<v8::Object> recv
  253. , int argc
  254. , v8::Local<v8::Value> argv[]) {
  255. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  256. v8::EscapableHandleScope scope(isolate);
  257. return scope.Escape(
  258. obj->CallAsFunction(isolate->GetCurrentContext(), recv, argc, argv)
  259. .FromMaybe(v8::Local<v8::Value>()));
  260. }
  261. inline MaybeLocal<v8::Value> CallAsConstructor(
  262. v8::Local<v8::Object> obj
  263. , int argc, v8::Local<v8::Value> argv[]) {
  264. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  265. v8::EscapableHandleScope scope(isolate);
  266. return scope.Escape(
  267. obj->CallAsConstructor(isolate->GetCurrentContext(), argc, argv)
  268. .FromMaybe(v8::Local<v8::Value>()));
  269. }
  270. inline
  271. MaybeLocal<v8::String> GetSourceLine(v8::Local<v8::Message> msg) {
  272. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  273. v8::EscapableHandleScope scope(isolate);
  274. return scope.Escape(msg->GetSourceLine(isolate->GetCurrentContext())
  275. .FromMaybe(v8::Local<v8::String>()));
  276. }
  277. inline Maybe<int> GetLineNumber(v8::Local<v8::Message> msg) {
  278. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  279. v8::HandleScope scope(isolate);
  280. return msg->GetLineNumber(isolate->GetCurrentContext());
  281. }
  282. inline Maybe<int> GetStartColumn(v8::Local<v8::Message> msg) {
  283. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  284. v8::HandleScope scope(isolate);
  285. return msg->GetStartColumn(isolate->GetCurrentContext());
  286. }
  287. inline Maybe<int> GetEndColumn(v8::Local<v8::Message> msg) {
  288. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  289. v8::HandleScope scope(isolate);
  290. return msg->GetEndColumn(isolate->GetCurrentContext());
  291. }
  292. inline MaybeLocal<v8::Object> CloneElementAt(
  293. v8::Local<v8::Array> array
  294. , uint32_t index) {
  295. #if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  296. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  297. v8::EscapableHandleScope scope(isolate);
  298. v8::Local<v8::Context> context = isolate->GetCurrentContext();
  299. v8::Local<v8::Value> elem;
  300. v8::Local<v8::Object> obj;
  301. if (!array->Get(context, index).ToLocal(&elem)) {
  302. return scope.Escape(obj);
  303. }
  304. if (!elem->ToObject(context).ToLocal(&obj)) {
  305. return scope.Escape(v8::Local<v8::Object>());
  306. }
  307. return scope.Escape(obj->Clone());
  308. #else
  309. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  310. v8::EscapableHandleScope scope(isolate);
  311. return scope.Escape(array->CloneElementAt(isolate->GetCurrentContext(), index)
  312. .FromMaybe(v8::Local<v8::Object>()));
  313. #endif
  314. }
  315. inline MaybeLocal<v8::Value> Call(
  316. v8::Local<v8::Function> fun
  317. , v8::Local<v8::Object> recv
  318. , int argc
  319. , v8::Local<v8::Value> argv[]) {
  320. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  321. v8::EscapableHandleScope scope(isolate);
  322. return scope.Escape(fun->Call(isolate->GetCurrentContext(), recv, argc, argv)
  323. .FromMaybe(v8::Local<v8::Value>()));
  324. }
  325. #endif // NAN_MAYBE_43_INL_H_