0sec-cli 0.18.0 → 0.22.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (329) hide show
  1. package/{0sec.js → 0.js} +90 -82
  2. package/LICENSE +1 -1
  3. package/README.md +62 -46
  4. package/chunks/adapt-loop-7I57LXXR.js +18 -0
  5. package/chunks/{adgraph-JLGA6RYI.js → adgraph-AUDQTBYA.js} +5 -5
  6. package/chunks/agent/skills/frameworks/entra-id.yaml +2 -2
  7. package/chunks/agent/skills/techniques/assumption-mining.yaml +2 -2
  8. package/chunks/agent/skills/techniques/cve-poc-adaptation.yaml +2 -2
  9. package/chunks/agent/skills/techniques/entra-attack-paths.yaml +1 -1
  10. package/chunks/agent/skills/techniques/kernel-weaponization.yaml +1 -1
  11. package/chunks/agent/skills/techniques/llm-prompt-injection.yaml +2 -2
  12. package/chunks/agent/skills/techniques/npm-ecosystem.yaml +1 -1
  13. package/chunks/agent/skills/techniques/poc-verification.yaml +1 -1
  14. package/chunks/agent/skills/techniques/seedless-depth-review.yaml +20 -38
  15. package/chunks/{artifact-scraper-HQ7HLP6V.js → artifact-scraper-PYZ3RU3O.js} +6 -6
  16. package/chunks/{assumption-mining-5SNIVVDN.js → assumption-mining-MFLF43IR.js} +24 -26
  17. package/chunks/{chunk-7DQEV5QI.js → chunk-23DG5YKS.js} +4 -4
  18. package/chunks/{chunk-UR4ELX2Y.js → chunk-2GV4HQKE.js} +340 -703
  19. package/chunks/{chunk-P6WKNFWX.js → chunk-2OQOQ2FZ.js} +8 -8
  20. package/chunks/{chunk-5G3ZXFBW.js → chunk-2WNCI664.js} +3 -3
  21. package/chunks/{chunk-DW5UWPFY.js → chunk-4VGLO2YS.js} +6 -5
  22. package/chunks/{chunk-KLTNTE2Z.js → chunk-5IBCBQEH.js} +13 -14
  23. package/chunks/{chunk-N7BOUYEV.js → chunk-5MWAG3FT.js} +14 -14
  24. package/chunks/{chunk-OEFNRYI2.js → chunk-6GHSR47F.js} +3 -6
  25. package/chunks/chunk-6YNKRUMC.js +4474 -0
  26. package/chunks/{chunk-6L26OBWI.js → chunk-76KSBWOQ.js} +255 -35
  27. package/chunks/chunk-7DEHG7YT.js +2143 -0
  28. package/chunks/{chunk-AD7WXDH6.js → chunk-7UFUALR2.js} +288 -22
  29. package/chunks/{chunk-53G27VPS.js → chunk-AZ7L3HFD.js} +7 -2
  30. package/chunks/{chunk-TSK2AG6J.js → chunk-CAYANSH6.js} +173 -1757
  31. package/chunks/{chunk-SAFFWQW4.js → chunk-DKSCN2AB.js} +7 -7
  32. package/chunks/{chunk-2JCCA2JL.js → chunk-DM5C5L5D.js} +3 -3
  33. package/chunks/{chunk-RWONANDA.js → chunk-DQTNI3KY.js} +5 -5
  34. package/chunks/{chunk-5JI7L7KV.js → chunk-EGZGZFAH.js} +10036 -12938
  35. package/chunks/{chunk-6LKRLK2R.js → chunk-FHC2B7LN.js} +3 -3
  36. package/chunks/{chunk-MYVT64FN.js → chunk-GHWODZMR.js} +2 -2
  37. package/chunks/{chunk-2RMLOJVB.js → chunk-GVD6SJGH.js} +2 -2
  38. package/chunks/{chunk-SF4KZ4O3.js → chunk-HLFS6WV2.js} +2 -2
  39. package/chunks/chunk-HR2TU42B.js +1228 -0
  40. package/chunks/{chunk-BFRB6HB2.js → chunk-HUNAPTKV.js} +3 -3
  41. package/chunks/chunk-IO7R7WBM.js +114 -0
  42. package/chunks/{chunk-WEBMRWEG.js → chunk-IXT64DV5.js} +656 -1868
  43. package/chunks/{chunk-3MOLBTLS.js → chunk-K2PWUPUI.js} +2 -2
  44. package/chunks/{chunk-WXQ6BJ6A.js → chunk-LCRTAQN2.js} +7 -7
  45. package/chunks/{chunk-SVJEDVYK.js → chunk-LPTRPRZR.js} +5 -5
  46. package/chunks/{chunk-CMKXP5RR.js → chunk-LYUAM3TG.js} +3 -3
  47. package/chunks/{chunk-BVAZO4WA.js → chunk-MICHDTBK.js} +3 -3
  48. package/chunks/{chunk-UCYRA73C.js → chunk-OAYUPGF6.js} +4 -4
  49. package/chunks/{chunk-XM5NLHYU.js → chunk-OWLJYJ5O.js} +14 -14
  50. package/chunks/{chunk-BKZFDZ23.js → chunk-QCUFBVIZ.js} +3 -3
  51. package/chunks/chunk-RE5EZDG2.js +5523 -0
  52. package/chunks/{chunk-ASUR522M.js → chunk-RRD4XXPU.js} +9 -9
  53. package/chunks/{chunk-AHTBZC3E.js → chunk-RSIUX3RW.js} +4 -4
  54. package/chunks/{chunk-BZNEY2ZS.js → chunk-SJBCK2EI.js} +15268 -16053
  55. package/chunks/{chunk-IOL7D5YV.js → chunk-SQCBHFWN.js} +3 -3
  56. package/chunks/chunk-TH2LW437.js +7829 -0
  57. package/chunks/{chunk-WVTBZEQO.js → chunk-TM74SSHC.js} +8 -8
  58. package/chunks/{chunk-U5FLXGHT.js → chunk-TO35Y326.js} +13 -729
  59. package/chunks/chunk-TRH2PJSN.js +547 -0
  60. package/chunks/{chunk-YYRBQVFE.js → chunk-TZFIAMWZ.js} +70 -16
  61. package/chunks/chunk-UELXHK2Y.js +1950 -0
  62. package/chunks/{chunk-RJWOYEOG.js → chunk-UEX7PFSE.js} +5 -5
  63. package/chunks/{chunk-VQG4FT5D.js → chunk-UFHN6RIU.js} +2 -2
  64. package/chunks/{chunk-JQHOLLTD.js → chunk-UU7PQP6N.js} +9 -9
  65. package/chunks/{chunk-BFR2CDV5.js → chunk-UV5QWZGE.js} +283 -153
  66. package/chunks/{chunk-WU6AFRAZ.js → chunk-V36HVTUL.js} +12 -12
  67. package/chunks/{chunk-IR537GON.js → chunk-VCSFCHAJ.js} +2 -2
  68. package/chunks/{chunk-YLMN3N25.js → chunk-VIT5ALXF.js} +3 -3
  69. package/chunks/{chunk-BATRQBOR.js → chunk-VMRRNRMR.js} +41 -41
  70. package/chunks/{chunk-72CISA2D.js → chunk-VNRY6AWF.js} +89 -41
  71. package/chunks/chunk-W6UWZFKL.js +298 -0
  72. package/chunks/{chunk-3KPWWJCI.js → chunk-XFCTUKXR.js} +88 -88
  73. package/chunks/{chunk-GMT2AZKM.js → chunk-YIQSGNAA.js} +897 -130
  74. package/chunks/codex-models-7TLJSY3L.js +17 -0
  75. package/chunks/{commands-TBC2F5CE.js → commands-NEI7L3QA.js} +2836 -1424
  76. package/chunks/corpus-v1.json +2 -2
  77. package/chunks/data/appsec-archetypes.json +2 -2
  78. package/chunks/data/chromium-archetypes.json +1 -1
  79. package/chunks/data/freebsd-archetypes.json +1 -1
  80. package/chunks/data/kernel-archetypes.json +2 -2
  81. package/chunks/db-5AP3JB4W.js +16 -0
  82. package/chunks/{disclose-EKTWSCGW.js → disclose-LDNKAVRH.js} +13 -11
  83. package/chunks/{dist-4YICS2J2.js → dist-3FNZIBB4.js} +394 -404
  84. package/chunks/{dist-4MAW5X6L.js → dist-5YHYNLI3.js} +6 -6
  85. package/chunks/{dist-FMGSR3DW.js → dist-DD4H6HMQ.js} +13 -5
  86. package/chunks/dist-KEULE5IQ.js +21914 -0
  87. package/chunks/dist-XZB53PS5.js +53 -0
  88. package/chunks/eval-runner-43BO2C4T.js +26 -0
  89. package/chunks/example-manifest.json +2 -2
  90. package/chunks/{exploit-agent-45YQOHPJ.js → exploit-agent-N4A5ELBK.js} +4 -4
  91. package/chunks/{exploit-autoclimb-UZGEMGYT.js → exploit-autoclimb-N7P6KIF5.js} +6 -6
  92. package/chunks/{exploit-climb-SG23KB2G.js → exploit-climb-WNEJUB5W.js} +12 -12
  93. package/chunks/fix-Z452M4EC.js +26 -0
  94. package/chunks/{github-issues-MJ6OYOOU.js → github-issues-IGTTWM7V.js} +7 -7
  95. package/chunks/harness-QJJJVSG4.js +22 -0
  96. package/chunks/http-conformance-U5F6GKYG.js +11 -0
  97. package/chunks/http-sender-I5XFFY35.js +10 -0
  98. package/chunks/hunt-scan-K6D2V3PX.js +36 -0
  99. package/chunks/{identity-6ZAIIWOR.js → identity-62SVRW4H.js} +5 -5
  100. package/chunks/{kernel-primitive-7U6CWHXD.js → kernel-primitive-TKUACPKD.js} +6 -6
  101. package/chunks/{kernel-vm-runner-3JAU4O6J.js → kernel-vm-runner-RAAO2PGT.js} +6 -6
  102. package/chunks/memsafety-scan-FCPAXIZE.js +16 -0
  103. package/chunks/{native-loop-RJCJPOUS.js → native-loop-2ZRJENZ3.js} +17 -18
  104. package/chunks/{npm-detectors-KB5Y5ZFX.js → npm-detectors-Z2ZIQCN4.js} +6 -6
  105. package/chunks/npm-dynamic-discovery-5J7FT3XM.js +13 -0
  106. package/chunks/orchestrate-2GWGNTBF.js +58 -0
  107. package/chunks/{pre-recon-cve-66EB6G4M.js → pre-recon-cve-X2K2P5YQ.js} +4 -4
  108. package/chunks/prepare-KYHGFWCJ.js +13 -0
  109. package/chunks/process-N245QIQG.js +12 -0
  110. package/chunks/{run-D56MPDKY.js → run-U3XU4CTC.js} +11507 -13632
  111. package/chunks/{runtime-G7YWIO3B.js → runtime-G75TEWQY.js} +10 -11
  112. package/chunks/runtime-XFWJTBUO.js +12 -0
  113. package/chunks/{scan-stream-FHI2FYZE.js → scan-stream-BMOCPKQX.js} +7 -7
  114. package/chunks/scope-WXI46UHJ.js +21 -0
  115. package/chunks/{session-store-GZ4A4KYT.js → session-store-27L3ZU73.js} +6 -6
  116. package/chunks/source-files-REHOM44I.js +12 -0
  117. package/chunks/{specdrift-WCLH6TTQ.js → specdrift-AFS54WEJ.js} +5 -5
  118. package/chunks/token-KRWP5JYA.js +72 -0
  119. package/chunks/token-util-DWTL33S3.js +8 -0
  120. package/chunks/variant-candidates-YWZORZMK.js +13 -0
  121. package/chunks/{web-recon-prepass-JJGPEOLE.js → web-recon-prepass-YVMFXCIK.js} +9 -9
  122. package/cli-path-traversal-fixture.js +352 -0
  123. package/dashboard/assets/bot-Bg1Tp9_d.js +1 -0
  124. package/dashboard/assets/dist-B_dDCPbu.js +1 -0
  125. package/dashboard/assets/findings-page-mayGhNx6.js +5 -0
  126. package/dashboard/assets/format--susmHX7.js +1 -0
  127. package/dashboard/assets/input-TuVPR3Zl.js +1 -0
  128. package/dashboard/assets/jsx-runtime-MGseXjj5.js +3 -0
  129. package/dashboard/assets/live-page-HWWHX99U.js +1 -0
  130. package/dashboard/assets/meta-tile-ckHo-w7k.js +1 -0
  131. package/dashboard/assets/operations-BxZ0OoXO.js +10 -0
  132. package/dashboard/assets/operations-CMFVrXEe.css +2 -0
  133. package/dashboard/assets/operations-app-D3d35rgo.js +46 -0
  134. package/dashboard/assets/overview-page-W_4IteEr.js +1 -0
  135. package/dashboard/assets/page-header-Bw9AJgBV.js +1 -0
  136. package/dashboard/assets/scans-page-CgzVXHnn.js +1 -0
  137. package/dashboard/assets/siren-KQj8QAnJ.js +1 -0
  138. package/dashboard/assets/table-8EDklulN.js +1 -0
  139. package/dashboard/assets/tabs-CFHPOwg8.js +1 -0
  140. package/dashboard/index.html +4 -5
  141. package/node_modules/node-addon-api/LICENSE.md +9 -0
  142. package/node_modules/node-addon-api/README.md +95 -0
  143. package/node_modules/node-addon-api/common.gypi +21 -0
  144. package/node_modules/node-addon-api/except.gypi +25 -0
  145. package/node_modules/node-addon-api/index.js +14 -0
  146. package/node_modules/node-addon-api/napi-inl.deprecated.h +186 -0
  147. package/node_modules/node-addon-api/napi-inl.h +7250 -0
  148. package/node_modules/node-addon-api/napi.h +3463 -0
  149. package/node_modules/node-addon-api/node_addon_api.gyp +42 -0
  150. package/node_modules/node-addon-api/node_api.gyp +9 -0
  151. package/node_modules/node-addon-api/noexcept.gypi +26 -0
  152. package/node_modules/node-addon-api/nothing.c +0 -0
  153. package/node_modules/node-addon-api/package-support.json +21 -0
  154. package/node_modules/node-addon-api/package.json +481 -0
  155. package/node_modules/node-addon-api/tools/README.md +73 -0
  156. package/node_modules/node-addon-api/tools/check-napi.js +99 -0
  157. package/node_modules/node-addon-api/tools/clang-format.js +71 -0
  158. package/node_modules/node-addon-api/tools/conversion.js +301 -0
  159. package/node_modules/node-gyp-build/LICENSE +21 -0
  160. package/node_modules/node-gyp-build/README.md +58 -0
  161. package/node_modules/node-gyp-build/SECURITY.md +5 -0
  162. package/node_modules/node-gyp-build/bin.js +84 -0
  163. package/node_modules/node-gyp-build/build-test.js +19 -0
  164. package/node_modules/node-gyp-build/index.js +6 -0
  165. package/node_modules/node-gyp-build/node-gyp-build.js +207 -0
  166. package/node_modules/node-gyp-build/optional.js +7 -0
  167. package/node_modules/node-gyp-build/package.json +43 -0
  168. package/node_modules/tree-sitter/LICENSE +21 -0
  169. package/node_modules/tree-sitter/README.md +90 -0
  170. package/node_modules/tree-sitter/binding.gyp +76 -0
  171. package/node_modules/tree-sitter/index.js +895 -0
  172. package/node_modules/tree-sitter/package.json +62 -0
  173. package/node_modules/tree-sitter/prebuilds/darwin-arm64/tree-sitter.node +0 -0
  174. package/node_modules/tree-sitter/prebuilds/darwin-x64/tree-sitter.node +0 -0
  175. package/node_modules/tree-sitter/prebuilds/linux-x64/tree-sitter.node +0 -0
  176. package/node_modules/tree-sitter/prebuilds/win32-x64/tree-sitter.node +0 -0
  177. package/node_modules/tree-sitter/src/addon_data.h +47 -0
  178. package/node_modules/tree-sitter/src/binding.cc +35 -0
  179. package/node_modules/tree-sitter/src/conversions.cc +140 -0
  180. package/node_modules/tree-sitter/src/conversions.h +22 -0
  181. package/node_modules/tree-sitter/src/language.cc +106 -0
  182. package/node_modules/tree-sitter/src/language.h +17 -0
  183. package/node_modules/tree-sitter/src/logger.cc +70 -0
  184. package/node_modules/tree-sitter/src/logger.h +19 -0
  185. package/node_modules/tree-sitter/src/lookaheaditerator.cc +122 -0
  186. package/node_modules/tree-sitter/src/lookaheaditerator.h +33 -0
  187. package/node_modules/tree-sitter/src/node.cc +1056 -0
  188. package/node_modules/tree-sitter/src/node.h +30 -0
  189. package/node_modules/tree-sitter/src/parser.cc +298 -0
  190. package/node_modules/tree-sitter/src/parser.h +35 -0
  191. package/node_modules/tree-sitter/src/query.cc +383 -0
  192. package/node_modules/tree-sitter/src/query.h +39 -0
  193. package/node_modules/tree-sitter/src/tree.cc +307 -0
  194. package/node_modules/tree-sitter/src/tree.h +45 -0
  195. package/node_modules/tree-sitter/src/tree_cursor.cc +213 -0
  196. package/node_modules/tree-sitter/src/tree_cursor.h +52 -0
  197. package/node_modules/tree-sitter/tree-sitter.d.ts +211 -0
  198. package/node_modules/tree-sitter/vendor/tree-sitter/LICENSE +21 -0
  199. package/node_modules/tree-sitter/vendor/tree-sitter/README.md +21 -0
  200. package/node_modules/tree-sitter/vendor/tree-sitter/lib/README.md +6 -0
  201. package/node_modules/tree-sitter/vendor/tree-sitter/lib/include/tree_sitter/api.h +1266 -0
  202. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/alloc.c +48 -0
  203. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/alloc.h +41 -0
  204. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/array.h +290 -0
  205. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/atomic.h +68 -0
  206. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/clock.h +146 -0
  207. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/error_costs.h +11 -0
  208. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/get_changed_ranges.c +501 -0
  209. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/get_changed_ranges.h +36 -0
  210. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/host.h +21 -0
  211. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/language.c +221 -0
  212. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/language.h +299 -0
  213. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/length.h +52 -0
  214. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lexer.c +419 -0
  215. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lexer.h +49 -0
  216. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/lib.c +14 -0
  217. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/node.c +774 -0
  218. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/parser.c +2151 -0
  219. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/parser.h +230 -0
  220. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/point.h +62 -0
  221. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/query.c +4134 -0
  222. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/reduce_action.h +34 -0
  223. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/reusable_node.h +95 -0
  224. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/stack.c +899 -0
  225. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/stack.h +133 -0
  226. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/subtree.c +1060 -0
  227. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/subtree.h +382 -0
  228. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree.c +165 -0
  229. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree.h +31 -0
  230. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree_cursor.c +714 -0
  231. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/tree_cursor.h +48 -0
  232. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/ICU_SHA +1 -0
  233. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/LICENSE +414 -0
  234. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/README.md +29 -0
  235. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/ptypes.h +1 -0
  236. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/umachine.h +448 -0
  237. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/urename.h +1 -0
  238. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf.h +1 -0
  239. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf16.h +733 -0
  240. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode/utf8.h +881 -0
  241. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/unicode.h +50 -0
  242. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/stdlib-symbols.txt +23 -0
  243. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/stdlib.c +109 -0
  244. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm/wasm-stdlib.h +1294 -0
  245. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm_store.c +1823 -0
  246. package/node_modules/tree-sitter/vendor/tree-sitter/lib/src/wasm_store.h +31 -0
  247. package/node_modules/tree-sitter/vendor/tree-sitter/test/fixtures/grammars/.gitkeep +0 -0
  248. package/node_modules/tree-sitter-c/LICENSE +21 -0
  249. package/node_modules/tree-sitter-c/README.md +18 -0
  250. package/node_modules/tree-sitter-c/binding.gyp +20 -0
  251. package/node_modules/tree-sitter-c/bindings/node/binding.cc +20 -0
  252. package/node_modules/tree-sitter-c/bindings/node/index.d.ts +28 -0
  253. package/node_modules/tree-sitter-c/bindings/node/index.js +7 -0
  254. package/node_modules/tree-sitter-c/grammar.js +1463 -0
  255. package/node_modules/tree-sitter-c/package.json +108 -0
  256. package/node_modules/tree-sitter-c/prebuilds/darwin-arm64/tree-sitter-c.node +0 -0
  257. package/node_modules/tree-sitter-c/prebuilds/darwin-x64/tree-sitter-c.node +0 -0
  258. package/node_modules/tree-sitter-c/prebuilds/linux-x64/tree-sitter-c.node +0 -0
  259. package/node_modules/tree-sitter-c/prebuilds/win32-x64/tree-sitter-c.node +0 -0
  260. package/node_modules/tree-sitter-c/queries/highlights.scm +81 -0
  261. package/node_modules/tree-sitter-c/queries/tags.scm +9 -0
  262. package/node_modules/tree-sitter-c/src/grammar.json +9682 -0
  263. package/node_modules/tree-sitter-c/src/node-types.json +4556 -0
  264. package/node_modules/tree-sitter-c/src/parser.c +113852 -0
  265. package/node_modules/tree-sitter-c/src/tree_sitter/alloc.h +54 -0
  266. package/node_modules/tree-sitter-c/src/tree_sitter/array.h +290 -0
  267. package/node_modules/tree-sitter-c/src/tree_sitter/parser.h +265 -0
  268. package/package.json +20 -13
  269. package/vendor/tree-sitter-0.21.1-node24.json +16 -0
  270. package/vendor/tree-sitter-0.21.1-node24.tgz +0 -0
  271. package/chunks/adapt-loop-SMH6V4PD.js +0 -18
  272. package/chunks/appsec-catalog-CBWHGTEL.js +0 -24
  273. package/chunks/chunk-242TMK6G.js +0 -110
  274. package/chunks/chunk-57ZENEX2.js +0 -169
  275. package/chunks/chunk-ADOLI6DT.js +0 -102
  276. package/chunks/chunk-ATATQACO.js +0 -471
  277. package/chunks/chunk-C2WXWFBB.js +0 -1070
  278. package/chunks/chunk-F3WBKITT.js +0 -687
  279. package/chunks/chunk-H2FFLZNK.js +0 -3
  280. package/chunks/chunk-LP3HHYQU.js +0 -245
  281. package/chunks/chunk-QI233I24.js +0 -333
  282. package/chunks/chunk-RRMJC3ZE.js +0 -105
  283. package/chunks/chunk-Z5FA2XOX.js +0 -2798
  284. package/chunks/cost-ledger-NU63MYZM.js +0 -13
  285. package/chunks/db-KFWOAUUB.js +0 -16
  286. package/chunks/eval-runner-7ZF472KV.js +0 -27
  287. package/chunks/fix-IG7LYXH3.js +0 -12
  288. package/chunks/harness-7ODOLOWU.js +0 -22
  289. package/chunks/http-conformance-DU66MZIU.js +0 -11
  290. package/chunks/http-sender-GWH2IYEA.js +0 -10
  291. package/chunks/hunt-scan-MMKUOETU.js +0 -38
  292. package/chunks/memsafety-scan-J7SECV5W.js +0 -16
  293. package/chunks/npm-dynamic-discovery-AHROOZDB.js +0 -13
  294. package/chunks/orchestrate-OC2VZN2X.js +0 -62
  295. package/chunks/pipeline-BZK5BLJR.js +0 -14
  296. package/chunks/prepare-MYY743TK.js +0 -13
  297. package/chunks/process-LQRCS5OY.js +0 -13
  298. package/chunks/replay-runner-45G3VU7X.js +0 -39
  299. package/chunks/runtime-J7PLXZNM.js +0 -12
  300. package/chunks/scope-BI7BF4ZY.js +0 -18
  301. package/chunks/source-files-PWRQR6LY.js +0 -12
  302. package/chunks/variant-candidates-YERCYMPD.js +0 -13
  303. package/dashboard/assets/0sec-icon-66SreztZ.gif +0 -0
  304. package/dashboard/assets/bot-yILPnRgD.js +0 -1
  305. package/dashboard/assets/chevron-down-BeDJ-Vka.js +0 -1
  306. package/dashboard/assets/circle-alert-BEnMBMT-.js +0 -1
  307. package/dashboard/assets/client-DRYsuOGl.js +0 -9
  308. package/dashboard/assets/copy-Bi5vzoF9.js +0 -1
  309. package/dashboard/assets/desktop-CXTQonNm.js +0 -32
  310. package/dashboard/assets/desktop-NOekk4QS.css +0 -1
  311. package/dashboard/assets/dist-CmX-h7a4.js +0 -1
  312. package/dashboard/assets/dist-DAwYQmf_.js +0 -45
  313. package/dashboard/assets/findings-page-CACW9nw2.js +0 -5
  314. package/dashboard/assets/format-PoISqsES.js +0 -1
  315. package/dashboard/assets/jsx-runtime-C7oxC63R.js +0 -1
  316. package/dashboard/assets/live-page-DrgQqQK5.js +0 -1
  317. package/dashboard/assets/meta-tile-CcskIV_o.js +0 -1
  318. package/dashboard/assets/operations-BKE8T-vY.css +0 -2
  319. package/dashboard/assets/operations-D8nUP5_m.js +0 -4
  320. package/dashboard/assets/operations-app-CQQhG54n.js +0 -2
  321. package/dashboard/assets/overview-page-DTGS8dPo.js +0 -1
  322. package/dashboard/assets/page-header-MFVvEtZW.js +0 -1
  323. package/dashboard/assets/play-Dwo-9xt6.js +0 -1
  324. package/dashboard/assets/scans-page-ChVtq8Us.js +0 -1
  325. package/dashboard/assets/search-CiooTLck.js +0 -1
  326. package/dashboard/assets/siren-CCBwTSuk.js +0 -1
  327. package/dashboard/assets/table-BYS-nIVA.js +0 -1
  328. package/dashboard/assets/tabs-DivxbhQy.js +0 -1
  329. package/dashboard/desktop.html +0 -21
