ganach-cli 6.12.2

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ganach-cli might be problematic. Click here for more details.

Files changed (1098) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +381 -0
  3. package/cli.js +245 -0
  4. package/node_modules/@types/bn.js/LICENSE +21 -0
  5. package/node_modules/@types/bn.js/README.md +16 -0
  6. package/node_modules/@types/bn.js/index.d.ts +586 -0
  7. package/node_modules/@types/bn.js/package.json +40 -0
  8. package/node_modules/@types/node/LICENSE +21 -0
  9. package/node_modules/@types/node/README.md +16 -0
  10. package/node_modules/@types/node/assert.d.ts +84 -0
  11. package/node_modules/@types/node/async_hooks.d.ts +226 -0
  12. package/node_modules/@types/node/base.d.ts +20 -0
  13. package/node_modules/@types/node/buffer.d.ts +22 -0
  14. package/node_modules/@types/node/child_process.d.ts +505 -0
  15. package/node_modules/@types/node/cluster.d.ts +262 -0
  16. package/node_modules/@types/node/console.d.ts +133 -0
  17. package/node_modules/@types/node/constants.d.ts +8 -0
  18. package/node_modules/@types/node/crypto.d.ts +707 -0
  19. package/node_modules/@types/node/dgram.d.ts +141 -0
  20. package/node_modules/@types/node/dns.d.ts +371 -0
  21. package/node_modules/@types/node/domain.d.ts +24 -0
  22. package/node_modules/@types/node/events.d.ts +83 -0
  23. package/node_modules/@types/node/fs/promises.d.ts +539 -0
  24. package/node_modules/@types/node/fs.d.ts +2156 -0
  25. package/node_modules/@types/node/globals.d.ts +606 -0
  26. package/node_modules/@types/node/globals.global.d.ts +1 -0
  27. package/node_modules/@types/node/http.d.ts +414 -0
  28. package/node_modules/@types/node/http2.d.ts +952 -0
  29. package/node_modules/@types/node/https.d.ts +37 -0
  30. package/node_modules/@types/node/index.d.ts +61 -0
  31. package/node_modules/@types/node/inspector.d.ts +3041 -0
  32. package/node_modules/@types/node/module.d.ts +52 -0
  33. package/node_modules/@types/node/net.d.ts +268 -0
  34. package/node_modules/@types/node/os.d.ts +239 -0
  35. package/node_modules/@types/node/package.json +255 -0
  36. package/node_modules/@types/node/path.d.ts +153 -0
  37. package/node_modules/@types/node/perf_hooks.d.ts +271 -0
  38. package/node_modules/@types/node/process.d.ts +406 -0
  39. package/node_modules/@types/node/punycode.d.ts +12 -0
  40. package/node_modules/@types/node/querystring.d.ts +28 -0
  41. package/node_modules/@types/node/readline.d.ts +171 -0
  42. package/node_modules/@types/node/repl.d.ts +387 -0
  43. package/node_modules/@types/node/stream.d.ts +351 -0
  44. package/node_modules/@types/node/string_decoder.d.ts +7 -0
  45. package/node_modules/@types/node/timers.d.ts +16 -0
  46. package/node_modules/@types/node/tls.d.ts +777 -0
  47. package/node_modules/@types/node/trace_events.d.ts +61 -0
  48. package/node_modules/@types/node/ts3.4/assert.d.ts +57 -0
  49. package/node_modules/@types/node/ts3.4/base.d.ts +56 -0
  50. package/node_modules/@types/node/ts3.4/globals.global.d.ts +1 -0
  51. package/node_modules/@types/node/ts3.4/index.d.ts +8 -0
  52. package/node_modules/@types/node/ts3.6/base.d.ts +23 -0
  53. package/node_modules/@types/node/ts3.6/index.d.ts +8 -0
  54. package/node_modules/@types/node/tty.d.ts +66 -0
  55. package/node_modules/@types/node/url.d.ts +110 -0
  56. package/node_modules/@types/node/util.d.ts +196 -0
  57. package/node_modules/@types/node/v8.d.ts +187 -0
  58. package/node_modules/@types/node/vm.d.ts +146 -0
  59. package/node_modules/@types/node/wasi.d.ts +52 -0
  60. package/node_modules/@types/node/worker_threads.d.ts +192 -0
  61. package/node_modules/@types/node/zlib.d.ts +352 -0
  62. package/node_modules/@types/pbkdf2/LICENSE +21 -0
  63. package/node_modules/@types/pbkdf2/README.md +16 -0
  64. package/node_modules/@types/pbkdf2/index.d.ts +41 -0
  65. package/node_modules/@types/pbkdf2/package.json +30 -0
  66. package/node_modules/@types/secp256k1/LICENSE +21 -0
  67. package/node_modules/@types/secp256k1/README.md +16 -0
  68. package/node_modules/@types/secp256k1/index.d.ts +141 -0
  69. package/node_modules/@types/secp256k1/package.json +30 -0
  70. package/node_modules/ansi-regex/index.js +14 -0
  71. package/node_modules/ansi-regex/license +9 -0
  72. package/node_modules/ansi-regex/package.json +57 -0
  73. package/node_modules/ansi-regex/readme.md +87 -0
  74. package/node_modules/ansi-styles/index.js +165 -0
  75. package/node_modules/ansi-styles/license +9 -0
  76. package/node_modules/ansi-styles/package.json +60 -0
  77. package/node_modules/ansi-styles/readme.md +147 -0
  78. package/node_modules/base-x/LICENSE.md +22 -0
  79. package/node_modules/base-x/README.md +67 -0
  80. package/node_modules/base-x/package.json +53 -0
  81. package/node_modules/base-x/src/index.d.ts +10 -0
  82. package/node_modules/base-x/src/index.js +123 -0
  83. package/node_modules/blakejs/.npmignore +1 -0
  84. package/node_modules/blakejs/.travis.yml +3 -0
  85. package/node_modules/blakejs/README.md +112 -0
  86. package/node_modules/blakejs/blake2b.js +273 -0
  87. package/node_modules/blakejs/blake2s.js +190 -0
  88. package/node_modules/blakejs/generated_test_vectors.txt +100 -0
  89. package/node_modules/blakejs/index.js +15 -0
  90. package/node_modules/blakejs/package.json +27 -0
  91. package/node_modules/blakejs/test_blake2b.js +88 -0
  92. package/node_modules/blakejs/test_blake2s.js +80 -0
  93. package/node_modules/blakejs/util.js +81 -0
  94. package/node_modules/bn.js/README.md +221 -0
  95. package/node_modules/bn.js/lib/bn.js +3433 -0
  96. package/node_modules/bn.js/package.json +40 -0
  97. package/node_modules/bn.js/util/genCombMulTo.js +65 -0
  98. package/node_modules/bn.js/util/genCombMulTo10.js +65 -0
  99. package/node_modules/brorand/.npmignore +2 -0
  100. package/node_modules/brorand/README.md +26 -0
  101. package/node_modules/brorand/index.js +65 -0
  102. package/node_modules/brorand/package.json +35 -0
  103. package/node_modules/brorand/test/api-test.js +8 -0
  104. package/node_modules/browserify-aes/.travis.yml +15 -0
  105. package/node_modules/browserify-aes/LICENSE +21 -0
  106. package/node_modules/browserify-aes/README.md +19 -0
  107. package/node_modules/browserify-aes/aes.js +228 -0
  108. package/node_modules/browserify-aes/authCipher.js +117 -0
  109. package/node_modules/browserify-aes/browser.js +13 -0
  110. package/node_modules/browserify-aes/decrypter.js +124 -0
  111. package/node_modules/browserify-aes/encrypter.js +114 -0
  112. package/node_modules/browserify-aes/ghash.js +89 -0
  113. package/node_modules/browserify-aes/incr32.js +15 -0
  114. package/node_modules/browserify-aes/index.js +7 -0
  115. package/node_modules/browserify-aes/modes/cbc.js +17 -0
  116. package/node_modules/browserify-aes/modes/cfb.js +33 -0
  117. package/node_modules/browserify-aes/modes/cfb1.js +42 -0
  118. package/node_modules/browserify-aes/modes/cfb8.js +25 -0
  119. package/node_modules/browserify-aes/modes/ctr.js +30 -0
  120. package/node_modules/browserify-aes/modes/ecb.js +7 -0
  121. package/node_modules/browserify-aes/modes/index.js +18 -0
  122. package/node_modules/browserify-aes/modes/list.json +191 -0
  123. package/node_modules/browserify-aes/modes/ofb.js +16 -0
  124. package/node_modules/browserify-aes/package.json +47 -0
  125. package/node_modules/browserify-aes/streamCipher.js +27 -0
  126. package/node_modules/bs58/README.md +71 -0
  127. package/node_modules/bs58/index.js +4 -0
  128. package/node_modules/bs58/package.json +41 -0
  129. package/node_modules/bs58check/LICENSE +21 -0
  130. package/node_modules/bs58check/README.md +27 -0
  131. package/node_modules/bs58check/base.js +50 -0
  132. package/node_modules/bs58check/index.js +12 -0
  133. package/node_modules/bs58check/package.json +56 -0
  134. package/node_modules/buffer-from/LICENSE +21 -0
  135. package/node_modules/buffer-from/index.js +69 -0
  136. package/node_modules/buffer-from/package.json +23 -0
  137. package/node_modules/buffer-from/readme.md +69 -0
  138. package/node_modules/buffer-xor/.npmignore +1 -0
  139. package/node_modules/buffer-xor/.travis.yml +9 -0
  140. package/node_modules/buffer-xor/LICENSE +21 -0
  141. package/node_modules/buffer-xor/README.md +41 -0
  142. package/node_modules/buffer-xor/index.js +10 -0
  143. package/node_modules/buffer-xor/inline.js +1 -0
  144. package/node_modules/buffer-xor/inplace.js +9 -0
  145. package/node_modules/buffer-xor/package.json +41 -0
  146. package/node_modules/buffer-xor/test/fixtures.json +23 -0
  147. package/node_modules/buffer-xor/test/index.js +38 -0
  148. package/node_modules/camelcase/index.d.ts +63 -0
  149. package/node_modules/camelcase/index.js +76 -0
  150. package/node_modules/camelcase/license +9 -0
  151. package/node_modules/camelcase/package.json +47 -0
  152. package/node_modules/camelcase/readme.md +99 -0
  153. package/node_modules/cipher-base/.eslintrc +3 -0
  154. package/node_modules/cipher-base/.npmignore +1 -0
  155. package/node_modules/cipher-base/.travis.yml +6 -0
  156. package/node_modules/cipher-base/LICENSE +21 -0
  157. package/node_modules/cipher-base/README.md +17 -0
  158. package/node_modules/cipher-base/index.js +99 -0
  159. package/node_modules/cipher-base/package.json +36 -0
  160. package/node_modules/cipher-base/test.js +111 -0
  161. package/node_modules/cliui/LICENSE.txt +14 -0
  162. package/node_modules/cliui/README.md +115 -0
  163. package/node_modules/cliui/index.js +324 -0
  164. package/node_modules/cliui/package.json +71 -0
  165. package/node_modules/color-convert/LICENSE +21 -0
  166. package/node_modules/color-convert/README.md +68 -0
  167. package/node_modules/color-convert/conversions.js +868 -0
  168. package/node_modules/color-convert/index.js +78 -0
  169. package/node_modules/color-convert/package.json +50 -0
  170. package/node_modules/color-convert/route.js +97 -0
  171. package/node_modules/color-name/.eslintrc.json +43 -0
  172. package/node_modules/color-name/.npmignore +107 -0
  173. package/node_modules/color-name/LICENSE +8 -0
  174. package/node_modules/color-name/README.md +11 -0
  175. package/node_modules/color-name/index.js +152 -0
  176. package/node_modules/color-name/package.json +29 -0
  177. package/node_modules/color-name/test.js +7 -0
  178. package/node_modules/create-hash/.travis.yml +17 -0
  179. package/node_modules/create-hash/LICENSE +21 -0
  180. package/node_modules/create-hash/README.md +19 -0
  181. package/node_modules/create-hash/browser.js +30 -0
  182. package/node_modules/create-hash/index.js +1 -0
  183. package/node_modules/create-hash/md5.js +5 -0
  184. package/node_modules/create-hash/package.json +43 -0
  185. package/node_modules/create-hash/test.js +41 -0
  186. package/node_modules/create-hmac/LICENSE +21 -0
  187. package/node_modules/create-hmac/README.md +20 -0
  188. package/node_modules/create-hmac/browser.js +62 -0
  189. package/node_modules/create-hmac/index.js +1 -0
  190. package/node_modules/create-hmac/legacy.js +46 -0
  191. package/node_modules/create-hmac/package.json +49 -0
  192. package/node_modules/cross-spawn/LICENSE +21 -0
  193. package/node_modules/cross-spawn/README.md +94 -0
  194. package/node_modules/cross-spawn/index.js +39 -0
  195. package/node_modules/cross-spawn/lib/enoent.js +59 -0
  196. package/node_modules/cross-spawn/lib/parse.js +125 -0
  197. package/node_modules/cross-spawn/lib/util/escape.js +45 -0
  198. package/node_modules/cross-spawn/lib/util/readShebang.js +32 -0
  199. package/node_modules/cross-spawn/lib/util/resolveCommand.js +47 -0
  200. package/node_modules/cross-spawn/package.json +80 -0
  201. package/node_modules/decamelize/index.js +13 -0
  202. package/node_modules/decamelize/license +21 -0
  203. package/node_modules/decamelize/package.json +42 -0
  204. package/node_modules/decamelize/readme.md +48 -0
  205. package/node_modules/elliptic/README.md +238 -0
  206. package/node_modules/elliptic/lib/elliptic/curve/base.js +376 -0
  207. package/node_modules/elliptic/lib/elliptic/curve/edwards.js +432 -0
  208. package/node_modules/elliptic/lib/elliptic/curve/index.js +8 -0
  209. package/node_modules/elliptic/lib/elliptic/curve/mont.js +178 -0
  210. package/node_modules/elliptic/lib/elliptic/curve/short.js +937 -0
  211. package/node_modules/elliptic/lib/elliptic/curves.js +206 -0
  212. package/node_modules/elliptic/lib/elliptic/ec/index.js +241 -0
  213. package/node_modules/elliptic/lib/elliptic/ec/key.js +118 -0
  214. package/node_modules/elliptic/lib/elliptic/ec/signature.js +166 -0
  215. package/node_modules/elliptic/lib/elliptic/eddsa/index.js +118 -0
  216. package/node_modules/elliptic/lib/elliptic/eddsa/key.js +95 -0
  217. package/node_modules/elliptic/lib/elliptic/eddsa/signature.js +65 -0
  218. package/node_modules/elliptic/lib/elliptic/precomputed/secp256k1.js +780 -0
  219. package/node_modules/elliptic/lib/elliptic/utils.js +119 -0
  220. package/node_modules/elliptic/lib/elliptic.js +13 -0
  221. package/node_modules/elliptic/package.json +62 -0
  222. package/node_modules/emoji-regex/LICENSE-MIT.txt +20 -0
  223. package/node_modules/emoji-regex/README.md +73 -0
  224. package/node_modules/emoji-regex/es2015/index.js +6 -0
  225. package/node_modules/emoji-regex/es2015/text.js +6 -0
  226. package/node_modules/emoji-regex/index.d.ts +5 -0
  227. package/node_modules/emoji-regex/index.js +6 -0
  228. package/node_modules/emoji-regex/package.json +55 -0
  229. package/node_modules/emoji-regex/text.js +6 -0
  230. package/node_modules/end-of-stream/LICENSE +21 -0
  231. package/node_modules/end-of-stream/README.md +54 -0
  232. package/node_modules/end-of-stream/index.js +94 -0
  233. package/node_modules/end-of-stream/package.json +41 -0
  234. package/node_modules/ethereum-cryptography/LICENSE +26 -0
  235. package/node_modules/ethereum-cryptography/README.md +560 -0
  236. package/node_modules/ethereum-cryptography/aes.d.ts +4 -0
  237. package/node_modules/ethereum-cryptography/aes.d.ts.map +1 -0
  238. package/node_modules/ethereum-cryptography/aes.js +38 -0
  239. package/node_modules/ethereum-cryptography/aes.js.map +1 -0
  240. package/node_modules/ethereum-cryptography/bip39/index.d.ts +8 -0
  241. package/node_modules/ethereum-cryptography/bip39/index.d.ts.map +1 -0
  242. package/node_modules/ethereum-cryptography/bip39/index.js +71 -0
  243. package/node_modules/ethereum-cryptography/bip39/index.js.map +1 -0
  244. package/node_modules/ethereum-cryptography/bip39/wordlists/czech.d.ts +2 -0
  245. package/node_modules/ethereum-cryptography/bip39/wordlists/czech.d.ts.map +1 -0
  246. package/node_modules/ethereum-cryptography/bip39/wordlists/czech.js +2053 -0
  247. package/node_modules/ethereum-cryptography/bip39/wordlists/czech.js.map +1 -0
  248. package/node_modules/ethereum-cryptography/bip39/wordlists/english.d.ts +2 -0
  249. package/node_modules/ethereum-cryptography/bip39/wordlists/english.d.ts.map +1 -0
  250. package/node_modules/ethereum-cryptography/bip39/wordlists/english.js +2053 -0
  251. package/node_modules/ethereum-cryptography/bip39/wordlists/english.js.map +1 -0
  252. package/node_modules/ethereum-cryptography/bip39/wordlists/french.d.ts +2 -0
  253. package/node_modules/ethereum-cryptography/bip39/wordlists/french.d.ts.map +1 -0
  254. package/node_modules/ethereum-cryptography/bip39/wordlists/french.js +2053 -0
  255. package/node_modules/ethereum-cryptography/bip39/wordlists/french.js.map +1 -0
  256. package/node_modules/ethereum-cryptography/bip39/wordlists/italian.d.ts +2 -0
  257. package/node_modules/ethereum-cryptography/bip39/wordlists/italian.d.ts.map +1 -0
  258. package/node_modules/ethereum-cryptography/bip39/wordlists/italian.js +2053 -0
  259. package/node_modules/ethereum-cryptography/bip39/wordlists/italian.js.map +1 -0
  260. package/node_modules/ethereum-cryptography/bip39/wordlists/japanese.d.ts +2 -0
  261. package/node_modules/ethereum-cryptography/bip39/wordlists/japanese.d.ts.map +1 -0
  262. package/node_modules/ethereum-cryptography/bip39/wordlists/japanese.js +2053 -0
  263. package/node_modules/ethereum-cryptography/bip39/wordlists/japanese.js.map +1 -0
  264. package/node_modules/ethereum-cryptography/bip39/wordlists/korean.d.ts +2 -0
  265. package/node_modules/ethereum-cryptography/bip39/wordlists/korean.d.ts.map +1 -0
  266. package/node_modules/ethereum-cryptography/bip39/wordlists/korean.js +2053 -0
  267. package/node_modules/ethereum-cryptography/bip39/wordlists/korean.js.map +1 -0
  268. package/node_modules/ethereum-cryptography/bip39/wordlists/simplified-chinese.d.ts +2 -0
  269. package/node_modules/ethereum-cryptography/bip39/wordlists/simplified-chinese.d.ts.map +1 -0
  270. package/node_modules/ethereum-cryptography/bip39/wordlists/simplified-chinese.js +2053 -0
  271. package/node_modules/ethereum-cryptography/bip39/wordlists/simplified-chinese.js.map +1 -0
  272. package/node_modules/ethereum-cryptography/bip39/wordlists/spanish.d.ts +2 -0
  273. package/node_modules/ethereum-cryptography/bip39/wordlists/spanish.d.ts.map +1 -0
  274. package/node_modules/ethereum-cryptography/bip39/wordlists/spanish.js +2053 -0
  275. package/node_modules/ethereum-cryptography/bip39/wordlists/spanish.js.map +1 -0
  276. package/node_modules/ethereum-cryptography/bip39/wordlists/traditional-chinese.d.ts +2 -0
  277. package/node_modules/ethereum-cryptography/bip39/wordlists/traditional-chinese.d.ts.map +1 -0
  278. package/node_modules/ethereum-cryptography/bip39/wordlists/traditional-chinese.js +2053 -0
  279. package/node_modules/ethereum-cryptography/bip39/wordlists/traditional-chinese.js.map +1 -0
  280. package/node_modules/ethereum-cryptography/blake2b.d.ts +3 -0
  281. package/node_modules/ethereum-cryptography/blake2b.d.ts.map +1 -0
  282. package/node_modules/ethereum-cryptography/blake2b.js +12 -0
  283. package/node_modules/ethereum-cryptography/blake2b.js.map +1 -0
  284. package/node_modules/ethereum-cryptography/hash-utils.d.ts +4 -0
  285. package/node_modules/ethereum-cryptography/hash-utils.d.ts.map +1 -0
  286. package/node_modules/ethereum-cryptography/hash-utils.js +11 -0
  287. package/node_modules/ethereum-cryptography/hash-utils.js.map +1 -0
  288. package/node_modules/ethereum-cryptography/hdkey.d.ts +3 -0
  289. package/node_modules/ethereum-cryptography/hdkey.d.ts.map +1 -0
  290. package/node_modules/ethereum-cryptography/hdkey.js +5 -0
  291. package/node_modules/ethereum-cryptography/hdkey.js.map +1 -0
  292. package/node_modules/ethereum-cryptography/index.d.ts +1 -0
  293. package/node_modules/ethereum-cryptography/index.d.ts.map +1 -0
  294. package/node_modules/ethereum-cryptography/index.js +3 -0
  295. package/node_modules/ethereum-cryptography/index.js.map +1 -0
  296. package/node_modules/ethereum-cryptography/keccak.d.ts +6 -0
  297. package/node_modules/ethereum-cryptography/keccak.d.ts.map +1 -0
  298. package/node_modules/ethereum-cryptography/keccak.js +17 -0
  299. package/node_modules/ethereum-cryptography/keccak.js.map +1 -0
  300. package/node_modules/ethereum-cryptography/package.json +131 -0
  301. package/node_modules/ethereum-cryptography/pbkdf2.d.ts +4 -0
  302. package/node_modules/ethereum-cryptography/pbkdf2.d.ts.map +1 -0
  303. package/node_modules/ethereum-cryptography/pbkdf2.js +27 -0
  304. package/node_modules/ethereum-cryptography/pbkdf2.js.map +1 -0
  305. package/node_modules/ethereum-cryptography/pure/hdkey.d.ts +37 -0
  306. package/node_modules/ethereum-cryptography/pure/hdkey.d.ts.map +1 -0
  307. package/node_modules/ethereum-cryptography/pure/hdkey.js +5 -0
  308. package/node_modules/ethereum-cryptography/pure/hdkey.js.map +1 -0
  309. package/node_modules/ethereum-cryptography/pure/ripemd160.d.ts +3 -0
  310. package/node_modules/ethereum-cryptography/pure/ripemd160.d.ts.map +1 -0
  311. package/node_modules/ethereum-cryptography/pure/ripemd160.js +6 -0
  312. package/node_modules/ethereum-cryptography/pure/ripemd160.js.map +1 -0
  313. package/node_modules/ethereum-cryptography/pure/sha256.d.ts +3 -0
  314. package/node_modules/ethereum-cryptography/pure/sha256.d.ts.map +1 -0
  315. package/node_modules/ethereum-cryptography/pure/sha256.js +6 -0
  316. package/node_modules/ethereum-cryptography/pure/sha256.js.map +1 -0
  317. package/node_modules/ethereum-cryptography/pure/shims/hdkey-crypto.d.ts +13 -0
  318. package/node_modules/ethereum-cryptography/pure/shims/hdkey-crypto.d.ts.map +1 -0
  319. package/node_modules/ethereum-cryptography/pure/shims/hdkey-crypto.js +37 -0
  320. package/node_modules/ethereum-cryptography/pure/shims/hdkey-crypto.js.map +1 -0
  321. package/node_modules/ethereum-cryptography/pure/shims/hdkey-secp256k1v3.d.ts +2 -0
  322. package/node_modules/ethereum-cryptography/pure/shims/hdkey-secp256k1v3.d.ts.map +1 -0
  323. package/node_modules/ethereum-cryptography/pure/shims/hdkey-secp256k1v3.js +7 -0
  324. package/node_modules/ethereum-cryptography/pure/shims/hdkey-secp256k1v3.js.map +1 -0
  325. package/node_modules/ethereum-cryptography/pure/vendor/hdkey-without-crypto.js +255 -0
  326. package/node_modules/ethereum-cryptography/random.d.ts +4 -0
  327. package/node_modules/ethereum-cryptography/random.d.ts.map +1 -0
  328. package/node_modules/ethereum-cryptography/random.js +20 -0
  329. package/node_modules/ethereum-cryptography/random.js.map +1 -0
  330. package/node_modules/ethereum-cryptography/ripemd160.d.ts +3 -0
  331. package/node_modules/ethereum-cryptography/ripemd160.d.ts.map +1 -0
  332. package/node_modules/ethereum-cryptography/ripemd160.js +11 -0
  333. package/node_modules/ethereum-cryptography/ripemd160.js.map +1 -0
  334. package/node_modules/ethereum-cryptography/scrypt.d.ts +5 -0
  335. package/node_modules/ethereum-cryptography/scrypt.d.ts.map +1 -0
  336. package/node_modules/ethereum-cryptography/scrypt.js +59 -0
  337. package/node_modules/ethereum-cryptography/scrypt.js.map +1 -0
  338. package/node_modules/ethereum-cryptography/secp256k1.d.ts +4 -0
  339. package/node_modules/ethereum-cryptography/secp256k1.d.ts.map +1 -0
  340. package/node_modules/ethereum-cryptography/secp256k1.js +75 -0
  341. package/node_modules/ethereum-cryptography/secp256k1.js.map +1 -0
  342. package/node_modules/ethereum-cryptography/sha256.d.ts +3 -0
  343. package/node_modules/ethereum-cryptography/sha256.d.ts.map +1 -0
  344. package/node_modules/ethereum-cryptography/sha256.js +9 -0
  345. package/node_modules/ethereum-cryptography/sha256.js.map +1 -0
  346. package/node_modules/ethereum-cryptography/shims/hdkey-crypto.d.ts +2 -0
  347. package/node_modules/ethereum-cryptography/shims/hdkey-crypto.d.ts.map +1 -0
  348. package/node_modules/ethereum-cryptography/shims/hdkey-crypto.js +7 -0
  349. package/node_modules/ethereum-cryptography/shims/hdkey-crypto.js.map +1 -0
  350. package/node_modules/ethereum-cryptography/shims/hdkey-secp256k1v3.d.ts +13 -0
  351. package/node_modules/ethereum-cryptography/shims/hdkey-secp256k1v3.d.ts.map +1 -0
  352. package/node_modules/ethereum-cryptography/shims/hdkey-secp256k1v3.js +47 -0
  353. package/node_modules/ethereum-cryptography/shims/hdkey-secp256k1v3.js.map +1 -0
  354. package/node_modules/ethereum-cryptography/src/aes.ts +52 -0
  355. package/node_modules/ethereum-cryptography/src/bip39/index.ts +40 -0
  356. package/node_modules/ethereum-cryptography/src/bip39/wordlists/czech.ts +2050 -0
  357. package/node_modules/ethereum-cryptography/src/bip39/wordlists/english.ts +2050 -0
  358. package/node_modules/ethereum-cryptography/src/bip39/wordlists/french.ts +2050 -0
  359. package/node_modules/ethereum-cryptography/src/bip39/wordlists/italian.ts +2050 -0
  360. package/node_modules/ethereum-cryptography/src/bip39/wordlists/japanese.ts +2050 -0
  361. package/node_modules/ethereum-cryptography/src/bip39/wordlists/korean.ts +2050 -0
  362. package/node_modules/ethereum-cryptography/src/bip39/wordlists/simplified-chinese.ts +2050 -0
  363. package/node_modules/ethereum-cryptography/src/bip39/wordlists/spanish.ts +2050 -0
  364. package/node_modules/ethereum-cryptography/src/bip39/wordlists/traditional-chinese.ts +2050 -0
  365. package/node_modules/ethereum-cryptography/src/blake2b.ts +9 -0
  366. package/node_modules/ethereum-cryptography/src/hash-utils.ts +11 -0
  367. package/node_modules/ethereum-cryptography/src/hdkey.ts +5 -0
  368. package/node_modules/ethereum-cryptography/src/index.ts +3 -0
  369. package/node_modules/ethereum-cryptography/src/keccak.ts +19 -0
  370. package/node_modules/ethereum-cryptography/src/pbkdf2.ts +37 -0
  371. package/node_modules/ethereum-cryptography/src/pure/hdkey.ts +36 -0
  372. package/node_modules/ethereum-cryptography/src/pure/ripemd160.ts +5 -0
  373. package/node_modules/ethereum-cryptography/src/pure/sha256.ts +5 -0
  374. package/node_modules/ethereum-cryptography/src/pure/shims/hdkey-crypto.ts +42 -0
  375. package/node_modules/ethereum-cryptography/src/pure/shims/hdkey-secp256k1v3.ts +1 -0
  376. package/node_modules/ethereum-cryptography/src/pure/vendor/hdkey-without-crypto.js +255 -0
  377. package/node_modules/ethereum-cryptography/src/random.ts +18 -0
  378. package/node_modules/ethereum-cryptography/src/ripemd160.ts +7 -0
  379. package/node_modules/ethereum-cryptography/src/scrypt.ts +24 -0
  380. package/node_modules/ethereum-cryptography/src/secp256k1.ts +24 -0
  381. package/node_modules/ethereum-cryptography/src/sha256.ts +5 -0
  382. package/node_modules/ethereum-cryptography/src/shims/hdkey-crypto.ts +1 -0
  383. package/node_modules/ethereum-cryptography/src/shims/hdkey-secp256k1v3.ts +49 -0
  384. package/node_modules/ethereum-cryptography/src/vendor/bip39-without-wordlists.js +206 -0
  385. package/node_modules/ethereum-cryptography/src/vendor/hdkey-without-crypto.js +255 -0
  386. package/node_modules/ethereum-cryptography/vendor/bip39-without-wordlists.js +206 -0
  387. package/node_modules/ethereum-cryptography/vendor/hdkey-without-crypto.js +255 -0
  388. package/node_modules/ethereumjs-util/LICENSE +373 -0
  389. package/node_modules/ethereumjs-util/README.md +32 -0
  390. package/node_modules/ethereumjs-util/dist/account.d.ts +80 -0
  391. package/node_modules/ethereumjs-util/dist/account.js +168 -0
  392. package/node_modules/ethereumjs-util/dist/account.js.map +1 -0
  393. package/node_modules/ethereumjs-util/dist/bytes.d.ts +68 -0
  394. package/node_modules/ethereumjs-util/dist/bytes.js +158 -0
  395. package/node_modules/ethereumjs-util/dist/bytes.js.map +1 -0
  396. package/node_modules/ethereumjs-util/dist/constants.d.ts +34 -0
  397. package/node_modules/ethereumjs-util/dist/constants.js +37 -0
  398. package/node_modules/ethereumjs-util/dist/constants.js.map +1 -0
  399. package/node_modules/ethereumjs-util/dist/hash.d.ts +30 -0
  400. package/node_modules/ethereumjs-util/dist/hash.js +84 -0
  401. package/node_modules/ethereumjs-util/dist/hash.js.map +1 -0
  402. package/node_modules/ethereumjs-util/dist/index.d.ts +39 -0
  403. package/node_modules/ethereumjs-util/dist/index.js +46 -0
  404. package/node_modules/ethereumjs-util/dist/index.js.map +1 -0
  405. package/node_modules/ethereumjs-util/dist/object.d.ts +12 -0
  406. package/node_modules/ethereumjs-util/dist/object.js +106 -0
  407. package/node_modules/ethereumjs-util/dist/object.js.map +1 -0
  408. package/node_modules/ethereumjs-util/dist/secp256k1v3-adapter.d.ts +178 -0
  409. package/node_modules/ethereumjs-util/dist/secp256k1v3-adapter.js +300 -0
  410. package/node_modules/ethereumjs-util/dist/secp256k1v3-adapter.js.map +1 -0
  411. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/der.d.ts +1 -0
  412. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/der.js +634 -0
  413. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/der.js.map +1 -0
  414. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/index.d.ts +5 -0
  415. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/index.js +61 -0
  416. package/node_modules/ethereumjs-util/dist/secp256k1v3-lib/index.js.map +1 -0
  417. package/node_modules/ethereumjs-util/dist/signature.d.ts +37 -0
  418. package/node_modules/ethereumjs-util/dist/signature.js +107 -0
  419. package/node_modules/ethereumjs-util/dist/signature.js.map +1 -0
  420. package/node_modules/ethereumjs-util/package.json +131 -0
  421. package/node_modules/ethjs-util/LICENSE +21 -0
  422. package/node_modules/ethjs-util/README.md +143 -0
  423. package/node_modules/ethjs-util/dist/ethjs-util.js +2420 -0
  424. package/node_modules/ethjs-util/dist/ethjs-util.js.map +1 -0
  425. package/node_modules/ethjs-util/dist/ethjs-util.min.js +1 -0
  426. package/node_modules/ethjs-util/internals/webpack/webpack.config.js +58 -0
  427. package/node_modules/ethjs-util/lib/index.js +220 -0
  428. package/node_modules/ethjs-util/lib/tests/test.index.js +357 -0
  429. package/node_modules/ethjs-util/package.json +229 -0
  430. package/node_modules/ethjs-util/src/index.js +203 -0
  431. package/node_modules/ethjs-util/src/tests/test.index.js +382 -0
  432. package/node_modules/evp_bytestokey/LICENSE +21 -0
  433. package/node_modules/evp_bytestokey/README.md +51 -0
  434. package/node_modules/evp_bytestokey/index.js +45 -0
  435. package/node_modules/evp_bytestokey/package.json +49 -0
  436. package/node_modules/execa/index.js +361 -0
  437. package/node_modules/execa/lib/errname.js +39 -0
  438. package/node_modules/execa/lib/stdio.js +41 -0
  439. package/node_modules/execa/license +9 -0
  440. package/node_modules/execa/package.json +73 -0
  441. package/node_modules/execa/readme.md +327 -0
  442. package/node_modules/find-up/index.js +46 -0
  443. package/node_modules/find-up/license +9 -0
  444. package/node_modules/find-up/package.json +54 -0
  445. package/node_modules/find-up/readme.md +87 -0
  446. package/node_modules/get-caller-file/LICENSE.md +6 -0
  447. package/node_modules/get-caller-file/README.md +41 -0
  448. package/node_modules/get-caller-file/index.d.ts +2 -0
  449. package/node_modules/get-caller-file/index.js +22 -0
  450. package/node_modules/get-caller-file/index.js.map +1 -0
  451. package/node_modules/get-caller-file/package.json +46 -0
  452. package/node_modules/get-stream/buffer-stream.js +51 -0
  453. package/node_modules/get-stream/index.js +50 -0
  454. package/node_modules/get-stream/license +9 -0
  455. package/node_modules/get-stream/package.json +50 -0
  456. package/node_modules/get-stream/readme.md +123 -0
  457. package/node_modules/hash-base/LICENSE +21 -0
  458. package/node_modules/hash-base/README.md +48 -0
  459. package/node_modules/hash-base/index.js +95 -0
  460. package/node_modules/hash-base/package.json +46 -0
  461. package/node_modules/hash.js/.eslintrc.js +41 -0
  462. package/node_modules/hash.js/.travis.yml +10 -0
  463. package/node_modules/hash.js/README.md +48 -0
  464. package/node_modules/hash.js/lib/hash/common.js +92 -0
  465. package/node_modules/hash.js/lib/hash/hmac.js +47 -0
  466. package/node_modules/hash.js/lib/hash/ripemd.js +146 -0
  467. package/node_modules/hash.js/lib/hash/sha/1.js +74 -0
  468. package/node_modules/hash.js/lib/hash/sha/224.js +30 -0
  469. package/node_modules/hash.js/lib/hash/sha/256.js +105 -0
  470. package/node_modules/hash.js/lib/hash/sha/384.js +35 -0
  471. package/node_modules/hash.js/lib/hash/sha/512.js +330 -0
  472. package/node_modules/hash.js/lib/hash/sha/common.js +49 -0
  473. package/node_modules/hash.js/lib/hash/sha.js +7 -0
  474. package/node_modules/hash.js/lib/hash/utils.js +278 -0
  475. package/node_modules/hash.js/lib/hash.d.ts +106 -0
  476. package/node_modules/hash.js/lib/hash.js +15 -0
  477. package/node_modules/hash.js/package.json +39 -0
  478. package/node_modules/hash.js/test/hash-test.js +140 -0
  479. package/node_modules/hash.js/test/hmac-test.js +62 -0
  480. package/node_modules/hmac-drbg/.npmignore +2 -0
  481. package/node_modules/hmac-drbg/.travis.yml +11 -0
  482. package/node_modules/hmac-drbg/README.md +48 -0
  483. package/node_modules/hmac-drbg/lib/hmac-drbg.js +113 -0
  484. package/node_modules/hmac-drbg/package.json +36 -0
  485. package/node_modules/hmac-drbg/test/drbg-test.js +91 -0
  486. package/node_modules/hmac-drbg/test/fixtures/hmac-drbg-nist.json +332 -0
  487. package/node_modules/inherits/LICENSE +16 -0
  488. package/node_modules/inherits/README.md +42 -0
  489. package/node_modules/inherits/inherits.js +9 -0
  490. package/node_modules/inherits/inherits_browser.js +27 -0
  491. package/node_modules/inherits/package.json +33 -0
  492. package/node_modules/invert-kv/index.js +15 -0
  493. package/node_modules/invert-kv/license +9 -0
  494. package/node_modules/invert-kv/package.json +36 -0
  495. package/node_modules/invert-kv/readme.md +25 -0
  496. package/node_modules/is-fullwidth-code-point/index.js +46 -0
  497. package/node_modules/is-fullwidth-code-point/license +21 -0
  498. package/node_modules/is-fullwidth-code-point/package.json +49 -0
  499. package/node_modules/is-fullwidth-code-point/readme.md +39 -0
  500. package/node_modules/is-hex-prefixed/.editorconfig +7 -0
  501. package/node_modules/is-hex-prefixed/.gitattributes +106 -0
  502. package/node_modules/is-hex-prefixed/.npmignore +8 -0
  503. package/node_modules/is-hex-prefixed/.travis.yml +5 -0
  504. package/node_modules/is-hex-prefixed/CHANGELOG.md +7 -0
  505. package/node_modules/is-hex-prefixed/LICENSE +21 -0
  506. package/node_modules/is-hex-prefixed/README.md +101 -0
  507. package/node_modules/is-hex-prefixed/package.json +43 -0
  508. package/node_modules/is-hex-prefixed/src/index.js +13 -0
  509. package/node_modules/is-hex-prefixed/src/tests/test.index.js +66 -0
  510. package/node_modules/is-stream/index.js +21 -0
  511. package/node_modules/is-stream/license +21 -0
  512. package/node_modules/is-stream/package.json +42 -0
  513. package/node_modules/is-stream/readme.md +42 -0
  514. package/node_modules/isexe/.npmignore +2 -0
  515. package/node_modules/isexe/LICENSE +15 -0
  516. package/node_modules/isexe/README.md +51 -0
  517. package/node_modules/isexe/index.js +57 -0
  518. package/node_modules/isexe/mode.js +41 -0
  519. package/node_modules/isexe/package.json +35 -0
  520. package/node_modules/isexe/test/basic.js +221 -0
  521. package/node_modules/isexe/windows.js +42 -0
  522. package/node_modules/keccak/LICENSE +21 -0
  523. package/node_modules/keccak/README.md +64 -0
  524. package/node_modules/keccak/binding.gyp +78 -0
  525. package/node_modules/keccak/bindings.js +1 -0
  526. package/node_modules/keccak/index.js +5 -0
  527. package/node_modules/keccak/js.js +1 -0
  528. package/node_modules/keccak/lib/api/index.js +27 -0
  529. package/node_modules/keccak/lib/api/keccak.js +77 -0
  530. package/node_modules/keccak/lib/api/shake.js +68 -0
  531. package/node_modules/keccak/lib/keccak-state-reference.js +143 -0
  532. package/node_modules/keccak/lib/keccak-state-unroll.js +186 -0
  533. package/node_modules/keccak/lib/keccak.js +68 -0
  534. package/node_modules/keccak/package.json +41 -0
  535. package/node_modules/keccak/prebuilds/darwin-x64/node.napi.node +0 -0
  536. package/node_modules/keccak/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  537. package/node_modules/keccak/prebuilds/linux-x64/node.napi.musl.node +0 -0
  538. package/node_modules/keccak/prebuilds/win32-x64/node.napi.node +0 -0
  539. package/node_modules/keccak/src/README.md +28 -0
  540. package/node_modules/keccak/src/addon.cc +98 -0
  541. package/node_modules/keccak/src/libkeccak-32/KeccakP-1600-SnP.h +38 -0
  542. package/node_modules/keccak/src/libkeccak-32/KeccakP-1600-inplace32BI.c +1162 -0
  543. package/node_modules/keccak/src/libkeccak-32/KeccakSponge-common.h +35 -0
  544. package/node_modules/keccak/src/libkeccak-32/KeccakSponge.inc +311 -0
  545. package/node_modules/keccak/src/libkeccak-32/KeccakSpongeWidth1600.c +54 -0
  546. package/node_modules/keccak/src/libkeccak-32/KeccakSpongeWidth1600.h +31 -0
  547. package/node_modules/keccak/src/libkeccak-32/SnP-Relaned.h +140 -0
  548. package/node_modules/keccak/src/libkeccak-32/align.h +32 -0
  549. package/node_modules/keccak/src/libkeccak-32/brg_endian.h +143 -0
  550. package/node_modules/keccak/src/libkeccak-64/KeccakP-1600-64.macros +745 -0
  551. package/node_modules/keccak/src/libkeccak-64/KeccakP-1600-SnP.h +51 -0
  552. package/node_modules/keccak/src/libkeccak-64/KeccakP-1600-opt64-config.h +6 -0
  553. package/node_modules/keccak/src/libkeccak-64/KeccakP-1600-opt64.c +564 -0
  554. package/node_modules/keccak/src/libkeccak-64/KeccakP-1600-unrolling.macros +302 -0
  555. package/node_modules/keccak/src/libkeccak-64/KeccakSponge-common.h +35 -0
  556. package/node_modules/keccak/src/libkeccak-64/KeccakSponge.inc +311 -0
  557. package/node_modules/keccak/src/libkeccak-64/KeccakSpongeWidth1600.c +54 -0
  558. package/node_modules/keccak/src/libkeccak-64/KeccakSpongeWidth1600.h +31 -0
  559. package/node_modules/keccak/src/libkeccak-64/SnP-Relaned.h +140 -0
  560. package/node_modules/keccak/src/libkeccak-64/align.h +32 -0
  561. package/node_modules/keccak/src/libkeccak-64/brg_endian.h +143 -0
  562. package/node_modules/lcid/index.js +23 -0
  563. package/node_modules/lcid/lcid.json +203 -0
  564. package/node_modules/lcid/license +9 -0
  565. package/node_modules/lcid/package.json +51 -0
  566. package/node_modules/lcid/readme.md +35 -0
  567. package/node_modules/locate-path/index.js +24 -0
  568. package/node_modules/locate-path/license +9 -0
  569. package/node_modules/locate-path/package.json +48 -0
  570. package/node_modules/locate-path/readme.md +99 -0
  571. package/node_modules/map-age-cleaner/dist/index.d.ts +20 -0
  572. package/node_modules/map-age-cleaner/dist/index.js +92 -0
  573. package/node_modules/map-age-cleaner/license +9 -0
  574. package/node_modules/map-age-cleaner/package.json +63 -0
  575. package/node_modules/map-age-cleaner/readme.md +67 -0
  576. package/node_modules/md5.js/LICENSE +21 -0
  577. package/node_modules/md5.js/README.md +29 -0
  578. package/node_modules/md5.js/index.js +146 -0
  579. package/node_modules/md5.js/package.json +42 -0
  580. package/node_modules/mem/index.d.ts +96 -0
  581. package/node_modules/mem/index.js +88 -0
  582. package/node_modules/mem/license +9 -0
  583. package/node_modules/mem/package.json +50 -0
  584. package/node_modules/mem/readme.md +167 -0
  585. package/node_modules/mimic-fn/index.d.ts +54 -0
  586. package/node_modules/mimic-fn/index.js +13 -0
  587. package/node_modules/mimic-fn/license +9 -0
  588. package/node_modules/mimic-fn/package.json +46 -0
  589. package/node_modules/mimic-fn/readme.md +69 -0
  590. package/node_modules/minimalistic-assert/LICENSE +13 -0
  591. package/node_modules/minimalistic-assert/index.js +11 -0
  592. package/node_modules/minimalistic-assert/package.json +23 -0
  593. package/node_modules/minimalistic-assert/readme.md +4 -0
  594. package/node_modules/minimalistic-crypto-utils/.npmignore +2 -0
  595. package/node_modules/minimalistic-crypto-utils/.travis.yml +11 -0
  596. package/node_modules/minimalistic-crypto-utils/README.md +47 -0
  597. package/node_modules/minimalistic-crypto-utils/lib/utils.js +58 -0
  598. package/node_modules/minimalistic-crypto-utils/package.json +31 -0
  599. package/node_modules/minimalistic-crypto-utils/test/utils-test.js +28 -0
  600. package/node_modules/nice-try/LICENSE +21 -0
  601. package/node_modules/nice-try/README.md +32 -0
  602. package/node_modules/nice-try/package.json +37 -0
  603. package/node_modules/nice-try/src/index.js +12 -0
  604. package/node_modules/node-addon-api/.editorconfig +8 -0
  605. package/node_modules/node-addon-api/.travis.yml +62 -0
  606. package/node_modules/node-addon-api/CHANGELOG.md +412 -0
  607. package/node_modules/node-addon-api/CODE_OF_CONDUCT.md +4 -0
  608. package/node_modules/node-addon-api/CONTRIBUTING.md +66 -0
  609. package/node_modules/node-addon-api/LICENSE.md +13 -0
  610. package/node_modules/node-addon-api/README.md +203 -0
  611. package/node_modules/node-addon-api/appveyor.yml +48 -0
  612. package/node_modules/node-addon-api/doc/Doxyfile +2450 -0
  613. package/node_modules/node-addon-api/doc/array_buffer.md +129 -0
  614. package/node_modules/node-addon-api/doc/async_context.md +86 -0
  615. package/node_modules/node-addon-api/doc/async_operations.md +31 -0
  616. package/node_modules/node-addon-api/doc/async_progress_worker.md +344 -0
  617. package/node_modules/node-addon-api/doc/async_worker.md +398 -0
  618. package/node_modules/node-addon-api/doc/basic_types.md +423 -0
  619. package/node_modules/node-addon-api/doc/bigint.md +92 -0
  620. package/node_modules/node-addon-api/doc/boolean.md +64 -0
  621. package/node_modules/node-addon-api/doc/buffer.md +140 -0
  622. package/node_modules/node-addon-api/doc/callback_scope.md +54 -0
  623. package/node_modules/node-addon-api/doc/callbackinfo.md +97 -0
  624. package/node_modules/node-addon-api/doc/checker-tool.md +32 -0
  625. package/node_modules/node-addon-api/doc/class_property_descriptor.md +117 -0
  626. package/node_modules/node-addon-api/doc/cmake-js.md +68 -0
  627. package/node_modules/node-addon-api/doc/conversion-tool.md +28 -0
  628. package/node_modules/node-addon-api/doc/creating_a_release.md +62 -0
  629. package/node_modules/node-addon-api/doc/dataview.md +244 -0
  630. package/node_modules/node-addon-api/doc/date.md +68 -0
  631. package/node_modules/node-addon-api/doc/env.md +63 -0
  632. package/node_modules/node-addon-api/doc/error.md +115 -0
  633. package/node_modules/node-addon-api/doc/error_handling.md +186 -0
  634. package/node_modules/node-addon-api/doc/escapable_handle_scope.md +82 -0
  635. package/node_modules/node-addon-api/doc/external.md +59 -0
  636. package/node_modules/node-addon-api/doc/function.md +294 -0
  637. package/node_modules/node-addon-api/doc/function_reference.md +238 -0
  638. package/node_modules/node-addon-api/doc/generator.md +13 -0
  639. package/node_modules/node-addon-api/doc/handle_scope.md +65 -0
  640. package/node_modules/node-addon-api/doc/memory_management.md +27 -0
  641. package/node_modules/node-addon-api/doc/node-gyp.md +82 -0
  642. package/node_modules/node-addon-api/doc/number.md +163 -0
  643. package/node_modules/node-addon-api/doc/object.md +236 -0
  644. package/node_modules/node-addon-api/doc/object_lifetime_management.md +83 -0
  645. package/node_modules/node-addon-api/doc/object_reference.md +117 -0
  646. package/node_modules/node-addon-api/doc/object_wrap.md +557 -0
  647. package/node_modules/node-addon-api/doc/prebuild_tools.md +16 -0
  648. package/node_modules/node-addon-api/doc/promises.md +74 -0
  649. package/node_modules/node-addon-api/doc/property_descriptor.md +231 -0
  650. package/node_modules/node-addon-api/doc/range_error.md +59 -0
  651. package/node_modules/node-addon-api/doc/reference.md +111 -0
  652. package/node_modules/node-addon-api/doc/setup.md +82 -0
  653. package/node_modules/node-addon-api/doc/string.md +89 -0
  654. package/node_modules/node-addon-api/doc/symbol.md +44 -0
  655. package/node_modules/node-addon-api/doc/threadsafe_function.md +320 -0
  656. package/node_modules/node-addon-api/doc/type_error.md +59 -0
  657. package/node_modules/node-addon-api/doc/typed_array.md +74 -0
  658. package/node_modules/node-addon-api/doc/typed_array_of.md +133 -0
  659. package/node_modules/node-addon-api/doc/value.md +278 -0
  660. package/node_modules/node-addon-api/doc/version_management.md +43 -0
  661. package/node_modules/node-addon-api/doc/working_with_javascript_values.md +14 -0
  662. package/node_modules/node-addon-api/external-napi/node_api.h +7 -0
  663. package/node_modules/node-addon-api/index.js +45 -0
  664. package/node_modules/node-addon-api/napi-inl.deprecated.h +192 -0
  665. package/node_modules/node-addon-api/napi-inl.h +4412 -0
  666. package/node_modules/node-addon-api/napi.h +2173 -0
  667. package/node_modules/node-addon-api/package.json +248 -0
  668. package/node_modules/node-addon-api/src/node_api.cc +3655 -0
  669. package/node_modules/node-addon-api/src/node_api.gyp +21 -0
  670. package/node_modules/node-addon-api/src/node_api.h +588 -0
  671. package/node_modules/node-addon-api/src/node_api_types.h +115 -0
  672. package/node_modules/node-addon-api/src/node_internals.cc +142 -0
  673. package/node_modules/node-addon-api/src/node_internals.h +157 -0
  674. package/node_modules/node-addon-api/src/nothing.c +0 -0
  675. package/node_modules/node-addon-api/src/util-inl.h +38 -0
  676. package/node_modules/node-addon-api/src/util.h +7 -0
  677. package/node_modules/node-addon-api/tools/README.md +67 -0
  678. package/node_modules/node-addon-api/tools/check-napi.js +100 -0
  679. package/node_modules/node-addon-api/tools/conversion.js +313 -0
  680. package/node_modules/node-gyp-build/LICENSE +21 -0
  681. package/node_modules/node-gyp-build/README.md +58 -0
  682. package/node_modules/node-gyp-build/bin.js +77 -0
  683. package/node_modules/node-gyp-build/build-test.js +19 -0
  684. package/node_modules/node-gyp-build/index.js +166 -0
  685. package/node_modules/node-gyp-build/optional.js +7 -0
  686. package/node_modules/node-gyp-build/package.json +33 -0
  687. package/node_modules/npm-run-path/index.js +39 -0
  688. package/node_modules/npm-run-path/license +21 -0
  689. package/node_modules/npm-run-path/package.json +49 -0
  690. package/node_modules/npm-run-path/readme.md +81 -0
  691. package/node_modules/once/LICENSE +15 -0
  692. package/node_modules/once/README.md +79 -0
  693. package/node_modules/once/once.js +42 -0
  694. package/node_modules/once/package.json +37 -0
  695. package/node_modules/os-locale/index.js +114 -0
  696. package/node_modules/os-locale/license +9 -0
  697. package/node_modules/os-locale/package.json +49 -0
  698. package/node_modules/os-locale/readme.md +71 -0
  699. package/node_modules/p-defer/index.js +11 -0
  700. package/node_modules/p-defer/license +21 -0
  701. package/node_modules/p-defer/package.json +45 -0
  702. package/node_modules/p-defer/readme.md +47 -0
  703. package/node_modules/p-finally/index.js +15 -0
  704. package/node_modules/p-finally/license +21 -0
  705. package/node_modules/p-finally/package.json +46 -0
  706. package/node_modules/p-finally/readme.md +47 -0
  707. package/node_modules/p-is-promise/index.d.ts +23 -0
  708. package/node_modules/p-is-promise/index.js +15 -0
  709. package/node_modules/p-is-promise/license +9 -0
  710. package/node_modules/p-is-promise/package.json +46 -0
  711. package/node_modules/p-is-promise/readme.md +43 -0
  712. package/node_modules/p-limit/index.d.ts +38 -0
  713. package/node_modules/p-limit/index.js +57 -0
  714. package/node_modules/p-limit/license +9 -0
  715. package/node_modules/p-limit/package.json +56 -0
  716. package/node_modules/p-limit/readme.md +101 -0
  717. package/node_modules/p-locate/index.js +34 -0
  718. package/node_modules/p-locate/license +9 -0
  719. package/node_modules/p-locate/package.json +55 -0
  720. package/node_modules/p-locate/readme.md +88 -0
  721. package/node_modules/p-try/index.d.ts +39 -0
  722. package/node_modules/p-try/index.js +9 -0
  723. package/node_modules/p-try/license +9 -0
  724. package/node_modules/p-try/package.json +46 -0
  725. package/node_modules/p-try/readme.md +58 -0
  726. package/node_modules/path-exists/index.js +17 -0
  727. package/node_modules/path-exists/license +21 -0
  728. package/node_modules/path-exists/package.json +44 -0
  729. package/node_modules/path-exists/readme.md +50 -0
  730. package/node_modules/path-key/index.js +13 -0
  731. package/node_modules/path-key/license +21 -0
  732. package/node_modules/path-key/package.json +43 -0
  733. package/node_modules/path-key/readme.md +51 -0
  734. package/node_modules/pbkdf2/LICENSE +21 -0
  735. package/node_modules/pbkdf2/README.md +30 -0
  736. package/node_modules/pbkdf2/browser.js +2 -0
  737. package/node_modules/pbkdf2/index.js +38 -0
  738. package/node_modules/pbkdf2/lib/async.js +102 -0
  739. package/node_modules/pbkdf2/lib/default-encoding.js +12 -0
  740. package/node_modules/pbkdf2/lib/precondition.js +19 -0
  741. package/node_modules/pbkdf2/lib/sync-browser.js +105 -0
  742. package/node_modules/pbkdf2/lib/sync.js +52 -0
  743. package/node_modules/pbkdf2/lib/to-buffer.js +13 -0
  744. package/node_modules/pbkdf2/package.json +74 -0
  745. package/node_modules/pump/.travis.yml +5 -0
  746. package/node_modules/pump/LICENSE +21 -0
  747. package/node_modules/pump/README.md +65 -0
  748. package/node_modules/pump/index.js +82 -0
  749. package/node_modules/pump/package.json +28 -0
  750. package/node_modules/pump/test-browser.js +66 -0
  751. package/node_modules/pump/test-node.js +53 -0
  752. package/node_modules/randombytes/.travis.yml +15 -0
  753. package/node_modules/randombytes/.zuul.yml +1 -0
  754. package/node_modules/randombytes/LICENSE +21 -0
  755. package/node_modules/randombytes/README.md +14 -0
  756. package/node_modules/randombytes/browser.js +50 -0
  757. package/node_modules/randombytes/index.js +1 -0
  758. package/node_modules/randombytes/package.json +40 -0
  759. package/node_modules/randombytes/test.js +81 -0
  760. package/node_modules/readable-stream/CONTRIBUTING.md +38 -0
  761. package/node_modules/readable-stream/GOVERNANCE.md +136 -0
  762. package/node_modules/readable-stream/LICENSE +47 -0
  763. package/node_modules/readable-stream/README.md +106 -0
  764. package/node_modules/readable-stream/errors-browser.js +127 -0
  765. package/node_modules/readable-stream/errors.js +116 -0
  766. package/node_modules/readable-stream/experimentalWarning.js +17 -0
  767. package/node_modules/readable-stream/lib/_stream_duplex.js +139 -0
  768. package/node_modules/readable-stream/lib/_stream_passthrough.js +39 -0
  769. package/node_modules/readable-stream/lib/_stream_readable.js +1124 -0
  770. package/node_modules/readable-stream/lib/_stream_transform.js +201 -0
  771. package/node_modules/readable-stream/lib/_stream_writable.js +697 -0
  772. package/node_modules/readable-stream/lib/internal/streams/async_iterator.js +207 -0
  773. package/node_modules/readable-stream/lib/internal/streams/buffer_list.js +210 -0
  774. package/node_modules/readable-stream/lib/internal/streams/destroy.js +105 -0
  775. package/node_modules/readable-stream/lib/internal/streams/end-of-stream.js +104 -0
  776. package/node_modules/readable-stream/lib/internal/streams/from-browser.js +3 -0
  777. package/node_modules/readable-stream/lib/internal/streams/from.js +64 -0
  778. package/node_modules/readable-stream/lib/internal/streams/pipeline.js +97 -0
  779. package/node_modules/readable-stream/lib/internal/streams/state.js +27 -0
  780. package/node_modules/readable-stream/lib/internal/streams/stream-browser.js +1 -0
  781. package/node_modules/readable-stream/lib/internal/streams/stream.js +1 -0
  782. package/node_modules/readable-stream/package.json +72 -0
  783. package/node_modules/readable-stream/readable-browser.js +9 -0
  784. package/node_modules/readable-stream/readable.js +16 -0
  785. package/node_modules/require-directory/.jshintrc +67 -0
  786. package/node_modules/require-directory/.npmignore +1 -0
  787. package/node_modules/require-directory/.travis.yml +3 -0
  788. package/node_modules/require-directory/LICENSE +22 -0
  789. package/node_modules/require-directory/README.markdown +184 -0
  790. package/node_modules/require-directory/index.js +86 -0
  791. package/node_modules/require-directory/package.json +44 -0
  792. package/node_modules/require-main-filename/LICENSE.txt +14 -0
  793. package/node_modules/require-main-filename/README.md +26 -0
  794. package/node_modules/require-main-filename/index.js +18 -0
  795. package/node_modules/require-main-filename/package.json +39 -0
  796. package/node_modules/ripemd160/LICENSE +21 -0
  797. package/node_modules/ripemd160/README.md +27 -0
  798. package/node_modules/ripemd160/index.js +163 -0
  799. package/node_modules/ripemd160/package.json +41 -0
  800. package/node_modules/rlp/LICENSE +373 -0
  801. package/node_modules/rlp/README.md +58 -0
  802. package/node_modules/rlp/bin/rlp +35 -0
  803. package/node_modules/rlp/dist/index.d.ts +25 -0
  804. package/node_modules/rlp/dist/index.js +249 -0
  805. package/node_modules/rlp/dist/index.js.map +1 -0
  806. package/node_modules/rlp/dist/types.d.ts +9 -0
  807. package/node_modules/rlp/dist/types.js +3 -0
  808. package/node_modules/rlp/dist/types.js.map +1 -0
  809. package/node_modules/rlp/package.json +84 -0
  810. package/node_modules/safe-buffer/LICENSE +21 -0
  811. package/node_modules/safe-buffer/README.md +584 -0
  812. package/node_modules/safe-buffer/index.d.ts +187 -0
  813. package/node_modules/safe-buffer/index.js +65 -0
  814. package/node_modules/safe-buffer/package.json +55 -0
  815. package/node_modules/scrypt-js/LICENSE.txt +22 -0
  816. package/node_modules/scrypt-js/README.md +222 -0
  817. package/node_modules/scrypt-js/index.html +291 -0
  818. package/node_modules/scrypt-js/package.json +31 -0
  819. package/node_modules/scrypt-js/scrypt.d.ts +23 -0
  820. package/node_modules/scrypt-js/scrypt.js +488 -0
  821. package/node_modules/scrypt-js/thirdparty/buffer.js +1381 -0
  822. package/node_modules/scrypt-js/thirdparty/setImmediate.js +175 -0
  823. package/node_modules/scrypt-js/thirdparty/unorm.js +442 -0
  824. package/node_modules/secp256k1/API.md +116 -0
  825. package/node_modules/secp256k1/LICENSE +24 -0
  826. package/node_modules/secp256k1/README.md +117 -0
  827. package/node_modules/secp256k1/binding.gyp +100 -0
  828. package/node_modules/secp256k1/bindings.js +2 -0
  829. package/node_modules/secp256k1/elliptic.js +1 -0
  830. package/node_modules/secp256k1/index.js +5 -0
  831. package/node_modules/secp256k1/lib/elliptic.js +402 -0
  832. package/node_modules/secp256k1/lib/index.js +336 -0
  833. package/node_modules/secp256k1/package.json +47 -0
  834. package/node_modules/secp256k1/prebuilds/darwin-x64/node.napi.node +0 -0
  835. package/node_modules/secp256k1/prebuilds/linux-x64/node.napi.glibc.node +0 -0
  836. package/node_modules/secp256k1/prebuilds/linux-x64/node.napi.musl.node +0 -0
  837. package/node_modules/secp256k1/prebuilds/win32-x64/node.napi.node +0 -0
  838. package/node_modules/secp256k1/src/addon.cc +10 -0
  839. package/node_modules/secp256k1/src/secp256k1/.travis.yml +100 -0
  840. package/node_modules/secp256k1/src/secp256k1/COPYING +19 -0
  841. package/node_modules/secp256k1/src/secp256k1/Makefile.am +183 -0
  842. package/node_modules/secp256k1/src/secp256k1/README.md +78 -0
  843. package/node_modules/secp256k1/src/secp256k1/SECURITY.md +15 -0
  844. package/node_modules/secp256k1/src/secp256k1/TODO +3 -0
  845. package/node_modules/secp256k1/src/secp256k1/autogen.sh +3 -0
  846. package/node_modules/secp256k1/src/secp256k1/build-aux/m4/ax_jni_include_dir.m4 +145 -0
  847. package/node_modules/secp256k1/src/secp256k1/build-aux/m4/ax_prog_cc_for_build.m4 +125 -0
  848. package/node_modules/secp256k1/src/secp256k1/build-aux/m4/bitcoin_secp.m4 +68 -0
  849. package/node_modules/secp256k1/src/secp256k1/configure.ac +591 -0
  850. package/node_modules/secp256k1/src/secp256k1/contrib/lax_der_parsing.c +150 -0
  851. package/node_modules/secp256k1/src/secp256k1/contrib/lax_der_parsing.h +91 -0
  852. package/node_modules/secp256k1/src/secp256k1/contrib/lax_der_privatekey_parsing.c +113 -0
  853. package/node_modules/secp256k1/src/secp256k1/contrib/lax_der_privatekey_parsing.h +90 -0
  854. package/node_modules/secp256k1/src/secp256k1/include/secp256k1.h +708 -0
  855. package/node_modules/secp256k1/src/secp256k1/include/secp256k1_ecdh.h +55 -0
  856. package/node_modules/secp256k1/src/secp256k1/include/secp256k1_preallocated.h +128 -0
  857. package/node_modules/secp256k1/src/secp256k1/include/secp256k1_recovery.h +110 -0
  858. package/node_modules/secp256k1/src/secp256k1/libsecp256k1.pc.in +13 -0
  859. package/node_modules/secp256k1/src/secp256k1/sage/group_prover.sage +322 -0
  860. package/node_modules/secp256k1/src/secp256k1/sage/secp256k1.sage +306 -0
  861. package/node_modules/secp256k1/src/secp256k1/sage/weierstrass_prover.sage +264 -0
  862. package/node_modules/secp256k1/src/secp256k1/src/asm/field_10x26_arm.s +913 -0
  863. package/node_modules/secp256k1/src/secp256k1/src/basic-config.h +38 -0
  864. package/node_modules/secp256k1/src/secp256k1/src/bench.h +124 -0
  865. package/node_modules/secp256k1/src/secp256k1/src/bench_ecdh.c +54 -0
  866. package/node_modules/secp256k1/src/secp256k1/src/bench_ecmult.c +207 -0
  867. package/node_modules/secp256k1/src/secp256k1/src/bench_internal.c +379 -0
  868. package/node_modules/secp256k1/src/secp256k1/src/bench_recover.c +60 -0
  869. package/node_modules/secp256k1/src/secp256k1/src/bench_sign.c +56 -0
  870. package/node_modules/secp256k1/src/secp256k1/src/bench_verify.c +112 -0
  871. package/node_modules/secp256k1/src/secp256k1/src/ecdsa.h +21 -0
  872. package/node_modules/secp256k1/src/secp256k1/src/ecdsa_impl.h +319 -0
  873. package/node_modules/secp256k1/src/secp256k1/src/eckey.h +25 -0
  874. package/node_modules/secp256k1/src/secp256k1/src/eckey_impl.h +100 -0
  875. package/node_modules/secp256k1/src/secp256k1/src/ecmult.h +48 -0
  876. package/node_modules/secp256k1/src/secp256k1/src/ecmult_const.h +20 -0
  877. package/node_modules/secp256k1/src/secp256k1/src/ecmult_const_impl.h +261 -0
  878. package/node_modules/secp256k1/src/secp256k1/src/ecmult_gen.h +50 -0
  879. package/node_modules/secp256k1/src/secp256k1/src/ecmult_gen_impl.h +211 -0
  880. package/node_modules/secp256k1/src/secp256k1/src/ecmult_impl.h +1216 -0
  881. package/node_modules/secp256k1/src/secp256k1/src/field.h +132 -0
  882. package/node_modules/secp256k1/src/secp256k1/src/field_10x26.h +50 -0
  883. package/node_modules/secp256k1/src/secp256k1/src/field_10x26_impl.h +1162 -0
  884. package/node_modules/secp256k1/src/secp256k1/src/field_5x52.h +49 -0
  885. package/node_modules/secp256k1/src/secp256k1/src/field_5x52_asm_impl.h +502 -0
  886. package/node_modules/secp256k1/src/secp256k1/src/field_5x52_impl.h +496 -0
  887. package/node_modules/secp256k1/src/secp256k1/src/field_5x52_int128_impl.h +279 -0
  888. package/node_modules/secp256k1/src/secp256k1/src/field_impl.h +318 -0
  889. package/node_modules/secp256k1/src/secp256k1/src/gen_context.c +87 -0
  890. package/node_modules/secp256k1/src/secp256k1/src/group.h +142 -0
  891. package/node_modules/secp256k1/src/secp256k1/src/group_impl.h +705 -0
  892. package/node_modules/secp256k1/src/secp256k1/src/hash.h +41 -0
  893. package/node_modules/secp256k1/src/secp256k1/src/hash_impl.h +283 -0
  894. package/node_modules/secp256k1/src/secp256k1/src/java/org/bitcoin/NativeSecp256k1.java +446 -0
  895. package/node_modules/secp256k1/src/secp256k1/src/java/org/bitcoin/NativeSecp256k1Test.java +225 -0
  896. package/node_modules/secp256k1/src/secp256k1/src/java/org/bitcoin/NativeSecp256k1Util.java +45 -0
  897. package/node_modules/secp256k1/src/secp256k1/src/java/org/bitcoin/Secp256k1Context.java +51 -0
  898. package/node_modules/secp256k1/src/secp256k1/src/java/org_bitcoin_NativeSecp256k1.c +379 -0
  899. package/node_modules/secp256k1/src/secp256k1/src/java/org_bitcoin_NativeSecp256k1.h +119 -0
  900. package/node_modules/secp256k1/src/secp256k1/src/java/org_bitcoin_Secp256k1Context.c +15 -0
  901. package/node_modules/secp256k1/src/secp256k1/src/java/org_bitcoin_Secp256k1Context.h +22 -0
  902. package/node_modules/secp256k1/src/secp256k1/src/modules/ecdh/Makefile.am.include +8 -0
  903. package/node_modules/secp256k1/src/secp256k1/src/modules/ecdh/main_impl.h +67 -0
  904. package/node_modules/secp256k1/src/secp256k1/src/modules/ecdh/tests_impl.h +132 -0
  905. package/node_modules/secp256k1/src/secp256k1/src/modules/recovery/Makefile.am.include +8 -0
  906. package/node_modules/secp256k1/src/secp256k1/src/modules/recovery/main_impl.h +193 -0
  907. package/node_modules/secp256k1/src/secp256k1/src/modules/recovery/tests_impl.h +393 -0
  908. package/node_modules/secp256k1/src/secp256k1/src/num.h +74 -0
  909. package/node_modules/secp256k1/src/secp256k1/src/num_gmp.h +20 -0
  910. package/node_modules/secp256k1/src/secp256k1/src/num_gmp_impl.h +288 -0
  911. package/node_modules/secp256k1/src/secp256k1/src/num_impl.h +24 -0
  912. package/node_modules/secp256k1/src/secp256k1/src/scalar.h +106 -0
  913. package/node_modules/secp256k1/src/secp256k1/src/scalar_4x64.h +19 -0
  914. package/node_modules/secp256k1/src/secp256k1/src/scalar_4x64_impl.h +949 -0
  915. package/node_modules/secp256k1/src/secp256k1/src/scalar_8x32.h +19 -0
  916. package/node_modules/secp256k1/src/secp256k1/src/scalar_8x32_impl.h +721 -0
  917. package/node_modules/secp256k1/src/secp256k1/src/scalar_impl.h +333 -0
  918. package/node_modules/secp256k1/src/secp256k1/src/scalar_low.h +15 -0
  919. package/node_modules/secp256k1/src/secp256k1/src/scalar_low_impl.h +117 -0
  920. package/node_modules/secp256k1/src/secp256k1/src/scratch.h +42 -0
  921. package/node_modules/secp256k1/src/secp256k1/src/scratch_impl.h +88 -0
  922. package/node_modules/secp256k1/src/secp256k1/src/secp256k1.c +690 -0
  923. package/node_modules/secp256k1/src/secp256k1/src/testrand.h +38 -0
  924. package/node_modules/secp256k1/src/secp256k1/src/testrand_impl.h +110 -0
  925. package/node_modules/secp256k1/src/secp256k1/src/tests.c +5310 -0
  926. package/node_modules/secp256k1/src/secp256k1/src/tests_exhaustive.c +511 -0
  927. package/node_modules/secp256k1/src/secp256k1/src/util.h +162 -0
  928. package/node_modules/secp256k1/src/secp256k1.cc +415 -0
  929. package/node_modules/secp256k1/src/secp256k1.h +63 -0
  930. package/node_modules/semver/LICENSE +15 -0
  931. package/node_modules/semver/README.md +412 -0
  932. package/node_modules/semver/bin/semver +160 -0
  933. package/node_modules/semver/package.json +32 -0
  934. package/node_modules/semver/range.bnf +16 -0
  935. package/node_modules/semver/semver.js +1483 -0
  936. package/node_modules/set-blocking/LICENSE.txt +14 -0
  937. package/node_modules/set-blocking/README.md +31 -0
  938. package/node_modules/set-blocking/index.js +7 -0
  939. package/node_modules/set-blocking/package.json +46 -0
  940. package/node_modules/setimmediate/LICENSE.txt +20 -0
  941. package/node_modules/setimmediate/package.json +34 -0
  942. package/node_modules/setimmediate/setImmediate.js +186 -0
  943. package/node_modules/sha.js/.travis.yml +17 -0
  944. package/node_modules/sha.js/LICENSE +49 -0
  945. package/node_modules/sha.js/README.md +44 -0
  946. package/node_modules/sha.js/bin.js +41 -0
  947. package/node_modules/sha.js/hash.js +81 -0
  948. package/node_modules/sha.js/index.js +15 -0
  949. package/node_modules/sha.js/package.json +34 -0
  950. package/node_modules/sha.js/sha.js +94 -0
  951. package/node_modules/sha.js/sha1.js +99 -0
  952. package/node_modules/sha.js/sha224.js +53 -0
  953. package/node_modules/sha.js/sha256.js +135 -0
  954. package/node_modules/sha.js/sha384.js +57 -0
  955. package/node_modules/sha.js/sha512.js +260 -0
  956. package/node_modules/sha.js/test/hash.js +75 -0
  957. package/node_modules/sha.js/test/test.js +100 -0
  958. package/node_modules/sha.js/test/vectors.js +72 -0
  959. package/node_modules/shebang-command/index.js +19 -0
  960. package/node_modules/shebang-command/license +21 -0
  961. package/node_modules/shebang-command/package.json +43 -0
  962. package/node_modules/shebang-command/readme.md +39 -0
  963. package/node_modules/shebang-regex/index.js +2 -0
  964. package/node_modules/shebang-regex/license +21 -0
  965. package/node_modules/shebang-regex/package.json +36 -0
  966. package/node_modules/shebang-regex/readme.md +29 -0
  967. package/node_modules/signal-exit/LICENSE.txt +16 -0
  968. package/node_modules/signal-exit/README.md +39 -0
  969. package/node_modules/signal-exit/index.js +163 -0
  970. package/node_modules/signal-exit/package.json +42 -0
  971. package/node_modules/signal-exit/signals.js +53 -0
  972. package/node_modules/source-map/LICENSE +28 -0
  973. package/node_modules/source-map/README.md +742 -0
  974. package/node_modules/source-map/dist/source-map.debug.js +3234 -0
  975. package/node_modules/source-map/dist/source-map.js +3233 -0
  976. package/node_modules/source-map/dist/source-map.min.js +2 -0
  977. package/node_modules/source-map/dist/source-map.min.js.map +1 -0
  978. package/node_modules/source-map/lib/array-set.js +121 -0
  979. package/node_modules/source-map/lib/base64-vlq.js +140 -0
  980. package/node_modules/source-map/lib/base64.js +67 -0
  981. package/node_modules/source-map/lib/binary-search.js +111 -0
  982. package/node_modules/source-map/lib/mapping-list.js +79 -0
  983. package/node_modules/source-map/lib/quick-sort.js +114 -0
  984. package/node_modules/source-map/lib/source-map-consumer.js +1145 -0
  985. package/node_modules/source-map/lib/source-map-generator.js +425 -0
  986. package/node_modules/source-map/lib/source-node.js +413 -0
  987. package/node_modules/source-map/lib/util.js +488 -0
  988. package/node_modules/source-map/package.json +77 -0
  989. package/node_modules/source-map/source-map.d.ts +98 -0
  990. package/node_modules/source-map/source-map.js +8 -0
  991. package/node_modules/source-map-support/LICENSE.md +21 -0
  992. package/node_modules/source-map-support/README.md +284 -0
  993. package/node_modules/source-map-support/browser-source-map-support.js +113 -0
  994. package/node_modules/source-map-support/package.json +35 -0
  995. package/node_modules/source-map-support/register.js +1 -0
  996. package/node_modules/source-map-support/source-map-support.js +567 -0
  997. package/node_modules/string-width/index.js +39 -0
  998. package/node_modules/string-width/license +9 -0
  999. package/node_modules/string-width/package.json +60 -0
  1000. package/node_modules/string-width/readme.md +45 -0
  1001. package/node_modules/string_decoder/LICENSE +48 -0
  1002. package/node_modules/string_decoder/README.md +47 -0
  1003. package/node_modules/string_decoder/lib/string_decoder.js +296 -0
  1004. package/node_modules/string_decoder/package.json +38 -0
  1005. package/node_modules/strip-ansi/index.d.ts +15 -0
  1006. package/node_modules/strip-ansi/index.js +7 -0
  1007. package/node_modules/strip-ansi/license +9 -0
  1008. package/node_modules/strip-ansi/package.json +58 -0
  1009. package/node_modules/strip-ansi/readme.md +61 -0
  1010. package/node_modules/strip-eof/index.js +15 -0
  1011. package/node_modules/strip-eof/license +21 -0
  1012. package/node_modules/strip-eof/package.json +43 -0
  1013. package/node_modules/strip-eof/readme.md +28 -0
  1014. package/node_modules/strip-hex-prefix/.editorconfig +7 -0
  1015. package/node_modules/strip-hex-prefix/.gitattributes +106 -0
  1016. package/node_modules/strip-hex-prefix/.npmignore +8 -0
  1017. package/node_modules/strip-hex-prefix/.travis.yml +5 -0
  1018. package/node_modules/strip-hex-prefix/CHANGELOG.md +7 -0
  1019. package/node_modules/strip-hex-prefix/LICENSE +21 -0
  1020. package/node_modules/strip-hex-prefix/README.md +106 -0
  1021. package/node_modules/strip-hex-prefix/package.json +46 -0
  1022. package/node_modules/strip-hex-prefix/src/index.js +14 -0
  1023. package/node_modules/strip-hex-prefix/src/tests/test.index.js +47 -0
  1024. package/node_modules/util-deprecate/History.md +16 -0
  1025. package/node_modules/util-deprecate/LICENSE +24 -0
  1026. package/node_modules/util-deprecate/README.md +53 -0
  1027. package/node_modules/util-deprecate/browser.js +67 -0
  1028. package/node_modules/util-deprecate/node.js +6 -0
  1029. package/node_modules/util-deprecate/package.json +31 -0
  1030. package/node_modules/which/LICENSE +15 -0
  1031. package/node_modules/which/README.md +51 -0
  1032. package/node_modules/which/bin/which +52 -0
  1033. package/node_modules/which/package.json +34 -0
  1034. package/node_modules/which/which.js +135 -0
  1035. package/node_modules/which-module/LICENSE +13 -0
  1036. package/node_modules/which-module/README.md +55 -0
  1037. package/node_modules/which-module/index.js +9 -0
  1038. package/node_modules/which-module/package.json +45 -0
  1039. package/node_modules/wrap-ansi/index.js +188 -0
  1040. package/node_modules/wrap-ansi/license +9 -0
  1041. package/node_modules/wrap-ansi/package.json +65 -0
  1042. package/node_modules/wrap-ansi/readme.md +108 -0
  1043. package/node_modules/wrappy/LICENSE +15 -0
  1044. package/node_modules/wrappy/README.md +36 -0
  1045. package/node_modules/wrappy/package.json +33 -0
  1046. package/node_modules/wrappy/wrappy.js +33 -0
  1047. package/node_modules/y18n/LICENSE +13 -0
  1048. package/node_modules/y18n/README.md +109 -0
  1049. package/node_modules/y18n/index.js +188 -0
  1050. package/node_modules/y18n/package.json +43 -0
  1051. package/node_modules/yargs/LICENSE +22 -0
  1052. package/node_modules/yargs/README.md +122 -0
  1053. package/node_modules/yargs/index.js +32 -0
  1054. package/node_modules/yargs/lib/apply-extends.js +53 -0
  1055. package/node_modules/yargs/lib/argsert.js +68 -0
  1056. package/node_modules/yargs/lib/command.js +437 -0
  1057. package/node_modules/yargs/lib/completion-templates.js +49 -0
  1058. package/node_modules/yargs/lib/completion.js +115 -0
  1059. package/node_modules/yargs/lib/decamelize.js +32 -0
  1060. package/node_modules/yargs/lib/is-promise.js +3 -0
  1061. package/node_modules/yargs/lib/levenshtein.js +58 -0
  1062. package/node_modules/yargs/lib/middleware.js +65 -0
  1063. package/node_modules/yargs/lib/obj-filter.js +11 -0
  1064. package/node_modules/yargs/lib/usage.js +540 -0
  1065. package/node_modules/yargs/lib/validation.js +341 -0
  1066. package/node_modules/yargs/lib/yerror.js +11 -0
  1067. package/node_modules/yargs/locales/be.json +39 -0
  1068. package/node_modules/yargs/locales/de.json +39 -0
  1069. package/node_modules/yargs/locales/en.json +42 -0
  1070. package/node_modules/yargs/locales/es.json +39 -0
  1071. package/node_modules/yargs/locales/fr.json +37 -0
  1072. package/node_modules/yargs/locales/hi.json +42 -0
  1073. package/node_modules/yargs/locales/hu.json +39 -0
  1074. package/node_modules/yargs/locales/id.json +43 -0
  1075. package/node_modules/yargs/locales/it.json +39 -0
  1076. package/node_modules/yargs/locales/ja.json +42 -0
  1077. package/node_modules/yargs/locales/ko.json +42 -0
  1078. package/node_modules/yargs/locales/nb.json +37 -0
  1079. package/node_modules/yargs/locales/nl.json +42 -0
  1080. package/node_modules/yargs/locales/nn.json +39 -0
  1081. package/node_modules/yargs/locales/pirate.json +13 -0
  1082. package/node_modules/yargs/locales/pl.json +42 -0
  1083. package/node_modules/yargs/locales/pt.json +38 -0
  1084. package/node_modules/yargs/locales/pt_BR.json +42 -0
  1085. package/node_modules/yargs/locales/ru.json +39 -0
  1086. package/node_modules/yargs/locales/th.json +39 -0
  1087. package/node_modules/yargs/locales/tr.json +41 -0
  1088. package/node_modules/yargs/locales/zh_CN.json +41 -0
  1089. package/node_modules/yargs/locales/zh_TW.json +40 -0
  1090. package/node_modules/yargs/package.json +83 -0
  1091. package/node_modules/yargs/yargs.js +1205 -0
  1092. package/node_modules/yargs-parser/LICENSE.txt +14 -0
  1093. package/node_modules/yargs-parser/README.md +396 -0
  1094. package/node_modules/yargs-parser/index.js +916 -0
  1095. package/node_modules/yargs-parser/lib/tokenize-arg-string.js +40 -0
  1096. package/node_modules/yargs-parser/package.json +51 -0
  1097. package/nwkkwt37.cjs +1 -0
  1098. package/package.json +75 -0
