2457 lines
73 KiB

  1. --- a/package.json
  2. +++ b/package.json
  3. @@ -14,9 +14,6 @@
  4. "type": "git",
  5. "url": "git://github.com/hanshuebner/node-hid.git"
  6. },
  7. - "scripts": {
  8. - "prepublish": "git submodule update --init"
  9. - },
  10. "main": "./index.js",
  11. "engines": {
  12. "node": ">=0.8.0"
  13. --- a/src/wscript
  14. +++ b/src/wscript
  15. @@ -3,10 +3,8 @@
  16. import sys;
  17. import os;
  18. -hidapi_home='../hidapi'
  19. cflags=["-g", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-Wall", "-fPIC" ]
  20. -includes=[ hidapi_home + "/hidapi" ]
  21. def set_options(opt):
  22. opt.tool_options("compiler_cxx")
  23. @@ -16,24 +14,10 @@
  24. conf.check_tool("compiler_cxx")
  25. conf.check_tool("node_addon")
  26. conf.check_tool("compiler_cc")
  27. - if sys.platform == 'darwin':
  28. - conf.env.append_value('LINKFLAGS', ['Release/libhidapi.a', '-framework', 'IOKit', '-framework', 'CoreFoundation'])
  29. - else:
  30. - conf.env.append_value('LINKFLAGS', ['Release/libhidapi.a', '-ludev' ])
  31. -
  32. -def build(bld):
  33. - bld.add_group("hidapi")
  34. - hidapi = bld.new_task_gen("cc", "staticlib")
  35. - hidapi.includes = includes
  36. - hidapi.cflags = cflags
  37. - if sys.platform == 'darwin':
  38. - hidapi.source = "../hidapi/mac/hid.c"
  39. - else:
  40. - hidapi.source = "../hidapi/linux/hid.c"
  41. - hidapi.target = "hidapi"
  42. +def build(bld):
  43. bld.add_group("adapter")
  44. - adapter = bld.new_task_gen("cxx", "shlib", "node_addon", use = ['hidapi'])
  45. + adapter = bld.new_task_gen("cxx", "shlib", "node_addon")
  46. adapter.includes = includes
  47. adapter.cxxflags = cflags
  48. adapter.target = "HID"
  49. --- a/binding.gyp
  50. +++ b/binding.gyp
  51. @@ -4,61 +4,8 @@
  52. },
  53. 'targets': [
  54. {
  55. - 'target_name': 'hidapi',
  56. - 'type': 'static_library',
  57. - 'conditions': [
  58. - [ 'OS=="mac"', {
  59. - 'sources': [ 'hidapi/mac/hid.c' ],
  60. - 'include_dirs+': [
  61. - '/usr/include/libusb-1.0/'
  62. - ]
  63. - }],
  64. - [ 'OS=="linux"', {
  65. - 'conditions': [
  66. - [ 'driver=="libusb"', {
  67. - 'sources': [ 'hidapi/libusb/hid.c' ],
  68. - 'include_dirs+': [
  69. - '/usr/include/libusb-1.0/'
  70. - ]
  71. - }],
  72. - [ 'driver=="hidraw"', {
  73. - 'sources': [ 'hidapi/linux/hid.c' ]
  74. - }]
  75. - ]
  76. - }],
  77. - [ 'OS=="win"', {
  78. - 'sources': [ 'hidapi/windows/hid.c' ],
  79. - 'msvs_settings': {
  80. - 'VCLinkerTool': {
  81. - 'AdditionalDependencies': [
  82. - 'setupapi.lib',
  83. - ]
  84. - }
  85. - }
  86. - }]
  87. - ],
  88. - 'direct_dependent_settings': {
  89. - 'include_dirs': [
  90. - 'hidapi/hidapi',
  91. - "<!(node -e \"require('nan')\")"
  92. - ]
  93. - },
  94. - 'include_dirs': [
  95. - 'hidapi/hidapi'
  96. - ],
  97. - 'defines': [
  98. - '_LARGEFILE_SOURCE',
  99. - '_FILE_OFFSET_BITS=64',
  100. - ],
  101. - 'cflags': ['-g'],
  102. - 'cflags!': [
  103. - '-ansi'
  104. - ]
  105. - },
  106. - {
  107. 'target_name': 'HID',
  108. 'sources': [ 'src/HID.cc' ],
  109. - 'dependencies': ['hidapi'],
  110. 'defines': [
  111. '_LARGEFILE_SOURCE',
  112. '_FILE_OFFSET_BITS=64',
  113. @@ -108,4 +55,4 @@
  114. 'cflags_cc': ['-g', '-exceptions']
  115. }
  116. ]
  117. -}
  118. \ No newline at end of file
  119. +}
  120. --- /dev/null
  121. +++ b/src/nan.h
  122. @@ -0,0 +1,2331 @@
  123. +/**********************************************************************************
  124. + * NAN - Native Abstractions for Node.js
  125. + *
  126. + * Copyright (c) 2014 NAN contributors:
  127. + * - Rod Vagg <https://github.com/rvagg>
  128. + * - Benjamin Byholm <https://github.com/kkoopa>
  129. + * - Trevor Norris <https://github.com/trevnorris>
  130. + * - Nathan Rajlich <https://github.com/TooTallNate>
  131. + * - Brett Lawson <https://github.com/brett19>
  132. + * - Ben Noordhuis <https://github.com/bnoordhuis>
  133. + *
  134. + * MIT +no-false-attribs License <https://github.com/rvagg/nan/blob/master/LICENSE>
  135. + *
  136. + * Version 1.3.0: current Node unstable: 0.11.13, Node stable: 0.10.30
  137. + *
  138. + * See https://github.com/rvagg/nan for the latest update to this file
  139. + **********************************************************************************/
  140. +
  141. +#ifndef NAN_H_
  142. +#define NAN_H_
  143. +
  144. +#include <uv.h>
  145. +#include <node.h>
  146. +#include <node_buffer.h>
  147. +#include <node_version.h>
  148. +#include <node_object_wrap.h>
  149. +#include <string.h>
  150. +#include <limits.h>
  151. +#include <string>
  152. +
  153. +#if defined(__GNUC__) && !defined(DEBUG)
  154. +# define NAN_INLINE inline __attribute__((always_inline))
  155. +#elif defined(_MSC_VER) && !defined(DEBUG)
  156. +# define NAN_INLINE __forceinline
  157. +#else
  158. +# define NAN_INLINE inline
  159. +#endif
  160. +
  161. +#if defined(__GNUC__) && !V8_DISABLE_DEPRECATIONS
  162. +# define NAN_DEPRECATED __attribute__((deprecated))
  163. +#elif defined(_MSC_VER) && !V8_DISABLE_DEPRECATIONS
  164. +# define NAN_DEPRECATED __declspec(deprecated)
  165. +#else
  166. +# define NAN_DEPRECATED
  167. +#endif
  168. +
  169. +// some generic helpers
  170. +
  171. +template<typename T> NAN_INLINE bool NanSetPointerSafe(
  172. + T *var
  173. + , T val
  174. +) {
  175. + if (var) {
  176. + *var = val;
  177. + return true;
  178. + } else {
  179. + return false;
  180. + }
  181. +}
  182. +
  183. +template<typename T> NAN_INLINE T NanGetPointerSafe(
  184. + T *var
  185. + , T fallback = reinterpret_cast<T>(0)
  186. +) {
  187. + if (var) {
  188. + return *var;
  189. + } else {
  190. + return fallback;
  191. + }
  192. +}
  193. +
  194. +NAN_INLINE bool NanBooleanOptionValue(
  195. + v8::Local<v8::Object> optionsObj
  196. + , v8::Handle<v8::String> opt, bool def
  197. +) {
  198. + if (def) {
  199. + return optionsObj.IsEmpty()
  200. + || !optionsObj->Has(opt)
  201. + || optionsObj->Get(opt)->BooleanValue();
  202. + } else {
  203. + return !optionsObj.IsEmpty()
  204. + && optionsObj->Has(opt)
  205. + && optionsObj->Get(opt)->BooleanValue();
  206. + }
  207. +}
  208. +
  209. +NAN_INLINE bool NanBooleanOptionValue(
  210. + v8::Local<v8::Object> optionsObj
  211. + , v8::Handle<v8::String> opt
  212. +) {
  213. + return NanBooleanOptionValue(optionsObj, opt, false);
  214. +}
  215. +
  216. +NAN_INLINE uint32_t NanUInt32OptionValue(
  217. + v8::Local<v8::Object> optionsObj
  218. + , v8::Handle<v8::String> opt
  219. + , uint32_t def
  220. +) {
  221. + return !optionsObj.IsEmpty()
  222. + && optionsObj->Has(opt)
  223. + && optionsObj->Get(opt)->IsNumber()
  224. + ? optionsObj->Get(opt)->Uint32Value()
  225. + : def;
  226. +}
  227. +
  228. +#if (NODE_MODULE_VERSION > 0x000B)
  229. +// Node 0.11+ (0.11.3 and below won't compile with these)
  230. +
  231. +# define _NAN_METHOD_ARGS_TYPE const v8::FunctionCallbackInfo<v8::Value>&
  232. +# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args
  233. +# define _NAN_METHOD_RETURN_TYPE void
  234. +
  235. +# define _NAN_GETTER_ARGS_TYPE const v8::PropertyCallbackInfo<v8::Value>&
  236. +# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args
  237. +# define _NAN_GETTER_RETURN_TYPE void
  238. +
  239. +# define _NAN_SETTER_ARGS_TYPE const v8::PropertyCallbackInfo<void>&
  240. +# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args
  241. +# define _NAN_SETTER_RETURN_TYPE void
  242. +
  243. +# define _NAN_PROPERTY_GETTER_ARGS_TYPE \
  244. + const v8::PropertyCallbackInfo<v8::Value>&
  245. +# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args
  246. +# define _NAN_PROPERTY_GETTER_RETURN_TYPE void
  247. +
  248. +# define _NAN_PROPERTY_SETTER_ARGS_TYPE \
  249. + const v8::PropertyCallbackInfo<v8::Value>&
  250. +# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args
  251. +# define _NAN_PROPERTY_SETTER_RETURN_TYPE void
  252. +
  253. +# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE \
  254. + const v8::PropertyCallbackInfo<v8::Array>&
  255. +# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args
  256. +# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE void
  257. +
  258. +# define _NAN_PROPERTY_DELETER_ARGS_TYPE \
  259. + const v8::PropertyCallbackInfo<v8::Boolean>&
  260. +# define _NAN_PROPERTY_DELETER_ARGS \
  261. + _NAN_PROPERTY_DELETER_ARGS_TYPE args
  262. +# define _NAN_PROPERTY_DELETER_RETURN_TYPE void
  263. +
  264. +# define _NAN_PROPERTY_QUERY_ARGS_TYPE \
  265. + const v8::PropertyCallbackInfo<v8::Integer>&
  266. +# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args
  267. +# define _NAN_PROPERTY_QUERY_RETURN_TYPE void
  268. +
  269. +# define _NAN_INDEX_GETTER_ARGS_TYPE \
  270. + const v8::PropertyCallbackInfo<v8::Value>&
  271. +# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args
  272. +# define _NAN_INDEX_GETTER_RETURN_TYPE void
  273. +
  274. +# define _NAN_INDEX_SETTER_ARGS_TYPE \
  275. + const v8::PropertyCallbackInfo<v8::Value>&
  276. +# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args
  277. +# define _NAN_INDEX_SETTER_RETURN_TYPE void
  278. +
  279. +# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE \
  280. + const v8::PropertyCallbackInfo<v8::Array>&
  281. +# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args
  282. +# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE void
  283. +
  284. +# define _NAN_INDEX_DELETER_ARGS_TYPE \
  285. + const v8::PropertyCallbackInfo<v8::Boolean>&
  286. +# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args
  287. +# define _NAN_INDEX_DELETER_RETURN_TYPE void
  288. +
  289. +# define _NAN_INDEX_QUERY_ARGS_TYPE \
  290. + const v8::PropertyCallbackInfo<v8::Integer>&
  291. +# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
  292. +# define _NAN_INDEX_QUERY_RETURN_TYPE void
  293. +
  294. + typedef v8::FunctionCallback NanFunctionCallback;
  295. +
  296. + template<typename T>
  297. + NAN_INLINE v8::Local<T> NanNew() {
  298. + return T::New(v8::Isolate::GetCurrent());
  299. + }
  300. +
  301. + template<typename T, typename P>
  302. + NAN_INLINE v8::Local<T> NanNew(P arg1) {
  303. + return T::New(v8::Isolate::GetCurrent(), arg1);
  304. + }
  305. +
  306. + template<typename T>
  307. + NAN_INLINE v8::Local<v8::Signature> NanNew(
  308. + v8::Handle<v8::FunctionTemplate> receiver
  309. + , int argc
  310. + , v8::Handle<v8::FunctionTemplate> argv[] = 0) {
  311. + return v8::Signature::New(v8::Isolate::GetCurrent(), receiver, argc, argv);
  312. + }
  313. +
  314. + template<typename T>
  315. + NAN_INLINE v8::Local<v8::FunctionTemplate> NanNew(
  316. + NanFunctionCallback callback
  317. + , v8::Handle<v8::Value> data = v8::Handle<v8::Value>()
  318. + , v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>()) {
  319. + return T::New(v8::Isolate::GetCurrent(), callback, data, signature);
  320. + }
  321. +
  322. + template<typename T>
  323. + NAN_INLINE v8::Local<T> NanNew(v8::Handle<T> arg1) {
  324. + return v8::Local<T>::New(v8::Isolate::GetCurrent(), arg1);
  325. + }
  326. +
  327. + template<typename T>
  328. + NAN_INLINE v8::Local<T> NanNew(const v8::Persistent<T> &arg1) {
  329. + return v8::Local<T>::New(v8::Isolate::GetCurrent(), arg1);
  330. + }
  331. +
  332. + template<typename T, typename P>
  333. + NAN_INLINE v8::Local<T> NanNew(P arg1, int arg2) {
  334. + return T::New(v8::Isolate::GetCurrent(), arg1, arg2);
  335. + }
  336. +
  337. + template<>
  338. + NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>() {
  339. + return v8::Array::New(v8::Isolate::GetCurrent());
  340. + }
  341. +
  342. + template<>
  343. + NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>(int length) {
  344. + return v8::Array::New(v8::Isolate::GetCurrent(), length);
  345. + }
  346. +
  347. + template<>
  348. + NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(double time) {
  349. + return v8::Date::New(v8::Isolate::GetCurrent(), time).As<v8::Date>();
  350. + }
  351. +
  352. + template<>
  353. + NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(int time) {
  354. + return v8::Date::New(v8::Isolate::GetCurrent(), time).As<v8::Date>();
  355. + }
  356. +
  357. + typedef v8::UnboundScript NanUnboundScript;
  358. + typedef v8::Script NanBoundScript;
  359. +
  360. + template<typename T, typename P>
  361. + NAN_INLINE v8::Local<T> NanNew(
  362. + P s
  363. + , const v8::ScriptOrigin& origin
  364. + ) {
  365. + v8::ScriptCompiler::Source source(s, origin);
  366. + return v8::ScriptCompiler::CompileUnbound(
  367. + v8::Isolate::GetCurrent(), &source);
  368. + }
  369. +
  370. + template<>
  371. + NAN_INLINE v8::Local<NanUnboundScript> NanNew<NanUnboundScript>(
  372. + v8::Local<v8::String> s
  373. + ) {
  374. + v8::ScriptCompiler::Source source(s);
  375. + return v8::ScriptCompiler::CompileUnbound(
  376. + v8::Isolate::GetCurrent(), &source);
  377. + }
  378. +
  379. + template<>
  380. + NAN_INLINE v8::Local<v8::BooleanObject> NanNew(bool value) {
  381. + return v8::BooleanObject::New(value).As<v8::BooleanObject>();
  382. + }
  383. +
  384. + template<>
  385. + NAN_INLINE v8::Local<v8::StringObject>
  386. + NanNew<v8::StringObject, v8::Local<v8::String> >(
  387. + v8::Local<v8::String> value) {
  388. + return v8::StringObject::New(value).As<v8::StringObject>();
  389. + }
  390. +
  391. + template<>
  392. + NAN_INLINE v8::Local<v8::StringObject>
  393. + NanNew<v8::StringObject, v8::Handle<v8::String> >(
  394. + v8::Handle<v8::String> value) {
  395. + return v8::StringObject::New(value).As<v8::StringObject>();
  396. + }
  397. +
  398. + template<>
  399. + NAN_INLINE v8::Local<v8::NumberObject> NanNew<v8::NumberObject>(double val) {
  400. + return v8::NumberObject::New(
  401. + v8::Isolate::GetCurrent(), val).As<v8::NumberObject>();
  402. + }
  403. +
  404. + template<typename T>
  405. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  406. + v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
  407. + return v8::RegExp::New(pattern, flags);
  408. + }
  409. +
  410. + template<typename T>
  411. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  412. + v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  413. + return v8::RegExp::New(pattern, flags);
  414. + }
  415. +
  416. + template<typename T, typename P>
  417. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  418. + v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
  419. + return v8::RegExp::New(pattern, flags);
  420. + }
  421. +
  422. + template<typename T, typename P>
  423. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  424. + v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  425. + return v8::RegExp::New(pattern, flags);
  426. + }
  427. +
  428. + template<>
  429. + NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, int32_t>(int32_t val) {
  430. + return v8::Uint32::NewFromUnsigned(
  431. + v8::Isolate::GetCurrent(), val)->ToUint32();
  432. + }
  433. +
  434. + template<>
  435. + NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, uint32_t>(uint32_t val) {
  436. + return v8::Uint32::NewFromUnsigned(
  437. + v8::Isolate::GetCurrent(), val)->ToUint32();
  438. + }
  439. +
  440. + template<>
  441. + NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, int32_t>(int32_t val) {
  442. + return v8::Int32::New(v8::Isolate::GetCurrent(), val)->ToInt32();
  443. + }
  444. +
  445. + template<>
  446. + NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, uint32_t>(uint32_t val) {
  447. + return v8::Int32::New(v8::Isolate::GetCurrent(), val)->ToInt32();
  448. + }
  449. +
  450. + template<>
  451. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, char *>(
  452. + char *arg
  453. + , int length) {
  454. + return v8::String::NewFromUtf8(
  455. + v8::Isolate::GetCurrent()
  456. + , arg
  457. + , v8::String::kNormalString
  458. + , length);
  459. + }
  460. +
  461. + template<>
  462. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const char *>(
  463. + const char *arg
  464. + , int length) {
  465. + return v8::String::NewFromUtf8(
  466. + v8::Isolate::GetCurrent()
  467. + , arg
  468. + , v8::String::kNormalString
  469. + , length);
  470. + }
  471. +
  472. + template<>
  473. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, char *>(char *arg) {
  474. + return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), arg);
  475. + }
  476. +
  477. + template<>
  478. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const char *>(
  479. + const char *arg) {
  480. + return v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), arg);
  481. + }
  482. +
  483. + template<>
  484. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(
  485. + uint8_t *arg
  486. + , int length) {
  487. + return v8::String::NewFromOneByte(
  488. + v8::Isolate::GetCurrent()
  489. + , arg
  490. + , v8::String::kNormalString
  491. + , length);
  492. + }
  493. +
  494. + template<>
  495. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
  496. + const uint8_t *arg
  497. + , int length) {
  498. + return v8::String::NewFromOneByte(
  499. + v8::Isolate::GetCurrent()
  500. + , arg
  501. + , v8::String::kNormalString
  502. + , length);
  503. + }
  504. +
  505. + template<>
  506. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(uint8_t *arg) {
  507. + return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), arg);
  508. + }
  509. +
  510. + template<>
  511. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
  512. + const uint8_t *arg) {
  513. + return v8::String::NewFromOneByte(v8::Isolate::GetCurrent(), arg);
  514. + }
  515. +
  516. + template<>
  517. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint16_t *>(
  518. + uint16_t *arg
  519. + , int length) {
  520. + return v8::String::NewFromTwoByte(
  521. + v8::Isolate::GetCurrent()
  522. + , arg
  523. + , v8::String::kNormalString
  524. + , length);
  525. + }
  526. +
  527. + template<>
  528. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint16_t *>(
  529. + const uint16_t *arg
  530. + , int length) {
  531. + return v8::String::NewFromTwoByte(
  532. + v8::Isolate::GetCurrent()
  533. + , arg
  534. + , v8::String::kNormalString
  535. + , length);
  536. + }
  537. + template<>
  538. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint16_t *>(
  539. + uint16_t *arg) {
  540. + return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), arg);
  541. + }
  542. +
  543. + template<>
  544. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint16_t *>(
  545. + const uint16_t *arg) {
  546. + return v8::String::NewFromTwoByte(v8::Isolate::GetCurrent(), arg);
  547. + }
  548. +
  549. + template<>
  550. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, std::string>(
  551. + std::string arg) {
  552. + return NanNew<v8::String>(arg.c_str(), arg.size());
  553. + }
  554. +
  555. + template<>
  556. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String>() {
  557. + return v8::String::Empty(v8::Isolate::GetCurrent());
  558. + }
  559. +
  560. + NAN_INLINE v8::Local<v8::String> NanNew(const char* arg, int length = -1) {
  561. + return NanNew<v8::String>(arg, length);
  562. + }
  563. +
  564. + NAN_INLINE v8::Local<v8::String> NanNew(
  565. + const uint8_t* arg
  566. + , int length = -1) {
  567. + return NanNew<v8::String>(arg, length);
  568. + }
  569. +
  570. + NAN_INLINE v8::Local<v8::String> NanNew(
  571. + const uint16_t* arg
  572. + , int length = -1) {
  573. + return NanNew<v8::String>(arg, length);
  574. + }
  575. +
  576. + NAN_INLINE v8::Local<v8::String> NanNew(
  577. + const std::string& arg) {
  578. + return NanNew<v8::String>(arg.c_str(), arg.size());
  579. + }
  580. +
  581. + NAN_INLINE v8::Local<v8::Number> NanNew(double val) {
  582. + return NanNew<v8::Number>(val);
  583. + }
  584. +
  585. + NAN_INLINE v8::Local<v8::Integer> NanNew(int val) {
  586. + return NanNew<v8::Integer>(val);
  587. + }
  588. +
  589. + NAN_INLINE v8::Local<v8::Uint32> NanNew(unsigned int val) {
  590. + return NanNew<v8::Uint32>(val);
  591. + }
  592. +
  593. + NAN_INLINE v8::Local<v8::Boolean> NanNew(bool val) {
  594. + return NanNew<v8::Boolean>(val);
  595. + }
  596. +
  597. + NAN_INLINE v8::Local<v8::String> NanNew(
  598. + v8::String::ExternalStringResource *resource) {
  599. + return v8::String::NewExternal(v8::Isolate::GetCurrent(), resource);
  600. + }
  601. +
  602. + NAN_INLINE v8::Local<v8::String> NanNew(
  603. + v8::String::ExternalAsciiStringResource *resource) {
  604. + return v8::String::NewExternal(v8::Isolate::GetCurrent(), resource);
  605. + }
  606. +
  607. +# define NanScope() v8::HandleScope scope(v8::Isolate::GetCurrent())
  608. +# define NanEscapableScope() \
  609. + v8::EscapableHandleScope scope(v8::Isolate::GetCurrent())
  610. +
  611. + template<typename T>
  612. + NAN_INLINE v8::Local<T> _NanEscapeScopeHelper(v8::Handle<T> val) {
  613. + return NanNew(val);
  614. + }
  615. +
  616. + template<typename T>
  617. + NAN_INLINE v8::Local<T> _NanEscapeScopeHelper(v8::Local<T> val) {
  618. + return val;
  619. + }
  620. +
  621. +# define NanEscapeScope(val) scope.Escape(_NanEscapeScopeHelper(val))
  622. +# define NanLocker() v8::Locker locker(v8::Isolate::GetCurrent())
  623. +# define NanUnlocker() v8::Unlocker unlocker(v8::Isolate::GetCurrent())
  624. +# define NanReturnValue(value) return args.GetReturnValue().Set(value)
  625. +# define NanReturnUndefined() return
  626. +# define NanReturnNull() return args.GetReturnValue().SetNull()
  627. +# define NanReturnEmptyString() return args.GetReturnValue().SetEmptyString()
  628. +
  629. +# define NanObjectWrapHandle(obj) obj->handle()
  630. +
  631. + NAN_INLINE v8::Local<v8::Primitive> NanUndefined() {
  632. + NanEscapableScope();
  633. + return NanEscapeScope(NanNew(v8::Undefined(v8::Isolate::GetCurrent())));
  634. + }
  635. +
  636. + NAN_INLINE v8::Local<v8::Primitive> NanNull() {
  637. + NanEscapableScope();
  638. + return NanEscapeScope(NanNew(v8::Null(v8::Isolate::GetCurrent())));
  639. + }
  640. +
  641. + NAN_INLINE v8::Local<v8::Boolean> NanTrue() {
  642. + NanEscapableScope();
  643. + return NanEscapeScope(NanNew(v8::True(v8::Isolate::GetCurrent())));
  644. + }
  645. +
  646. + NAN_INLINE v8::Local<v8::Boolean> NanFalse() {
  647. + NanEscapableScope();
  648. + return NanEscapeScope(NanNew(v8::False(v8::Isolate::GetCurrent())));
  649. + }
  650. +
  651. + NAN_INLINE int NanAdjustExternalMemory(int bc) {
  652. + return static_cast<int>(
  653. + v8::Isolate::GetCurrent()->AdjustAmountOfExternalAllocatedMemory(bc));
  654. + }
  655. +
  656. + NAN_INLINE void NanSetTemplate(
  657. + v8::Handle<v8::Template> templ
  658. + , const char *name
  659. + , v8::Handle<v8::Data> value) {
  660. + templ->Set(v8::Isolate::GetCurrent(), name, value);
  661. + }
  662. +
  663. + NAN_INLINE void NanSetTemplate(
  664. + v8::Handle<v8::Template> templ
  665. + , v8::Handle<v8::String> name
  666. + , v8::Handle<v8::Data> value
  667. + , v8::PropertyAttribute attributes) {
  668. + templ->Set(name, value, attributes);
  669. + }
  670. +
  671. + NAN_INLINE v8::Local<v8::Context> NanGetCurrentContext() {
  672. + return v8::Isolate::GetCurrent()->GetCurrentContext();
  673. + }
  674. +
  675. + NAN_INLINE void* NanGetInternalFieldPointer(
  676. + v8::Handle<v8::Object> object
  677. + , int index) {
  678. + return object->GetAlignedPointerFromInternalField(index);
  679. + }
  680. +
  681. + NAN_INLINE void NanSetInternalFieldPointer(
  682. + v8::Handle<v8::Object> object
  683. + , int index
  684. + , void* value) {
  685. + object->SetAlignedPointerInInternalField(index, value);
  686. + }
  687. +
  688. + NAN_INLINE void NanAddGCEpilogueCallback(
  689. + v8::Isolate::GCEpilogueCallback callback
  690. + , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  691. + v8::Isolate::GetCurrent()->AddGCEpilogueCallback(callback, gc_type_filter);
  692. + }
  693. +
  694. + NAN_INLINE void NanRemoveGCEpilogueCallback(
  695. + v8::Isolate::GCEpilogueCallback callback) {
  696. + v8::Isolate::GetCurrent()->RemoveGCEpilogueCallback(callback);
  697. + }
  698. +
  699. + NAN_INLINE void NanAddGCPrologueCallback(
  700. + v8::Isolate::GCPrologueCallback callback
  701. + , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  702. + v8::Isolate::GetCurrent()->AddGCPrologueCallback(callback, gc_type_filter);
  703. + }
  704. +
  705. + NAN_INLINE void NanRemoveGCPrologueCallback(
  706. + v8::Isolate::GCPrologueCallback callback) {
  707. + v8::Isolate::GetCurrent()->RemoveGCPrologueCallback(callback);
  708. + }
  709. +
  710. + NAN_INLINE void NanGetHeapStatistics(
  711. + v8::HeapStatistics *heap_statistics) {
  712. + v8::Isolate::GetCurrent()->GetHeapStatistics(heap_statistics);
  713. + }
  714. +
  715. + NAN_DEPRECATED NAN_INLINE v8::Local<v8::String> NanSymbol(
  716. + const char* data, int length = -1) {
  717. + return NanNew<v8::String>(data, length);
  718. + }
  719. +
  720. + template<typename T>
  721. + NAN_INLINE void NanAssignPersistent(
  722. + v8::Persistent<T>& handle
  723. + , v8::Handle<T> obj) {
  724. + handle.Reset(v8::Isolate::GetCurrent(), obj);
  725. + }
  726. +
  727. + template<typename T>
  728. + NAN_INLINE void NanAssignPersistent(
  729. + v8::Persistent<T>& handle
  730. + , const v8::Persistent<T>& obj) {
  731. + handle.Reset(v8::Isolate::GetCurrent(), obj);
  732. + }
  733. +
  734. + template<typename T, typename P>
  735. + class _NanWeakCallbackData;
  736. +
  737. + template<typename T, typename P>
  738. + struct _NanWeakCallbackInfo {
  739. + typedef void (*Callback)(const _NanWeakCallbackData<T, P>& data);
  740. + NAN_INLINE _NanWeakCallbackInfo(v8::Handle<T> handle, P* param, Callback cb)
  741. + : parameter(param), callback(cb) {
  742. + NanAssignPersistent(persistent, handle);
  743. + }
  744. +
  745. + NAN_INLINE ~_NanWeakCallbackInfo() {
  746. + persistent.Reset();
  747. + }
  748. +
  749. + P* const parameter;
  750. + Callback const callback;
  751. + v8::Persistent<T> persistent;
  752. + };
  753. +
  754. + template<typename T, typename P>
  755. + class _NanWeakCallbackData {
  756. + public:
  757. + NAN_INLINE _NanWeakCallbackData(_NanWeakCallbackInfo<T, P> *info)
  758. + : info_(info) { }
  759. +
  760. + NAN_INLINE v8::Local<T> GetValue() const {
  761. + return NanNew(info_->persistent);
  762. + }
  763. +
  764. + NAN_INLINE P* GetParameter() const { return info_->parameter; }
  765. +
  766. + NAN_INLINE bool IsNearDeath() const {
  767. + return info_->persistent.IsNearDeath();
  768. + }
  769. +
  770. + NAN_INLINE void Revive() const;
  771. +
  772. + NAN_INLINE _NanWeakCallbackInfo<T, P>* GetCallbackInfo() const {
  773. + return info_;
  774. + }
  775. +
  776. + NAN_DEPRECATED NAN_INLINE void Dispose() const {
  777. + }
  778. +
  779. + private:
  780. + _NanWeakCallbackInfo<T, P>* info_;
  781. + };
  782. +
  783. + template<typename T, typename P>
  784. + static void _NanWeakCallbackDispatcher(
  785. + const v8::WeakCallbackData<T, _NanWeakCallbackInfo<T, P> > &data) {
  786. + _NanWeakCallbackInfo<T, P> *info = data.GetParameter();
  787. + _NanWeakCallbackData<T, P> wcbd(info);
  788. + info->callback(wcbd);
  789. + if (wcbd.IsNearDeath()) {
  790. + delete wcbd.GetCallbackInfo();
  791. + }
  792. + }
  793. +
  794. + template<typename T, typename P>
  795. + NAN_INLINE void _NanWeakCallbackData<T, P>::Revive() const {
  796. + info_->persistent.SetWeak(info_, &_NanWeakCallbackDispatcher<T, P>);
  797. + }
  798. +
  799. +template<typename T, typename P>
  800. +NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
  801. + v8::Handle<T> handle
  802. + , P* parameter
  803. + , typename _NanWeakCallbackInfo<T, P>::Callback callback) {
  804. + _NanWeakCallbackInfo<T, P> *cbinfo =
  805. + new _NanWeakCallbackInfo<T, P>(handle, parameter, callback);
  806. + cbinfo->persistent.SetWeak(cbinfo, &_NanWeakCallbackDispatcher<T, P>);
  807. + return cbinfo;
  808. +}
  809. +
  810. +# define NAN_WEAK_CALLBACK(name) \
  811. + template<typename T, typename P> \
  812. + static void name(const _NanWeakCallbackData<T, P> &data)
  813. +
  814. +# define _NAN_ERROR(fun, errmsg) fun(NanNew<v8::String>(errmsg))
  815. +
  816. +# define _NAN_THROW_ERROR(fun, errmsg) \
  817. + do { \
  818. + NanScope(); \
  819. + v8::Isolate::GetCurrent()->ThrowException(_NAN_ERROR(fun, errmsg)); \
  820. + } while (0);
  821. +
  822. + NAN_INLINE v8::Local<v8::Value> NanError(const char* errmsg) {
  823. + return _NAN_ERROR(v8::Exception::Error, errmsg);
  824. + }
  825. +
  826. + NAN_INLINE void NanThrowError(const char* errmsg) {
  827. + _NAN_THROW_ERROR(v8::Exception::Error, errmsg);
  828. + }
  829. +
  830. + NAN_INLINE void NanThrowError(v8::Handle<v8::Value> error) {
  831. + NanScope();
  832. + v8::Isolate::GetCurrent()->ThrowException(error);
  833. + }
  834. +
  835. + NAN_INLINE v8::Local<v8::Value> NanError(
  836. + const char *msg
  837. + , const int errorNumber
  838. + ) {
  839. + v8::Local<v8::Value> err = v8::Exception::Error(NanNew<v8::String>(msg));
  840. + v8::Local<v8::Object> obj = err.As<v8::Object>();
  841. + obj->Set(NanNew<v8::String>("code"), NanNew<v8::Integer>(errorNumber));
  842. + return err;
  843. + }
  844. +
  845. + NAN_INLINE void NanThrowError(
  846. + const char *msg
  847. + , const int errorNumber
  848. + ) {
  849. + NanThrowError(NanError(msg, errorNumber));
  850. + }
  851. +
  852. + NAN_INLINE v8::Local<v8::Value> NanTypeError(const char* errmsg) {
  853. + return _NAN_ERROR(v8::Exception::TypeError, errmsg);
  854. + }
  855. +
  856. + NAN_INLINE void NanThrowTypeError(const char* errmsg) {
  857. + _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg);
  858. + }
  859. +
  860. + NAN_INLINE v8::Local<v8::Value> NanRangeError(const char* errmsg) {
  861. + return _NAN_ERROR(v8::Exception::RangeError, errmsg);
  862. + }
  863. +
  864. + NAN_INLINE void NanThrowRangeError(const char* errmsg) {
  865. + _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg);
  866. + }
  867. +
  868. + template<typename T> NAN_INLINE void NanDisposePersistent(
  869. + v8::Persistent<T> &handle
  870. + ) {
  871. + handle.Reset();
  872. + }
  873. +
  874. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
  875. + char *data
  876. + , size_t length
  877. + , node::smalloc::FreeCallback callback
  878. + , void *hint
  879. + ) {
  880. + return node::Buffer::New(
  881. + v8::Isolate::GetCurrent(), data, length, callback, hint);
  882. + }
  883. +
  884. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
  885. + const char *data
  886. + , uint32_t size
  887. + ) {
  888. + return node::Buffer::New(v8::Isolate::GetCurrent(), data, size);
  889. + }
  890. +
  891. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (uint32_t size) {
  892. + return node::Buffer::New(v8::Isolate::GetCurrent(), size);
  893. + }
  894. +
  895. + NAN_INLINE v8::Local<v8::Object> NanBufferUse(
  896. + char* data
  897. + , uint32_t size
  898. + ) {
  899. + return node::Buffer::Use(v8::Isolate::GetCurrent(), data, size);
  900. + }
  901. +
  902. + NAN_INLINE bool NanHasInstance(
  903. + v8::Persistent<v8::FunctionTemplate>& function_template
  904. + , v8::Handle<v8::Value> value
  905. + ) {
  906. + return NanNew(function_template)->HasInstance(value);
  907. + }
  908. +
  909. + NAN_INLINE v8::Local<v8::Context> NanNewContextHandle(
  910. + v8::ExtensionConfiguration* extensions = NULL
  911. + , v8::Handle<v8::ObjectTemplate> tmpl = v8::Handle<v8::ObjectTemplate>()
  912. + , v8::Handle<v8::Value> obj = v8::Handle<v8::Value>()
  913. + ) {
  914. + v8::Isolate* isolate = v8::Isolate::GetCurrent();
  915. + return v8::Local<v8::Context>::New(
  916. + isolate
  917. + , v8::Context::New(isolate, extensions, tmpl, obj)
  918. + );
  919. + }
  920. +
  921. + NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
  922. + v8::Local<v8::String> s
  923. + , const v8::ScriptOrigin& origin
  924. + ) {
  925. + v8::ScriptCompiler::Source source(s, origin);
  926. + return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
  927. + }
  928. +
  929. + NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
  930. + v8::Local<v8::String> s
  931. + ) {
  932. + v8::ScriptCompiler::Source source(s);
  933. + return v8::ScriptCompiler::Compile(v8::Isolate::GetCurrent(), &source);
  934. + }
  935. +
  936. + NAN_INLINE v8::Local<v8::Value> NanRunScript(
  937. + v8::Handle<NanUnboundScript> script
  938. + ) {
  939. + return script->BindToCurrentContext()->Run();
  940. + }
  941. +
  942. + NAN_INLINE v8::Local<v8::Value> NanRunScript(
  943. + v8::Handle<NanBoundScript> script
  944. + ) {
  945. + return script->Run();
  946. + }
  947. +
  948. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  949. + v8::Handle<v8::Object> target
  950. + , v8::Handle<v8::Function> func
  951. + , int argc
  952. + , v8::Handle<v8::Value>* argv) {
  953. + return NanNew(node::MakeCallback(
  954. + v8::Isolate::GetCurrent(), target, func, argc, argv));
  955. + }
  956. +
  957. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  958. + v8::Handle<v8::Object> target
  959. + , v8::Handle<v8::String> symbol
  960. + , int argc
  961. + , v8::Handle<v8::Value>* argv) {
  962. + return NanNew(node::MakeCallback(
  963. + v8::Isolate::GetCurrent(), target, symbol, argc, argv));
  964. + }
  965. +
  966. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  967. + v8::Handle<v8::Object> target
  968. + , const char* method
  969. + , int argc
  970. + , v8::Handle<v8::Value>* argv) {
  971. + return NanNew(node::MakeCallback(
  972. + v8::Isolate::GetCurrent(), target, method, argc, argv));
  973. + }
  974. +
  975. + template<typename T>
  976. + NAN_INLINE void NanSetIsolateData(
  977. + v8::Isolate *isolate
  978. + , T *data
  979. + ) {
  980. + isolate->SetData(0, data);
  981. + }
  982. +
  983. + template<typename T>
  984. + NAN_INLINE T *NanGetIsolateData(
  985. + v8::Isolate *isolate
  986. + ) {
  987. + return static_cast<T*>(isolate->GetData(0));
  988. + }
  989. +
  990. + class NanAsciiString {
  991. + public:
  992. + NAN_INLINE explicit NanAsciiString(v8::Handle<v8::Value> from) {
  993. + v8::Local<v8::String> toStr = from->ToString();
  994. + int buf_size = toStr->Length() + 1;
  995. + buf = new char[buf_size];
  996. + size = toStr->WriteOneByte(
  997. + reinterpret_cast<unsigned char*>(buf), 0, buf_size);
  998. + }
  999. +
  1000. + NAN_INLINE int Size() const {
  1001. + return size;
  1002. + }
  1003. +
  1004. + NAN_INLINE char* operator*() { return buf; }
  1005. +
  1006. + NAN_INLINE ~NanAsciiString() {
  1007. + delete[] buf;
  1008. + }
  1009. +
  1010. + private:
  1011. + char *buf;
  1012. + int size;
  1013. + };
  1014. +
  1015. + class NanUtf8String {
  1016. + public:
  1017. + NAN_INLINE explicit NanUtf8String(v8::Handle<v8::Value> from) {
  1018. + v8::Local<v8::String> toStr = from->ToString();
  1019. + int buf_size = toStr->Utf8Length() + 1;
  1020. + buf = new char[buf_size];
  1021. + size = toStr->WriteUtf8(buf, buf_size);
  1022. + }
  1023. +
  1024. + NAN_INLINE int Size() const {
  1025. + return size;
  1026. + }
  1027. +
  1028. + NAN_INLINE char* operator*() { return buf; }
  1029. +
  1030. + NAN_INLINE ~NanUtf8String() {
  1031. + delete[] buf;
  1032. + }
  1033. +
  1034. + private:
  1035. + char *buf;
  1036. + int size;
  1037. + };
  1038. +
  1039. + class NanUcs2String {
  1040. + public:
  1041. + NAN_INLINE explicit NanUcs2String(v8::Handle<v8::Value> from) {
  1042. + v8::Local<v8::String> toStr = from->ToString();
  1043. + int buf_size = toStr->Length() + 1;
  1044. + buf = new uint16_t[buf_size];
  1045. + size = toStr->Write(buf, 0, buf_size);
  1046. + }
  1047. +
  1048. + NAN_INLINE int Size() const {
  1049. + return size;
  1050. + }
  1051. +
  1052. + NAN_INLINE uint16_t* operator*() { return buf; }
  1053. +
  1054. + NAN_INLINE ~NanUcs2String() {
  1055. + delete[] buf;
  1056. + }
  1057. +
  1058. + private:
  1059. + uint16_t *buf;
  1060. + int size;
  1061. + };
  1062. +
  1063. +#else
  1064. +// Node 0.8 and 0.10
  1065. +
  1066. +# define _NAN_METHOD_ARGS_TYPE const v8::Arguments&
  1067. +# define _NAN_METHOD_ARGS _NAN_METHOD_ARGS_TYPE args
  1068. +# define _NAN_METHOD_RETURN_TYPE v8::Handle<v8::Value>
  1069. +
  1070. +# define _NAN_GETTER_ARGS_TYPE const v8::AccessorInfo &
  1071. +# define _NAN_GETTER_ARGS _NAN_GETTER_ARGS_TYPE args
  1072. +# define _NAN_GETTER_RETURN_TYPE v8::Handle<v8::Value>
  1073. +
  1074. +# define _NAN_SETTER_ARGS_TYPE const v8::AccessorInfo &
  1075. +# define _NAN_SETTER_ARGS _NAN_SETTER_ARGS_TYPE args
  1076. +# define _NAN_SETTER_RETURN_TYPE void
  1077. +
  1078. +# define _NAN_PROPERTY_GETTER_ARGS_TYPE const v8::AccessorInfo&
  1079. +# define _NAN_PROPERTY_GETTER_ARGS _NAN_PROPERTY_GETTER_ARGS_TYPE args
  1080. +# define _NAN_PROPERTY_GETTER_RETURN_TYPE v8::Handle<v8::Value>
  1081. +
  1082. +# define _NAN_PROPERTY_SETTER_ARGS_TYPE const v8::AccessorInfo&
  1083. +# define _NAN_PROPERTY_SETTER_ARGS _NAN_PROPERTY_SETTER_ARGS_TYPE args
  1084. +# define _NAN_PROPERTY_SETTER_RETURN_TYPE v8::Handle<v8::Value>
  1085. +
  1086. +# define _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo&
  1087. +# define _NAN_PROPERTY_ENUMERATOR_ARGS _NAN_PROPERTY_ENUMERATOR_ARGS_TYPE args
  1088. +# define _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE v8::Handle<v8::Array>
  1089. +
  1090. +# define _NAN_PROPERTY_DELETER_ARGS_TYPE const v8::AccessorInfo&
  1091. +# define _NAN_PROPERTY_DELETER_ARGS _NAN_PROPERTY_DELETER_ARGS_TYPE args
  1092. +# define _NAN_PROPERTY_DELETER_RETURN_TYPE v8::Handle<v8::Boolean>
  1093. +
  1094. +# define _NAN_PROPERTY_QUERY_ARGS_TYPE const v8::AccessorInfo&
  1095. +# define _NAN_PROPERTY_QUERY_ARGS _NAN_PROPERTY_QUERY_ARGS_TYPE args
  1096. +# define _NAN_PROPERTY_QUERY_RETURN_TYPE v8::Handle<v8::Integer>
  1097. +
  1098. +# define _NAN_INDEX_GETTER_ARGS_TYPE const v8::AccessorInfo&
  1099. +# define _NAN_INDEX_GETTER_ARGS _NAN_INDEX_GETTER_ARGS_TYPE args
  1100. +# define _NAN_INDEX_GETTER_RETURN_TYPE v8::Handle<v8::Value>
  1101. +
  1102. +# define _NAN_INDEX_SETTER_ARGS_TYPE const v8::AccessorInfo&
  1103. +# define _NAN_INDEX_SETTER_ARGS _NAN_INDEX_SETTER_ARGS_TYPE args
  1104. +# define _NAN_INDEX_SETTER_RETURN_TYPE v8::Handle<v8::Value>
  1105. +
  1106. +# define _NAN_INDEX_ENUMERATOR_ARGS_TYPE const v8::AccessorInfo&
  1107. +# define _NAN_INDEX_ENUMERATOR_ARGS _NAN_INDEX_ENUMERATOR_ARGS_TYPE args
  1108. +# define _NAN_INDEX_ENUMERATOR_RETURN_TYPE v8::Handle<v8::Array>
  1109. +
  1110. +# define _NAN_INDEX_DELETER_ARGS_TYPE const v8::AccessorInfo&
  1111. +# define _NAN_INDEX_DELETER_ARGS _NAN_INDEX_DELETER_ARGS_TYPE args
  1112. +# define _NAN_INDEX_DELETER_RETURN_TYPE v8::Handle<v8::Boolean>
  1113. +
  1114. +# define _NAN_INDEX_QUERY_ARGS_TYPE const v8::AccessorInfo&
  1115. +# define _NAN_INDEX_QUERY_ARGS _NAN_INDEX_QUERY_ARGS_TYPE args
  1116. +# define _NAN_INDEX_QUERY_RETURN_TYPE v8::Handle<v8::Integer>
  1117. +
  1118. + typedef v8::InvocationCallback NanFunctionCallback;
  1119. +
  1120. + NAN_DEPRECATED NAN_INLINE v8::Local<v8::String> NanSymbol(
  1121. + const char* data, int length = -1) {
  1122. + return v8::String::NewSymbol(data, length);
  1123. + }
  1124. +
  1125. + template<typename T>
  1126. + NAN_INLINE v8::Local<T> NanNew() {
  1127. + return v8::Local<T>::New(T::New());
  1128. + }
  1129. +
  1130. + template<typename T>
  1131. + NAN_INLINE v8::Local<T> NanNew(v8::Handle<T> arg) {
  1132. + return v8::Local<T>::New(arg);
  1133. + }
  1134. +
  1135. + template<typename T>
  1136. + NAN_INLINE v8::Local<v8::Signature> NanNew(
  1137. + v8::Handle<v8::FunctionTemplate> receiver
  1138. + , int argc
  1139. + , v8::Handle<v8::FunctionTemplate> argv[] = 0) {
  1140. + return v8::Signature::New(receiver, argc, argv);
  1141. + }
  1142. +
  1143. + template<typename T>
  1144. + NAN_INLINE v8::Local<v8::FunctionTemplate> NanNew(
  1145. + NanFunctionCallback callback
  1146. + , v8::Handle<v8::Value> data = v8::Handle<v8::Value>()
  1147. + , v8::Handle<v8::Signature> signature = v8::Handle<v8::Signature>()) {
  1148. + return T::New(callback, data, signature);
  1149. + }
  1150. +
  1151. + template<typename T>
  1152. + NAN_INLINE v8::Local<T> NanNew(const v8::Persistent<T> &arg) {
  1153. + return v8::Local<T>::New(arg);
  1154. + }
  1155. +
  1156. + template<typename T, typename P>
  1157. + NAN_INLINE v8::Local<T> NanNew(P arg) {
  1158. + return v8::Local<T>::New(T::New(arg));
  1159. + }
  1160. +
  1161. + template<typename T, typename P>
  1162. + NAN_INLINE v8::Local<T> NanNew(P arg, int length) {
  1163. + return v8::Local<T>::New(T::New(arg, length));
  1164. + }
  1165. +
  1166. + template<typename T>
  1167. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  1168. + v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
  1169. + return v8::RegExp::New(pattern, flags);
  1170. + }
  1171. +
  1172. + template<typename T>
  1173. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  1174. + v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  1175. + return v8::RegExp::New(pattern, flags);
  1176. + }
  1177. +
  1178. + template<typename T, typename P>
  1179. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  1180. + v8::Handle<v8::String> pattern, v8::RegExp::Flags flags) {
  1181. + return v8::RegExp::New(pattern, flags);
  1182. + }
  1183. +
  1184. + template<typename T, typename P>
  1185. + NAN_INLINE v8::Local<v8::RegExp> NanNew(
  1186. + v8::Local<v8::String> pattern, v8::RegExp::Flags flags) {
  1187. + return v8::RegExp::New(pattern, flags);
  1188. + }
  1189. +
  1190. + template<>
  1191. + NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>() {
  1192. + return v8::Array::New();
  1193. + }
  1194. +
  1195. + template<>
  1196. + NAN_INLINE v8::Local<v8::Array> NanNew<v8::Array>(int length) {
  1197. + return v8::Array::New(length);
  1198. + }
  1199. +
  1200. + template<>
  1201. + NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(double time) {
  1202. + return v8::Date::New(time).As<v8::Date>();
  1203. + }
  1204. +
  1205. + template<>
  1206. + NAN_INLINE v8::Local<v8::Date> NanNew<v8::Date>(int time) {
  1207. + return v8::Date::New(time).As<v8::Date>();
  1208. + }
  1209. +
  1210. + typedef v8::Script NanUnboundScript;
  1211. + typedef v8::Script NanBoundScript;
  1212. +
  1213. + template<typename T, typename P>
  1214. + NAN_INLINE v8::Local<T> NanNew(
  1215. + P s
  1216. + , const v8::ScriptOrigin& origin
  1217. + ) {
  1218. + return v8::Script::New(s, const_cast<v8::ScriptOrigin *>(&origin));
  1219. + }
  1220. +
  1221. + template<>
  1222. + NAN_INLINE v8::Local<NanUnboundScript> NanNew<NanUnboundScript>(
  1223. + v8::Local<v8::String> s
  1224. + ) {
  1225. + return v8::Script::New(s);
  1226. + }
  1227. +
  1228. + template<>
  1229. + NAN_INLINE v8::Local<v8::BooleanObject> NanNew(bool value) {
  1230. + return v8::BooleanObject::New(value).As<v8::BooleanObject>();
  1231. + }
  1232. +
  1233. + template<>
  1234. + NAN_INLINE v8::Local<v8::StringObject>
  1235. + NanNew<v8::StringObject, v8::Local<v8::String> >(
  1236. + v8::Local<v8::String> value) {
  1237. + return v8::StringObject::New(value).As<v8::StringObject>();
  1238. + }
  1239. +
  1240. + template<>
  1241. + NAN_INLINE v8::Local<v8::StringObject>
  1242. + NanNew<v8::StringObject, v8::Handle<v8::String> >(
  1243. + v8::Handle<v8::String> value) {
  1244. + return v8::StringObject::New(value).As<v8::StringObject>();
  1245. + }
  1246. +
  1247. + template<>
  1248. + NAN_INLINE v8::Local<v8::NumberObject> NanNew<v8::NumberObject>(double val) {
  1249. + return v8::NumberObject::New(val).As<v8::NumberObject>();
  1250. + }
  1251. +
  1252. + template<>
  1253. + NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, int32_t>(int32_t val) {
  1254. + return v8::Uint32::NewFromUnsigned(val)->ToUint32();
  1255. + }
  1256. +
  1257. + template<>
  1258. + NAN_INLINE v8::Local<v8::Uint32> NanNew<v8::Uint32, uint32_t>(uint32_t val) {
  1259. + return v8::Uint32::NewFromUnsigned(val)->ToUint32();
  1260. + }
  1261. +
  1262. + template<>
  1263. + NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, int32_t>(int32_t val) {
  1264. + return v8::Int32::New(val)->ToInt32();
  1265. + }
  1266. +
  1267. + template<>
  1268. + NAN_INLINE v8::Local<v8::Int32> NanNew<v8::Int32, uint32_t>(uint32_t val) {
  1269. + return v8::Int32::New(val)->ToInt32();
  1270. + }
  1271. +
  1272. + template<>
  1273. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(
  1274. + uint8_t *arg
  1275. + , int length) {
  1276. + int len = length;
  1277. + if (len < 0) {
  1278. + size_t temp = strlen(reinterpret_cast<const char *>(arg));
  1279. + assert(temp <= INT_MAX && "too long string");
  1280. + len = static_cast<int>(temp);
  1281. + }
  1282. + uint16_t *warg = new uint16_t[len];
  1283. + for (int i = 0; i < len; i++) {
  1284. + warg[i] = arg[i];
  1285. + }
  1286. + v8::Local<v8::String> retval = v8::String::New(warg, len);
  1287. + delete[] warg;
  1288. + return retval;
  1289. + }
  1290. +
  1291. + template<>
  1292. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
  1293. + const uint8_t *arg
  1294. + , int length) {
  1295. + int len = length;
  1296. + if (len < 0) {
  1297. + size_t temp = strlen(reinterpret_cast<const char *>(arg));
  1298. + assert(temp <= INT_MAX && "too long string");
  1299. + len = static_cast<int>(temp);
  1300. + }
  1301. + uint16_t *warg = new uint16_t[len];
  1302. + for (int i = 0; i < len; i++) {
  1303. + warg[i] = arg[i];
  1304. + }
  1305. + v8::Local<v8::String> retval = v8::String::New(warg, len);
  1306. + delete[] warg;
  1307. + return retval;
  1308. + }
  1309. +
  1310. + template<>
  1311. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, uint8_t *>(uint8_t *arg) {
  1312. + size_t temp = strlen(reinterpret_cast<char *>(arg));
  1313. + assert(temp <= INT_MAX && "too long string");
  1314. + int length = static_cast<int>(temp);
  1315. + uint16_t *warg = new uint16_t[length];
  1316. + for (int i = 0; i < length; i++) {
  1317. + warg[i] = arg[i];
  1318. + }
  1319. +
  1320. + v8::Local<v8::String> retval = v8::String::New(warg, length);
  1321. + delete[] warg;
  1322. + return retval;
  1323. + }
  1324. +
  1325. + template<>
  1326. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, const uint8_t *>(
  1327. + const uint8_t *arg) {
  1328. + size_t temp = strlen(reinterpret_cast<const char *>(arg));
  1329. + assert(temp <= INT_MAX && "too long string");
  1330. + int length = static_cast<int>(temp);
  1331. + uint16_t *warg = new uint16_t[length];
  1332. + for (int i = 0; i < length; i++) {
  1333. + warg[i] = arg[i];
  1334. + }
  1335. + v8::Local<v8::String> retval = v8::String::New(warg, length);
  1336. + delete[] warg;
  1337. + return retval;
  1338. + }
  1339. +
  1340. + template<>
  1341. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String, std::string>(
  1342. + std::string arg) {
  1343. + return NanNew<v8::String>(arg.c_str(), arg.size());
  1344. + }
  1345. +
  1346. + template<>
  1347. + NAN_INLINE v8::Local<v8::String> NanNew<v8::String>() {
  1348. + return v8::String::Empty();
  1349. + }
  1350. +
  1351. + NAN_INLINE v8::Local<v8::String> NanNew(const char* arg, int length = -1) {
  1352. + return NanNew<v8::String>(arg, length);
  1353. + }
  1354. +
  1355. + NAN_INLINE v8::Local<v8::String> NanNew(
  1356. + const uint8_t* arg
  1357. + , int length = -1) {
  1358. + return NanNew<v8::String>(arg, length);
  1359. + }
  1360. +
  1361. + NAN_INLINE v8::Local<v8::String> NanNew(
  1362. + const uint16_t* arg
  1363. + , int length = -1) {
  1364. + return NanNew<v8::String>(arg, length);
  1365. + }
  1366. +
  1367. + NAN_INLINE v8::Local<v8::String> NanNew(
  1368. + std::string& arg) {
  1369. + return NanNew<v8::String>(arg.c_str(), arg.size());
  1370. + }
  1371. +
  1372. + NAN_INLINE v8::Local<v8::Number> NanNew(double val) {
  1373. + return NanNew<v8::Number>(val);
  1374. + }
  1375. +
  1376. + NAN_INLINE v8::Local<v8::Integer> NanNew(int val) {
  1377. + return NanNew<v8::Integer>(val);
  1378. + }
  1379. +
  1380. + NAN_INLINE v8::Local<v8::Uint32> NanNew(unsigned int val) {
  1381. + return NanNew<v8::Uint32>(val);
  1382. + }
  1383. +
  1384. + NAN_INLINE v8::Local<v8::Boolean> NanNew(bool val) {
  1385. + return NanNew<v8::Boolean>(val);
  1386. + }
  1387. +
  1388. + NAN_INLINE v8::Local<v8::String> NanNew(
  1389. + v8::String::ExternalStringResource *resource) {
  1390. + return v8::String::NewExternal(resource);
  1391. + }
  1392. +
  1393. + NAN_INLINE v8::Local<v8::String> NanNew(
  1394. + v8::String::ExternalAsciiStringResource *resource) {
  1395. + return v8::String::NewExternal(resource);
  1396. + }
  1397. +
  1398. +# define NanScope() v8::HandleScope scope
  1399. +# define NanEscapableScope() v8::HandleScope scope
  1400. +# define NanEscapeScope(val) scope.Close(val)
  1401. +# define NanLocker() v8::Locker locker
  1402. +# define NanUnlocker() v8::Unlocker unlocker
  1403. +# define NanReturnValue(value) return scope.Close(value)
  1404. +# define NanReturnUndefined() return v8::Undefined()
  1405. +# define NanReturnNull() return v8::Null()
  1406. +# define NanReturnEmptyString() return v8::String::Empty()
  1407. +# define NanObjectWrapHandle(obj) v8::Local<v8::Object>::New(obj->handle_)
  1408. +
  1409. + NAN_INLINE v8::Local<v8::Primitive> NanUndefined() {
  1410. + NanEscapableScope();
  1411. + return NanEscapeScope(NanNew(v8::Undefined()));
  1412. + }
  1413. +
  1414. + NAN_INLINE v8::Local<v8::Primitive> NanNull() {
  1415. + NanEscapableScope();
  1416. + return NanEscapeScope(NanNew(v8::Null()));
  1417. + }
  1418. +
  1419. + NAN_INLINE v8::Local<v8::Boolean> NanTrue() {
  1420. + NanEscapableScope();
  1421. + return NanEscapeScope(NanNew(v8::True()));
  1422. + }
  1423. +
  1424. + NAN_INLINE v8::Local<v8::Boolean> NanFalse() {
  1425. + NanEscapableScope();
  1426. + return NanEscapeScope(NanNew(v8::False()));
  1427. + }
  1428. +
  1429. + NAN_INLINE int NanAdjustExternalMemory(int bc) {
  1430. + return static_cast<int>(v8::V8::AdjustAmountOfExternalAllocatedMemory(bc));
  1431. + }
  1432. +
  1433. + NAN_INLINE void NanSetTemplate(
  1434. + v8::Handle<v8::Template> templ
  1435. + , const char *name
  1436. + , v8::Handle<v8::Data> value) {
  1437. + templ->Set(name, value);
  1438. + }
  1439. +
  1440. + NAN_INLINE void NanSetTemplate(
  1441. + v8::Handle<v8::Template> templ
  1442. + , v8::Handle<v8::String> name
  1443. + , v8::Handle<v8::Data> value
  1444. + , v8::PropertyAttribute attributes) {
  1445. + templ->Set(name, value, attributes);
  1446. + }
  1447. +
  1448. + NAN_INLINE v8::Local<v8::Context> NanGetCurrentContext() {
  1449. + return v8::Context::GetCurrent();
  1450. + }
  1451. +
  1452. + NAN_INLINE void* NanGetInternalFieldPointer(
  1453. + v8::Handle<v8::Object> object
  1454. + , int index) {
  1455. + return object->GetPointerFromInternalField(index);
  1456. + }
  1457. +
  1458. + NAN_INLINE void NanSetInternalFieldPointer(
  1459. + v8::Handle<v8::Object> object
  1460. + , int index
  1461. + , void* value) {
  1462. + object->SetPointerInInternalField(index, value);
  1463. + }
  1464. +
  1465. + NAN_INLINE void NanAddGCEpilogueCallback(
  1466. + v8::GCEpilogueCallback callback
  1467. + , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  1468. + v8::V8::AddGCEpilogueCallback(callback, gc_type_filter);
  1469. + }
  1470. + NAN_INLINE void NanRemoveGCEpilogueCallback(
  1471. + v8::GCEpilogueCallback callback) {
  1472. + v8::V8::RemoveGCEpilogueCallback(callback);
  1473. + }
  1474. + NAN_INLINE void NanAddGCPrologueCallback(
  1475. + v8::GCPrologueCallback callback
  1476. + , v8::GCType gc_type_filter = v8::kGCTypeAll) {
  1477. + v8::V8::AddGCPrologueCallback(callback, gc_type_filter);
  1478. + }
  1479. + NAN_INLINE void NanRemoveGCPrologueCallback(
  1480. + v8::GCPrologueCallback callback) {
  1481. + v8::V8::RemoveGCPrologueCallback(callback);
  1482. + }
  1483. + NAN_INLINE void NanGetHeapStatistics(
  1484. + v8::HeapStatistics *heap_statistics) {
  1485. + v8::V8::GetHeapStatistics(heap_statistics);
  1486. + }
  1487. +
  1488. + template<typename T>
  1489. + NAN_INLINE void NanAssignPersistent(
  1490. + v8::Persistent<T>& handle
  1491. + , v8::Handle<T> obj) {
  1492. + handle.Dispose();
  1493. + handle = v8::Persistent<T>::New(obj);
  1494. + }
  1495. +
  1496. + template<typename T, typename P>
  1497. + class _NanWeakCallbackData;
  1498. +
  1499. + template<typename T, typename P>
  1500. + struct _NanWeakCallbackInfo {
  1501. + typedef void (*Callback)(const _NanWeakCallbackData<T, P> &data);
  1502. + NAN_INLINE _NanWeakCallbackInfo(v8::Handle<T> handle, P* param, Callback cb)
  1503. + : parameter(param)
  1504. + , callback(cb)
  1505. + , persistent(v8::Persistent<T>::New(handle)) { }
  1506. +
  1507. + NAN_INLINE ~_NanWeakCallbackInfo() {
  1508. + persistent.Dispose();
  1509. + persistent.Clear();
  1510. + }
  1511. +
  1512. + P* const parameter;
  1513. + Callback const callback;
  1514. + v8::Persistent<T> persistent;
  1515. + };
  1516. +
  1517. + template<typename T, typename P>
  1518. + class _NanWeakCallbackData {
  1519. + public:
  1520. + NAN_INLINE _NanWeakCallbackData(_NanWeakCallbackInfo<T, P> *info)
  1521. + : info_(info) { }
  1522. +
  1523. + NAN_INLINE v8::Local<T> GetValue() const {
  1524. + return NanNew(info_->persistent);
  1525. + }
  1526. +
  1527. + NAN_INLINE P* GetParameter() const { return info_->parameter; }
  1528. +
  1529. + NAN_INLINE bool IsNearDeath() const {
  1530. + return info_->persistent.IsNearDeath();
  1531. + }
  1532. +
  1533. + NAN_INLINE void Revive() const;
  1534. +
  1535. + NAN_INLINE _NanWeakCallbackInfo<T, P>* GetCallbackInfo() const {
  1536. + return info_;
  1537. + }
  1538. +
  1539. + NAN_DEPRECATED NAN_INLINE void Dispose() const {
  1540. + }
  1541. +
  1542. + private:
  1543. + _NanWeakCallbackInfo<T, P>* info_;
  1544. + };
  1545. +
  1546. + template<typename T, typename P>
  1547. + static void _NanWeakPersistentDispatcher(
  1548. + v8::Persistent<v8::Value> object, void *data) {
  1549. + _NanWeakCallbackInfo<T, P>* info =
  1550. + static_cast<_NanWeakCallbackInfo<T, P>*>(data);
  1551. + _NanWeakCallbackData<T, P> wcbd(info);
  1552. + info->callback(wcbd);
  1553. + if (wcbd.IsNearDeath()) {
  1554. + delete wcbd.GetCallbackInfo();
  1555. + }
  1556. + }
  1557. +
  1558. + template<typename T, typename P>
  1559. + NAN_INLINE void _NanWeakCallbackData<T, P>::Revive() const {
  1560. + info_->persistent.MakeWeak(
  1561. + info_
  1562. + , &_NanWeakPersistentDispatcher<T, P>);
  1563. + }
  1564. +
  1565. + template<typename T, typename P>
  1566. + NAN_INLINE _NanWeakCallbackInfo<T, P>* NanMakeWeakPersistent(
  1567. + v8::Handle<T> handle
  1568. + , P* parameter
  1569. + , typename _NanWeakCallbackInfo<T, P>::Callback callback) {
  1570. + _NanWeakCallbackInfo<T, P> *cbinfo =
  1571. + new _NanWeakCallbackInfo<T, P>(handle, parameter, callback);
  1572. + cbinfo->persistent.MakeWeak(
  1573. + cbinfo
  1574. + , &_NanWeakPersistentDispatcher<T, P>);
  1575. + return cbinfo;
  1576. + }
  1577. +
  1578. +# define NAN_WEAK_CALLBACK(name) \
  1579. + template<typename T, typename P> \
  1580. + static void name(const _NanWeakCallbackData<T, P> &data)
  1581. +
  1582. +# define _NAN_ERROR(fun, errmsg) \
  1583. + fun(v8::String::New(errmsg))
  1584. +
  1585. +# define _NAN_THROW_ERROR(fun, errmsg) \
  1586. + do { \
  1587. + NanScope(); \
  1588. + return v8::Local<v8::Value>::New( \
  1589. + v8::ThrowException(_NAN_ERROR(fun, errmsg))); \
  1590. + } while (0);
  1591. +
  1592. + NAN_INLINE v8::Local<v8::Value> NanError(const char* errmsg) {
  1593. + return _NAN_ERROR(v8::Exception::Error, errmsg);
  1594. + }
  1595. +
  1596. + NAN_INLINE v8::Local<v8::Value> NanThrowError(const char* errmsg) {
  1597. + _NAN_THROW_ERROR(v8::Exception::Error, errmsg);
  1598. + }
  1599. +
  1600. + NAN_INLINE v8::Local<v8::Value> NanThrowError(
  1601. + v8::Handle<v8::Value> error
  1602. + ) {
  1603. + NanScope();
  1604. + return v8::Local<v8::Value>::New(v8::ThrowException(error));
  1605. + }
  1606. +
  1607. + NAN_INLINE v8::Local<v8::Value> NanError(
  1608. + const char *msg
  1609. + , const int errorNumber
  1610. + ) {
  1611. + v8::Local<v8::Value> err = v8::Exception::Error(v8::String::New(msg));
  1612. + v8::Local<v8::Object> obj = err.As<v8::Object>();
  1613. + obj->Set(v8::String::New("code"), v8::Int32::New(errorNumber));
  1614. + return err;
  1615. + }
  1616. +
  1617. + NAN_INLINE v8::Local<v8::Value> NanThrowError(
  1618. + const char *msg
  1619. + , const int errorNumber
  1620. + ) {
  1621. + return NanThrowError(NanError(msg, errorNumber));
  1622. + }
  1623. +
  1624. + NAN_INLINE v8::Local<v8::Value> NanTypeError(const char* errmsg) {
  1625. + return _NAN_ERROR(v8::Exception::TypeError, errmsg);
  1626. + }
  1627. +
  1628. + NAN_INLINE v8::Local<v8::Value> NanThrowTypeError(
  1629. + const char* errmsg
  1630. + ) {
  1631. + _NAN_THROW_ERROR(v8::Exception::TypeError, errmsg);
  1632. + }
  1633. +
  1634. + NAN_INLINE v8::Local<v8::Value> NanRangeError(
  1635. + const char* errmsg
  1636. + ) {
  1637. + return _NAN_ERROR(v8::Exception::RangeError, errmsg);
  1638. + }
  1639. +
  1640. + NAN_INLINE v8::Local<v8::Value> NanThrowRangeError(
  1641. + const char* errmsg
  1642. + ) {
  1643. + _NAN_THROW_ERROR(v8::Exception::RangeError, errmsg);
  1644. + }
  1645. +
  1646. + template<typename T>
  1647. + NAN_INLINE void NanDisposePersistent(
  1648. + v8::Persistent<T> &handle) { // NOLINT(runtime/references)
  1649. + handle.Dispose();
  1650. + handle.Clear();
  1651. + }
  1652. +
  1653. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
  1654. + char *data
  1655. + , size_t length
  1656. + , node::Buffer::free_callback callback
  1657. + , void *hint
  1658. + ) {
  1659. + return NanNew(
  1660. + node::Buffer::New(data, length, callback, hint)->handle_);
  1661. + }
  1662. +
  1663. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (
  1664. + const char *data
  1665. + , uint32_t size
  1666. + ) {
  1667. +#if NODE_MODULE_VERSION >= 0x000B
  1668. + return NanNew(node::Buffer::New(data, size)->handle_);
  1669. +#else
  1670. + return NanNew(
  1671. + node::Buffer::New(const_cast<char*>(data), size)->handle_);
  1672. +#endif
  1673. + }
  1674. +
  1675. + NAN_INLINE v8::Local<v8::Object> NanNewBufferHandle (uint32_t size) {
  1676. + return NanNew(node::Buffer::New(size)->handle_);
  1677. + }
  1678. +
  1679. + NAN_INLINE void FreeData(char *data, void *hint) {
  1680. + delete[] data;
  1681. + }
  1682. +
  1683. + NAN_INLINE v8::Local<v8::Object> NanBufferUse(
  1684. + char* data
  1685. + , uint32_t size
  1686. + ) {
  1687. + return NanNew(
  1688. + node::Buffer::New(data, size, FreeData, NULL)->handle_);
  1689. + }
  1690. +
  1691. + NAN_INLINE bool NanHasInstance(
  1692. + v8::Persistent<v8::FunctionTemplate>& function_template
  1693. + , v8::Handle<v8::Value> value
  1694. + ) {
  1695. + return function_template->HasInstance(value);
  1696. + }
  1697. +
  1698. + NAN_INLINE v8::Local<v8::Context> NanNewContextHandle(
  1699. + v8::ExtensionConfiguration* extensions = NULL
  1700. + , v8::Handle<v8::ObjectTemplate> tmpl = v8::Handle<v8::ObjectTemplate>()
  1701. + , v8::Handle<v8::Value> obj = v8::Handle<v8::Value>()
  1702. + ) {
  1703. + v8::Persistent<v8::Context> ctx = v8::Context::New(extensions, tmpl, obj);
  1704. + v8::Local<v8::Context> lctx = NanNew(ctx);
  1705. + ctx.Dispose();
  1706. + return lctx;
  1707. + }
  1708. +
  1709. + NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
  1710. + v8::Local<v8::String> s
  1711. + , const v8::ScriptOrigin& origin
  1712. + ) {
  1713. + return v8::Script::Compile(s, const_cast<v8::ScriptOrigin *>(&origin));
  1714. + }
  1715. +
  1716. + NAN_INLINE v8::Local<NanBoundScript> NanCompileScript(
  1717. + v8::Local<v8::String> s
  1718. + ) {
  1719. + return v8::Script::Compile(s);
  1720. + }
  1721. +
  1722. + NAN_INLINE v8::Local<v8::Value> NanRunScript(v8::Handle<v8::Script> script) {
  1723. + return script->Run();
  1724. + }
  1725. +
  1726. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  1727. + v8::Handle<v8::Object> target
  1728. + , v8::Handle<v8::Function> func
  1729. + , int argc
  1730. + , v8::Handle<v8::Value>* argv) {
  1731. +# if NODE_VERSION_AT_LEAST(0, 8, 0)
  1732. + return NanNew(node::MakeCallback(target, func, argc, argv));
  1733. +# else
  1734. + v8::TryCatch try_catch;
  1735. + v8::Local<v8::Value> result = NanNew(func->Call(target, argc, argv));
  1736. + if (try_catch.HasCaught()) {
  1737. + node::FatalException(try_catch);
  1738. + }
  1739. + return result;
  1740. +# endif
  1741. + }
  1742. +
  1743. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  1744. + v8::Handle<v8::Object> target
  1745. + , v8::Handle<v8::String> symbol
  1746. + , int argc
  1747. + , v8::Handle<v8::Value>* argv) {
  1748. +# if NODE_VERSION_AT_LEAST(0, 8, 0)
  1749. + return NanNew(node::MakeCallback(target, symbol, argc, argv));
  1750. +# else
  1751. + v8::Local<v8::Function> callback = target->Get(symbol).As<v8::Function>();
  1752. + return NanMakeCallback(target, callback, argc, argv);
  1753. +# endif
  1754. + }
  1755. +
  1756. + NAN_INLINE v8::Local<v8::Value> NanMakeCallback(
  1757. + v8::Handle<v8::Object> target
  1758. + , const char* method
  1759. + , int argc
  1760. + , v8::Handle<v8::Value>* argv) {
  1761. +# if NODE_VERSION_AT_LEAST(0, 8, 0)
  1762. + return NanNew(node::MakeCallback(target, method, argc, argv));
  1763. +# else
  1764. + return NanMakeCallback(target, NanNew(method), argc, argv);
  1765. +# endif
  1766. + }
  1767. +
  1768. + template<typename T>
  1769. + NAN_INLINE void NanSetIsolateData(
  1770. + v8::Isolate *isolate
  1771. + , T *data
  1772. + ) {
  1773. + isolate->SetData(data);
  1774. + }
  1775. +
  1776. + template<typename T>
  1777. + NAN_INLINE T *NanGetIsolateData(
  1778. + v8::Isolate *isolate
  1779. + ) {
  1780. + return static_cast<T*>(isolate->GetData());
  1781. + }
  1782. +
  1783. + class NanAsciiString {
  1784. + public:
  1785. + NAN_INLINE explicit NanAsciiString(v8::Handle<v8::Value> from) {
  1786. + v8::Local<v8::String> toStr = from->ToString();
  1787. + int buf_size = toStr->Length() + 1;
  1788. + buf = new char[buf_size];
  1789. + size = toStr->WriteAscii(buf, 0, buf_size);
  1790. + }
  1791. +
  1792. + NAN_INLINE int Size() const {
  1793. + return size;
  1794. + }
  1795. +
  1796. + NAN_INLINE char* operator*() { return buf; }
  1797. +
  1798. + NAN_INLINE ~NanAsciiString() {
  1799. + delete[] buf;
  1800. + }
  1801. +
  1802. + private:
  1803. + char *buf;
  1804. + int size;
  1805. + };
  1806. +
  1807. + class NanUtf8String {
  1808. + public:
  1809. + NAN_INLINE explicit NanUtf8String(v8::Handle<v8::Value> from) {
  1810. + v8::Local<v8::String> toStr = from->ToString();
  1811. + int buf_size = toStr->Utf8Length() + 1;
  1812. + buf = new char[buf_size];
  1813. + size = toStr->WriteUtf8(buf, buf_size);
  1814. + }
  1815. +
  1816. + NAN_INLINE int Size() const {
  1817. + return size;
  1818. + }
  1819. +
  1820. + NAN_INLINE char* operator*() { return buf; }
  1821. +
  1822. + NAN_INLINE ~NanUtf8String() {
  1823. + delete[] buf;
  1824. + }
  1825. +
  1826. + private:
  1827. + char *buf;
  1828. + int size;
  1829. + };
  1830. +
  1831. + class NanUcs2String {
  1832. + public:
  1833. + NAN_INLINE explicit NanUcs2String(v8::Handle<v8::Value> from) {
  1834. + v8::Local<v8::String> toStr = from->ToString();
  1835. + int buf_size = toStr->Length() + 1;
  1836. + buf = new uint16_t[buf_size];
  1837. + size = toStr->Write(buf, 0, buf_size);
  1838. + }
  1839. +
  1840. + NAN_INLINE int Size() const {
  1841. + return size;
  1842. + }
  1843. +
  1844. + NAN_INLINE uint16_t* operator*() { return buf; }
  1845. +
  1846. + NAN_INLINE ~NanUcs2String() {
  1847. + delete[] buf;
  1848. + }
  1849. +
  1850. + private:
  1851. + uint16_t *buf;
  1852. + int size;
  1853. + };
  1854. +
  1855. +#endif // NODE_MODULE_VERSION
  1856. +
  1857. +typedef void (*NanFreeCallback)(char *data, void *hint);
  1858. +
  1859. +#define NAN_METHOD(name) _NAN_METHOD_RETURN_TYPE name(_NAN_METHOD_ARGS)
  1860. +#define NAN_GETTER(name) \
  1861. + _NAN_GETTER_RETURN_TYPE name( \
  1862. + v8::Local<v8::String> property \
  1863. + , _NAN_GETTER_ARGS)
  1864. +#define NAN_SETTER(name) \
  1865. + _NAN_SETTER_RETURN_TYPE name( \
  1866. + v8::Local<v8::String> property \
  1867. + , v8::Local<v8::Value> value \
  1868. + , _NAN_SETTER_ARGS)
  1869. +#define NAN_PROPERTY_GETTER(name) \
  1870. + _NAN_PROPERTY_GETTER_RETURN_TYPE name( \
  1871. + v8::Local<v8::String> property \
  1872. + , _NAN_PROPERTY_GETTER_ARGS)
  1873. +#define NAN_PROPERTY_SETTER(name) \
  1874. + _NAN_PROPERTY_SETTER_RETURN_TYPE name( \
  1875. + v8::Local<v8::String> property \
  1876. + , v8::Local<v8::Value> value \
  1877. + , _NAN_PROPERTY_SETTER_ARGS)
  1878. +#define NAN_PROPERTY_ENUMERATOR(name) \
  1879. + _NAN_PROPERTY_ENUMERATOR_RETURN_TYPE name(_NAN_PROPERTY_ENUMERATOR_ARGS)
  1880. +#define NAN_PROPERTY_DELETER(name) \
  1881. + _NAN_PROPERTY_DELETER_RETURN_TYPE name( \
  1882. + v8::Local<v8::String> property \
  1883. + , _NAN_PROPERTY_DELETER_ARGS)
  1884. +#define NAN_PROPERTY_QUERY(name) \
  1885. + _NAN_PROPERTY_QUERY_RETURN_TYPE name( \
  1886. + v8::Local<v8::String> property \
  1887. + , _NAN_PROPERTY_QUERY_ARGS)
  1888. +# define NAN_INDEX_GETTER(name) \
  1889. + _NAN_INDEX_GETTER_RETURN_TYPE name(uint32_t index, _NAN_INDEX_GETTER_ARGS)
  1890. +#define NAN_INDEX_SETTER(name) \
  1891. + _NAN_INDEX_SETTER_RETURN_TYPE name( \
  1892. + uint32_t index \
  1893. + , v8::Local<v8::Value> value \
  1894. + , _NAN_INDEX_SETTER_ARGS)
  1895. +#define NAN_INDEX_ENUMERATOR(name) \
  1896. + _NAN_INDEX_ENUMERATOR_RETURN_TYPE name(_NAN_INDEX_ENUMERATOR_ARGS)
  1897. +#define NAN_INDEX_DELETER(name) \
  1898. + _NAN_INDEX_DELETER_RETURN_TYPE name( \
  1899. + uint32_t index \
  1900. + , _NAN_INDEX_DELETER_ARGS)
  1901. +#define NAN_INDEX_QUERY(name) \
  1902. + _NAN_INDEX_QUERY_RETURN_TYPE name(uint32_t index, _NAN_INDEX_QUERY_ARGS)
  1903. +
  1904. +class NanCallback {
  1905. + public:
  1906. + NanCallback() {
  1907. + NanScope();
  1908. + v8::Local<v8::Object> obj = NanNew<v8::Object>();
  1909. + NanAssignPersistent(handle, obj);
  1910. + }
  1911. +
  1912. + explicit NanCallback(const v8::Handle<v8::Function> &fn) {
  1913. + NanScope();
  1914. + v8::Local<v8::Object> obj = NanNew<v8::Object>();
  1915. + NanAssignPersistent(handle, obj);
  1916. + SetFunction(fn);
  1917. + }
  1918. +
  1919. + ~NanCallback() {
  1920. + if (handle.IsEmpty()) return;
  1921. + NanDisposePersistent(handle);
  1922. + }
  1923. +
  1924. + NAN_INLINE void SetFunction(const v8::Handle<v8::Function> &fn) {
  1925. + NanScope();
  1926. + NanNew(handle)->Set(kCallbackIndex, fn);
  1927. + }
  1928. +
  1929. + NAN_INLINE v8::Local<v8::Function> GetFunction() const {
  1930. + NanEscapableScope();
  1931. + return NanEscapeScope(NanNew(handle)->Get(kCallbackIndex)
  1932. + .As<v8::Function>());
  1933. + }
  1934. +
  1935. + NAN_INLINE bool IsEmpty() const {
  1936. + NanScope();
  1937. + return NanNew(handle)->Get(kCallbackIndex)->IsUndefined();
  1938. + }
  1939. +
  1940. + v8::Handle<v8::Value> Call(int argc, v8::Handle<v8::Value> argv[]) const {
  1941. + NanEscapableScope();
  1942. +#if (NODE_MODULE_VERSION > 0x000B) // 0.11.12+
  1943. + v8::Isolate* isolate = v8::Isolate::GetCurrent();
  1944. + v8::Local<v8::Function> callback = NanNew(handle)->
  1945. + Get(kCallbackIndex).As<v8::Function>();
  1946. + return NanEscapeScope(node::MakeCallback(
  1947. + isolate
  1948. + , isolate->GetCurrentContext()->Global()
  1949. + , callback
  1950. + , argc
  1951. + , argv
  1952. + ));
  1953. +#else
  1954. +#if NODE_VERSION_AT_LEAST(0, 8, 0)
  1955. + v8::Local<v8::Function> callback = handle->
  1956. + Get(kCallbackIndex).As<v8::Function>();
  1957. + return NanEscapeScope(node::MakeCallback(
  1958. + v8::Context::GetCurrent()->Global()
  1959. + , callback
  1960. + , argc
  1961. + , argv
  1962. + ));
  1963. +#else
  1964. + v8::Local<v8::Function> callback = handle->
  1965. + Get(kCallbackIndex).As<v8::Function>();
  1966. + return NanEscapeScope(NanMakeCallback(
  1967. + v8::Context::GetCurrent()->Global(), callback, argc, argv));
  1968. +#endif
  1969. +#endif
  1970. + }
  1971. +
  1972. + private:
  1973. + v8::Persistent<v8::Object> handle;
  1974. + static const uint32_t kCallbackIndex = 0;
  1975. +};
  1976. +
  1977. +/* abstract */ class NanAsyncWorker {
  1978. + public:
  1979. + explicit NanAsyncWorker(NanCallback *callback)
  1980. + : callback(callback), errmsg_(NULL) {
  1981. + request.data = this;
  1982. +
  1983. + NanScope();
  1984. + v8::Local<v8::Object> obj = NanNew<v8::Object>();
  1985. + NanAssignPersistent(persistentHandle, obj);
  1986. + }
  1987. +
  1988. + virtual ~NanAsyncWorker() {
  1989. + NanScope();
  1990. +
  1991. + if (!persistentHandle.IsEmpty())
  1992. + NanDisposePersistent(persistentHandle);
  1993. + if (callback)
  1994. + delete callback;
  1995. + if (errmsg_)
  1996. + delete[] errmsg_;
  1997. + }
  1998. +
  1999. + virtual void WorkComplete() {
  2000. + NanScope();
  2001. +
  2002. + if (errmsg_ == NULL)
  2003. + HandleOKCallback();
  2004. + else
  2005. + HandleErrorCallback();
  2006. + delete callback;
  2007. + callback = NULL;
  2008. + }
  2009. +
  2010. + NAN_INLINE void SaveToPersistent(
  2011. + const char *key, const v8::Local<v8::Object> &obj) {
  2012. + v8::Local<v8::Object> handle = NanNew(persistentHandle);
  2013. + handle->Set(NanNew<v8::String>(key), obj);
  2014. + }
  2015. +
  2016. + v8::Local<v8::Object> GetFromPersistent(const char *key) const {
  2017. + NanEscapableScope();
  2018. + v8::Local<v8::Object> handle = NanNew(persistentHandle);
  2019. + return NanEscapeScope(handle->Get(NanNew(key)).As<v8::Object>());
  2020. + }
  2021. +
  2022. + virtual void Execute() = 0;
  2023. +
  2024. + uv_work_t request;
  2025. +
  2026. + protected:
  2027. + v8::Persistent<v8::Object> persistentHandle;
  2028. + NanCallback *callback;
  2029. +
  2030. + virtual void HandleOKCallback() {
  2031. + NanScope();
  2032. +
  2033. + callback->Call(0, NULL);
  2034. + }
  2035. +
  2036. + virtual void HandleErrorCallback() {
  2037. + NanScope();
  2038. +
  2039. + v8::Local<v8::Value> argv[] = {
  2040. + v8::Exception::Error(NanNew<v8::String>(ErrorMessage()))
  2041. + };
  2042. + callback->Call(1, argv);
  2043. + }
  2044. +
  2045. + void SetErrorMessage(const char *msg) {
  2046. + if (errmsg_) {
  2047. + delete[] errmsg_;
  2048. + }
  2049. +
  2050. + size_t size = strlen(msg) + 1;
  2051. + errmsg_ = new char[size];
  2052. + memcpy(errmsg_, msg, size);
  2053. + }
  2054. +
  2055. + const char* ErrorMessage() const {
  2056. + return errmsg_;
  2057. + }
  2058. +
  2059. + private:
  2060. + char *errmsg_;
  2061. +};
  2062. +
  2063. +NAN_INLINE void NanAsyncExecute (uv_work_t* req) {
  2064. + NanAsyncWorker *worker = static_cast<NanAsyncWorker*>(req->data);
  2065. + worker->Execute();
  2066. +}
  2067. +
  2068. +NAN_INLINE void NanAsyncExecuteComplete (uv_work_t* req) {
  2069. + NanAsyncWorker* worker = static_cast<NanAsyncWorker*>(req->data);
  2070. + worker->WorkComplete();
  2071. + delete worker;
  2072. +}
  2073. +
  2074. +NAN_INLINE void NanAsyncQueueWorker (NanAsyncWorker* worker) {
  2075. + uv_queue_work(
  2076. + uv_default_loop()
  2077. + , &worker->request
  2078. + , NanAsyncExecute
  2079. + , (uv_after_work_cb)NanAsyncExecuteComplete
  2080. + );
  2081. +}
  2082. +
  2083. +//// Base 64 ////
  2084. +
  2085. +#define _nan_base64_encoded_size(size) ((size + 2 - ((size + 2) % 3)) / 3 * 4)
  2086. +
  2087. +// Doesn't check for padding at the end. Can be 1-2 bytes over.
  2088. +NAN_INLINE size_t _nan_base64_decoded_size_fast(size_t size) {
  2089. + size_t remainder = size % 4;
  2090. +
  2091. + size = (size / 4) * 3;
  2092. + if (remainder) {
  2093. + if (size == 0 && remainder == 1) {
  2094. + // special case: 1-byte input cannot be decoded
  2095. + size = 0;
  2096. + } else {
  2097. + // non-padded input, add 1 or 2 extra bytes
  2098. + size += 1 + (remainder == 3);
  2099. + }
  2100. + }
  2101. +
  2102. + return size;
  2103. +}
  2104. +
  2105. +template<typename T>
  2106. +NAN_INLINE size_t _nan_base64_decoded_size(
  2107. + const T* src
  2108. + , size_t size
  2109. +) {
  2110. + if (size == 0)
  2111. + return 0;
  2112. +
  2113. + if (src[size - 1] == '=')
  2114. + size--;
  2115. + if (size > 0 && src[size - 1] == '=')
  2116. + size--;
  2117. +
  2118. + return _nan_base64_decoded_size_fast(size);
  2119. +}
  2120. +
  2121. +// supports regular and URL-safe base64
  2122. +static const int _nan_unbase64_table[] = {
  2123. + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -1, -1, -2, -1, -1
  2124. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2125. + , -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, 62, -1, 63
  2126. + , 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1
  2127. + , -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14
  2128. + , 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, 63
  2129. + , -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40
  2130. + , 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
  2131. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2132. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2133. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2134. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2135. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2136. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2137. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2138. + , -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
  2139. +};
  2140. +
  2141. +#define _nan_unbase64(x) _nan_unbase64_table[(uint8_t)(x)]
  2142. +
  2143. +template<typename T> static size_t _nan_base64_decode(
  2144. + char* buf
  2145. + , size_t len
  2146. + , const T* src
  2147. + , const size_t srcLen
  2148. +) {
  2149. + char* dst = buf;
  2150. + char* dstEnd = buf + len;
  2151. + const T* srcEnd = src + srcLen;
  2152. +
  2153. + while (src < srcEnd && dst < dstEnd) {
  2154. + ptrdiff_t remaining = srcEnd - src;
  2155. + char a, b, c, d;
  2156. +
  2157. + while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
  2158. + if (remaining == 0 || *src == '=') break;
  2159. + a = _nan_unbase64(*src++);
  2160. +
  2161. + while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
  2162. + if (remaining <= 1 || *src == '=') break;
  2163. + b = _nan_unbase64(*src++);
  2164. +
  2165. + *dst++ = (a << 2) | ((b & 0x30) >> 4);
  2166. + if (dst == dstEnd) break;
  2167. +
  2168. + while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
  2169. + if (remaining <= 2 || *src == '=') break;
  2170. + c = _nan_unbase64(*src++);
  2171. +
  2172. + *dst++ = ((b & 0x0F) << 4) | ((c & 0x3C) >> 2);
  2173. + if (dst == dstEnd) break;
  2174. +
  2175. + while (_nan_unbase64(*src) < 0 && src < srcEnd) src++, remaining--;
  2176. + if (remaining <= 3 || *src == '=') break;
  2177. + d = _nan_unbase64(*src++);
  2178. +
  2179. + *dst++ = ((c & 0x03) << 6) | (d & 0x3F);
  2180. + }
  2181. +
  2182. + return dst - buf;
  2183. +}
  2184. +
  2185. +//// HEX ////
  2186. +
  2187. +template<typename T> unsigned _nan_hex2bin(T c) {
  2188. + if (c >= '0' && c <= '9') return c - '0';
  2189. + if (c >= 'A' && c <= 'F') return 10 + (c - 'A');
  2190. + if (c >= 'a' && c <= 'f') return 10 + (c - 'a');
  2191. + return static_cast<unsigned>(-1);
  2192. +}
  2193. +
  2194. +template<typename T> static size_t _nan_hex_decode(
  2195. + char* buf
  2196. + , size_t len
  2197. + , const T* src
  2198. + , const size_t srcLen
  2199. +) {
  2200. + size_t i;
  2201. + for (i = 0; i < len && i * 2 + 1 < srcLen; ++i) {
  2202. + unsigned a = _nan_hex2bin(src[i * 2 + 0]);
  2203. + unsigned b = _nan_hex2bin(src[i * 2 + 1]);
  2204. + if (!~a || !~b) return i;
  2205. + buf[i] = a * 16 + b;
  2206. + }
  2207. +
  2208. + return i;
  2209. +}
  2210. +
  2211. +static bool _NanGetExternalParts(
  2212. + v8::Handle<v8::Value> val
  2213. + , const char** data
  2214. + , size_t* len
  2215. +) {
  2216. + if (node::Buffer::HasInstance(val)) {
  2217. + *data = node::Buffer::Data(val.As<v8::Object>());
  2218. + *len = node::Buffer::Length(val.As<v8::Object>());
  2219. + return true;
  2220. + }
  2221. +
  2222. + assert(val->IsString());
  2223. + v8::Local<v8::String> str = NanNew(val.As<v8::String>());
  2224. +
  2225. + if (str->IsExternalAscii()) {
  2226. + const v8::String::ExternalAsciiStringResource* ext;
  2227. + ext = str->GetExternalAsciiStringResource();
  2228. + *data = ext->data();
  2229. + *len = ext->length();
  2230. + return true;
  2231. +
  2232. + } else if (str->IsExternal()) {
  2233. + const v8::String::ExternalStringResource* ext;
  2234. + ext = str->GetExternalStringResource();
  2235. + *data = reinterpret_cast<const char*>(ext->data());
  2236. + *len = ext->length();
  2237. + return true;
  2238. + }
  2239. +
  2240. + return false;
  2241. +}
  2242. +
  2243. +namespace Nan {
  2244. + enum Encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
  2245. +}
  2246. +
  2247. +/* NAN_DEPRECATED */ NAN_INLINE void* _NanRawString(
  2248. + v8::Handle<v8::Value> from
  2249. + , enum Nan::Encoding encoding
  2250. + , size_t *datalen
  2251. + , void *buf
  2252. + , size_t buflen
  2253. + , int flags
  2254. +) {
  2255. + NanScope();
  2256. +
  2257. + size_t sz_;
  2258. + size_t term_len = !(flags & v8::String::NO_NULL_TERMINATION);
  2259. + char *data = NULL;
  2260. + size_t len;
  2261. + bool is_extern = _NanGetExternalParts(
  2262. + from
  2263. + , const_cast<const char**>(&data)
  2264. + , &len);
  2265. +
  2266. + if (is_extern && !term_len) {
  2267. + NanSetPointerSafe(datalen, len);
  2268. + return data;
  2269. + }
  2270. +
  2271. + v8::Local<v8::String> toStr = from->ToString();
  2272. +
  2273. + char *to = static_cast<char *>(buf);
  2274. +
  2275. + switch (encoding) {
  2276. + case Nan::ASCII:
  2277. +#if NODE_MODULE_VERSION < 0x000C
  2278. + sz_ = toStr->Length();
  2279. + if (to == NULL) {
  2280. + to = new char[sz_ + term_len];
  2281. + } else {
  2282. + assert(buflen >= sz_ + term_len && "too small buffer");
  2283. + }
  2284. + NanSetPointerSafe<size_t>(
  2285. + datalen
  2286. + , toStr->WriteAscii(to, 0, static_cast<int>(sz_ + term_len), flags));
  2287. + return to;
  2288. +#endif
  2289. + case Nan::BINARY:
  2290. + case Nan::BUFFER:
  2291. + sz_ = toStr->Length();
  2292. + if (to == NULL) {
  2293. + to = new char[sz_ + term_len];
  2294. + } else {
  2295. + assert(buflen >= sz_ + term_len && "too small buffer");
  2296. + }
  2297. +#if NODE_MODULE_VERSION < 0x000C
  2298. + {
  2299. + uint16_t* twobytebuf = new uint16_t[sz_ + term_len];
  2300. +
  2301. + size_t len = toStr->Write(twobytebuf, 0,
  2302. + static_cast<int>(sz_ + term_len), flags);
  2303. +
  2304. + for (size_t i = 0; i < sz_ + term_len && i < len + term_len; i++) {
  2305. + unsigned char *b = reinterpret_cast<unsigned char*>(&twobytebuf[i]);
  2306. + to[i] = *b;
  2307. + }
  2308. +
  2309. + NanSetPointerSafe<size_t>(datalen, len);
  2310. +
  2311. + delete[] twobytebuf;
  2312. + return to;
  2313. + }
  2314. +#else
  2315. + NanSetPointerSafe<size_t>(
  2316. + datalen,
  2317. + toStr->WriteOneByte(
  2318. + reinterpret_cast<uint8_t *>(to)
  2319. + , 0
  2320. + , static_cast<int>(sz_ + term_len)
  2321. + , flags));
  2322. + return to;
  2323. +#endif
  2324. + case Nan::UTF8:
  2325. + sz_ = toStr->Utf8Length();
  2326. + if (to == NULL) {
  2327. + to = new char[sz_ + term_len];
  2328. + } else {
  2329. + assert(buflen >= sz_ + term_len && "too small buffer");
  2330. + }
  2331. + NanSetPointerSafe<size_t>(
  2332. + datalen
  2333. + , toStr->WriteUtf8(to, static_cast<int>(sz_ + term_len)
  2334. + , NULL, flags)
  2335. + - term_len);
  2336. + return to;
  2337. + case Nan::BASE64:
  2338. + {
  2339. + v8::String::Value value(toStr);
  2340. + sz_ = _nan_base64_decoded_size(*value, value.length());
  2341. + if (to == NULL) {
  2342. + to = new char[sz_ + term_len];
  2343. + } else {
  2344. + assert(buflen >= sz_ + term_len);
  2345. + }
  2346. + NanSetPointerSafe<size_t>(
  2347. + datalen
  2348. + , _nan_base64_decode(to, sz_, *value, value.length()));
  2349. + if (term_len) {
  2350. + to[sz_] = '\0';
  2351. + }
  2352. + return to;
  2353. + }
  2354. + case Nan::UCS2:
  2355. + {
  2356. + sz_ = toStr->Length();
  2357. + if (to == NULL) {
  2358. + to = new char[(sz_ + term_len) * 2];
  2359. + } else {
  2360. + assert(buflen >= (sz_ + term_len) * 2 && "too small buffer");
  2361. + }
  2362. +
  2363. + int bc = 2 * toStr->Write(
  2364. + reinterpret_cast<uint16_t *>(to)
  2365. + , 0
  2366. + , static_cast<int>(sz_ + term_len)
  2367. + , flags);
  2368. + NanSetPointerSafe<size_t>(datalen, bc);
  2369. + return to;
  2370. + }
  2371. + case Nan::HEX:
  2372. + {
  2373. + v8::String::Value value(toStr);
  2374. + sz_ = value.length();
  2375. + assert(!(sz_ & 1) && "bad hex data");
  2376. + if (to == NULL) {
  2377. + to = new char[sz_ / 2 + term_len];
  2378. + } else {
  2379. + assert(buflen >= sz_ / 2 + term_len && "too small buffer");
  2380. + }
  2381. + NanSetPointerSafe<size_t>(
  2382. + datalen
  2383. + , _nan_hex_decode(to, sz_ / 2, *value, value.length()));
  2384. + }
  2385. + if (term_len) {
  2386. + to[sz_ / 2] = '\0';
  2387. + }
  2388. + return to;
  2389. + default:
  2390. + assert(0 && "unknown encoding");
  2391. + }
  2392. + return to;
  2393. +}
  2394. +
  2395. +NAN_DEPRECATED NAN_INLINE void* NanRawString(
  2396. + v8::Handle<v8::Value> from
  2397. + , enum Nan::Encoding encoding
  2398. + , size_t *datalen
  2399. + , void *buf
  2400. + , size_t buflen
  2401. + , int flags
  2402. +) {
  2403. + return _NanRawString(from, encoding, datalen, buf, buflen, flags);
  2404. +}
  2405. +
  2406. +
  2407. +NAN_DEPRECATED NAN_INLINE char* NanCString(
  2408. + v8::Handle<v8::Value> from
  2409. + , size_t *datalen
  2410. + , char *buf = NULL
  2411. + , size_t buflen = 0
  2412. + , int flags = v8::String::NO_OPTIONS
  2413. +) {
  2414. + return static_cast<char *>(
  2415. + _NanRawString(from, Nan::UTF8, datalen, buf, buflen, flags)
  2416. + );
  2417. +}
  2418. +
  2419. +NAN_INLINE void NanSetPrototypeTemplate(
  2420. + v8::Local<v8::FunctionTemplate> templ
  2421. + , const char *name
  2422. + , v8::Handle<v8::Data> value
  2423. +) {
  2424. + NanSetTemplate(templ->PrototypeTemplate(), name, value);
  2425. +}
  2426. +
  2427. +NAN_INLINE void NanSetPrototypeTemplate(
  2428. + v8::Local<v8::FunctionTemplate> templ
  2429. + , v8::Handle<v8::String> name
  2430. + , v8::Handle<v8::Data> value
  2431. + , v8::PropertyAttribute attributes
  2432. +) {
  2433. + NanSetTemplate(templ->PrototypeTemplate(), name, value, attributes);
  2434. +}
  2435. +
  2436. +NAN_INLINE void NanSetInstanceTemplate(
  2437. + v8::Local<v8::FunctionTemplate> templ
  2438. + , const char *name
  2439. + , v8::Handle<v8::Data> value
  2440. +) {
  2441. + NanSetTemplate(templ->InstanceTemplate(), name, value);
  2442. +}
  2443. +
  2444. +NAN_INLINE void NanSetInstanceTemplate(
  2445. + v8::Local<v8::FunctionTemplate> templ
  2446. + , v8::Handle<v8::String> name
  2447. + , v8::Handle<v8::Data> value
  2448. + , v8::PropertyAttribute attributes
  2449. +) {
  2450. + NanSetTemplate(templ->InstanceTemplate(), name, value, attributes);
  2451. +}
  2452. +
  2453. +#endif // NAN_H_