@@ -0,0 +1,3463 @@
1
+ #ifndef SRC_NAPI_H_
2
+ #define SRC_NAPI_H_
3
+
4
+ #ifndef NAPI_HAS_THREADS
5
+ #if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \
6
+ (defined(__wasi__) && defined(_REENTRANT)))
7
+ #define NAPI_HAS_THREADS 1
8
+ #else
9
+ #define NAPI_HAS_THREADS 0
10
+ #endif
11
+ #endif
12
+
13
+ #include <node_api.h>
14
+ #include <functional>
15
+ #include <initializer_list>
16
+ #include <memory>
17
+ #if NAPI_HAS_THREADS
18
+ #include <mutex>
19
+ #endif // NAPI_HAS_THREADS
20
+ #include <chrono>
21
+ #include <string>
22
+ #include <string_view>
23
+ #include <type_traits>
24
+ #include <utility>
25
+ #include <vector>
26
+
27
+ // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known
28
+ // good version)
29
+ #if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210
30
+ #define NAPI_HAS_CONSTEXPR 1
31
+ #endif
32
+
33
+ // VS2013 does not support char16_t literal strings, so we'll work around it
34
+ // using wchar_t strings and casting them. This is safe as long as the character
35
+ // sizes are the same.
36
+ #if defined(_MSC_VER) && _MSC_VER <= 1800
37
+ static_assert(sizeof(char16_t) == sizeof(wchar_t),
38
+ "Size mismatch between char16_t and wchar_t");
39
+ #define NAPI_WIDE_TEXT(x) reinterpret_cast<char16_t*>(L##x)
40
+ #else
41
+ #define NAPI_WIDE_TEXT(x) u##x
42
+ #endif
43
+
44
+ // Backwards-compatibility to handle the rename of this macro definition, in
45
+ // case they are used within userland code.
46
+ #ifdef NAPI_CPP_EXCEPTIONS
47
+ #define NODE_ADDON_API_CPP_EXCEPTIONS
48
+ #endif
49
+ #if defined(NODE_ADDON_API_CPP_EXCEPTIONS) && !defined(NAPI_CPP_EXCEPTIONS)
50
+ #define NAPI_CPP_EXCEPTIONS
51
+ #endif
52
+ #ifdef NAPI_DISABLE_CPP_EXCEPTIONS
53
+ #define NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS
54
+ #endif
55
+ #if defined(NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS) && \
56
+ !defined(NAPI_DISABLE_CPP_EXCEPTIONS)
57
+ #define NAPI_DISABLE_CPP_EXCEPTIONS
58
+ #endif
59
+
60
+ // If C++ exceptions are not explicitly enabled or disabled, enable them
61
+ // if exceptions were enabled in the compiler settings.
62
+ #if !defined(NODE_ADDON_API_CPP_EXCEPTIONS) && \
63
+ !defined(NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS)
64
+ #if defined(_CPPUNWIND) || defined(__EXCEPTIONS)
65
+ #define NODE_ADDON_API_CPP_EXCEPTIONS
66
+ #else
67
+ #error Exception support not detected. \
68
+ Define either NODE_ADDON_API_CPP_EXCEPTIONS or NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS.
69
+ #endif
70
+ #endif
71
+
72
+ // If C++ NODE_ADDON_API_CPP_EXCEPTIONS are enabled, NODE_ADDON_API_ENABLE_MAYBE
73
+ // should not be set
74
+ #if defined(NODE_ADDON_API_CPP_EXCEPTIONS) && \
75
+ defined(NODE_ADDON_API_ENABLE_MAYBE)
76
+ #error NODE_ADDON_API_ENABLE_MAYBE should not be set when \
77
+ NODE_ADDON_API_CPP_EXCEPTIONS is defined.
78
+ #endif
79
+
80
+ #ifdef _NOEXCEPT
81
+ #define NAPI_NOEXCEPT _NOEXCEPT
82
+ #else
83
+ #define NAPI_NOEXCEPT noexcept
84
+ #endif
85
+
86
+ #ifdef NODE_ADDON_API_CPP_EXCEPTIONS
87
+
88
+ // When C++ exceptions are enabled, Errors are thrown directly. There is no need
89
+ // to return anything after the throw statements. The variadic parameter is an
90
+ // optional return value that is ignored.
91
+ // We need _VOID versions of the macros to avoid warnings resulting from
92
+ // leaving the NAPI_THROW_* `...` argument empty.
93
+
94
+ #define NAPI_THROW(e, ...) throw e
95
+ #define NAPI_THROW_VOID(e) throw e
96
+
97
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
98
+ if ((status) != napi_ok) throw Napi::Error::New(env);
99
+
100
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
101
+ if ((status) != napi_ok) throw Napi::Error::New(env);
102
+
103
+ #else // NODE_ADDON_API_CPP_EXCEPTIONS
104
+
105
+ // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
106
+ // which are pending until the callback returns to JS. The variadic parameter
107
+ // is an optional return value; usually it is an empty result.
108
+ // We need _VOID versions of the macros to avoid warnings resulting from
109
+ // leaving the NAPI_THROW_* `...` argument empty.
110
+
111
+ #define NAPI_THROW(e, ...) \
112
+ do { \
113
+ (e).ThrowAsJavaScriptException(); \
114
+ return __VA_ARGS__; \
115
+ } while (0)
116
+
117
+ #define NAPI_THROW_VOID(e) \
118
+ do { \
119
+ (e).ThrowAsJavaScriptException(); \
120
+ return; \
121
+ } while (0)
122
+
123
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
124
+ if ((status) != napi_ok) { \
125
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
126
+ return __VA_ARGS__; \
127
+ }
128
+
129
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
130
+ if ((status) != napi_ok) { \
131
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
132
+ return; \
133
+ }
134
+
135
+ #endif // NODE_ADDON_API_CPP_EXCEPTIONS
136
+
137
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
138
+ #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \
139
+ NAPI_THROW_IF_FAILED(env, status, Napi::Nothing<type>())
140
+
141
+ #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \
142
+ NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \
143
+ return Napi::Just<type>(result);
144
+ #else
145
+ #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \
146
+ NAPI_THROW_IF_FAILED(env, status, type())
147
+
148
+ #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \
149
+ NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \
150
+ return result;
151
+ #endif
152
+
153
+ #define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
154
+ #define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
155
+
156
+ #define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \
157
+ NAPI_DISALLOW_ASSIGN(CLASS) \
158
+ NAPI_DISALLOW_COPY(CLASS)
159
+
160
+ #define NAPI_CHECK(condition, location, message) \
161
+ do { \
162
+ if (!(condition)) { \
163
+ Napi::Error::Fatal((location), (message)); \
164
+ } \
165
+ } while (0)
166
+
167
+ // Internal check helper. Be careful that the formatted message length should be
168
+ // max 255 size and null terminated.
169
+ #define NAPI_INTERNAL_CHECK(expr, location, ...) \
170
+ do { \
171
+ if (!(expr)) { \
172
+ std::string msg = Napi::details::StringFormat(__VA_ARGS__); \
173
+ Napi::Error::Fatal(location, msg.c_str()); \
174
+ } \
175
+ } while (0)
176
+
177
+ #define NAPI_INTERNAL_CHECK_EQ(actual, expected, value_format, location) \
178
+ do { \
179
+ auto actual_value = (actual); \
180
+ NAPI_INTERNAL_CHECK(actual_value == (expected), \
181
+ location, \
182
+ "Expected " #actual " to be equal to " #expected \
183
+ ", but got " value_format ".", \
184
+ actual_value); \
185
+ } while (0)
186
+
187
+ #define NAPI_FATAL_IF_FAILED(status, location, message) \
188
+ NAPI_CHECK((status) == napi_ok, location, message)
189
+
190
+ ////////////////////////////////////////////////////////////////////////////////
191
+ /// Node-API C++ Wrapper Classes
192
+ ///
193
+ /// These classes wrap the "Node-API" ABI-stable C APIs for Node.js, providing a
194
+ /// C++ object model and C++ exception-handling semantics with low overhead.
195
+ /// The wrappers are all header-only so that they do not affect the ABI.
196
+ ////////////////////////////////////////////////////////////////////////////////
197
+ namespace Napi {
198
+
199
+ #ifdef NAPI_CPP_CUSTOM_NAMESPACE
200
+ // NAPI_CPP_CUSTOM_NAMESPACE can be #define'd per-addon to avoid symbol
201
+ // conflicts between different instances of node-addon-api
202
+
203
+ // First dummy definition of the namespace to make sure that Napi::(name) still
204
+ // refers to the right things inside this file.
205
+ namespace NAPI_CPP_CUSTOM_NAMESPACE {}
206
+ using namespace NAPI_CPP_CUSTOM_NAMESPACE;
207
+
208
+ namespace NAPI_CPP_CUSTOM_NAMESPACE {
209
+ #endif
210
+
211
+ // Forward declarations
212
+ class Env;
213
+ class Value;
214
+ class Boolean;
215
+ class Number;
216
+ #if NAPI_VERSION > 5
217
+ class BigInt;
218
+ #endif // NAPI_VERSION > 5
219
+ #if (NAPI_VERSION > 4)
220
+ class Date;
221
+ #endif
222
+ class String;
223
+ class Object;
224
+ class Array;
225
+ class ArrayBuffer;
226
+ class Function;
227
+ class Error;
228
+ class PropertyDescriptor;
229
+ class CallbackInfo;
230
+ class TypedArray;
231
+ template <typename T>
232
+ class TypedArrayOf;
233
+
234
+ using Int8Array =
235
+ TypedArrayOf<int8_t>; ///< Typed-array of signed 8-bit integers
236
+ using Uint8Array =
237
+ TypedArrayOf<uint8_t>; ///< Typed-array of unsigned 8-bit integers
238
+ using Int16Array =
239
+ TypedArrayOf<int16_t>; ///< Typed-array of signed 16-bit integers
240
+ using Uint16Array =
241
+ TypedArrayOf<uint16_t>; ///< Typed-array of unsigned 16-bit integers
242
+ using Int32Array =
243
+ TypedArrayOf<int32_t>; ///< Typed-array of signed 32-bit integers
244
+ using Uint32Array =
245
+ TypedArrayOf<uint32_t>; ///< Typed-array of unsigned 32-bit integers
246
+ using Float32Array =
247
+ TypedArrayOf<float>; ///< Typed-array of 32-bit floating-point values
248
+ using Float64Array =
249
+ TypedArrayOf<double>; ///< Typed-array of 64-bit floating-point values
250
+ #if NAPI_VERSION > 5
251
+ using BigInt64Array =
252
+ TypedArrayOf<int64_t>; ///< Typed array of signed 64-bit integers
253
+ using BigUint64Array =
254
+ TypedArrayOf<uint64_t>; ///< Typed array of unsigned 64-bit integers
255
+ #endif // NAPI_VERSION > 5
256
+
257
+ /// Defines the signature of a Node-API C++ module's registration callback
258
+ /// (init) function.
259
+ using ModuleRegisterCallback = Object (*)(Env env, Object exports);
260
+
261
+ class MemoryManagement;
262
+
263
+ /// A simple Maybe type, representing an object which may or may not have a
264
+ /// value.
265
+ ///
266
+ /// If an API method returns a Maybe<>, the API method can potentially fail
267
+ /// either because an exception is thrown, or because an exception is pending,
268
+ /// e.g. because a previous API call threw an exception that hasn't been
269
+ /// caught yet. In that case, a "Nothing" value is returned.
270
+ template <class T>
271
+ class Maybe {
272
+ public:
273
+ bool IsNothing() const;
274
+ bool IsJust() const;
275
+
276
+ /// Short-hand for Unwrap(), which doesn't return a value. Could be used
277
+ /// where the actual value of the Maybe is not needed like Object::Set.
278
+ /// If this Maybe is nothing (empty), node-addon-api will crash the
279
+ /// process.
280
+ void Check() const;
281
+
282
+ /// Return the value of type T contained in the Maybe. If this Maybe is
283
+ /// nothing (empty), node-addon-api will crash the process.
284
+ T Unwrap() const;
285
+
286
+ /// Return the value of type T contained in the Maybe, or using a default
287
+ /// value if this Maybe is nothing (empty).
288
+ T UnwrapOr(const T& default_value) const;
289
+
290
+ /// Converts this Maybe to a value of type T in the out. If this Maybe is
291
+ /// nothing (empty), `false` is returned and `out` is left untouched.
292
+ bool UnwrapTo(T* out) const;
293
+
294
+ bool operator==(const Maybe& other) const;
295
+ bool operator!=(const Maybe& other) const;
296
+
297
+ private:
298
+ Maybe();
299
+ explicit Maybe(const T& t);
300
+
301
+ bool _has_value;
302
+ T _value;
303
+
304
+ template <class U>
305
+ friend Maybe<U> Nothing();
306
+ template <class U>
307
+ friend Maybe<U> Just(const U& u);
308
+ };
309
+
310
+ template <class T>
311
+ inline Maybe<T> Nothing();
312
+
313
+ template <class T>
314
+ inline Maybe<T> Just(const T& t);
315
+
316
+ #if defined(NODE_ADDON_API_ENABLE_MAYBE)
317
+ template <typename T>
318
+ using MaybeOrValue = Maybe<T>;
319
+ #else
320
+ template <typename T>
321
+ using MaybeOrValue = T;
322
+ #endif
323
+
324
+ #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
325
+ using node_addon_api_basic_env = node_api_nogc_env;
326
+ using node_addon_api_basic_finalize = node_api_nogc_finalize;
327
+ #else
328
+ using node_addon_api_basic_env = napi_env;
329
+ using node_addon_api_basic_finalize = napi_finalize;
330
+ #endif
331
+
332
+ /// Environment for Node-API values and operations.
333
+ ///
334
+ /// All Node-API values and operations must be associated with an environment.
335
+ /// An environment instance is always provided to callback functions; that
336
+ /// environment must then be used for any creation of Node-API values or other
337
+ /// Node-API operations within the callback. (Many methods infer the
338
+ /// environment from the `this` instance that the method is called on.)
339
+ ///
340
+ /// Multiple environments may co-exist in a single process or a thread.
341
+ ///
342
+ /// In the V8 JavaScript engine, a Node-API environment approximately
343
+ /// corresponds to an Isolate.
344
+ class BasicEnv {
345
+ private:
346
+ node_addon_api_basic_env _env;
347
+ #if NAPI_VERSION > 5
348
+ template <typename T>
349
+ static void DefaultFini(Env, T* data);
350
+ template <typename DataType, typename HintType>
351
+ static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
352
+ #endif // NAPI_VERSION > 5
353
+ public:
354
+ BasicEnv(node_addon_api_basic_env env);
355
+
356
+ operator node_addon_api_basic_env() const;
357
+
358
+ // Without these operator overloads, the error:
359
+ //
360
+ // Use of overloaded operator '==' is ambiguous (with operand types
361
+ // 'Napi::Env' and 'Napi::Env')
362
+ //
363
+ // ... occurs when comparing foo.Env() == bar.Env() or foo.Env() == nullptr
364
+ bool operator==(const BasicEnv& other) const {
365
+ return _env == other._env;
366
+ }
367
+ bool operator==(std::nullptr_t /*other*/) const {
368
+ return _env == nullptr;
369
+ }
370
+
371
+ #if NAPI_VERSION > 2
372
+ template <typename Hook, typename Arg = void>
373
+ class CleanupHook;
374
+
375
+ template <typename Hook>
376
+ CleanupHook<Hook> AddCleanupHook(Hook hook);
377
+
378
+ template <typename Hook, typename Arg>
379
+ CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);
380
+ #endif // NAPI_VERSION > 2
381
+
382
+ #if NAPI_VERSION > 5
383
+ template <typename T>
384
+ T* GetInstanceData() const;
385
+
386
+ template <typename T>
387
+ using Finalizer = void (*)(Env, T*);
388
+ template <typename T, Finalizer<T> fini = BasicEnv::DefaultFini<T>>
389
+ void SetInstanceData(T* data) const;
390
+
391
+ template <typename DataType, typename HintType>
392
+ using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
393
+ template <typename DataType,
394
+ typename HintType,
395
+ FinalizerWithHint<DataType, HintType> fini =
396
+ BasicEnv::DefaultFiniWithHint<DataType, HintType>>
397
+ void SetInstanceData(DataType* data, HintType* hint) const;
398
+ #endif // NAPI_VERSION > 5
399
+
400
+ #if NAPI_VERSION > 2
401
+ template <typename Hook, typename Arg>
402
+ class CleanupHook {
403
+ public:
404
+ CleanupHook();
405
+ CleanupHook(BasicEnv env, Hook hook, Arg* arg);
406
+ CleanupHook(BasicEnv env, Hook hook);
407
+ bool Remove(BasicEnv env);
408
+ bool IsEmpty() const;
409
+
410
+ private:
411
+ static inline void Wrapper(void* data) NAPI_NOEXCEPT;
412
+ static inline void WrapperWithArg(void* data) NAPI_NOEXCEPT;
413
+
414
+ void (*wrapper)(void* arg);
415
+ struct CleanupData {
416
+ Hook hook;
417
+ Arg* arg;
418
+ } * data;
419
+ };
420
+ #endif // NAPI_VERSION > 2
421
+
422
+ #if NAPI_VERSION > 8
423
+ const char* GetModuleFileName() const;
424
+ #endif // NAPI_VERSION > 8
425
+
426
+ #ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
427
+ template <typename FinalizerType>
428
+ inline void PostFinalizer(FinalizerType finalizeCallback) const;
429
+
430
+ template <typename FinalizerType, typename T>
431
+ inline void PostFinalizer(FinalizerType finalizeCallback, T* data) const;
432
+
433
+ template <typename FinalizerType, typename T, typename Hint>
434
+ inline void PostFinalizer(FinalizerType finalizeCallback,
435
+ T* data,
436
+ Hint* finalizeHint) const;
437
+ #endif // NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
438
+
439
+ friend class Env;
440
+ };
441
+
442
+ class Env : public BasicEnv {
443
+ public:
444
+ Env(napi_env env);
445
+
446
+ operator napi_env() const;
447
+
448
+ Object Global() const;
449
+ Value Undefined() const;
450
+ Value Null() const;
451
+
452
+ bool IsExceptionPending() const;
453
+ Error GetAndClearPendingException() const;
454
+
455
+ MaybeOrValue<Value> RunScript(const char* utf8script) const;
456
+ MaybeOrValue<Value> RunScript(const std::string& utf8script) const;
457
+ MaybeOrValue<Value> RunScript(String script) const;
458
+ };
459
+
460
+ /// A JavaScript value of unknown type.
461
+ ///
462
+ /// For type-specific operations, convert to one of the Value subclasses using a
463
+ /// `To*` or `As()` method. The `To*` methods do type coercion; the `As()`
464
+ /// method does not.
465
+ ///
466
+ /// Napi::Value value = ...
467
+ /// if (!value.IsString()) throw Napi::TypeError::New(env, "Invalid
468
+ /// arg..."); Napi::String str = value.As<Napi::String>(); // Cast to a
469
+ /// string value
470
+ ///
471
+ /// Napi::Value anotherValue = ...
472
+ /// bool isTruthy = anotherValue.ToBoolean(); // Coerce to a boolean value
473
+ class Value {
474
+ public:
475
+ Value(); ///< Creates a new _empty_ Value instance.
476
+ Value(napi_env env,
477
+ napi_value value); ///< Wraps a Node-API value primitive.
478
+
479
+ /// Creates a JS value from a C++ primitive.
480
+ ///
481
+ /// `value` may be any of:
482
+ /// - bool
483
+ /// - Any integer type
484
+ /// - Any floating point type
485
+ /// - const char* (encoded using UTF-8, null-terminated)
486
+ /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
487
+ /// - std::string (encoded using UTF-8)
488
+ /// - std::u16string
489
+ /// - napi::Value
490
+ /// - napi_value
491
+ template <typename T>
492
+ static Value From(napi_env env, const T& value);
493
+
494
+ static void CheckCast(napi_env env, napi_value value);
495
+
496
+ /// Converts to a Node-API value primitive.
497
+ ///
498
+ /// If the instance is _empty_, this returns `nullptr`.
499
+ operator napi_value() const;
500
+
501
+ /// Tests if this value strictly equals another value.
502
+ bool operator==(const Value& other) const;
503
+
504
+ /// Tests if this value does not strictly equal another value.
505
+ bool operator!=(const Value& other) const;
506
+
507
+ /// Tests if this value strictly equals another value.
508
+ bool StrictEquals(const Value& other) const;
509
+
510
+ /// Gets the environment the value is associated with.
511
+ Napi::Env Env() const;
512
+
513
+ /// Checks if the value is empty (uninitialized).
514
+ ///
515
+ /// An empty value is invalid, and most attempts to perform an operation on an
516
+ /// empty value will result in an exception. Note an empty value is distinct
517
+ /// from JavaScript `null` or `undefined`, which are valid values.
518
+ ///
519
+ /// When C++ exceptions are disabled at compile time, a method with a `Value`
520
+ /// return type may return an empty value to indicate a pending exception. So
521
+ /// when not using C++ exceptions, callers should check whether the value is
522
+ /// empty before attempting to use it.
523
+ bool IsEmpty() const;
524
+
525
+ napi_valuetype Type() const; ///< Gets the type of the value.
526
+
527
+ bool IsUndefined()
528
+ const; ///< Tests if a value is an undefined JavaScript value.
529
+ bool IsNull() const; ///< Tests if a value is a null JavaScript value.
530
+ bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
531
+ bool IsNumber() const; ///< Tests if a value is a JavaScript number.
532
+ #if NAPI_VERSION > 5
533
+ bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
534
+ #endif // NAPI_VERSION > 5
535
+ #if (NAPI_VERSION > 4)
536
+ bool IsDate() const; ///< Tests if a value is a JavaScript date.
537
+ #endif
538
+ bool IsString() const; ///< Tests if a value is a JavaScript string.
539
+ bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
540
+ bool IsArray() const; ///< Tests if a value is a JavaScript array.
541
+ bool IsArrayBuffer()
542
+ const; ///< Tests if a value is a JavaScript array buffer.
543
+ bool IsTypedArray() const; ///< Tests if a value is a JavaScript typed array.
544
+ bool IsObject() const; ///< Tests if a value is a JavaScript object.
545
+ bool IsFunction() const; ///< Tests if a value is a JavaScript function.
546
+ bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
547
+ bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
548
+ bool IsBuffer() const; ///< Tests if a value is a Node buffer.
549
+ bool IsExternal() const; ///< Tests if a value is a pointer to external data.
550
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
551
+ bool IsSharedArrayBuffer() const;
552
+ #endif
553
+
554
+ /// Casts to another type of `Napi::Value`, when the actual type is known or
555
+ /// assumed.
556
+ ///
557
+ /// This conversion does NOT coerce the type. Calling any methods
558
+ /// inappropriate for the actual value type will throw `Napi::Error`.
559
+ ///
560
+ /// If `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` is defined, this method
561
+ /// asserts that the actual type is the expected type.
562
+ template <typename T>
563
+ T As() const;
564
+
565
+ // Unsafe Value::As(), should be avoided.
566
+ template <typename T>
567
+ T UnsafeAs() const;
568
+
569
+ MaybeOrValue<Boolean> ToBoolean()
570
+ const; ///< Coerces a value to a JavaScript boolean.
571
+ MaybeOrValue<Number> ToNumber()
572
+ const; ///< Coerces a value to a JavaScript number.
573
+ MaybeOrValue<String> ToString()
574
+ const; ///< Coerces a value to a JavaScript string.
575
+ MaybeOrValue<Object> ToObject()
576
+ const; ///< Coerces a value to a JavaScript object.
577
+
578
+ protected:
579
+ /// !cond INTERNAL
580
+ napi_env _env;
581
+ napi_value _value;
582
+ /// !endcond
583
+ };
584
+
585
+ /// A JavaScript boolean value.
586
+ class Boolean : public Value {
587
+ public:
588
+ static Boolean New(napi_env env, ///< Node-API environment
589
+ bool value ///< Boolean value
590
+ );
591
+
592
+ static void CheckCast(napi_env env, napi_value value);
593
+
594
+ Boolean(); ///< Creates a new _empty_ Boolean instance.
595
+ Boolean(napi_env env,
596
+ napi_value value); ///< Wraps a Node-API value primitive.
597
+
598
+ operator bool() const; ///< Converts a Boolean value to a boolean primitive.
599
+ bool Value() const; ///< Converts a Boolean value to a boolean primitive.
600
+ };
601
+
602
+ /// A JavaScript number value.
603
+ class Number : public Value {
604
+ public:
605
+ static Number New(napi_env env, ///< Node-API environment
606
+ double value ///< Number value
607
+ );
608
+
609
+ static void CheckCast(napi_env env, napi_value value);
610
+
611
+ Number(); ///< Creates a new _empty_ Number instance.
612
+ Number(napi_env env,
613
+ napi_value value); ///< Wraps a Node-API value primitive.
614
+
615
+ operator int32_t()
616
+ const; ///< Converts a Number value to a 32-bit signed integer value.
617
+ operator uint32_t()
618
+ const; ///< Converts a Number value to a 32-bit unsigned integer value.
619
+ operator int64_t()
620
+ const; ///< Converts a Number value to a 64-bit signed integer value.
621
+ operator float()
622
+ const; ///< Converts a Number value to a 32-bit floating-point value.
623
+ operator double()
624
+ const; ///< Converts a Number value to a 64-bit floating-point value.
625
+
626
+ int32_t Int32Value()
627
+ const; ///< Converts a Number value to a 32-bit signed integer value.
628
+ uint32_t Uint32Value()
629
+ const; ///< Converts a Number value to a 32-bit unsigned integer value.
630
+ int64_t Int64Value()
631
+ const; ///< Converts a Number value to a 64-bit signed integer value.
632
+ float FloatValue()
633
+ const; ///< Converts a Number value to a 32-bit floating-point value.
634
+ double DoubleValue()
635
+ const; ///< Converts a Number value to a 64-bit floating-point value.
636
+ };
637
+
638
+ #if NAPI_VERSION > 5
639
+ /// A JavaScript bigint value.
640
+ class BigInt : public Value {
641
+ public:
642
+ static BigInt New(napi_env env, ///< Node-API environment
643
+ int64_t value ///< Number value
644
+ );
645
+ static BigInt New(napi_env env, ///< Node-API environment
646
+ uint64_t value ///< Number value
647
+ );
648
+
649
+ /// Creates a new BigInt object using a specified sign bit and a
650
+ /// specified list of digits/words.
651
+ /// The resulting number is calculated as:
652
+ /// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
653
+ static BigInt New(napi_env env, ///< Node-API environment
654
+ int sign_bit, ///< Sign bit. 1 if negative.
655
+ size_t word_count, ///< Number of words in array
656
+ const uint64_t* words ///< Array of words
657
+ );
658
+
659
+ static void CheckCast(napi_env env, napi_value value);
660
+
661
+ BigInt(); ///< Creates a new _empty_ BigInt instance.
662
+ BigInt(napi_env env,
663
+ napi_value value); ///< Wraps a Node-API value primitive.
664
+
665
+ int64_t Int64Value(bool* lossless)
666
+ const; ///< Converts a BigInt value to a 64-bit signed integer value.
667
+ uint64_t Uint64Value(bool* lossless)
668
+ const; ///< Converts a BigInt value to a 64-bit unsigned integer value.
669
+
670
+ size_t WordCount() const; ///< The number of 64-bit words needed to store
671
+ ///< the result of ToWords().
672
+
673
+ /// Writes the contents of this BigInt to a specified memory location.
674
+ /// `sign_bit` must be provided and will be set to 1 if this BigInt is
675
+ /// negative.
676
+ /// `*word_count` has to be initialized to the length of the `words` array.
677
+ /// Upon return, it will be set to the actual number of words that would
678
+ /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
679
+ void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
680
+ };
681
+ #endif // NAPI_VERSION > 5
682
+
683
+ #if (NAPI_VERSION > 4)
684
+ /// A JavaScript date value.
685
+ class Date : public Value {
686
+ public:
687
+ /// Creates a new Date value from a double primitive.
688
+ static Date New(napi_env env, ///< Node-API environment
689
+ double value ///< Number value
690
+ );
691
+
692
+ /// Creates a new Date value from a std::chrono::system_clock::time_point.
693
+ static Date New(
694
+ napi_env env, ///< Node-API environment
695
+ std::chrono::system_clock::time_point time_point ///< Time point value
696
+ );
697
+
698
+ static void CheckCast(napi_env env, napi_value value);
699
+
700
+ Date(); ///< Creates a new _empty_ Date instance.
701
+ Date(napi_env env, napi_value value); ///< Wraps a Node-API value primitive.
702
+ operator double() const; ///< Converts a Date value to double primitive
703
+
704
+ double ValueOf() const; ///< Converts a Date value to a double primitive.
705
+ };
706
+ #endif
707
+
708
+ /// A JavaScript string or symbol value (that can be used as a property name).
709
+ class Name : public Value {
710
+ public:
711
+ static void CheckCast(napi_env env, napi_value value);
712
+
713
+ Name(); ///< Creates a new _empty_ Name instance.
714
+ Name(napi_env env,
715
+ napi_value value); ///< Wraps a Node-API value primitive.
716
+ };
717
+
718
+ /// A JavaScript string value.
719
+ class String : public Name {
720
+ public:
721
+ /// Creates a new String value from a UTF-8 encoded C++ string.
722
+ static String New(napi_env env, ///< Node-API environment
723
+ const std::string& value ///< UTF-8 encoded C++ string
724
+ );
725
+
726
+ /// Creates a new String value from a UTF-16 encoded C++ string.
727
+ static String New(napi_env env, ///< Node-API environment
728
+ const std::u16string& value ///< UTF-16 encoded C++ string
729
+ );
730
+
731
+ /// Creates a new String value from a UTF-8 encoded C++ string view.
732
+ static String New(napi_env env, ///< Node-API environment
733
+ std::string_view value ///< UTF-8 encoded C++ string view
734
+ );
735
+
736
+ /// Creates a new String value from a UTF-8 encoded C string.
737
+ static String New(
738
+ napi_env env, ///< Node-API environment
739
+ const char* value ///< UTF-8 encoded null-terminated C string
740
+ );
741
+
742
+ /// Creates a new String value from a UTF-16 encoded C string.
743
+ static String New(
744
+ napi_env env, ///< Node-API environment
745
+ const char16_t* value ///< UTF-16 encoded null-terminated C string
746
+ );
747
+
748
+ /// Creates a new String value from a UTF-8 encoded C string with specified
749
+ /// length.
750
+ static String New(napi_env env, ///< Node-API environment
751
+ const char* value, ///< UTF-8 encoded C string (not
752
+ ///< necessarily null-terminated)
753
+ size_t length ///< length of the string in bytes
754
+ );
755
+
756
+ /// Creates a new String value from a UTF-16 encoded C string with specified
757
+ /// length.
758
+ static String New(
759
+ napi_env env, ///< Node-API environment
760
+ const char16_t* value, ///< UTF-16 encoded C string (not necessarily
761
+ ///< null-terminated)
762
+ size_t length ///< Length of the string in 2-byte code units
763
+ );
764
+
765
+ /// Creates a new String based on the original object's type.
766
+ ///
767
+ /// `value` may be any of:
768
+ /// - const char* (encoded using UTF-8, null-terminated)
769
+ /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
770
+ /// - std::string (encoded using UTF-8)
771
+ /// - std::u16string
772
+ template <typename T>
773
+ static String From(napi_env env, const T& value);
774
+
775
+ static void CheckCast(napi_env env, napi_value value);
776
+
777
+ String(); ///< Creates a new _empty_ String instance.
778
+ String(napi_env env,
779
+ napi_value value); ///< Wraps a Node-API value primitive.
780
+
781
+ operator std::string()
782
+ const; ///< Converts a String value to a UTF-8 encoded C++ string.
783
+ operator std::u16string()
784
+ const; ///< Converts a String value to a UTF-16 encoded C++ string.
785
+ std::string Utf8Value()
786
+ const; ///< Converts a String value to a UTF-8 encoded C++ string.
787
+ std::u16string Utf16Value()
788
+ const; ///< Converts a String value to a UTF-16 encoded C++ string.
789
+ };
790
+
791
+ namespace details {
792
+
793
+ // This overload set must mirror the non-template Symbol::For overloads.
794
+ struct string_convertible_probe {
795
+ static void select(const std::string&);
796
+ static void select(std::string_view);
797
+ static void select(const char*);
798
+ static void select(String);
799
+ static void select(napi_value);
800
+ };
801
+
802
+ template <typename T, typename = void>
803
+ struct has_unambiguous_string_convertible_overload : std::false_type {};
804
+
805
+ template <typename T>
806
+ struct has_unambiguous_string_convertible_overload<
807
+ T,
808
+ std::void_t<decltype(string_convertible_probe::select(std::declval<T>()))>>
809
+ : std::true_type {};
810
+
811
+ // Enable the template overload only for string-like arguments that have no
812
+ // unique best match among the non-template Symbol::For overloads.
813
+ //
814
+ // Exclude nullptr because it matches the pointer overloads equally well and
815
+ // cannot safely initialize a std::string_view.
816
+ template <typename T>
817
+ using enable_if_ambiguous_string_convertible_t =
818
+ std::enable_if_t<!std::is_null_pointer_v<std::decay_t<T>> &&
819
+ std::is_convertible_v<T, const std::string&> &&
820
+ std::is_convertible_v<T, std::string_view> &&
821
+ !has_unambiguous_string_convertible_overload<T>::value,
822
+ int>;
823
+
824
+ } // namespace details
825
+
826
+ /// A JavaScript symbol value.
827
+ class Symbol : public Name {
828
+ public:
829
+ /// Creates a new Symbol value with an optional description.
830
+ static Symbol New(
831
+ napi_env env, ///< Node-API environment
832
+ const char* description =
833
+ nullptr ///< Optional UTF-8 encoded null-terminated C string
834
+ /// describing the symbol
835
+ );
836
+
837
+ /// Creates a new Symbol value with a description.
838
+ static Symbol New(
839
+ napi_env env, ///< Node-API environment
840
+ const std::string&
841
+ description ///< UTF-8 encoded C++ string describing the symbol
842
+ );
843
+
844
+ /// Creates a new Symbol value with a description.
845
+ static Symbol New(
846
+ napi_env env, ///< Node-API environment
847
+ std::string_view
848
+ description ///< UTF-8 encoded C++ string view describing the symbol
849
+ );
850
+
851
+ /// Creates a new Symbol value with a description.
852
+ static Symbol New(napi_env env, ///< Node-API environment
853
+ String description ///< String value describing the symbol
854
+ );
855
+
856
+ /// Creates a new Symbol value with a description.
857
+ static Symbol New(
858
+ napi_env env, ///< Node-API environment
859
+ napi_value description ///< String value describing the symbol
860
+ );
861
+
862
+ /// Get a public Symbol (e.g. Symbol.iterator).
863
+ static MaybeOrValue<Symbol> WellKnown(napi_env, const std::string& name);
864
+
865
+ // Create a symbol in the global registry, UTF-8 Encoded cpp string
866
+ static MaybeOrValue<Symbol> For(napi_env env, const std::string& description);
867
+
868
+ // Create a symbol in the global registry, UTF-8 encoded cpp string view
869
+ static MaybeOrValue<Symbol> For(napi_env env, std::string_view description);
870
+
871
+ // Resolve otherwise ambiguous string-like arguments through the
872
+ // std::string_view overload
873
+ template <typename T,
874
+ details::enable_if_ambiguous_string_convertible_t<T&&> = 0>
875
+ static MaybeOrValue<Symbol> For(napi_env env, T&& description);
876
+
877
+ // Create a symbol in the global registry, C style string (null terminated)
878
+ static MaybeOrValue<Symbol> For(napi_env env, const char* description);
879
+
880
+ // Create a symbol in the global registry, String value describing the symbol
881
+ static MaybeOrValue<Symbol> For(napi_env env, String description);
882
+
883
+ // Create a symbol in the global registry, napi_value describing the symbol
884
+ static MaybeOrValue<Symbol> For(napi_env env, napi_value description);
885
+
886
+ static void CheckCast(napi_env env, napi_value value);
887
+
888
+ Symbol(); ///< Creates a new _empty_ Symbol instance.
889
+ Symbol(napi_env env,
890
+ napi_value value); ///< Wraps a Node-API value primitive.
891
+ };
892
+
893
+ class TypeTaggable : public Value {
894
+ public:
895
+ #if NAPI_VERSION >= 8
896
+ void TypeTag(const napi_type_tag* type_tag) const;
897
+ bool CheckTypeTag(const napi_type_tag* type_tag) const;
898
+ #endif // NAPI_VERSION >= 8
899
+ protected:
900
+ TypeTaggable();
901
+ TypeTaggable(napi_env env, napi_value value);
902
+ };
903
+
904
+ /// A JavaScript object value.
905
+ class Object : public TypeTaggable {
906
+ public:
907
+ /// Enables property and element assignments using indexing syntax.
908
+ ///
909
+ /// This is a convenient helper to get and set object properties. As
910
+ /// getting and setting object properties may throw with JavaScript
911
+ /// exceptions, it is notable that these operations may fail.
912
+ /// When NODE_ADDON_API_ENABLE_MAYBE is defined, the process will abort
913
+ /// on JavaScript exceptions.
914
+ ///
915
+ /// Example:
916
+ ///
917
+ /// Napi::Value propertyValue = object1['A'];
918
+ /// object2['A'] = propertyValue;
919
+ /// Napi::Value elementValue = array[0];
920
+ /// array[1] = elementValue;
921
+ template <typename Key>
922
+ class PropertyLValue {
923
+ public:
924
+ /// Converts an L-value to a value.
925
+ operator Value() const;
926
+
927
+ /// Assigns a value to the property. The type of value can be
928
+ /// anything supported by `Object::Set`.
929
+ template <typename ValueType>
930
+ PropertyLValue& operator=(ValueType value);
931
+
932
+ /// Converts an L-value to a value. For convenience.
933
+ Value AsValue() const;
934
+
935
+ private:
936
+ PropertyLValue() = delete;
937
+ PropertyLValue(Object object, Key key);
938
+ napi_env _env;
939
+ napi_value _object;
940
+ Key _key;
941
+
942
+ friend class Napi::Object;
943
+ };
944
+
945
+ /// Creates a new Object value.
946
+ static Object New(napi_env env ///< Node-API environment
947
+ );
948
+
949
+ static void CheckCast(napi_env env, napi_value value);
950
+
951
+ Object(); ///< Creates a new _empty_ Object instance.
952
+ Object(napi_env env,
953
+ napi_value value); ///< Wraps a Node-API value primitive.
954
+
955
+ /// Gets or sets a named property.
956
+ PropertyLValue<std::string> operator[](
957
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
958
+ );
959
+
960
+ /// Gets or sets a named property.
961
+ PropertyLValue<std::string> operator[](
962
+ const std::string& utf8name ///< UTF-8 encoded property name
963
+ );
964
+
965
+ /// Gets or sets an indexed property or array element.
966
+ PropertyLValue<uint32_t> operator[](
967
+ uint32_t index /// Property / element index
968
+ );
969
+
970
+ /// Gets or sets an indexed property or array element.
971
+ PropertyLValue<Value> operator[](Value index /// Property / element index
972
+ ) const;
973
+
974
+ /// Gets a named property.
975
+ MaybeOrValue<Value> operator[](
976
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
977
+ ) const;
978
+
979
+ /// Gets a named property.
980
+ MaybeOrValue<Value> operator[](
981
+ const std::string& utf8name ///< UTF-8 encoded property name
982
+ ) const;
983
+
984
+ /// Gets an indexed property or array element.
985
+ MaybeOrValue<Value> operator[](uint32_t index ///< Property / element index
986
+ ) const;
987
+
988
+ /// Checks whether a property is present.
989
+ MaybeOrValue<bool> Has(napi_value key ///< Property key primitive
990
+ ) const;
991
+
992
+ /// Checks whether a property is present.
993
+ MaybeOrValue<bool> Has(Value key ///< Property key
994
+ ) const;
995
+
996
+ /// Checks whether a named property is present.
997
+ MaybeOrValue<bool> Has(
998
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
999
+ ) const;
1000
+
1001
+ /// Checks whether a named property is present.
1002
+ MaybeOrValue<bool> Has(
1003
+ const std::string& utf8name ///< UTF-8 encoded property name
1004
+ ) const;
1005
+
1006
+ /// Checks whether a own property is present.
1007
+ MaybeOrValue<bool> HasOwnProperty(napi_value key ///< Property key primitive
1008
+ ) const;
1009
+
1010
+ /// Checks whether a own property is present.
1011
+ MaybeOrValue<bool> HasOwnProperty(Value key ///< Property key
1012
+ ) const;
1013
+
1014
+ /// Checks whether a own property is present.
1015
+ MaybeOrValue<bool> HasOwnProperty(
1016
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
1017
+ ) const;
1018
+
1019
+ /// Checks whether a own property is present.
1020
+ MaybeOrValue<bool> HasOwnProperty(
1021
+ const std::string& utf8name ///< UTF-8 encoded property name
1022
+ ) const;
1023
+
1024
+ /// Gets a property.
1025
+ MaybeOrValue<Value> Get(napi_value key ///< Property key primitive
1026
+ ) const;
1027
+
1028
+ /// Gets a property.
1029
+ MaybeOrValue<Value> Get(Value key ///< Property key
1030
+ ) const;
1031
+
1032
+ /// Gets a named property.
1033
+ MaybeOrValue<Value> Get(
1034
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
1035
+ ) const;
1036
+
1037
+ /// Gets a named property.
1038
+ MaybeOrValue<Value> Get(
1039
+ const std::string& utf8name ///< UTF-8 encoded property name
1040
+ ) const;
1041
+
1042
+ /// Sets a property.
1043
+ template <typename ValueType>
1044
+ MaybeOrValue<bool> Set(napi_value key, ///< Property key primitive
1045
+ const ValueType& value ///< Property value primitive
1046
+ ) const;
1047
+
1048
+ /// Sets a property.
1049
+ template <typename ValueType>
1050
+ MaybeOrValue<bool> Set(Value key, ///< Property key
1051
+ const ValueType& value ///< Property value
1052
+ ) const;
1053
+
1054
+ /// Sets a named property.
1055
+ template <typename ValueType>
1056
+ MaybeOrValue<bool> Set(
1057
+ const char* utf8name, ///< UTF-8 encoded null-terminated property name
1058
+ const ValueType& value) const;
1059
+
1060
+ /// Sets a named property.
1061
+ template <typename ValueType>
1062
+ MaybeOrValue<bool> Set(
1063
+ const std::string& utf8name, ///< UTF-8 encoded property name
1064
+ const ValueType& value ///< Property value primitive
1065
+ ) const;
1066
+
1067
+ /// Delete property.
1068
+ MaybeOrValue<bool> Delete(napi_value key ///< Property key primitive
1069
+ ) const;
1070
+
1071
+ /// Delete property.
1072
+ MaybeOrValue<bool> Delete(Value key ///< Property key
1073
+ ) const;
1074
+
1075
+ /// Delete property.
1076
+ MaybeOrValue<bool> Delete(
1077
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
1078
+ ) const;
1079
+
1080
+ /// Delete property.
1081
+ MaybeOrValue<bool> Delete(
1082
+ const std::string& utf8name ///< UTF-8 encoded property name
1083
+ ) const;
1084
+
1085
+ /// Checks whether an indexed property is present.
1086
+ MaybeOrValue<bool> Has(uint32_t index ///< Property / element index
1087
+ ) const;
1088
+
1089
+ /// Gets an indexed property or array element.
1090
+ MaybeOrValue<Value> Get(uint32_t index ///< Property / element index
1091
+ ) const;
1092
+
1093
+ /// Sets an indexed property or array element.
1094
+ template <typename ValueType>
1095
+ MaybeOrValue<bool> Set(uint32_t index, ///< Property / element index
1096
+ const ValueType& value ///< Property value primitive
1097
+ ) const;
1098
+
1099
+ /// Deletes an indexed property or array element.
1100
+ MaybeOrValue<bool> Delete(uint32_t index ///< Property / element index
1101
+ ) const;
1102
+
1103
+ /// This operation can fail in case of Proxy.[[OwnPropertyKeys]] and
1104
+ /// Proxy.[[GetOwnProperty]] calling into JavaScript. See:
1105
+ /// -
1106
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
1107
+ /// -
1108
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p
1109
+ MaybeOrValue<Array> GetPropertyNames() const; ///< Get all property names
1110
+
1111
+ /// Defines a property on the object.
1112
+ ///
1113
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
1114
+ /// into JavaScript. See
1115
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
1116
+ MaybeOrValue<bool> DefineProperty(
1117
+ const PropertyDescriptor&
1118
+ property ///< Descriptor for the property to be defined
1119
+ ) const;
1120
+
1121
+ /// Defines properties on the object.
1122
+ ///
1123
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
1124
+ /// into JavaScript. See
1125
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
1126
+ MaybeOrValue<bool> DefineProperties(
1127
+ const std::initializer_list<PropertyDescriptor>& properties
1128
+ ///< List of descriptors for the properties to be defined
1129
+ ) const;
1130
+
1131
+ /// Defines properties on the object.
1132
+ ///
1133
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
1134
+ /// into JavaScript. See
1135
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
1136
+ MaybeOrValue<bool> DefineProperties(
1137
+ const std::vector<PropertyDescriptor>& properties
1138
+ ///< Vector of descriptors for the properties to be defined
1139
+ ) const;
1140
+
1141
+ /// Checks if an object is an instance created by a constructor function.
1142
+ ///
1143
+ /// This is equivalent to the JavaScript `instanceof` operator.
1144
+ ///
1145
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
1146
+ /// JavaScript.
1147
+ /// See
1148
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
1149
+ MaybeOrValue<bool> InstanceOf(
1150
+ const Function& constructor ///< Constructor function
1151
+ ) const;
1152
+
1153
+ template <typename Finalizer, typename T>
1154
+ inline void AddFinalizer(Finalizer finalizeCallback, T* data) const;
1155
+
1156
+ template <typename Finalizer, typename T, typename Hint>
1157
+ inline void AddFinalizer(Finalizer finalizeCallback,
1158
+ T* data,
1159
+ Hint* finalizeHint) const;
1160
+
1161
+ #ifdef NODE_ADDON_API_CPP_EXCEPTIONS
1162
+ class const_iterator;
1163
+
1164
+ inline const_iterator begin() const;
1165
+
1166
+ inline const_iterator end() const;
1167
+
1168
+ class iterator;
1169
+
1170
+ inline iterator begin();
1171
+
1172
+ inline iterator end();
1173
+ #endif // NODE_ADDON_API_CPP_EXCEPTIONS
1174
+
1175
+ #if NAPI_VERSION >= 8
1176
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
1177
+ /// JavaScript.
1178
+ /// See
1179
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
1180
+ MaybeOrValue<bool> Freeze() const;
1181
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
1182
+ /// JavaScript.
1183
+ /// See
1184
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
1185
+ MaybeOrValue<bool> Seal() const;
1186
+ #endif // NAPI_VERSION >= 8
1187
+
1188
+ MaybeOrValue<Object> GetPrototype() const;
1189
+
1190
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SET_PROTOTYPE
1191
+ MaybeOrValue<bool> SetPrototype(const Object& value) const;
1192
+ #endif
1193
+ };
1194
+
1195
+ template <typename T>
1196
+ class External : public TypeTaggable {
1197
+ public:
1198
+ static External New(napi_env env, T* data);
1199
+
1200
+ // Finalizer must implement `void operator()(Env env, T* data)`.
1201
+ template <typename Finalizer>
1202
+ static External New(napi_env env, T* data, Finalizer finalizeCallback);
1203
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
1204
+ template <typename Finalizer, typename Hint>
1205
+ static External New(napi_env env,
1206
+ T* data,
1207
+ Finalizer finalizeCallback,
1208
+ Hint* finalizeHint);
1209
+
1210
+ static void CheckCast(napi_env env, napi_value value);
1211
+
1212
+ External();
1213
+ External(napi_env env, napi_value value);
1214
+
1215
+ T* Data() const;
1216
+ };
1217
+
1218
+ class Array : public Object {
1219
+ public:
1220
+ static Array New(napi_env env);
1221
+ static Array New(napi_env env, size_t length);
1222
+
1223
+ static void CheckCast(napi_env env, napi_value value);
1224
+
1225
+ Array();
1226
+ Array(napi_env env, napi_value value);
1227
+
1228
+ uint32_t Length() const;
1229
+ };
1230
+
1231
+ #ifdef NODE_ADDON_API_CPP_EXCEPTIONS
1232
+ class Object::const_iterator {
1233
+ private:
1234
+ enum class Type { BEGIN, END };
1235
+
1236
+ inline const_iterator(const Object* object, const Type type);
1237
+
1238
+ public:
1239
+ inline const_iterator& operator++();
1240
+
1241
+ inline bool operator==(const const_iterator& other) const;
1242
+
1243
+ inline bool operator!=(const const_iterator& other) const;
1244
+
1245
+ inline const std::pair<Value, Object::PropertyLValue<Value>> operator*()
1246
+ const;
1247
+
1248
+ private:
1249
+ const Napi::Object* _object;
1250
+ Array _keys;
1251
+ uint32_t _index;
1252
+
1253
+ friend class Object;
1254
+ };
1255
+
1256
+ class Object::iterator {
1257
+ private:
1258
+ enum class Type { BEGIN, END };
1259
+
1260
+ inline iterator(Object* object, const Type type);
1261
+
1262
+ public:
1263
+ inline iterator& operator++();
1264
+
1265
+ inline bool operator==(const iterator& other) const;
1266
+
1267
+ inline bool operator!=(const iterator& other) const;
1268
+
1269
+ inline std::pair<Value, Object::PropertyLValue<Value>> operator*();
1270
+
1271
+ private:
1272
+ Napi::Object* _object;
1273
+ Array _keys;
1274
+ uint32_t _index;
1275
+
1276
+ friend class Object;
1277
+ };
1278
+ #endif // NODE_ADDON_API_CPP_EXCEPTIONS
1279
+
1280
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
1281
+ class SharedArrayBuffer : public Object {
1282
+ public:
1283
+ SharedArrayBuffer();
1284
+ SharedArrayBuffer(napi_env env, napi_value value);
1285
+
1286
+ static SharedArrayBuffer New(napi_env env, size_t byteLength);
1287
+
1288
+ static void CheckCast(napi_env env, napi_value value);
1289
+
1290
+ void* Data();
1291
+ size_t ByteLength();
1292
+ };
1293
+ #endif
1294
+
1295
+ /// A JavaScript array buffer value.
1296
+ class ArrayBuffer : public Object {
1297
+ public:
1298
+ /// Creates a new ArrayBuffer instance over a new automatically-allocated
1299
+ /// buffer.
1300
+ static ArrayBuffer New(
1301
+ napi_env env, ///< Node-API environment
1302
+ size_t byteLength ///< Length of the buffer to be allocated, in bytes
1303
+ );
1304
+
1305
+ #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1306
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1307
+ /// specified byte length.
1308
+ static ArrayBuffer New(
1309
+ napi_env env, ///< Node-API environment
1310
+ void* externalData, ///< Pointer to the external buffer to be used by
1311
+ ///< the array
1312
+ size_t byteLength ///< Length of the external buffer to be used by the
1313
+ ///< array, in bytes
1314
+ );
1315
+
1316
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1317
+ /// specified byte length.
1318
+ template <typename Finalizer>
1319
+ static ArrayBuffer New(
1320
+ napi_env env, ///< Node-API environment
1321
+ void* externalData, ///< Pointer to the external buffer to be used by
1322
+ ///< the array
1323
+ size_t byteLength, ///< Length of the external buffer to be used by the
1324
+ ///< array,
1325
+ /// in bytes
1326
+ Finalizer finalizeCallback ///< Function to be called when the array
1327
+ ///< buffer is destroyed;
1328
+ /// must implement `void operator()(Env env,
1329
+ /// void* externalData)`
1330
+ );
1331
+
1332
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1333
+ /// specified byte length.
1334
+ template <typename Finalizer, typename Hint>
1335
+ static ArrayBuffer New(
1336
+ napi_env env, ///< Node-API environment
1337
+ void* externalData, ///< Pointer to the external buffer to be used by
1338
+ ///< the array
1339
+ size_t byteLength, ///< Length of the external buffer to be used by the
1340
+ ///< array,
1341
+ /// in bytes
1342
+ Finalizer finalizeCallback, ///< Function to be called when the array
1343
+ ///< buffer is destroyed;
1344
+ /// must implement `void operator()(Env
1345
+ /// env, void* externalData, Hint* hint)`
1346
+ Hint* finalizeHint ///< Hint (second parameter) to be passed to the
1347
+ ///< finalize callback
1348
+ );
1349
+ #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1350
+
1351
+ static void CheckCast(napi_env env, napi_value value);
1352
+
1353
+ ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
1354
+ ArrayBuffer(napi_env env,
1355
+ napi_value value); ///< Wraps a Node-API value primitive.
1356
+
1357
+ void* Data(); ///< Gets a pointer to the data buffer.
1358
+ size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
1359
+
1360
+ #if NAPI_VERSION >= 7
1361
+ bool IsDetached() const;
1362
+ void Detach();
1363
+ #endif // NAPI_VERSION >= 7
1364
+ };
1365
+
1366
+ /// A JavaScript typed-array value with unknown array type.
1367
+ ///
1368
+ /// For type-specific operations, cast to a `TypedArrayOf<T>` instance using the
1369
+ /// `As()` method:
1370
+ ///
1371
+ /// Napi::TypedArray array = ...
1372
+ /// if (t.TypedArrayType() == napi_int32_array) {
1373
+ /// Napi::Int32Array int32Array = t.As<Napi::Int32Array>();
1374
+ /// }
1375
+ class TypedArray : public Object {
1376
+ public:
1377
+ static void CheckCast(napi_env env, napi_value value);
1378
+
1379
+ TypedArray(); ///< Creates a new _empty_ TypedArray instance.
1380
+ TypedArray(napi_env env,
1381
+ napi_value value); ///< Wraps a Node-API value primitive.
1382
+
1383
+ napi_typedarray_type TypedArrayType()
1384
+ const; ///< Gets the type of this typed-array.
1385
+
1386
+ // Gets the backing `ArrayBuffer`.
1387
+ //
1388
+ // If this `TypedArray` is not backed by an `ArrayBuffer`, this method will
1389
+ // terminate the process with a fatal error when using
1390
+ // `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` or exhibit undefined behavior
1391
+ // otherwise. Use `Buffer()` instead to get the backing buffer without
1392
+ // assuming its type.
1393
+ Napi::ArrayBuffer ArrayBuffer() const;
1394
+
1395
+ // Gets the backing buffer (an `ArrayBuffer` or `SharedArrayBuffer`).
1396
+ //
1397
+ // Use `IsArrayBuffer()` or `IsSharedArrayBuffer()` to check the type of the
1398
+ // backing buffer prior to casting with `As<T>()`.
1399
+ Napi::Value Buffer() const;
1400
+
1401
+ uint8_t ElementSize()
1402
+ const; ///< Gets the size in bytes of one element in the array.
1403
+ size_t ElementLength() const; ///< Gets the number of elements in the array.
1404
+ size_t ByteOffset()
1405
+ const; ///< Gets the offset into the buffer where the array starts.
1406
+ size_t ByteLength() const; ///< Gets the length of the array in bytes.
1407
+
1408
+ protected:
1409
+ /// !cond INTERNAL
1410
+ napi_typedarray_type _type;
1411
+ size_t _length;
1412
+
1413
+ TypedArray(napi_env env,
1414
+ napi_value value,
1415
+ napi_typedarray_type type,
1416
+ size_t length);
1417
+
1418
+ template <typename T>
1419
+ static
1420
+ #if defined(NAPI_HAS_CONSTEXPR)
1421
+ constexpr
1422
+ #endif
1423
+ napi_typedarray_type
1424
+ TypedArrayTypeForPrimitiveType() {
1425
+ return std::is_same<T, int8_t>::value ? napi_int8_array
1426
+ : std::is_same<T, uint8_t>::value ? napi_uint8_array
1427
+ : std::is_same<T, int16_t>::value ? napi_int16_array
1428
+ : std::is_same<T, uint16_t>::value ? napi_uint16_array
1429
+ : std::is_same<T, int32_t>::value ? napi_int32_array
1430
+ : std::is_same<T, uint32_t>::value ? napi_uint32_array
1431
+ : std::is_same<T, float>::value ? napi_float32_array
1432
+ : std::is_same<T, double>::value ? napi_float64_array
1433
+ #if NAPI_VERSION > 5
1434
+ : std::is_same<T, int64_t>::value ? napi_bigint64_array
1435
+ : std::is_same<T, uint64_t>::value ? napi_biguint64_array
1436
+ #endif // NAPI_VERSION > 5
1437
+ : napi_int8_array;
1438
+ }
1439
+ /// !endcond
1440
+ };
1441
+
1442
+ /// A JavaScript typed-array value with known array type.
1443
+ ///
1444
+ /// Note while it is possible to create and access Uint8 "clamped" arrays using
1445
+ /// this class, the _clamping_ behavior is only applied in JavaScript.
1446
+ template <typename T>
1447
+ class TypedArrayOf : public TypedArray {
1448
+ public:
1449
+ /// Creates a new TypedArray instance over a new automatically-allocated array
1450
+ /// buffer.
1451
+ ///
1452
+ /// The array type parameter can normally be omitted (because it is inferred
1453
+ /// from the template parameter T), except when creating a "clamped" array:
1454
+ ///
1455
+ /// Uint8Array::New(env, length, napi_uint8_clamped_array)
1456
+ static TypedArrayOf New(
1457
+ napi_env env, ///< Node-API environment
1458
+ size_t elementLength, ///< Length of the created array, as a number of
1459
+ ///< elements
1460
+ #if defined(NAPI_HAS_CONSTEXPR)
1461
+ napi_typedarray_type type =
1462
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
1463
+ #else
1464
+ napi_typedarray_type type
1465
+ #endif
1466
+ ///< Type of array, if different from the default array type for the
1467
+ ///< template parameter T.
1468
+ );
1469
+
1470
+ /// Creates a new TypedArray instance over a provided array buffer.
1471
+ ///
1472
+ /// The array type parameter can normally be omitted (because it is inferred
1473
+ /// from the template parameter T), except when creating a "clamped" array:
1474
+ ///
1475
+ /// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
1476
+ static TypedArrayOf New(
1477
+ napi_env env, ///< Node-API environment
1478
+ size_t elementLength, ///< Length of the created array, as a number of
1479
+ ///< elements
1480
+ Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
1481
+ size_t bufferOffset, ///< Offset into the array buffer where the
1482
+ ///< typed-array starts
1483
+ #if defined(NAPI_HAS_CONSTEXPR)
1484
+ napi_typedarray_type type =
1485
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
1486
+ #else
1487
+ napi_typedarray_type type
1488
+ #endif
1489
+ ///< Type of array, if different from the default array type for the
1490
+ ///< template parameter T.
1491
+ );
1492
+
1493
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
1494
+ /// Creates a new TypedArray instance over a provided SharedArrayBuffer.
1495
+ ///
1496
+ /// The array type parameter can normally be omitted (because it is inferred
1497
+ /// from the template parameter T), except when creating a "clamped" array:
1498
+ ///
1499
+ /// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
1500
+ static TypedArrayOf New(
1501
+ napi_env env, ///< Node-API environment
1502
+ size_t elementLength, ///< Length of the created array, as a number of
1503
+ ///< elements
1504
+ Napi::SharedArrayBuffer
1505
+ arrayBuffer, ///< Backing shared array buffer instance to use
1506
+ size_t bufferOffset, ///< Offset into the array buffer where the
1507
+ ///< typed-array starts
1508
+ #if defined(NAPI_HAS_CONSTEXPR)
1509
+ napi_typedarray_type type =
1510
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
1511
+ #else
1512
+ napi_typedarray_type type
1513
+ #endif
1514
+ ///< Type of array, if different from the default array type for the
1515
+ ///< template parameter T.
1516
+ );
1517
+ #endif
1518
+
1519
+ static void CheckCast(napi_env env, napi_value value);
1520
+
1521
+ TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
1522
+ TypedArrayOf(napi_env env,
1523
+ napi_value value); ///< Wraps a Node-API value primitive.
1524
+
1525
+ T& operator[](size_t index); ///< Gets or sets an element in the array.
1526
+ const T& operator[](size_t index) const; ///< Gets an element in the array.
1527
+
1528
+ /// Gets a pointer to the array's backing buffer.
1529
+ ///
1530
+ /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer,
1531
+ /// because the typed-array may have a non-zero `ByteOffset()` into the
1532
+ /// `ArrayBuffer`.
1533
+ T* Data();
1534
+
1535
+ /// Gets a pointer to the array's backing buffer.
1536
+ ///
1537
+ /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer,
1538
+ /// because the typed-array may have a non-zero `ByteOffset()` into the
1539
+ /// `ArrayBuffer`.
1540
+ const T* Data() const;
1541
+
1542
+ private:
1543
+ T* _data;
1544
+
1545
+ TypedArrayOf(napi_env env,
1546
+ napi_value value,
1547
+ napi_typedarray_type type,
1548
+ size_t length,
1549
+ T* data);
1550
+ };
1551
+
1552
+ /// The DataView provides a low-level interface for reading/writing multiple
1553
+ /// number types in an ArrayBuffer irrespective of the platform's endianness.
1554
+ class DataView : public Object {
1555
+ public:
1556
+ static DataView New(napi_env env, Napi::ArrayBuffer arrayBuffer);
1557
+ static DataView New(napi_env env,
1558
+ Napi::ArrayBuffer arrayBuffer,
1559
+ size_t byteOffset);
1560
+ static DataView New(napi_env env,
1561
+ Napi::ArrayBuffer arrayBuffer,
1562
+ size_t byteOffset,
1563
+ size_t byteLength);
1564
+
1565
+ #ifdef NODE_API_EXPERIMENTAL_HAS_SHAREDARRAYBUFFER
1566
+ static DataView New(napi_env env, Napi::SharedArrayBuffer arrayBuffer);
1567
+ static DataView New(napi_env env,
1568
+ Napi::SharedArrayBuffer arrayBuffer,
1569
+ size_t byteOffset);
1570
+ static DataView New(napi_env env,
1571
+ Napi::SharedArrayBuffer arrayBuffer,
1572
+ size_t byteOffset,
1573
+ size_t byteLength);
1574
+ #endif
1575
+
1576
+ static void CheckCast(napi_env env, napi_value value);
1577
+
1578
+ DataView(); ///< Creates a new _empty_ DataView instance.
1579
+ DataView(napi_env env,
1580
+ napi_value value); ///< Wraps a Node-API value primitive.
1581
+
1582
+ // Gets the backing `ArrayBuffer`.
1583
+ //
1584
+ // If this `DataView` is not backed by an `ArrayBuffer`, this method will
1585
+ // terminate the process with a fatal error when using
1586
+ // `NODE_ADDON_API_ENABLE_TYPE_CHECK_ON_AS` or exhibit undefined behavior
1587
+ // otherwise. Use `Buffer()` instead to get the backing buffer without
1588
+ // assuming its type.
1589
+ Napi::ArrayBuffer ArrayBuffer() const;
1590
+
1591
+ // Gets the backing buffer (an `ArrayBuffer` or `SharedArrayBuffer`).
1592
+ //
1593
+ // Use `IsArrayBuffer()` or `IsSharedArrayBuffer()` to check the type of the
1594
+ // backing buffer prior to casting with `As<T>()`.
1595
+ Napi::Value Buffer() const;
1596
+ size_t ByteOffset()
1597
+ const; ///< Gets the offset into the buffer where the array starts.
1598
+ size_t ByteLength() const; ///< Gets the length of the array in bytes.
1599
+
1600
+ void* Data() const;
1601
+
1602
+ float GetFloat32(size_t byteOffset) const;
1603
+ double GetFloat64(size_t byteOffset) const;
1604
+ int8_t GetInt8(size_t byteOffset) const;
1605
+ int16_t GetInt16(size_t byteOffset) const;
1606
+ int32_t GetInt32(size_t byteOffset) const;
1607
+ uint8_t GetUint8(size_t byteOffset) const;
1608
+ uint16_t GetUint16(size_t byteOffset) const;
1609
+ uint32_t GetUint32(size_t byteOffset) const;
1610
+
1611
+ void SetFloat32(size_t byteOffset, float value) const;
1612
+ void SetFloat64(size_t byteOffset, double value) const;
1613
+ void SetInt8(size_t byteOffset, int8_t value) const;
1614
+ void SetInt16(size_t byteOffset, int16_t value) const;
1615
+ void SetInt32(size_t byteOffset, int32_t value) const;
1616
+ void SetUint8(size_t byteOffset, uint8_t value) const;
1617
+ void SetUint16(size_t byteOffset, uint16_t value) const;
1618
+ void SetUint32(size_t byteOffset, uint32_t value) const;
1619
+
1620
+ private:
1621
+ template <typename T>
1622
+ T ReadData(size_t byteOffset) const;
1623
+
1624
+ template <typename T>
1625
+ void WriteData(size_t byteOffset, T value) const;
1626
+
1627
+ void* _data{};
1628
+ size_t _length{};
1629
+ };
1630
+
1631
+ class Function : public Object {
1632
+ public:
1633
+ using VoidCallback = void (*)(const CallbackInfo& info);
1634
+ using Callback = Value (*)(const CallbackInfo& info);
1635
+
1636
+ template <VoidCallback cb>
1637
+ static Function New(napi_env env,
1638
+ const char* utf8name = nullptr,
1639
+ void* data = nullptr);
1640
+
1641
+ template <Callback cb>
1642
+ static Function New(napi_env env,
1643
+ const char* utf8name = nullptr,
1644
+ void* data = nullptr);
1645
+
1646
+ template <VoidCallback cb>
1647
+ static Function New(napi_env env,
1648
+ const std::string& utf8name,
1649
+ void* data = nullptr);
1650
+
1651
+ template <Callback cb>
1652
+ static Function New(napi_env env,
1653
+ const std::string& utf8name,
1654
+ void* data = nullptr);
1655
+
1656
+ /// Callable must implement operator() accepting a const CallbackInfo&
1657
+ /// and return either void or Value.
1658
+ template <typename Callable>
1659
+ static Function New(napi_env env,
1660
+ Callable cb,
1661
+ const char* utf8name = nullptr,
1662
+ void* data = nullptr);
1663
+ /// Callable must implement operator() accepting a const CallbackInfo&
1664
+ /// and return either void or Value.
1665
+ template <typename Callable>
1666
+ static Function New(napi_env env,
1667
+ Callable cb,
1668
+ const std::string& utf8name,
1669
+ void* data = nullptr);
1670
+
1671
+ static void CheckCast(napi_env env, napi_value value);
1672
+
1673
+ Function();
1674
+ Function(napi_env env, napi_value value);
1675
+
1676
+ MaybeOrValue<Value> operator()(
1677
+ const std::initializer_list<napi_value>& args) const;
1678
+
1679
+ MaybeOrValue<Value> Call(const std::initializer_list<napi_value>& args) const;
1680
+ MaybeOrValue<Value> Call(const std::vector<napi_value>& args) const;
1681
+ MaybeOrValue<Value> Call(const std::vector<Value>& args) const;
1682
+ MaybeOrValue<Value> Call(size_t argc, const napi_value* args) const;
1683
+ MaybeOrValue<Value> Call(napi_value recv,
1684
+ const std::initializer_list<napi_value>& args) const;
1685
+ MaybeOrValue<Value> Call(napi_value recv,
1686
+ const std::vector<napi_value>& args) const;
1687
+ MaybeOrValue<Value> Call(napi_value recv,
1688
+ const std::vector<Value>& args) const;
1689
+ MaybeOrValue<Value> Call(napi_value recv,
1690
+ size_t argc,
1691
+ const napi_value* args) const;
1692
+
1693
+ MaybeOrValue<Value> MakeCallback(
1694
+ napi_value recv,
1695
+ const std::initializer_list<napi_value>& args,
1696
+ napi_async_context context = nullptr) const;
1697
+ MaybeOrValue<Value> MakeCallback(napi_value recv,
1698
+ const std::vector<napi_value>& args,
1699
+ napi_async_context context = nullptr) const;
1700
+ MaybeOrValue<Value> MakeCallback(napi_value recv,
1701
+ size_t argc,
1702
+ const napi_value* args,
1703
+ napi_async_context context = nullptr) const;
1704
+
1705
+ MaybeOrValue<Object> New(const std::initializer_list<napi_value>& args) const;
1706
+ MaybeOrValue<Object> New(const std::vector<napi_value>& args) const;
1707
+ MaybeOrValue<Object> New(size_t argc, const napi_value* args) const;
1708
+ };
1709
+
1710
+ class Promise : public Object {
1711
+ public:
1712
+ class Deferred {
1713
+ public:
1714
+ static Deferred New(napi_env env);
1715
+ Deferred(napi_env env);
1716
+
1717
+ Napi::Promise Promise() const;
1718
+ Napi::Env Env() const;
1719
+
1720
+ void Resolve(napi_value value) const;
1721
+ void Reject(napi_value value) const;
1722
+
1723
+ private:
1724
+ napi_env _env;
1725
+ napi_deferred _deferred;
1726
+ napi_value _promise;
1727
+ };
1728
+
1729
+ static void CheckCast(napi_env env, napi_value value);
1730
+
1731
+ Promise();
1732
+ Promise(napi_env env, napi_value value);
1733
+
1734
+ MaybeOrValue<Promise> Then(napi_value onFulfilled) const;
1735
+ MaybeOrValue<Promise> Then(napi_value onFulfilled,
1736
+ napi_value onRejected) const;
1737
+ MaybeOrValue<Promise> Catch(napi_value onRejected) const;
1738
+
1739
+ MaybeOrValue<Promise> Then(const Function& onFulfilled) const;
1740
+ MaybeOrValue<Promise> Then(const Function& onFulfilled,
1741
+ const Function& onRejected) const;
1742
+ MaybeOrValue<Promise> Catch(const Function& onRejected) const;
1743
+ };
1744
+
1745
+ template <typename T>
1746
+ class Buffer : public Uint8Array {
1747
+ public:
1748
+ static Buffer<T> New(napi_env env, size_t length);
1749
+ #ifndef NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1750
+ static Buffer<T> New(napi_env env, T* data, size_t length);
1751
+
1752
+ // Finalizer must implement `void operator()(Env env, T* data)`.
1753
+ template <typename Finalizer>
1754
+ static Buffer<T> New(napi_env env,
1755
+ T* data,
1756
+ size_t length,
1757
+ Finalizer finalizeCallback);
1758
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
1759
+ template <typename Finalizer, typename Hint>
1760
+ static Buffer<T> New(napi_env env,
1761
+ T* data,
1762
+ size_t length,
1763
+ Finalizer finalizeCallback,
1764
+ Hint* finalizeHint);
1765
+ #endif // NODE_API_NO_EXTERNAL_BUFFERS_ALLOWED
1766
+
1767
+ static Buffer<T> NewOrCopy(napi_env env, T* data, size_t length);
1768
+ // Finalizer must implement `void operator()(Env env, T* data)`.
1769
+ template <typename Finalizer>
1770
+ static Buffer<T> NewOrCopy(napi_env env,
1771
+ T* data,
1772
+ size_t length,
1773
+ Finalizer finalizeCallback);
1774
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
1775
+ template <typename Finalizer, typename Hint>
1776
+ static Buffer<T> NewOrCopy(napi_env env,
1777
+ T* data,
1778
+ size_t length,
1779
+ Finalizer finalizeCallback,
1780
+ Hint* finalizeHint);
1781
+
1782
+ static Buffer<T> Copy(napi_env env, const T* data, size_t length);
1783
+
1784
+ static void CheckCast(napi_env env, napi_value value);
1785
+
1786
+ Buffer();
1787
+ Buffer(napi_env env, napi_value value);
1788
+ size_t Length() const;
1789
+ T* Data() const;
1790
+
1791
+ private:
1792
+ };
1793
+
1794
+ /// Holds a counted reference to a value; initially a weak reference unless
1795
+ /// otherwise specified, may be changed to/from a strong reference by adjusting
1796
+ /// the refcount.
1797
+ ///
1798
+ /// The referenced value is not immediately destroyed when the reference count
1799
+ /// is zero; it is merely then eligible for garbage-collection if there are no
1800
+ /// other references to the value.
1801
+ template <typename T>
1802
+ class Reference {
1803
+ public:
1804
+ static Reference<T> New(const T& value, uint32_t initialRefcount = 0);
1805
+
1806
+ Reference();
1807
+ Reference(napi_env env, napi_ref ref);
1808
+ ~Reference();
1809
+
1810
+ // A reference can be moved but cannot be copied.
1811
+ Reference(Reference<T>&& other);
1812
+ Reference<T>& operator=(Reference<T>&& other);
1813
+ NAPI_DISALLOW_ASSIGN(Reference<T>)
1814
+
1815
+ operator napi_ref() const;
1816
+ bool operator==(const Reference<T>& other) const;
1817
+ bool operator!=(const Reference<T>& other) const;
1818
+
1819
+ Napi::Env Env() const;
1820
+ bool IsEmpty() const;
1821
+
1822
+ // Note when getting the value of a Reference it is usually correct to do so
1823
+ // within a HandleScope so that the value handle gets cleaned up efficiently.
1824
+ T Value() const;
1825
+
1826
+ uint32_t Ref() const;
1827
+ uint32_t Unref() const;
1828
+ void Reset();
1829
+ void Reset(const T& value, uint32_t refcount = 0);
1830
+
1831
+ // Call this on a reference that is declared as static data, to prevent its
1832
+ // destructor from running at program shutdown time, which would attempt to
1833
+ // reset the reference when the environment is no longer valid. Avoid using
1834
+ // this if at all possible. If you do need to use static data, MAKE SURE to
1835
+ // warn your users that your addon is NOT threadsafe.
1836
+ void SuppressDestruct();
1837
+
1838
+ protected:
1839
+ Reference(const Reference<T>&);
1840
+
1841
+ /// !cond INTERNAL
1842
+ napi_env _env;
1843
+ napi_ref _ref;
1844
+ /// !endcond
1845
+
1846
+ private:
1847
+ bool _suppressDestruct;
1848
+ };
1849
+
1850
+ class ObjectReference : public Reference<Object> {
1851
+ public:
1852
+ ObjectReference();
1853
+ ObjectReference(napi_env env, napi_ref ref);
1854
+
1855
+ // A reference can be moved but cannot be copied.
1856
+ ObjectReference(Reference<Object>&& other);
1857
+ ObjectReference& operator=(Reference<Object>&& other);
1858
+ ObjectReference(ObjectReference&& other);
1859
+ ObjectReference& operator=(ObjectReference&& other);
1860
+ NAPI_DISALLOW_ASSIGN(ObjectReference)
1861
+
1862
+ MaybeOrValue<Napi::Value> Get(const char* utf8name) const;
1863
+ MaybeOrValue<Napi::Value> Get(const std::string& utf8name) const;
1864
+ MaybeOrValue<bool> Set(const char* utf8name, napi_value value) const;
1865
+ MaybeOrValue<bool> Set(const char* utf8name, Napi::Value value) const;
1866
+ MaybeOrValue<bool> Set(const char* utf8name, const char* utf8value) const;
1867
+ MaybeOrValue<bool> Set(const char* utf8name, bool boolValue) const;
1868
+ MaybeOrValue<bool> Set(const char* utf8name, double numberValue) const;
1869
+ MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value) const;
1870
+ MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value) const;
1871
+ MaybeOrValue<bool> Set(const std::string& utf8name,
1872
+ const std::string& utf8value) const;
1873
+ MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue) const;
1874
+ MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue) const;
1875
+
1876
+ MaybeOrValue<Napi::Value> Get(uint32_t index) const;
1877
+ MaybeOrValue<bool> Set(uint32_t index, const napi_value value) const;
1878
+ MaybeOrValue<bool> Set(uint32_t index, const Napi::Value value) const;
1879
+ MaybeOrValue<bool> Set(uint32_t index, const char* utf8value) const;
1880
+ MaybeOrValue<bool> Set(uint32_t index, const std::string& utf8value) const;
1881
+ MaybeOrValue<bool> Set(uint32_t index, bool boolValue) const;
1882
+ MaybeOrValue<bool> Set(uint32_t index, double numberValue) const;
1883
+
1884
+ protected:
1885
+ ObjectReference(const ObjectReference&);
1886
+ };
1887
+
1888
+ class FunctionReference : public Reference<Function> {
1889
+ public:
1890
+ FunctionReference();
1891
+ FunctionReference(napi_env env, napi_ref ref);
1892
+
1893
+ // A reference can be moved but cannot be copied.
1894
+ FunctionReference(Reference<Function>&& other);
1895
+ FunctionReference& operator=(Reference<Function>&& other);
1896
+ FunctionReference(FunctionReference&& other);
1897
+ FunctionReference& operator=(FunctionReference&& other);
1898
+ NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
1899
+
1900
+ MaybeOrValue<Napi::Value> operator()(
1901
+ const std::initializer_list<napi_value>& args) const;
1902
+
1903
+ MaybeOrValue<Napi::Value> Call(
1904
+ const std::initializer_list<napi_value>& args) const;
1905
+ MaybeOrValue<Napi::Value> Call(const std::vector<napi_value>& args) const;
1906
+ MaybeOrValue<Napi::Value> Call(
1907
+ napi_value recv, const std::initializer_list<napi_value>& args) const;
1908
+ MaybeOrValue<Napi::Value> Call(napi_value recv,
1909
+ const std::vector<napi_value>& args) const;
1910
+ MaybeOrValue<Napi::Value> Call(napi_value recv,
1911
+ size_t argc,
1912
+ const napi_value* args) const;
1913
+
1914
+ MaybeOrValue<Napi::Value> MakeCallback(
1915
+ napi_value recv,
1916
+ const std::initializer_list<napi_value>& args,
1917
+ napi_async_context context = nullptr) const;
1918
+ MaybeOrValue<Napi::Value> MakeCallback(
1919
+ napi_value recv,
1920
+ const std::vector<napi_value>& args,
1921
+ napi_async_context context = nullptr) const;
1922
+ MaybeOrValue<Napi::Value> MakeCallback(
1923
+ napi_value recv,
1924
+ size_t argc,
1925
+ const napi_value* args,
1926
+ napi_async_context context = nullptr) const;
1927
+
1928
+ MaybeOrValue<Object> New(const std::initializer_list<napi_value>& args) const;
1929
+ MaybeOrValue<Object> New(const std::vector<napi_value>& args) const;
1930
+ };
1931
+
1932
+ // Shortcuts to creating a new reference with inferred type and refcount = 0.
1933
+ template <typename T>
1934
+ Reference<T> Weak(T value);
1935
+ ObjectReference Weak(Object value);
1936
+ FunctionReference Weak(Function value);
1937
+
1938
+ // Shortcuts to creating a new reference with inferred type and refcount = 1.
1939
+ template <typename T>
1940
+ Reference<T> Persistent(T value);
1941
+ ObjectReference Persistent(Object value);
1942
+ FunctionReference Persistent(Function value);
1943
+
1944
+ /// A persistent reference to a JavaScript error object. Use of this class
1945
+ /// depends somewhat on whether C++ exceptions are enabled at compile time.
1946
+ ///
1947
+ /// ### Handling Errors With C++ Exceptions
1948
+ ///
1949
+ /// If C++ exceptions are enabled, then the `Error` class extends
1950
+ /// `std::exception` and enables integrated error-handling for C++ exceptions
1951
+ /// and JavaScript exceptions.
1952
+ ///
1953
+ /// If a Node-API call fails without executing any JavaScript code (for
1954
+ /// example due to an invalid argument), then the Node-API wrapper
1955
+ /// automatically converts and throws the error as a C++ exception of type
1956
+ /// `Napi::Error`. Or if a JavaScript function called by C++ code via Node-API
1957
+ /// throws a JavaScript exception, then the Node-API wrapper automatically
1958
+ /// converts and throws it as a C++ exception of type `Napi::Error`.
1959
+ ///
1960
+ /// If a C++ exception of type `Napi::Error` escapes from a Node-API C++
1961
+ /// callback, then the Node-API wrapper automatically converts and throws it
1962
+ /// as a JavaScript exception. Therefore, catching a C++ exception of type
1963
+ /// `Napi::Error` prevents a JavaScript exception from being thrown.
1964
+ ///
1965
+ /// #### Example 1A - Throwing a C++ exception:
1966
+ ///
1967
+ /// Napi::Env env = ...
1968
+ /// throw Napi::Error::New(env, "Example exception");
1969
+ ///
1970
+ /// Following C++ statements will not be executed. The exception will bubble
1971
+ /// up as a C++ exception of type `Napi::Error`, until it is either caught
1972
+ /// while still in C++, or else automatically propagated as a JavaScript
1973
+ /// exception when the callback returns to JavaScript.
1974
+ ///
1975
+ /// #### Example 2A - Propagating a Node-API C++ exception:
1976
+ ///
1977
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1978
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1979
+ ///
1980
+ /// Following C++ statements will not be executed. The exception will bubble
1981
+ /// up as a C++ exception of type `Napi::Error`, until it is either caught
1982
+ /// while still in C++, or else automatically propagated as a JavaScript
1983
+ /// exception when the callback returns to JavaScript.
1984
+ ///
1985
+ /// #### Example 3A - Handling a Node-API C++ exception:
1986
+ ///
1987
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1988
+ /// Napi::Value result;
1989
+ /// try {
1990
+ /// result = jsFunctionThatThrows({ arg1, arg2 });
1991
+ /// } catch (const Napi::Error& e) {
1992
+ /// cerr << "Caught JavaScript exception: " + e.what();
1993
+ /// }
1994
+ ///
1995
+ /// Since the exception was caught here, it will not be propagated as a
1996
+ /// JavaScript exception.
1997
+ ///
1998
+ /// ### Handling Errors Without C++ Exceptions
1999
+ ///
2000
+ /// If C++ exceptions are disabled (by defining
2001
+ /// `NODE_ADDON_API_DISABLE_CPP_EXCEPTIONS`) then this class does not extend
2002
+ /// `std::exception`, and APIs in the `Napi` namespace do not throw C++
2003
+ /// exceptions when they fail. Instead, they raise _pending_ JavaScript
2004
+ /// exceptions and return _empty_ `Value`s. Calling code should check
2005
+ /// `Value::IsEmpty()` before attempting to use a returned value, and may use
2006
+ /// methods on the `Env` class to check for, get, and clear a pending JavaScript
2007
+ /// exception. If the pending exception is not cleared, it will be thrown when
2008
+ /// the native callback returns to JavaScript.
2009
+ ///
2010
+ /// #### Example 1B - Throwing a JS exception
2011
+ ///
2012
+ /// Napi::Env env = ...
2013
+ /// Napi::Error::New(env, "Example
2014
+ /// exception").ThrowAsJavaScriptException(); return;
2015
+ ///
2016
+ /// After throwing a JS exception, the code should generally return
2017
+ /// immediately from the native callback, after performing any necessary
2018
+ /// cleanup.
2019
+ ///
2020
+ /// #### Example 2B - Propagating a Node-API JS exception:
2021
+ ///
2022
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
2023
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
2024
+ /// if (result.IsEmpty()) return;
2025
+ ///
2026
+ /// An empty value result from a Node-API call indicates an error occurred,
2027
+ /// and a JavaScript exception is pending. To let the exception propagate, the
2028
+ /// code should generally return immediately from the native callback, after
2029
+ /// performing any necessary cleanup.
2030
+ ///
2031
+ /// #### Example 3B - Handling a Node-API JS exception:
2032
+ ///
2033
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
2034
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
2035
+ /// if (result.IsEmpty()) {
2036
+ /// Napi::Error e = env.GetAndClearPendingException();
2037
+ /// cerr << "Caught JavaScript exception: " + e.Message();
2038
+ /// }
2039
+ ///
2040
+ /// Since the exception was cleared here, it will not be propagated as a
2041
+ /// JavaScript exception after the native callback returns.
2042
+ class Error : public ObjectReference
2043
+ #ifdef NODE_ADDON_API_CPP_EXCEPTIONS
2044
+ ,
2045
+ public std::exception
2046
+ #endif // NODE_ADDON_API_CPP_EXCEPTIONS
2047
+ {
2048
+ public:
2049
+ static Error New(napi_env env);
2050
+ static Error New(napi_env env, const char* message);
2051
+ static Error New(napi_env env, const std::string& message);
2052
+
2053
+ static NAPI_NO_RETURN void Fatal(const char* location, const char* message);
2054
+
2055
+ Error();
2056
+ Error(napi_env env, napi_value value);
2057
+
2058
+ // An error can be moved or copied.
2059
+ Error(Error&& other);
2060
+ Error& operator=(Error&& other);
2061
+ Error(const Error&);
2062
+ Error& operator=(const Error&);
2063
+
2064
+ const std::string& Message() const NAPI_NOEXCEPT;
2065
+ void ThrowAsJavaScriptException() const;
2066
+
2067
+ Object Value() const;
2068
+
2069
+ #ifdef NODE_ADDON_API_CPP_EXCEPTIONS
2070
+ const char* what() const NAPI_NOEXCEPT override;
2071
+ #endif // NODE_ADDON_API_CPP_EXCEPTIONS
2072
+
2073
+ protected:
2074
+ /// !cond INTERNAL
2075
+ using create_error_fn = napi_status (*)(napi_env envb,
2076
+ napi_value code,
2077
+ napi_value msg,
2078
+ napi_value* result);
2079
+
2080
+ template <typename TError>
2081
+ static TError New(napi_env env,
2082
+ const char* message,
2083
+ size_t length,
2084
+ create_error_fn create_error);
2085
+ /// !endcond
2086
+
2087
+ private:
2088
+ static inline const char* ERROR_WRAP_VALUE() NAPI_NOEXCEPT;
2089
+ mutable std::string _message;
2090
+ };
2091
+
2092
+ class TypeError : public Error {
2093
+ public:
2094
+ static TypeError New(napi_env env, const char* message);
2095
+ static TypeError New(napi_env env, const std::string& message);
2096
+
2097
+ TypeError();
2098
+ TypeError(napi_env env, napi_value value);
2099
+ };
2100
+
2101
+ class RangeError : public Error {
2102
+ public:
2103
+ static RangeError New(napi_env env, const char* message);
2104
+ static RangeError New(napi_env env, const std::string& message);
2105
+
2106
+ RangeError();
2107
+ RangeError(napi_env env, napi_value value);
2108
+ };
2109
+
2110
+ #if NAPI_VERSION > 8
2111
+ class SyntaxError : public Error {
2112
+ public:
2113
+ static SyntaxError New(napi_env env, const char* message);
2114
+ static SyntaxError New(napi_env env, const std::string& message);
2115
+
2116
+ SyntaxError();
2117
+ SyntaxError(napi_env env, napi_value value);
2118
+ };
2119
+ #endif // NAPI_VERSION > 8
2120
+
2121
+ class CallbackInfo {
2122
+ public:
2123
+ CallbackInfo(napi_env env, napi_callback_info info);
2124
+ ~CallbackInfo();
2125
+
2126
+ // Disallow copying to prevent multiple free of _dynamicArgs
2127
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
2128
+
2129
+ Napi::Env Env() const;
2130
+ Value NewTarget() const;
2131
+ bool IsConstructCall() const;
2132
+ size_t Length() const;
2133
+ const Value operator[](size_t index) const;
2134
+ Value This() const;
2135
+ void* Data() const;
2136
+ void SetData(void* data);
2137
+ explicit operator napi_callback_info() const;
2138
+
2139
+ private:
2140
+ const size_t _staticArgCount = 6;
2141
+ napi_env _env;
2142
+ napi_callback_info _info;
2143
+ napi_value _this;
2144
+ size_t _argc;
2145
+ napi_value* _argv;
2146
+ napi_value _staticArgs[6]{};
2147
+ napi_value* _dynamicArgs;
2148
+ void* _data;
2149
+ };
2150
+
2151
+ class PropertyDescriptor {
2152
+ public:
2153
+ using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info);
2154
+ using SetterCallback = void (*)(const Napi::CallbackInfo& info);
2155
+
2156
+ #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
2157
+ template <typename Getter>
2158
+ static PropertyDescriptor Accessor(
2159
+ const char* utf8name,
2160
+ Getter getter,
2161
+ napi_property_attributes attributes = napi_default,
2162
+ void* data = nullptr);
2163
+ template <typename Getter>
2164
+ static PropertyDescriptor Accessor(
2165
+ const std::string& utf8name,
2166
+ Getter getter,
2167
+ napi_property_attributes attributes = napi_default,
2168
+ void* data = nullptr);
2169
+ template <typename Getter>
2170
+ static PropertyDescriptor Accessor(
2171
+ napi_value name,
2172
+ Getter getter,
2173
+ napi_property_attributes attributes = napi_default,
2174
+ void* data = nullptr);
2175
+ template <typename Getter>
2176
+ static PropertyDescriptor Accessor(
2177
+ Name name,
2178
+ Getter getter,
2179
+ napi_property_attributes attributes = napi_default,
2180
+ void* data = nullptr);
2181
+ template <typename Getter, typename Setter>
2182
+ static PropertyDescriptor Accessor(
2183
+ const char* utf8name,
2184
+ Getter getter,
2185
+ Setter setter,
2186
+ napi_property_attributes attributes = napi_default,
2187
+ void* data = nullptr);
2188
+ template <typename Getter, typename Setter>
2189
+ static PropertyDescriptor Accessor(
2190
+ const std::string& utf8name,
2191
+ Getter getter,
2192
+ Setter setter,
2193
+ napi_property_attributes attributes = napi_default,
2194
+ void* data = nullptr);
2195
+ template <typename Getter, typename Setter>
2196
+ static PropertyDescriptor Accessor(
2197
+ napi_value name,
2198
+ Getter getter,
2199
+ Setter setter,
2200
+ napi_property_attributes attributes = napi_default,
2201
+ void* data = nullptr);
2202
+ template <typename Getter, typename Setter>
2203
+ static PropertyDescriptor Accessor(
2204
+ Name name,
2205
+ Getter getter,
2206
+ Setter setter,
2207
+ napi_property_attributes attributes = napi_default,
2208
+ void* data = nullptr);
2209
+ template <typename Callable>
2210
+ static PropertyDescriptor Function(
2211
+ const char* utf8name,
2212
+ Callable cb,
2213
+ napi_property_attributes attributes = napi_default,
2214
+ void* data = nullptr);
2215
+ template <typename Callable>
2216
+ static PropertyDescriptor Function(
2217
+ const std::string& utf8name,
2218
+ Callable cb,
2219
+ napi_property_attributes attributes = napi_default,
2220
+ void* data = nullptr);
2221
+ template <typename Callable>
2222
+ static PropertyDescriptor Function(
2223
+ napi_value name,
2224
+ Callable cb,
2225
+ napi_property_attributes attributes = napi_default,
2226
+ void* data = nullptr);
2227
+ template <typename Callable>
2228
+ static PropertyDescriptor Function(
2229
+ Name name,
2230
+ Callable cb,
2231
+ napi_property_attributes attributes = napi_default,
2232
+ void* data = nullptr);
2233
+ #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
2234
+
2235
+ template <GetterCallback Getter>
2236
+ static PropertyDescriptor Accessor(
2237
+ const char* utf8name,
2238
+ napi_property_attributes attributes = napi_default,
2239
+ void* data = nullptr);
2240
+
2241
+ template <GetterCallback Getter>
2242
+ static PropertyDescriptor Accessor(
2243
+ const std::string& utf8name,
2244
+ napi_property_attributes attributes = napi_default,
2245
+ void* data = nullptr);
2246
+
2247
+ template <GetterCallback Getter>
2248
+ static PropertyDescriptor Accessor(
2249
+ Name name,
2250
+ napi_property_attributes attributes = napi_default,
2251
+ void* data = nullptr);
2252
+
2253
+ template <GetterCallback Getter, SetterCallback Setter>
2254
+ static PropertyDescriptor Accessor(
2255
+ const char* utf8name,
2256
+ napi_property_attributes attributes = napi_default,
2257
+ void* data = nullptr);
2258
+
2259
+ template <GetterCallback Getter, SetterCallback Setter>
2260
+ static PropertyDescriptor Accessor(
2261
+ const std::string& utf8name,
2262
+ napi_property_attributes attributes = napi_default,
2263
+ void* data = nullptr);
2264
+
2265
+ template <GetterCallback Getter, SetterCallback Setter>
2266
+ static PropertyDescriptor Accessor(
2267
+ Name name,
2268
+ napi_property_attributes attributes = napi_default,
2269
+ void* data = nullptr);
2270
+
2271
+ template <typename Getter>
2272
+ static PropertyDescriptor Accessor(
2273
+ Napi::Env env,
2274
+ Napi::Object object,
2275
+ const char* utf8name,
2276
+ Getter getter,
2277
+ napi_property_attributes attributes = napi_default,
2278
+ void* data = nullptr);
2279
+ template <typename Getter>
2280
+ static PropertyDescriptor Accessor(
2281
+ Napi::Env env,
2282
+ Napi::Object object,
2283
+ const std::string& utf8name,
2284
+ Getter getter,
2285
+ napi_property_attributes attributes = napi_default,
2286
+ void* data = nullptr);
2287
+ template <typename Getter>
2288
+ static PropertyDescriptor Accessor(
2289
+ Napi::Env env,
2290
+ Napi::Object object,
2291
+ Name name,
2292
+ Getter getter,
2293
+ napi_property_attributes attributes = napi_default,
2294
+ void* data = nullptr);
2295
+ template <typename Getter, typename Setter>
2296
+ static PropertyDescriptor Accessor(
2297
+ Napi::Env env,
2298
+ Napi::Object object,
2299
+ const char* utf8name,
2300
+ Getter getter,
2301
+ Setter setter,
2302
+ napi_property_attributes attributes = napi_default,
2303
+ void* data = nullptr);
2304
+ template <typename Getter, typename Setter>
2305
+ static PropertyDescriptor Accessor(
2306
+ Napi::Env env,
2307
+ Napi::Object object,
2308
+ const std::string& utf8name,
2309
+ Getter getter,
2310
+ Setter setter,
2311
+ napi_property_attributes attributes = napi_default,
2312
+ void* data = nullptr);
2313
+ template <typename Getter, typename Setter>
2314
+ static PropertyDescriptor Accessor(
2315
+ Napi::Env env,
2316
+ Napi::Object object,
2317
+ Name name,
2318
+ Getter getter,
2319
+ Setter setter,
2320
+ napi_property_attributes attributes = napi_default,
2321
+ void* data = nullptr);
2322
+ template <typename Callable>
2323
+ static PropertyDescriptor Function(
2324
+ Napi::Env env,
2325
+ Napi::Object object,
2326
+ const char* utf8name,
2327
+ Callable cb,
2328
+ napi_property_attributes attributes = napi_default,
2329
+ void* data = nullptr);
2330
+ template <typename Callable>
2331
+ static PropertyDescriptor Function(
2332
+ Napi::Env env,
2333
+ Napi::Object object,
2334
+ const std::string& utf8name,
2335
+ Callable cb,
2336
+ napi_property_attributes attributes = napi_default,
2337
+ void* data = nullptr);
2338
+ template <typename Callable>
2339
+ static PropertyDescriptor Function(
2340
+ Napi::Env env,
2341
+ Napi::Object object,
2342
+ Name name,
2343
+ Callable cb,
2344
+ napi_property_attributes attributes = napi_default,
2345
+ void* data = nullptr);
2346
+ static PropertyDescriptor Value(
2347
+ const char* utf8name,
2348
+ napi_value value,
2349
+ napi_property_attributes attributes = napi_default);
2350
+ static PropertyDescriptor Value(
2351
+ const std::string& utf8name,
2352
+ napi_value value,
2353
+ napi_property_attributes attributes = napi_default);
2354
+ static PropertyDescriptor Value(
2355
+ napi_value name,
2356
+ napi_value value,
2357
+ napi_property_attributes attributes = napi_default);
2358
+ static PropertyDescriptor Value(
2359
+ Name name,
2360
+ Napi::Value value,
2361
+ napi_property_attributes attributes = napi_default);
2362
+
2363
+ PropertyDescriptor(napi_property_descriptor desc);
2364
+
2365
+ operator napi_property_descriptor&();
2366
+ operator const napi_property_descriptor&() const;
2367
+
2368
+ private:
2369
+ napi_property_descriptor _desc;
2370
+ };
2371
+
2372
+ /// Property descriptor for use with `ObjectWrap::DefineClass()`.
2373
+ ///
2374
+ /// This is different from the standalone `PropertyDescriptor` because it is
2375
+ /// specific to each `ObjectWrap<T>` subclass. This prevents using descriptors
2376
+ /// from a different class when defining a new class (preventing the callbacks
2377
+ /// from having incorrect `this` pointers).
2378
+ template <typename T>
2379
+ class ClassPropertyDescriptor {
2380
+ public:
2381
+ ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {}
2382
+
2383
+ operator napi_property_descriptor&() { return _desc; }
2384
+ operator const napi_property_descriptor&() const { return _desc; }
2385
+
2386
+ private:
2387
+ napi_property_descriptor _desc;
2388
+ };
2389
+
2390
+ template <typename T, typename TCallback>
2391
+ struct MethodCallbackData {
2392
+ TCallback callback;
2393
+ void* data;
2394
+ };
2395
+
2396
+ template <typename T, typename TGetterCallback, typename TSetterCallback>
2397
+ struct AccessorCallbackData {
2398
+ TGetterCallback getterCallback;
2399
+ TSetterCallback setterCallback;
2400
+ void* data;
2401
+ };
2402
+
2403
+ template <typename T>
2404
+ class InstanceWrap {
2405
+ public:
2406
+ using InstanceVoidMethodCallback = void (T::*)(const CallbackInfo& info);
2407
+ using InstanceMethodCallback = Napi::Value (T::*)(const CallbackInfo& info);
2408
+ using InstanceGetterCallback = Napi::Value (T::*)(const CallbackInfo& info);
2409
+ using InstanceSetterCallback = void (T::*)(const CallbackInfo& info,
2410
+ const Napi::Value& value);
2411
+
2412
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
2413
+
2414
+ static PropertyDescriptor InstanceMethod(
2415
+ const char* utf8name,
2416
+ InstanceVoidMethodCallback method,
2417
+ napi_property_attributes attributes = napi_default,
2418
+ void* data = nullptr);
2419
+ static PropertyDescriptor InstanceMethod(
2420
+ const char* utf8name,
2421
+ InstanceMethodCallback method,
2422
+ napi_property_attributes attributes = napi_default,
2423
+ void* data = nullptr);
2424
+ static PropertyDescriptor InstanceMethod(
2425
+ Symbol name,
2426
+ InstanceVoidMethodCallback method,
2427
+ napi_property_attributes attributes = napi_default,
2428
+ void* data = nullptr);
2429
+ static PropertyDescriptor InstanceMethod(
2430
+ Symbol name,
2431
+ InstanceMethodCallback method,
2432
+ napi_property_attributes attributes = napi_default,
2433
+ void* data = nullptr);
2434
+ template <InstanceVoidMethodCallback method>
2435
+ static PropertyDescriptor InstanceMethod(
2436
+ const char* utf8name,
2437
+ napi_property_attributes attributes = napi_default,
2438
+ void* data = nullptr);
2439
+ template <InstanceMethodCallback method>
2440
+ static PropertyDescriptor InstanceMethod(
2441
+ const char* utf8name,
2442
+ napi_property_attributes attributes = napi_default,
2443
+ void* data = nullptr);
2444
+ template <InstanceVoidMethodCallback method>
2445
+ static PropertyDescriptor InstanceMethod(
2446
+ Symbol name,
2447
+ napi_property_attributes attributes = napi_default,
2448
+ void* data = nullptr);
2449
+ template <InstanceMethodCallback method>
2450
+ static PropertyDescriptor InstanceMethod(
2451
+ Symbol name,
2452
+ napi_property_attributes attributes = napi_default,
2453
+ void* data = nullptr);
2454
+ static PropertyDescriptor InstanceAccessor(
2455
+ const char* utf8name,
2456
+ InstanceGetterCallback getter,
2457
+ InstanceSetterCallback setter,
2458
+ napi_property_attributes attributes = napi_default,
2459
+ void* data = nullptr);
2460
+ static PropertyDescriptor InstanceAccessor(
2461
+ Symbol name,
2462
+ InstanceGetterCallback getter,
2463
+ InstanceSetterCallback setter,
2464
+ napi_property_attributes attributes = napi_default,
2465
+ void* data = nullptr);
2466
+ template <InstanceGetterCallback getter,
2467
+ InstanceSetterCallback setter = nullptr>
2468
+ static PropertyDescriptor InstanceAccessor(
2469
+ const char* utf8name,
2470
+ napi_property_attributes attributes = napi_default,
2471
+ void* data = nullptr);
2472
+ template <InstanceGetterCallback getter,
2473
+ InstanceSetterCallback setter = nullptr>
2474
+ static PropertyDescriptor InstanceAccessor(
2475
+ Symbol name,
2476
+ napi_property_attributes attributes = napi_default,
2477
+ void* data = nullptr);
2478
+ static PropertyDescriptor InstanceValue(
2479
+ const char* utf8name,
2480
+ Napi::Value value,
2481
+ napi_property_attributes attributes = napi_default);
2482
+ static PropertyDescriptor InstanceValue(
2483
+ Symbol name,
2484
+ Napi::Value value,
2485
+ napi_property_attributes attributes = napi_default);
2486
+
2487
+ protected:
2488
+ static void AttachPropData(napi_env env,
2489
+ napi_value value,
2490
+ const napi_property_descriptor* prop);
2491
+
2492
+ private:
2493
+ using This = InstanceWrap<T>;
2494
+
2495
+ using InstanceVoidMethodCallbackData =
2496
+ MethodCallbackData<T, InstanceVoidMethodCallback>;
2497
+ using InstanceMethodCallbackData =
2498
+ MethodCallbackData<T, InstanceMethodCallback>;
2499
+ using InstanceAccessorCallbackData =
2500
+ AccessorCallbackData<T, InstanceGetterCallback, InstanceSetterCallback>;
2501
+
2502
+ static napi_value InstanceVoidMethodCallbackWrapper(napi_env env,
2503
+ napi_callback_info info);
2504
+ static napi_value InstanceMethodCallbackWrapper(napi_env env,
2505
+ napi_callback_info info);
2506
+ static napi_value InstanceGetterCallbackWrapper(napi_env env,
2507
+ napi_callback_info info);
2508
+ static napi_value InstanceSetterCallbackWrapper(napi_env env,
2509
+ napi_callback_info info);
2510
+
2511
+ template <InstanceSetterCallback method>
2512
+ static napi_value WrappedMethod(napi_env env,
2513
+ napi_callback_info info) NAPI_NOEXCEPT;
2514
+
2515
+ template <InstanceSetterCallback setter>
2516
+ struct SetterTag {};
2517
+
2518
+ template <InstanceSetterCallback setter>
2519
+ static napi_callback WrapSetter(SetterTag<setter>) NAPI_NOEXCEPT {
2520
+ return &This::WrappedMethod<setter>;
2521
+ }
2522
+ static napi_callback WrapSetter(SetterTag<nullptr>) NAPI_NOEXCEPT {
2523
+ return nullptr;
2524
+ }
2525
+ };
2526
+
2527
+ /// Base class to be extended by C++ classes exposed to JavaScript; each C++
2528
+ /// class instance gets "wrapped" by a JavaScript object that is managed by this
2529
+ /// class.
2530
+ ///
2531
+ /// At initialization time, the `DefineClass()` method must be used to
2532
+ /// hook up the accessor and method callbacks. It takes a list of
2533
+ /// property descriptors, which can be constructed via the various
2534
+ /// static methods on the base class.
2535
+ ///
2536
+ /// #### Example:
2537
+ ///
2538
+ /// class Example: public Napi::ObjectWrap<Example> {
2539
+ /// public:
2540
+ /// static void Initialize(Napi::Env& env, Napi::Object& target) {
2541
+ /// Napi::Function constructor = DefineClass(env, "Example", {
2542
+ /// InstanceAccessor<&Example::GetSomething,
2543
+ /// &Example::SetSomething>("value"),
2544
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
2545
+ /// });
2546
+ /// target.Set("Example", constructor);
2547
+ /// }
2548
+ ///
2549
+ /// Example(const Napi::CallbackInfo& info); // Constructor
2550
+ /// Napi::Value GetSomething(const Napi::CallbackInfo& info);
2551
+ /// void SetSomething(const Napi::CallbackInfo& info, const Napi::Value&
2552
+ /// value); Napi::Value DoSomething(const Napi::CallbackInfo& info);
2553
+ /// }
2554
+ template <typename T>
2555
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
2556
+ public:
2557
+ ObjectWrap(const CallbackInfo& callbackInfo);
2558
+ virtual ~ObjectWrap();
2559
+
2560
+ static T* Unwrap(Object wrapper);
2561
+
2562
+ // Methods exposed to JavaScript must conform to one of these callback
2563
+ // signatures.
2564
+ using StaticVoidMethodCallback = void (*)(const CallbackInfo& info);
2565
+ using StaticMethodCallback = Napi::Value (*)(const CallbackInfo& info);
2566
+ using StaticGetterCallback = Napi::Value (*)(const CallbackInfo& info);
2567
+ using StaticSetterCallback = void (*)(const CallbackInfo& info,
2568
+ const Napi::Value& value);
2569
+
2570
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
2571
+
2572
+ static Function DefineClass(
2573
+ Napi::Env env,
2574
+ const char* utf8name,
2575
+ const std::initializer_list<PropertyDescriptor>& properties,
2576
+ void* data = nullptr);
2577
+ static Function DefineClass(Napi::Env env,
2578
+ const char* utf8name,
2579
+ const std::vector<PropertyDescriptor>& properties,
2580
+ void* data = nullptr);
2581
+ static PropertyDescriptor StaticMethod(
2582
+ const char* utf8name,
2583
+ StaticVoidMethodCallback method,
2584
+ napi_property_attributes attributes = napi_default,
2585
+ void* data = nullptr);
2586
+ static PropertyDescriptor StaticMethod(
2587
+ const char* utf8name,
2588
+ StaticMethodCallback method,
2589
+ napi_property_attributes attributes = napi_default,
2590
+ void* data = nullptr);
2591
+ static PropertyDescriptor StaticMethod(
2592
+ Symbol name,
2593
+ StaticVoidMethodCallback method,
2594
+ napi_property_attributes attributes = napi_default,
2595
+ void* data = nullptr);
2596
+ static PropertyDescriptor StaticMethod(
2597
+ Symbol name,
2598
+ StaticMethodCallback method,
2599
+ napi_property_attributes attributes = napi_default,
2600
+ void* data = nullptr);
2601
+ template <StaticVoidMethodCallback method>
2602
+ static PropertyDescriptor StaticMethod(
2603
+ const char* utf8name,
2604
+ napi_property_attributes attributes = napi_default,
2605
+ void* data = nullptr);
2606
+ template <StaticVoidMethodCallback method>
2607
+ static PropertyDescriptor StaticMethod(
2608
+ Symbol name,
2609
+ napi_property_attributes attributes = napi_default,
2610
+ void* data = nullptr);
2611
+ template <StaticMethodCallback method>
2612
+ static PropertyDescriptor StaticMethod(
2613
+ const char* utf8name,
2614
+ napi_property_attributes attributes = napi_default,
2615
+ void* data = nullptr);
2616
+ template <StaticMethodCallback method>
2617
+ static PropertyDescriptor StaticMethod(
2618
+ Symbol name,
2619
+ napi_property_attributes attributes = napi_default,
2620
+ void* data = nullptr);
2621
+ static PropertyDescriptor StaticAccessor(
2622
+ const char* utf8name,
2623
+ StaticGetterCallback getter,
2624
+ StaticSetterCallback setter,
2625
+ napi_property_attributes attributes = napi_default,
2626
+ void* data = nullptr);
2627
+ static PropertyDescriptor StaticAccessor(
2628
+ Symbol name,
2629
+ StaticGetterCallback getter,
2630
+ StaticSetterCallback setter,
2631
+ napi_property_attributes attributes = napi_default,
2632
+ void* data = nullptr);
2633
+ template <StaticGetterCallback getter, StaticSetterCallback setter = nullptr>
2634
+ static PropertyDescriptor StaticAccessor(
2635
+ const char* utf8name,
2636
+ napi_property_attributes attributes = napi_default,
2637
+ void* data = nullptr);
2638
+ template <StaticGetterCallback getter, StaticSetterCallback setter = nullptr>
2639
+ static PropertyDescriptor StaticAccessor(
2640
+ Symbol name,
2641
+ napi_property_attributes attributes = napi_default,
2642
+ void* data = nullptr);
2643
+ static PropertyDescriptor StaticValue(
2644
+ const char* utf8name,
2645
+ Napi::Value value,
2646
+ napi_property_attributes attributes = napi_default);
2647
+ static PropertyDescriptor StaticValue(
2648
+ Symbol name,
2649
+ Napi::Value value,
2650
+ napi_property_attributes attributes = napi_default);
2651
+ static Napi::Value OnCalledAsFunction(const Napi::CallbackInfo& callbackInfo);
2652
+ virtual void Finalize(Napi::Env env);
2653
+ virtual void Finalize(BasicEnv env);
2654
+
2655
+ private:
2656
+ using This = ObjectWrap<T>;
2657
+
2658
+ static napi_value ConstructorCallbackWrapper(napi_env env,
2659
+ napi_callback_info info);
2660
+ static napi_value StaticVoidMethodCallbackWrapper(napi_env env,
2661
+ napi_callback_info info);
2662
+ static napi_value StaticMethodCallbackWrapper(napi_env env,
2663
+ napi_callback_info info);
2664
+ static napi_value StaticGetterCallbackWrapper(napi_env env,
2665
+ napi_callback_info info);
2666
+ static napi_value StaticSetterCallbackWrapper(napi_env env,
2667
+ napi_callback_info info);
2668
+ static void FinalizeCallback(node_addon_api_basic_env env,
2669
+ void* data,
2670
+ void* hint);
2671
+
2672
+ static void PostFinalizeCallback(napi_env env, void* data, void* hint);
2673
+
2674
+ static Function DefineClass(Napi::Env env,
2675
+ const char* utf8name,
2676
+ const size_t props_count,
2677
+ const napi_property_descriptor* props,
2678
+ void* data = nullptr);
2679
+
2680
+ using StaticVoidMethodCallbackData =
2681
+ MethodCallbackData<T, StaticVoidMethodCallback>;
2682
+ using StaticMethodCallbackData = MethodCallbackData<T, StaticMethodCallback>;
2683
+
2684
+ using StaticAccessorCallbackData =
2685
+ AccessorCallbackData<T, StaticGetterCallback, StaticSetterCallback>;
2686
+
2687
+ template <StaticSetterCallback method>
2688
+ static napi_value WrappedMethod(napi_env env,
2689
+ napi_callback_info info) NAPI_NOEXCEPT;
2690
+
2691
+ template <StaticSetterCallback setter>
2692
+ struct StaticSetterTag {};
2693
+
2694
+ template <StaticSetterCallback setter>
2695
+ static napi_callback WrapStaticSetter(StaticSetterTag<setter>) NAPI_NOEXCEPT {
2696
+ return &This::WrappedMethod<setter>;
2697
+ }
2698
+ static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>)
2699
+ NAPI_NOEXCEPT {
2700
+ return nullptr;
2701
+ }
2702
+
2703
+ bool _construction_failed = true;
2704
+ bool _finalized = false;
2705
+ };
2706
+
2707
+ class HandleScope {
2708
+ public:
2709
+ HandleScope(napi_env env, napi_handle_scope scope);
2710
+ explicit HandleScope(Napi::Env env);
2711
+ ~HandleScope();
2712
+
2713
+ // Disallow copying to prevent double close of napi_handle_scope
2714
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
2715
+
2716
+ operator napi_handle_scope() const;
2717
+
2718
+ Napi::Env Env() const;
2719
+
2720
+ private:
2721
+ napi_env _env;
2722
+ napi_handle_scope _scope;
2723
+ };
2724
+
2725
+ class EscapableHandleScope {
2726
+ public:
2727
+ EscapableHandleScope(napi_env env, napi_escapable_handle_scope scope);
2728
+ explicit EscapableHandleScope(Napi::Env env);
2729
+ ~EscapableHandleScope();
2730
+
2731
+ // Disallow copying to prevent double close of napi_escapable_handle_scope
2732
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
2733
+
2734
+ operator napi_escapable_handle_scope() const;
2735
+
2736
+ Napi::Env Env() const;
2737
+ Value Escape(napi_value escapee);
2738
+
2739
+ private:
2740
+ napi_env _env;
2741
+ napi_escapable_handle_scope _scope;
2742
+ };
2743
+
2744
+ #if (NAPI_VERSION > 2)
2745
+ class CallbackScope {
2746
+ public:
2747
+ CallbackScope(napi_env env, napi_callback_scope scope);
2748
+ CallbackScope(napi_env env, napi_async_context context);
2749
+ virtual ~CallbackScope();
2750
+
2751
+ // Disallow copying to prevent double close of napi_callback_scope
2752
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
2753
+
2754
+ operator napi_callback_scope() const;
2755
+
2756
+ Napi::Env Env() const;
2757
+
2758
+ private:
2759
+ napi_env _env;
2760
+ napi_callback_scope _scope;
2761
+ };
2762
+ #endif
2763
+
2764
+ class AsyncContext {
2765
+ public:
2766
+ explicit AsyncContext(napi_env env, const char* resource_name);
2767
+ explicit AsyncContext(napi_env env,
2768
+ const char* resource_name,
2769
+ const Object& resource);
2770
+ virtual ~AsyncContext();
2771
+
2772
+ AsyncContext(AsyncContext&& other);
2773
+ AsyncContext& operator=(AsyncContext&& other);
2774
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
2775
+
2776
+ operator napi_async_context() const;
2777
+
2778
+ Napi::Env Env() const;
2779
+
2780
+ private:
2781
+ napi_env _env;
2782
+ napi_async_context _context;
2783
+ };
2784
+
2785
+ #if NAPI_HAS_THREADS
2786
+ class AsyncWorker {
2787
+ public:
2788
+ virtual ~AsyncWorker();
2789
+
2790
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
2791
+
2792
+ operator napi_async_work() const;
2793
+
2794
+ Napi::Env Env() const;
2795
+
2796
+ void Queue();
2797
+ void Cancel();
2798
+ void SuppressDestruct();
2799
+
2800
+ ObjectReference& Receiver();
2801
+ FunctionReference& Callback();
2802
+
2803
+ virtual void OnExecute(Napi::Env env);
2804
+ virtual void OnWorkComplete(Napi::Env env, napi_status status);
2805
+
2806
+ protected:
2807
+ explicit AsyncWorker(const Function& callback);
2808
+ explicit AsyncWorker(const Function& callback, const char* resource_name);
2809
+ explicit AsyncWorker(const Function& callback,
2810
+ const char* resource_name,
2811
+ const Object& resource);
2812
+ explicit AsyncWorker(const Object& receiver, const Function& callback);
2813
+ explicit AsyncWorker(const Object& receiver,
2814
+ const Function& callback,
2815
+ const char* resource_name);
2816
+ explicit AsyncWorker(const Object& receiver,
2817
+ const Function& callback,
2818
+ const char* resource_name,
2819
+ const Object& resource);
2820
+
2821
+ explicit AsyncWorker(Napi::Env env);
2822
+ explicit AsyncWorker(Napi::Env env, const char* resource_name);
2823
+ explicit AsyncWorker(Napi::Env env,
2824
+ const char* resource_name,
2825
+ const Object& resource);
2826
+
2827
+ virtual void Execute() = 0;
2828
+ virtual void OnOK();
2829
+ virtual void OnError(const Error& e);
2830
+ virtual void Destroy();
2831
+ virtual std::vector<napi_value> GetResult(Napi::Env env);
2832
+
2833
+ void SetError(const std::string& error);
2834
+
2835
+ private:
2836
+ static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
2837
+ static inline void OnAsyncWorkComplete(napi_env env,
2838
+ napi_status status,
2839
+ void* asyncworker);
2840
+
2841
+ napi_env _env;
2842
+ napi_async_work _work;
2843
+ ObjectReference _receiver;
2844
+ FunctionReference _callback;
2845
+ std::string _error;
2846
+ bool _suppress_destruct;
2847
+ };
2848
+ #endif // NAPI_HAS_THREADS
2849
+
2850
+ #if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
2851
+ class ThreadSafeFunction {
2852
+ public:
2853
+ // This API may only be called from the main thread.
2854
+ template <typename ResourceString>
2855
+ static ThreadSafeFunction New(napi_env env,
2856
+ const Function& callback,
2857
+ ResourceString resourceName,
2858
+ size_t maxQueueSize,
2859
+ size_t initialThreadCount);
2860
+
2861
+ // This API may only be called from the main thread.
2862
+ template <typename ResourceString, typename ContextType>
2863
+ static ThreadSafeFunction New(napi_env env,
2864
+ const Function& callback,
2865
+ ResourceString resourceName,
2866
+ size_t maxQueueSize,
2867
+ size_t initialThreadCount,
2868
+ ContextType* context);
2869
+
2870
+ // This API may only be called from the main thread.
2871
+ template <typename ResourceString, typename Finalizer>
2872
+ static ThreadSafeFunction New(napi_env env,
2873
+ const Function& callback,
2874
+ ResourceString resourceName,
2875
+ size_t maxQueueSize,
2876
+ size_t initialThreadCount,
2877
+ Finalizer finalizeCallback);
2878
+
2879
+ // This API may only be called from the main thread.
2880
+ template <typename ResourceString,
2881
+ typename Finalizer,
2882
+ typename FinalizerDataType>
2883
+ static ThreadSafeFunction New(napi_env env,
2884
+ const Function& callback,
2885
+ ResourceString resourceName,
2886
+ size_t maxQueueSize,
2887
+ size_t initialThreadCount,
2888
+ Finalizer finalizeCallback,
2889
+ FinalizerDataType* data);
2890
+
2891
+ // This API may only be called from the main thread.
2892
+ template <typename ResourceString, typename ContextType, typename Finalizer>
2893
+ static ThreadSafeFunction New(napi_env env,
2894
+ const Function& callback,
2895
+ ResourceString resourceName,
2896
+ size_t maxQueueSize,
2897
+ size_t initialThreadCount,
2898
+ ContextType* context,
2899
+ Finalizer finalizeCallback);
2900
+
2901
+ // This API may only be called from the main thread.
2902
+ template <typename ResourceString,
2903
+ typename ContextType,
2904
+ typename Finalizer,
2905
+ typename FinalizerDataType>
2906
+ static ThreadSafeFunction New(napi_env env,
2907
+ const Function& callback,
2908
+ ResourceString resourceName,
2909
+ size_t maxQueueSize,
2910
+ size_t initialThreadCount,
2911
+ ContextType* context,
2912
+ Finalizer finalizeCallback,
2913
+ FinalizerDataType* data);
2914
+
2915
+ // This API may only be called from the main thread.
2916
+ template <typename ResourceString>
2917
+ static ThreadSafeFunction New(napi_env env,
2918
+ const Function& callback,
2919
+ const Object& resource,
2920
+ ResourceString resourceName,
2921
+ size_t maxQueueSize,
2922
+ size_t initialThreadCount);
2923
+
2924
+ // This API may only be called from the main thread.
2925
+ template <typename ResourceString, typename ContextType>
2926
+ static ThreadSafeFunction New(napi_env env,
2927
+ const Function& callback,
2928
+ const Object& resource,
2929
+ ResourceString resourceName,
2930
+ size_t maxQueueSize,
2931
+ size_t initialThreadCount,
2932
+ ContextType* context);
2933
+
2934
+ // This API may only be called from the main thread.
2935
+ template <typename ResourceString, typename Finalizer>
2936
+ static ThreadSafeFunction New(napi_env env,
2937
+ const Function& callback,
2938
+ const Object& resource,
2939
+ ResourceString resourceName,
2940
+ size_t maxQueueSize,
2941
+ size_t initialThreadCount,
2942
+ Finalizer finalizeCallback);
2943
+
2944
+ // This API may only be called from the main thread.
2945
+ template <typename ResourceString,
2946
+ typename Finalizer,
2947
+ typename FinalizerDataType>
2948
+ static ThreadSafeFunction New(napi_env env,
2949
+ const Function& callback,
2950
+ const Object& resource,
2951
+ ResourceString resourceName,
2952
+ size_t maxQueueSize,
2953
+ size_t initialThreadCount,
2954
+ Finalizer finalizeCallback,
2955
+ FinalizerDataType* data);
2956
+
2957
+ // This API may only be called from the main thread.
2958
+ template <typename ResourceString, typename ContextType, typename Finalizer>
2959
+ static ThreadSafeFunction New(napi_env env,
2960
+ const Function& callback,
2961
+ const Object& resource,
2962
+ ResourceString resourceName,
2963
+ size_t maxQueueSize,
2964
+ size_t initialThreadCount,
2965
+ ContextType* context,
2966
+ Finalizer finalizeCallback);
2967
+
2968
+ // This API may only be called from the main thread.
2969
+ template <typename ResourceString,
2970
+ typename ContextType,
2971
+ typename Finalizer,
2972
+ typename FinalizerDataType>
2973
+ static ThreadSafeFunction New(napi_env env,
2974
+ const Function& callback,
2975
+ const Object& resource,
2976
+ ResourceString resourceName,
2977
+ size_t maxQueueSize,
2978
+ size_t initialThreadCount,
2979
+ ContextType* context,
2980
+ Finalizer finalizeCallback,
2981
+ FinalizerDataType* data);
2982
+
2983
+ ThreadSafeFunction();
2984
+ ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
2985
+
2986
+ operator napi_threadsafe_function() const;
2987
+
2988
+ // This API may be called from any thread.
2989
+ napi_status BlockingCall() const;
2990
+
2991
+ // This API may be called from any thread.
2992
+ template <typename Callback>
2993
+ napi_status BlockingCall(Callback callback) const;
2994
+
2995
+ // This API may be called from any thread.
2996
+ template <typename DataType, typename Callback>
2997
+ napi_status BlockingCall(DataType* data, Callback callback) const;
2998
+
2999
+ // This API may be called from any thread.
3000
+ napi_status NonBlockingCall() const;
3001
+
3002
+ // This API may be called from any thread.
3003
+ template <typename Callback>
3004
+ napi_status NonBlockingCall(Callback callback) const;
3005
+
3006
+ // This API may be called from any thread.
3007
+ template <typename DataType, typename Callback>
3008
+ napi_status NonBlockingCall(DataType* data, Callback callback) const;
3009
+
3010
+ // This API may only be called from the main thread.
3011
+ void Ref(napi_env env) const;
3012
+
3013
+ // This API may only be called from the main thread.
3014
+ void Unref(napi_env env) const;
3015
+
3016
+ // This API may be called from any thread.
3017
+ napi_status Acquire() const;
3018
+
3019
+ // This API may be called from any thread.
3020
+ napi_status Release() const;
3021
+
3022
+ // This API may be called from any thread.
3023
+ napi_status Abort() const;
3024
+
3025
+ struct ConvertibleContext {
3026
+ template <class T>
3027
+ operator T*() {
3028
+ return static_cast<T*>(context);
3029
+ }
3030
+ void* context;
3031
+ };
3032
+
3033
+ // This API may be called from any thread.
3034
+ ConvertibleContext GetContext() const;
3035
+
3036
+ private:
3037
+ using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>;
3038
+
3039
+ template <typename ResourceString,
3040
+ typename ContextType,
3041
+ typename Finalizer,
3042
+ typename FinalizerDataType>
3043
+ static ThreadSafeFunction New(napi_env env,
3044
+ const Function& callback,
3045
+ const Object& resource,
3046
+ ResourceString resourceName,
3047
+ size_t maxQueueSize,
3048
+ size_t initialThreadCount,
3049
+ ContextType* context,
3050
+ Finalizer finalizeCallback,
3051
+ FinalizerDataType* data,
3052
+ napi_finalize wrapper);
3053
+
3054
+ napi_status CallInternal(CallbackWrapper* callbackWrapper,
3055
+ napi_threadsafe_function_call_mode mode) const;
3056
+
3057
+ static void CallJS(napi_env env,
3058
+ napi_value jsCallback,
3059
+ void* context,
3060
+ void* data);
3061
+
3062
+ napi_threadsafe_function _tsfn;
3063
+ };
3064
+
3065
+ // A TypedThreadSafeFunction by default has no context (nullptr) and can
3066
+ // accept any type (void) to its CallJs.
3067
+ template <typename ContextType = std::nullptr_t,
3068
+ typename DataType = void,
3069
+ void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) =
3070
+ nullptr>
3071
+ class TypedThreadSafeFunction {
3072
+ public:
3073
+ // This API may only be called from the main thread.
3074
+ // Helper function that returns nullptr if running Node-API 5+, otherwise a
3075
+ // non-empty, no-op Function. This provides the ability to specify at
3076
+ // compile-time a callback parameter to `New` that safely does no action
3077
+ // when targeting _any_ Node-API version.
3078
+ #if NAPI_VERSION > 4
3079
+ static std::nullptr_t EmptyFunctionFactory(Napi::Env env);
3080
+ #else
3081
+ static Napi::Function EmptyFunctionFactory(Napi::Env env);
3082
+ #endif
3083
+ static Napi::Function FunctionOrEmpty(Napi::Env env,
3084
+ Napi::Function& callback);
3085
+
3086
+ #if NAPI_VERSION > 4
3087
+ // This API may only be called from the main thread.
3088
+ // Creates a new threadsafe function with:
3089
+ // Callback [missing] Resource [missing] Finalizer [missing]
3090
+ template <typename ResourceString>
3091
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3092
+ napi_env env,
3093
+ ResourceString resourceName,
3094
+ size_t maxQueueSize,
3095
+ size_t initialThreadCount,
3096
+ ContextType* context = nullptr);
3097
+
3098
+ // This API may only be called from the main thread.
3099
+ // Creates a new threadsafe function with:
3100
+ // Callback [missing] Resource [passed] Finalizer [missing]
3101
+ template <typename ResourceString>
3102
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3103
+ napi_env env,
3104
+ const Object& resource,
3105
+ ResourceString resourceName,
3106
+ size_t maxQueueSize,
3107
+ size_t initialThreadCount,
3108
+ ContextType* context = nullptr);
3109
+
3110
+ // This API may only be called from the main thread.
3111
+ // Creates a new threadsafe function with:
3112
+ // Callback [missing] Resource [missing] Finalizer [passed]
3113
+ template <typename ResourceString,
3114
+ typename Finalizer,
3115
+ typename FinalizerDataType = void>
3116
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3117
+ napi_env env,
3118
+ ResourceString resourceName,
3119
+ size_t maxQueueSize,
3120
+ size_t initialThreadCount,
3121
+ ContextType* context,
3122
+ Finalizer finalizeCallback,
3123
+ FinalizerDataType* data = nullptr);
3124
+
3125
+ // This API may only be called from the main thread.
3126
+ // Creates a new threadsafe function with:
3127
+ // Callback [missing] Resource [passed] Finalizer [passed]
3128
+ template <typename ResourceString,
3129
+ typename Finalizer,
3130
+ typename FinalizerDataType = void>
3131
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3132
+ napi_env env,
3133
+ const Object& resource,
3134
+ ResourceString resourceName,
3135
+ size_t maxQueueSize,
3136
+ size_t initialThreadCount,
3137
+ ContextType* context,
3138
+ Finalizer finalizeCallback,
3139
+ FinalizerDataType* data = nullptr);
3140
+ #endif
3141
+
3142
+ // This API may only be called from the main thread.
3143
+ // Creates a new threadsafe function with:
3144
+ // Callback [passed] Resource [missing] Finalizer [missing]
3145
+ template <typename ResourceString>
3146
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3147
+ napi_env env,
3148
+ const Function& callback,
3149
+ ResourceString resourceName,
3150
+ size_t maxQueueSize,
3151
+ size_t initialThreadCount,
3152
+ ContextType* context = nullptr);
3153
+
3154
+ // This API may only be called from the main thread.
3155
+ // Creates a new threadsafe function with:
3156
+ // Callback [passed] Resource [passed] Finalizer [missing]
3157
+ template <typename ResourceString>
3158
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3159
+ napi_env env,
3160
+ const Function& callback,
3161
+ const Object& resource,
3162
+ ResourceString resourceName,
3163
+ size_t maxQueueSize,
3164
+ size_t initialThreadCount,
3165
+ ContextType* context = nullptr);
3166
+
3167
+ // This API may only be called from the main thread.
3168
+ // Creates a new threadsafe function with:
3169
+ // Callback [passed] Resource [missing] Finalizer [passed]
3170
+ template <typename ResourceString,
3171
+ typename Finalizer,
3172
+ typename FinalizerDataType = void>
3173
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3174
+ napi_env env,
3175
+ const Function& callback,
3176
+ ResourceString resourceName,
3177
+ size_t maxQueueSize,
3178
+ size_t initialThreadCount,
3179
+ ContextType* context,
3180
+ Finalizer finalizeCallback,
3181
+ FinalizerDataType* data = nullptr);
3182
+
3183
+ // This API may only be called from the main thread.
3184
+ // Creates a new threadsafe function with:
3185
+ // Callback [passed] Resource [passed] Finalizer [passed]
3186
+ template <typename CallbackType,
3187
+ typename ResourceString,
3188
+ typename Finalizer,
3189
+ typename FinalizerDataType>
3190
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3191
+ napi_env env,
3192
+ CallbackType callback,
3193
+ const Object& resource,
3194
+ ResourceString resourceName,
3195
+ size_t maxQueueSize,
3196
+ size_t initialThreadCount,
3197
+ ContextType* context,
3198
+ Finalizer finalizeCallback,
3199
+ FinalizerDataType* data = nullptr);
3200
+
3201
+ TypedThreadSafeFunction();
3202
+ TypedThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
3203
+
3204
+ operator napi_threadsafe_function() const;
3205
+
3206
+ // This API may be called from any thread.
3207
+ napi_status BlockingCall(DataType* data = nullptr) const;
3208
+
3209
+ // This API may be called from any thread.
3210
+ napi_status NonBlockingCall(DataType* data = nullptr) const;
3211
+
3212
+ // This API may only be called from the main thread.
3213
+ void Ref(napi_env env) const;
3214
+
3215
+ // This API may only be called from the main thread.
3216
+ void Unref(napi_env env) const;
3217
+
3218
+ // This API may be called from any thread.
3219
+ napi_status Acquire() const;
3220
+
3221
+ // This API may be called from any thread.
3222
+ napi_status Release() const;
3223
+
3224
+ // This API may be called from any thread.
3225
+ napi_status Abort() const;
3226
+
3227
+ // This API may be called from any thread.
3228
+ ContextType* GetContext() const;
3229
+
3230
+ private:
3231
+ template <typename ResourceString,
3232
+ typename Finalizer,
3233
+ typename FinalizerDataType>
3234
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
3235
+ napi_env env,
3236
+ const Function& callback,
3237
+ const Object& resource,
3238
+ ResourceString resourceName,
3239
+ size_t maxQueueSize,
3240
+ size_t initialThreadCount,
3241
+ ContextType* context,
3242
+ Finalizer finalizeCallback,
3243
+ FinalizerDataType* data,
3244
+ napi_finalize wrapper);
3245
+
3246
+ static void CallJsInternal(napi_env env,
3247
+ napi_value jsCallback,
3248
+ void* context,
3249
+ void* data);
3250
+
3251
+ protected:
3252
+ napi_threadsafe_function _tsfn;
3253
+ };
3254
+ template <typename DataType>
3255
+ class AsyncProgressWorkerBase : public AsyncWorker {
3256
+ public:
3257
+ virtual void OnWorkProgress(DataType* data) = 0;
3258
+ class ThreadSafeData {
3259
+ public:
3260
+ ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
3261
+ : _asyncprogressworker(asyncprogressworker), _data(data) {}
3262
+
3263
+ AsyncProgressWorkerBase* asyncprogressworker() {
3264
+ return _asyncprogressworker;
3265
+ }
3266
+ DataType* data() { return _data; }
3267
+
3268
+ private:
3269
+ AsyncProgressWorkerBase* _asyncprogressworker;
3270
+ DataType* _data;
3271
+ };
3272
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
3273
+
3274
+ protected:
3275
+ explicit AsyncProgressWorkerBase(const Object& receiver,
3276
+ const Function& callback,
3277
+ const char* resource_name,
3278
+ const Object& resource,
3279
+ size_t queue_size = 1);
3280
+ virtual ~AsyncProgressWorkerBase();
3281
+
3282
+ // Optional callback of Napi::ThreadSafeFunction only available after
3283
+ // NAPI_VERSION 4. Refs: https://github.com/nodejs/node/pull/27791
3284
+ #if NAPI_VERSION > 4
3285
+ explicit AsyncProgressWorkerBase(Napi::Env env,
3286
+ const char* resource_name,
3287
+ const Object& resource,
3288
+ size_t queue_size = 1);
3289
+ #endif
3290
+
3291
+ static inline void OnAsyncWorkProgress(Napi::Env env,
3292
+ Napi::Function jsCallback,
3293
+ void* data);
3294
+
3295
+ napi_status NonBlockingCall(DataType* data);
3296
+
3297
+ private:
3298
+ ThreadSafeFunction _tsfn;
3299
+ bool _work_completed = false;
3300
+ napi_status _complete_status;
3301
+ static inline void OnThreadSafeFunctionFinalize(
3302
+ Napi::Env env, void* data, AsyncProgressWorkerBase* context);
3303
+ };
3304
+
3305
+ template <class T>
3306
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
3307
+ public:
3308
+ virtual ~AsyncProgressWorker();
3309
+
3310
+ class ExecutionProgress {
3311
+ friend class AsyncProgressWorker;
3312
+
3313
+ public:
3314
+ void Signal() const;
3315
+ void Send(const T* data, size_t count) const;
3316
+
3317
+ private:
3318
+ explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {}
3319
+ AsyncProgressWorker* const _worker;
3320
+ };
3321
+
3322
+ void OnWorkProgress(void*) override;
3323
+
3324
+ protected:
3325
+ explicit AsyncProgressWorker(const Function& callback);
3326
+ explicit AsyncProgressWorker(const Function& callback,
3327
+ const char* resource_name);
3328
+ explicit AsyncProgressWorker(const Function& callback,
3329
+ const char* resource_name,
3330
+ const Object& resource);
3331
+ explicit AsyncProgressWorker(const Object& receiver,
3332
+ const Function& callback);
3333
+ explicit AsyncProgressWorker(const Object& receiver,
3334
+ const Function& callback,
3335
+ const char* resource_name);
3336
+ explicit AsyncProgressWorker(const Object& receiver,
3337
+ const Function& callback,
3338
+ const char* resource_name,
3339
+ const Object& resource);
3340
+
3341
+ // Optional callback of Napi::ThreadSafeFunction only available after
3342
+ // NAPI_VERSION 4. Refs: https://github.com/nodejs/node/pull/27791
3343
+ #if NAPI_VERSION > 4
3344
+ explicit AsyncProgressWorker(Napi::Env env);
3345
+ explicit AsyncProgressWorker(Napi::Env env, const char* resource_name);
3346
+ explicit AsyncProgressWorker(Napi::Env env,
3347
+ const char* resource_name,
3348
+ const Object& resource);
3349
+ #endif
3350
+ virtual void Execute(const ExecutionProgress& progress) = 0;
3351
+ virtual void OnProgress(const T* data, size_t count) = 0;
3352
+
3353
+ private:
3354
+ void Execute() override;
3355
+ void Signal();
3356
+ void SendProgress_(const T* data, size_t count);
3357
+
3358
+ std::mutex _mutex;
3359
+ T* _asyncdata;
3360
+ size_t _asyncsize;
3361
+ bool _signaled;
3362
+ };
3363
+
3364
+ template <class T>
3365
+ class AsyncProgressQueueWorker
3366
+ : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
3367
+ public:
3368
+ virtual ~AsyncProgressQueueWorker(){};
3369
+
3370
+ class ExecutionProgress {
3371
+ friend class AsyncProgressQueueWorker;
3372
+
3373
+ public:
3374
+ void Signal() const;
3375
+ void Send(const T* data, size_t count) const;
3376
+
3377
+ private:
3378
+ explicit ExecutionProgress(AsyncProgressQueueWorker* worker)
3379
+ : _worker(worker) {}
3380
+ AsyncProgressQueueWorker* const _worker;
3381
+ };
3382
+
3383
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
3384
+ void OnWorkProgress(std::pair<T*, size_t>*) override;
3385
+
3386
+ protected:
3387
+ explicit AsyncProgressQueueWorker(const Function& callback);
3388
+ explicit AsyncProgressQueueWorker(const Function& callback,
3389
+ const char* resource_name);
3390
+ explicit AsyncProgressQueueWorker(const Function& callback,
3391
+ const char* resource_name,
3392
+ const Object& resource);
3393
+ explicit AsyncProgressQueueWorker(const Object& receiver,
3394
+ const Function& callback);
3395
+ explicit AsyncProgressQueueWorker(const Object& receiver,
3396
+ const Function& callback,
3397
+ const char* resource_name);
3398
+ explicit AsyncProgressQueueWorker(const Object& receiver,
3399
+ const Function& callback,
3400
+ const char* resource_name,
3401
+ const Object& resource);
3402
+
3403
+ // Optional callback of Napi::ThreadSafeFunction only available after
3404
+ // NAPI_VERSION 4. Refs: https://github.com/nodejs/node/pull/27791
3405
+ #if NAPI_VERSION > 4
3406
+ explicit AsyncProgressQueueWorker(Napi::Env env);
3407
+ explicit AsyncProgressQueueWorker(Napi::Env env, const char* resource_name);
3408
+ explicit AsyncProgressQueueWorker(Napi::Env env,
3409
+ const char* resource_name,
3410
+ const Object& resource);
3411
+ #endif
3412
+ virtual void Execute(const ExecutionProgress& progress) = 0;
3413
+ virtual void OnProgress(const T* data, size_t count) = 0;
3414
+
3415
+ private:
3416
+ void Execute() override;
3417
+ void Signal() const;
3418
+ void SendProgress_(const T* data, size_t count);
3419
+ };
3420
+ #endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
3421
+
3422
+ // Memory management.
3423
+ class MemoryManagement {
3424
+ public:
3425
+ static int64_t AdjustExternalMemory(BasicEnv env, int64_t change_in_bytes);
3426
+ };
3427
+
3428
+ // Version management
3429
+ class VersionManagement {
3430
+ public:
3431
+ static uint32_t GetNapiVersion(BasicEnv env);
3432
+ static const napi_node_version* GetNodeVersion(BasicEnv env);
3433
+ };
3434
+
3435
+ #if NAPI_VERSION > 5
3436
+ template <typename T>
3437
+ class Addon : public InstanceWrap<T> {
3438
+ public:
3439
+ static inline Object Init(Env env, Object exports);
3440
+ static T* Unwrap(Object wrapper);
3441
+
3442
+ protected:
3443
+ using AddonProp = ClassPropertyDescriptor<T>;
3444
+ void DefineAddon(Object exports,
3445
+ const std::initializer_list<AddonProp>& props);
3446
+ Napi::Object DefineProperties(Object object,
3447
+ const std::initializer_list<AddonProp>& props);
3448
+
3449
+ private:
3450
+ Object entry_point_;
3451
+ };
3452
+ #endif // NAPI_VERSION > 5
3453
+
3454
+ #ifdef NAPI_CPP_CUSTOM_NAMESPACE
3455
+ } // namespace NAPI_CPP_CUSTOM_NAMESPACE
3456
+ #endif
3457
+
3458
+ } // namespace Napi
3459
+
3460
+ // Inline implementations of all the above class methods are included here.
3461
+ #include "napi-inl.h"
3462
+
3463
+ #endif // SRC_NAPI_H_