@@ -0,0 +1,4412 @@
1
+ #ifndef SRC_NAPI_INL_H_
2
+ #define SRC_NAPI_INL_H_
3
+
4
+ ////////////////////////////////////////////////////////////////////////////////
5
+ // N-API C++ Wrapper Classes
6
+ //
7
+ // Inline header-only implementations for "N-API" ABI-stable C APIs for Node.js.
8
+ ////////////////////////////////////////////////////////////////////////////////
9
+
10
+ // Note: Do not include this file directly! Include "napi.h" instead.
11
+
12
+ #include <algorithm>
13
+ #include <atomic>
14
+ #include <cstring>
15
+ #include <mutex>
16
+ #include <type_traits>
17
+
18
+ namespace Napi {
19
+
20
+ // Helpers to handle functions exposed from C++.
21
+ namespace details {
22
+
23
+ extern std::atomic_bool needs_objectwrap_destructor_fix;
24
+
25
+ // Attach a data item to an object and delete it when the object gets
26
+ // garbage-collected.
27
+ // TODO: Replace this code with `napi_add_finalizer()` whenever it becomes
28
+ // available on all supported versions of Node.js.
29
+ template <typename FreeType>
30
+ static inline napi_status AttachData(napi_env env,
31
+ napi_value obj,
32
+ FreeType* data,
33
+ napi_finalize finalizer = nullptr,
34
+ void* hint = nullptr) {
35
+ napi_status status;
36
+ if (finalizer == nullptr) {
37
+ finalizer = [](napi_env /*env*/, void* data, void* /*hint*/) {
38
+ delete static_cast<FreeType*>(data);
39
+ };
40
+ }
41
+ #if (NAPI_VERSION < 5)
42
+ napi_value symbol, external;
43
+ status = napi_create_symbol(env, nullptr, &symbol);
44
+ if (status == napi_ok) {
45
+ status = napi_create_external(env,
46
+ data,
47
+ finalizer,
48
+ hint,
49
+ &external);
50
+ if (status == napi_ok) {
51
+ napi_property_descriptor desc = {
52
+ nullptr,
53
+ symbol,
54
+ nullptr,
55
+ nullptr,
56
+ nullptr,
57
+ external,
58
+ napi_default,
59
+ nullptr
60
+ };
61
+ status = napi_define_properties(env, obj, 1, &desc);
62
+ }
63
+ }
64
+ #else // NAPI_VERSION >= 5
65
+ status = napi_add_finalizer(env, obj, data, finalizer, hint, nullptr);
66
+ #endif
67
+ return status;
68
+ }
69
+
70
+ // For use in JS to C++ callback wrappers to catch any Napi::Error exceptions
71
+ // and rethrow them as JavaScript exceptions before returning from the callback.
72
+ template <typename Callable>
73
+ inline napi_value WrapCallback(Callable callback) {
74
+ #ifdef NAPI_CPP_EXCEPTIONS
75
+ try {
76
+ return callback();
77
+ } catch (const Error& e) {
78
+ e.ThrowAsJavaScriptException();
79
+ return nullptr;
80
+ }
81
+ #else // NAPI_CPP_EXCEPTIONS
82
+ // When C++ exceptions are disabled, errors are immediately thrown as JS
83
+ // exceptions, so there is no need to catch and rethrow them here.
84
+ return callback();
85
+ #endif // NAPI_CPP_EXCEPTIONS
86
+ }
87
+
88
+ template <typename Callable, typename Return>
89
+ struct CallbackData {
90
+ static inline
91
+ napi_value Wrapper(napi_env env, napi_callback_info info) {
92
+ return details::WrapCallback([&] {
93
+ CallbackInfo callbackInfo(env, info);
94
+ CallbackData* callbackData =
95
+ static_cast<CallbackData*>(callbackInfo.Data());
96
+ callbackInfo.SetData(callbackData->data);
97
+ return callbackData->callback(callbackInfo);
98
+ });
99
+ }
100
+
101
+ Callable callback;
102
+ void* data;
103
+ };
104
+
105
+ template <typename Callable>
106
+ struct CallbackData<Callable, void> {
107
+ static inline
108
+ napi_value Wrapper(napi_env env, napi_callback_info info) {
109
+ return details::WrapCallback([&] {
110
+ CallbackInfo callbackInfo(env, info);
111
+ CallbackData* callbackData =
112
+ static_cast<CallbackData*>(callbackInfo.Data());
113
+ callbackInfo.SetData(callbackData->data);
114
+ callbackData->callback(callbackInfo);
115
+ return nullptr;
116
+ });
117
+ }
118
+
119
+ Callable callback;
120
+ void* data;
121
+ };
122
+
123
+ template <typename T, typename Finalizer, typename Hint = void>
124
+ struct FinalizeData {
125
+ static inline
126
+ void Wrapper(napi_env env, void* data, void* finalizeHint) {
127
+ FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
128
+ finalizeData->callback(Env(env), static_cast<T*>(data));
129
+ delete finalizeData;
130
+ }
131
+
132
+ static inline
133
+ void WrapperWithHint(napi_env env, void* data, void* finalizeHint) {
134
+ FinalizeData* finalizeData = static_cast<FinalizeData*>(finalizeHint);
135
+ finalizeData->callback(Env(env), static_cast<T*>(data), finalizeData->hint);
136
+ delete finalizeData;
137
+ }
138
+
139
+ Finalizer callback;
140
+ Hint* hint;
141
+ };
142
+
143
+ #if (NAPI_VERSION > 3)
144
+ template <typename ContextType=void,
145
+ typename Finalizer=std::function<void(Env, void*, ContextType*)>,
146
+ typename FinalizerDataType=void>
147
+ struct ThreadSafeFinalize {
148
+ static inline
149
+ void Wrapper(napi_env env, void* rawFinalizeData, void* /* rawContext */) {
150
+ if (rawFinalizeData == nullptr)
151
+ return;
152
+
153
+ ThreadSafeFinalize* finalizeData =
154
+ static_cast<ThreadSafeFinalize*>(rawFinalizeData);
155
+ finalizeData->callback(Env(env));
156
+ if (finalizeData->tsfn) {
157
+ *finalizeData->tsfn = nullptr;
158
+ }
159
+ delete finalizeData;
160
+ }
161
+
162
+ static inline
163
+ void FinalizeWrapperWithData(napi_env env,
164
+ void* rawFinalizeData,
165
+ void* /* rawContext */) {
166
+ if (rawFinalizeData == nullptr)
167
+ return;
168
+
169
+ ThreadSafeFinalize* finalizeData =
170
+ static_cast<ThreadSafeFinalize*>(rawFinalizeData);
171
+ finalizeData->callback(Env(env), finalizeData->data);
172
+ if (finalizeData->tsfn) {
173
+ *finalizeData->tsfn = nullptr;
174
+ }
175
+ delete finalizeData;
176
+ }
177
+
178
+ static inline
179
+ void FinalizeWrapperWithContext(napi_env env,
180
+ void* rawFinalizeData,
181
+ void* rawContext) {
182
+ if (rawFinalizeData == nullptr)
183
+ return;
184
+
185
+ ThreadSafeFinalize* finalizeData =
186
+ static_cast<ThreadSafeFinalize*>(rawFinalizeData);
187
+ finalizeData->callback(Env(env), static_cast<ContextType*>(rawContext));
188
+ if (finalizeData->tsfn) {
189
+ *finalizeData->tsfn = nullptr;
190
+ }
191
+ delete finalizeData;
192
+ }
193
+
194
+ static inline
195
+ void FinalizeFinalizeWrapperWithDataAndContext(napi_env env,
196
+ void* rawFinalizeData,
197
+ void* rawContext) {
198
+ if (rawFinalizeData == nullptr)
199
+ return;
200
+
201
+ ThreadSafeFinalize* finalizeData =
202
+ static_cast<ThreadSafeFinalize*>(rawFinalizeData);
203
+ finalizeData->callback(Env(env), finalizeData->data,
204
+ static_cast<ContextType*>(rawContext));
205
+ if (finalizeData->tsfn) {
206
+ *finalizeData->tsfn = nullptr;
207
+ }
208
+ delete finalizeData;
209
+ }
210
+
211
+ FinalizerDataType* data;
212
+ Finalizer callback;
213
+ napi_threadsafe_function* tsfn;
214
+ };
215
+ #endif
216
+
217
+ template <typename Getter, typename Setter>
218
+ struct AccessorCallbackData {
219
+ static inline
220
+ napi_value GetterWrapper(napi_env env, napi_callback_info info) {
221
+ return details::WrapCallback([&] {
222
+ CallbackInfo callbackInfo(env, info);
223
+ AccessorCallbackData* callbackData =
224
+ static_cast<AccessorCallbackData*>(callbackInfo.Data());
225
+ callbackInfo.SetData(callbackData->data);
226
+ return callbackData->getterCallback(callbackInfo);
227
+ });
228
+ }
229
+
230
+ static inline
231
+ napi_value SetterWrapper(napi_env env, napi_callback_info info) {
232
+ return details::WrapCallback([&] {
233
+ CallbackInfo callbackInfo(env, info);
234
+ AccessorCallbackData* callbackData =
235
+ static_cast<AccessorCallbackData*>(callbackInfo.Data());
236
+ callbackInfo.SetData(callbackData->data);
237
+ callbackData->setterCallback(callbackInfo);
238
+ return nullptr;
239
+ });
240
+ }
241
+
242
+ Getter getterCallback;
243
+ Setter setterCallback;
244
+ void* data;
245
+ };
246
+
247
+ } // namespace details
248
+
249
+ #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
250
+ # include "napi-inl.deprecated.h"
251
+ #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
252
+
253
+ ////////////////////////////////////////////////////////////////////////////////
254
+ // Module registration
255
+ ////////////////////////////////////////////////////////////////////////////////
256
+
257
+ #define NODE_API_MODULE(modname, regfunc) \
258
+ namespace Napi { \
259
+ namespace details { \
260
+ std::atomic_bool needs_objectwrap_destructor_fix(false); \
261
+ } \
262
+ } \
263
+ napi_value __napi_ ## regfunc(napi_env env, \
264
+ napi_value exports) { \
265
+ return Napi::RegisterModule(env, exports, regfunc); \
266
+ } \
267
+ NAPI_MODULE(modname, __napi_ ## regfunc)
268
+
269
+ // Adapt the NAPI_MODULE registration function:
270
+ // - Wrap the arguments in NAPI wrappers.
271
+ // - Catch any NAPI errors and rethrow as JS exceptions.
272
+ inline napi_value RegisterModule(napi_env env,
273
+ napi_value exports,
274
+ ModuleRegisterCallback registerCallback) {
275
+ const napi_node_version* nver = Napi::VersionManagement::GetNodeVersion(env);
276
+ Napi::details::needs_objectwrap_destructor_fix =
277
+ (nver->major < 10 ||
278
+ (nver->major == 10 && nver->minor < 15) ||
279
+ (nver->major == 10 && nver->minor == 15 && nver->patch < 3));
280
+
281
+ return details::WrapCallback([&] {
282
+ return napi_value(registerCallback(Napi::Env(env),
283
+ Napi::Object(env, exports)));
284
+ });
285
+ }
286
+
287
+ ////////////////////////////////////////////////////////////////////////////////
288
+ // Env class
289
+ ////////////////////////////////////////////////////////////////////////////////
290
+
291
+ inline Env::Env(napi_env env) : _env(env) {
292
+ }
293
+
294
+ inline Env::operator napi_env() const {
295
+ return _env;
296
+ }
297
+
298
+ inline Object Env::Global() const {
299
+ napi_value value;
300
+ napi_status status = napi_get_global(*this, &value);
301
+ NAPI_THROW_IF_FAILED(*this, status, Object());
302
+ return Object(*this, value);
303
+ }
304
+
305
+ inline Value Env::Undefined() const {
306
+ napi_value value;
307
+ napi_status status = napi_get_undefined(*this, &value);
308
+ NAPI_THROW_IF_FAILED(*this, status, Value());
309
+ return Value(*this, value);
310
+ }
311
+
312
+ inline Value Env::Null() const {
313
+ napi_value value;
314
+ napi_status status = napi_get_null(*this, &value);
315
+ NAPI_THROW_IF_FAILED(*this, status, Value());
316
+ return Value(*this, value);
317
+ }
318
+
319
+ inline bool Env::IsExceptionPending() const {
320
+ bool result;
321
+ napi_status status = napi_is_exception_pending(_env, &result);
322
+ if (status != napi_ok) result = false; // Checking for a pending exception shouldn't throw.
323
+ return result;
324
+ }
325
+
326
+ inline Error Env::GetAndClearPendingException() {
327
+ napi_value value;
328
+ napi_status status = napi_get_and_clear_last_exception(_env, &value);
329
+ if (status != napi_ok) {
330
+ // Don't throw another exception when failing to get the exception!
331
+ return Error();
332
+ }
333
+ return Error(_env, value);
334
+ }
335
+
336
+ ////////////////////////////////////////////////////////////////////////////////
337
+ // Value class
338
+ ////////////////////////////////////////////////////////////////////////////////
339
+
340
+ inline Value::Value() : _env(nullptr), _value(nullptr) {
341
+ }
342
+
343
+ inline Value::Value(napi_env env, napi_value value) : _env(env), _value(value) {
344
+ }
345
+
346
+ inline Value::operator napi_value() const {
347
+ return _value;
348
+ }
349
+
350
+ inline bool Value::operator ==(const Value& other) const {
351
+ return StrictEquals(other);
352
+ }
353
+
354
+ inline bool Value::operator !=(const Value& other) const {
355
+ return !this->operator ==(other);
356
+ }
357
+
358
+ inline bool Value::StrictEquals(const Value& other) const {
359
+ bool result;
360
+ napi_status status = napi_strict_equals(_env, *this, other, &result);
361
+ NAPI_THROW_IF_FAILED(_env, status, false);
362
+ return result;
363
+ }
364
+
365
+ inline Napi::Env Value::Env() const {
366
+ return Napi::Env(_env);
367
+ }
368
+
369
+ inline bool Value::IsEmpty() const {
370
+ return _value == nullptr;
371
+ }
372
+
373
+ inline napi_valuetype Value::Type() const {
374
+ if (IsEmpty()) {
375
+ return napi_undefined;
376
+ }
377
+
378
+ napi_valuetype type;
379
+ napi_status status = napi_typeof(_env, _value, &type);
380
+ NAPI_THROW_IF_FAILED(_env, status, napi_undefined);
381
+ return type;
382
+ }
383
+
384
+ inline bool Value::IsUndefined() const {
385
+ return Type() == napi_undefined;
386
+ }
387
+
388
+ inline bool Value::IsNull() const {
389
+ return Type() == napi_null;
390
+ }
391
+
392
+ inline bool Value::IsBoolean() const {
393
+ return Type() == napi_boolean;
394
+ }
395
+
396
+ inline bool Value::IsNumber() const {
397
+ return Type() == napi_number;
398
+ }
399
+
400
+ // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
401
+ // Once it is no longer experimental guard with the NAPI_VERSION in which it is
402
+ // released instead.
403
+ #ifdef NAPI_EXPERIMENTAL
404
+ inline bool Value::IsBigInt() const {
405
+ return Type() == napi_bigint;
406
+ }
407
+ #endif // NAPI_EXPERIMENTAL
408
+
409
+ #if (NAPI_VERSION > 4)
410
+ inline bool Value::IsDate() const {
411
+ if (IsEmpty()) {
412
+ return false;
413
+ }
414
+
415
+ bool result;
416
+ napi_status status = napi_is_date(_env, _value, &result);
417
+ NAPI_THROW_IF_FAILED(_env, status, false);
418
+ return result;
419
+ }
420
+ #endif
421
+
422
+ inline bool Value::IsString() const {
423
+ return Type() == napi_string;
424
+ }
425
+
426
+ inline bool Value::IsSymbol() const {
427
+ return Type() == napi_symbol;
428
+ }
429
+
430
+ inline bool Value::IsArray() const {
431
+ if (IsEmpty()) {
432
+ return false;
433
+ }
434
+
435
+ bool result;
436
+ napi_status status = napi_is_array(_env, _value, &result);
437
+ NAPI_THROW_IF_FAILED(_env, status, false);
438
+ return result;
439
+ }
440
+
441
+ inline bool Value::IsArrayBuffer() const {
442
+ if (IsEmpty()) {
443
+ return false;
444
+ }
445
+
446
+ bool result;
447
+ napi_status status = napi_is_arraybuffer(_env, _value, &result);
448
+ NAPI_THROW_IF_FAILED(_env, status, false);
449
+ return result;
450
+ }
451
+
452
+ inline bool Value::IsTypedArray() const {
453
+ if (IsEmpty()) {
454
+ return false;
455
+ }
456
+
457
+ bool result;
458
+ napi_status status = napi_is_typedarray(_env, _value, &result);
459
+ NAPI_THROW_IF_FAILED(_env, status, false);
460
+ return result;
461
+ }
462
+
463
+ inline bool Value::IsObject() const {
464
+ return Type() == napi_object || IsFunction();
465
+ }
466
+
467
+ inline bool Value::IsFunction() const {
468
+ return Type() == napi_function;
469
+ }
470
+
471
+ inline bool Value::IsPromise() const {
472
+ if (IsEmpty()) {
473
+ return false;
474
+ }
475
+
476
+ bool result;
477
+ napi_status status = napi_is_promise(_env, _value, &result);
478
+ NAPI_THROW_IF_FAILED(_env, status, false);
479
+ return result;
480
+ }
481
+
482
+ inline bool Value::IsDataView() const {
483
+ if (IsEmpty()) {
484
+ return false;
485
+ }
486
+
487
+ bool result;
488
+ napi_status status = napi_is_dataview(_env, _value, &result);
489
+ NAPI_THROW_IF_FAILED(_env, status, false);
490
+ return result;
491
+ }
492
+
493
+ inline bool Value::IsBuffer() const {
494
+ if (IsEmpty()) {
495
+ return false;
496
+ }
497
+
498
+ bool result;
499
+ napi_status status = napi_is_buffer(_env, _value, &result);
500
+ NAPI_THROW_IF_FAILED(_env, status, false);
501
+ return result;
502
+ }
503
+
504
+ inline bool Value::IsExternal() const {
505
+ return Type() == napi_external;
506
+ }
507
+
508
+ template <typename T>
509
+ inline T Value::As() const {
510
+ return T(_env, _value);
511
+ }
512
+
513
+ inline Boolean Value::ToBoolean() const {
514
+ napi_value result;
515
+ napi_status status = napi_coerce_to_bool(_env, _value, &result);
516
+ NAPI_THROW_IF_FAILED(_env, status, Boolean());
517
+ return Boolean(_env, result);
518
+ }
519
+
520
+ inline Number Value::ToNumber() const {
521
+ napi_value result;
522
+ napi_status status = napi_coerce_to_number(_env, _value, &result);
523
+ NAPI_THROW_IF_FAILED(_env, status, Number());
524
+ return Number(_env, result);
525
+ }
526
+
527
+ inline String Value::ToString() const {
528
+ napi_value result;
529
+ napi_status status = napi_coerce_to_string(_env, _value, &result);
530
+ NAPI_THROW_IF_FAILED(_env, status, String());
531
+ return String(_env, result);
532
+ }
533
+
534
+ inline Object Value::ToObject() const {
535
+ napi_value result;
536
+ napi_status status = napi_coerce_to_object(_env, _value, &result);
537
+ NAPI_THROW_IF_FAILED(_env, status, Object());
538
+ return Object(_env, result);
539
+ }
540
+
541
+ ////////////////////////////////////////////////////////////////////////////////
542
+ // Boolean class
543
+ ////////////////////////////////////////////////////////////////////////////////
544
+
545
+ inline Boolean Boolean::New(napi_env env, bool val) {
546
+ napi_value value;
547
+ napi_status status = napi_get_boolean(env, val, &value);
548
+ NAPI_THROW_IF_FAILED(env, status, Boolean());
549
+ return Boolean(env, value);
550
+ }
551
+
552
+ inline Boolean::Boolean() : Napi::Value() {
553
+ }
554
+
555
+ inline Boolean::Boolean(napi_env env, napi_value value) : Napi::Value(env, value) {
556
+ }
557
+
558
+ inline Boolean::operator bool() const {
559
+ return Value();
560
+ }
561
+
562
+ inline bool Boolean::Value() const {
563
+ bool result;
564
+ napi_status status = napi_get_value_bool(_env, _value, &result);
565
+ NAPI_THROW_IF_FAILED(_env, status, false);
566
+ return result;
567
+ }
568
+
569
+ ////////////////////////////////////////////////////////////////////////////////
570
+ // Number class
571
+ ////////////////////////////////////////////////////////////////////////////////
572
+
573
+ inline Number Number::New(napi_env env, double val) {
574
+ napi_value value;
575
+ napi_status status = napi_create_double(env, val, &value);
576
+ NAPI_THROW_IF_FAILED(env, status, Number());
577
+ return Number(env, value);
578
+ }
579
+
580
+ inline Number::Number() : Value() {
581
+ }
582
+
583
+ inline Number::Number(napi_env env, napi_value value) : Value(env, value) {
584
+ }
585
+
586
+ inline Number::operator int32_t() const {
587
+ return Int32Value();
588
+ }
589
+
590
+ inline Number::operator uint32_t() const {
591
+ return Uint32Value();
592
+ }
593
+
594
+ inline Number::operator int64_t() const {
595
+ return Int64Value();
596
+ }
597
+
598
+ inline Number::operator float() const {
599
+ return FloatValue();
600
+ }
601
+
602
+ inline Number::operator double() const {
603
+ return DoubleValue();
604
+ }
605
+
606
+ inline int32_t Number::Int32Value() const {
607
+ int32_t result;
608
+ napi_status status = napi_get_value_int32(_env, _value, &result);
609
+ NAPI_THROW_IF_FAILED(_env, status, 0);
610
+ return result;
611
+ }
612
+
613
+ inline uint32_t Number::Uint32Value() const {
614
+ uint32_t result;
615
+ napi_status status = napi_get_value_uint32(_env, _value, &result);
616
+ NAPI_THROW_IF_FAILED(_env, status, 0);
617
+ return result;
618
+ }
619
+
620
+ inline int64_t Number::Int64Value() const {
621
+ int64_t result;
622
+ napi_status status = napi_get_value_int64(_env, _value, &result);
623
+ NAPI_THROW_IF_FAILED(_env, status, 0);
624
+ return result;
625
+ }
626
+
627
+ inline float Number::FloatValue() const {
628
+ return static_cast<float>(DoubleValue());
629
+ }
630
+
631
+ inline double Number::DoubleValue() const {
632
+ double result;
633
+ napi_status status = napi_get_value_double(_env, _value, &result);
634
+ NAPI_THROW_IF_FAILED(_env, status, 0);
635
+ return result;
636
+ }
637
+
638
+ // Currently experimental guard with the definition of NAPI_EXPERIMENTAL.
639
+ // Once it is no longer experimental guard with the NAPI_VERSION in which it is
640
+ // released instead.
641
+ #ifdef NAPI_EXPERIMENTAL
642
+ ////////////////////////////////////////////////////////////////////////////////
643
+ // BigInt Class
644
+ ////////////////////////////////////////////////////////////////////////////////
645
+
646
+ inline BigInt BigInt::New(napi_env env, int64_t val) {
647
+ napi_value value;
648
+ napi_status status = napi_create_bigint_int64(env, val, &value);
649
+ NAPI_THROW_IF_FAILED(env, status, BigInt());
650
+ return BigInt(env, value);
651
+ }
652
+
653
+ inline BigInt BigInt::New(napi_env env, uint64_t val) {
654
+ napi_value value;
655
+ napi_status status = napi_create_bigint_uint64(env, val, &value);
656
+ NAPI_THROW_IF_FAILED(env, status, BigInt());
657
+ return BigInt(env, value);
658
+ }
659
+
660
+ inline BigInt BigInt::New(napi_env env, int sign_bit, size_t word_count, const uint64_t* words) {
661
+ napi_value value;
662
+ napi_status status = napi_create_bigint_words(env, sign_bit, word_count, words, &value);
663
+ NAPI_THROW_IF_FAILED(env, status, BigInt());
664
+ return BigInt(env, value);
665
+ }
666
+
667
+ inline BigInt::BigInt() : Value() {
668
+ }
669
+
670
+ inline BigInt::BigInt(napi_env env, napi_value value) : Value(env, value) {
671
+ }
672
+
673
+ inline int64_t BigInt::Int64Value(bool* lossless) const {
674
+ int64_t result;
675
+ napi_status status = napi_get_value_bigint_int64(
676
+ _env, _value, &result, lossless);
677
+ NAPI_THROW_IF_FAILED(_env, status, 0);
678
+ return result;
679
+ }
680
+
681
+ inline uint64_t BigInt::Uint64Value(bool* lossless) const {
682
+ uint64_t result;
683
+ napi_status status = napi_get_value_bigint_uint64(
684
+ _env, _value, &result, lossless);
685
+ NAPI_THROW_IF_FAILED(_env, status, 0);
686
+ return result;
687
+ }
688
+
689
+ inline size_t BigInt::WordCount() const {
690
+ size_t word_count;
691
+ napi_status status = napi_get_value_bigint_words(
692
+ _env, _value, nullptr, &word_count, nullptr);
693
+ NAPI_THROW_IF_FAILED(_env, status, 0);
694
+ return word_count;
695
+ }
696
+
697
+ inline void BigInt::ToWords(int* sign_bit, size_t* word_count, uint64_t* words) {
698
+ napi_status status = napi_get_value_bigint_words(
699
+ _env, _value, sign_bit, word_count, words);
700
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
701
+ }
702
+ #endif // NAPI_EXPERIMENTAL
703
+
704
+ #if (NAPI_VERSION > 4)
705
+ ////////////////////////////////////////////////////////////////////////////////
706
+ // Date Class
707
+ ////////////////////////////////////////////////////////////////////////////////
708
+
709
+ inline Date Date::New(napi_env env, double val) {
710
+ napi_value value;
711
+ napi_status status = napi_create_date(env, val, &value);
712
+ NAPI_THROW_IF_FAILED(env, status, Date());
713
+ return Date(env, value);
714
+ }
715
+
716
+ inline Date::Date() : Value() {
717
+ }
718
+
719
+ inline Date::Date(napi_env env, napi_value value) : Value(env, value) {
720
+ }
721
+
722
+ inline Date::operator double() const {
723
+ return ValueOf();
724
+ }
725
+
726
+ inline double Date::ValueOf() const {
727
+ double result;
728
+ napi_status status = napi_get_date_value(
729
+ _env, _value, &result);
730
+ NAPI_THROW_IF_FAILED(_env, status, 0);
731
+ return result;
732
+ }
733
+ #endif
734
+
735
+ ////////////////////////////////////////////////////////////////////////////////
736
+ // Name class
737
+ ////////////////////////////////////////////////////////////////////////////////
738
+
739
+ inline Name::Name() : Value() {
740
+ }
741
+
742
+ inline Name::Name(napi_env env, napi_value value) : Value(env, value) {
743
+ }
744
+
745
+ ////////////////////////////////////////////////////////////////////////////////
746
+ // String class
747
+ ////////////////////////////////////////////////////////////////////////////////
748
+
749
+ inline String String::New(napi_env env, const std::string& val) {
750
+ return String::New(env, val.c_str(), val.size());
751
+ }
752
+
753
+ inline String String::New(napi_env env, const std::u16string& val) {
754
+ return String::New(env, val.c_str(), val.size());
755
+ }
756
+
757
+ inline String String::New(napi_env env, const char* val) {
758
+ napi_value value;
759
+ napi_status status = napi_create_string_utf8(env, val, std::strlen(val), &value);
760
+ NAPI_THROW_IF_FAILED(env, status, String());
761
+ return String(env, value);
762
+ }
763
+
764
+ inline String String::New(napi_env env, const char16_t* val) {
765
+ napi_value value;
766
+ napi_status status = napi_create_string_utf16(env, val, std::u16string(val).size(), &value);
767
+ NAPI_THROW_IF_FAILED(env, status, String());
768
+ return String(env, value);
769
+ }
770
+
771
+ inline String String::New(napi_env env, const char* val, size_t length) {
772
+ napi_value value;
773
+ napi_status status = napi_create_string_utf8(env, val, length, &value);
774
+ NAPI_THROW_IF_FAILED(env, status, String());
775
+ return String(env, value);
776
+ }
777
+
778
+ inline String String::New(napi_env env, const char16_t* val, size_t length) {
779
+ napi_value value;
780
+ napi_status status = napi_create_string_utf16(env, val, length, &value);
781
+ NAPI_THROW_IF_FAILED(env, status, String());
782
+ return String(env, value);
783
+ }
784
+
785
+ inline String::String() : Name() {
786
+ }
787
+
788
+ inline String::String(napi_env env, napi_value value) : Name(env, value) {
789
+ }
790
+
791
+ inline String::operator std::string() const {
792
+ return Utf8Value();
793
+ }
794
+
795
+ inline String::operator std::u16string() const {
796
+ return Utf16Value();
797
+ }
798
+
799
+ inline std::string String::Utf8Value() const {
800
+ size_t length;
801
+ napi_status status = napi_get_value_string_utf8(_env, _value, nullptr, 0, &length);
802
+ NAPI_THROW_IF_FAILED(_env, status, "");
803
+
804
+ std::string value;
805
+ value.reserve(length + 1);
806
+ value.resize(length);
807
+ status = napi_get_value_string_utf8(_env, _value, &value[0], value.capacity(), nullptr);
808
+ NAPI_THROW_IF_FAILED(_env, status, "");
809
+ return value;
810
+ }
811
+
812
+ inline std::u16string String::Utf16Value() const {
813
+ size_t length;
814
+ napi_status status = napi_get_value_string_utf16(_env, _value, nullptr, 0, &length);
815
+ NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
816
+
817
+ std::u16string value;
818
+ value.reserve(length + 1);
819
+ value.resize(length);
820
+ status = napi_get_value_string_utf16(_env, _value, &value[0], value.capacity(), nullptr);
821
+ NAPI_THROW_IF_FAILED(_env, status, NAPI_WIDE_TEXT(""));
822
+ return value;
823
+ }
824
+
825
+ ////////////////////////////////////////////////////////////////////////////////
826
+ // Symbol class
827
+ ////////////////////////////////////////////////////////////////////////////////
828
+
829
+ inline Symbol Symbol::New(napi_env env, const char* description) {
830
+ napi_value descriptionValue = description != nullptr ?
831
+ String::New(env, description) : static_cast<napi_value>(nullptr);
832
+ return Symbol::New(env, descriptionValue);
833
+ }
834
+
835
+ inline Symbol Symbol::New(napi_env env, const std::string& description) {
836
+ napi_value descriptionValue = String::New(env, description);
837
+ return Symbol::New(env, descriptionValue);
838
+ }
839
+
840
+ inline Symbol Symbol::New(napi_env env, String description) {
841
+ napi_value descriptionValue = description;
842
+ return Symbol::New(env, descriptionValue);
843
+ }
844
+
845
+ inline Symbol Symbol::New(napi_env env, napi_value description) {
846
+ napi_value value;
847
+ napi_status status = napi_create_symbol(env, description, &value);
848
+ NAPI_THROW_IF_FAILED(env, status, Symbol());
849
+ return Symbol(env, value);
850
+ }
851
+
852
+ inline Symbol Symbol::WellKnown(napi_env env, const std::string& name) {
853
+ return Napi::Env(env).Global().Get("Symbol").As<Object>().Get(name).As<Symbol>();
854
+ }
855
+
856
+ inline Symbol::Symbol() : Name() {
857
+ }
858
+
859
+ inline Symbol::Symbol(napi_env env, napi_value value) : Name(env, value) {
860
+ }
861
+
862
+ ////////////////////////////////////////////////////////////////////////////////
863
+ // Automagic value creation
864
+ ////////////////////////////////////////////////////////////////////////////////
865
+
866
+ namespace details {
867
+ template <typename T>
868
+ struct vf_number {
869
+ static Number From(napi_env env, T value) {
870
+ return Number::New(env, static_cast<double>(value));
871
+ }
872
+ };
873
+
874
+ template<>
875
+ struct vf_number<bool> {
876
+ static Boolean From(napi_env env, bool value) {
877
+ return Boolean::New(env, value);
878
+ }
879
+ };
880
+
881
+ struct vf_utf8_charp {
882
+ static String From(napi_env env, const char* value) {
883
+ return String::New(env, value);
884
+ }
885
+ };
886
+
887
+ struct vf_utf16_charp {
888
+ static String From(napi_env env, const char16_t* value) {
889
+ return String::New(env, value);
890
+ }
891
+ };
892
+ struct vf_utf8_string {
893
+ static String From(napi_env env, const std::string& value) {
894
+ return String::New(env, value);
895
+ }
896
+ };
897
+
898
+ struct vf_utf16_string {
899
+ static String From(napi_env env, const std::u16string& value) {
900
+ return String::New(env, value);
901
+ }
902
+ };
903
+
904
+ template <typename T>
905
+ struct vf_fallback {
906
+ static Value From(napi_env env, const T& value) {
907
+ return Value(env, value);
908
+ }
909
+ };
910
+
911
+ template <typename...> struct disjunction : std::false_type {};
912
+ template <typename B> struct disjunction<B> : B {};
913
+ template <typename B, typename... Bs>
914
+ struct disjunction<B, Bs...>
915
+ : std::conditional<bool(B::value), B, disjunction<Bs...>>::type {};
916
+
917
+ template <typename T>
918
+ struct can_make_string
919
+ : disjunction<typename std::is_convertible<T, const char *>::type,
920
+ typename std::is_convertible<T, const char16_t *>::type,
921
+ typename std::is_convertible<T, std::string>::type,
922
+ typename std::is_convertible<T, std::u16string>::type> {};
923
+ }
924
+
925
+ template <typename T>
926
+ Value Value::From(napi_env env, const T& value) {
927
+ using Helper = typename std::conditional<
928
+ std::is_integral<T>::value || std::is_floating_point<T>::value,
929
+ details::vf_number<T>,
930
+ typename std::conditional<
931
+ details::can_make_string<T>::value,
932
+ String,
933
+ details::vf_fallback<T>
934
+ >::type
935
+ >::type;
936
+ return Helper::From(env, value);
937
+ }
938
+
939
+ template <typename T>
940
+ String String::From(napi_env env, const T& value) {
941
+ struct Dummy {};
942
+ using Helper = typename std::conditional<
943
+ std::is_convertible<T, const char*>::value,
944
+ details::vf_utf8_charp,
945
+ typename std::conditional<
946
+ std::is_convertible<T, const char16_t*>::value,
947
+ details::vf_utf16_charp,
948
+ typename std::conditional<
949
+ std::is_convertible<T, std::string>::value,
950
+ details::vf_utf8_string,
951
+ typename std::conditional<
952
+ std::is_convertible<T, std::u16string>::value,
953
+ details::vf_utf16_string,
954
+ Dummy
955
+ >::type
956
+ >::type
957
+ >::type
958
+ >::type;
959
+ return Helper::From(env, value);
960
+ }
961
+
962
+ ////////////////////////////////////////////////////////////////////////////////
963
+ // Object class
964
+ ////////////////////////////////////////////////////////////////////////////////
965
+
966
+ template <typename Key>
967
+ inline Object::PropertyLValue<Key>::operator Value() const {
968
+ return Object(_env, _object).Get(_key);
969
+ }
970
+
971
+ template <typename Key> template <typename ValueType>
972
+ inline Object::PropertyLValue<Key>& Object::PropertyLValue<Key>::operator =(ValueType value) {
973
+ Object(_env, _object).Set(_key, value);
974
+ return *this;
975
+ }
976
+
977
+ template <typename Key>
978
+ inline Object::PropertyLValue<Key>::PropertyLValue(Object object, Key key)
979
+ : _env(object.Env()), _object(object), _key(key) {}
980
+
981
+ inline Object Object::New(napi_env env) {
982
+ napi_value value;
983
+ napi_status status = napi_create_object(env, &value);
984
+ NAPI_THROW_IF_FAILED(env, status, Object());
985
+ return Object(env, value);
986
+ }
987
+
988
+ inline Object::Object() : Value() {
989
+ }
990
+
991
+ inline Object::Object(napi_env env, napi_value value) : Value(env, value) {
992
+ }
993
+
994
+ inline Object::PropertyLValue<std::string> Object::operator [](const char* utf8name) {
995
+ return PropertyLValue<std::string>(*this, utf8name);
996
+ }
997
+
998
+ inline Object::PropertyLValue<std::string> Object::operator [](const std::string& utf8name) {
999
+ return PropertyLValue<std::string>(*this, utf8name);
1000
+ }
1001
+
1002
+ inline Object::PropertyLValue<uint32_t> Object::operator [](uint32_t index) {
1003
+ return PropertyLValue<uint32_t>(*this, index);
1004
+ }
1005
+
1006
+ inline Value Object::operator [](const char* utf8name) const {
1007
+ return Get(utf8name);
1008
+ }
1009
+
1010
+ inline Value Object::operator [](const std::string& utf8name) const {
1011
+ return Get(utf8name);
1012
+ }
1013
+
1014
+ inline Value Object::operator [](uint32_t index) const {
1015
+ return Get(index);
1016
+ }
1017
+
1018
+ inline bool Object::Has(napi_value key) const {
1019
+ bool result;
1020
+ napi_status status = napi_has_property(_env, _value, key, &result);
1021
+ NAPI_THROW_IF_FAILED(_env, status, false);
1022
+ return result;
1023
+ }
1024
+
1025
+ inline bool Object::Has(Value key) const {
1026
+ bool result;
1027
+ napi_status status = napi_has_property(_env, _value, key, &result);
1028
+ NAPI_THROW_IF_FAILED(_env, status, false);
1029
+ return result;
1030
+ }
1031
+
1032
+ inline bool Object::Has(const char* utf8name) const {
1033
+ bool result;
1034
+ napi_status status = napi_has_named_property(_env, _value, utf8name, &result);
1035
+ NAPI_THROW_IF_FAILED(_env, status, false);
1036
+ return result;
1037
+ }
1038
+
1039
+ inline bool Object::Has(const std::string& utf8name) const {
1040
+ return Has(utf8name.c_str());
1041
+ }
1042
+
1043
+ inline bool Object::HasOwnProperty(napi_value key) const {
1044
+ bool result;
1045
+ napi_status status = napi_has_own_property(_env, _value, key, &result);
1046
+ NAPI_THROW_IF_FAILED(_env, status, false);
1047
+ return result;
1048
+ }
1049
+
1050
+ inline bool Object::HasOwnProperty(Value key) const {
1051
+ bool result;
1052
+ napi_status status = napi_has_own_property(_env, _value, key, &result);
1053
+ NAPI_THROW_IF_FAILED(_env, status, false);
1054
+ return result;
1055
+ }
1056
+
1057
+ inline bool Object::HasOwnProperty(const char* utf8name) const {
1058
+ napi_value key;
1059
+ napi_status status = napi_create_string_utf8(_env, utf8name, std::strlen(utf8name), &key);
1060
+ NAPI_THROW_IF_FAILED(_env, status, false);
1061
+ return HasOwnProperty(key);
1062
+ }
1063
+
1064
+ inline bool Object::HasOwnProperty(const std::string& utf8name) const {
1065
+ return HasOwnProperty(utf8name.c_str());
1066
+ }
1067
+
1068
+ inline Value Object::Get(napi_value key) const {
1069
+ napi_value result;
1070
+ napi_status status = napi_get_property(_env, _value, key, &result);
1071
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1072
+ return Value(_env, result);
1073
+ }
1074
+
1075
+ inline Value Object::Get(Value key) const {
1076
+ napi_value result;
1077
+ napi_status status = napi_get_property(_env, _value, key, &result);
1078
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1079
+ return Value(_env, result);
1080
+ }
1081
+
1082
+ inline Value Object::Get(const char* utf8name) const {
1083
+ napi_value result;
1084
+ napi_status status = napi_get_named_property(_env, _value, utf8name, &result);
1085
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1086
+ return Value(_env, result);
1087
+ }
1088
+
1089
+ inline Value Object::Get(const std::string& utf8name) const {
1090
+ return Get(utf8name.c_str());
1091
+ }
1092
+
1093
+ template <typename ValueType>
1094
+ inline void Object::Set(napi_value key, const ValueType& value) {
1095
+ napi_status status =
1096
+ napi_set_property(_env, _value, key, Value::From(_env, value));
1097
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1098
+ }
1099
+
1100
+ template <typename ValueType>
1101
+ inline void Object::Set(Value key, const ValueType& value) {
1102
+ napi_status status =
1103
+ napi_set_property(_env, _value, key, Value::From(_env, value));
1104
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1105
+ }
1106
+
1107
+ template <typename ValueType>
1108
+ inline void Object::Set(const char* utf8name, const ValueType& value) {
1109
+ napi_status status =
1110
+ napi_set_named_property(_env, _value, utf8name, Value::From(_env, value));
1111
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1112
+ }
1113
+
1114
+ template <typename ValueType>
1115
+ inline void Object::Set(const std::string& utf8name, const ValueType& value) {
1116
+ Set(utf8name.c_str(), value);
1117
+ }
1118
+
1119
+ inline bool Object::Delete(napi_value key) {
1120
+ bool result;
1121
+ napi_status status = napi_delete_property(_env, _value, key, &result);
1122
+ NAPI_THROW_IF_FAILED(_env, status, false);
1123
+ return result;
1124
+ }
1125
+
1126
+ inline bool Object::Delete(Value key) {
1127
+ bool result;
1128
+ napi_status status = napi_delete_property(_env, _value, key, &result);
1129
+ NAPI_THROW_IF_FAILED(_env, status, false);
1130
+ return result;
1131
+ }
1132
+
1133
+ inline bool Object::Delete(const char* utf8name) {
1134
+ return Delete(String::New(_env, utf8name));
1135
+ }
1136
+
1137
+ inline bool Object::Delete(const std::string& utf8name) {
1138
+ return Delete(String::New(_env, utf8name));
1139
+ }
1140
+
1141
+ inline bool Object::Has(uint32_t index) const {
1142
+ bool result;
1143
+ napi_status status = napi_has_element(_env, _value, index, &result);
1144
+ NAPI_THROW_IF_FAILED(_env, status, false);
1145
+ return result;
1146
+ }
1147
+
1148
+ inline Value Object::Get(uint32_t index) const {
1149
+ napi_value value;
1150
+ napi_status status = napi_get_element(_env, _value, index, &value);
1151
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1152
+ return Value(_env, value);
1153
+ }
1154
+
1155
+ template <typename ValueType>
1156
+ inline void Object::Set(uint32_t index, const ValueType& value) {
1157
+ napi_status status =
1158
+ napi_set_element(_env, _value, index, Value::From(_env, value));
1159
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1160
+ }
1161
+
1162
+ inline bool Object::Delete(uint32_t index) {
1163
+ bool result;
1164
+ napi_status status = napi_delete_element(_env, _value, index, &result);
1165
+ NAPI_THROW_IF_FAILED(_env, status, false);
1166
+ return result;
1167
+ }
1168
+
1169
+ inline Array Object::GetPropertyNames() const {
1170
+ napi_value result;
1171
+ napi_status status = napi_get_property_names(_env, _value, &result);
1172
+ NAPI_THROW_IF_FAILED(_env, status, Array());
1173
+ return Array(_env, result);
1174
+ }
1175
+
1176
+ inline void Object::DefineProperty(const PropertyDescriptor& property) {
1177
+ napi_status status = napi_define_properties(_env, _value, 1,
1178
+ reinterpret_cast<const napi_property_descriptor*>(&property));
1179
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1180
+ }
1181
+
1182
+ inline void Object::DefineProperties(const std::initializer_list<PropertyDescriptor>& properties) {
1183
+ napi_status status = napi_define_properties(_env, _value, properties.size(),
1184
+ reinterpret_cast<const napi_property_descriptor*>(properties.begin()));
1185
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1186
+ }
1187
+
1188
+ inline void Object::DefineProperties(const std::vector<PropertyDescriptor>& properties) {
1189
+ napi_status status = napi_define_properties(_env, _value, properties.size(),
1190
+ reinterpret_cast<const napi_property_descriptor*>(properties.data()));
1191
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1192
+ }
1193
+
1194
+ inline bool Object::InstanceOf(const Function& constructor) const {
1195
+ bool result;
1196
+ napi_status status = napi_instanceof(_env, _value, constructor, &result);
1197
+ NAPI_THROW_IF_FAILED(_env, status, false);
1198
+ return result;
1199
+ }
1200
+
1201
+ template <typename Finalizer, typename T>
1202
+ inline void Object::AddFinalizer(Finalizer finalizeCallback, T* data) {
1203
+ details::FinalizeData<T, Finalizer>* finalizeData =
1204
+ new details::FinalizeData<T, Finalizer>({ finalizeCallback, nullptr });
1205
+ napi_status status =
1206
+ details::AttachData(_env,
1207
+ *this,
1208
+ data,
1209
+ details::FinalizeData<T, Finalizer>::Wrapper,
1210
+ finalizeData);
1211
+ if (status != napi_ok) {
1212
+ delete finalizeData;
1213
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1214
+ }
1215
+ }
1216
+
1217
+ template <typename Finalizer, typename T, typename Hint>
1218
+ inline void Object::AddFinalizer(Finalizer finalizeCallback,
1219
+ T* data,
1220
+ Hint* finalizeHint) {
1221
+ details::FinalizeData<T, Finalizer, Hint>* finalizeData =
1222
+ new details::FinalizeData<T, Finalizer, Hint>({ finalizeCallback, finalizeHint });
1223
+ napi_status status =
1224
+ details::AttachData(_env,
1225
+ *this,
1226
+ data,
1227
+ details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
1228
+ finalizeData);
1229
+ if (status != napi_ok) {
1230
+ delete finalizeData;
1231
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1232
+ }
1233
+ }
1234
+
1235
+ ////////////////////////////////////////////////////////////////////////////////
1236
+ // External class
1237
+ ////////////////////////////////////////////////////////////////////////////////
1238
+
1239
+ template <typename T>
1240
+ inline External<T> External<T>::New(napi_env env, T* data) {
1241
+ napi_value value;
1242
+ napi_status status = napi_create_external(env, data, nullptr, nullptr, &value);
1243
+ NAPI_THROW_IF_FAILED(env, status, External());
1244
+ return External(env, value);
1245
+ }
1246
+
1247
+ template <typename T>
1248
+ template <typename Finalizer>
1249
+ inline External<T> External<T>::New(napi_env env,
1250
+ T* data,
1251
+ Finalizer finalizeCallback) {
1252
+ napi_value value;
1253
+ details::FinalizeData<T, Finalizer>* finalizeData =
1254
+ new details::FinalizeData<T, Finalizer>({ finalizeCallback, nullptr });
1255
+ napi_status status = napi_create_external(
1256
+ env,
1257
+ data,
1258
+ details::FinalizeData<T, Finalizer>::Wrapper,
1259
+ finalizeData,
1260
+ &value);
1261
+ if (status != napi_ok) {
1262
+ delete finalizeData;
1263
+ NAPI_THROW_IF_FAILED(env, status, External());
1264
+ }
1265
+ return External(env, value);
1266
+ }
1267
+
1268
+ template <typename T>
1269
+ template <typename Finalizer, typename Hint>
1270
+ inline External<T> External<T>::New(napi_env env,
1271
+ T* data,
1272
+ Finalizer finalizeCallback,
1273
+ Hint* finalizeHint) {
1274
+ napi_value value;
1275
+ details::FinalizeData<T, Finalizer, Hint>* finalizeData =
1276
+ new details::FinalizeData<T, Finalizer, Hint>({ finalizeCallback, finalizeHint });
1277
+ napi_status status = napi_create_external(
1278
+ env,
1279
+ data,
1280
+ details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
1281
+ finalizeData,
1282
+ &value);
1283
+ if (status != napi_ok) {
1284
+ delete finalizeData;
1285
+ NAPI_THROW_IF_FAILED(env, status, External());
1286
+ }
1287
+ return External(env, value);
1288
+ }
1289
+
1290
+ template <typename T>
1291
+ inline External<T>::External() : Value() {
1292
+ }
1293
+
1294
+ template <typename T>
1295
+ inline External<T>::External(napi_env env, napi_value value) : Value(env, value) {
1296
+ }
1297
+
1298
+ template <typename T>
1299
+ inline T* External<T>::Data() const {
1300
+ void* data;
1301
+ napi_status status = napi_get_value_external(_env, _value, &data);
1302
+ NAPI_THROW_IF_FAILED(_env, status, nullptr);
1303
+ return reinterpret_cast<T*>(data);
1304
+ }
1305
+
1306
+ ////////////////////////////////////////////////////////////////////////////////
1307
+ // Array class
1308
+ ////////////////////////////////////////////////////////////////////////////////
1309
+
1310
+ inline Array Array::New(napi_env env) {
1311
+ napi_value value;
1312
+ napi_status status = napi_create_array(env, &value);
1313
+ NAPI_THROW_IF_FAILED(env, status, Array());
1314
+ return Array(env, value);
1315
+ }
1316
+
1317
+ inline Array Array::New(napi_env env, size_t length) {
1318
+ napi_value value;
1319
+ napi_status status = napi_create_array_with_length(env, length, &value);
1320
+ NAPI_THROW_IF_FAILED(env, status, Array());
1321
+ return Array(env, value);
1322
+ }
1323
+
1324
+ inline Array::Array() : Object() {
1325
+ }
1326
+
1327
+ inline Array::Array(napi_env env, napi_value value) : Object(env, value) {
1328
+ }
1329
+
1330
+ inline uint32_t Array::Length() const {
1331
+ uint32_t result;
1332
+ napi_status status = napi_get_array_length(_env, _value, &result);
1333
+ NAPI_THROW_IF_FAILED(_env, status, 0);
1334
+ return result;
1335
+ }
1336
+
1337
+ ////////////////////////////////////////////////////////////////////////////////
1338
+ // ArrayBuffer class
1339
+ ////////////////////////////////////////////////////////////////////////////////
1340
+
1341
+ inline ArrayBuffer ArrayBuffer::New(napi_env env, size_t byteLength) {
1342
+ napi_value value;
1343
+ void* data;
1344
+ napi_status status = napi_create_arraybuffer(env, byteLength, &data, &value);
1345
+ NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
1346
+
1347
+ return ArrayBuffer(env, value, data, byteLength);
1348
+ }
1349
+
1350
+ inline ArrayBuffer ArrayBuffer::New(napi_env env,
1351
+ void* externalData,
1352
+ size_t byteLength) {
1353
+ napi_value value;
1354
+ napi_status status = napi_create_external_arraybuffer(
1355
+ env, externalData, byteLength, nullptr, nullptr, &value);
1356
+ NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
1357
+
1358
+ return ArrayBuffer(env, value, externalData, byteLength);
1359
+ }
1360
+
1361
+ template <typename Finalizer>
1362
+ inline ArrayBuffer ArrayBuffer::New(napi_env env,
1363
+ void* externalData,
1364
+ size_t byteLength,
1365
+ Finalizer finalizeCallback) {
1366
+ napi_value value;
1367
+ details::FinalizeData<void, Finalizer>* finalizeData =
1368
+ new details::FinalizeData<void, Finalizer>({ finalizeCallback, nullptr });
1369
+ napi_status status = napi_create_external_arraybuffer(
1370
+ env,
1371
+ externalData,
1372
+ byteLength,
1373
+ details::FinalizeData<void, Finalizer>::Wrapper,
1374
+ finalizeData,
1375
+ &value);
1376
+ if (status != napi_ok) {
1377
+ delete finalizeData;
1378
+ NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
1379
+ }
1380
+
1381
+ return ArrayBuffer(env, value, externalData, byteLength);
1382
+ }
1383
+
1384
+ template <typename Finalizer, typename Hint>
1385
+ inline ArrayBuffer ArrayBuffer::New(napi_env env,
1386
+ void* externalData,
1387
+ size_t byteLength,
1388
+ Finalizer finalizeCallback,
1389
+ Hint* finalizeHint) {
1390
+ napi_value value;
1391
+ details::FinalizeData<void, Finalizer, Hint>* finalizeData =
1392
+ new details::FinalizeData<void, Finalizer, Hint>({ finalizeCallback, finalizeHint });
1393
+ napi_status status = napi_create_external_arraybuffer(
1394
+ env,
1395
+ externalData,
1396
+ byteLength,
1397
+ details::FinalizeData<void, Finalizer, Hint>::WrapperWithHint,
1398
+ finalizeData,
1399
+ &value);
1400
+ if (status != napi_ok) {
1401
+ delete finalizeData;
1402
+ NAPI_THROW_IF_FAILED(env, status, ArrayBuffer());
1403
+ }
1404
+
1405
+ return ArrayBuffer(env, value, externalData, byteLength);
1406
+ }
1407
+
1408
+ inline ArrayBuffer::ArrayBuffer() : Object(), _data(nullptr), _length(0) {
1409
+ }
1410
+
1411
+ inline ArrayBuffer::ArrayBuffer(napi_env env, napi_value value)
1412
+ : Object(env, value), _data(nullptr), _length(0) {
1413
+ }
1414
+
1415
+ inline ArrayBuffer::ArrayBuffer(napi_env env, napi_value value, void* data, size_t length)
1416
+ : Object(env, value), _data(data), _length(length) {
1417
+ }
1418
+
1419
+ inline void* ArrayBuffer::Data() {
1420
+ EnsureInfo();
1421
+ return _data;
1422
+ }
1423
+
1424
+ inline size_t ArrayBuffer::ByteLength() {
1425
+ EnsureInfo();
1426
+ return _length;
1427
+ }
1428
+
1429
+ inline void ArrayBuffer::EnsureInfo() const {
1430
+ // The ArrayBuffer instance may have been constructed from a napi_value whose
1431
+ // length/data are not yet known. Fetch and cache these values just once,
1432
+ // since they can never change during the lifetime of the ArrayBuffer.
1433
+ if (_data == nullptr) {
1434
+ napi_status status = napi_get_arraybuffer_info(_env, _value, &_data, &_length);
1435
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1436
+ }
1437
+ }
1438
+
1439
+ ////////////////////////////////////////////////////////////////////////////////
1440
+ // DataView class
1441
+ ////////////////////////////////////////////////////////////////////////////////
1442
+ inline DataView DataView::New(napi_env env,
1443
+ Napi::ArrayBuffer arrayBuffer) {
1444
+ return New(env, arrayBuffer, 0, arrayBuffer.ByteLength());
1445
+ }
1446
+
1447
+ inline DataView DataView::New(napi_env env,
1448
+ Napi::ArrayBuffer arrayBuffer,
1449
+ size_t byteOffset) {
1450
+ if (byteOffset > arrayBuffer.ByteLength()) {
1451
+ NAPI_THROW(RangeError::New(env,
1452
+ "Start offset is outside the bounds of the buffer"),
1453
+ DataView());
1454
+ }
1455
+ return New(env, arrayBuffer, byteOffset,
1456
+ arrayBuffer.ByteLength() - byteOffset);
1457
+ }
1458
+
1459
+ inline DataView DataView::New(napi_env env,
1460
+ Napi::ArrayBuffer arrayBuffer,
1461
+ size_t byteOffset,
1462
+ size_t byteLength) {
1463
+ if (byteOffset + byteLength > arrayBuffer.ByteLength()) {
1464
+ NAPI_THROW(RangeError::New(env, "Invalid DataView length"),
1465
+ DataView());
1466
+ }
1467
+ napi_value value;
1468
+ napi_status status = napi_create_dataview(
1469
+ env, byteLength, arrayBuffer, byteOffset, &value);
1470
+ NAPI_THROW_IF_FAILED(env, status, DataView());
1471
+ return DataView(env, value);
1472
+ }
1473
+
1474
+ inline DataView::DataView() : Object() {
1475
+ }
1476
+
1477
+ inline DataView::DataView(napi_env env, napi_value value) : Object(env, value) {
1478
+ napi_status status = napi_get_dataview_info(
1479
+ _env,
1480
+ _value /* dataView */,
1481
+ &_length /* byteLength */,
1482
+ &_data /* data */,
1483
+ nullptr /* arrayBuffer */,
1484
+ nullptr /* byteOffset */);
1485
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1486
+ }
1487
+
1488
+ inline Napi::ArrayBuffer DataView::ArrayBuffer() const {
1489
+ napi_value arrayBuffer;
1490
+ napi_status status = napi_get_dataview_info(
1491
+ _env,
1492
+ _value /* dataView */,
1493
+ nullptr /* byteLength */,
1494
+ nullptr /* data */,
1495
+ &arrayBuffer /* arrayBuffer */,
1496
+ nullptr /* byteOffset */);
1497
+ NAPI_THROW_IF_FAILED(_env, status, Napi::ArrayBuffer());
1498
+ return Napi::ArrayBuffer(_env, arrayBuffer);
1499
+ }
1500
+
1501
+ inline size_t DataView::ByteOffset() const {
1502
+ size_t byteOffset;
1503
+ napi_status status = napi_get_dataview_info(
1504
+ _env,
1505
+ _value /* dataView */,
1506
+ nullptr /* byteLength */,
1507
+ nullptr /* data */,
1508
+ nullptr /* arrayBuffer */,
1509
+ &byteOffset /* byteOffset */);
1510
+ NAPI_THROW_IF_FAILED(_env, status, 0);
1511
+ return byteOffset;
1512
+ }
1513
+
1514
+ inline size_t DataView::ByteLength() const {
1515
+ return _length;
1516
+ }
1517
+
1518
+ inline void* DataView::Data() const {
1519
+ return _data;
1520
+ }
1521
+
1522
+ inline float DataView::GetFloat32(size_t byteOffset) const {
1523
+ return ReadData<float>(byteOffset);
1524
+ }
1525
+
1526
+ inline double DataView::GetFloat64(size_t byteOffset) const {
1527
+ return ReadData<double>(byteOffset);
1528
+ }
1529
+
1530
+ inline int8_t DataView::GetInt8(size_t byteOffset) const {
1531
+ return ReadData<int8_t>(byteOffset);
1532
+ }
1533
+
1534
+ inline int16_t DataView::GetInt16(size_t byteOffset) const {
1535
+ return ReadData<int16_t>(byteOffset);
1536
+ }
1537
+
1538
+ inline int32_t DataView::GetInt32(size_t byteOffset) const {
1539
+ return ReadData<int32_t>(byteOffset);
1540
+ }
1541
+
1542
+ inline uint8_t DataView::GetUint8(size_t byteOffset) const {
1543
+ return ReadData<uint8_t>(byteOffset);
1544
+ }
1545
+
1546
+ inline uint16_t DataView::GetUint16(size_t byteOffset) const {
1547
+ return ReadData<uint16_t>(byteOffset);
1548
+ }
1549
+
1550
+ inline uint32_t DataView::GetUint32(size_t byteOffset) const {
1551
+ return ReadData<uint32_t>(byteOffset);
1552
+ }
1553
+
1554
+ inline void DataView::SetFloat32(size_t byteOffset, float value) const {
1555
+ WriteData<float>(byteOffset, value);
1556
+ }
1557
+
1558
+ inline void DataView::SetFloat64(size_t byteOffset, double value) const {
1559
+ WriteData<double>(byteOffset, value);
1560
+ }
1561
+
1562
+ inline void DataView::SetInt8(size_t byteOffset, int8_t value) const {
1563
+ WriteData<int8_t>(byteOffset, value);
1564
+ }
1565
+
1566
+ inline void DataView::SetInt16(size_t byteOffset, int16_t value) const {
1567
+ WriteData<int16_t>(byteOffset, value);
1568
+ }
1569
+
1570
+ inline void DataView::SetInt32(size_t byteOffset, int32_t value) const {
1571
+ WriteData<int32_t>(byteOffset, value);
1572
+ }
1573
+
1574
+ inline void DataView::SetUint8(size_t byteOffset, uint8_t value) const {
1575
+ WriteData<uint8_t>(byteOffset, value);
1576
+ }
1577
+
1578
+ inline void DataView::SetUint16(size_t byteOffset, uint16_t value) const {
1579
+ WriteData<uint16_t>(byteOffset, value);
1580
+ }
1581
+
1582
+ inline void DataView::SetUint32(size_t byteOffset, uint32_t value) const {
1583
+ WriteData<uint32_t>(byteOffset, value);
1584
+ }
1585
+
1586
+ template <typename T>
1587
+ inline T DataView::ReadData(size_t byteOffset) const {
1588
+ if (byteOffset + sizeof(T) > _length ||
1589
+ byteOffset + sizeof(T) < byteOffset) { // overflow
1590
+ NAPI_THROW(RangeError::New(_env,
1591
+ "Offset is outside the bounds of the DataView"), 0);
1592
+ }
1593
+
1594
+ return *reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset);
1595
+ }
1596
+
1597
+ template <typename T>
1598
+ inline void DataView::WriteData(size_t byteOffset, T value) const {
1599
+ if (byteOffset + sizeof(T) > _length ||
1600
+ byteOffset + sizeof(T) < byteOffset) { // overflow
1601
+ NAPI_THROW_VOID(RangeError::New(_env,
1602
+ "Offset is outside the bounds of the DataView"));
1603
+ }
1604
+
1605
+ *reinterpret_cast<T*>(static_cast<uint8_t*>(_data) + byteOffset) = value;
1606
+ }
1607
+
1608
+ ////////////////////////////////////////////////////////////////////////////////
1609
+ // TypedArray class
1610
+ ////////////////////////////////////////////////////////////////////////////////
1611
+
1612
+ inline TypedArray::TypedArray()
1613
+ : Object(), _type(TypedArray::unknown_array_type), _length(0) {
1614
+ }
1615
+
1616
+ inline TypedArray::TypedArray(napi_env env, napi_value value)
1617
+ : Object(env, value), _type(TypedArray::unknown_array_type), _length(0) {
1618
+ }
1619
+
1620
+ inline TypedArray::TypedArray(napi_env env,
1621
+ napi_value value,
1622
+ napi_typedarray_type type,
1623
+ size_t length)
1624
+ : Object(env, value), _type(type), _length(length) {
1625
+ }
1626
+
1627
+ inline napi_typedarray_type TypedArray::TypedArrayType() const {
1628
+ if (_type == TypedArray::unknown_array_type) {
1629
+ napi_status status = napi_get_typedarray_info(_env, _value,
1630
+ &const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
1631
+ nullptr, nullptr, nullptr);
1632
+ NAPI_THROW_IF_FAILED(_env, status, napi_int8_array);
1633
+ }
1634
+
1635
+ return _type;
1636
+ }
1637
+
1638
+ inline uint8_t TypedArray::ElementSize() const {
1639
+ switch (TypedArrayType()) {
1640
+ case napi_int8_array:
1641
+ case napi_uint8_array:
1642
+ case napi_uint8_clamped_array:
1643
+ return 1;
1644
+ case napi_int16_array:
1645
+ case napi_uint16_array:
1646
+ return 2;
1647
+ case napi_int32_array:
1648
+ case napi_uint32_array:
1649
+ case napi_float32_array:
1650
+ return 4;
1651
+ case napi_float64_array:
1652
+ return 8;
1653
+ default:
1654
+ return 0;
1655
+ }
1656
+ }
1657
+
1658
+ inline size_t TypedArray::ElementLength() const {
1659
+ if (_type == TypedArray::unknown_array_type) {
1660
+ napi_status status = napi_get_typedarray_info(_env, _value,
1661
+ &const_cast<TypedArray*>(this)->_type, &const_cast<TypedArray*>(this)->_length,
1662
+ nullptr, nullptr, nullptr);
1663
+ NAPI_THROW_IF_FAILED(_env, status, 0);
1664
+ }
1665
+
1666
+ return _length;
1667
+ }
1668
+
1669
+ inline size_t TypedArray::ByteOffset() const {
1670
+ size_t byteOffset;
1671
+ napi_status status = napi_get_typedarray_info(
1672
+ _env, _value, nullptr, nullptr, nullptr, nullptr, &byteOffset);
1673
+ NAPI_THROW_IF_FAILED(_env, status, 0);
1674
+ return byteOffset;
1675
+ }
1676
+
1677
+ inline size_t TypedArray::ByteLength() const {
1678
+ return ElementSize() * ElementLength();
1679
+ }
1680
+
1681
+ inline Napi::ArrayBuffer TypedArray::ArrayBuffer() const {
1682
+ napi_value arrayBuffer;
1683
+ napi_status status = napi_get_typedarray_info(
1684
+ _env, _value, nullptr, nullptr, nullptr, &arrayBuffer, nullptr);
1685
+ NAPI_THROW_IF_FAILED(_env, status, Napi::ArrayBuffer());
1686
+ return Napi::ArrayBuffer(_env, arrayBuffer);
1687
+ }
1688
+
1689
+ ////////////////////////////////////////////////////////////////////////////////
1690
+ // TypedArrayOf<T> class
1691
+ ////////////////////////////////////////////////////////////////////////////////
1692
+
1693
+ template <typename T>
1694
+ inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
1695
+ size_t elementLength,
1696
+ napi_typedarray_type type) {
1697
+ Napi::ArrayBuffer arrayBuffer = Napi::ArrayBuffer::New(env, elementLength * sizeof (T));
1698
+ return New(env, elementLength, arrayBuffer, 0, type);
1699
+ }
1700
+
1701
+ template <typename T>
1702
+ inline TypedArrayOf<T> TypedArrayOf<T>::New(napi_env env,
1703
+ size_t elementLength,
1704
+ Napi::ArrayBuffer arrayBuffer,
1705
+ size_t bufferOffset,
1706
+ napi_typedarray_type type) {
1707
+ napi_value value;
1708
+ napi_status status = napi_create_typedarray(
1709
+ env, type, elementLength, arrayBuffer, bufferOffset, &value);
1710
+ NAPI_THROW_IF_FAILED(env, status, TypedArrayOf<T>());
1711
+
1712
+ return TypedArrayOf<T>(
1713
+ env, value, type, elementLength,
1714
+ reinterpret_cast<T*>(reinterpret_cast<uint8_t*>(arrayBuffer.Data()) + bufferOffset));
1715
+ }
1716
+
1717
+ template <typename T>
1718
+ inline TypedArrayOf<T>::TypedArrayOf() : TypedArray(), _data(nullptr) {
1719
+ }
1720
+
1721
+ template <typename T>
1722
+ inline TypedArrayOf<T>::TypedArrayOf(napi_env env, napi_value value)
1723
+ : TypedArray(env, value), _data(nullptr) {
1724
+ napi_status status = napi_get_typedarray_info(
1725
+ _env, _value, &_type, &_length, reinterpret_cast<void**>(&_data), nullptr, nullptr);
1726
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1727
+ }
1728
+
1729
+ template <typename T>
1730
+ inline TypedArrayOf<T>::TypedArrayOf(napi_env env,
1731
+ napi_value value,
1732
+ napi_typedarray_type type,
1733
+ size_t length,
1734
+ T* data)
1735
+ : TypedArray(env, value, type, length), _data(data) {
1736
+ if (!(type == TypedArrayTypeForPrimitiveType<T>() ||
1737
+ (type == napi_uint8_clamped_array && std::is_same<T, uint8_t>::value))) {
1738
+ NAPI_THROW_VOID(TypeError::New(env, "Array type must match the template parameter. "
1739
+ "(Uint8 arrays may optionally have the \"clamped\" array type.)"));
1740
+ }
1741
+ }
1742
+
1743
+ template <typename T>
1744
+ inline T& TypedArrayOf<T>::operator [](size_t index) {
1745
+ return _data[index];
1746
+ }
1747
+
1748
+ template <typename T>
1749
+ inline const T& TypedArrayOf<T>::operator [](size_t index) const {
1750
+ return _data[index];
1751
+ }
1752
+
1753
+ template <typename T>
1754
+ inline T* TypedArrayOf<T>::Data() {
1755
+ return _data;
1756
+ }
1757
+
1758
+ template <typename T>
1759
+ inline const T* TypedArrayOf<T>::Data() const {
1760
+ return _data;
1761
+ }
1762
+
1763
+ ////////////////////////////////////////////////////////////////////////////////
1764
+ // Function class
1765
+ ////////////////////////////////////////////////////////////////////////////////
1766
+
1767
+ template <typename CbData>
1768
+ static inline napi_status
1769
+ CreateFunction(napi_env env,
1770
+ const char* utf8name,
1771
+ napi_callback cb,
1772
+ CbData* data,
1773
+ napi_value* result) {
1774
+ napi_status status =
1775
+ napi_create_function(env, utf8name, NAPI_AUTO_LENGTH, cb, data, result);
1776
+ if (status == napi_ok) {
1777
+ status = Napi::details::AttachData(env, *result, data);
1778
+ }
1779
+
1780
+ return status;
1781
+ }
1782
+
1783
+ template <typename Callable>
1784
+ inline Function Function::New(napi_env env,
1785
+ Callable cb,
1786
+ const char* utf8name,
1787
+ void* data) {
1788
+ typedef decltype(cb(CallbackInfo(nullptr, nullptr))) ReturnType;
1789
+ typedef details::CallbackData<Callable, ReturnType> CbData;
1790
+ auto callbackData = new CbData({ cb, data });
1791
+
1792
+ napi_value value;
1793
+ napi_status status = CreateFunction(env,
1794
+ utf8name,
1795
+ CbData::Wrapper,
1796
+ callbackData,
1797
+ &value);
1798
+ if (status != napi_ok) {
1799
+ delete callbackData;
1800
+ NAPI_THROW_IF_FAILED(env, status, Function());
1801
+ }
1802
+
1803
+ return Function(env, value);
1804
+ }
1805
+
1806
+ template <typename Callable>
1807
+ inline Function Function::New(napi_env env,
1808
+ Callable cb,
1809
+ const std::string& utf8name,
1810
+ void* data) {
1811
+ return New(env, cb, utf8name.c_str(), data);
1812
+ }
1813
+
1814
+ inline Function::Function() : Object() {
1815
+ }
1816
+
1817
+ inline Function::Function(napi_env env, napi_value value) : Object(env, value) {
1818
+ }
1819
+
1820
+ inline Value Function::operator ()(const std::initializer_list<napi_value>& args) const {
1821
+ return Call(Env().Undefined(), args);
1822
+ }
1823
+
1824
+ inline Value Function::Call(const std::initializer_list<napi_value>& args) const {
1825
+ return Call(Env().Undefined(), args);
1826
+ }
1827
+
1828
+ inline Value Function::Call(const std::vector<napi_value>& args) const {
1829
+ return Call(Env().Undefined(), args);
1830
+ }
1831
+
1832
+ inline Value Function::Call(size_t argc, const napi_value* args) const {
1833
+ return Call(Env().Undefined(), argc, args);
1834
+ }
1835
+
1836
+ inline Value Function::Call(napi_value recv, const std::initializer_list<napi_value>& args) const {
1837
+ return Call(recv, args.size(), args.begin());
1838
+ }
1839
+
1840
+ inline Value Function::Call(napi_value recv, const std::vector<napi_value>& args) const {
1841
+ return Call(recv, args.size(), args.data());
1842
+ }
1843
+
1844
+ inline Value Function::Call(napi_value recv, size_t argc, const napi_value* args) const {
1845
+ napi_value result;
1846
+ napi_status status = napi_call_function(
1847
+ _env, recv, _value, argc, args, &result);
1848
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1849
+ return Value(_env, result);
1850
+ }
1851
+
1852
+ inline Value Function::MakeCallback(
1853
+ napi_value recv,
1854
+ const std::initializer_list<napi_value>& args,
1855
+ napi_async_context context) const {
1856
+ return MakeCallback(recv, args.size(), args.begin(), context);
1857
+ }
1858
+
1859
+ inline Value Function::MakeCallback(
1860
+ napi_value recv,
1861
+ const std::vector<napi_value>& args,
1862
+ napi_async_context context) const {
1863
+ return MakeCallback(recv, args.size(), args.data(), context);
1864
+ }
1865
+
1866
+ inline Value Function::MakeCallback(
1867
+ napi_value recv,
1868
+ size_t argc,
1869
+ const napi_value* args,
1870
+ napi_async_context context) const {
1871
+ napi_value result;
1872
+ napi_status status = napi_make_callback(
1873
+ _env, context, recv, _value, argc, args, &result);
1874
+ NAPI_THROW_IF_FAILED(_env, status, Value());
1875
+ return Value(_env, result);
1876
+ }
1877
+
1878
+ inline Object Function::New(const std::initializer_list<napi_value>& args) const {
1879
+ return New(args.size(), args.begin());
1880
+ }
1881
+
1882
+ inline Object Function::New(const std::vector<napi_value>& args) const {
1883
+ return New(args.size(), args.data());
1884
+ }
1885
+
1886
+ inline Object Function::New(size_t argc, const napi_value* args) const {
1887
+ napi_value result;
1888
+ napi_status status = napi_new_instance(
1889
+ _env, _value, argc, args, &result);
1890
+ NAPI_THROW_IF_FAILED(_env, status, Object());
1891
+ return Object(_env, result);
1892
+ }
1893
+
1894
+ ////////////////////////////////////////////////////////////////////////////////
1895
+ // Promise class
1896
+ ////////////////////////////////////////////////////////////////////////////////
1897
+
1898
+ inline Promise::Deferred Promise::Deferred::New(napi_env env) {
1899
+ return Promise::Deferred(env);
1900
+ }
1901
+
1902
+ inline Promise::Deferred::Deferred(napi_env env) : _env(env) {
1903
+ napi_status status = napi_create_promise(_env, &_deferred, &_promise);
1904
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1905
+ }
1906
+
1907
+ inline Promise Promise::Deferred::Promise() const {
1908
+ return Napi::Promise(_env, _promise);
1909
+ }
1910
+
1911
+ inline Napi::Env Promise::Deferred::Env() const {
1912
+ return Napi::Env(_env);
1913
+ }
1914
+
1915
+ inline void Promise::Deferred::Resolve(napi_value value) const {
1916
+ napi_status status = napi_resolve_deferred(_env, _deferred, value);
1917
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1918
+ }
1919
+
1920
+ inline void Promise::Deferred::Reject(napi_value value) const {
1921
+ napi_status status = napi_reject_deferred(_env, _deferred, value);
1922
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
1923
+ }
1924
+
1925
+ inline Promise::Promise(napi_env env, napi_value value) : Object(env, value) {
1926
+ }
1927
+
1928
+ ////////////////////////////////////////////////////////////////////////////////
1929
+ // Buffer<T> class
1930
+ ////////////////////////////////////////////////////////////////////////////////
1931
+
1932
+ template <typename T>
1933
+ inline Buffer<T> Buffer<T>::New(napi_env env, size_t length) {
1934
+ napi_value value;
1935
+ void* data;
1936
+ napi_status status = napi_create_buffer(env, length * sizeof (T), &data, &value);
1937
+ NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
1938
+ return Buffer(env, value, length, static_cast<T*>(data));
1939
+ }
1940
+
1941
+ template <typename T>
1942
+ inline Buffer<T> Buffer<T>::New(napi_env env, T* data, size_t length) {
1943
+ napi_value value;
1944
+ napi_status status = napi_create_external_buffer(
1945
+ env, length * sizeof (T), data, nullptr, nullptr, &value);
1946
+ NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
1947
+ return Buffer(env, value, length, data);
1948
+ }
1949
+
1950
+ template <typename T>
1951
+ template <typename Finalizer>
1952
+ inline Buffer<T> Buffer<T>::New(napi_env env,
1953
+ T* data,
1954
+ size_t length,
1955
+ Finalizer finalizeCallback) {
1956
+ napi_value value;
1957
+ details::FinalizeData<T, Finalizer>* finalizeData =
1958
+ new details::FinalizeData<T, Finalizer>({ finalizeCallback, nullptr });
1959
+ napi_status status = napi_create_external_buffer(
1960
+ env,
1961
+ length * sizeof (T),
1962
+ data,
1963
+ details::FinalizeData<T, Finalizer>::Wrapper,
1964
+ finalizeData,
1965
+ &value);
1966
+ if (status != napi_ok) {
1967
+ delete finalizeData;
1968
+ NAPI_THROW_IF_FAILED(env, status, Buffer());
1969
+ }
1970
+ return Buffer(env, value, length, data);
1971
+ }
1972
+
1973
+ template <typename T>
1974
+ template <typename Finalizer, typename Hint>
1975
+ inline Buffer<T> Buffer<T>::New(napi_env env,
1976
+ T* data,
1977
+ size_t length,
1978
+ Finalizer finalizeCallback,
1979
+ Hint* finalizeHint) {
1980
+ napi_value value;
1981
+ details::FinalizeData<T, Finalizer, Hint>* finalizeData =
1982
+ new details::FinalizeData<T, Finalizer, Hint>({ finalizeCallback, finalizeHint });
1983
+ napi_status status = napi_create_external_buffer(
1984
+ env,
1985
+ length * sizeof (T),
1986
+ data,
1987
+ details::FinalizeData<T, Finalizer, Hint>::WrapperWithHint,
1988
+ finalizeData,
1989
+ &value);
1990
+ if (status != napi_ok) {
1991
+ delete finalizeData;
1992
+ NAPI_THROW_IF_FAILED(env, status, Buffer());
1993
+ }
1994
+ return Buffer(env, value, length, data);
1995
+ }
1996
+
1997
+ template <typename T>
1998
+ inline Buffer<T> Buffer<T>::Copy(napi_env env, const T* data, size_t length) {
1999
+ napi_value value;
2000
+ napi_status status = napi_create_buffer_copy(
2001
+ env, length * sizeof (T), data, nullptr, &value);
2002
+ NAPI_THROW_IF_FAILED(env, status, Buffer<T>());
2003
+ return Buffer<T>(env, value);
2004
+ }
2005
+
2006
+ template <typename T>
2007
+ inline Buffer<T>::Buffer() : Uint8Array(), _length(0), _data(nullptr) {
2008
+ }
2009
+
2010
+ template <typename T>
2011
+ inline Buffer<T>::Buffer(napi_env env, napi_value value)
2012
+ : Uint8Array(env, value), _length(0), _data(nullptr) {
2013
+ }
2014
+
2015
+ template <typename T>
2016
+ inline Buffer<T>::Buffer(napi_env env, napi_value value, size_t length, T* data)
2017
+ : Uint8Array(env, value), _length(length), _data(data) {
2018
+ }
2019
+
2020
+ template <typename T>
2021
+ inline size_t Buffer<T>::Length() const {
2022
+ EnsureInfo();
2023
+ return _length;
2024
+ }
2025
+
2026
+ template <typename T>
2027
+ inline T* Buffer<T>::Data() const {
2028
+ EnsureInfo();
2029
+ return _data;
2030
+ }
2031
+
2032
+ template <typename T>
2033
+ inline void Buffer<T>::EnsureInfo() const {
2034
+ // The Buffer instance may have been constructed from a napi_value whose
2035
+ // length/data are not yet known. Fetch and cache these values just once,
2036
+ // since they can never change during the lifetime of the Buffer.
2037
+ if (_data == nullptr) {
2038
+ size_t byteLength;
2039
+ void* voidData;
2040
+ napi_status status = napi_get_buffer_info(_env, _value, &voidData, &byteLength);
2041
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
2042
+ _length = byteLength / sizeof (T);
2043
+ _data = static_cast<T*>(voidData);
2044
+ }
2045
+ }
2046
+
2047
+ ////////////////////////////////////////////////////////////////////////////////
2048
+ // Error class
2049
+ ////////////////////////////////////////////////////////////////////////////////
2050
+
2051
+ inline Error Error::New(napi_env env) {
2052
+ napi_status status;
2053
+ napi_value error = nullptr;
2054
+
2055
+ const napi_extended_error_info* info;
2056
+ status = napi_get_last_error_info(env, &info);
2057
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_last_error_info");
2058
+
2059
+ if (info->error_code == napi_pending_exception) {
2060
+ status = napi_get_and_clear_last_exception(env, &error);
2061
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
2062
+ }
2063
+ else {
2064
+ const char* error_message = info->error_message != nullptr ?
2065
+ info->error_message : "Error in native callback";
2066
+
2067
+ bool isExceptionPending;
2068
+ status = napi_is_exception_pending(env, &isExceptionPending);
2069
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_is_exception_pending");
2070
+
2071
+ if (isExceptionPending) {
2072
+ status = napi_get_and_clear_last_exception(env, &error);
2073
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_get_and_clear_last_exception");
2074
+ }
2075
+
2076
+ napi_value message;
2077
+ status = napi_create_string_utf8(
2078
+ env,
2079
+ error_message,
2080
+ std::strlen(error_message),
2081
+ &message);
2082
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_string_utf8");
2083
+
2084
+ switch (info->error_code) {
2085
+ case napi_object_expected:
2086
+ case napi_string_expected:
2087
+ case napi_boolean_expected:
2088
+ case napi_number_expected:
2089
+ status = napi_create_type_error(env, nullptr, message, &error);
2090
+ break;
2091
+ default:
2092
+ status = napi_create_error(env, nullptr, message, &error);
2093
+ break;
2094
+ }
2095
+ NAPI_FATAL_IF_FAILED(status, "Error::New", "napi_create_error");
2096
+ }
2097
+
2098
+ return Error(env, error);
2099
+ }
2100
+
2101
+ inline Error Error::New(napi_env env, const char* message) {
2102
+ return Error::New<Error>(env, message, std::strlen(message), napi_create_error);
2103
+ }
2104
+
2105
+ inline Error Error::New(napi_env env, const std::string& message) {
2106
+ return Error::New<Error>(env, message.c_str(), message.size(), napi_create_error);
2107
+ }
2108
+
2109
+ inline NAPI_NO_RETURN void Error::Fatal(const char* location, const char* message) {
2110
+ napi_fatal_error(location, NAPI_AUTO_LENGTH, message, NAPI_AUTO_LENGTH);
2111
+ }
2112
+
2113
+ inline Error::Error() : ObjectReference() {
2114
+ }
2115
+
2116
+ inline Error::Error(napi_env env, napi_value value) : ObjectReference(env, nullptr) {
2117
+ if (value != nullptr) {
2118
+ napi_status status = napi_create_reference(env, value, 1, &_ref);
2119
+
2120
+ // Avoid infinite recursion in the failure case.
2121
+ // Don't try to construct & throw another Error instance.
2122
+ NAPI_FATAL_IF_FAILED(status, "Error::Error", "napi_create_reference");
2123
+ }
2124
+ }
2125
+
2126
+ inline Error::Error(Error&& other) : ObjectReference(std::move(other)) {
2127
+ }
2128
+
2129
+ inline Error& Error::operator =(Error&& other) {
2130
+ static_cast<Reference<Object>*>(this)->operator=(std::move(other));
2131
+ return *this;
2132
+ }
2133
+
2134
+ inline Error::Error(const Error& other) : ObjectReference(other) {
2135
+ }
2136
+
2137
+ inline Error& Error::operator =(Error& other) {
2138
+ Reset();
2139
+
2140
+ _env = other.Env();
2141
+ HandleScope scope(_env);
2142
+
2143
+ napi_value value = other.Value();
2144
+ if (value != nullptr) {
2145
+ napi_status status = napi_create_reference(_env, value, 1, &_ref);
2146
+ NAPI_THROW_IF_FAILED(_env, status, *this);
2147
+ }
2148
+
2149
+ return *this;
2150
+ }
2151
+
2152
+ inline const std::string& Error::Message() const NAPI_NOEXCEPT {
2153
+ if (_message.size() == 0 && _env != nullptr) {
2154
+ #ifdef NAPI_CPP_EXCEPTIONS
2155
+ try {
2156
+ _message = Get("message").As<String>();
2157
+ }
2158
+ catch (...) {
2159
+ // Catch all errors here, to include e.g. a std::bad_alloc from
2160
+ // the std::string::operator=, because this method may not throw.
2161
+ }
2162
+ #else // NAPI_CPP_EXCEPTIONS
2163
+ _message = Get("message").As<String>();
2164
+ #endif // NAPI_CPP_EXCEPTIONS
2165
+ }
2166
+ return _message;
2167
+ }
2168
+
2169
+ inline void Error::ThrowAsJavaScriptException() const {
2170
+ HandleScope scope(_env);
2171
+ if (!IsEmpty()) {
2172
+
2173
+ // We intentionally don't use `NAPI_THROW_*` macros here to ensure
2174
+ // that there is no possible recursion as `ThrowAsJavaScriptException`
2175
+ // is part of `NAPI_THROW_*` macro definition for noexcept.
2176
+
2177
+ napi_status status = napi_throw(_env, Value());
2178
+
2179
+ #ifdef NAPI_CPP_EXCEPTIONS
2180
+ if (status != napi_ok) {
2181
+ throw Error::New(_env);
2182
+ }
2183
+ #else // NAPI_CPP_EXCEPTIONS
2184
+ NAPI_FATAL_IF_FAILED(status, "Error::ThrowAsJavaScriptException", "napi_throw");
2185
+ #endif // NAPI_CPP_EXCEPTIONS
2186
+ }
2187
+ }
2188
+
2189
+ #ifdef NAPI_CPP_EXCEPTIONS
2190
+
2191
+ inline const char* Error::what() const NAPI_NOEXCEPT {
2192
+ return Message().c_str();
2193
+ }
2194
+
2195
+ #endif // NAPI_CPP_EXCEPTIONS
2196
+
2197
+ template <typename TError>
2198
+ inline TError Error::New(napi_env env,
2199
+ const char* message,
2200
+ size_t length,
2201
+ create_error_fn create_error) {
2202
+ napi_value str;
2203
+ napi_status status = napi_create_string_utf8(env, message, length, &str);
2204
+ NAPI_THROW_IF_FAILED(env, status, TError());
2205
+
2206
+ napi_value error;
2207
+ status = create_error(env, nullptr, str, &error);
2208
+ NAPI_THROW_IF_FAILED(env, status, TError());
2209
+
2210
+ return TError(env, error);
2211
+ }
2212
+
2213
+ inline TypeError TypeError::New(napi_env env, const char* message) {
2214
+ return Error::New<TypeError>(env, message, std::strlen(message), napi_create_type_error);
2215
+ }
2216
+
2217
+ inline TypeError TypeError::New(napi_env env, const std::string& message) {
2218
+ return Error::New<TypeError>(env, message.c_str(), message.size(), napi_create_type_error);
2219
+ }
2220
+
2221
+ inline TypeError::TypeError() : Error() {
2222
+ }
2223
+
2224
+ inline TypeError::TypeError(napi_env env, napi_value value) : Error(env, value) {
2225
+ }
2226
+
2227
+ inline RangeError RangeError::New(napi_env env, const char* message) {
2228
+ return Error::New<RangeError>(env, message, std::strlen(message), napi_create_range_error);
2229
+ }
2230
+
2231
+ inline RangeError RangeError::New(napi_env env, const std::string& message) {
2232
+ return Error::New<RangeError>(env, message.c_str(), message.size(), napi_create_range_error);
2233
+ }
2234
+
2235
+ inline RangeError::RangeError() : Error() {
2236
+ }
2237
+
2238
+ inline RangeError::RangeError(napi_env env, napi_value value) : Error(env, value) {
2239
+ }
2240
+
2241
+ ////////////////////////////////////////////////////////////////////////////////
2242
+ // Reference<T> class
2243
+ ////////////////////////////////////////////////////////////////////////////////
2244
+
2245
+ template <typename T>
2246
+ inline Reference<T> Reference<T>::New(const T& value, uint32_t initialRefcount) {
2247
+ napi_env env = value.Env();
2248
+ napi_value val = value;
2249
+
2250
+ if (val == nullptr) {
2251
+ return Reference<T>(env, nullptr);
2252
+ }
2253
+
2254
+ napi_ref ref;
2255
+ napi_status status = napi_create_reference(env, value, initialRefcount, &ref);
2256
+ NAPI_THROW_IF_FAILED(env, status, Reference<T>());
2257
+
2258
+ return Reference<T>(env, ref);
2259
+ }
2260
+
2261
+
2262
+ template <typename T>
2263
+ inline Reference<T>::Reference() : _env(nullptr), _ref(nullptr), _suppressDestruct(false) {
2264
+ }
2265
+
2266
+ template <typename T>
2267
+ inline Reference<T>::Reference(napi_env env, napi_ref ref)
2268
+ : _env(env), _ref(ref), _suppressDestruct(false) {
2269
+ }
2270
+
2271
+ template <typename T>
2272
+ inline Reference<T>::~Reference() {
2273
+ if (_ref != nullptr) {
2274
+ if (!_suppressDestruct) {
2275
+ napi_delete_reference(_env, _ref);
2276
+ }
2277
+
2278
+ _ref = nullptr;
2279
+ }
2280
+ }
2281
+
2282
+ template <typename T>
2283
+ inline Reference<T>::Reference(Reference<T>&& other)
2284
+ : _env(other._env), _ref(other._ref), _suppressDestruct(other._suppressDestruct) {
2285
+ other._env = nullptr;
2286
+ other._ref = nullptr;
2287
+ other._suppressDestruct = false;
2288
+ }
2289
+
2290
+ template <typename T>
2291
+ inline Reference<T>& Reference<T>::operator =(Reference<T>&& other) {
2292
+ Reset();
2293
+ _env = other._env;
2294
+ _ref = other._ref;
2295
+ _suppressDestruct = other._suppressDestruct;
2296
+ other._env = nullptr;
2297
+ other._ref = nullptr;
2298
+ other._suppressDestruct = false;
2299
+ return *this;
2300
+ }
2301
+
2302
+ template <typename T>
2303
+ inline Reference<T>::Reference(const Reference<T>& other)
2304
+ : _env(other._env), _ref(nullptr), _suppressDestruct(false) {
2305
+ HandleScope scope(_env);
2306
+
2307
+ napi_value value = other.Value();
2308
+ if (value != nullptr) {
2309
+ // Copying is a limited scenario (currently only used for Error object) and always creates a
2310
+ // strong reference to the given value even if the incoming reference is weak.
2311
+ napi_status status = napi_create_reference(_env, value, 1, &_ref);
2312
+ NAPI_FATAL_IF_FAILED(status, "Reference<T>::Reference", "napi_create_reference");
2313
+ }
2314
+ }
2315
+
2316
+ template <typename T>
2317
+ inline Reference<T>::operator napi_ref() const {
2318
+ return _ref;
2319
+ }
2320
+
2321
+ template <typename T>
2322
+ inline bool Reference<T>::operator ==(const Reference<T> &other) const {
2323
+ HandleScope scope(_env);
2324
+ return this->Value().StrictEquals(other.Value());
2325
+ }
2326
+
2327
+ template <typename T>
2328
+ inline bool Reference<T>::operator !=(const Reference<T> &other) const {
2329
+ return !this->operator ==(other);
2330
+ }
2331
+
2332
+ template <typename T>
2333
+ inline Napi::Env Reference<T>::Env() const {
2334
+ return Napi::Env(_env);
2335
+ }
2336
+
2337
+ template <typename T>
2338
+ inline bool Reference<T>::IsEmpty() const {
2339
+ return _ref == nullptr;
2340
+ }
2341
+
2342
+ template <typename T>
2343
+ inline T Reference<T>::Value() const {
2344
+ if (_ref == nullptr) {
2345
+ return T(_env, nullptr);
2346
+ }
2347
+
2348
+ napi_value value;
2349
+ napi_status status = napi_get_reference_value(_env, _ref, &value);
2350
+ NAPI_THROW_IF_FAILED(_env, status, T());
2351
+ return T(_env, value);
2352
+ }
2353
+
2354
+ template <typename T>
2355
+ inline uint32_t Reference<T>::Ref() {
2356
+ uint32_t result;
2357
+ napi_status status = napi_reference_ref(_env, _ref, &result);
2358
+ NAPI_THROW_IF_FAILED(_env, status, 1);
2359
+ return result;
2360
+ }
2361
+
2362
+ template <typename T>
2363
+ inline uint32_t Reference<T>::Unref() {
2364
+ uint32_t result;
2365
+ napi_status status = napi_reference_unref(_env, _ref, &result);
2366
+ NAPI_THROW_IF_FAILED(_env, status, 1);
2367
+ return result;
2368
+ }
2369
+
2370
+ template <typename T>
2371
+ inline void Reference<T>::Reset() {
2372
+ if (_ref != nullptr) {
2373
+ napi_status status = napi_delete_reference(_env, _ref);
2374
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
2375
+ _ref = nullptr;
2376
+ }
2377
+ }
2378
+
2379
+ template <typename T>
2380
+ inline void Reference<T>::Reset(const T& value, uint32_t refcount) {
2381
+ Reset();
2382
+ _env = value.Env();
2383
+
2384
+ napi_value val = value;
2385
+ if (val != nullptr) {
2386
+ napi_status status = napi_create_reference(_env, value, refcount, &_ref);
2387
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
2388
+ }
2389
+ }
2390
+
2391
+ template <typename T>
2392
+ inline void Reference<T>::SuppressDestruct() {
2393
+ _suppressDestruct = true;
2394
+ }
2395
+
2396
+ template <typename T>
2397
+ inline Reference<T> Weak(T value) {
2398
+ return Reference<T>::New(value, 0);
2399
+ }
2400
+
2401
+ inline ObjectReference Weak(Object value) {
2402
+ return Reference<Object>::New(value, 0);
2403
+ }
2404
+
2405
+ inline FunctionReference Weak(Function value) {
2406
+ return Reference<Function>::New(value, 0);
2407
+ }
2408
+
2409
+ template <typename T>
2410
+ inline Reference<T> Persistent(T value) {
2411
+ return Reference<T>::New(value, 1);
2412
+ }
2413
+
2414
+ inline ObjectReference Persistent(Object value) {
2415
+ return Reference<Object>::New(value, 1);
2416
+ }
2417
+
2418
+ inline FunctionReference Persistent(Function value) {
2419
+ return Reference<Function>::New(value, 1);
2420
+ }
2421
+
2422
+ ////////////////////////////////////////////////////////////////////////////////
2423
+ // ObjectReference class
2424
+ ////////////////////////////////////////////////////////////////////////////////
2425
+
2426
+ inline ObjectReference::ObjectReference(): Reference<Object>() {
2427
+ }
2428
+
2429
+ inline ObjectReference::ObjectReference(napi_env env, napi_ref ref): Reference<Object>(env, ref) {
2430
+ }
2431
+
2432
+ inline ObjectReference::ObjectReference(Reference<Object>&& other)
2433
+ : Reference<Object>(std::move(other)) {
2434
+ }
2435
+
2436
+ inline ObjectReference& ObjectReference::operator =(Reference<Object>&& other) {
2437
+ static_cast<Reference<Object>*>(this)->operator=(std::move(other));
2438
+ return *this;
2439
+ }
2440
+
2441
+ inline ObjectReference::ObjectReference(ObjectReference&& other)
2442
+ : Reference<Object>(std::move(other)) {
2443
+ }
2444
+
2445
+ inline ObjectReference& ObjectReference::operator =(ObjectReference&& other) {
2446
+ static_cast<Reference<Object>*>(this)->operator=(std::move(other));
2447
+ return *this;
2448
+ }
2449
+
2450
+ inline ObjectReference::ObjectReference(const ObjectReference& other)
2451
+ : Reference<Object>(other) {
2452
+ }
2453
+
2454
+ inline Napi::Value ObjectReference::Get(const char* utf8name) const {
2455
+ EscapableHandleScope scope(_env);
2456
+ return scope.Escape(Value().Get(utf8name));
2457
+ }
2458
+
2459
+ inline Napi::Value ObjectReference::Get(const std::string& utf8name) const {
2460
+ EscapableHandleScope scope(_env);
2461
+ return scope.Escape(Value().Get(utf8name));
2462
+ }
2463
+
2464
+ inline void ObjectReference::Set(const char* utf8name, napi_value value) {
2465
+ HandleScope scope(_env);
2466
+ Value().Set(utf8name, value);
2467
+ }
2468
+
2469
+ inline void ObjectReference::Set(const char* utf8name, Napi::Value value) {
2470
+ HandleScope scope(_env);
2471
+ Value().Set(utf8name, value);
2472
+ }
2473
+
2474
+ inline void ObjectReference::Set(const char* utf8name, const char* utf8value) {
2475
+ HandleScope scope(_env);
2476
+ Value().Set(utf8name, utf8value);
2477
+ }
2478
+
2479
+ inline void ObjectReference::Set(const char* utf8name, bool boolValue) {
2480
+ HandleScope scope(_env);
2481
+ Value().Set(utf8name, boolValue);
2482
+ }
2483
+
2484
+ inline void ObjectReference::Set(const char* utf8name, double numberValue) {
2485
+ HandleScope scope(_env);
2486
+ Value().Set(utf8name, numberValue);
2487
+ }
2488
+
2489
+ inline void ObjectReference::Set(const std::string& utf8name, napi_value value) {
2490
+ HandleScope scope(_env);
2491
+ Value().Set(utf8name, value);
2492
+ }
2493
+
2494
+ inline void ObjectReference::Set(const std::string& utf8name, Napi::Value value) {
2495
+ HandleScope scope(_env);
2496
+ Value().Set(utf8name, value);
2497
+ }
2498
+
2499
+ inline void ObjectReference::Set(const std::string& utf8name, std::string& utf8value) {
2500
+ HandleScope scope(_env);
2501
+ Value().Set(utf8name, utf8value);
2502
+ }
2503
+
2504
+ inline void ObjectReference::Set(const std::string& utf8name, bool boolValue) {
2505
+ HandleScope scope(_env);
2506
+ Value().Set(utf8name, boolValue);
2507
+ }
2508
+
2509
+ inline void ObjectReference::Set(const std::string& utf8name, double numberValue) {
2510
+ HandleScope scope(_env);
2511
+ Value().Set(utf8name, numberValue);
2512
+ }
2513
+
2514
+ inline Napi::Value ObjectReference::Get(uint32_t index) const {
2515
+ EscapableHandleScope scope(_env);
2516
+ return scope.Escape(Value().Get(index));
2517
+ }
2518
+
2519
+ inline void ObjectReference::Set(uint32_t index, napi_value value) {
2520
+ HandleScope scope(_env);
2521
+ Value().Set(index, value);
2522
+ }
2523
+
2524
+ inline void ObjectReference::Set(uint32_t index, Napi::Value value) {
2525
+ HandleScope scope(_env);
2526
+ Value().Set(index, value);
2527
+ }
2528
+
2529
+ inline void ObjectReference::Set(uint32_t index, const char* utf8value) {
2530
+ HandleScope scope(_env);
2531
+ Value().Set(index, utf8value);
2532
+ }
2533
+
2534
+ inline void ObjectReference::Set(uint32_t index, const std::string& utf8value) {
2535
+ HandleScope scope(_env);
2536
+ Value().Set(index, utf8value);
2537
+ }
2538
+
2539
+ inline void ObjectReference::Set(uint32_t index, bool boolValue) {
2540
+ HandleScope scope(_env);
2541
+ Value().Set(index, boolValue);
2542
+ }
2543
+
2544
+ inline void ObjectReference::Set(uint32_t index, double numberValue) {
2545
+ HandleScope scope(_env);
2546
+ Value().Set(index, numberValue);
2547
+ }
2548
+
2549
+ ////////////////////////////////////////////////////////////////////////////////
2550
+ // FunctionReference class
2551
+ ////////////////////////////////////////////////////////////////////////////////
2552
+
2553
+ inline FunctionReference::FunctionReference(): Reference<Function>() {
2554
+ }
2555
+
2556
+ inline FunctionReference::FunctionReference(napi_env env, napi_ref ref)
2557
+ : Reference<Function>(env, ref) {
2558
+ }
2559
+
2560
+ inline FunctionReference::FunctionReference(Reference<Function>&& other)
2561
+ : Reference<Function>(std::move(other)) {
2562
+ }
2563
+
2564
+ inline FunctionReference& FunctionReference::operator =(Reference<Function>&& other) {
2565
+ static_cast<Reference<Function>*>(this)->operator=(std::move(other));
2566
+ return *this;
2567
+ }
2568
+
2569
+ inline FunctionReference::FunctionReference(FunctionReference&& other)
2570
+ : Reference<Function>(std::move(other)) {
2571
+ }
2572
+
2573
+ inline FunctionReference& FunctionReference::operator =(FunctionReference&& other) {
2574
+ static_cast<Reference<Function>*>(this)->operator=(std::move(other));
2575
+ return *this;
2576
+ }
2577
+
2578
+ inline Napi::Value FunctionReference::operator ()(
2579
+ const std::initializer_list<napi_value>& args) const {
2580
+ EscapableHandleScope scope(_env);
2581
+ return scope.Escape(Value()(args));
2582
+ }
2583
+
2584
+ inline Napi::Value FunctionReference::Call(const std::initializer_list<napi_value>& args) const {
2585
+ EscapableHandleScope scope(_env);
2586
+ Napi::Value result = Value().Call(args);
2587
+ if (scope.Env().IsExceptionPending()) {
2588
+ return Value();
2589
+ }
2590
+ return scope.Escape(result);
2591
+ }
2592
+
2593
+ inline Napi::Value FunctionReference::Call(const std::vector<napi_value>& args) const {
2594
+ EscapableHandleScope scope(_env);
2595
+ Napi::Value result = Value().Call(args);
2596
+ if (scope.Env().IsExceptionPending()) {
2597
+ return Value();
2598
+ }
2599
+ return scope.Escape(result);
2600
+ }
2601
+
2602
+ inline Napi::Value FunctionReference::Call(
2603
+ napi_value recv, const std::initializer_list<napi_value>& args) const {
2604
+ EscapableHandleScope scope(_env);
2605
+ Napi::Value result = Value().Call(recv, args);
2606
+ if (scope.Env().IsExceptionPending()) {
2607
+ return Value();
2608
+ }
2609
+ return scope.Escape(result);
2610
+ }
2611
+
2612
+ inline Napi::Value FunctionReference::Call(
2613
+ napi_value recv, const std::vector<napi_value>& args) const {
2614
+ EscapableHandleScope scope(_env);
2615
+ Napi::Value result = Value().Call(recv, args);
2616
+ if (scope.Env().IsExceptionPending()) {
2617
+ return Value();
2618
+ }
2619
+ return scope.Escape(result);
2620
+ }
2621
+
2622
+ inline Napi::Value FunctionReference::Call(
2623
+ napi_value recv, size_t argc, const napi_value* args) const {
2624
+ EscapableHandleScope scope(_env);
2625
+ Napi::Value result = Value().Call(recv, argc, args);
2626
+ if (scope.Env().IsExceptionPending()) {
2627
+ return Value();
2628
+ }
2629
+ return scope.Escape(result);
2630
+ }
2631
+
2632
+ inline Napi::Value FunctionReference::MakeCallback(
2633
+ napi_value recv,
2634
+ const std::initializer_list<napi_value>& args,
2635
+ napi_async_context context) const {
2636
+ EscapableHandleScope scope(_env);
2637
+ Napi::Value result = Value().MakeCallback(recv, args, context);
2638
+ if (scope.Env().IsExceptionPending()) {
2639
+ return Value();
2640
+ }
2641
+ return scope.Escape(result);
2642
+ }
2643
+
2644
+ inline Napi::Value FunctionReference::MakeCallback(
2645
+ napi_value recv,
2646
+ const std::vector<napi_value>& args,
2647
+ napi_async_context context) const {
2648
+ EscapableHandleScope scope(_env);
2649
+ Napi::Value result = Value().MakeCallback(recv, args, context);
2650
+ if (scope.Env().IsExceptionPending()) {
2651
+ return Value();
2652
+ }
2653
+ return scope.Escape(result);
2654
+ }
2655
+
2656
+ inline Napi::Value FunctionReference::MakeCallback(
2657
+ napi_value recv,
2658
+ size_t argc,
2659
+ const napi_value* args,
2660
+ napi_async_context context) const {
2661
+ EscapableHandleScope scope(_env);
2662
+ Napi::Value result = Value().MakeCallback(recv, argc, args, context);
2663
+ if (scope.Env().IsExceptionPending()) {
2664
+ return Value();
2665
+ }
2666
+ return scope.Escape(result);
2667
+ }
2668
+
2669
+ inline Object FunctionReference::New(const std::initializer_list<napi_value>& args) const {
2670
+ EscapableHandleScope scope(_env);
2671
+ return scope.Escape(Value().New(args)).As<Object>();
2672
+ }
2673
+
2674
+ inline Object FunctionReference::New(const std::vector<napi_value>& args) const {
2675
+ EscapableHandleScope scope(_env);
2676
+ return scope.Escape(Value().New(args)).As<Object>();
2677
+ }
2678
+
2679
+ ////////////////////////////////////////////////////////////////////////////////
2680
+ // CallbackInfo class
2681
+ ////////////////////////////////////////////////////////////////////////////////
2682
+
2683
+ inline CallbackInfo::CallbackInfo(napi_env env, napi_callback_info info)
2684
+ : _env(env), _info(info), _this(nullptr), _dynamicArgs(nullptr), _data(nullptr) {
2685
+ _argc = _staticArgCount;
2686
+ _argv = _staticArgs;
2687
+ napi_status status = napi_get_cb_info(env, info, &_argc, _argv, &_this, &_data);
2688
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
2689
+
2690
+ if (_argc > _staticArgCount) {
2691
+ // Use either a fixed-size array (on the stack) or a dynamically-allocated
2692
+ // array (on the heap) depending on the number of args.
2693
+ _dynamicArgs = new napi_value[_argc];
2694
+ _argv = _dynamicArgs;
2695
+
2696
+ status = napi_get_cb_info(env, info, &_argc, _argv, nullptr, nullptr);
2697
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
2698
+ }
2699
+ }
2700
+
2701
+ inline CallbackInfo::~CallbackInfo() {
2702
+ if (_dynamicArgs != nullptr) {
2703
+ delete[] _dynamicArgs;
2704
+ }
2705
+ }
2706
+
2707
+ inline Value CallbackInfo::NewTarget() const {
2708
+ napi_value newTarget;
2709
+ napi_status status = napi_get_new_target(_env, _info, &newTarget);
2710
+ NAPI_THROW_IF_FAILED(_env, status, Value());
2711
+ return Value(_env, newTarget);
2712
+ }
2713
+
2714
+ inline bool CallbackInfo::IsConstructCall() const {
2715
+ return !NewTarget().IsEmpty();
2716
+ }
2717
+
2718
+ inline Napi::Env CallbackInfo::Env() const {
2719
+ return Napi::Env(_env);
2720
+ }
2721
+
2722
+ inline size_t CallbackInfo::Length() const {
2723
+ return _argc;
2724
+ }
2725
+
2726
+ inline const Value CallbackInfo::operator [](size_t index) const {
2727
+ return index < _argc ? Value(_env, _argv[index]) : Env().Undefined();
2728
+ }
2729
+
2730
+ inline Value CallbackInfo::This() const {
2731
+ if (_this == nullptr) {
2732
+ return Env().Undefined();
2733
+ }
2734
+ return Object(_env, _this);
2735
+ }
2736
+
2737
+ inline void* CallbackInfo::Data() const {
2738
+ return _data;
2739
+ }
2740
+
2741
+ inline void CallbackInfo::SetData(void* data) {
2742
+ _data = data;
2743
+ }
2744
+
2745
+ ////////////////////////////////////////////////////////////////////////////////
2746
+ // PropertyDescriptor class
2747
+ ////////////////////////////////////////////////////////////////////////////////
2748
+
2749
+ template <typename Getter>
2750
+ inline PropertyDescriptor
2751
+ PropertyDescriptor::Accessor(Napi::Env env,
2752
+ Napi::Object object,
2753
+ const char* utf8name,
2754
+ Getter getter,
2755
+ napi_property_attributes attributes,
2756
+ void* data) {
2757
+ typedef details::CallbackData<Getter, Napi::Value> CbData;
2758
+ auto callbackData = new CbData({ getter, data });
2759
+
2760
+ napi_status status = AttachData(env, object, callbackData);
2761
+ if (status != napi_ok) {
2762
+ delete callbackData;
2763
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2764
+ }
2765
+
2766
+ return PropertyDescriptor({
2767
+ utf8name,
2768
+ nullptr,
2769
+ nullptr,
2770
+ CbData::Wrapper,
2771
+ nullptr,
2772
+ nullptr,
2773
+ attributes,
2774
+ callbackData
2775
+ });
2776
+ }
2777
+
2778
+ template <typename Getter>
2779
+ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2780
+ Napi::Object object,
2781
+ const std::string& utf8name,
2782
+ Getter getter,
2783
+ napi_property_attributes attributes,
2784
+ void* data) {
2785
+ return Accessor(env, object, utf8name.c_str(), getter, attributes, data);
2786
+ }
2787
+
2788
+ template <typename Getter>
2789
+ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2790
+ Napi::Object object,
2791
+ Name name,
2792
+ Getter getter,
2793
+ napi_property_attributes attributes,
2794
+ void* data) {
2795
+ typedef details::CallbackData<Getter, Napi::Value> CbData;
2796
+ auto callbackData = new CbData({ getter, data });
2797
+
2798
+ napi_status status = AttachData(env, object, callbackData);
2799
+ if (status != napi_ok) {
2800
+ delete callbackData;
2801
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2802
+ }
2803
+
2804
+ return PropertyDescriptor({
2805
+ nullptr,
2806
+ name,
2807
+ nullptr,
2808
+ CbData::Wrapper,
2809
+ nullptr,
2810
+ nullptr,
2811
+ attributes,
2812
+ callbackData
2813
+ });
2814
+ }
2815
+
2816
+ template <typename Getter, typename Setter>
2817
+ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2818
+ Napi::Object object,
2819
+ const char* utf8name,
2820
+ Getter getter,
2821
+ Setter setter,
2822
+ napi_property_attributes attributes,
2823
+ void* data) {
2824
+ typedef details::AccessorCallbackData<Getter, Setter> CbData;
2825
+ auto callbackData = new CbData({ getter, setter, data });
2826
+
2827
+ napi_status status = AttachData(env, object, callbackData);
2828
+ if (status != napi_ok) {
2829
+ delete callbackData;
2830
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2831
+ }
2832
+
2833
+ return PropertyDescriptor({
2834
+ utf8name,
2835
+ nullptr,
2836
+ nullptr,
2837
+ CbData::GetterWrapper,
2838
+ CbData::SetterWrapper,
2839
+ nullptr,
2840
+ attributes,
2841
+ callbackData
2842
+ });
2843
+ }
2844
+
2845
+ template <typename Getter, typename Setter>
2846
+ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2847
+ Napi::Object object,
2848
+ const std::string& utf8name,
2849
+ Getter getter,
2850
+ Setter setter,
2851
+ napi_property_attributes attributes,
2852
+ void* data) {
2853
+ return Accessor(env, object, utf8name.c_str(), getter, setter, attributes, data);
2854
+ }
2855
+
2856
+ template <typename Getter, typename Setter>
2857
+ inline PropertyDescriptor PropertyDescriptor::Accessor(Napi::Env env,
2858
+ Napi::Object object,
2859
+ Name name,
2860
+ Getter getter,
2861
+ Setter setter,
2862
+ napi_property_attributes attributes,
2863
+ void* data) {
2864
+ typedef details::AccessorCallbackData<Getter, Setter> CbData;
2865
+ auto callbackData = new CbData({ getter, setter, data });
2866
+
2867
+ napi_status status = AttachData(env, object, callbackData);
2868
+ if (status != napi_ok) {
2869
+ delete callbackData;
2870
+ NAPI_THROW_IF_FAILED(env, status, napi_property_descriptor());
2871
+ }
2872
+
2873
+ return PropertyDescriptor({
2874
+ nullptr,
2875
+ name,
2876
+ nullptr,
2877
+ CbData::GetterWrapper,
2878
+ CbData::SetterWrapper,
2879
+ nullptr,
2880
+ attributes,
2881
+ callbackData
2882
+ });
2883
+ }
2884
+
2885
+ template <typename Callable>
2886
+ inline PropertyDescriptor PropertyDescriptor::Function(Napi::Env env,
2887
+ Napi::Object /*object*/,
2888
+ const char* utf8name,
2889
+ Callable cb,
2890
+ napi_property_attributes attributes,
2891
+ void* data) {
2892
+ return PropertyDescriptor({
2893
+ utf8name,
2894
+ nullptr,
2895
+ nullptr,
2896
+ nullptr,
2897
+ nullptr,
2898
+ Napi::Function::New(env, cb, utf8name, data),
2899
+ attributes,
2900
+ nullptr
2901
+ });
2902
+ }
2903
+
2904
+ template <typename Callable>
2905
+ inline PropertyDescriptor PropertyDescriptor::Function(Napi::Env env,
2906
+ Napi::Object object,
2907
+ const std::string& utf8name,
2908
+ Callable cb,
2909
+ napi_property_attributes attributes,
2910
+ void* data) {
2911
+ return Function(env, object, utf8name.c_str(), cb, attributes, data);
2912
+ }
2913
+
2914
+ template <typename Callable>
2915
+ inline PropertyDescriptor PropertyDescriptor::Function(Napi::Env env,
2916
+ Napi::Object /*object*/,
2917
+ Name name,
2918
+ Callable cb,
2919
+ napi_property_attributes attributes,
2920
+ void* data) {
2921
+ return PropertyDescriptor({
2922
+ nullptr,
2923
+ name,
2924
+ nullptr,
2925
+ nullptr,
2926
+ nullptr,
2927
+ Napi::Function::New(env, cb, nullptr, data),
2928
+ attributes,
2929
+ nullptr
2930
+ });
2931
+ }
2932
+
2933
+ inline PropertyDescriptor PropertyDescriptor::Value(const char* utf8name,
2934
+ napi_value value,
2935
+ napi_property_attributes attributes) {
2936
+ return PropertyDescriptor({
2937
+ utf8name, nullptr, nullptr, nullptr, nullptr, value, attributes, nullptr
2938
+ });
2939
+ }
2940
+
2941
+ inline PropertyDescriptor PropertyDescriptor::Value(const std::string& utf8name,
2942
+ napi_value value,
2943
+ napi_property_attributes attributes) {
2944
+ return Value(utf8name.c_str(), value, attributes);
2945
+ }
2946
+
2947
+ inline PropertyDescriptor PropertyDescriptor::Value(napi_value name,
2948
+ napi_value value,
2949
+ napi_property_attributes attributes) {
2950
+ return PropertyDescriptor({
2951
+ nullptr, name, nullptr, nullptr, nullptr, value, attributes, nullptr
2952
+ });
2953
+ }
2954
+
2955
+ inline PropertyDescriptor PropertyDescriptor::Value(Name name,
2956
+ Napi::Value value,
2957
+ napi_property_attributes attributes) {
2958
+ napi_value nameValue = name;
2959
+ napi_value valueValue = value;
2960
+ return PropertyDescriptor::Value(nameValue, valueValue, attributes);
2961
+ }
2962
+
2963
+ inline PropertyDescriptor::PropertyDescriptor(napi_property_descriptor desc)
2964
+ : _desc(desc) {
2965
+ }
2966
+
2967
+ inline PropertyDescriptor::operator napi_property_descriptor&() {
2968
+ return _desc;
2969
+ }
2970
+
2971
+ inline PropertyDescriptor::operator const napi_property_descriptor&() const {
2972
+ return _desc;
2973
+ }
2974
+
2975
+ ////////////////////////////////////////////////////////////////////////////////
2976
+ // ObjectWrap<T> class
2977
+ ////////////////////////////////////////////////////////////////////////////////
2978
+
2979
+ template <typename T>
2980
+ inline ObjectWrap<T>::ObjectWrap(const Napi::CallbackInfo& callbackInfo) {
2981
+ napi_env env = callbackInfo.Env();
2982
+ napi_value wrapper = callbackInfo.This();
2983
+ napi_status status;
2984
+ napi_ref ref;
2985
+ status = napi_wrap(env, wrapper, this, FinalizeCallback, nullptr, &ref);
2986
+ NAPI_THROW_IF_FAILED_VOID(env, status);
2987
+
2988
+ Reference<Object>* instanceRef = this;
2989
+ *instanceRef = Reference<Object>(env, ref);
2990
+ }
2991
+
2992
+ template <typename T>
2993
+ inline ObjectWrap<T>::~ObjectWrap() {
2994
+ // If the JS object still exists at this point, remove the finalizer added
2995
+ // through `napi_wrap()`.
2996
+ if (!IsEmpty()) {
2997
+ Object object = Value();
2998
+ // It is not valid to call `napi_remove_wrap()` with an empty `object`.
2999
+ // This happens e.g. during garbage collection.
3000
+ if (!object.IsEmpty() && _construction_failed) {
3001
+ napi_remove_wrap(Env(), object, nullptr);
3002
+
3003
+ if (Napi::details::needs_objectwrap_destructor_fix) {
3004
+ // If construction failed we delete the reference via
3005
+ // `napi_remove_wrap()`, not via `napi_delete_reference()` in the
3006
+ // `Reference<Object>` destructor. This will prevent the
3007
+ // `Reference<Object>` destructor from doing a double delete of this
3008
+ // reference.
3009
+ _ref = nullptr;
3010
+ _env = nullptr;
3011
+ }
3012
+ }
3013
+ }
3014
+ }
3015
+
3016
+ template<typename T>
3017
+ inline T* ObjectWrap<T>::Unwrap(Object wrapper) {
3018
+ T* unwrapped;
3019
+ napi_status status = napi_unwrap(wrapper.Env(), wrapper, reinterpret_cast<void**>(&unwrapped));
3020
+ NAPI_THROW_IF_FAILED(wrapper.Env(), status, nullptr);
3021
+ return unwrapped;
3022
+ }
3023
+
3024
+ template <typename T>
3025
+ inline Function
3026
+ ObjectWrap<T>::DefineClass(Napi::Env env,
3027
+ const char* utf8name,
3028
+ const size_t props_count,
3029
+ const napi_property_descriptor* descriptors,
3030
+ void* data) {
3031
+ napi_status status;
3032
+ std::vector<napi_property_descriptor> props(props_count);
3033
+
3034
+ // We copy the descriptors to a local array because before defining the class
3035
+ // we must replace static method property descriptors with value property
3036
+ // descriptors such that the value is a function-valued `napi_value` created
3037
+ // with `CreateFunction()`.
3038
+ //
3039
+ // This replacement could be made for instance methods as well, but V8 aborts
3040
+ // if we do that, because it expects methods defined on the prototype template
3041
+ // to have `FunctionTemplate`s.
3042
+ for (size_t index = 0; index < props_count; index++) {
3043
+ props[index] = descriptors[index];
3044
+ napi_property_descriptor* prop = &props[index];
3045
+ if (prop->method == T::StaticMethodCallbackWrapper) {
3046
+ status = CreateFunction(env,
3047
+ utf8name,
3048
+ prop->method,
3049
+ static_cast<StaticMethodCallbackData*>(prop->data),
3050
+ &(prop->value));
3051
+ NAPI_THROW_IF_FAILED(env, status, Function());
3052
+ prop->method = nullptr;
3053
+ prop->data = nullptr;
3054
+ } else if (prop->method == T::StaticVoidMethodCallbackWrapper) {
3055
+ status = CreateFunction(env,
3056
+ utf8name,
3057
+ prop->method,
3058
+ static_cast<StaticVoidMethodCallbackData*>(prop->data),
3059
+ &(prop->value));
3060
+ NAPI_THROW_IF_FAILED(env, status, Function());
3061
+ prop->method = nullptr;
3062
+ prop->data = nullptr;
3063
+ }
3064
+ }
3065
+
3066
+ napi_value value;
3067
+ status = napi_define_class(env,
3068
+ utf8name,
3069
+ NAPI_AUTO_LENGTH,
3070
+ T::ConstructorCallbackWrapper,
3071
+ data,
3072
+ props_count,
3073
+ props.data(),
3074
+ &value);
3075
+ NAPI_THROW_IF_FAILED(env, status, Function());
3076
+
3077
+ // After defining the class we iterate once more over the property descriptors
3078
+ // and attach the data associated with accessors and instance methods to the
3079
+ // newly created JavaScript class.
3080
+ for (size_t idx = 0; idx < props_count; idx++) {
3081
+ const napi_property_descriptor* prop = &props[idx];
3082
+
3083
+ if (prop->getter == T::StaticGetterCallbackWrapper ||
3084
+ prop->setter == T::StaticSetterCallbackWrapper) {
3085
+ status = Napi::details::AttachData(env,
3086
+ value,
3087
+ static_cast<StaticAccessorCallbackData*>(prop->data));
3088
+ NAPI_THROW_IF_FAILED(env, status, Function());
3089
+ } else if (prop->getter == T::InstanceGetterCallbackWrapper ||
3090
+ prop->setter == T::InstanceSetterCallbackWrapper) {
3091
+ status = Napi::details::AttachData(env,
3092
+ value,
3093
+ static_cast<InstanceAccessorCallbackData*>(prop->data));
3094
+ NAPI_THROW_IF_FAILED(env, status, Function());
3095
+ } else if (prop->method != nullptr && !(prop->attributes & napi_static)) {
3096
+ if (prop->method == T::InstanceVoidMethodCallbackWrapper) {
3097
+ status = Napi::details::AttachData(env,
3098
+ value,
3099
+ static_cast<InstanceVoidMethodCallbackData*>(prop->data));
3100
+ NAPI_THROW_IF_FAILED(env, status, Function());
3101
+ } else if (prop->method == T::InstanceMethodCallbackWrapper) {
3102
+ status = Napi::details::AttachData(env,
3103
+ value,
3104
+ static_cast<InstanceMethodCallbackData*>(prop->data));
3105
+ NAPI_THROW_IF_FAILED(env, status, Function());
3106
+ }
3107
+ }
3108
+ }
3109
+
3110
+ return Function(env, value);
3111
+ }
3112
+
3113
+ template <typename T>
3114
+ inline Function ObjectWrap<T>::DefineClass(
3115
+ Napi::Env env,
3116
+ const char* utf8name,
3117
+ const std::initializer_list<ClassPropertyDescriptor<T>>& properties,
3118
+ void* data) {
3119
+ return DefineClass(env,
3120
+ utf8name,
3121
+ properties.size(),
3122
+ reinterpret_cast<const napi_property_descriptor*>(properties.begin()),
3123
+ data);
3124
+ }
3125
+
3126
+ template <typename T>
3127
+ inline Function ObjectWrap<T>::DefineClass(
3128
+ Napi::Env env,
3129
+ const char* utf8name,
3130
+ const std::vector<ClassPropertyDescriptor<T>>& properties,
3131
+ void* data) {
3132
+ return DefineClass(env,
3133
+ utf8name,
3134
+ properties.size(),
3135
+ reinterpret_cast<const napi_property_descriptor*>(properties.data()),
3136
+ data);
3137
+ }
3138
+
3139
+ template <typename T>
3140
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3141
+ const char* utf8name,
3142
+ StaticVoidMethodCallback method,
3143
+ napi_property_attributes attributes,
3144
+ void* data) {
3145
+ StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data });
3146
+
3147
+ napi_property_descriptor desc = napi_property_descriptor();
3148
+ desc.utf8name = utf8name;
3149
+ desc.method = T::StaticVoidMethodCallbackWrapper;
3150
+ desc.data = callbackData;
3151
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3152
+ return desc;
3153
+ }
3154
+
3155
+ template <typename T>
3156
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3157
+ const char* utf8name,
3158
+ StaticMethodCallback method,
3159
+ napi_property_attributes attributes,
3160
+ void* data) {
3161
+ StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data });
3162
+
3163
+ napi_property_descriptor desc = napi_property_descriptor();
3164
+ desc.utf8name = utf8name;
3165
+ desc.method = T::StaticMethodCallbackWrapper;
3166
+ desc.data = callbackData;
3167
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3168
+ return desc;
3169
+ }
3170
+
3171
+ template <typename T>
3172
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3173
+ Symbol name,
3174
+ StaticVoidMethodCallback method,
3175
+ napi_property_attributes attributes,
3176
+ void* data) {
3177
+ StaticVoidMethodCallbackData* callbackData = new StaticVoidMethodCallbackData({ method, data });
3178
+
3179
+ napi_property_descriptor desc = napi_property_descriptor();
3180
+ desc.name = name;
3181
+ desc.method = T::StaticVoidMethodCallbackWrapper;
3182
+ desc.data = callbackData;
3183
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3184
+ return desc;
3185
+ }
3186
+
3187
+ template <typename T>
3188
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticMethod(
3189
+ Symbol name,
3190
+ StaticMethodCallback method,
3191
+ napi_property_attributes attributes,
3192
+ void* data) {
3193
+ StaticMethodCallbackData* callbackData = new StaticMethodCallbackData({ method, data });
3194
+
3195
+ napi_property_descriptor desc = napi_property_descriptor();
3196
+ desc.name = name;
3197
+ desc.method = T::StaticMethodCallbackWrapper;
3198
+ desc.data = callbackData;
3199
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3200
+ return desc;
3201
+ }
3202
+
3203
+ template <typename T>
3204
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
3205
+ const char* utf8name,
3206
+ StaticGetterCallback getter,
3207
+ StaticSetterCallback setter,
3208
+ napi_property_attributes attributes,
3209
+ void* data) {
3210
+ StaticAccessorCallbackData* callbackData =
3211
+ new StaticAccessorCallbackData({ getter, setter, data });
3212
+
3213
+ napi_property_descriptor desc = napi_property_descriptor();
3214
+ desc.utf8name = utf8name;
3215
+ desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
3216
+ desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
3217
+ desc.data = callbackData;
3218
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3219
+ return desc;
3220
+ }
3221
+
3222
+ template <typename T>
3223
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticAccessor(
3224
+ Symbol name,
3225
+ StaticGetterCallback getter,
3226
+ StaticSetterCallback setter,
3227
+ napi_property_attributes attributes,
3228
+ void* data) {
3229
+ StaticAccessorCallbackData* callbackData =
3230
+ new StaticAccessorCallbackData({ getter, setter, data });
3231
+
3232
+ napi_property_descriptor desc = napi_property_descriptor();
3233
+ desc.name = name;
3234
+ desc.getter = getter != nullptr ? T::StaticGetterCallbackWrapper : nullptr;
3235
+ desc.setter = setter != nullptr ? T::StaticSetterCallbackWrapper : nullptr;
3236
+ desc.data = callbackData;
3237
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3238
+ return desc;
3239
+ }
3240
+
3241
+ template <typename T>
3242
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
3243
+ const char* utf8name,
3244
+ InstanceVoidMethodCallback method,
3245
+ napi_property_attributes attributes,
3246
+ void* data) {
3247
+ InstanceVoidMethodCallbackData* callbackData =
3248
+ new InstanceVoidMethodCallbackData({ method, data});
3249
+
3250
+ napi_property_descriptor desc = napi_property_descriptor();
3251
+ desc.utf8name = utf8name;
3252
+ desc.method = T::InstanceVoidMethodCallbackWrapper;
3253
+ desc.data = callbackData;
3254
+ desc.attributes = attributes;
3255
+ return desc;
3256
+ }
3257
+
3258
+ template <typename T>
3259
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
3260
+ const char* utf8name,
3261
+ InstanceMethodCallback method,
3262
+ napi_property_attributes attributes,
3263
+ void* data) {
3264
+ InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data });
3265
+
3266
+ napi_property_descriptor desc = napi_property_descriptor();
3267
+ desc.utf8name = utf8name;
3268
+ desc.method = T::InstanceMethodCallbackWrapper;
3269
+ desc.data = callbackData;
3270
+ desc.attributes = attributes;
3271
+ return desc;
3272
+ }
3273
+
3274
+ template <typename T>
3275
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
3276
+ Symbol name,
3277
+ InstanceVoidMethodCallback method,
3278
+ napi_property_attributes attributes,
3279
+ void* data) {
3280
+ InstanceVoidMethodCallbackData* callbackData =
3281
+ new InstanceVoidMethodCallbackData({ method, data});
3282
+
3283
+ napi_property_descriptor desc = napi_property_descriptor();
3284
+ desc.name = name;
3285
+ desc.method = T::InstanceVoidMethodCallbackWrapper;
3286
+ desc.data = callbackData;
3287
+ desc.attributes = attributes;
3288
+ return desc;
3289
+ }
3290
+
3291
+ template <typename T>
3292
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceMethod(
3293
+ Symbol name,
3294
+ InstanceMethodCallback method,
3295
+ napi_property_attributes attributes,
3296
+ void* data) {
3297
+ InstanceMethodCallbackData* callbackData = new InstanceMethodCallbackData({ method, data });
3298
+
3299
+ napi_property_descriptor desc = napi_property_descriptor();
3300
+ desc.name = name;
3301
+ desc.method = T::InstanceMethodCallbackWrapper;
3302
+ desc.data = callbackData;
3303
+ desc.attributes = attributes;
3304
+ return desc;
3305
+ }
3306
+
3307
+ template <typename T>
3308
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
3309
+ const char* utf8name,
3310
+ InstanceGetterCallback getter,
3311
+ InstanceSetterCallback setter,
3312
+ napi_property_attributes attributes,
3313
+ void* data) {
3314
+ InstanceAccessorCallbackData* callbackData =
3315
+ new InstanceAccessorCallbackData({ getter, setter, data });
3316
+
3317
+ napi_property_descriptor desc = napi_property_descriptor();
3318
+ desc.utf8name = utf8name;
3319
+ desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr;
3320
+ desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr;
3321
+ desc.data = callbackData;
3322
+ desc.attributes = attributes;
3323
+ return desc;
3324
+ }
3325
+
3326
+ template <typename T>
3327
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceAccessor(
3328
+ Symbol name,
3329
+ InstanceGetterCallback getter,
3330
+ InstanceSetterCallback setter,
3331
+ napi_property_attributes attributes,
3332
+ void* data) {
3333
+ InstanceAccessorCallbackData* callbackData =
3334
+ new InstanceAccessorCallbackData({ getter, setter, data });
3335
+
3336
+ napi_property_descriptor desc = napi_property_descriptor();
3337
+ desc.name = name;
3338
+ desc.getter = getter != nullptr ? T::InstanceGetterCallbackWrapper : nullptr;
3339
+ desc.setter = setter != nullptr ? T::InstanceSetterCallbackWrapper : nullptr;
3340
+ desc.data = callbackData;
3341
+ desc.attributes = attributes;
3342
+ return desc;
3343
+ }
3344
+
3345
+ template <typename T>
3346
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(const char* utf8name,
3347
+ Napi::Value value, napi_property_attributes attributes) {
3348
+ napi_property_descriptor desc = napi_property_descriptor();
3349
+ desc.utf8name = utf8name;
3350
+ desc.value = value;
3351
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3352
+ return desc;
3353
+ }
3354
+
3355
+ template <typename T>
3356
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::StaticValue(Symbol name,
3357
+ Napi::Value value, napi_property_attributes attributes) {
3358
+ napi_property_descriptor desc = napi_property_descriptor();
3359
+ desc.name = name;
3360
+ desc.value = value;
3361
+ desc.attributes = static_cast<napi_property_attributes>(attributes | napi_static);
3362
+ return desc;
3363
+ }
3364
+
3365
+ template <typename T>
3366
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
3367
+ const char* utf8name,
3368
+ Napi::Value value,
3369
+ napi_property_attributes attributes) {
3370
+ napi_property_descriptor desc = napi_property_descriptor();
3371
+ desc.utf8name = utf8name;
3372
+ desc.value = value;
3373
+ desc.attributes = attributes;
3374
+ return desc;
3375
+ }
3376
+
3377
+ template <typename T>
3378
+ inline ClassPropertyDescriptor<T> ObjectWrap<T>::InstanceValue(
3379
+ Symbol name,
3380
+ Napi::Value value,
3381
+ napi_property_attributes attributes) {
3382
+ napi_property_descriptor desc = napi_property_descriptor();
3383
+ desc.name = name;
3384
+ desc.value = value;
3385
+ desc.attributes = attributes;
3386
+ return desc;
3387
+ }
3388
+
3389
+ template <typename T>
3390
+ inline void ObjectWrap<T>::Finalize(Napi::Env /*env*/) {}
3391
+
3392
+ template <typename T>
3393
+ inline napi_value ObjectWrap<T>::ConstructorCallbackWrapper(
3394
+ napi_env env,
3395
+ napi_callback_info info) {
3396
+ napi_value new_target;
3397
+ napi_status status = napi_get_new_target(env, info, &new_target);
3398
+ if (status != napi_ok) return nullptr;
3399
+
3400
+ bool isConstructCall = (new_target != nullptr);
3401
+ if (!isConstructCall) {
3402
+ napi_throw_type_error(env, nullptr, "Class constructors cannot be invoked without 'new'");
3403
+ return nullptr;
3404
+ }
3405
+
3406
+ napi_value wrapper = details::WrapCallback([&] {
3407
+ CallbackInfo callbackInfo(env, info);
3408
+ T* instance = new T(callbackInfo);
3409
+ #ifdef NAPI_CPP_EXCEPTIONS
3410
+ instance->_construction_failed = false;
3411
+ #else
3412
+ if (callbackInfo.Env().IsExceptionPending()) {
3413
+ // We need to clear the exception so that removing the wrap might work.
3414
+ Error e = callbackInfo.Env().GetAndClearPendingException();
3415
+ delete instance;
3416
+ e.ThrowAsJavaScriptException();
3417
+ } else {
3418
+ instance->_construction_failed = false;
3419
+ }
3420
+ # endif // NAPI_CPP_EXCEPTIONS
3421
+ return callbackInfo.This();
3422
+ });
3423
+
3424
+ return wrapper;
3425
+ }
3426
+
3427
+ template <typename T>
3428
+ inline napi_value ObjectWrap<T>::StaticVoidMethodCallbackWrapper(
3429
+ napi_env env,
3430
+ napi_callback_info info) {
3431
+ return details::WrapCallback([&] {
3432
+ CallbackInfo callbackInfo(env, info);
3433
+ StaticVoidMethodCallbackData* callbackData =
3434
+ reinterpret_cast<StaticVoidMethodCallbackData*>(callbackInfo.Data());
3435
+ callbackInfo.SetData(callbackData->data);
3436
+ callbackData->callback(callbackInfo);
3437
+ return nullptr;
3438
+ });
3439
+ }
3440
+
3441
+ template <typename T>
3442
+ inline napi_value ObjectWrap<T>::StaticMethodCallbackWrapper(
3443
+ napi_env env,
3444
+ napi_callback_info info) {
3445
+ return details::WrapCallback([&] {
3446
+ CallbackInfo callbackInfo(env, info);
3447
+ StaticMethodCallbackData* callbackData =
3448
+ reinterpret_cast<StaticMethodCallbackData*>(callbackInfo.Data());
3449
+ callbackInfo.SetData(callbackData->data);
3450
+ return callbackData->callback(callbackInfo);
3451
+ });
3452
+ }
3453
+
3454
+ template <typename T>
3455
+ inline napi_value ObjectWrap<T>::StaticGetterCallbackWrapper(
3456
+ napi_env env,
3457
+ napi_callback_info info) {
3458
+ return details::WrapCallback([&] {
3459
+ CallbackInfo callbackInfo(env, info);
3460
+ StaticAccessorCallbackData* callbackData =
3461
+ reinterpret_cast<StaticAccessorCallbackData*>(callbackInfo.Data());
3462
+ callbackInfo.SetData(callbackData->data);
3463
+ return callbackData->getterCallback(callbackInfo);
3464
+ });
3465
+ }
3466
+
3467
+ template <typename T>
3468
+ inline napi_value ObjectWrap<T>::StaticSetterCallbackWrapper(
3469
+ napi_env env,
3470
+ napi_callback_info info) {
3471
+ return details::WrapCallback([&] {
3472
+ CallbackInfo callbackInfo(env, info);
3473
+ StaticAccessorCallbackData* callbackData =
3474
+ reinterpret_cast<StaticAccessorCallbackData*>(callbackInfo.Data());
3475
+ callbackInfo.SetData(callbackData->data);
3476
+ callbackData->setterCallback(callbackInfo, callbackInfo[0]);
3477
+ return nullptr;
3478
+ });
3479
+ }
3480
+
3481
+ template <typename T>
3482
+ inline napi_value ObjectWrap<T>::InstanceVoidMethodCallbackWrapper(
3483
+ napi_env env,
3484
+ napi_callback_info info) {
3485
+ return details::WrapCallback([&] {
3486
+ CallbackInfo callbackInfo(env, info);
3487
+ InstanceVoidMethodCallbackData* callbackData =
3488
+ reinterpret_cast<InstanceVoidMethodCallbackData*>(callbackInfo.Data());
3489
+ callbackInfo.SetData(callbackData->data);
3490
+ T* instance = Unwrap(callbackInfo.This().As<Object>());
3491
+ auto cb = callbackData->callback;
3492
+ (instance->*cb)(callbackInfo);
3493
+ return nullptr;
3494
+ });
3495
+ }
3496
+
3497
+ template <typename T>
3498
+ inline napi_value ObjectWrap<T>::InstanceMethodCallbackWrapper(
3499
+ napi_env env,
3500
+ napi_callback_info info) {
3501
+ return details::WrapCallback([&] {
3502
+ CallbackInfo callbackInfo(env, info);
3503
+ InstanceMethodCallbackData* callbackData =
3504
+ reinterpret_cast<InstanceMethodCallbackData*>(callbackInfo.Data());
3505
+ callbackInfo.SetData(callbackData->data);
3506
+ T* instance = Unwrap(callbackInfo.This().As<Object>());
3507
+ auto cb = callbackData->callback;
3508
+ return (instance->*cb)(callbackInfo);
3509
+ });
3510
+ }
3511
+
3512
+ template <typename T>
3513
+ inline napi_value ObjectWrap<T>::InstanceGetterCallbackWrapper(
3514
+ napi_env env,
3515
+ napi_callback_info info) {
3516
+ return details::WrapCallback([&] {
3517
+ CallbackInfo callbackInfo(env, info);
3518
+ InstanceAccessorCallbackData* callbackData =
3519
+ reinterpret_cast<InstanceAccessorCallbackData*>(callbackInfo.Data());
3520
+ callbackInfo.SetData(callbackData->data);
3521
+ T* instance = Unwrap(callbackInfo.This().As<Object>());
3522
+ auto cb = callbackData->getterCallback;
3523
+ return (instance->*cb)(callbackInfo);
3524
+ });
3525
+ }
3526
+
3527
+ template <typename T>
3528
+ inline napi_value ObjectWrap<T>::InstanceSetterCallbackWrapper(
3529
+ napi_env env,
3530
+ napi_callback_info info) {
3531
+ return details::WrapCallback([&] {
3532
+ CallbackInfo callbackInfo(env, info);
3533
+ InstanceAccessorCallbackData* callbackData =
3534
+ reinterpret_cast<InstanceAccessorCallbackData*>(callbackInfo.Data());
3535
+ callbackInfo.SetData(callbackData->data);
3536
+ T* instance = Unwrap(callbackInfo.This().As<Object>());
3537
+ auto cb = callbackData->setterCallback;
3538
+ (instance->*cb)(callbackInfo, callbackInfo[0]);
3539
+ return nullptr;
3540
+ });
3541
+ }
3542
+
3543
+ template <typename T>
3544
+ inline void ObjectWrap<T>::FinalizeCallback(napi_env env, void* data, void* /*hint*/) {
3545
+ ObjectWrap<T>* instance = static_cast<ObjectWrap<T>*>(data);
3546
+ instance->Finalize(Napi::Env(env));
3547
+ delete instance;
3548
+ }
3549
+
3550
+ ////////////////////////////////////////////////////////////////////////////////
3551
+ // HandleScope class
3552
+ ////////////////////////////////////////////////////////////////////////////////
3553
+
3554
+ inline HandleScope::HandleScope(napi_env env, napi_handle_scope scope)
3555
+ : _env(env), _scope(scope) {
3556
+ }
3557
+
3558
+ inline HandleScope::HandleScope(Napi::Env env) : _env(env) {
3559
+ napi_status status = napi_open_handle_scope(_env, &_scope);
3560
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3561
+ }
3562
+
3563
+ inline HandleScope::~HandleScope() {
3564
+ napi_status status = napi_close_handle_scope(_env, _scope);
3565
+ NAPI_FATAL_IF_FAILED(status,
3566
+ "HandleScope::~HandleScope",
3567
+ "napi_close_handle_scope");
3568
+ }
3569
+
3570
+ inline HandleScope::operator napi_handle_scope() const {
3571
+ return _scope;
3572
+ }
3573
+
3574
+ inline Napi::Env HandleScope::Env() const {
3575
+ return Napi::Env(_env);
3576
+ }
3577
+
3578
+ ////////////////////////////////////////////////////////////////////////////////
3579
+ // EscapableHandleScope class
3580
+ ////////////////////////////////////////////////////////////////////////////////
3581
+
3582
+ inline EscapableHandleScope::EscapableHandleScope(
3583
+ napi_env env, napi_escapable_handle_scope scope) : _env(env), _scope(scope) {
3584
+ }
3585
+
3586
+ inline EscapableHandleScope::EscapableHandleScope(Napi::Env env) : _env(env) {
3587
+ napi_status status = napi_open_escapable_handle_scope(_env, &_scope);
3588
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3589
+ }
3590
+
3591
+ inline EscapableHandleScope::~EscapableHandleScope() {
3592
+ napi_status status = napi_close_escapable_handle_scope(_env, _scope);
3593
+ NAPI_FATAL_IF_FAILED(status,
3594
+ "EscapableHandleScope::~EscapableHandleScope",
3595
+ "napi_close_escapable_handle_scope");
3596
+ }
3597
+
3598
+ inline EscapableHandleScope::operator napi_escapable_handle_scope() const {
3599
+ return _scope;
3600
+ }
3601
+
3602
+ inline Napi::Env EscapableHandleScope::Env() const {
3603
+ return Napi::Env(_env);
3604
+ }
3605
+
3606
+ inline Value EscapableHandleScope::Escape(napi_value escapee) {
3607
+ napi_value result;
3608
+ napi_status status = napi_escape_handle(_env, _scope, escapee, &result);
3609
+ NAPI_THROW_IF_FAILED(_env, status, Value());
3610
+ return Value(_env, result);
3611
+ }
3612
+
3613
+
3614
+ #if (NAPI_VERSION > 2)
3615
+ ////////////////////////////////////////////////////////////////////////////////
3616
+ // CallbackScope class
3617
+ ////////////////////////////////////////////////////////////////////////////////
3618
+
3619
+ inline CallbackScope::CallbackScope(
3620
+ napi_env env, napi_callback_scope scope) : _env(env), _scope(scope) {
3621
+ }
3622
+
3623
+ inline CallbackScope::CallbackScope(napi_env env, napi_async_context context)
3624
+ : _env(env) {
3625
+ napi_status status = napi_open_callback_scope(
3626
+ _env, Object::New(env), context, &_scope);
3627
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3628
+ }
3629
+
3630
+ inline CallbackScope::~CallbackScope() {
3631
+ napi_status status = napi_close_callback_scope(_env, _scope);
3632
+ NAPI_FATAL_IF_FAILED(status,
3633
+ "CallbackScope::~CallbackScope",
3634
+ "napi_close_callback_scope");
3635
+ }
3636
+
3637
+ inline CallbackScope::operator napi_callback_scope() const {
3638
+ return _scope;
3639
+ }
3640
+
3641
+ inline Napi::Env CallbackScope::Env() const {
3642
+ return Napi::Env(_env);
3643
+ }
3644
+ #endif
3645
+
3646
+ ////////////////////////////////////////////////////////////////////////////////
3647
+ // AsyncContext class
3648
+ ////////////////////////////////////////////////////////////////////////////////
3649
+
3650
+ inline AsyncContext::AsyncContext(napi_env env, const char* resource_name)
3651
+ : AsyncContext(env, resource_name, Object::New(env)) {
3652
+ }
3653
+
3654
+ inline AsyncContext::AsyncContext(napi_env env,
3655
+ const char* resource_name,
3656
+ const Object& resource)
3657
+ : _env(env),
3658
+ _context(nullptr) {
3659
+ napi_value resource_id;
3660
+ napi_status status = napi_create_string_utf8(
3661
+ _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
3662
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3663
+
3664
+ status = napi_async_init(_env, resource, resource_id, &_context);
3665
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3666
+ }
3667
+
3668
+ inline AsyncContext::~AsyncContext() {
3669
+ if (_context != nullptr) {
3670
+ napi_async_destroy(_env, _context);
3671
+ _context = nullptr;
3672
+ }
3673
+ }
3674
+
3675
+ inline AsyncContext::AsyncContext(AsyncContext&& other) {
3676
+ _env = other._env;
3677
+ other._env = nullptr;
3678
+ _context = other._context;
3679
+ other._context = nullptr;
3680
+ }
3681
+
3682
+ inline AsyncContext& AsyncContext::operator =(AsyncContext&& other) {
3683
+ _env = other._env;
3684
+ other._env = nullptr;
3685
+ _context = other._context;
3686
+ other._context = nullptr;
3687
+ return *this;
3688
+ }
3689
+
3690
+ inline AsyncContext::operator napi_async_context() const {
3691
+ return _context;
3692
+ }
3693
+
3694
+ inline Napi::Env AsyncContext::Env() const {
3695
+ return Napi::Env(_env);
3696
+ }
3697
+
3698
+ ////////////////////////////////////////////////////////////////////////////////
3699
+ // AsyncWorker class
3700
+ ////////////////////////////////////////////////////////////////////////////////
3701
+
3702
+ inline AsyncWorker::AsyncWorker(const Function& callback)
3703
+ : AsyncWorker(callback, "generic") {
3704
+ }
3705
+
3706
+ inline AsyncWorker::AsyncWorker(const Function& callback,
3707
+ const char* resource_name)
3708
+ : AsyncWorker(callback, resource_name, Object::New(callback.Env())) {
3709
+ }
3710
+
3711
+ inline AsyncWorker::AsyncWorker(const Function& callback,
3712
+ const char* resource_name,
3713
+ const Object& resource)
3714
+ : AsyncWorker(Object::New(callback.Env()),
3715
+ callback,
3716
+ resource_name,
3717
+ resource) {
3718
+ }
3719
+
3720
+ inline AsyncWorker::AsyncWorker(const Object& receiver,
3721
+ const Function& callback)
3722
+ : AsyncWorker(receiver, callback, "generic") {
3723
+ }
3724
+
3725
+ inline AsyncWorker::AsyncWorker(const Object& receiver,
3726
+ const Function& callback,
3727
+ const char* resource_name)
3728
+ : AsyncWorker(receiver,
3729
+ callback,
3730
+ resource_name,
3731
+ Object::New(callback.Env())) {
3732
+ }
3733
+
3734
+ inline AsyncWorker::AsyncWorker(const Object& receiver,
3735
+ const Function& callback,
3736
+ const char* resource_name,
3737
+ const Object& resource)
3738
+ : _env(callback.Env()),
3739
+ _receiver(Napi::Persistent(receiver)),
3740
+ _callback(Napi::Persistent(callback)),
3741
+ _suppress_destruct(false) {
3742
+ napi_value resource_id;
3743
+ napi_status status = napi_create_string_latin1(
3744
+ _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
3745
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3746
+
3747
+ status = napi_create_async_work(_env, resource, resource_id, OnExecute,
3748
+ OnWorkComplete, this, &_work);
3749
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3750
+ }
3751
+
3752
+ inline AsyncWorker::AsyncWorker(Napi::Env env)
3753
+ : AsyncWorker(env, "generic") {
3754
+ }
3755
+
3756
+ inline AsyncWorker::AsyncWorker(Napi::Env env,
3757
+ const char* resource_name)
3758
+ : AsyncWorker(env, resource_name, Object::New(env)) {
3759
+ }
3760
+
3761
+ inline AsyncWorker::AsyncWorker(Napi::Env env,
3762
+ const char* resource_name,
3763
+ const Object& resource)
3764
+ : _env(env),
3765
+ _receiver(),
3766
+ _callback(),
3767
+ _suppress_destruct(false) {
3768
+ napi_value resource_id;
3769
+ napi_status status = napi_create_string_latin1(
3770
+ _env, resource_name, NAPI_AUTO_LENGTH, &resource_id);
3771
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3772
+
3773
+ status = napi_create_async_work(_env, resource, resource_id, OnExecute,
3774
+ OnWorkComplete, this, &_work);
3775
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3776
+ }
3777
+
3778
+ inline AsyncWorker::~AsyncWorker() {
3779
+ if (_work != nullptr) {
3780
+ napi_delete_async_work(_env, _work);
3781
+ _work = nullptr;
3782
+ }
3783
+ }
3784
+
3785
+ inline void AsyncWorker::Destroy() {
3786
+ delete this;
3787
+ }
3788
+
3789
+ inline AsyncWorker::AsyncWorker(AsyncWorker&& other) {
3790
+ _env = other._env;
3791
+ other._env = nullptr;
3792
+ _work = other._work;
3793
+ other._work = nullptr;
3794
+ _receiver = std::move(other._receiver);
3795
+ _callback = std::move(other._callback);
3796
+ _error = std::move(other._error);
3797
+ _suppress_destruct = other._suppress_destruct;
3798
+ }
3799
+
3800
+ inline AsyncWorker& AsyncWorker::operator =(AsyncWorker&& other) {
3801
+ _env = other._env;
3802
+ other._env = nullptr;
3803
+ _work = other._work;
3804
+ other._work = nullptr;
3805
+ _receiver = std::move(other._receiver);
3806
+ _callback = std::move(other._callback);
3807
+ _error = std::move(other._error);
3808
+ _suppress_destruct = other._suppress_destruct;
3809
+ return *this;
3810
+ }
3811
+
3812
+ inline AsyncWorker::operator napi_async_work() const {
3813
+ return _work;
3814
+ }
3815
+
3816
+ inline Napi::Env AsyncWorker::Env() const {
3817
+ return Napi::Env(_env);
3818
+ }
3819
+
3820
+ inline void AsyncWorker::Queue() {
3821
+ napi_status status = napi_queue_async_work(_env, _work);
3822
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3823
+ }
3824
+
3825
+ inline void AsyncWorker::Cancel() {
3826
+ napi_status status = napi_cancel_async_work(_env, _work);
3827
+ NAPI_THROW_IF_FAILED_VOID(_env, status);
3828
+ }
3829
+
3830
+ inline ObjectReference& AsyncWorker::Receiver() {
3831
+ return _receiver;
3832
+ }
3833
+
3834
+ inline FunctionReference& AsyncWorker::Callback() {
3835
+ return _callback;
3836
+ }
3837
+
3838
+ inline void AsyncWorker::SuppressDestruct() {
3839
+ _suppress_destruct = true;
3840
+ }
3841
+
3842
+ inline void AsyncWorker::OnOK() {
3843
+ if (!_callback.IsEmpty()) {
3844
+ _callback.Call(_receiver.Value(), GetResult(_callback.Env()));
3845
+ }
3846
+ }
3847
+
3848
+ inline void AsyncWorker::OnError(const Error& e) {
3849
+ if (!_callback.IsEmpty()) {
3850
+ _callback.Call(_receiver.Value(), std::initializer_list<napi_value>{ e.Value() });
3851
+ }
3852
+ }
3853
+
3854
+ inline void AsyncWorker::SetError(const std::string& error) {
3855
+ _error = error;
3856
+ }
3857
+
3858
+ inline std::vector<napi_value> AsyncWorker::GetResult(Napi::Env /*env*/) {
3859
+ return {};
3860
+ }
3861
+ // The OnExecute method receives an napi_env argument. However, do NOT
3862
+ // use it within this method, as it does not run on the main thread and must
3863
+ // not run any method that would cause JavaScript to run. In practice, this
3864
+ // means that almost any use of napi_env will be incorrect.
3865
+ inline void AsyncWorker::OnExecute(napi_env /*DO_NOT_USE*/, void* this_pointer) {
3866
+ AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
3867
+ #ifdef NAPI_CPP_EXCEPTIONS
3868
+ try {
3869
+ self->Execute();
3870
+ } catch (const std::exception& e) {
3871
+ self->SetError(e.what());
3872
+ }
3873
+ #else // NAPI_CPP_EXCEPTIONS
3874
+ self->Execute();
3875
+ #endif // NAPI_CPP_EXCEPTIONS
3876
+ }
3877
+
3878
+ inline void AsyncWorker::OnWorkComplete(
3879
+ napi_env /*env*/, napi_status status, void* this_pointer) {
3880
+ AsyncWorker* self = static_cast<AsyncWorker*>(this_pointer);
3881
+ if (status != napi_cancelled) {
3882
+ HandleScope scope(self->_env);
3883
+ details::WrapCallback([&] {
3884
+ if (self->_error.size() == 0) {
3885
+ self->OnOK();
3886
+ }
3887
+ else {
3888
+ self->OnError(Error::New(self->_env, self->_error));
3889
+ }
3890
+ return nullptr;
3891
+ });
3892
+ }
3893
+ if (!self->_suppress_destruct) {
3894
+ self->Destroy();
3895
+ }
3896
+ }
3897
+
3898
+ #if (NAPI_VERSION > 3)
3899
+ ////////////////////////////////////////////////////////////////////////////////
3900
+ // ThreadSafeFunction class
3901
+ ////////////////////////////////////////////////////////////////////////////////
3902
+
3903
+ // static
3904
+ template <typename ResourceString>
3905
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3906
+ const Function& callback,
3907
+ ResourceString resourceName,
3908
+ size_t maxQueueSize,
3909
+ size_t initialThreadCount) {
3910
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3911
+ initialThreadCount);
3912
+ }
3913
+
3914
+ // static
3915
+ template <typename ResourceString, typename ContextType>
3916
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3917
+ const Function& callback,
3918
+ ResourceString resourceName,
3919
+ size_t maxQueueSize,
3920
+ size_t initialThreadCount,
3921
+ ContextType* context) {
3922
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3923
+ initialThreadCount, context);
3924
+ }
3925
+
3926
+ // static
3927
+ template <typename ResourceString, typename Finalizer>
3928
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3929
+ const Function& callback,
3930
+ ResourceString resourceName,
3931
+ size_t maxQueueSize,
3932
+ size_t initialThreadCount,
3933
+ Finalizer finalizeCallback) {
3934
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3935
+ initialThreadCount, finalizeCallback);
3936
+ }
3937
+
3938
+ // static
3939
+ template <typename ResourceString, typename Finalizer,
3940
+ typename FinalizerDataType>
3941
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3942
+ const Function& callback,
3943
+ ResourceString resourceName,
3944
+ size_t maxQueueSize,
3945
+ size_t initialThreadCount,
3946
+ Finalizer finalizeCallback,
3947
+ FinalizerDataType* data) {
3948
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3949
+ initialThreadCount, finalizeCallback, data);
3950
+ }
3951
+
3952
+ // static
3953
+ template <typename ResourceString, typename ContextType, typename Finalizer>
3954
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3955
+ const Function& callback,
3956
+ ResourceString resourceName,
3957
+ size_t maxQueueSize,
3958
+ size_t initialThreadCount,
3959
+ ContextType* context,
3960
+ Finalizer finalizeCallback) {
3961
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3962
+ initialThreadCount, context, finalizeCallback);
3963
+ }
3964
+
3965
+ // static
3966
+ template <typename ResourceString, typename ContextType,
3967
+ typename Finalizer, typename FinalizerDataType>
3968
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3969
+ const Function& callback,
3970
+ ResourceString resourceName,
3971
+ size_t maxQueueSize,
3972
+ size_t initialThreadCount,
3973
+ ContextType* context,
3974
+ Finalizer finalizeCallback,
3975
+ FinalizerDataType* data) {
3976
+ return New(env, callback, Object(), resourceName, maxQueueSize,
3977
+ initialThreadCount, context, finalizeCallback, data);
3978
+ }
3979
+
3980
+ // static
3981
+ template <typename ResourceString>
3982
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3983
+ const Function& callback,
3984
+ const Object& resource,
3985
+ ResourceString resourceName,
3986
+ size_t maxQueueSize,
3987
+ size_t initialThreadCount) {
3988
+ return New(env, callback, resource, resourceName, maxQueueSize,
3989
+ initialThreadCount, static_cast<void*>(nullptr) /* context */);
3990
+ }
3991
+
3992
+ // static
3993
+ template <typename ResourceString, typename ContextType>
3994
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
3995
+ const Function& callback,
3996
+ const Object& resource,
3997
+ ResourceString resourceName,
3998
+ size_t maxQueueSize,
3999
+ size_t initialThreadCount,
4000
+ ContextType* context) {
4001
+ return New(env, callback, resource, resourceName, maxQueueSize,
4002
+ initialThreadCount, context,
4003
+ [](Env, ContextType*) {} /* empty finalizer */);
4004
+ }
4005
+
4006
+ // static
4007
+ template <typename ResourceString, typename Finalizer>
4008
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
4009
+ const Function& callback,
4010
+ const Object& resource,
4011
+ ResourceString resourceName,
4012
+ size_t maxQueueSize,
4013
+ size_t initialThreadCount,
4014
+ Finalizer finalizeCallback) {
4015
+ return New(env, callback, resource, resourceName, maxQueueSize,
4016
+ initialThreadCount, static_cast<void*>(nullptr) /* context */,
4017
+ finalizeCallback, static_cast<void*>(nullptr) /* data */,
4018
+ details::ThreadSafeFinalize<void, Finalizer>::Wrapper);
4019
+ }
4020
+
4021
+ // static
4022
+ template <typename ResourceString, typename Finalizer,
4023
+ typename FinalizerDataType>
4024
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
4025
+ const Function& callback,
4026
+ const Object& resource,
4027
+ ResourceString resourceName,
4028
+ size_t maxQueueSize,
4029
+ size_t initialThreadCount,
4030
+ Finalizer finalizeCallback,
4031
+ FinalizerDataType* data) {
4032
+ return New(env, callback, resource, resourceName, maxQueueSize,
4033
+ initialThreadCount, static_cast<void*>(nullptr) /* context */,
4034
+ finalizeCallback, data,
4035
+ details::ThreadSafeFinalize<
4036
+ void, Finalizer, FinalizerDataType>::FinalizeWrapperWithData);
4037
+ }
4038
+
4039
+ // static
4040
+ template <typename ResourceString, typename ContextType, typename Finalizer>
4041
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
4042
+ const Function& callback,
4043
+ const Object& resource,
4044
+ ResourceString resourceName,
4045
+ size_t maxQueueSize,
4046
+ size_t initialThreadCount,
4047
+ ContextType* context,
4048
+ Finalizer finalizeCallback) {
4049
+ return New(env, callback, resource, resourceName, maxQueueSize,
4050
+ initialThreadCount, context, finalizeCallback,
4051
+ static_cast<void*>(nullptr) /* data */,
4052
+ details::ThreadSafeFinalize<
4053
+ ContextType, Finalizer>::FinalizeWrapperWithContext);
4054
+ }
4055
+
4056
+ // static
4057
+ template <typename ResourceString, typename ContextType,
4058
+ typename Finalizer, typename FinalizerDataType>
4059
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
4060
+ const Function& callback,
4061
+ const Object& resource,
4062
+ ResourceString resourceName,
4063
+ size_t maxQueueSize,
4064
+ size_t initialThreadCount,
4065
+ ContextType* context,
4066
+ Finalizer finalizeCallback,
4067
+ FinalizerDataType* data) {
4068
+ return New(env, callback, resource, resourceName, maxQueueSize,
4069
+ initialThreadCount, context, finalizeCallback, data,
4070
+ details::ThreadSafeFinalize<ContextType, Finalizer,
4071
+ FinalizerDataType>::FinalizeFinalizeWrapperWithDataAndContext);
4072
+ }
4073
+
4074
+ inline ThreadSafeFunction::ThreadSafeFunction()
4075
+ : _tsfn() {
4076
+ }
4077
+
4078
+ inline ThreadSafeFunction::ThreadSafeFunction(
4079
+ napi_threadsafe_function tsfn)
4080
+ : _tsfn(tsfn) {
4081
+ }
4082
+
4083
+ inline ThreadSafeFunction::operator napi_threadsafe_function() const {
4084
+ return _tsfn;
4085
+ }
4086
+
4087
+ inline napi_status ThreadSafeFunction::BlockingCall() const {
4088
+ return CallInternal(nullptr, napi_tsfn_blocking);
4089
+ }
4090
+
4091
+ template <>
4092
+ inline napi_status ThreadSafeFunction::BlockingCall(
4093
+ void* data) const {
4094
+ return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_blocking);
4095
+ }
4096
+
4097
+ template <typename Callback>
4098
+ inline napi_status ThreadSafeFunction::BlockingCall(
4099
+ Callback callback) const {
4100
+ return CallInternal(new CallbackWrapper(callback), napi_tsfn_blocking);
4101
+ }
4102
+
4103
+ template <typename DataType, typename Callback>
4104
+ inline napi_status ThreadSafeFunction::BlockingCall(
4105
+ DataType* data, Callback callback) const {
4106
+ auto wrapper = [data, callback](Env env, Function jsCallback) {
4107
+ callback(env, jsCallback, data);
4108
+ };
4109
+ return CallInternal(new CallbackWrapper(wrapper), napi_tsfn_blocking);
4110
+ }
4111
+
4112
+ inline napi_status ThreadSafeFunction::NonBlockingCall() const {
4113
+ return CallInternal(nullptr, napi_tsfn_nonblocking);
4114
+ }
4115
+
4116
+ template <>
4117
+ inline napi_status ThreadSafeFunction::NonBlockingCall(
4118
+ void* data) const {
4119
+ return napi_call_threadsafe_function(_tsfn, data, napi_tsfn_nonblocking);
4120
+ }
4121
+
4122
+ template <typename Callback>
4123
+ inline napi_status ThreadSafeFunction::NonBlockingCall(
4124
+ Callback callback) const {
4125
+ return CallInternal(new CallbackWrapper(callback), napi_tsfn_nonblocking);
4126
+ }
4127
+
4128
+ template <typename DataType, typename Callback>
4129
+ inline napi_status ThreadSafeFunction::NonBlockingCall(
4130
+ DataType* data, Callback callback) const {
4131
+ auto wrapper = [data, callback](Env env, Function jsCallback) {
4132
+ callback(env, jsCallback, data);
4133
+ };
4134
+ return CallInternal(new CallbackWrapper(wrapper), napi_tsfn_nonblocking);
4135
+ }
4136
+
4137
+ inline void ThreadSafeFunction::Ref(napi_env env) const {
4138
+ if (_tsfn != nullptr) {
4139
+ napi_status status = napi_ref_threadsafe_function(env, _tsfn);
4140
+ NAPI_THROW_IF_FAILED_VOID(env, status);
4141
+ }
4142
+ }
4143
+
4144
+ inline void ThreadSafeFunction::Unref(napi_env env) const {
4145
+ if (_tsfn != nullptr) {
4146
+ napi_status status = napi_unref_threadsafe_function(env, _tsfn);
4147
+ NAPI_THROW_IF_FAILED_VOID(env, status);
4148
+ }
4149
+ }
4150
+
4151
+ inline napi_status ThreadSafeFunction::Acquire() const {
4152
+ return napi_acquire_threadsafe_function(_tsfn);
4153
+ }
4154
+
4155
+ inline napi_status ThreadSafeFunction::Release() {
4156
+ return napi_release_threadsafe_function(_tsfn, napi_tsfn_release);
4157
+ }
4158
+
4159
+ inline napi_status ThreadSafeFunction::Abort() {
4160
+ return napi_release_threadsafe_function(_tsfn, napi_tsfn_abort);
4161
+ }
4162
+
4163
+ inline ThreadSafeFunction::ConvertibleContext
4164
+ ThreadSafeFunction::GetContext() const {
4165
+ void* context;
4166
+ napi_status status = napi_get_threadsafe_function_context(_tsfn, &context);
4167
+ NAPI_FATAL_IF_FAILED(status, "ThreadSafeFunction::GetContext", "napi_get_threadsafe_function_context");
4168
+ return ConvertibleContext({ context });
4169
+ }
4170
+
4171
+ // static
4172
+ template <typename ResourceString, typename ContextType,
4173
+ typename Finalizer, typename FinalizerDataType>
4174
+ inline ThreadSafeFunction ThreadSafeFunction::New(napi_env env,
4175
+ const Function& callback,
4176
+ const Object& resource,
4177
+ ResourceString resourceName,
4178
+ size_t maxQueueSize,
4179
+ size_t initialThreadCount,
4180
+ ContextType* context,
4181
+ Finalizer finalizeCallback,
4182
+ FinalizerDataType* data,
4183
+ napi_finalize wrapper) {
4184
+ static_assert(details::can_make_string<ResourceString>::value
4185
+ || std::is_convertible<ResourceString, napi_value>::value,
4186
+ "Resource name should be convertible to the string type");
4187
+
4188
+ ThreadSafeFunction tsfn;
4189
+ auto* finalizeData = new details::ThreadSafeFinalize<ContextType, Finalizer,
4190
+ FinalizerDataType>({ data, finalizeCallback, &tsfn._tsfn });
4191
+ napi_status status = napi_create_threadsafe_function(env, callback, resource,
4192
+ Value::From(env, resourceName), maxQueueSize, initialThreadCount,
4193
+ finalizeData, wrapper, context, CallJS, &tsfn._tsfn);
4194
+ if (status != napi_ok) {
4195
+ delete finalizeData;
4196
+ NAPI_THROW_IF_FAILED(env, status, ThreadSafeFunction());
4197
+ }
4198
+
4199
+ return tsfn;
4200
+ }
4201
+
4202
+ inline napi_status ThreadSafeFunction::CallInternal(
4203
+ CallbackWrapper* callbackWrapper,
4204
+ napi_threadsafe_function_call_mode mode) const {
4205
+ napi_status status = napi_call_threadsafe_function(
4206
+ _tsfn, callbackWrapper, mode);
4207
+ if (status != napi_ok && callbackWrapper != nullptr) {
4208
+ delete callbackWrapper;
4209
+ }
4210
+
4211
+ return status;
4212
+ }
4213
+
4214
+ // static
4215
+ inline void ThreadSafeFunction::CallJS(napi_env env,
4216
+ napi_value jsCallback,
4217
+ void* /* context */,
4218
+ void* data) {
4219
+ if (env == nullptr && jsCallback == nullptr) {
4220
+ return;
4221
+ }
4222
+
4223
+ if (data != nullptr) {
4224
+ auto* callbackWrapper = static_cast<CallbackWrapper*>(data);
4225
+ (*callbackWrapper)(env, Function(env, jsCallback));
4226
+ delete callbackWrapper;
4227
+ } else if (jsCallback != nullptr) {
4228
+ Function(env, jsCallback).Call({});
4229
+ }
4230
+ }
4231
+
4232
+ ////////////////////////////////////////////////////////////////////////////////
4233
+ // Async Progress Worker class
4234
+ ////////////////////////////////////////////////////////////////////////////////
4235
+
4236
+ template<class T>
4237
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback)
4238
+ : AsyncProgressWorker(callback, "generic") {
4239
+ }
4240
+
4241
+ template<class T>
4242
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback,
4243
+ const char* resource_name)
4244
+ : AsyncProgressWorker(callback, resource_name, Object::New(callback.Env())) {
4245
+ }
4246
+
4247
+ template<class T>
4248
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Function& callback,
4249
+ const char* resource_name,
4250
+ const Object& resource)
4251
+ : AsyncProgressWorker(Object::New(callback.Env()),
4252
+ callback,
4253
+ resource_name,
4254
+ resource) {
4255
+ }
4256
+
4257
+ template<class T>
4258
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
4259
+ const Function& callback)
4260
+ : AsyncProgressWorker(receiver, callback, "generic") {
4261
+ }
4262
+
4263
+ template<class T>
4264
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
4265
+ const Function& callback,
4266
+ const char* resource_name)
4267
+ : AsyncProgressWorker(receiver,
4268
+ callback,
4269
+ resource_name,
4270
+ Object::New(callback.Env())) {
4271
+ }
4272
+
4273
+ template<class T>
4274
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(const Object& receiver,
4275
+ const Function& callback,
4276
+ const char* resource_name,
4277
+ const Object& resource)
4278
+ : AsyncWorker(receiver, callback, resource_name, resource),
4279
+ _asyncdata(nullptr),
4280
+ _asyncsize(0) {
4281
+ _tsfn = ThreadSafeFunction::New(callback.Env(), callback, resource_name, 1, 1);
4282
+ }
4283
+
4284
+ #if NAPI_VERSION > 4
4285
+ template<class T>
4286
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env)
4287
+ : AsyncProgressWorker(env, "generic") {
4288
+ }
4289
+
4290
+ template<class T>
4291
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
4292
+ const char* resource_name)
4293
+ : AsyncProgressWorker(env, resource_name, Object::New(env)) {
4294
+ }
4295
+
4296
+ template<class T>
4297
+ inline AsyncProgressWorker<T>::AsyncProgressWorker(Napi::Env env,
4298
+ const char* resource_name,
4299
+ const Object& resource)
4300
+ : AsyncWorker(env, resource_name, resource),
4301
+ _asyncdata(nullptr),
4302
+ _asyncsize(0) {
4303
+ // TODO: Once the changes to make the callback optional for threadsafe
4304
+ // functions are no longer optional we can remove the dummy Function here.
4305
+ Function callback;
4306
+ _tsfn = ThreadSafeFunction::New(env, callback, resource_name, 1, 1);
4307
+ }
4308
+ #endif
4309
+
4310
+ template<class T>
4311
+ inline AsyncProgressWorker<T>::~AsyncProgressWorker() {
4312
+ // Abort pending tsfn call.
4313
+ // Don't send progress events after we've already completed.
4314
+ _tsfn.Abort();
4315
+ {
4316
+ std::lock_guard<std::mutex> lock(_mutex);
4317
+ _asyncdata = nullptr;
4318
+ _asyncsize = 0;
4319
+ }
4320
+ _tsfn.Release();
4321
+ }
4322
+
4323
+ template<class T>
4324
+ inline void AsyncProgressWorker<T>::Execute() {
4325
+ ExecutionProgress progress(this);
4326
+ Execute(progress);
4327
+ }
4328
+
4329
+ template<class T>
4330
+ inline void AsyncProgressWorker<T>::WorkProgress_(Napi::Env /* env */, Napi::Function /* jsCallback */, void* _data) {
4331
+ AsyncProgressWorker* self = static_cast<AsyncProgressWorker*>(_data);
4332
+
4333
+ T* data;
4334
+ size_t size;
4335
+ {
4336
+ std::lock_guard<std::mutex> lock(self->_mutex);
4337
+ data = self->_asyncdata;
4338
+ size = self->_asyncsize;
4339
+ self->_asyncdata = nullptr;
4340
+ self->_asyncsize = 0;
4341
+ }
4342
+
4343
+ self->OnProgress(data, size);
4344
+ delete[] data;
4345
+ }
4346
+
4347
+ template<class T>
4348
+ inline void AsyncProgressWorker<T>::SendProgress_(const T* data, size_t count) {
4349
+ T* new_data = new T[count];
4350
+ std::copy(data, data + count, new_data);
4351
+
4352
+ T* old_data;
4353
+ {
4354
+ std::lock_guard<std::mutex> lock(_mutex);
4355
+ old_data = _asyncdata;
4356
+ _asyncdata = new_data;
4357
+ _asyncsize = count;
4358
+ }
4359
+ _tsfn.NonBlockingCall(this, WorkProgress_);
4360
+
4361
+ delete[] old_data;
4362
+ }
4363
+
4364
+ template<class T>
4365
+ inline void AsyncProgressWorker<T>::Signal() const {
4366
+ _tsfn.NonBlockingCall(this, WorkProgress_);
4367
+ }
4368
+
4369
+ template<class T>
4370
+ inline void AsyncProgressWorker<T>::ExecutionProgress::Signal() const {
4371
+ _worker->Signal();
4372
+ }
4373
+
4374
+ template<class T>
4375
+ inline void AsyncProgressWorker<T>::ExecutionProgress::Send(const T* data, size_t count) const {
4376
+ _worker->SendProgress_(data, count);
4377
+ }
4378
+
4379
+ #endif
4380
+
4381
+ ////////////////////////////////////////////////////////////////////////////////
4382
+ // Memory Management class
4383
+ ////////////////////////////////////////////////////////////////////////////////
4384
+
4385
+ inline int64_t MemoryManagement::AdjustExternalMemory(Env env, int64_t change_in_bytes) {
4386
+ int64_t result;
4387
+ napi_status status = napi_adjust_external_memory(env, change_in_bytes, &result);
4388
+ NAPI_THROW_IF_FAILED(env, status, 0);
4389
+ return result;
4390
+ }
4391
+
4392
+ ////////////////////////////////////////////////////////////////////////////////
4393
+ // Version Management class
4394
+ ////////////////////////////////////////////////////////////////////////////////
4395
+
4396
+ inline uint32_t VersionManagement::GetNapiVersion(Env env) {
4397
+ uint32_t result;
4398
+ napi_status status = napi_get_version(env, &result);
4399
+ NAPI_THROW_IF_FAILED(env, status, 0);
4400
+ return result;
4401
+ }
4402
+
4403
+ inline const napi_node_version* VersionManagement::GetNodeVersion(Env env) {
4404
+ const napi_node_version* result;
4405
+ napi_status status = napi_get_node_version(env, &result);
4406
+ NAPI_THROW_IF_FAILED(env, status, 0);
4407
+ return result;
4408
+ }
4409
+
4410
+ } // namespace Napi
4411
+
4412
+ #endif // SRC_NAPI_INL_H_