koffi 0.9.1 → 0.9.2

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 (384) hide show
  1. package/CMakeLists.txt +2 -36
  2. package/build/ALL_BUILD.vcxproj +4 -8
  3. package/build/CMakeCache.txt +1 -13
  4. package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_C.bin +0 -0
  5. package/build/CMakeFiles/3.23.0-rc1/CMakeDetermineCompilerABI_CXX.bin +0 -0
  6. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/CompilerIdC.exe +0 -0
  7. package/build/CMakeFiles/3.23.0-rc1/CompilerIdC/Debug/CMakeCCompilerId.obj +0 -0
  8. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/CompilerIdCXX.exe +0 -0
  9. package/build/CMakeFiles/3.23.0-rc1/CompilerIdCXX/Debug/CMakeCXXCompilerId.obj +0 -0
  10. package/build/CMakeFiles/CMakeOutput.log +14 -14
  11. package/build/CMakeFiles/TargetDirectories.txt +0 -1
  12. package/build/CMakeFiles/generate.stamp.depend +81 -0
  13. package/build/Release/koffi.node +0 -0
  14. package/build/ZERO_CHECK.vcxproj +4 -4
  15. package/build/cmake_install.cmake +1 -1
  16. package/build/koffi.dir/Release/call_arm64.obj +0 -0
  17. package/build/koffi.dir/Release/call_x64_sysv.obj +0 -0
  18. package/build/koffi.dir/Release/call_x64_win.obj +0 -0
  19. package/build/koffi.dir/Release/call_x64_win_fwd.obj +0 -0
  20. package/build/koffi.dir/Release/call_x86.obj +0 -0
  21. package/build/koffi.dir/Release/ffi.obj +0 -0
  22. package/build/koffi.dir/Release/koffi.tlog/CL.command.1.tlog +0 -0
  23. package/build/koffi.dir/Release/koffi.tlog/CL.read.1.tlog +0 -0
  24. package/build/koffi.dir/Release/koffi.tlog/CL.write.1.tlog +0 -0
  25. package/build/koffi.dir/Release/koffi.tlog/CustomBuild.read.1.tlog +81 -0
  26. package/build/koffi.dir/Release/koffi.tlog/koffi.write.1u.tlog +0 -0
  27. package/build/koffi.dir/Release/libcc.obj +0 -0
  28. package/build/koffi.dir/Release/util.obj +0 -0
  29. package/build/koffi.dir/Release/win_delay_load_hook.obj +0 -0
  30. package/build/koffi.sln +0 -14
  31. package/build/koffi.vcxproj +21 -21
  32. package/build/koffi.vcxproj.filters +1 -1
  33. package/build/x64/Release/ALL_BUILD/ALL_BUILD.recipe +0 -3
  34. package/build/x64/Release/ALL_BUILD/ALL_BUILD.tlog/CustomBuild.read.1.tlog +81 -0
  35. package/build/x64/Release/ZERO_CHECK/ZERO_CHECK.tlog/CustomBuild.read.1.tlog +81 -0
  36. package/package.json +1 -1
  37. package/vendor/dragonbox/CMakeLists.txt +123 -0
  38. package/vendor/dragonbox/LICENSE-Apache2-LLVM +218 -0
  39. package/vendor/dragonbox/LICENSE-Boost +23 -0
  40. package/vendor/dragonbox/README.md +277 -0
  41. package/vendor/dragonbox/cmake/dragonboxConfig.cmake +1 -0
  42. package/vendor/dragonbox/include/dragonbox/dragonbox.h +2670 -0
  43. package/vendor/dragonbox/include/dragonbox/dragonbox_to_chars.h +108 -0
  44. package/vendor/dragonbox/other_files/Dragonbox.pdf +0 -0
  45. package/vendor/dragonbox/other_files/Dragonbox_old.pdf +0 -0
  46. package/vendor/dragonbox/other_files/milo_benchmark.png +0 -0
  47. package/vendor/dragonbox/other_files/unknown_win64_vc2019.html +540 -0
  48. package/vendor/dragonbox/other_files/unknown_win64_vc2019_randomdigit_time.png +0 -0
  49. package/vendor/dragonbox/source/dragonbox_to_chars.cpp +303 -0
  50. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/CMakeLists.txt +24 -0
  51. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.cpp +238 -0
  52. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/fp_to_chars.h +95 -0
  53. package/vendor/dragonbox/subproject/3rdparty/grisu_exact/grisu_exact.h +2666 -0
  54. package/vendor/dragonbox/subproject/3rdparty/ryu/CMakeLists.txt +16 -0
  55. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/common.h +114 -0
  56. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s.c +509 -0
  57. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_full_table.h +367 -0
  58. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/d2s_intrinsics.h +357 -0
  59. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/digit_table.h +35 -0
  60. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s.c +345 -0
  61. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_full_table.h +55 -0
  62. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/f2s_intrinsics.h +128 -0
  63. package/vendor/dragonbox/subproject/3rdparty/ryu/ryu/ryu.h +46 -0
  64. package/vendor/dragonbox/subproject/3rdparty/schubfach/CMakeLists.txt +22 -0
  65. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.cc +699 -0
  66. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_32.h +31 -0
  67. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.cc +1354 -0
  68. package/vendor/dragonbox/subproject/3rdparty/schubfach/schubfach_64.h +31 -0
  69. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/example_shaded_plots.m +68 -0
  70. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/license.txt +25 -0
  71. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution.m +92 -0
  72. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_distribution_prctile.m +121 -0
  73. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_histogram_shaded.m +99 -0
  74. package/vendor/dragonbox/subproject/3rdparty/shaded_plots/plot_shaded.m +93 -0
  75. package/vendor/dragonbox/subproject/benchmark/CMakeLists.txt +65 -0
  76. package/vendor/dragonbox/subproject/benchmark/include/benchmark.h +40 -0
  77. package/vendor/dragonbox/subproject/benchmark/matlab/plot_benchmarks.m +22 -0
  78. package/vendor/dragonbox/subproject/benchmark/matlab/plot_digit_benchmark.m +78 -0
  79. package/vendor/dragonbox/subproject/benchmark/matlab/plot_uniform_benchmark.m +95 -0
  80. package/vendor/dragonbox/subproject/benchmark/results/digits_benchmark_binary32_clang.png +0 -0
  81. package/vendor/dragonbox/subproject/benchmark/results/digits_benchmark_binary32_msvc.png +0 -0
  82. package/vendor/dragonbox/subproject/benchmark/results/digits_benchmark_binary64_clang.png +0 -0
  83. package/vendor/dragonbox/subproject/benchmark/results/digits_benchmark_binary64_msvc.png +0 -0
  84. package/vendor/dragonbox/subproject/benchmark/results/uniform_benchmark_binary32_clang.png +0 -0
  85. package/vendor/dragonbox/subproject/benchmark/results/uniform_benchmark_binary32_msvc.png +0 -0
  86. package/vendor/dragonbox/subproject/benchmark/results/uniform_benchmark_binary64_clang.png +0 -0
  87. package/vendor/dragonbox/subproject/benchmark/results/uniform_benchmark_binary64_msvc.png +0 -0
  88. package/vendor/dragonbox/subproject/benchmark/source/benchmark.cpp +238 -0
  89. package/vendor/dragonbox/subproject/benchmark/source/dragonbox.cpp +30 -0
  90. package/vendor/dragonbox/subproject/benchmark/source/grisu_exact.cpp +36 -0
  91. package/vendor/dragonbox/subproject/benchmark/source/ryu.cpp +27 -0
  92. package/vendor/dragonbox/subproject/benchmark/source/schubfach.cpp +31 -0
  93. package/vendor/dragonbox/subproject/common/CMakeLists.txt +42 -0
  94. package/vendor/dragonbox/subproject/common/include/best_rational_approx.h +97 -0
  95. package/vendor/dragonbox/subproject/common/include/big_uint.h +218 -0
  96. package/vendor/dragonbox/subproject/common/include/continued_fractions.h +174 -0
  97. package/vendor/dragonbox/subproject/common/include/good_rational_approx.h +267 -0
  98. package/vendor/dragonbox/subproject/common/include/random_float.h +182 -0
  99. package/vendor/dragonbox/subproject/common/include/rational_continued_fractions.h +57 -0
  100. package/vendor/dragonbox/subproject/common/source/big_uint.cpp +602 -0
  101. package/vendor/dragonbox/subproject/meta/CMakeLists.txt +41 -0
  102. package/vendor/dragonbox/subproject/meta/results/binary32_generated_cache.txt +82 -0
  103. package/vendor/dragonbox/subproject/meta/results/binary64_compressed_cache_error_table.txt +10 -0
  104. package/vendor/dragonbox/subproject/meta/results/binary64_generated_cache.txt +623 -0
  105. package/vendor/dragonbox/subproject/meta/source/generate_cache.cpp +126 -0
  106. package/vendor/dragonbox/subproject/meta/source/live_test.cpp +81 -0
  107. package/vendor/dragonbox/subproject/meta/source/perf_test.cpp +104 -0
  108. package/vendor/dragonbox/subproject/meta/source/sandbox.cpp +20 -0
  109. package/vendor/dragonbox/subproject/test/CMakeLists.txt +70 -0
  110. package/vendor/dragonbox/subproject/test/results/binary32.csv +255 -0
  111. package/vendor/dragonbox/subproject/test/results/binary64.csv +2047 -0
  112. package/vendor/dragonbox/subproject/test/results/plot_required_bits.m +18 -0
  113. package/vendor/dragonbox/subproject/test/source/test_all_shorter_interval_cases.cpp +88 -0
  114. package/vendor/dragonbox/subproject/test/source/uniform_random_test.cpp +95 -0
  115. package/vendor/dragonbox/subproject/test/source/verify_cache_precision.cpp +338 -0
  116. package/vendor/dragonbox/subproject/test/source/verify_compressed_cache.cpp +154 -0
  117. package/vendor/dragonbox/subproject/test/source/verify_fast_multiplication.cpp +168 -0
  118. package/vendor/dragonbox/subproject/test/source/verify_log_computation.cpp +251 -0
  119. package/vendor/dragonbox/subproject/test/source/verify_magic_division.cpp +113 -0
  120. package/vendor/libcc/libcc.cc +7651 -0
  121. package/vendor/libcc/libcc.hh +4312 -0
  122. package/vendor/node-addon-api/CHANGELOG.md +859 -0
  123. package/vendor/node-addon-api/CODE_OF_CONDUCT.md +4 -0
  124. package/vendor/node-addon-api/CONTRIBUTING.md +93 -0
  125. package/vendor/node-addon-api/LICENSE.md +13 -0
  126. package/vendor/node-addon-api/README.md +293 -0
  127. package/vendor/node-addon-api/appveyor.yml +37 -0
  128. package/vendor/node-addon-api/benchmark/README.md +47 -0
  129. package/vendor/node-addon-api/benchmark/binding.gyp +25 -0
  130. package/vendor/node-addon-api/benchmark/function_args.cc +217 -0
  131. package/vendor/node-addon-api/benchmark/function_args.js +60 -0
  132. package/vendor/node-addon-api/benchmark/index.js +34 -0
  133. package/vendor/node-addon-api/benchmark/property_descriptor.cc +91 -0
  134. package/vendor/node-addon-api/benchmark/property_descriptor.js +37 -0
  135. package/vendor/node-addon-api/common.gypi +21 -0
  136. package/vendor/node-addon-api/doc/addon.md +163 -0
  137. package/vendor/node-addon-api/doc/array.md +81 -0
  138. package/vendor/node-addon-api/doc/array_buffer.md +155 -0
  139. package/vendor/node-addon-api/doc/async_context.md +86 -0
  140. package/vendor/node-addon-api/doc/async_operations.md +31 -0
  141. package/vendor/node-addon-api/doc/async_worker.md +427 -0
  142. package/vendor/node-addon-api/doc/async_worker_variants.md +557 -0
  143. package/vendor/node-addon-api/doc/bigint.md +97 -0
  144. package/vendor/node-addon-api/doc/boolean.md +68 -0
  145. package/vendor/node-addon-api/doc/buffer.md +150 -0
  146. package/vendor/node-addon-api/doc/callback_scope.md +54 -0
  147. package/vendor/node-addon-api/doc/callbackinfo.md +97 -0
  148. package/vendor/node-addon-api/doc/checker-tool.md +32 -0
  149. package/vendor/node-addon-api/doc/class_property_descriptor.md +115 -0
  150. package/vendor/node-addon-api/doc/cmake-js.md +68 -0
  151. package/vendor/node-addon-api/doc/conversion-tool.md +28 -0
  152. package/vendor/node-addon-api/doc/creating_a_release.md +62 -0
  153. package/vendor/node-addon-api/doc/dataview.md +248 -0
  154. package/vendor/node-addon-api/doc/date.md +68 -0
  155. package/vendor/node-addon-api/doc/env.md +196 -0
  156. package/vendor/node-addon-api/doc/error.md +120 -0
  157. package/vendor/node-addon-api/doc/error_handling.md +254 -0
  158. package/vendor/node-addon-api/doc/escapable_handle_scope.md +80 -0
  159. package/vendor/node-addon-api/doc/external.md +63 -0
  160. package/vendor/node-addon-api/doc/function.md +402 -0
  161. package/vendor/node-addon-api/doc/function_reference.md +238 -0
  162. package/vendor/node-addon-api/doc/generator.md +13 -0
  163. package/vendor/node-addon-api/doc/handle_scope.md +63 -0
  164. package/vendor/node-addon-api/doc/hierarchy.md +91 -0
  165. package/vendor/node-addon-api/doc/instance_wrap.md +408 -0
  166. package/vendor/node-addon-api/doc/maybe.md +76 -0
  167. package/vendor/node-addon-api/doc/memory_management.md +27 -0
  168. package/vendor/node-addon-api/doc/name.md +29 -0
  169. package/vendor/node-addon-api/doc/node-gyp.md +82 -0
  170. package/vendor/node-addon-api/doc/number.md +163 -0
  171. package/vendor/node-addon-api/doc/object.md +432 -0
  172. package/vendor/node-addon-api/doc/object_lifetime_management.md +83 -0
  173. package/vendor/node-addon-api/doc/object_reference.md +117 -0
  174. package/vendor/node-addon-api/doc/object_wrap.md +561 -0
  175. package/vendor/node-addon-api/doc/prebuild_tools.md +16 -0
  176. package/vendor/node-addon-api/doc/promises.md +79 -0
  177. package/vendor/node-addon-api/doc/property_descriptor.md +286 -0
  178. package/vendor/node-addon-api/doc/propertylvalue.md +50 -0
  179. package/vendor/node-addon-api/doc/range_error.md +59 -0
  180. package/vendor/node-addon-api/doc/reference.md +113 -0
  181. package/vendor/node-addon-api/doc/setup.md +110 -0
  182. package/vendor/node-addon-api/doc/string.md +93 -0
  183. package/vendor/node-addon-api/doc/symbol.md +61 -0
  184. package/vendor/node-addon-api/doc/threadsafe.md +121 -0
  185. package/vendor/node-addon-api/doc/threadsafe_function.md +290 -0
  186. package/vendor/node-addon-api/doc/type_error.md +59 -0
  187. package/vendor/node-addon-api/doc/typed_array.md +78 -0
  188. package/vendor/node-addon-api/doc/typed_array_of.md +137 -0
  189. package/vendor/node-addon-api/doc/typed_threadsafe_function.md +306 -0
  190. package/vendor/node-addon-api/doc/value.md +340 -0
  191. package/vendor/node-addon-api/doc/version_management.md +43 -0
  192. package/vendor/node-addon-api/except.gypi +25 -0
  193. package/vendor/node-addon-api/index.js +11 -0
  194. package/vendor/node-addon-api/napi-inl.deprecated.h +192 -0
  195. package/vendor/node-addon-api/napi-inl.h +6209 -0
  196. package/vendor/node-addon-api/napi.h +2983 -0
  197. package/vendor/node-addon-api/node_api.gyp +9 -0
  198. package/vendor/node-addon-api/noexcept.gypi +26 -0
  199. package/vendor/node-addon-api/nothing.c +0 -0
  200. package/vendor/node-addon-api/package-support.json +21 -0
  201. package/vendor/node-addon-api/package.json +399 -0
  202. package/vendor/node-addon-api/test/README.md +91 -0
  203. package/vendor/node-addon-api/test/addon.cc +36 -0
  204. package/vendor/node-addon-api/test/addon.js +11 -0
  205. package/vendor/node-addon-api/test/addon_build/index.js +49 -0
  206. package/vendor/node-addon-api/test/addon_build/tpl/addon.cc +17 -0
  207. package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +62 -0
  208. package/vendor/node-addon-api/test/addon_build/tpl/index.js +9 -0
  209. package/vendor/node-addon-api/test/addon_build/tpl/package.json +11 -0
  210. package/vendor/node-addon-api/test/addon_data.cc +99 -0
  211. package/vendor/node-addon-api/test/addon_data.js +46 -0
  212. package/vendor/node-addon-api/test/array_buffer.cc +243 -0
  213. package/vendor/node-addon-api/test/array_buffer.js +69 -0
  214. package/vendor/node-addon-api/test/async_context.cc +21 -0
  215. package/vendor/node-addon-api/test/async_context.js +86 -0
  216. package/vendor/node-addon-api/test/async_progress_queue_worker.cc +83 -0
  217. package/vendor/node-addon-api/test/async_progress_queue_worker.js +46 -0
  218. package/vendor/node-addon-api/test/async_progress_worker.cc +134 -0
  219. package/vendor/node-addon-api/test/async_progress_worker.js +61 -0
  220. package/vendor/node-addon-api/test/async_worker.cc +106 -0
  221. package/vendor/node-addon-api/test/async_worker.js +179 -0
  222. package/vendor/node-addon-api/test/async_worker_nocallback.js +13 -0
  223. package/vendor/node-addon-api/test/async_worker_persistent.cc +63 -0
  224. package/vendor/node-addon-api/test/async_worker_persistent.js +24 -0
  225. package/vendor/node-addon-api/test/basic_types/array.cc +40 -0
  226. package/vendor/node-addon-api/test/basic_types/array.js +35 -0
  227. package/vendor/node-addon-api/test/basic_types/boolean.cc +38 -0
  228. package/vendor/node-addon-api/test/basic_types/boolean.js +35 -0
  229. package/vendor/node-addon-api/test/basic_types/number.cc +99 -0
  230. package/vendor/node-addon-api/test/basic_types/number.js +114 -0
  231. package/vendor/node-addon-api/test/basic_types/value.cc +120 -0
  232. package/vendor/node-addon-api/test/basic_types/value.js +133 -0
  233. package/vendor/node-addon-api/test/bigint.cc +91 -0
  234. package/vendor/node-addon-api/test/bigint.js +53 -0
  235. package/vendor/node-addon-api/test/binding-swallowexcept.cc +12 -0
  236. package/vendor/node-addon-api/test/binding.cc +171 -0
  237. package/vendor/node-addon-api/test/binding.gyp +117 -0
  238. package/vendor/node-addon-api/test/buffer.cc +183 -0
  239. package/vendor/node-addon-api/test/buffer.js +69 -0
  240. package/vendor/node-addon-api/test/callbackscope.cc +22 -0
  241. package/vendor/node-addon-api/test/callbackscope.js +49 -0
  242. package/vendor/node-addon-api/test/common/index.js +113 -0
  243. package/vendor/node-addon-api/test/common/test_helper.h +61 -0
  244. package/vendor/node-addon-api/test/dataview/dataview.cc +48 -0
  245. package/vendor/node-addon-api/test/dataview/dataview.js +35 -0
  246. package/vendor/node-addon-api/test/dataview/dataview_read_write.cc +115 -0
  247. package/vendor/node-addon-api/test/dataview/dataview_read_write.js +90 -0
  248. package/vendor/node-addon-api/test/date.cc +44 -0
  249. package/vendor/node-addon-api/test/date.js +18 -0
  250. package/vendor/node-addon-api/test/env_cleanup.cc +88 -0
  251. package/vendor/node-addon-api/test/env_cleanup.js +56 -0
  252. package/vendor/node-addon-api/test/error.cc +287 -0
  253. package/vendor/node-addon-api/test/error.js +81 -0
  254. package/vendor/node-addon-api/test/error_handling_for_primitives.cc +13 -0
  255. package/vendor/node-addon-api/test/error_handling_for_primitives.js +29 -0
  256. package/vendor/node-addon-api/test/error_terminating_environment.js +94 -0
  257. package/vendor/node-addon-api/test/external.cc +81 -0
  258. package/vendor/node-addon-api/test/external.js +88 -0
  259. package/vendor/node-addon-api/test/function.cc +295 -0
  260. package/vendor/node-addon-api/test/function.js +121 -0
  261. package/vendor/node-addon-api/test/function_reference.cc +202 -0
  262. package/vendor/node-addon-api/test/function_reference.js +157 -0
  263. package/vendor/node-addon-api/test/globalObject/global_object.cc +61 -0
  264. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +31 -0
  265. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.js +61 -0
  266. package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +40 -0
  267. package/vendor/node-addon-api/test/globalObject/global_object_get_property.js +57 -0
  268. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +28 -0
  269. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.js +48 -0
  270. package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +31 -0
  271. package/vendor/node-addon-api/test/globalObject/global_object_set_property.js +58 -0
  272. package/vendor/node-addon-api/test/handlescope.cc +60 -0
  273. package/vendor/node-addon-api/test/handlescope.js +14 -0
  274. package/vendor/node-addon-api/test/index.js +136 -0
  275. package/vendor/node-addon-api/test/maybe/check.cc +23 -0
  276. package/vendor/node-addon-api/test/maybe/index.js +38 -0
  277. package/vendor/node-addon-api/test/memory_management.cc +17 -0
  278. package/vendor/node-addon-api/test/memory_management.js +9 -0
  279. package/vendor/node-addon-api/test/movable_callbacks.cc +23 -0
  280. package/vendor/node-addon-api/test/movable_callbacks.js +21 -0
  281. package/vendor/node-addon-api/test/name.cc +108 -0
  282. package/vendor/node-addon-api/test/name.js +59 -0
  283. package/vendor/node-addon-api/test/napi_child.js +14 -0
  284. package/vendor/node-addon-api/test/object/delete_property.cc +38 -0
  285. package/vendor/node-addon-api/test/object/delete_property.js +41 -0
  286. package/vendor/node-addon-api/test/object/finalizer.cc +29 -0
  287. package/vendor/node-addon-api/test/object/finalizer.js +28 -0
  288. package/vendor/node-addon-api/test/object/get_property.cc +34 -0
  289. package/vendor/node-addon-api/test/object/get_property.js +40 -0
  290. package/vendor/node-addon-api/test/object/has_own_property.cc +34 -0
  291. package/vendor/node-addon-api/test/object/has_own_property.js +34 -0
  292. package/vendor/node-addon-api/test/object/has_property.cc +38 -0
  293. package/vendor/node-addon-api/test/object/has_property.js +37 -0
  294. package/vendor/node-addon-api/test/object/object.cc +348 -0
  295. package/vendor/node-addon-api/test/object/object.js +217 -0
  296. package/vendor/node-addon-api/test/object/object_deprecated.cc +66 -0
  297. package/vendor/node-addon-api/test/object/object_deprecated.js +47 -0
  298. package/vendor/node-addon-api/test/object/object_freeze_seal.cc +25 -0
  299. package/vendor/node-addon-api/test/object/object_freeze_seal.js +61 -0
  300. package/vendor/node-addon-api/test/object/set_property.cc +37 -0
  301. package/vendor/node-addon-api/test/object/set_property.js +29 -0
  302. package/vendor/node-addon-api/test/object/subscript_operator.cc +42 -0
  303. package/vendor/node-addon-api/test/object/subscript_operator.js +17 -0
  304. package/vendor/node-addon-api/test/object_reference.cc +219 -0
  305. package/vendor/node-addon-api/test/object_reference.js +259 -0
  306. package/vendor/node-addon-api/test/objectwrap.cc +268 -0
  307. package/vendor/node-addon-api/test/objectwrap.js +284 -0
  308. package/vendor/node-addon-api/test/objectwrap_constructor_exception.cc +26 -0
  309. package/vendor/node-addon-api/test/objectwrap_constructor_exception.js +18 -0
  310. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.cc +30 -0
  311. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.js +13 -0
  312. package/vendor/node-addon-api/test/objectwrap_removewrap.cc +45 -0
  313. package/vendor/node-addon-api/test/objectwrap_removewrap.js +40 -0
  314. package/vendor/node-addon-api/test/objectwrap_worker_thread.js +19 -0
  315. package/vendor/node-addon-api/test/promise.cc +29 -0
  316. package/vendor/node-addon-api/test/promise.js +18 -0
  317. package/vendor/node-addon-api/test/reference.cc +24 -0
  318. package/vendor/node-addon-api/test/reference.js +14 -0
  319. package/vendor/node-addon-api/test/run_script.cc +56 -0
  320. package/vendor/node-addon-api/test/run_script.js +45 -0
  321. package/vendor/node-addon-api/test/symbol.cc +79 -0
  322. package/vendor/node-addon-api/test/symbol.js +73 -0
  323. package/vendor/node-addon-api/test/testUtil.js +54 -0
  324. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.cc +195 -0
  325. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.js +188 -0
  326. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.cc +63 -0
  327. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.js +12 -0
  328. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.cc +115 -0
  329. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.js +14 -0
  330. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.cc +26 -0
  331. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.js +7 -0
  332. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.cc +225 -0
  333. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.js +59 -0
  334. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.cc +42 -0
  335. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.js +53 -0
  336. package/vendor/node-addon-api/test/thunking_manual.cc +140 -0
  337. package/vendor/node-addon-api/test/thunking_manual.js +17 -0
  338. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.cc +215 -0
  339. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.js +188 -0
  340. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +68 -0
  341. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +12 -0
  342. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc +127 -0
  343. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.js +14 -0
  344. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc +28 -0
  345. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.js +7 -0
  346. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +237 -0
  347. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.js +59 -0
  348. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc +53 -0
  349. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.js +53 -0
  350. package/vendor/node-addon-api/test/typedarray-bigint.js +58 -0
  351. package/vendor/node-addon-api/test/typedarray.cc +216 -0
  352. package/vendor/node-addon-api/test/typedarray.js +69 -0
  353. package/vendor/node-addon-api/test/version_management.cc +27 -0
  354. package/vendor/node-addon-api/test/version_management.js +31 -0
  355. package/vendor/node-addon-api/tools/README.md +73 -0
  356. package/vendor/node-addon-api/tools/check-napi.js +100 -0
  357. package/vendor/node-addon-api/tools/clang-format.js +68 -0
  358. package/vendor/node-addon-api/tools/conversion.js +309 -0
  359. package/vendor/node-addon-api/tools/eslint-format.js +71 -0
  360. package/build/Raylib.dir/Release/Raylib.dll.recipe +0 -14
  361. package/build/Raylib.dir/Release/Raylib.tlog/CL.command.1.tlog +0 -0
  362. package/build/Raylib.dir/Release/Raylib.tlog/CL.read.1.tlog +0 -0
  363. package/build/Raylib.dir/Release/Raylib.tlog/CL.write.1.tlog +0 -0
  364. package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.command.1.tlog +0 -10
  365. package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.read.1.tlog +0 -27
  366. package/build/Raylib.dir/Release/Raylib.tlog/CustomBuild.write.1.tlog +0 -2
  367. package/build/Raylib.dir/Release/Raylib.tlog/Raylib.lastbuildstate +0 -2
  368. package/build/Raylib.dir/Release/Raylib.tlog/Raylib.write.1u.tlog +0 -0
  369. package/build/Raylib.dir/Release/Raylib.tlog/link.command.1.tlog +0 -0
  370. package/build/Raylib.dir/Release/Raylib.tlog/link.read.1.tlog +0 -0
  371. package/build/Raylib.dir/Release/Raylib.tlog/link.write.1.tlog +0 -0
  372. package/build/Raylib.dir/Release/raudio.obj +0 -0
  373. package/build/Raylib.dir/Release/rcore.obj +0 -0
  374. package/build/Raylib.dir/Release/rglfw.obj +0 -0
  375. package/build/Raylib.dir/Release/rmodels.obj +0 -0
  376. package/build/Raylib.dir/Release/rshapes.obj +0 -0
  377. package/build/Raylib.dir/Release/rtext.obj +0 -0
  378. package/build/Raylib.dir/Release/rtextures.obj +0 -0
  379. package/build/Raylib.dir/Release/utils.obj +0 -0
  380. package/build/Raylib.vcxproj +0 -358
  381. package/build/Raylib.vcxproj.filters +0 -37
  382. package/build/Release/Raylib.dll +0 -0
  383. package/build/Release/Raylib.exp +0 -0
  384. package/build/Release/Raylib.lib +0 -0
