nan.h 86 KB

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