nan.h 86 KB

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