nan_callbacks_12_inl.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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_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. inline v8::Local<v8::Function> Callee() const { return info_.Callee(); }
  91. inline v8::Local<v8::Value> Data() const { return data_; }
  92. inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
  93. inline bool IsConstructCall() const { return info_.IsConstructCall(); }
  94. inline int Length() const { return info_.Length(); }
  95. inline v8::Local<v8::Value> operator[](int i) const { return info_[i]; }
  96. inline v8::Local<v8::Object> This() const { return info_.This(); }
  97. inline v8::Isolate *GetIsolate() const { return info_.GetIsolate(); }
  98. protected:
  99. static const int kHolderIndex = 0;
  100. static const int kIsolateIndex = 1;
  101. static const int kReturnValueDefaultValueIndex = 2;
  102. static const int kReturnValueIndex = 3;
  103. static const int kDataIndex = 4;
  104. static const int kCalleeIndex = 5;
  105. static const int kContextSaveIndex = 6;
  106. static const int kArgsLength = 7;
  107. private:
  108. NAN_DISALLOW_ASSIGN_COPY_MOVE(FunctionCallbackInfo)
  109. };
  110. template<typename T>
  111. class PropertyCallbackInfo {
  112. const v8::PropertyCallbackInfo<T> &info_;
  113. const v8::Local<v8::Value> data_;
  114. public:
  115. explicit inline PropertyCallbackInfo(
  116. const v8::PropertyCallbackInfo<T> &info
  117. , const v8::Local<v8::Value> data) :
  118. info_(info)
  119. , data_(data) {}
  120. inline v8::Isolate* GetIsolate() const { return info_.GetIsolate(); }
  121. inline v8::Local<v8::Value> Data() const { return data_; }
  122. inline v8::Local<v8::Object> This() const { return info_.This(); }
  123. inline v8::Local<v8::Object> Holder() const { return info_.Holder(); }
  124. inline ReturnValue<T> GetReturnValue() const {
  125. return ReturnValue<T>(info_.GetReturnValue());
  126. }
  127. protected:
  128. static const int kHolderIndex = 0;
  129. static const int kIsolateIndex = 1;
  130. static const int kReturnValueDefaultValueIndex = 2;
  131. static const int kReturnValueIndex = 3;
  132. static const int kDataIndex = 4;
  133. static const int kThisIndex = 5;
  134. static const int kArgsLength = 6;
  135. private:
  136. NAN_DISALLOW_ASSIGN_COPY_MOVE(PropertyCallbackInfo)
  137. };
  138. namespace imp {
  139. static
  140. void FunctionCallbackWrapper(const v8::FunctionCallbackInfo<v8::Value> &info) {
  141. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  142. FunctionCallback callback = reinterpret_cast<FunctionCallback>(
  143. reinterpret_cast<intptr_t>(
  144. obj->GetInternalField(kFunctionIndex).As<v8::External>()->Value()));
  145. FunctionCallbackInfo<v8::Value>
  146. cbinfo(info, obj->GetInternalField(kDataIndex));
  147. callback(cbinfo);
  148. }
  149. typedef void (*NativeFunction)(const v8::FunctionCallbackInfo<v8::Value> &);
  150. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  151. static
  152. void GetterCallbackWrapper(
  153. v8::Local<v8::Name> property
  154. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  155. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  156. PropertyCallbackInfo<v8::Value>
  157. cbinfo(info, obj->GetInternalField(kDataIndex));
  158. GetterCallback callback = reinterpret_cast<GetterCallback>(
  159. reinterpret_cast<intptr_t>(
  160. obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
  161. callback(property.As<v8::String>(), cbinfo);
  162. }
  163. typedef void (*NativeGetter)
  164. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
  165. static
  166. void SetterCallbackWrapper(
  167. v8::Local<v8::Name> property
  168. , v8::Local<v8::Value> value
  169. , const v8::PropertyCallbackInfo<void> &info) {
  170. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  171. PropertyCallbackInfo<void>
  172. cbinfo(info, obj->GetInternalField(kDataIndex));
  173. SetterCallback callback = reinterpret_cast<SetterCallback>(
  174. reinterpret_cast<intptr_t>(
  175. obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
  176. callback(property.As<v8::String>(), value, cbinfo);
  177. }
  178. typedef void (*NativeSetter)(
  179. v8::Local<v8::Name>
  180. , v8::Local<v8::Value>
  181. , const v8::PropertyCallbackInfo<void> &);
  182. #else
  183. static
  184. void GetterCallbackWrapper(
  185. v8::Local<v8::String> property
  186. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  187. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  188. PropertyCallbackInfo<v8::Value>
  189. cbinfo(info, obj->GetInternalField(kDataIndex));
  190. GetterCallback callback = reinterpret_cast<GetterCallback>(
  191. reinterpret_cast<intptr_t>(
  192. obj->GetInternalField(kGetterIndex).As<v8::External>()->Value()));
  193. callback(property, cbinfo);
  194. }
  195. typedef void (*NativeGetter)
  196. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
  197. static
  198. void SetterCallbackWrapper(
  199. v8::Local<v8::String> property
  200. , v8::Local<v8::Value> value
  201. , const v8::PropertyCallbackInfo<void> &info) {
  202. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  203. PropertyCallbackInfo<void>
  204. cbinfo(info, obj->GetInternalField(kDataIndex));
  205. SetterCallback callback = reinterpret_cast<SetterCallback>(
  206. reinterpret_cast<intptr_t>(
  207. obj->GetInternalField(kSetterIndex).As<v8::External>()->Value()));
  208. callback(property, value, cbinfo);
  209. }
  210. typedef void (*NativeSetter)(
  211. v8::Local<v8::String>
  212. , v8::Local<v8::Value>
  213. , const v8::PropertyCallbackInfo<void> &);
  214. #endif
  215. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  216. static
  217. void PropertyGetterCallbackWrapper(
  218. v8::Local<v8::Name> property
  219. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  220. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  221. PropertyCallbackInfo<v8::Value>
  222. cbinfo(info, obj->GetInternalField(kDataIndex));
  223. PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
  224. reinterpret_cast<intptr_t>(
  225. obj->GetInternalField(kPropertyGetterIndex)
  226. .As<v8::External>()->Value()));
  227. callback(property.As<v8::String>(), cbinfo);
  228. }
  229. typedef void (*NativePropertyGetter)
  230. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Value> &);
  231. static
  232. void PropertySetterCallbackWrapper(
  233. v8::Local<v8::Name> property
  234. , v8::Local<v8::Value> value
  235. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  236. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  237. PropertyCallbackInfo<v8::Value>
  238. cbinfo(info, obj->GetInternalField(kDataIndex));
  239. PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
  240. reinterpret_cast<intptr_t>(
  241. obj->GetInternalField(kPropertySetterIndex)
  242. .As<v8::External>()->Value()));
  243. callback(property.As<v8::String>(), value, cbinfo);
  244. }
  245. typedef void (*NativePropertySetter)(
  246. v8::Local<v8::Name>
  247. , v8::Local<v8::Value>
  248. , const v8::PropertyCallbackInfo<v8::Value> &);
  249. static
  250. void PropertyEnumeratorCallbackWrapper(
  251. const v8::PropertyCallbackInfo<v8::Array> &info) {
  252. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  253. PropertyCallbackInfo<v8::Array>
  254. cbinfo(info, obj->GetInternalField(kDataIndex));
  255. PropertyEnumeratorCallback callback =
  256. reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
  257. obj->GetInternalField(kPropertyEnumeratorIndex)
  258. .As<v8::External>()->Value()));
  259. callback(cbinfo);
  260. }
  261. typedef void (*NativePropertyEnumerator)
  262. (const v8::PropertyCallbackInfo<v8::Array> &);
  263. static
  264. void PropertyDeleterCallbackWrapper(
  265. v8::Local<v8::Name> property
  266. , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  267. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  268. PropertyCallbackInfo<v8::Boolean>
  269. cbinfo(info, obj->GetInternalField(kDataIndex));
  270. PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
  271. reinterpret_cast<intptr_t>(
  272. obj->GetInternalField(kPropertyDeleterIndex)
  273. .As<v8::External>()->Value()));
  274. callback(property.As<v8::String>(), cbinfo);
  275. }
  276. typedef void (NativePropertyDeleter)
  277. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Boolean> &);
  278. static
  279. void PropertyQueryCallbackWrapper(
  280. v8::Local<v8::Name> property
  281. , const v8::PropertyCallbackInfo<v8::Integer> &info) {
  282. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  283. PropertyCallbackInfo<v8::Integer>
  284. cbinfo(info, obj->GetInternalField(kDataIndex));
  285. PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
  286. reinterpret_cast<intptr_t>(
  287. obj->GetInternalField(kPropertyQueryIndex)
  288. .As<v8::External>()->Value()));
  289. callback(property.As<v8::String>(), cbinfo);
  290. }
  291. typedef void (*NativePropertyQuery)
  292. (v8::Local<v8::Name>, const v8::PropertyCallbackInfo<v8::Integer> &);
  293. #else
  294. static
  295. void PropertyGetterCallbackWrapper(
  296. v8::Local<v8::String> property
  297. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  298. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  299. PropertyCallbackInfo<v8::Value>
  300. cbinfo(info, obj->GetInternalField(kDataIndex));
  301. PropertyGetterCallback callback = reinterpret_cast<PropertyGetterCallback>(
  302. reinterpret_cast<intptr_t>(
  303. obj->GetInternalField(kPropertyGetterIndex)
  304. .As<v8::External>()->Value()));
  305. callback(property, cbinfo);
  306. }
  307. typedef void (*NativePropertyGetter)
  308. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Value> &);
  309. static
  310. void PropertySetterCallbackWrapper(
  311. v8::Local<v8::String> property
  312. , v8::Local<v8::Value> value
  313. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  314. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  315. PropertyCallbackInfo<v8::Value>
  316. cbinfo(info, obj->GetInternalField(kDataIndex));
  317. PropertySetterCallback callback = reinterpret_cast<PropertySetterCallback>(
  318. reinterpret_cast<intptr_t>(
  319. obj->GetInternalField(kPropertySetterIndex)
  320. .As<v8::External>()->Value()));
  321. callback(property, value, cbinfo);
  322. }
  323. typedef void (*NativePropertySetter)(
  324. v8::Local<v8::String>
  325. , v8::Local<v8::Value>
  326. , const v8::PropertyCallbackInfo<v8::Value> &);
  327. static
  328. void PropertyEnumeratorCallbackWrapper(
  329. const v8::PropertyCallbackInfo<v8::Array> &info) {
  330. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  331. PropertyCallbackInfo<v8::Array>
  332. cbinfo(info, obj->GetInternalField(kDataIndex));
  333. PropertyEnumeratorCallback callback =
  334. reinterpret_cast<PropertyEnumeratorCallback>(reinterpret_cast<intptr_t>(
  335. obj->GetInternalField(kPropertyEnumeratorIndex)
  336. .As<v8::External>()->Value()));
  337. callback(cbinfo);
  338. }
  339. typedef void (*NativePropertyEnumerator)
  340. (const v8::PropertyCallbackInfo<v8::Array> &);
  341. static
  342. void PropertyDeleterCallbackWrapper(
  343. v8::Local<v8::String> property
  344. , const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  345. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  346. PropertyCallbackInfo<v8::Boolean>
  347. cbinfo(info, obj->GetInternalField(kDataIndex));
  348. PropertyDeleterCallback callback = reinterpret_cast<PropertyDeleterCallback>(
  349. reinterpret_cast<intptr_t>(
  350. obj->GetInternalField(kPropertyDeleterIndex)
  351. .As<v8::External>()->Value()));
  352. callback(property, cbinfo);
  353. }
  354. typedef void (NativePropertyDeleter)
  355. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Boolean> &);
  356. static
  357. void PropertyQueryCallbackWrapper(
  358. v8::Local<v8::String> property
  359. , const v8::PropertyCallbackInfo<v8::Integer> &info) {
  360. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  361. PropertyCallbackInfo<v8::Integer>
  362. cbinfo(info, obj->GetInternalField(kDataIndex));
  363. PropertyQueryCallback callback = reinterpret_cast<PropertyQueryCallback>(
  364. reinterpret_cast<intptr_t>(
  365. obj->GetInternalField(kPropertyQueryIndex)
  366. .As<v8::External>()->Value()));
  367. callback(property, cbinfo);
  368. }
  369. typedef void (*NativePropertyQuery)
  370. (v8::Local<v8::String>, const v8::PropertyCallbackInfo<v8::Integer> &);
  371. #endif
  372. static
  373. void IndexGetterCallbackWrapper(
  374. uint32_t index, const v8::PropertyCallbackInfo<v8::Value> &info) {
  375. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  376. PropertyCallbackInfo<v8::Value>
  377. cbinfo(info, obj->GetInternalField(kDataIndex));
  378. IndexGetterCallback callback = reinterpret_cast<IndexGetterCallback>(
  379. reinterpret_cast<intptr_t>(
  380. obj->GetInternalField(kIndexPropertyGetterIndex)
  381. .As<v8::External>()->Value()));
  382. callback(index, cbinfo);
  383. }
  384. typedef void (*NativeIndexGetter)
  385. (uint32_t, const v8::PropertyCallbackInfo<v8::Value> &);
  386. static
  387. void IndexSetterCallbackWrapper(
  388. uint32_t index
  389. , v8::Local<v8::Value> value
  390. , const v8::PropertyCallbackInfo<v8::Value> &info) {
  391. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  392. PropertyCallbackInfo<v8::Value>
  393. cbinfo(info, obj->GetInternalField(kDataIndex));
  394. IndexSetterCallback callback = reinterpret_cast<IndexSetterCallback>(
  395. reinterpret_cast<intptr_t>(
  396. obj->GetInternalField(kIndexPropertySetterIndex)
  397. .As<v8::External>()->Value()));
  398. callback(index, value, cbinfo);
  399. }
  400. typedef void (*NativeIndexSetter)(
  401. uint32_t
  402. , v8::Local<v8::Value>
  403. , const v8::PropertyCallbackInfo<v8::Value> &);
  404. static
  405. void IndexEnumeratorCallbackWrapper(
  406. const v8::PropertyCallbackInfo<v8::Array> &info) {
  407. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  408. PropertyCallbackInfo<v8::Array>
  409. cbinfo(info, obj->GetInternalField(kDataIndex));
  410. IndexEnumeratorCallback callback = reinterpret_cast<IndexEnumeratorCallback>(
  411. reinterpret_cast<intptr_t>(
  412. obj->GetInternalField(
  413. kIndexPropertyEnumeratorIndex).As<v8::External>()->Value()));
  414. callback(cbinfo);
  415. }
  416. typedef void (*NativeIndexEnumerator)
  417. (const v8::PropertyCallbackInfo<v8::Array> &);
  418. static
  419. void IndexDeleterCallbackWrapper(
  420. uint32_t index, const v8::PropertyCallbackInfo<v8::Boolean> &info) {
  421. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  422. PropertyCallbackInfo<v8::Boolean>
  423. cbinfo(info, obj->GetInternalField(kDataIndex));
  424. IndexDeleterCallback callback = reinterpret_cast<IndexDeleterCallback>(
  425. reinterpret_cast<intptr_t>(
  426. obj->GetInternalField(kIndexPropertyDeleterIndex)
  427. .As<v8::External>()->Value()));
  428. callback(index, cbinfo);
  429. }
  430. typedef void (*NativeIndexDeleter)
  431. (uint32_t, const v8::PropertyCallbackInfo<v8::Boolean> &);
  432. static
  433. void IndexQueryCallbackWrapper(
  434. uint32_t index, const v8::PropertyCallbackInfo<v8::Integer> &info) {
  435. v8::Local<v8::Object> obj = info.Data().As<v8::Object>();
  436. PropertyCallbackInfo<v8::Integer>
  437. cbinfo(info, obj->GetInternalField(kDataIndex));
  438. IndexQueryCallback callback = reinterpret_cast<IndexQueryCallback>(
  439. reinterpret_cast<intptr_t>(
  440. obj->GetInternalField(kIndexPropertyQueryIndex)
  441. .As<v8::External>()->Value()));
  442. callback(index, cbinfo);
  443. }
  444. typedef void (*NativeIndexQuery)
  445. (uint32_t, const v8::PropertyCallbackInfo<v8::Integer> &);
  446. } // end of namespace imp
  447. #endif // NAN_CALLBACKS_12_INL_H_