UI for Zipcoin Blue

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