You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nan_maybe_43_inl.h 12KB

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