@@ -0,0 +1,2983 @@
1
+ #ifndef SRC_NAPI_H_
2
+ #define SRC_NAPI_H_
3
+
4
+ #include <node_api.h>
5
+ #include <functional>
6
+ #include <initializer_list>
7
+ #include <memory>
8
+ #include <mutex>
9
+ #include <string>
10
+ #include <vector>
11
+
12
+ // VS2015 RTM has bugs with constexpr, so require min of VS2015 Update 3 (known good version)
13
+ #if !defined(_MSC_VER) || _MSC_FULL_VER >= 190024210
14
+ #define NAPI_HAS_CONSTEXPR 1
15
+ #endif
16
+
17
+ // VS2013 does not support char16_t literal strings, so we'll work around it using wchar_t strings
18
+ // and casting them. This is safe as long as the character sizes are the same.
19
+ #if defined(_MSC_VER) && _MSC_VER <= 1800
20
+ static_assert(sizeof(char16_t) == sizeof(wchar_t), "Size mismatch between char16_t and wchar_t");
21
+ #define NAPI_WIDE_TEXT(x) reinterpret_cast<char16_t*>(L ## x)
22
+ #else
23
+ #define NAPI_WIDE_TEXT(x) u ## x
24
+ #endif
25
+
26
+ // If C++ exceptions are not explicitly enabled or disabled, enable them
27
+ // if exceptions were enabled in the compiler settings.
28
+ #if !defined(NAPI_CPP_EXCEPTIONS) && !defined(NAPI_DISABLE_CPP_EXCEPTIONS)
29
+ #if defined(_CPPUNWIND) || defined (__EXCEPTIONS)
30
+ #define NAPI_CPP_EXCEPTIONS
31
+ #else
32
+ #error Exception support not detected. \
33
+ Define either NAPI_CPP_EXCEPTIONS or NAPI_DISABLE_CPP_EXCEPTIONS.
34
+ #endif
35
+ #endif
36
+
37
+ // If C++ NAPI_CPP_EXCEPTIONS are enabled, NODE_ADDON_API_ENABLE_MAYBE should
38
+ // not be set
39
+ #if defined(NAPI_CPP_EXCEPTIONS) && defined(NODE_ADDON_API_ENABLE_MAYBE)
40
+ #error NODE_ADDON_API_ENABLE_MAYBE should not be set when \
41
+ NAPI_CPP_EXCEPTIONS is defined.
42
+ #endif
43
+
44
+ #ifdef _NOEXCEPT
45
+ #define NAPI_NOEXCEPT _NOEXCEPT
46
+ #else
47
+ #define NAPI_NOEXCEPT noexcept
48
+ #endif
49
+
50
+ #ifdef NAPI_CPP_EXCEPTIONS
51
+
52
+ // When C++ exceptions are enabled, Errors are thrown directly. There is no need
53
+ // to return anything after the throw statements. The variadic parameter is an
54
+ // optional return value that is ignored.
55
+ // We need _VOID versions of the macros to avoid warnings resulting from
56
+ // leaving the NAPI_THROW_* `...` argument empty.
57
+
58
+ #define NAPI_THROW(e, ...) throw e
59
+ #define NAPI_THROW_VOID(e) throw e
60
+
61
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
62
+ if ((status) != napi_ok) throw Napi::Error::New(env);
63
+
64
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
65
+ if ((status) != napi_ok) throw Napi::Error::New(env);
66
+
67
+ #else // NAPI_CPP_EXCEPTIONS
68
+
69
+ // When C++ exceptions are disabled, Errors are thrown as JavaScript exceptions,
70
+ // which are pending until the callback returns to JS. The variadic parameter
71
+ // is an optional return value; usually it is an empty result.
72
+ // We need _VOID versions of the macros to avoid warnings resulting from
73
+ // leaving the NAPI_THROW_* `...` argument empty.
74
+
75
+ #define NAPI_THROW(e, ...) \
76
+ do { \
77
+ (e).ThrowAsJavaScriptException(); \
78
+ return __VA_ARGS__; \
79
+ } while (0)
80
+
81
+ #define NAPI_THROW_VOID(e) \
82
+ do { \
83
+ (e).ThrowAsJavaScriptException(); \
84
+ return; \
85
+ } while (0)
86
+
87
+ #define NAPI_THROW_IF_FAILED(env, status, ...) \
88
+ if ((status) != napi_ok) { \
89
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
90
+ return __VA_ARGS__; \
91
+ }
92
+
93
+ #define NAPI_THROW_IF_FAILED_VOID(env, status) \
94
+ if ((status) != napi_ok) { \
95
+ Napi::Error::New(env).ThrowAsJavaScriptException(); \
96
+ return; \
97
+ }
98
+
99
+ #endif // NAPI_CPP_EXCEPTIONS
100
+
101
+ #ifdef NODE_ADDON_API_ENABLE_MAYBE
102
+ #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \
103
+ NAPI_THROW_IF_FAILED(env, status, Napi::Nothing<type>())
104
+
105
+ #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \
106
+ NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \
107
+ return Napi::Just<type>(result);
108
+ #else
109
+ #define NAPI_MAYBE_THROW_IF_FAILED(env, status, type) \
110
+ NAPI_THROW_IF_FAILED(env, status, type())
111
+
112
+ #define NAPI_RETURN_OR_THROW_IF_FAILED(env, status, result, type) \
113
+ NAPI_MAYBE_THROW_IF_FAILED(env, status, type); \
114
+ return result;
115
+ #endif
116
+
117
+ # define NAPI_DISALLOW_ASSIGN(CLASS) void operator=(const CLASS&) = delete;
118
+ # define NAPI_DISALLOW_COPY(CLASS) CLASS(const CLASS&) = delete;
119
+
120
+ #define NAPI_DISALLOW_ASSIGN_COPY(CLASS) \
121
+ NAPI_DISALLOW_ASSIGN(CLASS) \
122
+ NAPI_DISALLOW_COPY(CLASS)
123
+
124
+ #define NAPI_CHECK(condition, location, message) \
125
+ do { \
126
+ if (!(condition)) { \
127
+ Napi::Error::Fatal((location), (message)); \
128
+ } \
129
+ } while (0)
130
+
131
+ #define NAPI_FATAL_IF_FAILED(status, location, message) \
132
+ NAPI_CHECK((status) == napi_ok, location, message)
133
+
134
+ ////////////////////////////////////////////////////////////////////////////////
135
+ /// Node-API C++ Wrapper Classes
136
+ ///
137
+ /// These classes wrap the "Node-API" ABI-stable C APIs for Node.js, providing a
138
+ /// C++ object model and C++ exception-handling semantics with low overhead.
139
+ /// The wrappers are all header-only so that they do not affect the ABI.
140
+ ////////////////////////////////////////////////////////////////////////////////
141
+ namespace Napi {
142
+
143
+ // Forward declarations
144
+ class Env;
145
+ class Value;
146
+ class Boolean;
147
+ class Number;
148
+ #if NAPI_VERSION > 5
149
+ class BigInt;
150
+ #endif // NAPI_VERSION > 5
151
+ #if (NAPI_VERSION > 4)
152
+ class Date;
153
+ #endif
154
+ class String;
155
+ class Object;
156
+ class Array;
157
+ class ArrayBuffer;
158
+ class Function;
159
+ class Error;
160
+ class PropertyDescriptor;
161
+ class CallbackInfo;
162
+ class TypedArray;
163
+ template <typename T> class TypedArrayOf;
164
+
165
+ using Int8Array =
166
+ TypedArrayOf<int8_t>; ///< Typed-array of signed 8-bit integers
167
+ using Uint8Array =
168
+ TypedArrayOf<uint8_t>; ///< Typed-array of unsigned 8-bit integers
169
+ using Int16Array =
170
+ TypedArrayOf<int16_t>; ///< Typed-array of signed 16-bit integers
171
+ using Uint16Array =
172
+ TypedArrayOf<uint16_t>; ///< Typed-array of unsigned 16-bit integers
173
+ using Int32Array =
174
+ TypedArrayOf<int32_t>; ///< Typed-array of signed 32-bit integers
175
+ using Uint32Array =
176
+ TypedArrayOf<uint32_t>; ///< Typed-array of unsigned 32-bit integers
177
+ using Float32Array =
178
+ TypedArrayOf<float>; ///< Typed-array of 32-bit floating-point values
179
+ using Float64Array =
180
+ TypedArrayOf<double>; ///< Typed-array of 64-bit floating-point values
181
+ #if NAPI_VERSION > 5
182
+ using BigInt64Array =
183
+ TypedArrayOf<int64_t>; ///< Typed array of signed 64-bit integers
184
+ using BigUint64Array =
185
+ TypedArrayOf<uint64_t>; ///< Typed array of unsigned 64-bit integers
186
+ #endif // NAPI_VERSION > 5
187
+
188
+ /// Defines the signature of a Node-API C++ module's registration callback
189
+ /// (init) function.
190
+ using ModuleRegisterCallback = Object (*)(Env env, Object exports);
191
+
192
+ class MemoryManagement;
193
+
194
+ /// A simple Maybe type, representing an object which may or may not have a
195
+ /// value.
196
+ ///
197
+ /// If an API method returns a Maybe<>, the API method can potentially fail
198
+ /// either because an exception is thrown, or because an exception is pending,
199
+ /// e.g. because a previous API call threw an exception that hasn't been
200
+ /// caught yet. In that case, a "Nothing" value is returned.
201
+ template <class T>
202
+ class Maybe {
203
+ public:
204
+ bool IsNothing() const;
205
+ bool IsJust() const;
206
+
207
+ /// Short-hand for Unwrap(), which doesn't return a value. Could be used
208
+ /// where the actual value of the Maybe is not needed like Object::Set.
209
+ /// If this Maybe is nothing (empty), node-addon-api will crash the
210
+ /// process.
211
+ void Check() const;
212
+
213
+ /// Return the value of type T contained in the Maybe. If this Maybe is
214
+ /// nothing (empty), node-addon-api will crash the process.
215
+ T Unwrap() const;
216
+
217
+ /// Return the value of type T contained in the Maybe, or using a default
218
+ /// value if this Maybe is nothing (empty).
219
+ T UnwrapOr(const T& default_value) const;
220
+
221
+ /// Converts this Maybe to a value of type T in the out. If this Maybe is
222
+ /// nothing (empty), `false` is returned and `out` is left untouched.
223
+ bool UnwrapTo(T* out) const;
224
+
225
+ bool operator==(const Maybe& other) const;
226
+ bool operator!=(const Maybe& other) const;
227
+
228
+ private:
229
+ Maybe();
230
+ explicit Maybe(const T& t);
231
+
232
+ bool _has_value;
233
+ T _value;
234
+
235
+ template <class U>
236
+ friend Maybe<U> Nothing();
237
+ template <class U>
238
+ friend Maybe<U> Just(const U& u);
239
+ };
240
+
241
+ template <class T>
242
+ inline Maybe<T> Nothing();
243
+
244
+ template <class T>
245
+ inline Maybe<T> Just(const T& t);
246
+
247
+ #if defined(NODE_ADDON_API_ENABLE_MAYBE)
248
+ template <typename T>
249
+ using MaybeOrValue = Maybe<T>;
250
+ #else
251
+ template <typename T>
252
+ using MaybeOrValue = T;
253
+ #endif
254
+
255
+ /// Environment for Node-API values and operations.
256
+ ///
257
+ /// All Node-API values and operations must be associated with an environment.
258
+ /// An environment instance is always provided to callback functions; that
259
+ /// environment must then be used for any creation of Node-API values or other
260
+ /// Node-API operations within the callback. (Many methods infer the
261
+ /// environment from the `this` instance that the method is called on.)
262
+ ///
263
+ /// In the future, multiple environments per process may be supported,
264
+ /// although current implementations only support one environment per process.
265
+ ///
266
+ /// In the V8 JavaScript engine, a Node-API environment approximately
267
+ /// corresponds to an Isolate.
268
+ class Env {
269
+ private:
270
+ #if NAPI_VERSION > 2
271
+ template <typename Hook, typename Arg = void>
272
+ class CleanupHook;
273
+ #endif // NAPI_VERSION > 2
274
+ #if NAPI_VERSION > 5
275
+ template <typename T> static void DefaultFini(Env, T* data);
276
+ template <typename DataType, typename HintType>
277
+ static void DefaultFiniWithHint(Env, DataType* data, HintType* hint);
278
+ #endif // NAPI_VERSION > 5
279
+ public:
280
+ Env(napi_env env);
281
+
282
+ operator napi_env() const;
283
+
284
+ Object Global() const;
285
+ Value Undefined() const;
286
+ Value Null() const;
287
+
288
+ bool IsExceptionPending() const;
289
+ Error GetAndClearPendingException();
290
+
291
+ MaybeOrValue<Value> RunScript(const char* utf8script);
292
+ MaybeOrValue<Value> RunScript(const std::string& utf8script);
293
+ MaybeOrValue<Value> RunScript(String script);
294
+
295
+ #if NAPI_VERSION > 2
296
+ template <typename Hook>
297
+ CleanupHook<Hook> AddCleanupHook(Hook hook);
298
+
299
+ template <typename Hook, typename Arg>
300
+ CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);
301
+ #endif // NAPI_VERSION > 2
302
+
303
+ #if NAPI_VERSION > 5
304
+ template <typename T> T* GetInstanceData();
305
+
306
+ template <typename T> using Finalizer = void (*)(Env, T*);
307
+ template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
308
+ void SetInstanceData(T* data);
309
+
310
+ template <typename DataType, typename HintType>
311
+ using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
312
+ template <typename DataType,
313
+ typename HintType,
314
+ FinalizerWithHint<DataType, HintType> fini =
315
+ Env::DefaultFiniWithHint<DataType, HintType>>
316
+ void SetInstanceData(DataType* data, HintType* hint);
317
+ #endif // NAPI_VERSION > 5
318
+
319
+ private:
320
+ napi_env _env;
321
+
322
+ #if NAPI_VERSION > 2
323
+ template <typename Hook, typename Arg>
324
+ class CleanupHook {
325
+ public:
326
+ CleanupHook(Env env, Hook hook, Arg* arg);
327
+ CleanupHook(Env env, Hook hook);
328
+ bool Remove(Env env);
329
+ bool IsEmpty() const;
330
+
331
+ private:
332
+ static inline void Wrapper(void* data) NAPI_NOEXCEPT;
333
+ static inline void WrapperWithArg(void* data) NAPI_NOEXCEPT;
334
+
335
+ void (*wrapper)(void* arg);
336
+ struct CleanupData {
337
+ Hook hook;
338
+ Arg* arg;
339
+ } * data;
340
+ };
341
+ };
342
+ #endif // NAPI_VERSION > 2
343
+
344
+ /// A JavaScript value of unknown type.
345
+ ///
346
+ /// For type-specific operations, convert to one of the Value subclasses using a `To*` or `As()`
347
+ /// method. The `To*` methods do type coercion; the `As()` method does not.
348
+ ///
349
+ /// Napi::Value value = ...
350
+ /// if (!value.IsString()) throw Napi::TypeError::New(env, "Invalid arg...");
351
+ /// Napi::String str = value.As<Napi::String>(); // Cast to a string value
352
+ ///
353
+ /// Napi::Value anotherValue = ...
354
+ /// bool isTruthy = anotherValue.ToBoolean(); // Coerce to a boolean value
355
+ class Value {
356
+ public:
357
+ Value(); ///< Creates a new _empty_ Value instance.
358
+ Value(napi_env env,
359
+ napi_value value); ///< Wraps a Node-API value primitive.
360
+
361
+ /// Creates a JS value from a C++ primitive.
362
+ ///
363
+ /// `value` may be any of:
364
+ /// - bool
365
+ /// - Any integer type
366
+ /// - Any floating point type
367
+ /// - const char* (encoded using UTF-8, null-terminated)
368
+ /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
369
+ /// - std::string (encoded using UTF-8)
370
+ /// - std::u16string
371
+ /// - napi::Value
372
+ /// - napi_value
373
+ template <typename T>
374
+ static Value From(napi_env env, const T& value);
375
+
376
+ /// Converts to a Node-API value primitive.
377
+ ///
378
+ /// If the instance is _empty_, this returns `nullptr`.
379
+ operator napi_value() const;
380
+
381
+ /// Tests if this value strictly equals another value.
382
+ bool operator ==(const Value& other) const;
383
+
384
+ /// Tests if this value does not strictly equal another value.
385
+ bool operator !=(const Value& other) const;
386
+
387
+ /// Tests if this value strictly equals another value.
388
+ bool StrictEquals(const Value& other) const;
389
+
390
+ /// Gets the environment the value is associated with.
391
+ Napi::Env Env() const;
392
+
393
+ /// Checks if the value is empty (uninitialized).
394
+ ///
395
+ /// An empty value is invalid, and most attempts to perform an operation on an empty value
396
+ /// will result in an exception. Note an empty value is distinct from JavaScript `null` or
397
+ /// `undefined`, which are valid values.
398
+ ///
399
+ /// When C++ exceptions are disabled at compile time, a method with a `Value` return type may
400
+ /// return an empty value to indicate a pending exception. So when not using C++ exceptions,
401
+ /// callers should check whether the value is empty before attempting to use it.
402
+ bool IsEmpty() const;
403
+
404
+ napi_valuetype Type() const; ///< Gets the type of the value.
405
+
406
+ bool IsUndefined() const; ///< Tests if a value is an undefined JavaScript value.
407
+ bool IsNull() const; ///< Tests if a value is a null JavaScript value.
408
+ bool IsBoolean() const; ///< Tests if a value is a JavaScript boolean.
409
+ bool IsNumber() const; ///< Tests if a value is a JavaScript number.
410
+ #if NAPI_VERSION > 5
411
+ bool IsBigInt() const; ///< Tests if a value is a JavaScript bigint.
412
+ #endif // NAPI_VERSION > 5
413
+ #if (NAPI_VERSION > 4)
414
+ bool IsDate() const; ///< Tests if a value is a JavaScript date.
415
+ #endif
416
+ bool IsString() const; ///< Tests if a value is a JavaScript string.
417
+ bool IsSymbol() const; ///< Tests if a value is a JavaScript symbol.
418
+ bool IsArray() const; ///< Tests if a value is a JavaScript array.
419
+ bool IsArrayBuffer() const; ///< Tests if a value is a JavaScript array buffer.
420
+ bool IsTypedArray() const; ///< Tests if a value is a JavaScript typed array.
421
+ bool IsObject() const; ///< Tests if a value is a JavaScript object.
422
+ bool IsFunction() const; ///< Tests if a value is a JavaScript function.
423
+ bool IsPromise() const; ///< Tests if a value is a JavaScript promise.
424
+ bool IsDataView() const; ///< Tests if a value is a JavaScript data view.
425
+ bool IsBuffer() const; ///< Tests if a value is a Node buffer.
426
+ bool IsExternal() const; ///< Tests if a value is a pointer to external data.
427
+
428
+ /// Casts to another type of `Napi::Value`, when the actual type is known or assumed.
429
+ ///
430
+ /// This conversion does NOT coerce the type. Calling any methods inappropriate for the actual
431
+ /// value type will throw `Napi::Error`.
432
+ template <typename T> T As() const;
433
+
434
+ MaybeOrValue<Boolean> ToBoolean()
435
+ const; ///< Coerces a value to a JavaScript boolean.
436
+ MaybeOrValue<Number> ToNumber()
437
+ const; ///< Coerces a value to a JavaScript number.
438
+ MaybeOrValue<String> ToString()
439
+ const; ///< Coerces a value to a JavaScript string.
440
+ MaybeOrValue<Object> ToObject()
441
+ const; ///< Coerces a value to a JavaScript object.
442
+
443
+ protected:
444
+ /// !cond INTERNAL
445
+ napi_env _env;
446
+ napi_value _value;
447
+ /// !endcond
448
+ };
449
+
450
+ /// A JavaScript boolean value.
451
+ class Boolean : public Value {
452
+ public:
453
+ static Boolean New(napi_env env, ///< Node-API environment
454
+ bool value ///< Boolean value
455
+ );
456
+
457
+ Boolean(); ///< Creates a new _empty_ Boolean instance.
458
+ Boolean(napi_env env,
459
+ napi_value value); ///< Wraps a Node-API value primitive.
460
+
461
+ operator bool() const; ///< Converts a Boolean value to a boolean primitive.
462
+ bool Value() const; ///< Converts a Boolean value to a boolean primitive.
463
+ };
464
+
465
+ /// A JavaScript number value.
466
+ class Number : public Value {
467
+ public:
468
+ static Number New(napi_env env, ///< Node-API environment
469
+ double value ///< Number value
470
+ );
471
+
472
+ Number(); ///< Creates a new _empty_ Number instance.
473
+ Number(napi_env env,
474
+ napi_value value); ///< Wraps a Node-API value primitive.
475
+
476
+ operator int32_t()
477
+ const; ///< Converts a Number value to a 32-bit signed integer value.
478
+ operator uint32_t()
479
+ const; ///< Converts a Number value to a 32-bit unsigned integer value.
480
+ operator int64_t()
481
+ const; ///< Converts a Number value to a 64-bit signed integer value.
482
+ operator float()
483
+ const; ///< Converts a Number value to a 32-bit floating-point value.
484
+ operator double()
485
+ const; ///< Converts a Number value to a 64-bit floating-point value.
486
+
487
+ int32_t Int32Value()
488
+ const; ///< Converts a Number value to a 32-bit signed integer value.
489
+ uint32_t Uint32Value()
490
+ const; ///< Converts a Number value to a 32-bit unsigned integer value.
491
+ int64_t Int64Value()
492
+ const; ///< Converts a Number value to a 64-bit signed integer value.
493
+ float FloatValue()
494
+ const; ///< Converts a Number value to a 32-bit floating-point value.
495
+ double DoubleValue()
496
+ const; ///< Converts a Number value to a 64-bit floating-point value.
497
+ };
498
+
499
+ #if NAPI_VERSION > 5
500
+ /// A JavaScript bigint value.
501
+ class BigInt : public Value {
502
+ public:
503
+ static BigInt New(napi_env env, ///< Node-API environment
504
+ int64_t value ///< Number value
505
+ );
506
+ static BigInt New(napi_env env, ///< Node-API environment
507
+ uint64_t value ///< Number value
508
+ );
509
+
510
+ /// Creates a new BigInt object using a specified sign bit and a
511
+ /// specified list of digits/words.
512
+ /// The resulting number is calculated as:
513
+ /// (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
514
+ static BigInt New(napi_env env, ///< Node-API environment
515
+ int sign_bit, ///< Sign bit. 1 if negative.
516
+ size_t word_count, ///< Number of words in array
517
+ const uint64_t* words ///< Array of words
518
+ );
519
+
520
+ BigInt(); ///< Creates a new _empty_ BigInt instance.
521
+ BigInt(napi_env env,
522
+ napi_value value); ///< Wraps a Node-API value primitive.
523
+
524
+ int64_t Int64Value(bool* lossless)
525
+ const; ///< Converts a BigInt value to a 64-bit signed integer value.
526
+ uint64_t Uint64Value(bool* lossless)
527
+ const; ///< Converts a BigInt value to a 64-bit unsigned integer value.
528
+
529
+ size_t WordCount() const; ///< The number of 64-bit words needed to store
530
+ ///< the result of ToWords().
531
+
532
+ /// Writes the contents of this BigInt to a specified memory location.
533
+ /// `sign_bit` must be provided and will be set to 1 if this BigInt is
534
+ /// negative.
535
+ /// `*word_count` has to be initialized to the length of the `words` array.
536
+ /// Upon return, it will be set to the actual number of words that would
537
+ /// be needed to store this BigInt (i.e. the return value of `WordCount()`).
538
+ void ToWords(int* sign_bit, size_t* word_count, uint64_t* words);
539
+ };
540
+ #endif // NAPI_VERSION > 5
541
+
542
+ #if (NAPI_VERSION > 4)
543
+ /// A JavaScript date value.
544
+ class Date : public Value {
545
+ public:
546
+ /// Creates a new Date value from a double primitive.
547
+ static Date New(napi_env env, ///< Node-API environment
548
+ double value ///< Number value
549
+ );
550
+
551
+ Date(); ///< Creates a new _empty_ Date instance.
552
+ Date(napi_env env, napi_value value); ///< Wraps a Node-API value primitive.
553
+ operator double() const; ///< Converts a Date value to double primitive
554
+
555
+ double ValueOf() const; ///< Converts a Date value to a double primitive.
556
+ };
557
+ #endif
558
+
559
+ /// A JavaScript string or symbol value (that can be used as a property name).
560
+ class Name : public Value {
561
+ public:
562
+ Name(); ///< Creates a new _empty_ Name instance.
563
+ Name(napi_env env,
564
+ napi_value value); ///< Wraps a Node-API value primitive.
565
+ };
566
+
567
+ /// A JavaScript string value.
568
+ class String : public Name {
569
+ public:
570
+ /// Creates a new String value from a UTF-8 encoded C++ string.
571
+ static String New(napi_env env, ///< Node-API environment
572
+ const std::string& value ///< UTF-8 encoded C++ string
573
+ );
574
+
575
+ /// Creates a new String value from a UTF-16 encoded C++ string.
576
+ static String New(napi_env env, ///< Node-API environment
577
+ const std::u16string& value ///< UTF-16 encoded C++ string
578
+ );
579
+
580
+ /// Creates a new String value from a UTF-8 encoded C string.
581
+ static String New(
582
+ napi_env env, ///< Node-API environment
583
+ const char* value ///< UTF-8 encoded null-terminated C string
584
+ );
585
+
586
+ /// Creates a new String value from a UTF-16 encoded C string.
587
+ static String New(
588
+ napi_env env, ///< Node-API environment
589
+ const char16_t* value ///< UTF-16 encoded null-terminated C string
590
+ );
591
+
592
+ /// Creates a new String value from a UTF-8 encoded C string with specified
593
+ /// length.
594
+ static String New(napi_env env, ///< Node-API environment
595
+ const char* value, ///< UTF-8 encoded C string (not
596
+ ///< necessarily null-terminated)
597
+ size_t length ///< length of the string in bytes
598
+ );
599
+
600
+ /// Creates a new String value from a UTF-16 encoded C string with specified
601
+ /// length.
602
+ static String New(
603
+ napi_env env, ///< Node-API environment
604
+ const char16_t* value, ///< UTF-16 encoded C string (not necessarily
605
+ ///< null-terminated)
606
+ size_t length ///< Length of the string in 2-byte code units
607
+ );
608
+
609
+ /// Creates a new String based on the original object's type.
610
+ ///
611
+ /// `value` may be any of:
612
+ /// - const char* (encoded using UTF-8, null-terminated)
613
+ /// - const char16_t* (encoded using UTF-16-LE, null-terminated)
614
+ /// - std::string (encoded using UTF-8)
615
+ /// - std::u16string
616
+ template <typename T>
617
+ static String From(napi_env env, const T& value);
618
+
619
+ String(); ///< Creates a new _empty_ String instance.
620
+ String(napi_env env,
621
+ napi_value value); ///< Wraps a Node-API value primitive.
622
+
623
+ operator std::string()
624
+ const; ///< Converts a String value to a UTF-8 encoded C++ string.
625
+ operator std::u16string()
626
+ const; ///< Converts a String value to a UTF-16 encoded C++ string.
627
+ std::string Utf8Value()
628
+ const; ///< Converts a String value to a UTF-8 encoded C++ string.
629
+ std::u16string Utf16Value()
630
+ const; ///< Converts a String value to a UTF-16 encoded C++ string.
631
+ };
632
+
633
+ /// A JavaScript symbol value.
634
+ class Symbol : public Name {
635
+ public:
636
+ /// Creates a new Symbol value with an optional description.
637
+ static Symbol New(
638
+ napi_env env, ///< Node-API environment
639
+ const char* description =
640
+ nullptr ///< Optional UTF-8 encoded null-terminated C string
641
+ /// describing the symbol
642
+ );
643
+
644
+ /// Creates a new Symbol value with a description.
645
+ static Symbol New(
646
+ napi_env env, ///< Node-API environment
647
+ const std::string&
648
+ description ///< UTF-8 encoded C++ string describing the symbol
649
+ );
650
+
651
+ /// Creates a new Symbol value with a description.
652
+ static Symbol New(napi_env env, ///< Node-API environment
653
+ String description ///< String value describing the symbol
654
+ );
655
+
656
+ /// Creates a new Symbol value with a description.
657
+ static Symbol New(
658
+ napi_env env, ///< Node-API environment
659
+ napi_value description ///< String value describing the symbol
660
+ );
661
+
662
+ /// Get a public Symbol (e.g. Symbol.iterator).
663
+ static MaybeOrValue<Symbol> WellKnown(napi_env, const std::string& name);
664
+
665
+ // Create a symbol in the global registry, UTF-8 Encoded cpp string
666
+ static MaybeOrValue<Symbol> For(napi_env env,
667
+ const std::string& description);
668
+
669
+ // Create a symbol in the global registry, C style string (null terminated)
670
+ static MaybeOrValue<Symbol> For(napi_env env, const char* description);
671
+
672
+ // Create a symbol in the global registry, String value describing the symbol
673
+ static MaybeOrValue<Symbol> For(napi_env env, String description);
674
+
675
+ // Create a symbol in the global registry, napi_value describing the symbol
676
+ static MaybeOrValue<Symbol> For(napi_env env, napi_value description);
677
+
678
+ Symbol(); ///< Creates a new _empty_ Symbol instance.
679
+ Symbol(napi_env env,
680
+ napi_value value); ///< Wraps a Node-API value primitive.
681
+ };
682
+
683
+ /// A JavaScript object value.
684
+ class Object : public Value {
685
+ public:
686
+ /// Enables property and element assignments using indexing syntax.
687
+ ///
688
+ /// This is a convenient helper to get and set object properties. As
689
+ /// getting and setting object properties may throw with JavaScript
690
+ /// exceptions, it is notable that these operations may fail.
691
+ /// When NODE_ADDON_API_ENABLE_MAYBE is defined, the process will abort
692
+ /// on JavaScript exceptions.
693
+ ///
694
+ /// Example:
695
+ ///
696
+ /// Napi::Value propertyValue = object1['A'];
697
+ /// object2['A'] = propertyValue;
698
+ /// Napi::Value elementValue = array[0];
699
+ /// array[1] = elementValue;
700
+ template <typename Key>
701
+ class PropertyLValue {
702
+ public:
703
+ /// Converts an L-value to a value.
704
+ operator Value() const;
705
+
706
+ /// Assigns a value to the property. The type of value can be
707
+ /// anything supported by `Object::Set`.
708
+ template <typename ValueType>
709
+ PropertyLValue& operator =(ValueType value);
710
+
711
+ private:
712
+ PropertyLValue() = delete;
713
+ PropertyLValue(Object object, Key key);
714
+ napi_env _env;
715
+ napi_value _object;
716
+ Key _key;
717
+
718
+ friend class Napi::Object;
719
+ };
720
+
721
+ /// Creates a new Object value.
722
+ static Object New(napi_env env ///< Node-API environment
723
+ );
724
+
725
+ Object(); ///< Creates a new _empty_ Object instance.
726
+ Object(napi_env env,
727
+ napi_value value); ///< Wraps a Node-API value primitive.
728
+
729
+ /// Gets or sets a named property.
730
+ PropertyLValue<std::string> operator [](
731
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
732
+ );
733
+
734
+ /// Gets or sets a named property.
735
+ PropertyLValue<std::string> operator [](
736
+ const std::string& utf8name ///< UTF-8 encoded property name
737
+ );
738
+
739
+ /// Gets or sets an indexed property or array element.
740
+ PropertyLValue<uint32_t> operator [](
741
+ uint32_t index /// Property / element index
742
+ );
743
+
744
+ /// Gets or sets an indexed property or array element.
745
+ PropertyLValue<Value> operator[](Value index /// Property / element index
746
+ );
747
+
748
+ /// Gets or sets an indexed property or array element.
749
+ PropertyLValue<Value> operator[](Value index /// Property / element index
750
+ ) const;
751
+
752
+ /// Gets a named property.
753
+ MaybeOrValue<Value> operator[](
754
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
755
+ ) const;
756
+
757
+ /// Gets a named property.
758
+ MaybeOrValue<Value> operator[](
759
+ const std::string& utf8name ///< UTF-8 encoded property name
760
+ ) const;
761
+
762
+ /// Gets an indexed property or array element.
763
+ MaybeOrValue<Value> operator[](uint32_t index ///< Property / element index
764
+ ) const;
765
+
766
+ /// Checks whether a property is present.
767
+ MaybeOrValue<bool> Has(napi_value key ///< Property key primitive
768
+ ) const;
769
+
770
+ /// Checks whether a property is present.
771
+ MaybeOrValue<bool> Has(Value key ///< Property key
772
+ ) const;
773
+
774
+ /// Checks whether a named property is present.
775
+ MaybeOrValue<bool> Has(
776
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
777
+ ) const;
778
+
779
+ /// Checks whether a named property is present.
780
+ MaybeOrValue<bool> Has(
781
+ const std::string& utf8name ///< UTF-8 encoded property name
782
+ ) const;
783
+
784
+ /// Checks whether a own property is present.
785
+ MaybeOrValue<bool> HasOwnProperty(
786
+ napi_value key ///< Property key primitive
787
+ ) const;
788
+
789
+ /// Checks whether a own property is present.
790
+ MaybeOrValue<bool> HasOwnProperty(Value key ///< Property key
791
+ ) const;
792
+
793
+ /// Checks whether a own property is present.
794
+ MaybeOrValue<bool> HasOwnProperty(
795
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
796
+ ) const;
797
+
798
+ /// Checks whether a own property is present.
799
+ MaybeOrValue<bool> HasOwnProperty(
800
+ const std::string& utf8name ///< UTF-8 encoded property name
801
+ ) const;
802
+
803
+ /// Gets a property.
804
+ MaybeOrValue<Value> Get(napi_value key ///< Property key primitive
805
+ ) const;
806
+
807
+ /// Gets a property.
808
+ MaybeOrValue<Value> Get(Value key ///< Property key
809
+ ) const;
810
+
811
+ /// Gets a named property.
812
+ MaybeOrValue<Value> Get(
813
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
814
+ ) const;
815
+
816
+ /// Gets a named property.
817
+ MaybeOrValue<Value> Get(
818
+ const std::string& utf8name ///< UTF-8 encoded property name
819
+ ) const;
820
+
821
+ /// Sets a property.
822
+ template <typename ValueType>
823
+ MaybeOrValue<bool> Set(napi_value key, ///< Property key primitive
824
+ const ValueType& value ///< Property value primitive
825
+ );
826
+
827
+ /// Sets a property.
828
+ template <typename ValueType>
829
+ MaybeOrValue<bool> Set(Value key, ///< Property key
830
+ const ValueType& value ///< Property value
831
+ );
832
+
833
+ /// Sets a named property.
834
+ template <typename ValueType>
835
+ MaybeOrValue<bool> Set(
836
+ const char* utf8name, ///< UTF-8 encoded null-terminated property name
837
+ const ValueType& value);
838
+
839
+ /// Sets a named property.
840
+ template <typename ValueType>
841
+ MaybeOrValue<bool> Set(
842
+ const std::string& utf8name, ///< UTF-8 encoded property name
843
+ const ValueType& value ///< Property value primitive
844
+ );
845
+
846
+ /// Delete property.
847
+ MaybeOrValue<bool> Delete(napi_value key ///< Property key primitive
848
+ );
849
+
850
+ /// Delete property.
851
+ MaybeOrValue<bool> Delete(Value key ///< Property key
852
+ );
853
+
854
+ /// Delete property.
855
+ MaybeOrValue<bool> Delete(
856
+ const char* utf8name ///< UTF-8 encoded null-terminated property name
857
+ );
858
+
859
+ /// Delete property.
860
+ MaybeOrValue<bool> Delete(
861
+ const std::string& utf8name ///< UTF-8 encoded property name
862
+ );
863
+
864
+ /// Checks whether an indexed property is present.
865
+ MaybeOrValue<bool> Has(uint32_t index ///< Property / element index
866
+ ) const;
867
+
868
+ /// Gets an indexed property or array element.
869
+ MaybeOrValue<Value> Get(uint32_t index ///< Property / element index
870
+ ) const;
871
+
872
+ /// Sets an indexed property or array element.
873
+ template <typename ValueType>
874
+ MaybeOrValue<bool> Set(uint32_t index, ///< Property / element index
875
+ const ValueType& value ///< Property value primitive
876
+ );
877
+
878
+ /// Deletes an indexed property or array element.
879
+ MaybeOrValue<bool> Delete(uint32_t index ///< Property / element index
880
+ );
881
+
882
+ /// This operation can fail in case of Proxy.[[OwnPropertyKeys]] and
883
+ /// Proxy.[[GetOwnProperty]] calling into JavaScript. See:
884
+ /// -
885
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-ownpropertykeys
886
+ /// -
887
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getownproperty-p
888
+ MaybeOrValue<Array> GetPropertyNames() const; ///< Get all property names
889
+
890
+ /// Defines a property on the object.
891
+ ///
892
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
893
+ /// into JavaScript. See
894
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
895
+ MaybeOrValue<bool> DefineProperty(
896
+ const PropertyDescriptor&
897
+ property ///< Descriptor for the property to be defined
898
+ );
899
+
900
+ /// Defines properties on the object.
901
+ ///
902
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
903
+ /// into JavaScript. See
904
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
905
+ MaybeOrValue<bool> DefineProperties(
906
+ const std::initializer_list<PropertyDescriptor>& properties
907
+ ///< List of descriptors for the properties to be defined
908
+ );
909
+
910
+ /// Defines properties on the object.
911
+ ///
912
+ /// This operation can fail in case of Proxy.[[DefineOwnProperty]] calling
913
+ /// into JavaScript. See
914
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc
915
+ MaybeOrValue<bool> DefineProperties(
916
+ const std::vector<PropertyDescriptor>& properties
917
+ ///< Vector of descriptors for the properties to be defined
918
+ );
919
+
920
+ /// Checks if an object is an instance created by a constructor function.
921
+ ///
922
+ /// This is equivalent to the JavaScript `instanceof` operator.
923
+ ///
924
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
925
+ /// JavaScript.
926
+ /// See
927
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
928
+ MaybeOrValue<bool> InstanceOf(
929
+ const Function& constructor ///< Constructor function
930
+ ) const;
931
+
932
+ template <typename Finalizer, typename T>
933
+ inline void AddFinalizer(Finalizer finalizeCallback, T* data);
934
+
935
+ template <typename Finalizer, typename T, typename Hint>
936
+ inline void AddFinalizer(Finalizer finalizeCallback,
937
+ T* data,
938
+ Hint* finalizeHint);
939
+
940
+ #ifdef NAPI_CPP_EXCEPTIONS
941
+ class const_iterator;
942
+
943
+ inline const_iterator begin() const;
944
+
945
+ inline const_iterator end() const;
946
+
947
+ class iterator;
948
+
949
+ inline iterator begin();
950
+
951
+ inline iterator end();
952
+ #endif // NAPI_CPP_EXCEPTIONS
953
+
954
+ #if NAPI_VERSION >= 8
955
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
956
+ /// JavaScript.
957
+ /// See
958
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
959
+ MaybeOrValue<bool> Freeze();
960
+ /// This operation can fail in case of Proxy.[[GetPrototypeOf]] calling into
961
+ /// JavaScript.
962
+ /// See
963
+ /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-getprototypeof
964
+ MaybeOrValue<bool> Seal();
965
+ #endif // NAPI_VERSION >= 8
966
+ };
967
+
968
+ template <typename T>
969
+ class External : public Value {
970
+ public:
971
+ static External New(napi_env env, T* data);
972
+
973
+ // Finalizer must implement `void operator()(Env env, T* data)`.
974
+ template <typename Finalizer>
975
+ static External New(napi_env env,
976
+ T* data,
977
+ Finalizer finalizeCallback);
978
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
979
+ template <typename Finalizer, typename Hint>
980
+ static External New(napi_env env,
981
+ T* data,
982
+ Finalizer finalizeCallback,
983
+ Hint* finalizeHint);
984
+
985
+ External();
986
+ External(napi_env env, napi_value value);
987
+
988
+ T* Data() const;
989
+ };
990
+
991
+ class Array : public Object {
992
+ public:
993
+ static Array New(napi_env env);
994
+ static Array New(napi_env env, size_t length);
995
+
996
+ Array();
997
+ Array(napi_env env, napi_value value);
998
+
999
+ uint32_t Length() const;
1000
+ };
1001
+
1002
+ #ifdef NAPI_CPP_EXCEPTIONS
1003
+ class Object::const_iterator {
1004
+ private:
1005
+ enum class Type { BEGIN, END };
1006
+
1007
+ inline const_iterator(const Object* object, const Type type);
1008
+
1009
+ public:
1010
+ inline const_iterator& operator++();
1011
+
1012
+ inline bool operator==(const const_iterator& other) const;
1013
+
1014
+ inline bool operator!=(const const_iterator& other) const;
1015
+
1016
+ inline const std::pair<Value, Object::PropertyLValue<Value>> operator*()
1017
+ const;
1018
+
1019
+ private:
1020
+ const Napi::Object* _object;
1021
+ Array _keys;
1022
+ uint32_t _index;
1023
+
1024
+ friend class Object;
1025
+ };
1026
+
1027
+ class Object::iterator {
1028
+ private:
1029
+ enum class Type { BEGIN, END };
1030
+
1031
+ inline iterator(Object* object, const Type type);
1032
+
1033
+ public:
1034
+ inline iterator& operator++();
1035
+
1036
+ inline bool operator==(const iterator& other) const;
1037
+
1038
+ inline bool operator!=(const iterator& other) const;
1039
+
1040
+ inline std::pair<Value, Object::PropertyLValue<Value>> operator*();
1041
+
1042
+ private:
1043
+ Napi::Object* _object;
1044
+ Array _keys;
1045
+ uint32_t _index;
1046
+
1047
+ friend class Object;
1048
+ };
1049
+ #endif // NAPI_CPP_EXCEPTIONS
1050
+
1051
+ /// A JavaScript array buffer value.
1052
+ class ArrayBuffer : public Object {
1053
+ public:
1054
+ /// Creates a new ArrayBuffer instance over a new automatically-allocated buffer.
1055
+ static ArrayBuffer New(
1056
+ napi_env env, ///< Node-API environment
1057
+ size_t byteLength ///< Length of the buffer to be allocated, in bytes
1058
+ );
1059
+
1060
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1061
+ /// specified byte length.
1062
+ static ArrayBuffer New(
1063
+ napi_env env, ///< Node-API environment
1064
+ void* externalData, ///< Pointer to the external buffer to be used by
1065
+ ///< the array
1066
+ size_t byteLength ///< Length of the external buffer to be used by the
1067
+ ///< array, in bytes
1068
+ );
1069
+
1070
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1071
+ /// specified byte length.
1072
+ template <typename Finalizer>
1073
+ static ArrayBuffer New(
1074
+ napi_env env, ///< Node-API environment
1075
+ void* externalData, ///< Pointer to the external buffer to be used by
1076
+ ///< the array
1077
+ size_t byteLength, ///< Length of the external buffer to be used by the
1078
+ ///< array,
1079
+ /// in bytes
1080
+ Finalizer finalizeCallback ///< Function to be called when the array
1081
+ ///< buffer is destroyed;
1082
+ /// must implement `void operator()(Env env,
1083
+ /// void* externalData)`
1084
+ );
1085
+
1086
+ /// Creates a new ArrayBuffer instance, using an external buffer with
1087
+ /// specified byte length.
1088
+ template <typename Finalizer, typename Hint>
1089
+ static ArrayBuffer New(
1090
+ napi_env env, ///< Node-API environment
1091
+ void* externalData, ///< Pointer to the external buffer to be used by
1092
+ ///< the array
1093
+ size_t byteLength, ///< Length of the external buffer to be used by the
1094
+ ///< array,
1095
+ /// in bytes
1096
+ Finalizer finalizeCallback, ///< Function to be called when the array
1097
+ ///< buffer is destroyed;
1098
+ /// must implement `void operator()(Env
1099
+ /// env, void* externalData, Hint* hint)`
1100
+ Hint* finalizeHint ///< Hint (second parameter) to be passed to the
1101
+ ///< finalize callback
1102
+ );
1103
+
1104
+ ArrayBuffer(); ///< Creates a new _empty_ ArrayBuffer instance.
1105
+ ArrayBuffer(napi_env env,
1106
+ napi_value value); ///< Wraps a Node-API value primitive.
1107
+
1108
+ void* Data(); ///< Gets a pointer to the data buffer.
1109
+ size_t ByteLength(); ///< Gets the length of the array buffer in bytes.
1110
+
1111
+ #if NAPI_VERSION >= 7
1112
+ bool IsDetached() const;
1113
+ void Detach();
1114
+ #endif // NAPI_VERSION >= 7
1115
+ };
1116
+
1117
+ /// A JavaScript typed-array value with unknown array type.
1118
+ ///
1119
+ /// For type-specific operations, cast to a `TypedArrayOf<T>` instance using the `As()`
1120
+ /// method:
1121
+ ///
1122
+ /// Napi::TypedArray array = ...
1123
+ /// if (t.TypedArrayType() == napi_int32_array) {
1124
+ /// Napi::Int32Array int32Array = t.As<Napi::Int32Array>();
1125
+ /// }
1126
+ class TypedArray : public Object {
1127
+ public:
1128
+ TypedArray(); ///< Creates a new _empty_ TypedArray instance.
1129
+ TypedArray(napi_env env,
1130
+ napi_value value); ///< Wraps a Node-API value primitive.
1131
+
1132
+ napi_typedarray_type TypedArrayType() const; ///< Gets the type of this typed-array.
1133
+ Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
1134
+
1135
+ uint8_t ElementSize() const; ///< Gets the size in bytes of one element in the array.
1136
+ size_t ElementLength() const; ///< Gets the number of elements in the array.
1137
+ size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
1138
+ size_t ByteLength() const; ///< Gets the length of the array in bytes.
1139
+
1140
+ protected:
1141
+ /// !cond INTERNAL
1142
+ napi_typedarray_type _type;
1143
+ size_t _length;
1144
+
1145
+ TypedArray(napi_env env, napi_value value, napi_typedarray_type type, size_t length);
1146
+
1147
+ static const napi_typedarray_type unknown_array_type = static_cast<napi_typedarray_type>(-1);
1148
+
1149
+ template <typename T>
1150
+ static
1151
+ #if defined(NAPI_HAS_CONSTEXPR)
1152
+ constexpr
1153
+ #endif
1154
+ napi_typedarray_type TypedArrayTypeForPrimitiveType() {
1155
+ return std::is_same<T, int8_t>::value ? napi_int8_array
1156
+ : std::is_same<T, uint8_t>::value ? napi_uint8_array
1157
+ : std::is_same<T, int16_t>::value ? napi_int16_array
1158
+ : std::is_same<T, uint16_t>::value ? napi_uint16_array
1159
+ : std::is_same<T, int32_t>::value ? napi_int32_array
1160
+ : std::is_same<T, uint32_t>::value ? napi_uint32_array
1161
+ : std::is_same<T, float>::value ? napi_float32_array
1162
+ : std::is_same<T, double>::value ? napi_float64_array
1163
+ #if NAPI_VERSION > 5
1164
+ : std::is_same<T, int64_t>::value ? napi_bigint64_array
1165
+ : std::is_same<T, uint64_t>::value ? napi_biguint64_array
1166
+ #endif // NAPI_VERSION > 5
1167
+ : unknown_array_type;
1168
+ }
1169
+ /// !endcond
1170
+ };
1171
+
1172
+ /// A JavaScript typed-array value with known array type.
1173
+ ///
1174
+ /// Note while it is possible to create and access Uint8 "clamped" arrays using this class,
1175
+ /// the _clamping_ behavior is only applied in JavaScript.
1176
+ template <typename T>
1177
+ class TypedArrayOf : public TypedArray {
1178
+ public:
1179
+ /// Creates a new TypedArray instance over a new automatically-allocated array buffer.
1180
+ ///
1181
+ /// The array type parameter can normally be omitted (because it is inferred from the template
1182
+ /// parameter T), except when creating a "clamped" array:
1183
+ ///
1184
+ /// Uint8Array::New(env, length, napi_uint8_clamped_array)
1185
+ static TypedArrayOf New(
1186
+ napi_env env, ///< Node-API environment
1187
+ size_t elementLength, ///< Length of the created array, as a number of
1188
+ ///< elements
1189
+ #if defined(NAPI_HAS_CONSTEXPR)
1190
+ napi_typedarray_type type =
1191
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
1192
+ #else
1193
+ napi_typedarray_type type
1194
+ #endif
1195
+ ///< Type of array, if different from the default array type for the
1196
+ ///< template parameter T.
1197
+ );
1198
+
1199
+ /// Creates a new TypedArray instance over a provided array buffer.
1200
+ ///
1201
+ /// The array type parameter can normally be omitted (because it is inferred from the template
1202
+ /// parameter T), except when creating a "clamped" array:
1203
+ ///
1204
+ /// Uint8Array::New(env, length, buffer, 0, napi_uint8_clamped_array)
1205
+ static TypedArrayOf New(
1206
+ napi_env env, ///< Node-API environment
1207
+ size_t elementLength, ///< Length of the created array, as a number of
1208
+ ///< elements
1209
+ Napi::ArrayBuffer arrayBuffer, ///< Backing array buffer instance to use
1210
+ size_t bufferOffset, ///< Offset into the array buffer where the
1211
+ ///< typed-array starts
1212
+ #if defined(NAPI_HAS_CONSTEXPR)
1213
+ napi_typedarray_type type =
1214
+ TypedArray::TypedArrayTypeForPrimitiveType<T>()
1215
+ #else
1216
+ napi_typedarray_type type
1217
+ #endif
1218
+ ///< Type of array, if different from the default array type for the
1219
+ ///< template parameter T.
1220
+ );
1221
+
1222
+ TypedArrayOf(); ///< Creates a new _empty_ TypedArrayOf instance.
1223
+ TypedArrayOf(napi_env env,
1224
+ napi_value value); ///< Wraps a Node-API value primitive.
1225
+
1226
+ T& operator [](size_t index); ///< Gets or sets an element in the array.
1227
+ const T& operator [](size_t index) const; ///< Gets an element in the array.
1228
+
1229
+ /// Gets a pointer to the array's backing buffer.
1230
+ ///
1231
+ /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer, because the
1232
+ /// typed-array may have a non-zero `ByteOffset()` into the `ArrayBuffer`.
1233
+ T* Data();
1234
+
1235
+ /// Gets a pointer to the array's backing buffer.
1236
+ ///
1237
+ /// This is not necessarily the same as the `ArrayBuffer::Data()` pointer, because the
1238
+ /// typed-array may have a non-zero `ByteOffset()` into the `ArrayBuffer`.
1239
+ const T* Data() const;
1240
+
1241
+ private:
1242
+ T* _data;
1243
+
1244
+ TypedArrayOf(napi_env env,
1245
+ napi_value value,
1246
+ napi_typedarray_type type,
1247
+ size_t length,
1248
+ T* data);
1249
+ };
1250
+
1251
+ /// The DataView provides a low-level interface for reading/writing multiple
1252
+ /// number types in an ArrayBuffer irrespective of the platform's endianness.
1253
+ class DataView : public Object {
1254
+ public:
1255
+ static DataView New(napi_env env,
1256
+ Napi::ArrayBuffer arrayBuffer);
1257
+ static DataView New(napi_env env,
1258
+ Napi::ArrayBuffer arrayBuffer,
1259
+ size_t byteOffset);
1260
+ static DataView New(napi_env env,
1261
+ Napi::ArrayBuffer arrayBuffer,
1262
+ size_t byteOffset,
1263
+ size_t byteLength);
1264
+
1265
+ DataView(); ///< Creates a new _empty_ DataView instance.
1266
+ DataView(napi_env env,
1267
+ napi_value value); ///< Wraps a Node-API value primitive.
1268
+
1269
+ Napi::ArrayBuffer ArrayBuffer() const; ///< Gets the backing array buffer.
1270
+ size_t ByteOffset() const; ///< Gets the offset into the buffer where the array starts.
1271
+ size_t ByteLength() const; ///< Gets the length of the array in bytes.
1272
+
1273
+ void* Data() const;
1274
+
1275
+ float GetFloat32(size_t byteOffset) const;
1276
+ double GetFloat64(size_t byteOffset) const;
1277
+ int8_t GetInt8(size_t byteOffset) const;
1278
+ int16_t GetInt16(size_t byteOffset) const;
1279
+ int32_t GetInt32(size_t byteOffset) const;
1280
+ uint8_t GetUint8(size_t byteOffset) const;
1281
+ uint16_t GetUint16(size_t byteOffset) const;
1282
+ uint32_t GetUint32(size_t byteOffset) const;
1283
+
1284
+ void SetFloat32(size_t byteOffset, float value) const;
1285
+ void SetFloat64(size_t byteOffset, double value) const;
1286
+ void SetInt8(size_t byteOffset, int8_t value) const;
1287
+ void SetInt16(size_t byteOffset, int16_t value) const;
1288
+ void SetInt32(size_t byteOffset, int32_t value) const;
1289
+ void SetUint8(size_t byteOffset, uint8_t value) const;
1290
+ void SetUint16(size_t byteOffset, uint16_t value) const;
1291
+ void SetUint32(size_t byteOffset, uint32_t value) const;
1292
+
1293
+ private:
1294
+ template <typename T>
1295
+ T ReadData(size_t byteOffset) const;
1296
+
1297
+ template <typename T>
1298
+ void WriteData(size_t byteOffset, T value) const;
1299
+
1300
+ void* _data;
1301
+ size_t _length;
1302
+ };
1303
+
1304
+ class Function : public Object {
1305
+ public:
1306
+ using VoidCallback = void (*)(const CallbackInfo& info);
1307
+ using Callback = Value (*)(const CallbackInfo& info);
1308
+
1309
+ template <VoidCallback cb>
1310
+ static Function New(napi_env env,
1311
+ const char* utf8name = nullptr,
1312
+ void* data = nullptr);
1313
+
1314
+ template <Callback cb>
1315
+ static Function New(napi_env env,
1316
+ const char* utf8name = nullptr,
1317
+ void* data = nullptr);
1318
+
1319
+ template <VoidCallback cb>
1320
+ static Function New(napi_env env,
1321
+ const std::string& utf8name,
1322
+ void* data = nullptr);
1323
+
1324
+ template <Callback cb>
1325
+ static Function New(napi_env env,
1326
+ const std::string& utf8name,
1327
+ void* data = nullptr);
1328
+
1329
+ /// Callable must implement operator() accepting a const CallbackInfo&
1330
+ /// and return either void or Value.
1331
+ template <typename Callable>
1332
+ static Function New(napi_env env,
1333
+ Callable cb,
1334
+ const char* utf8name = nullptr,
1335
+ void* data = nullptr);
1336
+ /// Callable must implement operator() accepting a const CallbackInfo&
1337
+ /// and return either void or Value.
1338
+ template <typename Callable>
1339
+ static Function New(napi_env env,
1340
+ Callable cb,
1341
+ const std::string& utf8name,
1342
+ void* data = nullptr);
1343
+
1344
+ Function();
1345
+ Function(napi_env env, napi_value value);
1346
+
1347
+ MaybeOrValue<Value> operator()(
1348
+ const std::initializer_list<napi_value>& args) const;
1349
+
1350
+ MaybeOrValue<Value> Call(
1351
+ const std::initializer_list<napi_value>& args) const;
1352
+ MaybeOrValue<Value> Call(const std::vector<napi_value>& args) const;
1353
+ MaybeOrValue<Value> Call(size_t argc, const napi_value* args) const;
1354
+ MaybeOrValue<Value> Call(
1355
+ napi_value recv, const std::initializer_list<napi_value>& args) const;
1356
+ MaybeOrValue<Value> Call(napi_value recv,
1357
+ const std::vector<napi_value>& args) const;
1358
+ MaybeOrValue<Value> Call(napi_value recv,
1359
+ size_t argc,
1360
+ const napi_value* args) const;
1361
+
1362
+ MaybeOrValue<Value> MakeCallback(
1363
+ napi_value recv,
1364
+ const std::initializer_list<napi_value>& args,
1365
+ napi_async_context context = nullptr) const;
1366
+ MaybeOrValue<Value> MakeCallback(napi_value recv,
1367
+ const std::vector<napi_value>& args,
1368
+ napi_async_context context = nullptr) const;
1369
+ MaybeOrValue<Value> MakeCallback(napi_value recv,
1370
+ size_t argc,
1371
+ const napi_value* args,
1372
+ napi_async_context context = nullptr) const;
1373
+
1374
+ MaybeOrValue<Object> New(
1375
+ const std::initializer_list<napi_value>& args) const;
1376
+ MaybeOrValue<Object> New(const std::vector<napi_value>& args) const;
1377
+ MaybeOrValue<Object> New(size_t argc, const napi_value* args) const;
1378
+ };
1379
+
1380
+ class Promise : public Object {
1381
+ public:
1382
+ class Deferred {
1383
+ public:
1384
+ static Deferred New(napi_env env);
1385
+ Deferred(napi_env env);
1386
+
1387
+ Napi::Promise Promise() const;
1388
+ Napi::Env Env() const;
1389
+
1390
+ void Resolve(napi_value value) const;
1391
+ void Reject(napi_value value) const;
1392
+
1393
+ private:
1394
+ napi_env _env;
1395
+ napi_deferred _deferred;
1396
+ napi_value _promise;
1397
+ };
1398
+
1399
+ Promise(napi_env env, napi_value value);
1400
+ };
1401
+
1402
+ template <typename T>
1403
+ class Buffer : public Uint8Array {
1404
+ public:
1405
+ static Buffer<T> New(napi_env env, size_t length);
1406
+ static Buffer<T> New(napi_env env, T* data, size_t length);
1407
+
1408
+ // Finalizer must implement `void operator()(Env env, T* data)`.
1409
+ template <typename Finalizer>
1410
+ static Buffer<T> New(napi_env env, T* data,
1411
+ size_t length,
1412
+ Finalizer finalizeCallback);
1413
+ // Finalizer must implement `void operator()(Env env, T* data, Hint* hint)`.
1414
+ template <typename Finalizer, typename Hint>
1415
+ static Buffer<T> New(napi_env env, T* data,
1416
+ size_t length,
1417
+ Finalizer finalizeCallback,
1418
+ Hint* finalizeHint);
1419
+
1420
+ static Buffer<T> Copy(napi_env env, const T* data, size_t length);
1421
+
1422
+ Buffer();
1423
+ Buffer(napi_env env, napi_value value);
1424
+ size_t Length() const;
1425
+ T* Data() const;
1426
+
1427
+ private:
1428
+ mutable size_t _length;
1429
+ mutable T* _data;
1430
+
1431
+ Buffer(napi_env env, napi_value value, size_t length, T* data);
1432
+ void EnsureInfo() const;
1433
+ };
1434
+
1435
+ /// Holds a counted reference to a value; initially a weak reference unless otherwise specified,
1436
+ /// may be changed to/from a strong reference by adjusting the refcount.
1437
+ ///
1438
+ /// The referenced value is not immediately destroyed when the reference count is zero; it is
1439
+ /// merely then eligible for garbage-collection if there are no other references to the value.
1440
+ template <typename T>
1441
+ class Reference {
1442
+ public:
1443
+ static Reference<T> New(const T& value, uint32_t initialRefcount = 0);
1444
+
1445
+ Reference();
1446
+ Reference(napi_env env, napi_ref ref);
1447
+ ~Reference();
1448
+
1449
+ // A reference can be moved but cannot be copied.
1450
+ Reference(Reference<T>&& other);
1451
+ Reference<T>& operator =(Reference<T>&& other);
1452
+ NAPI_DISALLOW_ASSIGN(Reference<T>)
1453
+
1454
+ operator napi_ref() const;
1455
+ bool operator ==(const Reference<T> &other) const;
1456
+ bool operator !=(const Reference<T> &other) const;
1457
+
1458
+ Napi::Env Env() const;
1459
+ bool IsEmpty() const;
1460
+
1461
+ // Note when getting the value of a Reference it is usually correct to do so
1462
+ // within a HandleScope so that the value handle gets cleaned up efficiently.
1463
+ T Value() const;
1464
+
1465
+ uint32_t Ref();
1466
+ uint32_t Unref();
1467
+ void Reset();
1468
+ void Reset(const T& value, uint32_t refcount = 0);
1469
+
1470
+ // Call this on a reference that is declared as static data, to prevent its
1471
+ // destructor from running at program shutdown time, which would attempt to
1472
+ // reset the reference when the environment is no longer valid. Avoid using
1473
+ // this if at all possible. If you do need to use static data, MAKE SURE to
1474
+ // warn your users that your addon is NOT threadsafe.
1475
+ void SuppressDestruct();
1476
+
1477
+ protected:
1478
+ Reference(const Reference<T>&);
1479
+
1480
+ /// !cond INTERNAL
1481
+ napi_env _env;
1482
+ napi_ref _ref;
1483
+ /// !endcond
1484
+
1485
+ private:
1486
+ bool _suppressDestruct;
1487
+ };
1488
+
1489
+ class ObjectReference: public Reference<Object> {
1490
+ public:
1491
+ ObjectReference();
1492
+ ObjectReference(napi_env env, napi_ref ref);
1493
+
1494
+ // A reference can be moved but cannot be copied.
1495
+ ObjectReference(Reference<Object>&& other);
1496
+ ObjectReference& operator =(Reference<Object>&& other);
1497
+ ObjectReference(ObjectReference&& other);
1498
+ ObjectReference& operator =(ObjectReference&& other);
1499
+ NAPI_DISALLOW_ASSIGN(ObjectReference)
1500
+
1501
+ MaybeOrValue<Napi::Value> Get(const char* utf8name) const;
1502
+ MaybeOrValue<Napi::Value> Get(const std::string& utf8name) const;
1503
+ MaybeOrValue<bool> Set(const char* utf8name, napi_value value);
1504
+ MaybeOrValue<bool> Set(const char* utf8name, Napi::Value value);
1505
+ MaybeOrValue<bool> Set(const char* utf8name, const char* utf8value);
1506
+ MaybeOrValue<bool> Set(const char* utf8name, bool boolValue);
1507
+ MaybeOrValue<bool> Set(const char* utf8name, double numberValue);
1508
+ MaybeOrValue<bool> Set(const std::string& utf8name, napi_value value);
1509
+ MaybeOrValue<bool> Set(const std::string& utf8name, Napi::Value value);
1510
+ MaybeOrValue<bool> Set(const std::string& utf8name, std::string& utf8value);
1511
+ MaybeOrValue<bool> Set(const std::string& utf8name, bool boolValue);
1512
+ MaybeOrValue<bool> Set(const std::string& utf8name, double numberValue);
1513
+
1514
+ MaybeOrValue<Napi::Value> Get(uint32_t index) const;
1515
+ MaybeOrValue<bool> Set(uint32_t index, const napi_value value);
1516
+ MaybeOrValue<bool> Set(uint32_t index, const Napi::Value value);
1517
+ MaybeOrValue<bool> Set(uint32_t index, const char* utf8value);
1518
+ MaybeOrValue<bool> Set(uint32_t index, const std::string& utf8value);
1519
+ MaybeOrValue<bool> Set(uint32_t index, bool boolValue);
1520
+ MaybeOrValue<bool> Set(uint32_t index, double numberValue);
1521
+
1522
+ protected:
1523
+ ObjectReference(const ObjectReference&);
1524
+ };
1525
+
1526
+ class FunctionReference: public Reference<Function> {
1527
+ public:
1528
+ FunctionReference();
1529
+ FunctionReference(napi_env env, napi_ref ref);
1530
+
1531
+ // A reference can be moved but cannot be copied.
1532
+ FunctionReference(Reference<Function>&& other);
1533
+ FunctionReference& operator =(Reference<Function>&& other);
1534
+ FunctionReference(FunctionReference&& other);
1535
+ FunctionReference& operator =(FunctionReference&& other);
1536
+ NAPI_DISALLOW_ASSIGN_COPY(FunctionReference)
1537
+
1538
+ MaybeOrValue<Napi::Value> operator()(
1539
+ const std::initializer_list<napi_value>& args) const;
1540
+
1541
+ MaybeOrValue<Napi::Value> Call(
1542
+ const std::initializer_list<napi_value>& args) const;
1543
+ MaybeOrValue<Napi::Value> Call(const std::vector<napi_value>& args) const;
1544
+ MaybeOrValue<Napi::Value> Call(
1545
+ napi_value recv, const std::initializer_list<napi_value>& args) const;
1546
+ MaybeOrValue<Napi::Value> Call(napi_value recv,
1547
+ const std::vector<napi_value>& args) const;
1548
+ MaybeOrValue<Napi::Value> Call(napi_value recv,
1549
+ size_t argc,
1550
+ const napi_value* args) const;
1551
+
1552
+ MaybeOrValue<Napi::Value> MakeCallback(
1553
+ napi_value recv,
1554
+ const std::initializer_list<napi_value>& args,
1555
+ napi_async_context context = nullptr) const;
1556
+ MaybeOrValue<Napi::Value> MakeCallback(
1557
+ napi_value recv,
1558
+ const std::vector<napi_value>& args,
1559
+ napi_async_context context = nullptr) const;
1560
+ MaybeOrValue<Napi::Value> MakeCallback(
1561
+ napi_value recv,
1562
+ size_t argc,
1563
+ const napi_value* args,
1564
+ napi_async_context context = nullptr) const;
1565
+
1566
+ MaybeOrValue<Object> New(
1567
+ const std::initializer_list<napi_value>& args) const;
1568
+ MaybeOrValue<Object> New(const std::vector<napi_value>& args) const;
1569
+ };
1570
+
1571
+ // Shortcuts to creating a new reference with inferred type and refcount = 0.
1572
+ template <typename T> Reference<T> Weak(T value);
1573
+ ObjectReference Weak(Object value);
1574
+ FunctionReference Weak(Function value);
1575
+
1576
+ // Shortcuts to creating a new reference with inferred type and refcount = 1.
1577
+ template <typename T> Reference<T> Persistent(T value);
1578
+ ObjectReference Persistent(Object value);
1579
+ FunctionReference Persistent(Function value);
1580
+
1581
+ /// A persistent reference to a JavaScript error object. Use of this class
1582
+ /// depends somewhat on whether C++ exceptions are enabled at compile time.
1583
+ ///
1584
+ /// ### Handling Errors With C++ Exceptions
1585
+ ///
1586
+ /// If C++ exceptions are enabled, then the `Error` class extends
1587
+ /// `std::exception` and enables integrated error-handling for C++ exceptions
1588
+ /// and JavaScript exceptions.
1589
+ ///
1590
+ /// If a Node-API call fails without executing any JavaScript code (for
1591
+ /// example due to an invalid argument), then the Node-API wrapper
1592
+ /// automatically converts and throws the error as a C++ exception of type
1593
+ /// `Napi::Error`. Or if a JavaScript function called by C++ code via Node-API
1594
+ /// throws a JavaScript exception, then the Node-API wrapper automatically
1595
+ /// converts and throws it as a C++ exception of type `Napi::Error`.
1596
+ ///
1597
+ /// If a C++ exception of type `Napi::Error` escapes from a Node-API C++
1598
+ /// callback, then the Node-API wrapper automatically converts and throws it
1599
+ /// as a JavaScript exception. Therefore, catching a C++ exception of type
1600
+ /// `Napi::Error` prevents a JavaScript exception from being thrown.
1601
+ ///
1602
+ /// #### Example 1A - Throwing a C++ exception:
1603
+ ///
1604
+ /// Napi::Env env = ...
1605
+ /// throw Napi::Error::New(env, "Example exception");
1606
+ ///
1607
+ /// Following C++ statements will not be executed. The exception will bubble
1608
+ /// up as a C++ exception of type `Napi::Error`, until it is either caught
1609
+ /// while still in C++, or else automatically propataged as a JavaScript
1610
+ /// exception when the callback returns to JavaScript.
1611
+ ///
1612
+ /// #### Example 2A - Propagating a Node-API C++ exception:
1613
+ ///
1614
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1615
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1616
+ ///
1617
+ /// Following C++ statements will not be executed. The exception will bubble
1618
+ /// up as a C++ exception of type `Napi::Error`, until it is either caught
1619
+ /// while still in C++, or else automatically propagated as a JavaScript
1620
+ /// exception when the callback returns to JavaScript.
1621
+ ///
1622
+ /// #### Example 3A - Handling a Node-API C++ exception:
1623
+ ///
1624
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1625
+ /// Napi::Value result;
1626
+ /// try {
1627
+ /// result = jsFunctionThatThrows({ arg1, arg2 });
1628
+ /// } catch (const Napi::Error& e) {
1629
+ /// cerr << "Caught JavaScript exception: " + e.what();
1630
+ /// }
1631
+ ///
1632
+ /// Since the exception was caught here, it will not be propagated as a
1633
+ /// JavaScript exception.
1634
+ ///
1635
+ /// ### Handling Errors Without C++ Exceptions
1636
+ ///
1637
+ /// If C++ exceptions are disabled (by defining `NAPI_DISABLE_CPP_EXCEPTIONS`)
1638
+ /// then this class does not extend `std::exception`, and APIs in the `Napi`
1639
+ /// namespace do not throw C++ exceptions when they fail. Instead, they raise
1640
+ /// _pending_ JavaScript exceptions and return _empty_ `Value`s. Calling code
1641
+ /// should check `Value::IsEmpty()` before attempting to use a returned value,
1642
+ /// and may use methods on the `Env` class to check for, get, and clear a
1643
+ /// pending JavaScript exception. If the pending exception is not cleared, it
1644
+ /// will be thrown when the native callback returns to JavaScript.
1645
+ ///
1646
+ /// #### Example 1B - Throwing a JS exception
1647
+ ///
1648
+ /// Napi::Env env = ...
1649
+ /// Napi::Error::New(env, "Example
1650
+ /// exception").ThrowAsJavaScriptException(); return;
1651
+ ///
1652
+ /// After throwing a JS exception, the code should generally return
1653
+ /// immediately from the native callback, after performing any necessary
1654
+ /// cleanup.
1655
+ ///
1656
+ /// #### Example 2B - Propagating a Node-API JS exception:
1657
+ ///
1658
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1659
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1660
+ /// if (result.IsEmpty()) return;
1661
+ ///
1662
+ /// An empty value result from a Node-API call indicates an error occurred,
1663
+ /// and a JavaScript exception is pending. To let the exception propagate, the
1664
+ /// code should generally return immediately from the native callback, after
1665
+ /// performing any necessary cleanup.
1666
+ ///
1667
+ /// #### Example 3B - Handling a Node-API JS exception:
1668
+ ///
1669
+ /// Napi::Function jsFunctionThatThrows = someObj.As<Napi::Function>();
1670
+ /// Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
1671
+ /// if (result.IsEmpty()) {
1672
+ /// Napi::Error e = env.GetAndClearPendingException();
1673
+ /// cerr << "Caught JavaScript exception: " + e.Message();
1674
+ /// }
1675
+ ///
1676
+ /// Since the exception was cleared here, it will not be propagated as a
1677
+ /// JavaScript exception after the native callback returns.
1678
+ class Error : public ObjectReference
1679
+ #ifdef NAPI_CPP_EXCEPTIONS
1680
+ , public std::exception
1681
+ #endif // NAPI_CPP_EXCEPTIONS
1682
+ {
1683
+ public:
1684
+ static Error New(napi_env env);
1685
+ static Error New(napi_env env, const char* message);
1686
+ static Error New(napi_env env, const std::string& message);
1687
+
1688
+ static NAPI_NO_RETURN void Fatal(const char* location, const char* message);
1689
+
1690
+ Error();
1691
+ Error(napi_env env, napi_value value);
1692
+
1693
+ // An error can be moved or copied.
1694
+ Error(Error&& other);
1695
+ Error& operator =(Error&& other);
1696
+ Error(const Error&);
1697
+ Error& operator =(const Error&);
1698
+
1699
+ const std::string& Message() const NAPI_NOEXCEPT;
1700
+ void ThrowAsJavaScriptException() const;
1701
+
1702
+ Object Value() const;
1703
+
1704
+ #ifdef NAPI_CPP_EXCEPTIONS
1705
+ const char* what() const NAPI_NOEXCEPT override;
1706
+ #endif // NAPI_CPP_EXCEPTIONS
1707
+
1708
+ protected:
1709
+ /// !cond INTERNAL
1710
+ using create_error_fn = napi_status (*)(napi_env envb,
1711
+ napi_value code,
1712
+ napi_value msg,
1713
+ napi_value* result);
1714
+
1715
+ template <typename TError>
1716
+ static TError New(napi_env env,
1717
+ const char* message,
1718
+ size_t length,
1719
+ create_error_fn create_error);
1720
+ /// !endcond
1721
+
1722
+ private:
1723
+ const char* ERROR_WRAP_VALUE =
1724
+ "4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal";
1725
+ mutable std::string _message;
1726
+ };
1727
+
1728
+ class TypeError : public Error {
1729
+ public:
1730
+ static TypeError New(napi_env env, const char* message);
1731
+ static TypeError New(napi_env env, const std::string& message);
1732
+
1733
+ TypeError();
1734
+ TypeError(napi_env env, napi_value value);
1735
+ };
1736
+
1737
+ class RangeError : public Error {
1738
+ public:
1739
+ static RangeError New(napi_env env, const char* message);
1740
+ static RangeError New(napi_env env, const std::string& message);
1741
+
1742
+ RangeError();
1743
+ RangeError(napi_env env, napi_value value);
1744
+ };
1745
+
1746
+ class CallbackInfo {
1747
+ public:
1748
+ CallbackInfo(napi_env env, napi_callback_info info);
1749
+ ~CallbackInfo();
1750
+
1751
+ // Disallow copying to prevent multiple free of _dynamicArgs
1752
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackInfo)
1753
+
1754
+ Napi::Env Env() const;
1755
+ Value NewTarget() const;
1756
+ bool IsConstructCall() const;
1757
+ size_t Length() const;
1758
+ const Value operator [](size_t index) const;
1759
+ Value This() const;
1760
+ void* Data() const;
1761
+ void SetData(void* data);
1762
+
1763
+ private:
1764
+ const size_t _staticArgCount = 6;
1765
+ napi_env _env;
1766
+ napi_callback_info _info;
1767
+ napi_value _this;
1768
+ size_t _argc;
1769
+ napi_value* _argv;
1770
+ napi_value _staticArgs[6];
1771
+ napi_value* _dynamicArgs;
1772
+ void* _data;
1773
+ };
1774
+
1775
+ class PropertyDescriptor {
1776
+ public:
1777
+ using GetterCallback = Napi::Value (*)(const Napi::CallbackInfo& info);
1778
+ using SetterCallback = void (*)(const Napi::CallbackInfo& info);
1779
+
1780
+ #ifndef NODE_ADDON_API_DISABLE_DEPRECATED
1781
+ template <typename Getter>
1782
+ static PropertyDescriptor Accessor(const char* utf8name,
1783
+ Getter getter,
1784
+ napi_property_attributes attributes = napi_default,
1785
+ void* data = nullptr);
1786
+ template <typename Getter>
1787
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1788
+ Getter getter,
1789
+ napi_property_attributes attributes = napi_default,
1790
+ void* data = nullptr);
1791
+ template <typename Getter>
1792
+ static PropertyDescriptor Accessor(napi_value name,
1793
+ Getter getter,
1794
+ napi_property_attributes attributes = napi_default,
1795
+ void* data = nullptr);
1796
+ template <typename Getter>
1797
+ static PropertyDescriptor Accessor(Name name,
1798
+ Getter getter,
1799
+ napi_property_attributes attributes = napi_default,
1800
+ void* data = nullptr);
1801
+ template <typename Getter, typename Setter>
1802
+ static PropertyDescriptor Accessor(const char* utf8name,
1803
+ Getter getter,
1804
+ Setter setter,
1805
+ napi_property_attributes attributes = napi_default,
1806
+ void* data = nullptr);
1807
+ template <typename Getter, typename Setter>
1808
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1809
+ Getter getter,
1810
+ Setter setter,
1811
+ napi_property_attributes attributes = napi_default,
1812
+ void* data = nullptr);
1813
+ template <typename Getter, typename Setter>
1814
+ static PropertyDescriptor Accessor(napi_value name,
1815
+ Getter getter,
1816
+ Setter setter,
1817
+ napi_property_attributes attributes = napi_default,
1818
+ void* data = nullptr);
1819
+ template <typename Getter, typename Setter>
1820
+ static PropertyDescriptor Accessor(Name name,
1821
+ Getter getter,
1822
+ Setter setter,
1823
+ napi_property_attributes attributes = napi_default,
1824
+ void* data = nullptr);
1825
+ template <typename Callable>
1826
+ static PropertyDescriptor Function(const char* utf8name,
1827
+ Callable cb,
1828
+ napi_property_attributes attributes = napi_default,
1829
+ void* data = nullptr);
1830
+ template <typename Callable>
1831
+ static PropertyDescriptor Function(const std::string& utf8name,
1832
+ Callable cb,
1833
+ napi_property_attributes attributes = napi_default,
1834
+ void* data = nullptr);
1835
+ template <typename Callable>
1836
+ static PropertyDescriptor Function(napi_value name,
1837
+ Callable cb,
1838
+ napi_property_attributes attributes = napi_default,
1839
+ void* data = nullptr);
1840
+ template <typename Callable>
1841
+ static PropertyDescriptor Function(Name name,
1842
+ Callable cb,
1843
+ napi_property_attributes attributes = napi_default,
1844
+ void* data = nullptr);
1845
+ #endif // !NODE_ADDON_API_DISABLE_DEPRECATED
1846
+
1847
+ template <GetterCallback Getter>
1848
+ static PropertyDescriptor Accessor(const char* utf8name,
1849
+ napi_property_attributes attributes = napi_default,
1850
+ void* data = nullptr);
1851
+
1852
+ template <GetterCallback Getter>
1853
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1854
+ napi_property_attributes attributes = napi_default,
1855
+ void* data = nullptr);
1856
+
1857
+ template <GetterCallback Getter>
1858
+ static PropertyDescriptor Accessor(Name name,
1859
+ napi_property_attributes attributes = napi_default,
1860
+ void* data = nullptr);
1861
+
1862
+ template <GetterCallback Getter, SetterCallback Setter>
1863
+ static PropertyDescriptor Accessor(const char* utf8name,
1864
+ napi_property_attributes attributes = napi_default,
1865
+ void* data = nullptr);
1866
+
1867
+ template <GetterCallback Getter, SetterCallback Setter>
1868
+ static PropertyDescriptor Accessor(const std::string& utf8name,
1869
+ napi_property_attributes attributes = napi_default,
1870
+ void* data = nullptr);
1871
+
1872
+ template <GetterCallback Getter, SetterCallback Setter>
1873
+ static PropertyDescriptor Accessor(Name name,
1874
+ napi_property_attributes attributes = napi_default,
1875
+ void* data = nullptr);
1876
+
1877
+ template <typename Getter>
1878
+ static PropertyDescriptor Accessor(Napi::Env env,
1879
+ Napi::Object object,
1880
+ const char* utf8name,
1881
+ Getter getter,
1882
+ napi_property_attributes attributes = napi_default,
1883
+ void* data = nullptr);
1884
+ template <typename Getter>
1885
+ static PropertyDescriptor Accessor(Napi::Env env,
1886
+ Napi::Object object,
1887
+ const std::string& utf8name,
1888
+ Getter getter,
1889
+ napi_property_attributes attributes = napi_default,
1890
+ void* data = nullptr);
1891
+ template <typename Getter>
1892
+ static PropertyDescriptor Accessor(Napi::Env env,
1893
+ Napi::Object object,
1894
+ Name name,
1895
+ Getter getter,
1896
+ napi_property_attributes attributes = napi_default,
1897
+ void* data = nullptr);
1898
+ template <typename Getter, typename Setter>
1899
+ static PropertyDescriptor Accessor(Napi::Env env,
1900
+ Napi::Object object,
1901
+ const char* utf8name,
1902
+ Getter getter,
1903
+ Setter setter,
1904
+ napi_property_attributes attributes = napi_default,
1905
+ void* data = nullptr);
1906
+ template <typename Getter, typename Setter>
1907
+ static PropertyDescriptor Accessor(Napi::Env env,
1908
+ Napi::Object object,
1909
+ const std::string& utf8name,
1910
+ Getter getter,
1911
+ Setter setter,
1912
+ napi_property_attributes attributes = napi_default,
1913
+ void* data = nullptr);
1914
+ template <typename Getter, typename Setter>
1915
+ static PropertyDescriptor Accessor(Napi::Env env,
1916
+ Napi::Object object,
1917
+ Name name,
1918
+ Getter getter,
1919
+ Setter setter,
1920
+ napi_property_attributes attributes = napi_default,
1921
+ void* data = nullptr);
1922
+ template <typename Callable>
1923
+ static PropertyDescriptor Function(Napi::Env env,
1924
+ Napi::Object object,
1925
+ const char* utf8name,
1926
+ Callable cb,
1927
+ napi_property_attributes attributes = napi_default,
1928
+ void* data = nullptr);
1929
+ template <typename Callable>
1930
+ static PropertyDescriptor Function(Napi::Env env,
1931
+ Napi::Object object,
1932
+ const std::string& utf8name,
1933
+ Callable cb,
1934
+ napi_property_attributes attributes = napi_default,
1935
+ void* data = nullptr);
1936
+ template <typename Callable>
1937
+ static PropertyDescriptor Function(Napi::Env env,
1938
+ Napi::Object object,
1939
+ Name name,
1940
+ Callable cb,
1941
+ napi_property_attributes attributes = napi_default,
1942
+ void* data = nullptr);
1943
+ static PropertyDescriptor Value(const char* utf8name,
1944
+ napi_value value,
1945
+ napi_property_attributes attributes = napi_default);
1946
+ static PropertyDescriptor Value(const std::string& utf8name,
1947
+ napi_value value,
1948
+ napi_property_attributes attributes = napi_default);
1949
+ static PropertyDescriptor Value(napi_value name,
1950
+ napi_value value,
1951
+ napi_property_attributes attributes = napi_default);
1952
+ static PropertyDescriptor Value(Name name,
1953
+ Napi::Value value,
1954
+ napi_property_attributes attributes = napi_default);
1955
+
1956
+ PropertyDescriptor(napi_property_descriptor desc);
1957
+
1958
+ operator napi_property_descriptor&();
1959
+ operator const napi_property_descriptor&() const;
1960
+
1961
+ private:
1962
+ napi_property_descriptor _desc;
1963
+ };
1964
+
1965
+ /// Property descriptor for use with `ObjectWrap::DefineClass()`.
1966
+ ///
1967
+ /// This is different from the standalone `PropertyDescriptor` because it is specific to each
1968
+ /// `ObjectWrap<T>` subclass. This prevents using descriptors from a different class when
1969
+ /// defining a new class (preventing the callbacks from having incorrect `this` pointers).
1970
+ template <typename T>
1971
+ class ClassPropertyDescriptor {
1972
+ public:
1973
+ ClassPropertyDescriptor(napi_property_descriptor desc) : _desc(desc) {}
1974
+
1975
+ operator napi_property_descriptor&() { return _desc; }
1976
+ operator const napi_property_descriptor&() const { return _desc; }
1977
+
1978
+ private:
1979
+ napi_property_descriptor _desc;
1980
+ };
1981
+
1982
+ template <typename T, typename TCallback>
1983
+ struct MethodCallbackData {
1984
+ TCallback callback;
1985
+ void* data;
1986
+ };
1987
+
1988
+ template <typename T, typename TGetterCallback, typename TSetterCallback>
1989
+ struct AccessorCallbackData {
1990
+ TGetterCallback getterCallback;
1991
+ TSetterCallback setterCallback;
1992
+ void* data;
1993
+ };
1994
+
1995
+ template <typename T>
1996
+ class InstanceWrap {
1997
+ public:
1998
+ using InstanceVoidMethodCallback = void (T::*)(const CallbackInfo& info);
1999
+ using InstanceMethodCallback = Napi::Value (T::*)(const CallbackInfo& info);
2000
+ using InstanceGetterCallback = Napi::Value (T::*)(const CallbackInfo& info);
2001
+ using InstanceSetterCallback = void (T::*)(const CallbackInfo& info,
2002
+ const Napi::Value& value);
2003
+
2004
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
2005
+
2006
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
2007
+ InstanceVoidMethodCallback method,
2008
+ napi_property_attributes attributes = napi_default,
2009
+ void* data = nullptr);
2010
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
2011
+ InstanceMethodCallback method,
2012
+ napi_property_attributes attributes = napi_default,
2013
+ void* data = nullptr);
2014
+ static PropertyDescriptor InstanceMethod(Symbol name,
2015
+ InstanceVoidMethodCallback method,
2016
+ napi_property_attributes attributes = napi_default,
2017
+ void* data = nullptr);
2018
+ static PropertyDescriptor InstanceMethod(Symbol name,
2019
+ InstanceMethodCallback method,
2020
+ napi_property_attributes attributes = napi_default,
2021
+ void* data = nullptr);
2022
+ template <InstanceVoidMethodCallback method>
2023
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
2024
+ napi_property_attributes attributes = napi_default,
2025
+ void* data = nullptr);
2026
+ template <InstanceMethodCallback method>
2027
+ static PropertyDescriptor InstanceMethod(const char* utf8name,
2028
+ napi_property_attributes attributes = napi_default,
2029
+ void* data = nullptr);
2030
+ template <InstanceVoidMethodCallback method>
2031
+ static PropertyDescriptor InstanceMethod(Symbol name,
2032
+ napi_property_attributes attributes = napi_default,
2033
+ void* data = nullptr);
2034
+ template <InstanceMethodCallback method>
2035
+ static PropertyDescriptor InstanceMethod(Symbol name,
2036
+ napi_property_attributes attributes = napi_default,
2037
+ void* data = nullptr);
2038
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
2039
+ InstanceGetterCallback getter,
2040
+ InstanceSetterCallback setter,
2041
+ napi_property_attributes attributes = napi_default,
2042
+ void* data = nullptr);
2043
+ static PropertyDescriptor InstanceAccessor(Symbol name,
2044
+ InstanceGetterCallback getter,
2045
+ InstanceSetterCallback setter,
2046
+ napi_property_attributes attributes = napi_default,
2047
+ void* data = nullptr);
2048
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
2049
+ static PropertyDescriptor InstanceAccessor(const char* utf8name,
2050
+ napi_property_attributes attributes = napi_default,
2051
+ void* data = nullptr);
2052
+ template <InstanceGetterCallback getter, InstanceSetterCallback setter=nullptr>
2053
+ static PropertyDescriptor InstanceAccessor(Symbol name,
2054
+ napi_property_attributes attributes = napi_default,
2055
+ void* data = nullptr);
2056
+ static PropertyDescriptor InstanceValue(const char* utf8name,
2057
+ Napi::Value value,
2058
+ napi_property_attributes attributes = napi_default);
2059
+ static PropertyDescriptor InstanceValue(Symbol name,
2060
+ Napi::Value value,
2061
+ napi_property_attributes attributes = napi_default);
2062
+
2063
+ protected:
2064
+ static void AttachPropData(napi_env env, napi_value value, const napi_property_descriptor* prop);
2065
+
2066
+ private:
2067
+ using This = InstanceWrap<T>;
2068
+
2069
+ using InstanceVoidMethodCallbackData =
2070
+ MethodCallbackData<T, InstanceVoidMethodCallback>;
2071
+ using InstanceMethodCallbackData =
2072
+ MethodCallbackData<T, InstanceMethodCallback>;
2073
+ using InstanceAccessorCallbackData =
2074
+ AccessorCallbackData<T, InstanceGetterCallback, InstanceSetterCallback>;
2075
+
2076
+ static napi_value InstanceVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
2077
+ static napi_value InstanceMethodCallbackWrapper(napi_env env, napi_callback_info info);
2078
+ static napi_value InstanceGetterCallbackWrapper(napi_env env, napi_callback_info info);
2079
+ static napi_value InstanceSetterCallbackWrapper(napi_env env, napi_callback_info info);
2080
+
2081
+ template <InstanceSetterCallback method>
2082
+ static napi_value WrappedMethod(napi_env env,
2083
+ napi_callback_info info) NAPI_NOEXCEPT;
2084
+
2085
+ template <InstanceSetterCallback setter> struct SetterTag {};
2086
+
2087
+ template <InstanceSetterCallback setter>
2088
+ static napi_callback WrapSetter(SetterTag<setter>) NAPI_NOEXCEPT {
2089
+ return &This::WrappedMethod<setter>;
2090
+ }
2091
+ static napi_callback WrapSetter(SetterTag<nullptr>) NAPI_NOEXCEPT {
2092
+ return nullptr;
2093
+ }
2094
+ };
2095
+
2096
+ /// Base class to be extended by C++ classes exposed to JavaScript; each C++ class instance gets
2097
+ /// "wrapped" by a JavaScript object that is managed by this class.
2098
+ ///
2099
+ /// At initialization time, the `DefineClass()` method must be used to
2100
+ /// hook up the accessor and method callbacks. It takes a list of
2101
+ /// property descriptors, which can be constructed via the various
2102
+ /// static methods on the base class.
2103
+ ///
2104
+ /// #### Example:
2105
+ ///
2106
+ /// class Example: public Napi::ObjectWrap<Example> {
2107
+ /// public:
2108
+ /// static void Initialize(Napi::Env& env, Napi::Object& target) {
2109
+ /// Napi::Function constructor = DefineClass(env, "Example", {
2110
+ /// InstanceAccessor<&Example::GetSomething, &Example::SetSomething>("value"),
2111
+ /// InstanceMethod<&Example::DoSomething>("doSomething"),
2112
+ /// });
2113
+ /// target.Set("Example", constructor);
2114
+ /// }
2115
+ ///
2116
+ /// Example(const Napi::CallbackInfo& info); // Constructor
2117
+ /// Napi::Value GetSomething(const Napi::CallbackInfo& info);
2118
+ /// void SetSomething(const Napi::CallbackInfo& info, const Napi::Value& value);
2119
+ /// Napi::Value DoSomething(const Napi::CallbackInfo& info);
2120
+ /// }
2121
+ template <typename T>
2122
+ class ObjectWrap : public InstanceWrap<T>, public Reference<Object> {
2123
+ public:
2124
+ ObjectWrap(const CallbackInfo& callbackInfo);
2125
+ virtual ~ObjectWrap();
2126
+
2127
+ static T* Unwrap(Object wrapper);
2128
+
2129
+ // Methods exposed to JavaScript must conform to one of these callback signatures.
2130
+ using StaticVoidMethodCallback = void (*)(const CallbackInfo& info);
2131
+ using StaticMethodCallback = Napi::Value (*)(const CallbackInfo& info);
2132
+ using StaticGetterCallback = Napi::Value (*)(const CallbackInfo& info);
2133
+ using StaticSetterCallback = void (*)(const CallbackInfo& info,
2134
+ const Napi::Value& value);
2135
+
2136
+ using PropertyDescriptor = ClassPropertyDescriptor<T>;
2137
+
2138
+ static Function DefineClass(Napi::Env env,
2139
+ const char* utf8name,
2140
+ const std::initializer_list<PropertyDescriptor>& properties,
2141
+ void* data = nullptr);
2142
+ static Function DefineClass(Napi::Env env,
2143
+ const char* utf8name,
2144
+ const std::vector<PropertyDescriptor>& properties,
2145
+ void* data = nullptr);
2146
+ static PropertyDescriptor StaticMethod(const char* utf8name,
2147
+ StaticVoidMethodCallback method,
2148
+ napi_property_attributes attributes = napi_default,
2149
+ void* data = nullptr);
2150
+ static PropertyDescriptor StaticMethod(const char* utf8name,
2151
+ StaticMethodCallback method,
2152
+ napi_property_attributes attributes = napi_default,
2153
+ void* data = nullptr);
2154
+ static PropertyDescriptor StaticMethod(Symbol name,
2155
+ StaticVoidMethodCallback method,
2156
+ napi_property_attributes attributes = napi_default,
2157
+ void* data = nullptr);
2158
+ static PropertyDescriptor StaticMethod(Symbol name,
2159
+ StaticMethodCallback method,
2160
+ napi_property_attributes attributes = napi_default,
2161
+ void* data = nullptr);
2162
+ template <StaticVoidMethodCallback method>
2163
+ static PropertyDescriptor StaticMethod(const char* utf8name,
2164
+ napi_property_attributes attributes = napi_default,
2165
+ void* data = nullptr);
2166
+ template <StaticVoidMethodCallback method>
2167
+ static PropertyDescriptor StaticMethod(Symbol name,
2168
+ napi_property_attributes attributes = napi_default,
2169
+ void* data = nullptr);
2170
+ template <StaticMethodCallback method>
2171
+ static PropertyDescriptor StaticMethod(const char* utf8name,
2172
+ napi_property_attributes attributes = napi_default,
2173
+ void* data = nullptr);
2174
+ template <StaticMethodCallback method>
2175
+ static PropertyDescriptor StaticMethod(Symbol name,
2176
+ napi_property_attributes attributes = napi_default,
2177
+ void* data = nullptr);
2178
+ static PropertyDescriptor StaticAccessor(const char* utf8name,
2179
+ StaticGetterCallback getter,
2180
+ StaticSetterCallback setter,
2181
+ napi_property_attributes attributes = napi_default,
2182
+ void* data = nullptr);
2183
+ static PropertyDescriptor StaticAccessor(Symbol name,
2184
+ StaticGetterCallback getter,
2185
+ StaticSetterCallback setter,
2186
+ napi_property_attributes attributes = napi_default,
2187
+ void* data = nullptr);
2188
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
2189
+ static PropertyDescriptor StaticAccessor(const char* utf8name,
2190
+ napi_property_attributes attributes = napi_default,
2191
+ void* data = nullptr);
2192
+ template <StaticGetterCallback getter, StaticSetterCallback setter=nullptr>
2193
+ static PropertyDescriptor StaticAccessor(Symbol name,
2194
+ napi_property_attributes attributes = napi_default,
2195
+ void* data = nullptr);
2196
+ static PropertyDescriptor StaticValue(const char* utf8name,
2197
+ Napi::Value value,
2198
+ napi_property_attributes attributes = napi_default);
2199
+ static PropertyDescriptor StaticValue(Symbol name,
2200
+ Napi::Value value,
2201
+ napi_property_attributes attributes = napi_default);
2202
+ virtual void Finalize(Napi::Env env);
2203
+
2204
+ private:
2205
+ using This = ObjectWrap<T>;
2206
+
2207
+ static napi_value ConstructorCallbackWrapper(napi_env env, napi_callback_info info);
2208
+ static napi_value StaticVoidMethodCallbackWrapper(napi_env env, napi_callback_info info);
2209
+ static napi_value StaticMethodCallbackWrapper(napi_env env, napi_callback_info info);
2210
+ static napi_value StaticGetterCallbackWrapper(napi_env env, napi_callback_info info);
2211
+ static napi_value StaticSetterCallbackWrapper(napi_env env, napi_callback_info info);
2212
+ static void FinalizeCallback(napi_env env, void* data, void* hint);
2213
+ static Function DefineClass(Napi::Env env,
2214
+ const char* utf8name,
2215
+ const size_t props_count,
2216
+ const napi_property_descriptor* props,
2217
+ void* data = nullptr);
2218
+
2219
+ using StaticVoidMethodCallbackData =
2220
+ MethodCallbackData<T, StaticVoidMethodCallback>;
2221
+ using StaticMethodCallbackData =
2222
+ MethodCallbackData<T, StaticMethodCallback>;
2223
+
2224
+ using StaticAccessorCallbackData =
2225
+ AccessorCallbackData<T, StaticGetterCallback, StaticSetterCallback>;
2226
+
2227
+ template <StaticSetterCallback method>
2228
+ static napi_value WrappedMethod(napi_env env,
2229
+ napi_callback_info info) NAPI_NOEXCEPT;
2230
+
2231
+ template <StaticSetterCallback setter> struct StaticSetterTag {};
2232
+
2233
+ template <StaticSetterCallback setter>
2234
+ static napi_callback WrapStaticSetter(StaticSetterTag<setter>)
2235
+ NAPI_NOEXCEPT {
2236
+ return &This::WrappedMethod<setter>;
2237
+ }
2238
+ static napi_callback WrapStaticSetter(StaticSetterTag<nullptr>)
2239
+ NAPI_NOEXCEPT {
2240
+ return nullptr;
2241
+ }
2242
+
2243
+ bool _construction_failed = true;
2244
+ };
2245
+
2246
+ class HandleScope {
2247
+ public:
2248
+ HandleScope(napi_env env, napi_handle_scope scope);
2249
+ explicit HandleScope(Napi::Env env);
2250
+ ~HandleScope();
2251
+
2252
+ // Disallow copying to prevent double close of napi_handle_scope
2253
+ NAPI_DISALLOW_ASSIGN_COPY(HandleScope)
2254
+
2255
+ operator napi_handle_scope() const;
2256
+
2257
+ Napi::Env Env() const;
2258
+
2259
+ private:
2260
+ napi_env _env;
2261
+ napi_handle_scope _scope;
2262
+ };
2263
+
2264
+ class EscapableHandleScope {
2265
+ public:
2266
+ EscapableHandleScope(napi_env env, napi_escapable_handle_scope scope);
2267
+ explicit EscapableHandleScope(Napi::Env env);
2268
+ ~EscapableHandleScope();
2269
+
2270
+ // Disallow copying to prevent double close of napi_escapable_handle_scope
2271
+ NAPI_DISALLOW_ASSIGN_COPY(EscapableHandleScope)
2272
+
2273
+ operator napi_escapable_handle_scope() const;
2274
+
2275
+ Napi::Env Env() const;
2276
+ Value Escape(napi_value escapee);
2277
+
2278
+ private:
2279
+ napi_env _env;
2280
+ napi_escapable_handle_scope _scope;
2281
+ };
2282
+
2283
+ #if (NAPI_VERSION > 2)
2284
+ class CallbackScope {
2285
+ public:
2286
+ CallbackScope(napi_env env, napi_callback_scope scope);
2287
+ CallbackScope(napi_env env, napi_async_context context);
2288
+ virtual ~CallbackScope();
2289
+
2290
+ // Disallow copying to prevent double close of napi_callback_scope
2291
+ NAPI_DISALLOW_ASSIGN_COPY(CallbackScope)
2292
+
2293
+ operator napi_callback_scope() const;
2294
+
2295
+ Napi::Env Env() const;
2296
+
2297
+ private:
2298
+ napi_env _env;
2299
+ napi_callback_scope _scope;
2300
+ };
2301
+ #endif
2302
+
2303
+ class AsyncContext {
2304
+ public:
2305
+ explicit AsyncContext(napi_env env, const char* resource_name);
2306
+ explicit AsyncContext(napi_env env, const char* resource_name, const Object& resource);
2307
+ virtual ~AsyncContext();
2308
+
2309
+ AsyncContext(AsyncContext&& other);
2310
+ AsyncContext& operator =(AsyncContext&& other);
2311
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncContext)
2312
+
2313
+ operator napi_async_context() const;
2314
+
2315
+ Napi::Env Env() const;
2316
+
2317
+ private:
2318
+ napi_env _env;
2319
+ napi_async_context _context;
2320
+ };
2321
+
2322
+ class AsyncWorker {
2323
+ public:
2324
+ virtual ~AsyncWorker();
2325
+
2326
+ // An async worker can be moved but cannot be copied.
2327
+ AsyncWorker(AsyncWorker&& other);
2328
+ AsyncWorker& operator =(AsyncWorker&& other);
2329
+ NAPI_DISALLOW_ASSIGN_COPY(AsyncWorker)
2330
+
2331
+ operator napi_async_work() const;
2332
+
2333
+ Napi::Env Env() const;
2334
+
2335
+ void Queue();
2336
+ void Cancel();
2337
+ void SuppressDestruct();
2338
+
2339
+ ObjectReference& Receiver();
2340
+ FunctionReference& Callback();
2341
+
2342
+ virtual void OnExecute(Napi::Env env);
2343
+ virtual void OnWorkComplete(Napi::Env env,
2344
+ napi_status status);
2345
+
2346
+ protected:
2347
+ explicit AsyncWorker(const Function& callback);
2348
+ explicit AsyncWorker(const Function& callback,
2349
+ const char* resource_name);
2350
+ explicit AsyncWorker(const Function& callback,
2351
+ const char* resource_name,
2352
+ const Object& resource);
2353
+ explicit AsyncWorker(const Object& receiver,
2354
+ const Function& callback);
2355
+ explicit AsyncWorker(const Object& receiver,
2356
+ const Function& callback,
2357
+ const char* resource_name);
2358
+ explicit AsyncWorker(const Object& receiver,
2359
+ const Function& callback,
2360
+ const char* resource_name,
2361
+ const Object& resource);
2362
+
2363
+ explicit AsyncWorker(Napi::Env env);
2364
+ explicit AsyncWorker(Napi::Env env,
2365
+ const char* resource_name);
2366
+ explicit AsyncWorker(Napi::Env env,
2367
+ const char* resource_name,
2368
+ const Object& resource);
2369
+
2370
+ virtual void Execute() = 0;
2371
+ virtual void OnOK();
2372
+ virtual void OnError(const Error& e);
2373
+ virtual void Destroy();
2374
+ virtual std::vector<napi_value> GetResult(Napi::Env env);
2375
+
2376
+ void SetError(const std::string& error);
2377
+
2378
+ private:
2379
+ static inline void OnAsyncWorkExecute(napi_env env, void* asyncworker);
2380
+ static inline void OnAsyncWorkComplete(napi_env env,
2381
+ napi_status status,
2382
+ void* asyncworker);
2383
+
2384
+ napi_env _env;
2385
+ napi_async_work _work;
2386
+ ObjectReference _receiver;
2387
+ FunctionReference _callback;
2388
+ std::string _error;
2389
+ bool _suppress_destruct;
2390
+ };
2391
+
2392
+ #if (NAPI_VERSION > 3 && !defined(__wasm32__))
2393
+ class ThreadSafeFunction {
2394
+ public:
2395
+ // This API may only be called from the main thread.
2396
+ template <typename ResourceString>
2397
+ static ThreadSafeFunction New(napi_env env,
2398
+ const Function& callback,
2399
+ ResourceString resourceName,
2400
+ size_t maxQueueSize,
2401
+ size_t initialThreadCount);
2402
+
2403
+ // This API may only be called from the main thread.
2404
+ template <typename ResourceString, typename ContextType>
2405
+ static ThreadSafeFunction New(napi_env env,
2406
+ const Function& callback,
2407
+ ResourceString resourceName,
2408
+ size_t maxQueueSize,
2409
+ size_t initialThreadCount,
2410
+ ContextType* context);
2411
+
2412
+ // This API may only be called from the main thread.
2413
+ template <typename ResourceString, typename Finalizer>
2414
+ static ThreadSafeFunction New(napi_env env,
2415
+ const Function& callback,
2416
+ ResourceString resourceName,
2417
+ size_t maxQueueSize,
2418
+ size_t initialThreadCount,
2419
+ Finalizer finalizeCallback);
2420
+
2421
+ // This API may only be called from the main thread.
2422
+ template <typename ResourceString, typename Finalizer,
2423
+ typename FinalizerDataType>
2424
+ static ThreadSafeFunction New(napi_env env,
2425
+ const Function& callback,
2426
+ ResourceString resourceName,
2427
+ size_t maxQueueSize,
2428
+ size_t initialThreadCount,
2429
+ Finalizer finalizeCallback,
2430
+ FinalizerDataType* data);
2431
+
2432
+ // This API may only be called from the main thread.
2433
+ template <typename ResourceString, typename ContextType, typename Finalizer>
2434
+ static ThreadSafeFunction New(napi_env env,
2435
+ const Function& callback,
2436
+ ResourceString resourceName,
2437
+ size_t maxQueueSize,
2438
+ size_t initialThreadCount,
2439
+ ContextType* context,
2440
+ Finalizer finalizeCallback);
2441
+
2442
+ // This API may only be called from the main thread.
2443
+ template <typename ResourceString, typename ContextType,
2444
+ typename Finalizer, typename FinalizerDataType>
2445
+ static ThreadSafeFunction New(napi_env env,
2446
+ const Function& callback,
2447
+ ResourceString resourceName,
2448
+ size_t maxQueueSize,
2449
+ size_t initialThreadCount,
2450
+ ContextType* context,
2451
+ Finalizer finalizeCallback,
2452
+ FinalizerDataType* data);
2453
+
2454
+ // This API may only be called from the main thread.
2455
+ template <typename ResourceString>
2456
+ static ThreadSafeFunction New(napi_env env,
2457
+ const Function& callback,
2458
+ const Object& resource,
2459
+ ResourceString resourceName,
2460
+ size_t maxQueueSize,
2461
+ size_t initialThreadCount);
2462
+
2463
+ // This API may only be called from the main thread.
2464
+ template <typename ResourceString, typename ContextType>
2465
+ static ThreadSafeFunction New(napi_env env,
2466
+ const Function& callback,
2467
+ const Object& resource,
2468
+ ResourceString resourceName,
2469
+ size_t maxQueueSize,
2470
+ size_t initialThreadCount,
2471
+ ContextType* context);
2472
+
2473
+ // This API may only be called from the main thread.
2474
+ template <typename ResourceString, typename Finalizer>
2475
+ static ThreadSafeFunction New(napi_env env,
2476
+ const Function& callback,
2477
+ const Object& resource,
2478
+ ResourceString resourceName,
2479
+ size_t maxQueueSize,
2480
+ size_t initialThreadCount,
2481
+ Finalizer finalizeCallback);
2482
+
2483
+ // This API may only be called from the main thread.
2484
+ template <typename ResourceString, typename Finalizer,
2485
+ typename FinalizerDataType>
2486
+ static ThreadSafeFunction New(napi_env env,
2487
+ const Function& callback,
2488
+ const Object& resource,
2489
+ ResourceString resourceName,
2490
+ size_t maxQueueSize,
2491
+ size_t initialThreadCount,
2492
+ Finalizer finalizeCallback,
2493
+ FinalizerDataType* data);
2494
+
2495
+ // This API may only be called from the main thread.
2496
+ template <typename ResourceString, typename ContextType, typename Finalizer>
2497
+ static ThreadSafeFunction New(napi_env env,
2498
+ const Function& callback,
2499
+ const Object& resource,
2500
+ ResourceString resourceName,
2501
+ size_t maxQueueSize,
2502
+ size_t initialThreadCount,
2503
+ ContextType* context,
2504
+ Finalizer finalizeCallback);
2505
+
2506
+ // This API may only be called from the main thread.
2507
+ template <typename ResourceString, typename ContextType,
2508
+ typename Finalizer, typename FinalizerDataType>
2509
+ static ThreadSafeFunction New(napi_env env,
2510
+ const Function& callback,
2511
+ const Object& resource,
2512
+ ResourceString resourceName,
2513
+ size_t maxQueueSize,
2514
+ size_t initialThreadCount,
2515
+ ContextType* context,
2516
+ Finalizer finalizeCallback,
2517
+ FinalizerDataType* data);
2518
+
2519
+ ThreadSafeFunction();
2520
+ ThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
2521
+
2522
+ operator napi_threadsafe_function() const;
2523
+
2524
+ // This API may be called from any thread.
2525
+ napi_status BlockingCall() const;
2526
+
2527
+ // This API may be called from any thread.
2528
+ template <typename Callback>
2529
+ napi_status BlockingCall(Callback callback) const;
2530
+
2531
+ // This API may be called from any thread.
2532
+ template <typename DataType, typename Callback>
2533
+ napi_status BlockingCall(DataType* data, Callback callback) const;
2534
+
2535
+ // This API may be called from any thread.
2536
+ napi_status NonBlockingCall() const;
2537
+
2538
+ // This API may be called from any thread.
2539
+ template <typename Callback>
2540
+ napi_status NonBlockingCall(Callback callback) const;
2541
+
2542
+ // This API may be called from any thread.
2543
+ template <typename DataType, typename Callback>
2544
+ napi_status NonBlockingCall(DataType* data, Callback callback) const;
2545
+
2546
+ // This API may only be called from the main thread.
2547
+ void Ref(napi_env env) const;
2548
+
2549
+ // This API may only be called from the main thread.
2550
+ void Unref(napi_env env) const;
2551
+
2552
+ // This API may be called from any thread.
2553
+ napi_status Acquire() const;
2554
+
2555
+ // This API may be called from any thread.
2556
+ napi_status Release();
2557
+
2558
+ // This API may be called from any thread.
2559
+ napi_status Abort();
2560
+
2561
+ struct ConvertibleContext
2562
+ {
2563
+ template <class T>
2564
+ operator T*() { return static_cast<T*>(context); }
2565
+ void* context;
2566
+ };
2567
+
2568
+ // This API may be called from any thread.
2569
+ ConvertibleContext GetContext() const;
2570
+
2571
+ private:
2572
+ using CallbackWrapper = std::function<void(Napi::Env, Napi::Function)>;
2573
+
2574
+ template <typename ResourceString, typename ContextType,
2575
+ typename Finalizer, typename FinalizerDataType>
2576
+ static ThreadSafeFunction New(napi_env env,
2577
+ const Function& callback,
2578
+ const Object& resource,
2579
+ ResourceString resourceName,
2580
+ size_t maxQueueSize,
2581
+ size_t initialThreadCount,
2582
+ ContextType* context,
2583
+ Finalizer finalizeCallback,
2584
+ FinalizerDataType* data,
2585
+ napi_finalize wrapper);
2586
+
2587
+ napi_status CallInternal(CallbackWrapper* callbackWrapper,
2588
+ napi_threadsafe_function_call_mode mode) const;
2589
+
2590
+ static void CallJS(napi_env env,
2591
+ napi_value jsCallback,
2592
+ void* context,
2593
+ void* data);
2594
+
2595
+ napi_threadsafe_function _tsfn;
2596
+ };
2597
+
2598
+ // A TypedThreadSafeFunction by default has no context (nullptr) and can
2599
+ // accept any type (void) to its CallJs.
2600
+ template <typename ContextType = std::nullptr_t,
2601
+ typename DataType = void,
2602
+ void (*CallJs)(Napi::Env, Napi::Function, ContextType*, DataType*) =
2603
+ nullptr>
2604
+ class TypedThreadSafeFunction {
2605
+ public:
2606
+ // This API may only be called from the main thread.
2607
+ // Helper function that returns nullptr if running Node-API 5+, otherwise a
2608
+ // non-empty, no-op Function. This provides the ability to specify at
2609
+ // compile-time a callback parameter to `New` that safely does no action
2610
+ // when targeting _any_ Node-API version.
2611
+ #if NAPI_VERSION > 4
2612
+ static std::nullptr_t EmptyFunctionFactory(Napi::Env env);
2613
+ #else
2614
+ static Napi::Function EmptyFunctionFactory(Napi::Env env);
2615
+ #endif
2616
+ static Napi::Function FunctionOrEmpty(Napi::Env env,
2617
+ Napi::Function& callback);
2618
+
2619
+ #if NAPI_VERSION > 4
2620
+ // This API may only be called from the main thread.
2621
+ // Creates a new threadsafe function with:
2622
+ // Callback [missing] Resource [missing] Finalizer [missing]
2623
+ template <typename ResourceString>
2624
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2625
+ napi_env env,
2626
+ ResourceString resourceName,
2627
+ size_t maxQueueSize,
2628
+ size_t initialThreadCount,
2629
+ ContextType* context = nullptr);
2630
+
2631
+ // This API may only be called from the main thread.
2632
+ // Creates a new threadsafe function with:
2633
+ // Callback [missing] Resource [passed] Finalizer [missing]
2634
+ template <typename ResourceString>
2635
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2636
+ napi_env env,
2637
+ const Object& resource,
2638
+ ResourceString resourceName,
2639
+ size_t maxQueueSize,
2640
+ size_t initialThreadCount,
2641
+ ContextType* context = nullptr);
2642
+
2643
+ // This API may only be called from the main thread.
2644
+ // Creates a new threadsafe function with:
2645
+ // Callback [missing] Resource [missing] Finalizer [passed]
2646
+ template <typename ResourceString,
2647
+ typename Finalizer,
2648
+ typename FinalizerDataType = void>
2649
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2650
+ napi_env env,
2651
+ ResourceString resourceName,
2652
+ size_t maxQueueSize,
2653
+ size_t initialThreadCount,
2654
+ ContextType* context,
2655
+ Finalizer finalizeCallback,
2656
+ FinalizerDataType* data = nullptr);
2657
+
2658
+ // This API may only be called from the main thread.
2659
+ // Creates a new threadsafe function with:
2660
+ // Callback [missing] Resource [passed] Finalizer [passed]
2661
+ template <typename ResourceString,
2662
+ typename Finalizer,
2663
+ typename FinalizerDataType = void>
2664
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2665
+ napi_env env,
2666
+ const Object& resource,
2667
+ ResourceString resourceName,
2668
+ size_t maxQueueSize,
2669
+ size_t initialThreadCount,
2670
+ ContextType* context,
2671
+ Finalizer finalizeCallback,
2672
+ FinalizerDataType* data = nullptr);
2673
+ #endif
2674
+
2675
+ // This API may only be called from the main thread.
2676
+ // Creates a new threadsafe function with:
2677
+ // Callback [passed] Resource [missing] Finalizer [missing]
2678
+ template <typename ResourceString>
2679
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2680
+ napi_env env,
2681
+ const Function& callback,
2682
+ ResourceString resourceName,
2683
+ size_t maxQueueSize,
2684
+ size_t initialThreadCount,
2685
+ ContextType* context = nullptr);
2686
+
2687
+ // This API may only be called from the main thread.
2688
+ // Creates a new threadsafe function with:
2689
+ // Callback [passed] Resource [passed] Finalizer [missing]
2690
+ template <typename ResourceString>
2691
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2692
+ napi_env env,
2693
+ const Function& callback,
2694
+ const Object& resource,
2695
+ ResourceString resourceName,
2696
+ size_t maxQueueSize,
2697
+ size_t initialThreadCount,
2698
+ ContextType* context = nullptr);
2699
+
2700
+ // This API may only be called from the main thread.
2701
+ // Creates a new threadsafe function with:
2702
+ // Callback [passed] Resource [missing] Finalizer [passed]
2703
+ template <typename ResourceString,
2704
+ typename Finalizer,
2705
+ typename FinalizerDataType = void>
2706
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2707
+ napi_env env,
2708
+ const Function& callback,
2709
+ ResourceString resourceName,
2710
+ size_t maxQueueSize,
2711
+ size_t initialThreadCount,
2712
+ ContextType* context,
2713
+ Finalizer finalizeCallback,
2714
+ FinalizerDataType* data = nullptr);
2715
+
2716
+ // This API may only be called from the main thread.
2717
+ // Creates a new threadsafe function with:
2718
+ // Callback [passed] Resource [passed] Finalizer [passed]
2719
+ template <typename CallbackType,
2720
+ typename ResourceString,
2721
+ typename Finalizer,
2722
+ typename FinalizerDataType>
2723
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2724
+ napi_env env,
2725
+ CallbackType callback,
2726
+ const Object& resource,
2727
+ ResourceString resourceName,
2728
+ size_t maxQueueSize,
2729
+ size_t initialThreadCount,
2730
+ ContextType* context,
2731
+ Finalizer finalizeCallback,
2732
+ FinalizerDataType* data = nullptr);
2733
+
2734
+ TypedThreadSafeFunction();
2735
+ TypedThreadSafeFunction(napi_threadsafe_function tsFunctionValue);
2736
+
2737
+ operator napi_threadsafe_function() const;
2738
+
2739
+ // This API may be called from any thread.
2740
+ napi_status BlockingCall(DataType* data = nullptr) const;
2741
+
2742
+ // This API may be called from any thread.
2743
+ napi_status NonBlockingCall(DataType* data = nullptr) const;
2744
+
2745
+ // This API may only be called from the main thread.
2746
+ void Ref(napi_env env) const;
2747
+
2748
+ // This API may only be called from the main thread.
2749
+ void Unref(napi_env env) const;
2750
+
2751
+ // This API may be called from any thread.
2752
+ napi_status Acquire() const;
2753
+
2754
+ // This API may be called from any thread.
2755
+ napi_status Release();
2756
+
2757
+ // This API may be called from any thread.
2758
+ napi_status Abort();
2759
+
2760
+ // This API may be called from any thread.
2761
+ ContextType* GetContext() const;
2762
+
2763
+ private:
2764
+ template <typename ResourceString,
2765
+ typename Finalizer,
2766
+ typename FinalizerDataType>
2767
+ static TypedThreadSafeFunction<ContextType, DataType, CallJs> New(
2768
+ napi_env env,
2769
+ const Function& callback,
2770
+ const Object& resource,
2771
+ ResourceString resourceName,
2772
+ size_t maxQueueSize,
2773
+ size_t initialThreadCount,
2774
+ ContextType* context,
2775
+ Finalizer finalizeCallback,
2776
+ FinalizerDataType* data,
2777
+ napi_finalize wrapper);
2778
+
2779
+ static void CallJsInternal(napi_env env,
2780
+ napi_value jsCallback,
2781
+ void* context,
2782
+ void* data);
2783
+
2784
+ protected:
2785
+ napi_threadsafe_function _tsfn;
2786
+ };
2787
+ template <typename DataType>
2788
+ class AsyncProgressWorkerBase : public AsyncWorker {
2789
+ public:
2790
+ virtual void OnWorkProgress(DataType* data) = 0;
2791
+ class ThreadSafeData {
2792
+ public:
2793
+ ThreadSafeData(AsyncProgressWorkerBase* asyncprogressworker, DataType* data)
2794
+ : _asyncprogressworker(asyncprogressworker), _data(data) {}
2795
+
2796
+ AsyncProgressWorkerBase* asyncprogressworker() { return _asyncprogressworker; };
2797
+ DataType* data() { return _data; };
2798
+
2799
+ private:
2800
+ AsyncProgressWorkerBase* _asyncprogressworker;
2801
+ DataType* _data;
2802
+ };
2803
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2804
+ protected:
2805
+ explicit AsyncProgressWorkerBase(const Object& receiver,
2806
+ const Function& callback,
2807
+ const char* resource_name,
2808
+ const Object& resource,
2809
+ size_t queue_size = 1);
2810
+ virtual ~AsyncProgressWorkerBase();
2811
+
2812
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2813
+ // Refs: https://github.com/nodejs/node/pull/27791
2814
+ #if NAPI_VERSION > 4
2815
+ explicit AsyncProgressWorkerBase(Napi::Env env,
2816
+ const char* resource_name,
2817
+ const Object& resource,
2818
+ size_t queue_size = 1);
2819
+ #endif
2820
+
2821
+ static inline void OnAsyncWorkProgress(Napi::Env env,
2822
+ Napi::Function jsCallback,
2823
+ void* data);
2824
+
2825
+ napi_status NonBlockingCall(DataType* data);
2826
+
2827
+ private:
2828
+ ThreadSafeFunction _tsfn;
2829
+ bool _work_completed = false;
2830
+ napi_status _complete_status;
2831
+ static inline void OnThreadSafeFunctionFinalize(Napi::Env env, void* data, AsyncProgressWorkerBase* context);
2832
+ };
2833
+
2834
+ template<class T>
2835
+ class AsyncProgressWorker : public AsyncProgressWorkerBase<void> {
2836
+ public:
2837
+ virtual ~AsyncProgressWorker();
2838
+
2839
+ class ExecutionProgress {
2840
+ friend class AsyncProgressWorker;
2841
+ public:
2842
+ void Signal() const;
2843
+ void Send(const T* data, size_t count) const;
2844
+ private:
2845
+ explicit ExecutionProgress(AsyncProgressWorker* worker) : _worker(worker) {}
2846
+ AsyncProgressWorker* const _worker;
2847
+ };
2848
+
2849
+ void OnWorkProgress(void*) override;
2850
+
2851
+ protected:
2852
+ explicit AsyncProgressWorker(const Function& callback);
2853
+ explicit AsyncProgressWorker(const Function& callback,
2854
+ const char* resource_name);
2855
+ explicit AsyncProgressWorker(const Function& callback,
2856
+ const char* resource_name,
2857
+ const Object& resource);
2858
+ explicit AsyncProgressWorker(const Object& receiver,
2859
+ const Function& callback);
2860
+ explicit AsyncProgressWorker(const Object& receiver,
2861
+ const Function& callback,
2862
+ const char* resource_name);
2863
+ explicit AsyncProgressWorker(const Object& receiver,
2864
+ const Function& callback,
2865
+ const char* resource_name,
2866
+ const Object& resource);
2867
+
2868
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2869
+ // Refs: https://github.com/nodejs/node/pull/27791
2870
+ #if NAPI_VERSION > 4
2871
+ explicit AsyncProgressWorker(Napi::Env env);
2872
+ explicit AsyncProgressWorker(Napi::Env env,
2873
+ const char* resource_name);
2874
+ explicit AsyncProgressWorker(Napi::Env env,
2875
+ const char* resource_name,
2876
+ const Object& resource);
2877
+ #endif
2878
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2879
+ virtual void OnProgress(const T* data, size_t count) = 0;
2880
+
2881
+ private:
2882
+ void Execute() override;
2883
+ void Signal() const;
2884
+ void SendProgress_(const T* data, size_t count);
2885
+
2886
+ std::mutex _mutex;
2887
+ T* _asyncdata;
2888
+ size_t _asyncsize;
2889
+ };
2890
+
2891
+ template<class T>
2892
+ class AsyncProgressQueueWorker : public AsyncProgressWorkerBase<std::pair<T*, size_t>> {
2893
+ public:
2894
+ virtual ~AsyncProgressQueueWorker() {};
2895
+
2896
+ class ExecutionProgress {
2897
+ friend class AsyncProgressQueueWorker;
2898
+ public:
2899
+ void Signal() const;
2900
+ void Send(const T* data, size_t count) const;
2901
+ private:
2902
+ explicit ExecutionProgress(AsyncProgressQueueWorker* worker) : _worker(worker) {}
2903
+ AsyncProgressQueueWorker* const _worker;
2904
+ };
2905
+
2906
+ void OnWorkComplete(Napi::Env env, napi_status status) override;
2907
+ void OnWorkProgress(std::pair<T*, size_t>*) override;
2908
+
2909
+ protected:
2910
+ explicit AsyncProgressQueueWorker(const Function& callback);
2911
+ explicit AsyncProgressQueueWorker(const Function& callback,
2912
+ const char* resource_name);
2913
+ explicit AsyncProgressQueueWorker(const Function& callback,
2914
+ const char* resource_name,
2915
+ const Object& resource);
2916
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2917
+ const Function& callback);
2918
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2919
+ const Function& callback,
2920
+ const char* resource_name);
2921
+ explicit AsyncProgressQueueWorker(const Object& receiver,
2922
+ const Function& callback,
2923
+ const char* resource_name,
2924
+ const Object& resource);
2925
+
2926
+ // Optional callback of Napi::ThreadSafeFunction only available after NAPI_VERSION 4.
2927
+ // Refs: https://github.com/nodejs/node/pull/27791
2928
+ #if NAPI_VERSION > 4
2929
+ explicit AsyncProgressQueueWorker(Napi::Env env);
2930
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2931
+ const char* resource_name);
2932
+ explicit AsyncProgressQueueWorker(Napi::Env env,
2933
+ const char* resource_name,
2934
+ const Object& resource);
2935
+ #endif
2936
+ virtual void Execute(const ExecutionProgress& progress) = 0;
2937
+ virtual void OnProgress(const T* data, size_t count) = 0;
2938
+
2939
+ private:
2940
+ void Execute() override;
2941
+ void Signal() const;
2942
+ void SendProgress_(const T* data, size_t count);
2943
+ };
2944
+ #endif // NAPI_VERSION > 3 && !defined(__wasm32__)
2945
+
2946
+ // Memory management.
2947
+ class MemoryManagement {
2948
+ public:
2949
+ static int64_t AdjustExternalMemory(Env env, int64_t change_in_bytes);
2950
+ };
2951
+
2952
+ // Version management
2953
+ class VersionManagement {
2954
+ public:
2955
+ static uint32_t GetNapiVersion(Env env);
2956
+ static const napi_node_version* GetNodeVersion(Env env);
2957
+ };
2958
+
2959
+ #if NAPI_VERSION > 5
2960
+ template <typename T>
2961
+ class Addon : public InstanceWrap<T> {
2962
+ public:
2963
+ static inline Object Init(Env env, Object exports);
2964
+ static T* Unwrap(Object wrapper);
2965
+
2966
+ protected:
2967
+ using AddonProp = ClassPropertyDescriptor<T>;
2968
+ void DefineAddon(Object exports,
2969
+ const std::initializer_list<AddonProp>& props);
2970
+ Napi::Object DefineProperties(Object object,
2971
+ const std::initializer_list<AddonProp>& props);
2972
+
2973
+ private:
2974
+ Object entry_point_;
2975
+ };
2976
+ #endif // NAPI_VERSION > 5
2977
+
2978
+ } // namespace Napi
2979
+
2980
+ // Inline implementations of all the above class methods are included here.
2981
+ #include "napi-inl.h"
2982
+
2983
+ #endif // SRC_NAPI_H_