nan.h 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904
  1. /*********************************************************************
  2. * NAN - Native Abstractions for Node.js
  3. *
  4. * Copyright (c) 2018 NAN contributors:
  5. * - Rod Vagg <https://github.com/rvagg>
  6. * - Benjamin Byholm <https://github.com/kkoopa>
  7. * - Trevor Norris <https://github.com/trevnorris>
  8. * - Nathan Rajlich <https://github.com/TooTallNate>
  9. * - Brett Lawson <https://github.com/brett19>
  10. * - Ben Noordhuis <https://github.com/bnoordhuis>
  11. * - David Siegel <https://github.com/agnat>
  12. * - Michael Ira Krufky <https://github.com/mkrufky>
  13. *
  14. * MIT License <https://github.com/nodejs/nan/blob/master/LICENSE.md>
  15. *
  16. * Version 2.15.0: current Node 16.6.1, Node 0.12: 0.12.18, Node 0.10: 0.10.48, iojs: 3.3.1
  17. *
  18. * See https://github.com/nodejs/nan for the latest update to this file
  19. **********************************************************************************/
  20. #ifndef NAN_H_
  21. #define NAN_H_
  22. #include <node_version.h>
  23. #define NODE_0_10_MODULE_VERSION 11
  24. #define NODE_0_12_MODULE_VERSION 14
  25. #define ATOM_0_21_MODULE_VERSION 41
  26. #define IOJS_1_0_MODULE_VERSION 42
  27. #define IOJS_1_1_MODULE_VERSION 43
  28. #define IOJS_2_0_MODULE_VERSION 44
  29. #define IOJS_3_0_MODULE_VERSION 45
  30. #define NODE_4_0_MODULE_VERSION 46
  31. #define NODE_5_0_MODULE_VERSION 47
  32. #define NODE_6_0_MODULE_VERSION 48
  33. #define NODE_7_0_MODULE_VERSION 51
  34. #define NODE_8_0_MODULE_VERSION 57
  35. #define NODE_9_0_MODULE_VERSION 59
  36. #define NODE_10_0_MODULE_VERSION 64
  37. #define NODE_11_0_MODULE_VERSION 67
  38. #define NODE_12_0_MODULE_VERSION 72
  39. #define NODE_13_0_MODULE_VERSION 79
  40. #define NODE_14_0_MODULE_VERSION 83
  41. #define NODE_15_0_MODULE_VERSION 88
  42. #define NODE_16_0_MODULE_VERSION 93
  43. #ifdef _MSC_VER
  44. # define NAN_HAS_CPLUSPLUS_11 (_MSC_VER >= 1800)
  45. #else
  46. # define NAN_HAS_CPLUSPLUS_11 (__cplusplus >= 201103L)
  47. #endif
  48. #if NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION && !NAN_HAS_CPLUSPLUS_11
  49. # error This version of node/NAN/v8 requires a C++11 compiler
  50. #endif
  51. #include <uv.h>
  52. #include <node.h>
  53. #include <node_buffer.h>
  54. #include <node_object_wrap.h>
  55. #include <algorithm>
  56. #include <cstring>
  57. #include <climits>
  58. #include <cstdlib>
  59. #include <utility>
  60. #if defined(_MSC_VER)
  61. # pragma warning( push )
  62. # pragma warning( disable : 4530 )
  63. # include <queue>
  64. # include <string>
  65. # include <vector>
  66. # pragma warning( pop )
  67. #else
  68. # include <queue>
  69. # include <string>
  70. # include <vector>
  71. #endif
  72. // uv helpers
  73. #ifdef UV_VERSION_MAJOR
  74. # ifndef UV_VERSION_PATCH
  75. # define UV_VERSION_PATCH 0
  76. # endif
  77. # define NAUV_UVVERSION ((UV_VERSION_MAJOR << 16) | \
  78. (UV_VERSION_MINOR << 8) | \
  79. (UV_VERSION_PATCH))
  80. #else
  81. # define NAUV_UVVERSION 0x000b00
  82. #endif
  83. #if NAUV_UVVERSION < 0x000b0b
  84. # ifdef WIN32
  85. # include <windows.h>
  86. # else
  87. # include <pthread.h>
  88. # endif
  89. #endif
  90. namespace Nan {
  91. #define NAN_CONCAT(a, b) NAN_CONCAT_HELPER(a, b)
  92. #define NAN_CONCAT_HELPER(a, b) a##b
  93. #define NAN_INLINE inline // TODO(bnoordhuis) Remove in v3.0.0.
  94. #if defined(__GNUC__) && \
  95. !(defined(V8_DISABLE_DEPRECATIONS) && V8_DISABLE_DEPRECATIONS)
  96. # define NAN_DEPRECATED __attribute__((deprecated))
  97. #elif defined(_MSC_VER) && \
  98. !(defined(V8_DISABLE_DEPRECATIONS) && V8_DISABLE_DEPRECATIONS)
  99. # define NAN_DEPRECATED __declspec(deprecated)
  100. #else
  101. # define NAN_DEPRECATED
  102. #endif
  103. #if NAN_HAS_CPLUSPLUS_11
  104. # define NAN_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
  105. # define NAN_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
  106. # define NAN_DISALLOW_MOVE(CLASS) \
  107. CLASS(CLASS&&) = delete; /* NOLINT(build/c++11) */ \
  108. void operator=(CLASS&&) = delete;
  109. #else
  110. # define NAN_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&);
  111. # define NAN_DISALLOW_COPY(CLASS) CLASS(const CLASS&);
  112. # define NAN_DISALLOW_MOVE(CLASS)
  113. #endif
  114. #define NAN_DISALLOW_ASSIGN_COPY(CLASS) \
  115. NAN_DISALLOW_ASSIGN(CLASS) \
  116. NAN_DISALLOW_COPY(CLASS)
  117. #define NAN_DISALLOW_ASSIGN_MOVE(CLASS) \
  118. NAN_DISALLOW_ASSIGN(CLASS) \
  119. NAN_DISALLOW_MOVE(CLASS)
  120. #define NAN_DISALLOW_COPY_MOVE(CLASS) \
  121. NAN_DISALLOW_COPY(CLASS) \
  122. NAN_DISALLOW_MOVE(CLASS)
  123. #define NAN_DISALLOW_ASSIGN_COPY_MOVE(CLASS) \
  124. NAN_DISALLOW_ASSIGN(CLASS) \
  125. NAN_DISALLOW_COPY(CLASS) \
  126. NAN_DISALLOW_MOVE(CLASS)
  127. #define TYPE_CHECK(T, S) \
  128. while (false) { \
  129. *(static_cast<T *volatile *>(0)) = static_cast<S*>(0); \
  130. }
  131. //=== RegistrationFunction =====================================================
  132. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  133. typedef v8::Handle<v8::Object> ADDON_REGISTER_FUNCTION_ARGS_TYPE;
  134. #else
  135. typedef v8::Local<v8::Object> ADDON_REGISTER_FUNCTION_ARGS_TYPE;
  136. #endif
  137. #define NAN_MODULE_INIT(name) \
  138. void name(Nan::ADDON_REGISTER_FUNCTION_ARGS_TYPE target)
  139. #if NODE_MAJOR_VERSION >= 10 || \
  140. NODE_MAJOR_VERSION == 9 && NODE_MINOR_VERSION >= 3
  141. #define NAN_MODULE_WORKER_ENABLED(module_name, registration) \
  142. extern "C" NODE_MODULE_EXPORT void \
  143. NAN_CONCAT(node_register_module_v, NODE_MODULE_VERSION)( \
  144. v8::Local<v8::Object> exports, v8::Local<v8::Value> module, \
  145. v8::Local<v8::Context> context) \
  146. { \
  147. registration(exports); \
  148. }
  149. #else
  150. #define NAN_MODULE_WORKER_ENABLED(module_name, registration) \
  151. NODE_MODULE(module_name, registration)
  152. #endif
  153. //=== CallbackInfo =============================================================
  154. #include "nan_callbacks.h" // NOLINT(build/include)
  155. //==============================================================================
  156. #if (NODE_MODULE_VERSION < NODE_0_12_MODULE_VERSION)
  157. typedef v8::Script UnboundScript;
  158. typedef v8::Script BoundScript;
  159. #else
  160. typedef v8::UnboundScript UnboundScript;
  161. typedef v8::Script BoundScript;
  162. #endif
  163. #if (NODE_MODULE_VERSION < ATOM_0_21_MODULE_VERSION)
  164. typedef v8::String::ExternalAsciiStringResource
  165. ExternalOneByteStringResource;
  166. #else
  167. typedef v8::String::ExternalOneByteStringResource
  168. ExternalOneByteStringResource;
  169. #endif
  170. #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
  171. template<typename T>
  172. class NonCopyablePersistentTraits :
  173. public v8::NonCopyablePersistentTraits<T> {};
  174. template<typename T>
  175. class CopyablePersistentTraits :
  176. public v8::CopyablePersistentTraits<T> {};
  177. template<typename T>
  178. class PersistentBase :
  179. public v8::PersistentBase<T> {};
  180. template<typename T, typename M = v8::NonCopyablePersistentTraits<T> >
  181. class Persistent;
  182. #else
  183. template<typename T> class NonCopyablePersistentTraits;
  184. template<typename T> class PersistentBase;
  185. template<typename T, typename P> class WeakCallbackData;
  186. template<typename T, typename M = NonCopyablePersistentTraits<T> >
  187. class Persistent;
  188. #endif // NODE_MODULE_VERSION
  189. template<typename T>
  190. class Maybe {
  191. public:
  192. inline bool IsNothing() const { return !has_value_; }
  193. inline bool IsJust() const { return has_value_; }
  194. inline T ToChecked() const { return FromJust(); }
  195. inline void Check() const { FromJust(); }
  196. inline bool To(T* out) const {
  197. if (IsJust()) *out = value_;
  198. return IsJust();
  199. }
  200. inline T FromJust() const {
  201. #if defined(V8_ENABLE_CHECKS)
  202. assert(IsJust() && "FromJust is Nothing");
  203. #endif // V8_ENABLE_CHECKS
  204. return value_;
  205. }
  206. inline T FromMaybe(const T& default_value) const {
  207. return has_value_ ? value_ : default_value;
  208. }
  209. inline bool operator==(const Maybe &other) const {
  210. return (IsJust() == other.IsJust()) &&
  211. (!IsJust() || FromJust() == other.FromJust());
  212. }
  213. inline bool operator!=(const Maybe &other) const {
  214. return !operator==(other);
  215. }
  216. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  217. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  218. // Allow implicit conversions from v8::Maybe<T> to Nan::Maybe<T>.
  219. Maybe(const v8::Maybe<T>& that) // NOLINT(runtime/explicit)
  220. : has_value_(that.IsJust())
  221. , value_(that.FromMaybe(T())) {}
  222. #endif
  223. private:
  224. Maybe() : has_value_(false) {}
  225. explicit Maybe(const T& t) : has_value_(true), value_(t) {}
  226. bool has_value_;
  227. T value_;
  228. template<typename U>
  229. friend Maybe<U> Nothing();
  230. template<typename U>
  231. friend Maybe<U> Just(const U& u);
  232. };
  233. template<typename T>
  234. inline Maybe<T> Nothing() {
  235. return Maybe<T>();
  236. }
  237. template<typename T>
  238. inline Maybe<T> Just(const T& t) {
  239. return Maybe<T>(t);
  240. }
  241. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  242. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  243. # include "nan_maybe_43_inl.h" // NOLINT(build/include)
  244. #else
  245. # include "nan_maybe_pre_43_inl.h" // NOLINT(build/include)
  246. #endif
  247. #include "nan_converters.h" // NOLINT(build/include)
  248. #include "nan_new.h" // NOLINT(build/include)
  249. #if NAUV_UVVERSION < 0x000b17
  250. #define NAUV_WORK_CB(func) \
  251. void func(uv_async_t *async, int)
  252. #else
  253. #define NAUV_WORK_CB(func) \
  254. void func(uv_async_t *async)
  255. #endif
  256. #if NAUV_UVVERSION >= 0x000b0b
  257. typedef uv_key_t nauv_key_t;
  258. inline int nauv_key_create(nauv_key_t *key) {
  259. return uv_key_create(key);
  260. }
  261. inline void nauv_key_delete(nauv_key_t *key) {
  262. uv_key_delete(key);
  263. }
  264. inline void* nauv_key_get(nauv_key_t *key) {
  265. return uv_key_get(key);
  266. }
  267. inline void nauv_key_set(nauv_key_t *key, void *value) {
  268. uv_key_set(key, value);
  269. }
  270. #else
  271. /* Implement thread local storage for older versions of libuv.
  272. * This is essentially a backport of libuv commit 5d2434bf
  273. * written by Ben Noordhuis, adjusted for names and inline.
  274. */
  275. #ifndef WIN32
  276. typedef pthread_key_t nauv_key_t;
  277. inline int nauv_key_create(nauv_key_t* key) {
  278. return -pthread_key_create(key, NULL);
  279. }
  280. inline void nauv_key_delete(nauv_key_t* key) {
  281. if (pthread_key_delete(*key))
  282. abort();
  283. }
  284. inline void* nauv_key_get(nauv_key_t* key) {
  285. return pthread_getspecific(*key);
  286. }
  287. inline void nauv_key_set(nauv_key_t* key, void* value) {
  288. if (pthread_setspecific(*key, value))
  289. abort();
  290. }
  291. #else
  292. typedef struct {
  293. DWORD tls_index;
  294. } nauv_key_t;
  295. inline int nauv_key_create(nauv_key_t* key) {
  296. key->tls_index = TlsAlloc();
  297. if (key->tls_index == TLS_OUT_OF_INDEXES)
  298. return UV_ENOMEM;
  299. return 0;
  300. }
  301. inline void nauv_key_delete(nauv_key_t* key) {
  302. if (TlsFree(key->tls_index) == FALSE)
  303. abort();
  304. key->tls_index = TLS_OUT_OF_INDEXES;
  305. }
  306. inline void* nauv_key_get(nauv_key_t* key) {
  307. void* value = TlsGetValue(key->tls_index);
  308. if (value == NULL)
  309. if (GetLastError() != ERROR_SUCCESS)
  310. abort();
  311. return value;
  312. }
  313. inline void nauv_key_set(nauv_key_t* key, void* value) {
  314. if (TlsSetValue(key->tls_index, value) == FALSE)
  315. abort();
  316. }
  317. #endif
  318. #endif
  319. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  320. template<typename T>
  321. v8::Local<T> New(v8::Handle<T>);
  322. #endif
  323. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  324. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  325. typedef v8::WeakCallbackType WeakCallbackType;
  326. #else
  327. struct WeakCallbackType {
  328. enum E {kParameter, kInternalFields};
  329. E type;
  330. WeakCallbackType(E other) : type(other) {} // NOLINT(runtime/explicit)
  331. inline bool operator==(E other) { return other == this->type; }
  332. inline bool operator!=(E other) { return !operator==(other); }
  333. };
  334. #endif
  335. template<typename P> class WeakCallbackInfo;
  336. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  337. # include "nan_persistent_12_inl.h" // NOLINT(build/include)
  338. #else
  339. # include "nan_persistent_pre_12_inl.h" // NOLINT(build/include)
  340. #endif
  341. namespace imp {
  342. static const size_t kMaxLength = 0x3fffffff;
  343. // v8::String::REPLACE_INVALID_UTF8 was introduced
  344. // in node.js v0.10.29 and v0.8.27.
  345. #if NODE_MAJOR_VERSION > 0 || \
  346. NODE_MINOR_VERSION > 10 || \
  347. NODE_MINOR_VERSION == 10 && NODE_PATCH_VERSION >= 29 || \
  348. NODE_MINOR_VERSION == 8 && NODE_PATCH_VERSION >= 27
  349. static const unsigned kReplaceInvalidUtf8 = v8::String::REPLACE_INVALID_UTF8;
  350. #else
  351. static const unsigned kReplaceInvalidUtf8 = 0;
  352. #endif
  353. } // end of namespace imp
  354. //=== HandleScope ==============================================================
  355. class HandleScope {
  356. v8::HandleScope scope;
  357. public:
  358. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  359. inline HandleScope() : scope(v8::Isolate::GetCurrent()) {}
  360. inline static int NumberOfHandles() {
  361. return v8::HandleScope::NumberOfHandles(v8::Isolate::GetCurrent());
  362. }
  363. #else
  364. inline HandleScope() : scope() {}
  365. inline static int NumberOfHandles() {
  366. return v8::HandleScope::NumberOfHandles();
  367. }
  368. #endif
  369. private:
  370. // Make it hard to create heap-allocated or illegal handle scopes by
  371. // disallowing certain operations.
  372. HandleScope(const HandleScope &);
  373. void operator=(const HandleScope &);
  374. void *operator new(size_t size);
  375. void operator delete(void *, size_t) {
  376. abort();
  377. }
  378. };
  379. class EscapableHandleScope {
  380. public:
  381. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  382. inline EscapableHandleScope() : scope(v8::Isolate::GetCurrent()) {}
  383. inline static int NumberOfHandles() {
  384. return v8::EscapableHandleScope::NumberOfHandles(v8::Isolate::GetCurrent());
  385. }
  386. template<typename T>
  387. inline v8::Local<T> Escape(v8::Local<T> value) {
  388. return scope.Escape(value);
  389. }
  390. private:
  391. v8::EscapableHandleScope scope;
  392. #else
  393. inline EscapableHandleScope() : scope() {}
  394. inline static int NumberOfHandles() {
  395. return v8::HandleScope::NumberOfHandles();
  396. }
  397. template<typename T>
  398. inline v8::Local<T> Escape(v8::Local<T> value) {
  399. return scope.Close(value);
  400. }
  401. private:
  402. v8::HandleScope scope;
  403. #endif
  404. private:
  405. // Make it hard to create heap-allocated or illegal handle scopes by
  406. // disallowing certain operations.
  407. EscapableHandleScope(const EscapableHandleScope &);
  408. void operator=(const EscapableHandleScope &);
  409. void *operator new(size_t size);
  410. void operator delete(void *, size_t) {
  411. abort();
  412. }
  413. };
  414. //=== TryCatch =================================================================
  415. class TryCatch {
  416. v8::TryCatch try_catch_;
  417. friend void FatalException(const TryCatch&);
  418. public:
  419. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  420. TryCatch() : try_catch_(v8::Isolate::GetCurrent()) {}
  421. #endif
  422. inline bool HasCaught() const { return try_catch_.HasCaught(); }
  423. inline bool CanContinue() const { return try_catch_.CanContinue(); }
  424. inline v8::Local<v8::Value> ReThrow() {
  425. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  426. return New(try_catch_.ReThrow());
  427. #else
  428. return try_catch_.ReThrow();
  429. #endif
  430. }
  431. inline v8::Local<v8::Value> Exception() const {
  432. return try_catch_.Exception();
  433. }
  434. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  435. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  436. inline v8::MaybeLocal<v8::Value> StackTrace() const {
  437. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  438. v8::EscapableHandleScope scope(isolate);
  439. return scope.Escape(try_catch_.StackTrace(isolate->GetCurrentContext())
  440. .FromMaybe(v8::Local<v8::Value>()));
  441. }
  442. #else
  443. inline MaybeLocal<v8::Value> StackTrace() const {
  444. return try_catch_.StackTrace();
  445. }
  446. #endif
  447. inline v8::Local<v8::Message> Message() const {
  448. return try_catch_.Message();
  449. }
  450. inline void Reset() { try_catch_.Reset(); }
  451. inline void SetVerbose(bool value) { try_catch_.SetVerbose(value); }
  452. inline void SetCaptureMessage(bool value) {
  453. try_catch_.SetCaptureMessage(value);
  454. }
  455. };
  456. v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
  457. v8::Local<v8::Function> func,
  458. int argc,
  459. v8::Local<v8::Value>* argv);
  460. v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
  461. v8::Local<v8::String> symbol,
  462. int argc,
  463. v8::Local<v8::Value>* argv);
  464. v8::Local<v8::Value> MakeCallback(v8::Local<v8::Object> target,
  465. const char* method,
  466. int argc,
  467. v8::Local<v8::Value>* argv);
  468. // === AsyncResource ===========================================================
  469. class AsyncResource {
  470. public:
  471. AsyncResource(
  472. v8::Local<v8::String> name
  473. , v8::Local<v8::Object> resource = New<v8::Object>()) {
  474. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  475. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  476. if (resource.IsEmpty()) {
  477. resource = New<v8::Object>();
  478. }
  479. context = node::EmitAsyncInit(isolate, resource, name);
  480. #endif
  481. }
  482. AsyncResource(
  483. const char* name
  484. , v8::Local<v8::Object> resource = New<v8::Object>()) {
  485. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  486. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  487. if (resource.IsEmpty()) {
  488. resource = New<v8::Object>();
  489. }
  490. v8::Local<v8::String> name_string =
  491. New<v8::String>(name).ToLocalChecked();
  492. context = node::EmitAsyncInit(isolate, resource, name_string);
  493. #endif
  494. }
  495. ~AsyncResource() {
  496. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  497. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  498. node::EmitAsyncDestroy(isolate, context);
  499. #endif
  500. }
  501. inline MaybeLocal<v8::Value> runInAsyncScope(
  502. v8::Local<v8::Object> target
  503. , v8::Local<v8::Function> func
  504. , int argc
  505. , v8::Local<v8::Value>* argv) {
  506. #if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
  507. return MakeCallback(target, func, argc, argv);
  508. #else
  509. return node::MakeCallback(
  510. v8::Isolate::GetCurrent(), target, func, argc, argv, context);
  511. #endif
  512. }
  513. inline MaybeLocal<v8::Value> runInAsyncScope(
  514. v8::Local<v8::Object> target
  515. , v8::Local<v8::String> symbol
  516. , int argc
  517. , v8::Local<v8::Value>* argv) {
  518. #if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
  519. return MakeCallback(target, symbol, argc, argv);
  520. #else
  521. return node::MakeCallback(
  522. v8::Isolate::GetCurrent(), target, symbol, argc, argv, context);
  523. #endif
  524. }
  525. inline MaybeLocal<v8::Value> runInAsyncScope(
  526. v8::Local<v8::Object> target
  527. , const char* method
  528. , int argc
  529. , v8::Local<v8::Value>* argv) {
  530. #if NODE_MODULE_VERSION < NODE_9_0_MODULE_VERSION
  531. return MakeCallback(target, method, argc, argv);
  532. #else
  533. return node::MakeCallback(
  534. v8::Isolate::GetCurrent(), target, method, argc, argv, context);
  535. #endif
  536. }
  537. private:
  538. NAN_DISALLOW_ASSIGN_COPY_MOVE(AsyncResource)
  539. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  540. node::async_context context;
  541. #endif
  542. };
  543. inline uv_loop_t* GetCurrentEventLoop() {
  544. #if NODE_MAJOR_VERSION >= 10 || \
  545. NODE_MAJOR_VERSION == 9 && NODE_MINOR_VERSION >= 3 || \
  546. NODE_MAJOR_VERSION == 8 && NODE_MINOR_VERSION >= 10
  547. return node::GetCurrentEventLoop(v8::Isolate::GetCurrent());
  548. #else
  549. return uv_default_loop();
  550. #endif
  551. }
  552. //============ =================================================================
  553. /* node 0.12 */
  554. #if NODE_MODULE_VERSION >= NODE_0_12_MODULE_VERSION
  555. inline
  556. void SetCounterFunction(v8::CounterLookupCallback cb) {
  557. v8::Isolate::GetCurrent()->SetCounterFunction(cb);
  558. }
  559. inline
  560. void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
  561. v8::Isolate::GetCurrent()->SetCreateHistogramFunction(cb);
  562. }
  563. inline
  564. void SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) {
  565. v8::Isolate::GetCurrent()->SetAddHistogramSampleFunction(cb);
  566. }
  567. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  568. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  569. inline bool IdleNotification(int idle_time_in_ms) {
  570. return v8::Isolate::GetCurrent()->IdleNotificationDeadline(
  571. idle_time_in_ms * 0.001);
  572. }
  573. # else
  574. inline bool IdleNotification(int idle_time_in_ms) {
  575. return v8::Isolate::GetCurrent()->IdleNotification(idle_time_in_ms);
  576. }
  577. #endif
  578. inline void LowMemoryNotification() {
  579. v8::Isolate::GetCurrent()->LowMemoryNotification();
  580. }
  581. inline void ContextDisposedNotification() {
  582. v8::Isolate::GetCurrent()->ContextDisposedNotification();
  583. }
  584. #else
  585. inline
  586. void SetCounterFunction(v8::CounterLookupCallback cb) {
  587. v8::V8::SetCounterFunction(cb);
  588. }
  589. inline
  590. void SetCreateHistogramFunction(v8::CreateHistogramCallback cb) {
  591. v8::V8::SetCreateHistogramFunction(cb);
  592. }
  593. inline
  594. void SetAddHistogramSampleFunction(v8::AddHistogramSampleCallback cb) {
  595. v8::V8::SetAddHistogramSampleFunction(cb);
  596. }
  597. inline bool IdleNotification(int idle_time_in_ms) {
  598. return v8::V8::IdleNotification(idle_time_in_ms);
  599. }
  600. inline void LowMemoryNotification() {
  601. v8::V8::LowMemoryNotification();
  602. }
  603. inline void ContextDisposedNotification() {
  604. v8::V8::ContextDisposedNotification();
  605. }
  606. #endif
  607. #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION) // Node 0.12
  608. inline v8::Local<v8::Primitive> Undefined() {
  609. # if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  610. EscapableHandleScope scope;
  611. return scope.Escape(New(v8::Undefined(v8::Isolate::GetCurrent())));
  612. # else
  613. return v8::Undefined(v8::Isolate::GetCurrent());
  614. # endif
  615. }
  616. inline v8::Local<v8::Primitive> Null() {
  617. # if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  618. EscapableHandleScope scope;
  619. return scope.Escape(New(v8::Null(v8::Isolate::GetCurrent())));
  620. # else
  621. return v8::Null(v8::Isolate::GetCurrent());
  622. # endif
  623. }
  624. inline v8::Local<v8::Boolean> True() {
  625. # if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  626. EscapableHandleScope scope;
  627. return scope.Escape(New(v8::True(v8::Isolate::GetCurrent())));
  628. # else
  629. return v8::True(v8::Isolate::GetCurrent());
  630. # endif
  631. }
  632. inline v8::Local<v8::Boolean> False() {
  633. # if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  634. EscapableHandleScope scope;
  635. return scope.Escape(New(v8::False(v8::Isolate::GetCurrent())));
  636. # else
  637. return v8::False(v8::Isolate::GetCurrent());
  638. # endif
  639. }
  640. inline v8::Local<v8::String> EmptyString() {
  641. return v8::String::Empty(v8::Isolate::GetCurrent());
  642. }
  643. inline int AdjustExternalMemory(int bc) {
  644. return static_cast<int>(
  645. v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(bc));
  646. }
  647. inline void SetTemplate(
  648. v8::Local<v8::Template> templ
  649. , const char *name
  650. , v8::Local<v8::Data> value) {
  651. templ->Set(v8::Isolate::GetCurrent(), name, value);
  652. }
  653. inline void SetTemplate(
  654. v8::Local<v8::Template> templ
  655. , v8::Local<v8::String> name
  656. , v8::Local<v8::Data> value
  657. , v8::PropertyAttribute attributes) {
  658. templ->Set(name, value, attributes);
  659. }
  660. inline v8::Local<v8::Context> GetCurrentContext() {
  661. return v8::Isolate::GetCurrent()->GetCurrentContext();
  662. }
  663. inline void* GetInternalFieldPointer(
  664. v8::Local<v8::Object> object
  665. , int index) {
  666. return object->GetAlignedPointerFromInternalField(index);
  667. }
  668. inline void SetInternalFieldPointer(
  669. v8::Local<v8::Object> object
  670. , int index
  671. , void* value) {
  672. object->SetAlignedPointerInInternalField(index, value);
  673. }
  674. # define NAN_GC_CALLBACK(name) \
  675. void name(v8::Isolate *isolate, v8::GCType type, v8::GCCallbackFlags flags)
  676. #if NODE_MODULE_VERSION <= NODE_4_0_MODULE_VERSION
  677. typedef v8::Isolate::GCEpilogueCallback GCEpilogueCallback;
  678. typedef v8::Isolate::GCPrologueCallback GCPrologueCallback;
  679. #else
  680. typedef v8::Isolate::GCCallback GCEpilogueCallback;
  681. typedef v8::Isolate::GCCallback GCPrologueCallback;
  682. #endif
  683. inline void AddGCEpilogueCallback(
  684. GCEpilogueCallback callback
  685. , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  686. v8::Isolate::GetCurrent()->AddGCEpilogueCallback(callback, gc_type_filter);
  687. }
  688. inline void RemoveGCEpilogueCallback(
  689. GCEpilogueCallback callback) {
  690. v8::Isolate::GetCurrent()->RemoveGCEpilogueCallback(callback);
  691. }
  692. inline void AddGCPrologueCallback(
  693. GCPrologueCallback callback
  694. , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  695. v8::Isolate::GetCurrent()->AddGCPrologueCallback(callback, gc_type_filter);
  696. }
  697. inline void RemoveGCPrologueCallback(
  698. GCPrologueCallback callback) {
  699. v8::Isolate::GetCurrent()->RemoveGCPrologueCallback(callback);
  700. }
  701. inline void GetHeapStatistics(
  702. v8::HeapStatistics *heap_statistics) {
  703. v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics);
  704. }
  705. # define X(NAME) \
  706. inline v8::Local<v8::Value> NAME(const char *msg) { \
  707. EscapableHandleScope scope; \
  708. return scope.Escape(v8::Exception::NAME(New(msg).ToLocalChecked())); \
  709. } \
  710. \
  711. inline \
  712. v8::Local<v8::Value> NAME(v8::Local<v8::String> msg) { \
  713. return v8::Exception::NAME(msg); \
  714. } \
  715. \
  716. inline void Throw ## NAME(const char *msg) { \
  717. HandleScope scope; \
  718. v8::Isolate::GetCurrent()->ThrowException( \
  719. v8::Exception::NAME(New(msg).ToLocalChecked())); \
  720. } \
  721. \
  722. inline void Throw ## NAME(v8::Local<v8::String> msg) { \
  723. HandleScope scope; \
  724. v8::Isolate::GetCurrent()->ThrowException( \
  725. v8::Exception::NAME(msg)); \
  726. }
  727. X(Error)
  728. X(RangeError)
  729. X(ReferenceError)
  730. X(SyntaxError)
  731. X(TypeError)
  732. # undef X
  733. inline void ThrowError(v8::Local<v8::Value> error) {
  734. v8::Isolate::GetCurrent()->ThrowException(error);
  735. }
  736. inline MaybeLocal<v8::Object> NewBuffer(
  737. char *data
  738. , size_t length
  739. #if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
  740. , node::Buffer::FreeCallback callback
  741. #else
  742. , node::smalloc::FreeCallback callback
  743. #endif
  744. , void *hint
  745. ) {
  746. // arbitrary buffer lengths requires
  747. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  748. assert(length <= imp::kMaxLength && "too large buffer");
  749. #if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
  750. return node::Buffer::New(
  751. v8::Isolate::GetCurrent(), data, length, callback, hint);
  752. #else
  753. return node::Buffer::New(v8::Isolate::GetCurrent(), data, length, callback,
  754. hint);
  755. #endif
  756. }
  757. inline MaybeLocal<v8::Object> CopyBuffer(
  758. const char *data
  759. , uint32_t size
  760. ) {
  761. // arbitrary buffer lengths requires
  762. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  763. assert(size <= imp::kMaxLength && "too large buffer");
  764. #if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
  765. return node::Buffer::Copy(
  766. v8::Isolate::GetCurrent(), data, size);
  767. #else
  768. return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
  769. #endif
  770. }
  771. inline MaybeLocal<v8::Object> NewBuffer(uint32_t size) {
  772. // arbitrary buffer lengths requires
  773. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  774. assert(size <= imp::kMaxLength && "too large buffer");
  775. #if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
  776. return node::Buffer::New(
  777. v8::Isolate::GetCurrent(), size);
  778. #else
  779. return node::Buffer::New(v8::Isolate::GetCurrent(), size);
  780. #endif
  781. }
  782. inline MaybeLocal<v8::Object> NewBuffer(
  783. char* data
  784. , uint32_t size
  785. ) {
  786. // arbitrary buffer lengths requires
  787. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  788. assert(size <= imp::kMaxLength && "too large buffer");
  789. #if NODE_MODULE_VERSION > IOJS_2_0_MODULE_VERSION
  790. return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
  791. #else
  792. return node::Buffer::Use(v8::Isolate::GetCurrent(), data, size);
  793. #endif
  794. }
  795. #if defined(V8_MAJOR_VERSION) && (V8_MAJOR_VERSION > 4 || \
  796. (V8_MAJOR_VERSION == 4 && defined(V8_MINOR_VERSION) && V8_MINOR_VERSION >= 3))
  797. inline MaybeLocal<v8::String>
  798. NewOneByteString(const uint8_t * value, int length = -1) {
  799. return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
  800. v8::NewStringType::kNormal, length);
  801. }
  802. inline MaybeLocal<BoundScript> CompileScript(
  803. v8::Local<v8::String> s
  804. , const v8::ScriptOrigin& origin
  805. ) {
  806. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  807. v8::EscapableHandleScope scope(isolate);
  808. v8::ScriptCompiler::Source source(s, origin);
  809. return scope.Escape(
  810. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source)
  811. .FromMaybe(v8::Local<BoundScript>()));
  812. }
  813. inline MaybeLocal<BoundScript> CompileScript(
  814. v8::Local<v8::String> s
  815. ) {
  816. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  817. v8::EscapableHandleScope scope(isolate);
  818. v8::ScriptCompiler::Source source(s);
  819. return scope.Escape(
  820. v8::ScriptCompiler::Compile(isolate->GetCurrentContext(), &source)
  821. .FromMaybe(v8::Local<BoundScript>()));
  822. }
  823. inline MaybeLocal<v8::Value> RunScript(
  824. v8::Local<UnboundScript> script
  825. ) {
  826. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  827. v8::EscapableHandleScope scope(isolate);
  828. return scope.Escape(script->BindToCurrentContext()
  829. ->Run(isolate->GetCurrentContext())
  830. .FromMaybe(v8::Local<v8::Value>()));
  831. }
  832. inline MaybeLocal<v8::Value> RunScript(
  833. v8::Local<BoundScript> script
  834. ) {
  835. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  836. v8::EscapableHandleScope scope(isolate);
  837. return scope.Escape(script->Run(isolate->GetCurrentContext())
  838. .FromMaybe(v8::Local<v8::Value>()));
  839. }
  840. #else
  841. inline MaybeLocal<v8::String>
  842. NewOneByteString(const uint8_t * value, int length = -1) {
  843. return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), value,
  844. v8::String::kNormalString, length);
  845. }
  846. inline MaybeLocal<BoundScript> CompileScript(
  847. v8::Local<v8::String> s
  848. , const v8::ScriptOrigin& origin
  849. ) {
  850. v8::ScriptCompiler::Source source(s, origin);
  851. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
  852. }
  853. inline MaybeLocal<BoundScript> CompileScript(
  854. v8::Local<v8::String> s
  855. ) {
  856. v8::ScriptCompiler::Source source(s);
  857. return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
  858. }
  859. inline MaybeLocal<v8::Value> RunScript(
  860. v8::Local<UnboundScript> script
  861. ) {
  862. EscapableHandleScope scope;
  863. return scope.Escape(script->BindToCurrentContext()->Run());
  864. }
  865. inline MaybeLocal<v8::Value> RunScript(
  866. v8::Local<BoundScript> script
  867. ) {
  868. return script->Run();
  869. }
  870. #endif
  871. NAN_DEPRECATED inline v8::Local<v8::Value> MakeCallback(
  872. v8::Local<v8::Object> target
  873. , v8::Local<v8::Function> func
  874. , int argc
  875. , v8::Local<v8::Value>* argv) {
  876. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  877. EscapableHandleScope scope;
  878. return scope.Escape(New(node::MakeCallback(
  879. v8::Isolate::GetCurrent(), target, func, argc, argv)));
  880. #else
  881. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  882. AsyncResource res("nan:makeCallback");
  883. return res.runInAsyncScope(target, func, argc, argv)
  884. .FromMaybe(v8::Local<v8::Value>());
  885. # else
  886. return node::MakeCallback(
  887. v8::Isolate::GetCurrent(), target, func, argc, argv);
  888. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  889. #endif // NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  890. }
  891. NAN_DEPRECATED inline v8::Local<v8::Value> MakeCallback(
  892. v8::Local<v8::Object> target
  893. , v8::Local<v8::String> symbol
  894. , int argc
  895. , v8::Local<v8::Value>* argv) {
  896. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  897. EscapableHandleScope scope;
  898. return scope.Escape(New(node::MakeCallback(
  899. v8::Isolate::GetCurrent(), target, symbol, argc, argv)));
  900. #else
  901. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  902. AsyncResource res("nan:makeCallback");
  903. return res.runInAsyncScope(target, symbol, argc, argv)
  904. .FromMaybe(v8::Local<v8::Value>());
  905. # else
  906. return node::MakeCallback(
  907. v8::Isolate::GetCurrent(), target, symbol, argc, argv);
  908. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  909. #endif // NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  910. }
  911. NAN_DEPRECATED inline v8::Local<v8::Value> MakeCallback(
  912. v8::Local<v8::Object> target
  913. , const char* method
  914. , int argc
  915. , v8::Local<v8::Value>* argv) {
  916. #if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  917. EscapableHandleScope scope;
  918. return scope.Escape(New(node::MakeCallback(
  919. v8::Isolate::GetCurrent(), target, method, argc, argv)));
  920. #else
  921. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  922. AsyncResource res("nan:makeCallback");
  923. return res.runInAsyncScope(target, method, argc, argv)
  924. .FromMaybe(v8::Local<v8::Value>());
  925. # else
  926. return node::MakeCallback(
  927. v8::Isolate::GetCurrent(), target, method, argc, argv);
  928. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  929. #endif // NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  930. }
  931. inline void FatalException(const TryCatch& try_catch) {
  932. node::FatalException(v8::Isolate::GetCurrent(), try_catch.try_catch_);
  933. }
  934. inline v8::Local<v8::Value> ErrnoException(
  935. int errorno
  936. , const char* syscall = NULL
  937. , const char* message = NULL
  938. , const char* path = NULL) {
  939. return node::ErrnoException(v8::Isolate::GetCurrent(), errorno, syscall,
  940. message, path);
  941. }
  942. NAN_DEPRECATED inline v8::Local<v8::Value> NanErrnoException(
  943. int errorno
  944. , const char* syscall = NULL
  945. , const char* message = NULL
  946. , const char* path = NULL) {
  947. return ErrnoException(errorno, syscall, message, path);
  948. }
  949. template<typename T>
  950. inline void SetIsolateData(
  951. v8::Isolate *isolate
  952. , T *data
  953. ) {
  954. isolate->SetData(0, data);
  955. }
  956. template<typename T>
  957. inline T *GetIsolateData(
  958. v8::Isolate *isolate
  959. ) {
  960. return static_cast<T*>(isolate->GetData(0));
  961. }
  962. class Utf8String {
  963. public:
  964. inline explicit Utf8String(v8::Local<v8::Value> from) :
  965. length_(0), str_(str_st_) {
  966. HandleScope scope;
  967. if (!from.IsEmpty()) {
  968. #if NODE_MAJOR_VERSION >= 10
  969. v8::Local<v8::Context> context = GetCurrentContext();
  970. v8::Local<v8::String> string =
  971. from->ToString(context).FromMaybe(v8::Local<v8::String>());
  972. #else
  973. v8::Local<v8::String> string = from->ToString();
  974. #endif
  975. if (!string.IsEmpty()) {
  976. size_t len = 3 * string->Length() + 1;
  977. assert(len <= INT_MAX);
  978. if (len > sizeof (str_st_)) {
  979. str_ = static_cast<char*>(malloc(len));
  980. assert(str_ != 0);
  981. }
  982. const int flags =
  983. v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8;
  984. #if NODE_MAJOR_VERSION >= 11
  985. length_ = string->WriteUtf8(v8::Isolate::GetCurrent(), str_,
  986. static_cast<int>(len), 0, flags);
  987. #else
  988. // See https://github.com/nodejs/nan/issues/832.
  989. // Disable the warning as there is no way around it.
  990. #ifdef _MSC_VER
  991. #pragma warning(push)
  992. #pragma warning(disable : 4996)
  993. #endif
  994. #ifdef __GNUC__
  995. #pragma GCC diagnostic push
  996. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  997. #endif
  998. length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags);
  999. #ifdef __GNUC__
  1000. #pragma GCC diagnostic pop
  1001. #endif
  1002. #ifdef _MSC_VER
  1003. #pragma warning(pop)
  1004. #endif
  1005. #endif // NODE_MAJOR_VERSION < 11
  1006. str_[length_] = '\0';
  1007. }
  1008. }
  1009. }
  1010. inline int length() const {
  1011. return length_;
  1012. }
  1013. inline char* operator*() { return str_; }
  1014. inline const char* operator*() const { return str_; }
  1015. inline ~Utf8String() {
  1016. if (str_ != str_st_) {
  1017. free(str_);
  1018. }
  1019. }
  1020. private:
  1021. NAN_DISALLOW_ASSIGN_COPY_MOVE(Utf8String)
  1022. int length_;
  1023. char *str_;
  1024. char str_st_[1024];
  1025. };
  1026. #else // Node 0.8 and 0.10
  1027. inline v8::Local<v8::Primitive> Undefined() {
  1028. EscapableHandleScope scope;
  1029. return scope.Escape(New(v8::Undefined()));
  1030. }
  1031. inline v8::Local<v8::Primitive> Null() {
  1032. EscapableHandleScope scope;
  1033. return scope.Escape(New(v8::Null()));
  1034. }
  1035. inline v8::Local<v8::Boolean> True() {
  1036. EscapableHandleScope scope;
  1037. return scope.Escape(New(v8::True()));
  1038. }
  1039. inline v8::Local<v8::Boolean> False() {
  1040. EscapableHandleScope scope;
  1041. return scope.Escape(New(v8::False()));
  1042. }
  1043. inline v8::Local<v8::String> EmptyString() {
  1044. return v8::String::Empty();
  1045. }
  1046. inline int AdjustExternalMemory(int bc) {
  1047. return static_cast<int>(v8::V8::AdjustAmountOfExternalAllocatedMemory(bc));
  1048. }
  1049. inline void SetTemplate(
  1050. v8::Local<v8::Template> templ
  1051. , const char *name
  1052. , v8::Local<v8::Data> value) {
  1053. templ->Set(name, value);
  1054. }
  1055. inline void SetTemplate(
  1056. v8::Local<v8::Template> templ
  1057. , v8::Local<v8::String> name
  1058. , v8::Local<v8::Data> value
  1059. , v8::PropertyAttribute attributes) {
  1060. templ->Set(name, value, attributes);
  1061. }
  1062. inline v8::Local<v8::Context> GetCurrentContext() {
  1063. return v8::Context::GetCurrent();
  1064. }
  1065. inline void* GetInternalFieldPointer(
  1066. v8::Local<v8::Object> object
  1067. , int index) {
  1068. return object->GetPointerFromInternalField(index);
  1069. }
  1070. inline void SetInternalFieldPointer(
  1071. v8::Local<v8::Object> object
  1072. , int index
  1073. , void* value) {
  1074. object->SetPointerInInternalField(index, value);
  1075. }
  1076. # define NAN_GC_CALLBACK(name) \
  1077. void name(v8::GCType type, v8::GCCallbackFlags flags)
  1078. inline void AddGCEpilogueCallback(
  1079. v8::GCEpilogueCallback callback
  1080. , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  1081. v8::V8::AddGCEpilogueCallback(callback, gc_type_filter);
  1082. }
  1083. inline void RemoveGCEpilogueCallback(
  1084. v8::GCEpilogueCallback callback) {
  1085. v8::V8::RemoveGCEpilogueCallback(callback);
  1086. }
  1087. inline void AddGCPrologueCallback(
  1088. v8::GCPrologueCallback callback
  1089. , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  1090. v8::V8::AddGCPrologueCallback(callback, gc_type_filter);
  1091. }
  1092. inline void RemoveGCPrologueCallback(
  1093. v8::GCPrologueCallback callback) {
  1094. v8::V8::RemoveGCPrologueCallback(callback);
  1095. }
  1096. inline void GetHeapStatistics(
  1097. v8::HeapStatistics *heap_statistics) {
  1098. v8::V8::GetHeapStatistics(heap_statistics);
  1099. }
  1100. # define X(NAME) \
  1101. inline v8::Local<v8::Value> NAME(const char *msg) { \
  1102. EscapableHandleScope scope; \
  1103. return scope.Escape(v8::Exception::NAME(New(msg).ToLocalChecked())); \
  1104. } \
  1105. \
  1106. inline \
  1107. v8::Local<v8::Value> NAME(v8::Local<v8::String> msg) { \
  1108. return v8::Exception::NAME(msg); \
  1109. } \
  1110. \
  1111. inline void Throw ## NAME(const char *msg) { \
  1112. HandleScope scope; \
  1113. v8::ThrowException(v8::Exception::NAME(New(msg).ToLocalChecked())); \
  1114. } \
  1115. \
  1116. inline \
  1117. void Throw ## NAME(v8::Local<v8::String> errmsg) { \
  1118. HandleScope scope; \
  1119. v8::ThrowException(v8::Exception::NAME(errmsg)); \
  1120. }
  1121. X(Error)
  1122. X(RangeError)
  1123. X(ReferenceError)
  1124. X(SyntaxError)
  1125. X(TypeError)
  1126. # undef X
  1127. inline void ThrowError(v8::Local<v8::Value> error) {
  1128. v8::ThrowException(error);
  1129. }
  1130. inline MaybeLocal<v8::Object> NewBuffer(
  1131. char *data
  1132. , size_t length
  1133. , node::Buffer::free_callback callback
  1134. , void *hint
  1135. ) {
  1136. EscapableHandleScope scope;
  1137. // arbitrary buffer lengths requires
  1138. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  1139. assert(length <= imp::kMaxLength && "too large buffer");
  1140. return scope.Escape(
  1141. New(node::Buffer::New(data, length, callback, hint)->handle_));
  1142. }
  1143. inline MaybeLocal<v8::Object> CopyBuffer(
  1144. const char *data
  1145. , uint32_t size
  1146. ) {
  1147. EscapableHandleScope scope;
  1148. // arbitrary buffer lengths requires
  1149. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  1150. assert(size <= imp::kMaxLength && "too large buffer");
  1151. #if NODE_MODULE_VERSION >= NODE_0_10_MODULE_VERSION
  1152. return scope.Escape(New(node::Buffer::New(data, size)->handle_));
  1153. #else
  1154. return scope.Escape(
  1155. New(node::Buffer::New(const_cast<char *>(data), size)->handle_));
  1156. #endif
  1157. }
  1158. inline MaybeLocal<v8::Object> NewBuffer(uint32_t size) {
  1159. // arbitrary buffer lengths requires
  1160. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  1161. EscapableHandleScope scope;
  1162. assert(size <= imp::kMaxLength && "too large buffer");
  1163. return scope.Escape(New(node::Buffer::New(size)->handle_));
  1164. }
  1165. inline void FreeData(char *data, void *hint) {
  1166. (void) hint; // unused
  1167. delete[] data;
  1168. }
  1169. inline MaybeLocal<v8::Object> NewBuffer(
  1170. char* data
  1171. , uint32_t size
  1172. ) {
  1173. EscapableHandleScope scope;
  1174. // arbitrary buffer lengths requires
  1175. // NODE_MODULE_VERSION >= IOJS_3_0_MODULE_VERSION
  1176. assert(size <= imp::kMaxLength && "too large buffer");
  1177. return scope.Escape(
  1178. New(node::Buffer::New(data, size, FreeData, NULL)->handle_));
  1179. }
  1180. namespace imp {
  1181. inline void
  1182. widenString(std::vector<uint16_t> *ws, const uint8_t *s, int l) {
  1183. size_t len = static_cast<size_t>(l);
  1184. if (l < 0) {
  1185. len = strlen(reinterpret_cast<const char*>(s));
  1186. }
  1187. assert(len <= INT_MAX && "string too long");
  1188. ws->resize(len);
  1189. std::copy(s, s + len, ws->begin()); // NOLINT(build/include_what_you_use)
  1190. }
  1191. } // end of namespace imp
  1192. inline MaybeLocal<v8::String>
  1193. NewOneByteString(const uint8_t * value, int length = -1) {
  1194. std::vector<uint16_t> wideString; // NOLINT(build/include_what_you_use)
  1195. imp::widenString(&wideString, value, length);
  1196. return v8::String::New(wideString.data(),
  1197. static_cast<int>(wideString.size()));
  1198. }
  1199. inline MaybeLocal<BoundScript> CompileScript(
  1200. v8::Local<v8::String> s
  1201. , const v8::ScriptOrigin& origin
  1202. ) {
  1203. return v8::Script::Compile(s, const_cast<v8::ScriptOrigin *>(&origin));
  1204. }
  1205. inline MaybeLocal<BoundScript> CompileScript(
  1206. v8::Local<v8::String> s
  1207. ) {
  1208. return v8::Script::Compile(s);
  1209. }
  1210. inline
  1211. MaybeLocal<v8::Value> RunScript(v8::Local<v8::Script> script) {
  1212. return script->Run();
  1213. }
  1214. inline v8::Local<v8::Value> MakeCallback(
  1215. v8::Local<v8::Object> target
  1216. , v8::Local<v8::Function> func
  1217. , int argc
  1218. , v8::Local<v8::Value>* argv) {
  1219. v8::HandleScope scope;
  1220. return scope.Close(New(node::MakeCallback(target, func, argc, argv)));
  1221. }
  1222. inline v8::Local<v8::Value> MakeCallback(
  1223. v8::Local<v8::Object> target
  1224. , v8::Local<v8::String> symbol
  1225. , int argc
  1226. , v8::Local<v8::Value>* argv) {
  1227. v8::HandleScope scope;
  1228. return scope.Close(New(node::MakeCallback(target, symbol, argc, argv)));
  1229. }
  1230. inline v8::Local<v8::Value> MakeCallback(
  1231. v8::Local<v8::Object> target
  1232. , const char* method
  1233. , int argc
  1234. , v8::Local<v8::Value>* argv) {
  1235. v8::HandleScope scope;
  1236. return scope.Close(New(node::MakeCallback(target, method, argc, argv)));
  1237. }
  1238. inline void FatalException(const TryCatch& try_catch) {
  1239. node::FatalException(const_cast<v8::TryCatch &>(try_catch.try_catch_));
  1240. }
  1241. inline v8::Local<v8::Value> ErrnoException(
  1242. int errorno
  1243. , const char* syscall = NULL
  1244. , const char* message = NULL
  1245. , const char* path = NULL) {
  1246. return node::ErrnoException(errorno, syscall, message, path);
  1247. }
  1248. NAN_DEPRECATED inline v8::Local<v8::Value> NanErrnoException(
  1249. int errorno
  1250. , const char* syscall = NULL
  1251. , const char* message = NULL
  1252. , const char* path = NULL) {
  1253. return ErrnoException(errorno, syscall, message, path);
  1254. }
  1255. template<typename T>
  1256. inline void SetIsolateData(
  1257. v8::Isolate *isolate
  1258. , T *data
  1259. ) {
  1260. isolate->SetData(data);
  1261. }
  1262. template<typename T>
  1263. inline T *GetIsolateData(
  1264. v8::Isolate *isolate
  1265. ) {
  1266. return static_cast<T*>(isolate->GetData());
  1267. }
  1268. class Utf8String {
  1269. public:
  1270. inline explicit Utf8String(v8::Local<v8::Value> from) :
  1271. length_(0), str_(str_st_) {
  1272. v8::HandleScope scope;
  1273. if (!from.IsEmpty()) {
  1274. v8::Local<v8::String> string = from->ToString();
  1275. if (!string.IsEmpty()) {
  1276. size_t len = 3 * string->Length() + 1;
  1277. assert(len <= INT_MAX);
  1278. if (len > sizeof (str_st_)) {
  1279. str_ = static_cast<char*>(malloc(len));
  1280. assert(str_ != 0);
  1281. }
  1282. const int flags =
  1283. v8::String::NO_NULL_TERMINATION | imp::kReplaceInvalidUtf8;
  1284. length_ = string->WriteUtf8(str_, static_cast<int>(len), 0, flags);
  1285. str_[length_] = '\0';
  1286. }
  1287. }
  1288. }
  1289. inline int length() const {
  1290. return length_;
  1291. }
  1292. inline char* operator*() { return str_; }
  1293. inline const char* operator*() const { return str_; }
  1294. inline ~Utf8String() {
  1295. if (str_ != str_st_) {
  1296. free(str_);
  1297. }
  1298. }
  1299. private:
  1300. NAN_DISALLOW_ASSIGN_COPY_MOVE(Utf8String)
  1301. int length_;
  1302. char *str_;
  1303. char str_st_[1024];
  1304. };
  1305. #endif // NODE_MODULE_VERSION
  1306. typedef void (*FreeCallback)(char *data, void *hint);
  1307. typedef const FunctionCallbackInfo<v8::Value>& NAN_METHOD_ARGS_TYPE;
  1308. typedef void NAN_METHOD_RETURN_TYPE;
  1309. typedef const PropertyCallbackInfo<v8::Value>& NAN_GETTER_ARGS_TYPE;
  1310. typedef void NAN_GETTER_RETURN_TYPE;
  1311. typedef const PropertyCallbackInfo<void>& NAN_SETTER_ARGS_TYPE;
  1312. typedef void NAN_SETTER_RETURN_TYPE;
  1313. typedef const PropertyCallbackInfo<v8::Value>&
  1314. NAN_PROPERTY_GETTER_ARGS_TYPE;
  1315. typedef void NAN_PROPERTY_GETTER_RETURN_TYPE;
  1316. typedef const PropertyCallbackInfo<v8::Value>&
  1317. NAN_PROPERTY_SETTER_ARGS_TYPE;
  1318. typedef void NAN_PROPERTY_SETTER_RETURN_TYPE;
  1319. typedef const PropertyCallbackInfo<v8::Array>&
  1320. NAN_PROPERTY_ENUMERATOR_ARGS_TYPE;
  1321. typedef void NAN_PROPERTY_ENUMERATOR_RETURN_TYPE;
  1322. typedef const PropertyCallbackInfo<v8::Boolean>&
  1323. NAN_PROPERTY_DELETER_ARGS_TYPE;
  1324. typedef void NAN_PROPERTY_DELETER_RETURN_TYPE;
  1325. typedef const PropertyCallbackInfo<v8::Integer>&
  1326. NAN_PROPERTY_QUERY_ARGS_TYPE;
  1327. typedef void NAN_PROPERTY_QUERY_RETURN_TYPE;
  1328. typedef const PropertyCallbackInfo<v8::Value>& NAN_INDEX_GETTER_ARGS_TYPE;
  1329. typedef void NAN_INDEX_GETTER_RETURN_TYPE;
  1330. typedef const PropertyCallbackInfo<v8::Value>& NAN_INDEX_SETTER_ARGS_TYPE;
  1331. typedef void NAN_INDEX_SETTER_RETURN_TYPE;
  1332. typedef const PropertyCallbackInfo<v8::Array>&
  1333. NAN_INDEX_ENUMERATOR_ARGS_TYPE;
  1334. typedef void NAN_INDEX_ENUMERATOR_RETURN_TYPE;
  1335. typedef const PropertyCallbackInfo<v8::Boolean>&
  1336. NAN_INDEX_DELETER_ARGS_TYPE;
  1337. typedef void NAN_INDEX_DELETER_RETURN_TYPE;
  1338. typedef const PropertyCallbackInfo<v8::Integer>&
  1339. NAN_INDEX_QUERY_ARGS_TYPE;
  1340. typedef void NAN_INDEX_QUERY_RETURN_TYPE;
  1341. #define NAN_METHOD(name) \
  1342. Nan::NAN_METHOD_RETURN_TYPE name(Nan::NAN_METHOD_ARGS_TYPE info)
  1343. #define NAN_GETTER(name) \
  1344. Nan::NAN_GETTER_RETURN_TYPE name( \
  1345. v8::Local<v8::String> property \
  1346. , Nan::NAN_GETTER_ARGS_TYPE info)
  1347. #define NAN_SETTER(name) \
  1348. Nan::NAN_SETTER_RETURN_TYPE name( \
  1349. v8::Local<v8::String> property \
  1350. , v8::Local<v8::Value> value \
  1351. , Nan::NAN_SETTER_ARGS_TYPE info)
  1352. #define NAN_PROPERTY_GETTER(name) \
  1353. Nan::NAN_PROPERTY_GETTER_RETURN_TYPE name( \
  1354. v8::Local<v8::String> property \
  1355. , Nan::NAN_PROPERTY_GETTER_ARGS_TYPE info)
  1356. #define NAN_PROPERTY_SETTER(name) \
  1357. Nan::NAN_PROPERTY_SETTER_RETURN_TYPE name( \
  1358. v8::Local<v8::String> property \
  1359. , v8::Local<v8::Value> value \
  1360. , Nan::NAN_PROPERTY_SETTER_ARGS_TYPE info)
  1361. #define NAN_PROPERTY_ENUMERATOR(name) \
  1362. Nan::NAN_PROPERTY_ENUMERATOR_RETURN_TYPE name( \
  1363. Nan::NAN_PROPERTY_ENUMERATOR_ARGS_TYPE info)
  1364. #define NAN_PROPERTY_DELETER(name) \
  1365. Nan::NAN_PROPERTY_DELETER_RETURN_TYPE name( \
  1366. v8::Local<v8::String> property \
  1367. , Nan::NAN_PROPERTY_DELETER_ARGS_TYPE info)
  1368. #define NAN_PROPERTY_QUERY(name) \
  1369. Nan::NAN_PROPERTY_QUERY_RETURN_TYPE name( \
  1370. v8::Local<v8::String> property \
  1371. , Nan::NAN_PROPERTY_QUERY_ARGS_TYPE info)
  1372. # define NAN_INDEX_GETTER(name) \
  1373. Nan::NAN_INDEX_GETTER_RETURN_TYPE name( \
  1374. uint32_t index \
  1375. , Nan::NAN_INDEX_GETTER_ARGS_TYPE info)
  1376. #define NAN_INDEX_SETTER(name) \
  1377. Nan::NAN_INDEX_SETTER_RETURN_TYPE name( \
  1378. uint32_t index \
  1379. , v8::Local<v8::Value> value \
  1380. , Nan::NAN_INDEX_SETTER_ARGS_TYPE info)
  1381. #define NAN_INDEX_ENUMERATOR(name) \
  1382. Nan::NAN_INDEX_ENUMERATOR_RETURN_TYPE \
  1383. name(Nan::NAN_INDEX_ENUMERATOR_ARGS_TYPE info)
  1384. #define NAN_INDEX_DELETER(name) \
  1385. Nan::NAN_INDEX_DELETER_RETURN_TYPE name( \
  1386. uint32_t index \
  1387. , Nan::NAN_INDEX_DELETER_ARGS_TYPE info)
  1388. #define NAN_INDEX_QUERY(name) \
  1389. Nan::NAN_INDEX_QUERY_RETURN_TYPE name( \
  1390. uint32_t index \
  1391. , Nan::NAN_INDEX_QUERY_ARGS_TYPE info)
  1392. class Callback {
  1393. public:
  1394. Callback() {}
  1395. explicit Callback(const v8::Local<v8::Function> &fn) : handle_(fn) {}
  1396. ~Callback() {
  1397. handle_.Reset();
  1398. }
  1399. bool operator==(const Callback &other) const {
  1400. return handle_ == other.handle_;
  1401. }
  1402. bool operator!=(const Callback &other) const {
  1403. return !operator==(other);
  1404. }
  1405. inline
  1406. v8::Local<v8::Function> operator*() const { return GetFunction(); }
  1407. NAN_DEPRECATED inline v8::Local<v8::Value> operator()(
  1408. v8::Local<v8::Object> target
  1409. , int argc = 0
  1410. , v8::Local<v8::Value> argv[] = 0) const {
  1411. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1412. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1413. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1414. AsyncResource async("nan:Callback:operator()");
  1415. return Call_(isolate, target, argc, argv, &async)
  1416. .FromMaybe(v8::Local<v8::Value>());
  1417. # else
  1418. return Call_(isolate, target, argc, argv);
  1419. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1420. #else
  1421. return Call_(target, argc, argv);
  1422. #endif // NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1423. }
  1424. NAN_DEPRECATED inline v8::Local<v8::Value> operator()(
  1425. int argc = 0
  1426. , v8::Local<v8::Value> argv[] = 0) const {
  1427. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1428. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1429. v8::EscapableHandleScope scope(isolate);
  1430. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1431. AsyncResource async("nan:Callback:operator()");
  1432. return scope.Escape(Call_(isolate, isolate->GetCurrentContext()->Global(),
  1433. argc, argv, &async)
  1434. .FromMaybe(v8::Local<v8::Value>()));
  1435. # else
  1436. return scope.Escape(
  1437. Call_(isolate, isolate->GetCurrentContext()->Global(), argc, argv));
  1438. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1439. #else
  1440. v8::HandleScope scope;
  1441. return scope.Close(Call_(v8::Context::GetCurrent()->Global(), argc, argv));
  1442. #endif // NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1443. }
  1444. inline MaybeLocal<v8::Value> operator()(
  1445. AsyncResource* resource
  1446. , int argc = 0
  1447. , v8::Local<v8::Value> argv[] = 0) const {
  1448. return this->Call(argc, argv, resource);
  1449. }
  1450. inline MaybeLocal<v8::Value> operator()(
  1451. AsyncResource* resource
  1452. , v8::Local<v8::Object> target
  1453. , int argc = 0
  1454. , v8::Local<v8::Value> argv[] = 0) const {
  1455. return this->Call(target, argc, argv, resource);
  1456. }
  1457. // TODO(kkoopa): remove
  1458. inline void SetFunction(const v8::Local<v8::Function> &fn) {
  1459. Reset(fn);
  1460. }
  1461. inline void Reset(const v8::Local<v8::Function> &fn) {
  1462. handle_.Reset(fn);
  1463. }
  1464. inline void Reset() {
  1465. handle_.Reset();
  1466. }
  1467. inline v8::Local<v8::Function> GetFunction() const {
  1468. return New(handle_);
  1469. }
  1470. inline bool IsEmpty() const {
  1471. return handle_.IsEmpty();
  1472. }
  1473. // Deprecated: For async callbacks Use the versions that accept an
  1474. // AsyncResource. If this callback does not correspond to an async resource,
  1475. // that is, it is a synchronous function call on a non-empty JS stack, you
  1476. // should Nan::Call instead.
  1477. NAN_DEPRECATED inline v8::Local<v8::Value>
  1478. Call(v8::Local<v8::Object> target
  1479. , int argc
  1480. , v8::Local<v8::Value> argv[]) const {
  1481. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1482. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1483. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1484. AsyncResource async("nan:Callback:Call");
  1485. return Call_(isolate, target, argc, argv, &async)
  1486. .FromMaybe(v8::Local<v8::Value>());
  1487. # else
  1488. return Call_(isolate, target, argc, argv);
  1489. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1490. #else
  1491. return Call_(target, argc, argv);
  1492. #endif
  1493. }
  1494. // Deprecated: For async callbacks Use the versions that accept an
  1495. // AsyncResource. If this callback does not correspond to an async resource,
  1496. // that is, it is a synchronous function call on a non-empty JS stack, you
  1497. // should Nan::Call instead.
  1498. NAN_DEPRECATED inline v8::Local<v8::Value>
  1499. Call(int argc, v8::Local<v8::Value> argv[]) const {
  1500. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1501. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1502. v8::EscapableHandleScope scope(isolate);
  1503. # if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1504. AsyncResource async("nan:Callback:Call");
  1505. return scope.Escape(Call_(isolate, isolate->GetCurrentContext()->Global(),
  1506. argc, argv, &async)
  1507. .FromMaybe(v8::Local<v8::Value>()));
  1508. # else
  1509. return scope.Escape(
  1510. Call_(isolate, isolate->GetCurrentContext()->Global(), argc, argv));
  1511. # endif // NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1512. #else
  1513. v8::HandleScope scope;
  1514. return scope.Close(Call_(v8::Context::GetCurrent()->Global(), argc, argv));
  1515. #endif
  1516. }
  1517. inline MaybeLocal<v8::Value>
  1518. Call(v8::Local<v8::Object> target
  1519. , int argc
  1520. , v8::Local<v8::Value> argv[]
  1521. , AsyncResource* resource) const {
  1522. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1523. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  1524. return Call_(isolate, target, argc, argv, resource);
  1525. #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1526. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1527. return Call_(isolate, target, argc, argv);
  1528. #else
  1529. return Call_(target, argc, argv);
  1530. #endif
  1531. }
  1532. inline MaybeLocal<v8::Value>
  1533. Call(int argc, v8::Local<v8::Value> argv[], AsyncResource* resource) const {
  1534. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1535. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  1536. return Call(isolate->GetCurrentContext()->Global(), argc, argv, resource);
  1537. #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1538. v8::Isolate *isolate = v8::Isolate::GetCurrent();
  1539. v8::EscapableHandleScope scope(isolate);
  1540. return scope.Escape(
  1541. Call_(isolate, isolate->GetCurrentContext()->Global(), argc, argv));
  1542. #else
  1543. v8::HandleScope scope;
  1544. return scope.Close(Call_(v8::Context::GetCurrent()->Global(), argc, argv));
  1545. #endif
  1546. }
  1547. private:
  1548. NAN_DISALLOW_ASSIGN_COPY_MOVE(Callback)
  1549. Persistent<v8::Function> handle_;
  1550. #if NODE_MODULE_VERSION >= NODE_9_0_MODULE_VERSION
  1551. MaybeLocal<v8::Value> Call_(v8::Isolate *isolate
  1552. , v8::Local<v8::Object> target
  1553. , int argc
  1554. , v8::Local<v8::Value> argv[]
  1555. , AsyncResource* resource) const {
  1556. EscapableHandleScope scope;
  1557. v8::Local<v8::Function> func = New(handle_);
  1558. auto maybe = resource->runInAsyncScope(target, func, argc, argv);
  1559. v8::Local<v8::Value> local;
  1560. if (!maybe.ToLocal(&local)) return MaybeLocal<v8::Value>();
  1561. return scope.Escape(local);
  1562. }
  1563. #elif NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1564. v8::Local<v8::Value> Call_(v8::Isolate *isolate
  1565. , v8::Local<v8::Object> target
  1566. , int argc
  1567. , v8::Local<v8::Value> argv[]) const {
  1568. EscapableHandleScope scope;
  1569. v8::Local<v8::Function> callback = New(handle_);
  1570. # if NODE_MODULE_VERSION < IOJS_3_0_MODULE_VERSION
  1571. return scope.Escape(New(node::MakeCallback(
  1572. isolate
  1573. , target
  1574. , callback
  1575. , argc
  1576. , argv
  1577. )));
  1578. # else
  1579. return scope.Escape(node::MakeCallback(
  1580. isolate
  1581. , target
  1582. , callback
  1583. , argc
  1584. , argv
  1585. ));
  1586. # endif
  1587. }
  1588. #else
  1589. v8::Local<v8::Value> Call_(v8::Local<v8::Object> target
  1590. , int argc
  1591. , v8::Local<v8::Value> argv[]) const {
  1592. EscapableHandleScope scope;
  1593. v8::Local<v8::Function> callback = New(handle_);
  1594. return scope.Escape(New(node::MakeCallback(
  1595. target
  1596. , callback
  1597. , argc
  1598. , argv
  1599. )));
  1600. }
  1601. #endif
  1602. };
  1603. inline MaybeLocal<v8::Value> Call(
  1604. const Nan::Callback& callback
  1605. , v8::Local<v8::Object> recv
  1606. , int argc
  1607. , v8::Local<v8::Value> argv[]) {
  1608. return Call(*callback, recv, argc, argv);
  1609. }
  1610. inline MaybeLocal<v8::Value> Call(
  1611. const Nan::Callback& callback
  1612. , int argc
  1613. , v8::Local<v8::Value> argv[]) {
  1614. #if NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION
  1615. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  1616. v8::EscapableHandleScope scope(isolate);
  1617. return scope.Escape(
  1618. Call(*callback, isolate->GetCurrentContext()->Global(), argc, argv)
  1619. .FromMaybe(v8::Local<v8::Value>()));
  1620. #else
  1621. EscapableHandleScope scope;
  1622. return scope.Escape(
  1623. Call(*callback, v8::Context::GetCurrent()->Global(), argc, argv)
  1624. .FromMaybe(v8::Local<v8::Value>()));
  1625. #endif
  1626. }
  1627. inline MaybeLocal<v8::Value> Call(
  1628. v8::Local<v8::String> symbol
  1629. , v8::Local<v8::Object> recv
  1630. , int argc
  1631. , v8::Local<v8::Value> argv[]) {
  1632. EscapableHandleScope scope;
  1633. v8::Local<v8::Value> fn_v =
  1634. Get(recv, symbol).FromMaybe(v8::Local<v8::Value>());
  1635. if (fn_v.IsEmpty() || !fn_v->IsFunction()) return v8::Local<v8::Value>();
  1636. v8::Local<v8::Function> fn = fn_v.As<v8::Function>();
  1637. return scope.Escape(
  1638. Call(fn, recv, argc, argv).FromMaybe(v8::Local<v8::Value>()));
  1639. }
  1640. inline MaybeLocal<v8::Value> Call(
  1641. const char* method
  1642. , v8::Local<v8::Object> recv
  1643. , int argc
  1644. , v8::Local<v8::Value> argv[]) {
  1645. EscapableHandleScope scope;
  1646. v8::Local<v8::String> method_string =
  1647. New<v8::String>(method).ToLocalChecked();
  1648. return scope.Escape(
  1649. Call(method_string, recv, argc, argv).FromMaybe(v8::Local<v8::Value>()));
  1650. }
  1651. /* abstract */ class AsyncWorker {
  1652. public:
  1653. explicit AsyncWorker(Callback *callback_,
  1654. const char* resource_name = "nan:AsyncWorker")
  1655. : callback(callback_), errmsg_(NULL) {
  1656. request.data = this;
  1657. HandleScope scope;
  1658. v8::Local<v8::Object> obj = New<v8::Object>();
  1659. persistentHandle.Reset(obj);
  1660. async_resource = new AsyncResource(resource_name, obj);
  1661. }
  1662. virtual ~AsyncWorker() {
  1663. HandleScope scope;
  1664. if (!persistentHandle.IsEmpty())
  1665. persistentHandle.Reset();
  1666. delete callback;
  1667. delete[] errmsg_;
  1668. delete async_resource;
  1669. }
  1670. virtual void WorkComplete() {
  1671. HandleScope scope;
  1672. if (errmsg_ == NULL)
  1673. HandleOKCallback();
  1674. else
  1675. HandleErrorCallback();
  1676. delete callback;
  1677. callback = NULL;
  1678. }
  1679. inline void SaveToPersistent(
  1680. const char *key, const v8::Local<v8::Value> &value) {
  1681. HandleScope scope;
  1682. Set(New(persistentHandle), New(key).ToLocalChecked(), value).FromJust();
  1683. }
  1684. inline void SaveToPersistent(
  1685. const v8::Local<v8::String> &key, const v8::Local<v8::Value> &value) {
  1686. HandleScope scope;
  1687. Set(New(persistentHandle), key, value).FromJust();
  1688. }
  1689. inline void SaveToPersistent(
  1690. uint32_t index, const v8::Local<v8::Value> &value) {
  1691. HandleScope scope;
  1692. Set(New(persistentHandle), index, value).FromJust();
  1693. }
  1694. inline v8::Local<v8::Value> GetFromPersistent(const char *key) const {
  1695. EscapableHandleScope scope;
  1696. return scope.Escape(
  1697. Get(New(persistentHandle), New(key).ToLocalChecked())
  1698. .FromMaybe(v8::Local<v8::Value>()));
  1699. }
  1700. inline v8::Local<v8::Value>
  1701. GetFromPersistent(const v8::Local<v8::String> &key) const {
  1702. EscapableHandleScope scope;
  1703. return scope.Escape(
  1704. Get(New(persistentHandle), key)
  1705. .FromMaybe(v8::Local<v8::Value>()));
  1706. }
  1707. inline v8::Local<v8::Value> GetFromPersistent(uint32_t index) const {
  1708. EscapableHandleScope scope;
  1709. return scope.Escape(
  1710. Get(New(persistentHandle), index)
  1711. .FromMaybe(v8::Local<v8::Value>()));
  1712. }
  1713. virtual void Execute() = 0;
  1714. uv_work_t request;
  1715. virtual void Destroy() {
  1716. delete this;
  1717. }
  1718. protected:
  1719. Persistent<v8::Object> persistentHandle;
  1720. Callback *callback;
  1721. AsyncResource *async_resource;
  1722. virtual void HandleOKCallback() {
  1723. HandleScope scope;
  1724. callback->Call(0, NULL, async_resource);
  1725. }
  1726. virtual void HandleErrorCallback() {
  1727. HandleScope scope;
  1728. v8::Local<v8::Value> argv[] = {
  1729. v8::Exception::Error(New<v8::String>(ErrorMessage()).ToLocalChecked())
  1730. };
  1731. callback->Call(1, argv, async_resource);
  1732. }
  1733. void SetErrorMessage(const char *msg) {
  1734. delete[] errmsg_;
  1735. size_t size = strlen(msg) + 1;
  1736. errmsg_ = new char[size];
  1737. memcpy(errmsg_, msg, size);
  1738. }
  1739. const char* ErrorMessage() const {
  1740. return errmsg_;
  1741. }
  1742. private:
  1743. NAN_DISALLOW_ASSIGN_COPY_MOVE(AsyncWorker)
  1744. char *errmsg_;
  1745. };
  1746. /* abstract */ class AsyncBareProgressWorkerBase : public AsyncWorker {
  1747. public:
  1748. explicit AsyncBareProgressWorkerBase(
  1749. Callback *callback_,
  1750. const char* resource_name = "nan:AsyncBareProgressWorkerBase")
  1751. : AsyncWorker(callback_, resource_name) {
  1752. uv_async_init(
  1753. GetCurrentEventLoop()
  1754. , &async
  1755. , AsyncProgress_
  1756. );
  1757. async.data = this;
  1758. }
  1759. virtual ~AsyncBareProgressWorkerBase() {
  1760. }
  1761. virtual void WorkProgress() = 0;
  1762. virtual void Destroy() {
  1763. uv_close(reinterpret_cast<uv_handle_t*>(&async), AsyncClose_);
  1764. }
  1765. private:
  1766. inline static NAUV_WORK_CB(AsyncProgress_) {
  1767. AsyncBareProgressWorkerBase *worker =
  1768. static_cast<AsyncBareProgressWorkerBase*>(async->data);
  1769. worker->WorkProgress();
  1770. }
  1771. inline static void AsyncClose_(uv_handle_t* handle) {
  1772. AsyncBareProgressWorkerBase *worker =
  1773. static_cast<AsyncBareProgressWorkerBase*>(handle->data);
  1774. delete worker;
  1775. }
  1776. protected:
  1777. uv_async_t async;
  1778. };
  1779. template<class T>
  1780. /* abstract */
  1781. class AsyncBareProgressWorker : public AsyncBareProgressWorkerBase {
  1782. public:
  1783. explicit AsyncBareProgressWorker(
  1784. Callback *callback_,
  1785. const char* resource_name = "nan:AsyncBareProgressWorker")
  1786. : AsyncBareProgressWorkerBase(callback_, resource_name) {
  1787. uv_mutex_init(&async_lock);
  1788. }
  1789. virtual ~AsyncBareProgressWorker() {
  1790. uv_mutex_destroy(&async_lock);
  1791. }
  1792. class ExecutionProgress {
  1793. friend class AsyncBareProgressWorker;
  1794. public:
  1795. void Signal() const {
  1796. uv_mutex_lock(&that_->async_lock);
  1797. uv_async_send(&that_->async);
  1798. uv_mutex_unlock(&that_->async_lock);
  1799. }
  1800. void Send(const T* data, size_t count) const {
  1801. that_->SendProgress_(data, count);
  1802. }
  1803. private:
  1804. explicit ExecutionProgress(AsyncBareProgressWorker *that) : that_(that) {}
  1805. NAN_DISALLOW_ASSIGN_COPY_MOVE(ExecutionProgress)
  1806. AsyncBareProgressWorker* const that_;
  1807. };
  1808. virtual void Execute(const ExecutionProgress& progress) = 0;
  1809. virtual void HandleProgressCallback(const T *data, size_t size) = 0;
  1810. protected:
  1811. uv_mutex_t async_lock;
  1812. private:
  1813. void Execute() /*final override*/ {
  1814. ExecutionProgress progress(this);
  1815. Execute(progress);
  1816. }
  1817. virtual void SendProgress_(const T *data, size_t count) = 0;
  1818. };
  1819. template<class T>
  1820. /* abstract */
  1821. class AsyncProgressWorkerBase : public AsyncBareProgressWorker<T> {
  1822. public:
  1823. explicit AsyncProgressWorkerBase(
  1824. Callback *callback_,
  1825. const char* resource_name = "nan:AsyncProgressWorkerBase")
  1826. : AsyncBareProgressWorker<T>(callback_, resource_name), asyncdata_(NULL),
  1827. asyncsize_(0) {
  1828. }
  1829. virtual ~AsyncProgressWorkerBase() {
  1830. delete[] asyncdata_;
  1831. }
  1832. void WorkProgress() {
  1833. uv_mutex_lock(&this->async_lock);
  1834. T *data = asyncdata_;
  1835. size_t size = asyncsize_;
  1836. asyncdata_ = NULL;
  1837. asyncsize_ = 0;
  1838. uv_mutex_unlock(&this->async_lock);
  1839. // Don't send progress events after we've already completed.
  1840. if (this->callback) {
  1841. this->HandleProgressCallback(data, size);
  1842. }
  1843. delete[] data;
  1844. }
  1845. private:
  1846. void SendProgress_(const T *data, size_t count) {
  1847. T *new_data = new T[count];
  1848. std::copy(data, data + count, new_data);
  1849. uv_mutex_lock(&this->async_lock);
  1850. T *old_data = asyncdata_;
  1851. asyncdata_ = new_data;
  1852. asyncsize_ = count;
  1853. uv_async_send(&this->async);
  1854. uv_mutex_unlock(&this->async_lock);
  1855. delete[] old_data;
  1856. }
  1857. T *asyncdata_;
  1858. size_t asyncsize_;
  1859. };
  1860. // This ensures compatibility to the previous un-templated AsyncProgressWorker
  1861. // class definition.
  1862. typedef AsyncProgressWorkerBase<char> AsyncProgressWorker;
  1863. template<class T>
  1864. /* abstract */
  1865. class AsyncBareProgressQueueWorker : public AsyncBareProgressWorkerBase {
  1866. public:
  1867. explicit AsyncBareProgressQueueWorker(
  1868. Callback *callback_,
  1869. const char* resource_name = "nan:AsyncBareProgressQueueWorker")
  1870. : AsyncBareProgressWorkerBase(callback_, resource_name) {
  1871. }
  1872. virtual ~AsyncBareProgressQueueWorker() {
  1873. }
  1874. class ExecutionProgress {
  1875. friend class AsyncBareProgressQueueWorker;
  1876. public:
  1877. void Send(const T* data, size_t count) const {
  1878. that_->SendProgress_(data, count);
  1879. }
  1880. private:
  1881. explicit ExecutionProgress(AsyncBareProgressQueueWorker *that)
  1882. : that_(that) {}
  1883. NAN_DISALLOW_ASSIGN_COPY_MOVE(ExecutionProgress)
  1884. AsyncBareProgressQueueWorker* const that_;
  1885. };
  1886. virtual void Execute(const ExecutionProgress& progress) = 0;
  1887. virtual void HandleProgressCallback(const T *data, size_t size) = 0;
  1888. private:
  1889. void Execute() /*final override*/ {
  1890. ExecutionProgress progress(this);
  1891. Execute(progress);
  1892. }
  1893. virtual void SendProgress_(const T *data, size_t count) = 0;
  1894. };
  1895. template<class T>
  1896. /* abstract */
  1897. class AsyncProgressQueueWorker : public AsyncBareProgressQueueWorker<T> {
  1898. public:
  1899. explicit AsyncProgressQueueWorker(
  1900. Callback *callback_,
  1901. const char* resource_name = "nan:AsyncProgressQueueWorker")
  1902. : AsyncBareProgressQueueWorker<T>(callback_) {
  1903. uv_mutex_init(&async_lock);
  1904. }
  1905. virtual ~AsyncProgressQueueWorker() {
  1906. uv_mutex_lock(&async_lock);
  1907. while (!asyncdata_.empty()) {
  1908. std::pair<T*, size_t> &datapair = asyncdata_.front();
  1909. T *data = datapair.first;
  1910. asyncdata_.pop();
  1911. delete[] data;
  1912. }
  1913. uv_mutex_unlock(&async_lock);
  1914. uv_mutex_destroy(&async_lock);
  1915. }
  1916. void WorkComplete() {
  1917. WorkProgress();
  1918. AsyncWorker::WorkComplete();
  1919. }
  1920. void WorkProgress() {
  1921. uv_mutex_lock(&async_lock);
  1922. while (!asyncdata_.empty()) {
  1923. std::pair<T*, size_t> &datapair = asyncdata_.front();
  1924. T *data = datapair.first;
  1925. size_t size = datapair.second;
  1926. asyncdata_.pop();
  1927. uv_mutex_unlock(&async_lock);
  1928. // Don't send progress events after we've already completed.
  1929. if (this->callback) {
  1930. this->HandleProgressCallback(data, size);
  1931. }
  1932. delete[] data;
  1933. uv_mutex_lock(&async_lock);
  1934. }
  1935. uv_mutex_unlock(&async_lock);
  1936. }
  1937. private:
  1938. void SendProgress_(const T *data, size_t count) {
  1939. T *new_data = new T[count];
  1940. std::copy(data, data + count, new_data);
  1941. uv_mutex_lock(&async_lock);
  1942. asyncdata_.push(std::pair<T*, size_t>(new_data, count));
  1943. uv_mutex_unlock(&async_lock);
  1944. uv_async_send(&this->async);
  1945. }
  1946. uv_mutex_t async_lock;
  1947. std::queue<std::pair<T*, size_t> > asyncdata_;
  1948. };
  1949. inline void AsyncExecute (uv_work_t* req) {
  1950. AsyncWorker *worker = static_cast<AsyncWorker*>(req->data);
  1951. worker->Execute();
  1952. }
  1953. /* uv_after_work_cb has 1 argument before node-v0.9.4 and
  1954. * 2 arguments since node-v0.9.4
  1955. * https://github.com/libuv/libuv/commit/92fb84b751e18f032c02609467f44bfe927b80c5
  1956. */
  1957. inline void AsyncExecuteComplete(uv_work_t *req) {
  1958. AsyncWorker* worker = static_cast<AsyncWorker*>(req->data);
  1959. worker->WorkComplete();
  1960. worker->Destroy();
  1961. }
  1962. inline void AsyncExecuteComplete (uv_work_t* req, int status) {
  1963. AsyncExecuteComplete(req);
  1964. }
  1965. inline void AsyncQueueWorker (AsyncWorker* worker) {
  1966. uv_queue_work(
  1967. GetCurrentEventLoop()
  1968. , &worker->request
  1969. , AsyncExecute
  1970. , AsyncExecuteComplete
  1971. );
  1972. }
  1973. namespace imp {
  1974. inline
  1975. ExternalOneByteStringResource const*
  1976. GetExternalResource(v8::Local<v8::String> str) {
  1977. #if NODE_MODULE_VERSION < ATOM_0_21_MODULE_VERSION
  1978. return str->GetExternalAsciiStringResource();
  1979. #else
  1980. return str->GetExternalOneByteStringResource();
  1981. #endif
  1982. }
  1983. inline
  1984. bool
  1985. IsExternal(v8::Local<v8::String> str) {
  1986. #if NODE_MODULE_VERSION < ATOM_0_21_MODULE_VERSION
  1987. return str->IsExternalAscii();
  1988. #else
  1989. return str->IsExternalOneByte();
  1990. #endif
  1991. }
  1992. } // end of namespace imp
  1993. enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
  1994. #if NODE_MODULE_VERSION < NODE_0_10_MODULE_VERSION
  1995. # include "nan_string_bytes.h" // NOLINT(build/include)
  1996. #endif
  1997. inline v8::Local<v8::Value> Encode(
  1998. const void *buf, size_t len, enum Encoding encoding = BINARY) {
  1999. #if (NODE_MODULE_VERSION >= ATOM_0_21_MODULE_VERSION)
  2000. v8::Isolate* isolate = v8::Isolate::GetCurrent();
  2001. node::encoding node_enc = static_cast<node::encoding>(encoding);
  2002. if (encoding == UCS2) {
  2003. return node::Encode(
  2004. isolate
  2005. , reinterpret_cast<const uint16_t *>(buf)
  2006. , len / 2);
  2007. } else {
  2008. return node::Encode(
  2009. isolate
  2010. , reinterpret_cast<const char *>(buf)
  2011. , len
  2012. , node_enc);
  2013. }
  2014. #elif (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
  2015. return node::Encode(
  2016. v8::Isolate::GetCurrent()
  2017. , buf, len
  2018. , static_cast<node::encoding>(encoding));
  2019. #else
  2020. # if NODE_MODULE_VERSION >= NODE_0_10_MODULE_VERSION
  2021. return node::Encode(buf, len, static_cast<node::encoding>(encoding));
  2022. # else
  2023. return imp::Encode(reinterpret_cast<const char*>(buf), len, encoding);
  2024. # endif
  2025. #endif
  2026. }
  2027. inline ssize_t DecodeBytes(
  2028. v8::Local<v8::Value> val, enum Encoding encoding = BINARY) {
  2029. #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
  2030. return node::DecodeBytes(
  2031. v8::Isolate::GetCurrent()
  2032. , val
  2033. , static_cast<node::encoding>(encoding));
  2034. #else
  2035. # if (NODE_MODULE_VERSION < NODE_0_10_MODULE_VERSION)
  2036. if (encoding == BUFFER) {
  2037. return node::DecodeBytes(val, node::BINARY);
  2038. }
  2039. # endif
  2040. return node::DecodeBytes(val, static_cast<node::encoding>(encoding));
  2041. #endif
  2042. }
  2043. inline ssize_t DecodeWrite(
  2044. char *buf
  2045. , size_t len
  2046. , v8::Local<v8::Value> val
  2047. , enum Encoding encoding = BINARY) {
  2048. #if (NODE_MODULE_VERSION > NODE_0_10_MODULE_VERSION)
  2049. return node::DecodeWrite(
  2050. v8::Isolate::GetCurrent()
  2051. , buf
  2052. , len
  2053. , val
  2054. , static_cast<node::encoding>(encoding));
  2055. #else
  2056. # if (NODE_MODULE_VERSION < NODE_0_10_MODULE_VERSION)
  2057. if (encoding == BUFFER) {
  2058. return node::DecodeWrite(buf, len, val, node::BINARY);
  2059. }
  2060. # endif
  2061. return node::DecodeWrite(
  2062. buf
  2063. , len
  2064. , val
  2065. , static_cast<node::encoding>(encoding));
  2066. #endif
  2067. }
  2068. inline void SetPrototypeTemplate(
  2069. v8::Local<v8::FunctionTemplate> templ
  2070. , const char *name
  2071. , v8::Local<v8::Data> value
  2072. ) {
  2073. HandleScope scope;
  2074. SetTemplate(templ->PrototypeTemplate(), name, value);
  2075. }
  2076. inline void SetPrototypeTemplate(
  2077. v8::Local<v8::FunctionTemplate> templ
  2078. , v8::Local<v8::String> name
  2079. , v8::Local<v8::Data> value
  2080. , v8::PropertyAttribute attributes
  2081. ) {
  2082. HandleScope scope;
  2083. SetTemplate(templ->PrototypeTemplate(), name, value, attributes);
  2084. }
  2085. inline void SetInstanceTemplate(
  2086. v8::Local<v8::FunctionTemplate> templ
  2087. , const char *name
  2088. , v8::Local<v8::Data> value
  2089. ) {
  2090. HandleScope scope;
  2091. SetTemplate(templ->InstanceTemplate(), name, value);
  2092. }
  2093. inline void SetInstanceTemplate(
  2094. v8::Local<v8::FunctionTemplate> templ
  2095. , v8::Local<v8::String> name
  2096. , v8::Local<v8::Data> value
  2097. , v8::PropertyAttribute attributes
  2098. ) {
  2099. HandleScope scope;
  2100. SetTemplate(templ->InstanceTemplate(), name, value, attributes);
  2101. }
  2102. namespace imp {
  2103. // Note(@agnat): Helper to distinguish different receiver types. The first
  2104. // version deals with receivers derived from v8::Template. The second version
  2105. // handles everything else. The final argument only serves as discriminator and
  2106. // is unused.
  2107. template <typename T>
  2108. inline
  2109. void
  2110. SetMethodAux(T recv,
  2111. v8::Local<v8::String> name,
  2112. v8::Local<v8::FunctionTemplate> tpl,
  2113. v8::Template *) {
  2114. recv->Set(name, tpl);
  2115. }
  2116. template <typename T>
  2117. inline
  2118. void
  2119. SetMethodAux(T recv,
  2120. v8::Local<v8::String> name,
  2121. v8::Local<v8::FunctionTemplate> tpl,
  2122. ...) {
  2123. Set(recv, name, GetFunction(tpl).ToLocalChecked());
  2124. }
  2125. } // end of namespace imp
  2126. template <typename T, template <typename> class HandleType>
  2127. inline void SetMethod(
  2128. HandleType<T> recv
  2129. , const char *name
  2130. , FunctionCallback callback
  2131. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2132. HandleScope scope;
  2133. v8::Local<v8::FunctionTemplate> t = New<v8::FunctionTemplate>(callback, data);
  2134. v8::Local<v8::String> fn_name = New(name).ToLocalChecked();
  2135. t->SetClassName(fn_name);
  2136. // Note(@agnat): Pass an empty T* as discriminator. See note on
  2137. // SetMethodAux(...) above
  2138. imp::SetMethodAux(recv, fn_name, t, static_cast<T*>(0));
  2139. }
  2140. inline void SetPrototypeMethod(
  2141. v8::Local<v8::FunctionTemplate> recv
  2142. , const char* name
  2143. , FunctionCallback callback
  2144. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2145. HandleScope scope;
  2146. v8::Local<v8::FunctionTemplate> t = New<v8::FunctionTemplate>(
  2147. callback
  2148. , data
  2149. , New<v8::Signature>(recv));
  2150. v8::Local<v8::String> fn_name = New(name).ToLocalChecked();
  2151. recv->PrototypeTemplate()->Set(fn_name, t);
  2152. t->SetClassName(fn_name);
  2153. }
  2154. //=== Accessors and Such =======================================================
  2155. inline void SetAccessor(
  2156. v8::Local<v8::ObjectTemplate> tpl
  2157. , v8::Local<v8::String> name
  2158. , GetterCallback getter
  2159. , SetterCallback setter = 0
  2160. , v8::Local<v8::Value> data = v8::Local<v8::Value>()
  2161. , v8::AccessControl settings = v8::DEFAULT
  2162. , v8::PropertyAttribute attribute = v8::None
  2163. , imp::Sig signature = imp::Sig()) {
  2164. HandleScope scope;
  2165. imp::NativeGetter getter_ =
  2166. imp::GetterCallbackWrapper;
  2167. imp::NativeSetter setter_ =
  2168. setter ? imp::SetterCallbackWrapper : 0;
  2169. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2170. otpl->SetInternalFieldCount(imp::kAccessorFieldCount);
  2171. v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
  2172. obj->SetInternalField(
  2173. imp::kGetterIndex
  2174. , New<v8::External>(reinterpret_cast<void *>(getter)));
  2175. if (setter != 0) {
  2176. obj->SetInternalField(
  2177. imp::kSetterIndex
  2178. , New<v8::External>(reinterpret_cast<void *>(setter)));
  2179. }
  2180. if (!data.IsEmpty()) {
  2181. obj->SetInternalField(imp::kDataIndex, data);
  2182. }
  2183. tpl->SetAccessor(
  2184. name
  2185. , getter_
  2186. , setter_
  2187. , obj
  2188. , settings
  2189. , attribute
  2190. , signature);
  2191. }
  2192. inline bool SetAccessor(
  2193. v8::Local<v8::Object> obj
  2194. , v8::Local<v8::String> name
  2195. , GetterCallback getter
  2196. , SetterCallback setter = 0
  2197. , v8::Local<v8::Value> data = v8::Local<v8::Value>()
  2198. , v8::AccessControl settings = v8::DEFAULT
  2199. , v8::PropertyAttribute attribute = v8::None) {
  2200. HandleScope scope;
  2201. imp::NativeGetter getter_ =
  2202. imp::GetterCallbackWrapper;
  2203. imp::NativeSetter setter_ =
  2204. setter ? imp::SetterCallbackWrapper : 0;
  2205. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2206. otpl->SetInternalFieldCount(imp::kAccessorFieldCount);
  2207. v8::Local<v8::Object> dataobj = NewInstance(otpl).ToLocalChecked();
  2208. dataobj->SetInternalField(
  2209. imp::kGetterIndex
  2210. , New<v8::External>(reinterpret_cast<void *>(getter)));
  2211. if (!data.IsEmpty()) {
  2212. dataobj->SetInternalField(imp::kDataIndex, data);
  2213. }
  2214. if (setter) {
  2215. dataobj->SetInternalField(
  2216. imp::kSetterIndex
  2217. , New<v8::External>(reinterpret_cast<void *>(setter)));
  2218. }
  2219. #if (NODE_MODULE_VERSION >= NODE_6_0_MODULE_VERSION)
  2220. return obj->SetAccessor(
  2221. GetCurrentContext()
  2222. , name
  2223. , getter_
  2224. , setter_
  2225. , dataobj
  2226. , settings
  2227. , attribute).FromMaybe(false);
  2228. #else
  2229. return obj->SetAccessor(
  2230. name
  2231. , getter_
  2232. , setter_
  2233. , dataobj
  2234. , settings
  2235. , attribute);
  2236. #endif
  2237. }
  2238. inline void SetNamedPropertyHandler(
  2239. v8::Local<v8::ObjectTemplate> tpl
  2240. , PropertyGetterCallback getter
  2241. , PropertySetterCallback setter = 0
  2242. , PropertyQueryCallback query = 0
  2243. , PropertyDeleterCallback deleter = 0
  2244. , PropertyEnumeratorCallback enumerator = 0
  2245. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2246. HandleScope scope;
  2247. imp::NativePropertyGetter getter_ =
  2248. imp::PropertyGetterCallbackWrapper;
  2249. imp::NativePropertySetter setter_ =
  2250. setter ? imp::PropertySetterCallbackWrapper : 0;
  2251. imp::NativePropertyQuery query_ =
  2252. query ? imp::PropertyQueryCallbackWrapper : 0;
  2253. imp::NativePropertyDeleter *deleter_ =
  2254. deleter ? imp::PropertyDeleterCallbackWrapper : 0;
  2255. imp::NativePropertyEnumerator enumerator_ =
  2256. enumerator ? imp::PropertyEnumeratorCallbackWrapper : 0;
  2257. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2258. otpl->SetInternalFieldCount(imp::kPropertyFieldCount);
  2259. v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
  2260. obj->SetInternalField(
  2261. imp::kPropertyGetterIndex
  2262. , New<v8::External>(reinterpret_cast<void *>(getter)));
  2263. if (setter) {
  2264. obj->SetInternalField(
  2265. imp::kPropertySetterIndex
  2266. , New<v8::External>(reinterpret_cast<void *>(setter)));
  2267. }
  2268. if (query) {
  2269. obj->SetInternalField(
  2270. imp::kPropertyQueryIndex
  2271. , New<v8::External>(reinterpret_cast<void *>(query)));
  2272. }
  2273. if (deleter) {
  2274. obj->SetInternalField(
  2275. imp::kPropertyDeleterIndex
  2276. , New<v8::External>(reinterpret_cast<void *>(deleter)));
  2277. }
  2278. if (enumerator) {
  2279. obj->SetInternalField(
  2280. imp::kPropertyEnumeratorIndex
  2281. , New<v8::External>(reinterpret_cast<void *>(enumerator)));
  2282. }
  2283. if (!data.IsEmpty()) {
  2284. obj->SetInternalField(imp::kDataIndex, data);
  2285. }
  2286. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  2287. tpl->SetHandler(v8::NamedPropertyHandlerConfiguration(
  2288. getter_, setter_, query_, deleter_, enumerator_, obj));
  2289. #else
  2290. tpl->SetNamedPropertyHandler(
  2291. getter_
  2292. , setter_
  2293. , query_
  2294. , deleter_
  2295. , enumerator_
  2296. , obj);
  2297. #endif
  2298. }
  2299. inline void SetIndexedPropertyHandler(
  2300. v8::Local<v8::ObjectTemplate> tpl
  2301. , IndexGetterCallback getter
  2302. , IndexSetterCallback setter = 0
  2303. , IndexQueryCallback query = 0
  2304. , IndexDeleterCallback deleter = 0
  2305. , IndexEnumeratorCallback enumerator = 0
  2306. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2307. HandleScope scope;
  2308. imp::NativeIndexGetter getter_ =
  2309. imp::IndexGetterCallbackWrapper;
  2310. imp::NativeIndexSetter setter_ =
  2311. setter ? imp::IndexSetterCallbackWrapper : 0;
  2312. imp::NativeIndexQuery query_ =
  2313. query ? imp::IndexQueryCallbackWrapper : 0;
  2314. imp::NativeIndexDeleter deleter_ =
  2315. deleter ? imp::IndexDeleterCallbackWrapper : 0;
  2316. imp::NativeIndexEnumerator enumerator_ =
  2317. enumerator ? imp::IndexEnumeratorCallbackWrapper : 0;
  2318. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2319. otpl->SetInternalFieldCount(imp::kIndexPropertyFieldCount);
  2320. v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
  2321. obj->SetInternalField(
  2322. imp::kIndexPropertyGetterIndex
  2323. , New<v8::External>(reinterpret_cast<void *>(getter)));
  2324. if (setter) {
  2325. obj->SetInternalField(
  2326. imp::kIndexPropertySetterIndex
  2327. , New<v8::External>(reinterpret_cast<void *>(setter)));
  2328. }
  2329. if (query) {
  2330. obj->SetInternalField(
  2331. imp::kIndexPropertyQueryIndex
  2332. , New<v8::External>(reinterpret_cast<void *>(query)));
  2333. }
  2334. if (deleter) {
  2335. obj->SetInternalField(
  2336. imp::kIndexPropertyDeleterIndex
  2337. , New<v8::External>(reinterpret_cast<void *>(deleter)));
  2338. }
  2339. if (enumerator) {
  2340. obj->SetInternalField(
  2341. imp::kIndexPropertyEnumeratorIndex
  2342. , New<v8::External>(reinterpret_cast<void *>(enumerator)));
  2343. }
  2344. if (!data.IsEmpty()) {
  2345. obj->SetInternalField(imp::kDataIndex, data);
  2346. }
  2347. #if NODE_MODULE_VERSION > NODE_0_12_MODULE_VERSION
  2348. tpl->SetHandler(v8::IndexedPropertyHandlerConfiguration(
  2349. getter_, setter_, query_, deleter_, enumerator_, obj));
  2350. #else
  2351. tpl->SetIndexedPropertyHandler(
  2352. getter_
  2353. , setter_
  2354. , query_
  2355. , deleter_
  2356. , enumerator_
  2357. , obj);
  2358. #endif
  2359. }
  2360. inline void SetCallHandler(
  2361. v8::Local<v8::FunctionTemplate> tpl
  2362. , FunctionCallback callback
  2363. , v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2364. HandleScope scope;
  2365. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2366. otpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  2367. v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
  2368. obj->SetInternalField(
  2369. imp::kFunctionIndex
  2370. , New<v8::External>(reinterpret_cast<void *>(callback)));
  2371. if (!data.IsEmpty()) {
  2372. obj->SetInternalField(imp::kDataIndex, data);
  2373. }
  2374. tpl->SetCallHandler(imp::FunctionCallbackWrapper, obj);
  2375. }
  2376. inline void SetCallAsFunctionHandler(
  2377. v8::Local<v8::ObjectTemplate> tpl,
  2378. FunctionCallback callback,
  2379. v8::Local<v8::Value> data = v8::Local<v8::Value>()) {
  2380. HandleScope scope;
  2381. v8::Local<v8::ObjectTemplate> otpl = New<v8::ObjectTemplate>();
  2382. otpl->SetInternalFieldCount(imp::kFunctionFieldCount);
  2383. v8::Local<v8::Object> obj = NewInstance(otpl).ToLocalChecked();
  2384. obj->SetInternalField(
  2385. imp::kFunctionIndex
  2386. , New<v8::External>(reinterpret_cast<void *>(callback)));
  2387. if (!data.IsEmpty()) {
  2388. obj->SetInternalField(imp::kDataIndex, data);
  2389. }
  2390. tpl->SetCallAsFunctionHandler(imp::FunctionCallbackWrapper, obj);
  2391. }
  2392. //=== Weak Persistent Handling =================================================
  2393. #include "nan_weak.h" // NOLINT(build/include)
  2394. //=== ObjectWrap ===============================================================
  2395. #include "nan_object_wrap.h" // NOLINT(build/include)
  2396. //=== HiddenValue/Private ======================================================
  2397. #include "nan_private.h" // NOLINT(build/include)
  2398. //=== Export ==================================================================
  2399. inline
  2400. void
  2401. Export(ADDON_REGISTER_FUNCTION_ARGS_TYPE target, const char *name,
  2402. FunctionCallback f) {
  2403. HandleScope scope;
  2404. Set(target, New<v8::String>(name).ToLocalChecked(),
  2405. GetFunction(New<v8::FunctionTemplate>(f)).ToLocalChecked());
  2406. }
  2407. //=== Tap Reverse Binding =====================================================
  2408. struct Tap {
  2409. explicit Tap(v8::Local<v8::Value> t) : t_() {
  2410. HandleScope scope;
  2411. t_.Reset(To<v8::Object>(t).ToLocalChecked());
  2412. }
  2413. ~Tap() { t_.Reset(); } // not sure if necessary
  2414. inline void plan(int i) {
  2415. HandleScope scope;
  2416. v8::Local<v8::Value> arg = New(i);
  2417. Call("plan", New(t_), 1, &arg);
  2418. }
  2419. inline void ok(bool isOk, const char *msg = NULL) {
  2420. HandleScope scope;
  2421. v8::Local<v8::Value> args[2];
  2422. args[0] = New(isOk);
  2423. if (msg) args[1] = New(msg).ToLocalChecked();
  2424. Call("ok", New(t_), msg ? 2 : 1, args);
  2425. }
  2426. inline void pass(const char * msg = NULL) {
  2427. HandleScope scope;
  2428. v8::Local<v8::Value> hmsg;
  2429. if (msg) hmsg = New(msg).ToLocalChecked();
  2430. Call("pass", New(t_), msg ? 1 : 0, &hmsg);
  2431. }
  2432. inline void end() {
  2433. HandleScope scope;
  2434. Call("end", New(t_), 0, NULL);
  2435. }
  2436. private:
  2437. Persistent<v8::Object> t_;
  2438. };
  2439. #define NAN_STRINGIZE2(x) #x
  2440. #define NAN_STRINGIZE(x) NAN_STRINGIZE2(x)
  2441. #define NAN_TEST_EXPRESSION(expression) \
  2442. ( expression ), __FILE__ ":" NAN_STRINGIZE(__LINE__) ": " #expression
  2443. #define NAN_EXPORT(target, function) Export(target, #function, function)
  2444. #undef TYPE_CHECK
  2445. //=== Generic Maybefication ===================================================
  2446. namespace imp {
  2447. template <typename T> struct Maybefier;
  2448. template <typename T> struct Maybefier<v8::Local<T> > {
  2449. inline static MaybeLocal<T> convert(v8::Local<T> v) {
  2450. return v;
  2451. }
  2452. };
  2453. template <typename T> struct Maybefier<MaybeLocal<T> > {
  2454. inline static MaybeLocal<T> convert(MaybeLocal<T> v) {
  2455. return v;
  2456. }
  2457. };
  2458. } // end of namespace imp
  2459. template <typename T, template <typename> class MaybeMaybe>
  2460. inline MaybeLocal<T>
  2461. MakeMaybe(MaybeMaybe<T> v) {
  2462. return imp::Maybefier<MaybeMaybe<T> >::convert(v);
  2463. }
  2464. //=== TypedArrayContents =======================================================
  2465. #include "nan_typedarray_contents.h" // NOLINT(build/include)
  2466. //=== JSON =====================================================================
  2467. #include "nan_json.h" // NOLINT(build/include)
  2468. //=== ScriptOrigin =============================================================
  2469. #include "nan_scriptorigin.h" // NOLINT(build/include)
  2470. } // end of namespace Nan
  2471. #endif // NAN_H_