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_callbacks_12_inl.h 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514
  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_CALLBACKS_12_INL_H_
  9. #define NAN_CALLBACKS_12_INL_H_
  10. template<typename T>
  11. class ReturnValue {
  12. v8::ReturnValue<T> value_;
  13. public:
  14. template <class S>
  15. explicit inline ReturnValue(const v8::ReturnValue<S> &value) :
  16. value_(value) {}
  17. template <class S>
  18. explicit inline ReturnValue(const ReturnValue<S>& that)
  19. : value_(that.value_) {
  20. TYPE_CHECK(T, S);
  21. }
  22. // Handle setters
  23. template <typename S> inline void Set(const v8::Local<S> &handle) {
  24. TYPE_CHECK(T, S);
  25. value_.Set(handle);
  26. }
  27. template <typename S> inline void Set(const Global<S> &handle) {
  28. TYPE_CHECK(T, S);
  29. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  30. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && \
  31. (V8_MINOR_VERSION > 5 || (V8_MINOR_VERSION == 5 && \
  32. defined(V8_BUILD_NUMBER) && V8_BUILD_NUMBER >= 8))))
  33. value_.Set(handle);
  34. #else
  35. value_.Set(*reinterpret_cast<const v8::Persistent<S>*>(&handle));
  36. const_cast<Global<S> &>(handle).Reset();
  37. #endif
  38. }
  39. // Fast primitive setters
  40. inline void Set(bool value) {
  41. TYPE_CHECK(T, v8::Boolean);
  42. value_.Set(value);
  43. }
  44. inline void Set(double i) {
  45. TYPE_CHECK(T, v8::Number);
  46. value_.Set(i);
  47. }
  48. inline void Set(int32_t i) {
  49. TYPE_CHECK(T, v8::Integer);
  50. value_.Set(i);
  51. }
  52. inline void Set(uint32_t i) {
  53. TYPE_CHECK(T, v8::Integer);
  54. value_.Set(i);
  55. }
  56. // Fast JS primitive setters
  57. inline void SetNull() {
  58. TYPE_CHECK(T, v8::Primitive);
  59. value_.SetNull();
  60. }
  61. inline void SetUndefined() {
  62. TYPE_CHECK(T, v8::Primitive);
  63. value_.SetUndefined();
  64. }
  65. inline void SetEmptyString() {
  66. TYPE_CHECK(T, v8::String);
  67. value_.SetEmptyString();
  68. }
  69. // Convenience getter for isolate
  70. inline v8::Isolate *GetIsolate() const {
  71. return value_.GetIsolate();
  72. }
  73. // Pointer setter: Uncompilable to prevent inadvertent misuse.
  74. template<typename S>
  75. inline void Set(S *whatever) { TYPE_CHECK(S*, v8::Primitive); }
  76. };
  77. template<typename T>
  78. class FunctionCallbackInfo {
  79. const v8::FunctionCallbackInfo<T> &info_;
  80. const v8::Local<v8::Value> data_;
  81. public:
  82. explicit inline FunctionCallbackInfo(
  83. const v8::FunctionCallbackInfo<T> &info
  84. , v8::Local<v8::Value> data) :
  85. info_(info)
  86. , data_(data) {}
  87. inline ReturnValue<T> GetReturnValue() const {
  88. return ReturnValue<T>(info_.GetReturnValue());
  89. }
  90. #if NODE_MAJOR_VERSION < 10
  91. inline v8::Local<v8::Function> Callee() const { return info_.Callee(); }
  92. #endif
  93. inline v8::Local<v8::Value> Data() const { return data_; }
  94. inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
  95. inline bool IsConstructCall() const { return info_.IsConstructCall(); }
  96. inline int Length() const { return info_.Length(); }
  97. inline v8::Local<v8::Value> operator[](int i) const { return info_[i]; }
  98. inline v8::Local<v8::Object> This() const { return info_.This(); }
  99. inline v8::Isolate *GetIsolate() const { return info_.GetIsolate(); }
  100. protected:
  101. static const int kHolderIndex = 0;
  102. static const int kIsolateIndex = 1;
  103. static const int kReturnValueDefaultValueIndex = 2;
  104. static const int kReturnValueIndex = 3;
  105. static const int kDataIndex = 4;
  106. static const int kCalleeIndex = 5;
  107. static const int kContextSaveIndex = 6;
  108. static const int kArgsLength = 7;
  109. private:
  110. NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
  111. };
  112. template<typename T>
  113. class PropertyCallbackInfo {
  114. const v8::PropertyCallbackInfo<T> &info_;
  115. const v8::Local<v8::Value> data_;
  116. public:
  117. explicit inline PropertyCallbackInfo(
  118. const v8::PropertyCallbackInfo<T> &info
  119. , const v8::Local<v8::Value> data) :
  120. info_(info)
  121. , data_(data) {}
  122. inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); }
  123. inline v8::Local<v8::Value> Data() const { return data_; }
  124. inline v8::Local<v8::Object> This() const { return info_.This(); }
  125. inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
  126. inline ReturnValue<T> GetReturnValue() const {
  127. return ReturnValue<T>(info_.GetReturnValue());
  128. }
  129. protected:
  130. static const int kHolderIndex = 0;
  131. static const int kIsolateIndex = 1;
  132. static const int kReturnValueDefaultValueIndex = 2;
  133. static const int kReturnValueIndex = 3;
  134. static const int kDataIndex = 4;
  135. static const int kThisIndex = 5;
  136. static const int kArgsLength = 6;
  137. private:
  138. NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
  139. };
  140. namespace imp {
  141. static
  142. void FunctionCallbackWrapper(const v8::FunctionCallbackInfo<v8::Value> &info) {
  143. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  144. FunctionCallback callback = reinterpret_cast<FunctionCallback>(
  145. reinterpret_cast<intptr_t>(
  146. obj->GetInternalField(kFunctionIndex).As<v8::External>()->Value()));
  147. FunctionCallbackInfo<v8::Value>
  148. cbinfo(info, obj->GetInternalField(kDataIndex));
  149. callback(cbinfo);
  150. }
  151. typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
  152. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  153. static
  154. void GetterCallbackWrapper(
  155. v8::Local<v8::Name> property
  156. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  157. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  158. PropertyCallbackInfo<v8::Value>
  159. cbinfo(info, obj->GetInternalField(kDataIndex));
  160. GetterCallback callback = reinterpret_cast<GetterCallback>(
  161. reinterpret_cast<intptr_t>(
  162. obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
  163. callback(property.As<v8::String>(), cbinfo);
  164. }
  165. typedef void (*NativeGetter)
  166. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
  167. static
  168. void SetterCallbackWrapper(
  169. v8::Local<v8::Name> property
  170. , v8::Local<v8::Value> value
  171. , const v8::PropertyCallbackInfo<void> &info) {
  172. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  173. PropertyCallbackInfo<void>
  174. cbinfo(info, obj->GetInternalField(kDataIndex));
  175. SetterCallback callback = reinterpret_cast<SetterCallback>(
  176. reinterpret_cast<intptr_t>(
  177. obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
  178. callback(property.As<v8::String>(), value, cbinfo);
  179. }
  180. typedef void (*NativeSetter)(
  181. v8::Local<v8::Name>
  182. , v8::Local<v8::Value>
  183. , const v8::PropertyCallbackInfo<void> &);
  184. #else
  185. static
  186. void GetterCallbackWrapper(
  187. v8::Local<v8::String> property
  188. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  189. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  190. PropertyCallbackInfo<v8::Value>
  191. cbinfo(info, obj->GetInternalField(kDataIndex));
  192. GetterCallback callback = reinterpret_cast<GetterCallback>(
  193. reinterpret_cast<intptr_t>(
  194. obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
  195. callback(property, cbinfo);
  196. }
  197. typedef void (*NativeGetter)
  198. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
  199. static
  200. void SetterCallbackWrapper(
  201. v8::Local<v8::String> property
  202. , v8::Local<v8::Value> value
  203. , const v8::PropertyCallbackInfo<void> &info) {
  204. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  205. PropertyCallbackInfo<void>
  206. cbinfo(info, obj->GetInternalField(kDataIndex));
  207. SetterCallback callback = reinterpret_cast<SetterCallback>(
  208. reinterpret_cast<intptr_t>(
  209. obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
  210. callback(property, value, cbinfo);
  211. }
  212. typedef void (*NativeSetter)(
  213. v8::Local<v8::String>
  214. , v8::Local<v8::Value>
  215. , const v8::PropertyCallbackInfo<void> &);
  216. #endif
  217. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  218. static
  219. void PropertyGetterCallbackWrapper(
  220. v8::Local<v8::Name> property
  221. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  222. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  223. PropertyCallbackInfo<v8::Value>
  224. cbinfo(info, obj->GetInternalField(kDataIndex));
  225. PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
  226. reinterpret_cast<intptr_t>(
  227. obj->GetInternalField(kPropertyGetterIndex)
  228. .As<v8::External>()->Value()));
  229. callback(property.As<v8::String>(), cbinfo);
  230. }
  231. typedef void (*NativePropertyGetter)
  232. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
  233. static
  234. void PropertySetterCallbackWrapper(
  235. v8::Local<v8::Name> property
  236. , v8::Local<v8::Value> value
  237. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  238. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  239. PropertyCallbackInfo<v8::Value>
  240. cbinfo(info, obj->GetInternalField(kDataIndex));
  241. PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
  242. reinterpret_cast<intptr_t>(
  243. obj->GetInternalField(kPropertySetterIndex)
  244. .As<v8::External>()->Value()));
  245. callback(property.As<v8::String>(), value, cbinfo);
  246. }
  247. typedef void (*NativePropertySetter)(
  248. v8::Local<v8::Name>
  249. , v8::Local<v8::Value>
  250. , const v8::PropertyCallbackInfo<v8::Value> &);
  251. static
  252. void PropertyEnumeratorCallbackWrapper(
  253. const v8::PropertyCallbackInfo<v8::Array> &info) {
  254. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  255. PropertyCallbackInfo<v8::Array>
  256. cbinfo(info, obj->GetInternalField(kDataIndex));
  257. PropertyEnumeratorCallback callback =
  258. reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
  259. obj->GetInternalField(kPropertyEnumeratorIndex)
  260. .As<v8::External>()->Value()));
  261. callback(cbinfo);
  262. }
  263. typedef void (*NativePropertyEnumerator)
  264. (const v8::PropertyCallbackInfo<v8::Array> &);
  265. static
  266. void PropertyDeleterCallbackWrapper(
  267. v8::Local<v8::Name> property
  268. , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  269. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  270. PropertyCallbackInfo<v8::Boolean>
  271. cbinfo(info, obj->GetInternalField(kDataIndex));
  272. PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
  273. reinterpret_cast<intptr_t>(
  274. obj->GetInternalField(kPropertyDeleterIndex)
  275. .As<v8::External>()->Value()));
  276. callback(property.As<v8::String>(), cbinfo);
  277. }
  278. typedef void (NativePropertyDeleter)
  279. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
  280. static
  281. void PropertyQueryCallbackWrapper(
  282. v8::Local<v8::Name> property
  283. , const v8::PropertyCallbackInfo<v8::Integer> &info) {
  284. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  285. PropertyCallbackInfo<v8::Integer>
  286. cbinfo(info, obj->GetInternalField(kDataIndex));
  287. PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
  288. reinterpret_cast<intptr_t>(
  289. obj->GetInternalField(kPropertyQueryIndex)
  290. .As<v8::External>()->Value()));
  291. callback(property.As<v8::String>(), cbinfo);
  292. }
  293. typedef void (*NativePropertyQuery)
  294. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
  295. #else
  296. static
  297. void PropertyGetterCallbackWrapper(
  298. v8::Local<v8::String> property
  299. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  300. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  301. PropertyCallbackInfo<v8::Value>
  302. cbinfo(info, obj->GetInternalField(kDataIndex));
  303. PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
  304. reinterpret_cast<intptr_t>(
  305. obj->GetInternalField(kPropertyGetterIndex)
  306. .As<v8::External>()->Value()));
  307. callback(property, cbinfo);
  308. }
  309. typedef void (*NativePropertyGetter)
  310. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
  311. static
  312. void PropertySetterCallbackWrapper(
  313. v8::Local<v8::String> property
  314. , v8::Local<v8::Value> value
  315. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  316. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  317. PropertyCallbackInfo<v8::Value>
  318. cbinfo(info, obj->GetInternalField(kDataIndex));
  319. PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
  320. reinterpret_cast<intptr_t>(
  321. obj->GetInternalField(kPropertySetterIndex)
  322. .As<v8::External>()->Value()));
  323. callback(property, value, cbinfo);
  324. }
  325. typedef void (*NativePropertySetter)(
  326. v8::Local<v8::String>
  327. , v8::Local<v8::Value>
  328. , const v8::PropertyCallbackInfo<v8::Value> &);
  329. static
  330. void PropertyEnumeratorCallbackWrapper(
  331. const v8::PropertyCallbackInfo<v8::Array> &info) {
  332. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  333. PropertyCallbackInfo<v8::Array>
  334. cbinfo(info, obj->GetInternalField(kDataIndex));
  335. PropertyEnumeratorCallback callback =
  336. reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
  337. obj->GetInternalField(kPropertyEnumeratorIndex)
  338. .As<v8::External>()->Value()));
  339. callback(cbinfo);
  340. }
  341. typedef void (*NativePropertyEnumerator)
  342. (const v8::PropertyCallbackInfo<v8::Array> &);
  343. static
  344. void PropertyDeleterCallbackWrapper(
  345. v8::Local<v8::String> property
  346. , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  347. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  348. PropertyCallbackInfo<v8::Boolean>
  349. cbinfo(info, obj->GetInternalField(kDataIndex));
  350. PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
  351. reinterpret_cast<intptr_t>(
  352. obj->GetInternalField(kPropertyDeleterIndex)
  353. .As<v8::External>()->Value()));
  354. callback(property, cbinfo);
  355. }
  356. typedef void (NativePropertyDeleter)
  357. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
  358. static
  359. void PropertyQueryCallbackWrapper(
  360. v8::Local<v8::String> property
  361. , const v8::PropertyCallbackInfo<v8::Integer> &info) {
  362. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  363. PropertyCallbackInfo<v8::Integer>
  364. cbinfo(info, obj->GetInternalField(kDataIndex));
  365. PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
  366. reinterpret_cast<intptr_t>(
  367. obj->GetInternalField(kPropertyQueryIndex)
  368. .As<v8::External>()->Value()));
  369. callback(property, cbinfo);
  370. }
  371. typedef void (*NativePropertyQuery)
  372. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
  373. #endif
  374. static
  375. void IndexGetterCallbackWrapper(
  376. uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info) {
  377. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  378. PropertyCallbackInfo<v8::Value>
  379. cbinfo(info, obj->GetInternalField(kDataIndex));
  380. IndexGetterCallback callback = reinterpret_cast<IndexGetterCallback>(
  381. reinterpret_cast<intptr_t>(
  382. obj->GetInternalField(kIndexPropertyGetterIndex)
  383. .As<v8::External>()->Value()));
  384. callback(index, cbinfo);
  385. }
  386. typedef void (*NativeIndexGetter)
  387. (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
  388. static
  389. void IndexSetterCallbackWrapper(
  390. uint32_t index
  391. , v8::Local<v8::Value> value
  392. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  393. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  394. PropertyCallbackInfo<v8::Value>
  395. cbinfo(info, obj->GetInternalField(kDataIndex));
  396. IndexSetterCallback callback = reinterpret_cast<IndexSetterCallback>(
  397. reinterpret_cast<intptr_t>(
  398. obj->GetInternalField(kIndexPropertySetterIndex)
  399. .As<v8::External>()->Value()));
  400. callback(index, value, cbinfo);
  401. }
  402. typedef void (*NativeIndexSetter)(
  403. uint32_t
  404. , v8::Local<v8::Value>
  405. , const v8::PropertyCallbackInfo<v8::Value> &);
  406. static
  407. void IndexEnumeratorCallbackWrapper(
  408. const v8::PropertyCallbackInfo<v8::Array> &info) {
  409. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  410. PropertyCallbackInfo<v8::Array>
  411. cbinfo(info, obj->GetInternalField(kDataIndex));
  412. IndexEnumeratorCallback callback = reinterpret_cast<IndexEnumeratorCallback>(
  413. reinterpret_cast<intptr_t>(
  414. obj->GetInternalField(
  415. kIndexPropertyEnumeratorIndex).As<v8::External>()->Value()));
  416. callback(cbinfo);
  417. }
  418. typedef void (*NativeIndexEnumerator)
  419. (const v8::PropertyCallbackInfo<v8::Array> &);
  420. static
  421. void IndexDeleterCallbackWrapper(
  422. uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  423. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  424. PropertyCallbackInfo<v8::Boolean>
  425. cbinfo(info, obj->GetInternalField(kDataIndex));
  426. IndexDeleterCallback callback = reinterpret_cast<IndexDeleterCallback>(
  427. reinterpret_cast<intptr_t>(
  428. obj->GetInternalField(kIndexPropertyDeleterIndex)
  429. .As<v8::External>()->Value()));
  430. callback(index, cbinfo);
  431. }
  432. typedef void (*NativeIndexDeleter)
  433. (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
  434. static
  435. void IndexQueryCallbackWrapper(
  436. uint32_t index, const v8::PropertyCallbackInfo<v8::Integer> &info) {
  437. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  438. PropertyCallbackInfo<v8::Integer>
  439. cbinfo(info, obj->GetInternalField(kDataIndex));
  440. IndexQueryCallback callback = reinterpret_cast<IndexQueryCallback>(
  441. reinterpret_cast<intptr_t>(
  442. obj->GetInternalField(kIndexPropertyQueryIndex)
  443. .As<v8::External>()->Value()));
  444. callback(index, cbinfo);
  445. }
  446. typedef void (*NativeIndexQuery)
  447. (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
  448. } // end of namespace imp
  449. #endif // NAN_CALLBACKS_12_INL_H_