koffi 1.1.0-beta.1 → 1.1.0-beta.4

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 (270) hide show
  1. package/CMakeLists.txt +4 -0
  2. package/README.md +20 -11
  3. package/build/qemu/1.1.0-beta.4/koffi_darwin_x64.tar.gz +0 -0
  4. package/build/qemu/1.1.0-beta.4/koffi_freebsd_arm64.tar.gz +0 -0
  5. package/build/qemu/1.1.0-beta.4/koffi_freebsd_ia32.tar.gz +0 -0
  6. package/build/qemu/1.1.0-beta.4/koffi_freebsd_x64.tar.gz +0 -0
  7. package/build/qemu/1.1.0-beta.4/koffi_linux_arm.tar.gz +0 -0
  8. package/build/qemu/1.1.0-beta.4/koffi_linux_arm64.tar.gz +0 -0
  9. package/build/qemu/1.1.0-beta.4/koffi_linux_ia32.tar.gz +0 -0
  10. package/build/qemu/1.1.0-beta.4/koffi_linux_x64.tar.gz +0 -0
  11. package/build/qemu/1.1.0-beta.4/koffi_win32_ia32.tar.gz +0 -0
  12. package/build/qemu/1.1.0-beta.4/koffi_win32_x64.tar.gz +0 -0
  13. package/package.json +1 -1
  14. package/qemu/qemu.js +1 -0
  15. package/src/abi_arm32.cc +27 -45
  16. package/src/abi_arm64.cc +58 -132
  17. package/src/abi_x64_sysv.cc +24 -7
  18. package/src/abi_x64_win.cc +3 -0
  19. package/src/abi_x86.cc +3 -0
  20. package/src/call.cc +540 -33
  21. package/src/call.hh +5 -3
  22. package/src/ffi.cc +129 -4
  23. package/src/ffi.hh +5 -1
  24. package/src/parser.cc +7 -2
  25. package/src/util.cc +72 -0
  26. package/src/util.hh +2 -0
  27. package/test/misc.c +14 -8
  28. package/vendor/node-addon-api/CODE_OF_CONDUCT.md +4 -0
  29. package/vendor/node-addon-api/CONTRIBUTING.md +93 -0
  30. package/vendor/node-addon-api/appveyor.yml +37 -0
  31. package/vendor/node-addon-api/benchmark/README.md +47 -0
  32. package/vendor/node-addon-api/benchmark/binding.gyp +25 -0
  33. package/vendor/node-addon-api/benchmark/function_args.cc +217 -0
  34. package/vendor/node-addon-api/benchmark/function_args.js +60 -0
  35. package/vendor/node-addon-api/benchmark/index.js +34 -0
  36. package/vendor/node-addon-api/benchmark/property_descriptor.cc +91 -0
  37. package/vendor/node-addon-api/benchmark/property_descriptor.js +37 -0
  38. package/vendor/node-addon-api/doc/addon.md +163 -0
  39. package/vendor/node-addon-api/doc/array.md +81 -0
  40. package/vendor/node-addon-api/doc/array_buffer.md +155 -0
  41. package/vendor/node-addon-api/doc/async_context.md +86 -0
  42. package/vendor/node-addon-api/doc/async_operations.md +31 -0
  43. package/vendor/node-addon-api/doc/async_worker.md +427 -0
  44. package/vendor/node-addon-api/doc/async_worker_variants.md +557 -0
  45. package/vendor/node-addon-api/doc/bigint.md +97 -0
  46. package/vendor/node-addon-api/doc/boolean.md +68 -0
  47. package/vendor/node-addon-api/doc/buffer.md +150 -0
  48. package/vendor/node-addon-api/doc/callback_scope.md +54 -0
  49. package/vendor/node-addon-api/doc/callbackinfo.md +97 -0
  50. package/vendor/node-addon-api/doc/checker-tool.md +32 -0
  51. package/vendor/node-addon-api/doc/class_property_descriptor.md +123 -0
  52. package/vendor/node-addon-api/doc/cmake-js.md +68 -0
  53. package/vendor/node-addon-api/doc/conversion-tool.md +28 -0
  54. package/vendor/node-addon-api/doc/creating_a_release.md +62 -0
  55. package/vendor/node-addon-api/doc/dataview.md +248 -0
  56. package/vendor/node-addon-api/doc/date.md +68 -0
  57. package/vendor/node-addon-api/doc/env.md +196 -0
  58. package/vendor/node-addon-api/doc/error.md +120 -0
  59. package/vendor/node-addon-api/doc/error_handling.md +254 -0
  60. package/vendor/node-addon-api/doc/escapable_handle_scope.md +80 -0
  61. package/vendor/node-addon-api/doc/external.md +63 -0
  62. package/vendor/node-addon-api/doc/function.md +402 -0
  63. package/vendor/node-addon-api/doc/function_reference.md +238 -0
  64. package/vendor/node-addon-api/doc/generator.md +13 -0
  65. package/vendor/node-addon-api/doc/handle_scope.md +63 -0
  66. package/vendor/node-addon-api/doc/hierarchy.md +91 -0
  67. package/vendor/node-addon-api/doc/instance_wrap.md +408 -0
  68. package/vendor/node-addon-api/doc/maybe.md +76 -0
  69. package/vendor/node-addon-api/doc/memory_management.md +27 -0
  70. package/vendor/node-addon-api/doc/name.md +29 -0
  71. package/vendor/node-addon-api/doc/node-gyp.md +82 -0
  72. package/vendor/node-addon-api/doc/number.md +163 -0
  73. package/vendor/node-addon-api/doc/object.md +411 -0
  74. package/vendor/node-addon-api/doc/object_lifetime_management.md +83 -0
  75. package/vendor/node-addon-api/doc/object_reference.md +117 -0
  76. package/vendor/node-addon-api/doc/object_wrap.md +588 -0
  77. package/vendor/node-addon-api/doc/prebuild_tools.md +16 -0
  78. package/vendor/node-addon-api/doc/promises.md +79 -0
  79. package/vendor/node-addon-api/doc/property_descriptor.md +286 -0
  80. package/vendor/node-addon-api/doc/propertylvalue.md +50 -0
  81. package/vendor/node-addon-api/doc/range_error.md +59 -0
  82. package/vendor/node-addon-api/doc/reference.md +113 -0
  83. package/vendor/node-addon-api/doc/setup.md +110 -0
  84. package/vendor/node-addon-api/doc/string.md +93 -0
  85. package/vendor/node-addon-api/doc/symbol.md +61 -0
  86. package/vendor/node-addon-api/doc/threadsafe.md +121 -0
  87. package/vendor/node-addon-api/doc/threadsafe_function.md +290 -0
  88. package/vendor/node-addon-api/doc/type_error.md +59 -0
  89. package/vendor/node-addon-api/doc/typed_array.md +78 -0
  90. package/vendor/node-addon-api/doc/typed_array_of.md +137 -0
  91. package/vendor/node-addon-api/doc/typed_threadsafe_function.md +306 -0
  92. package/vendor/node-addon-api/doc/value.md +340 -0
  93. package/vendor/node-addon-api/doc/version_management.md +43 -0
  94. package/vendor/node-addon-api/package.json +415 -0
  95. package/vendor/node-addon-api/test/README.md +91 -0
  96. package/vendor/node-addon-api/test/addon.cc +36 -0
  97. package/vendor/node-addon-api/test/addon.js +11 -0
  98. package/vendor/node-addon-api/test/addon_build/index.js +49 -0
  99. package/vendor/node-addon-api/test/addon_build/tpl/addon.cc +17 -0
  100. package/vendor/node-addon-api/test/addon_build/tpl/binding.gyp +62 -0
  101. package/vendor/node-addon-api/test/addon_build/tpl/index.js +9 -0
  102. package/vendor/node-addon-api/test/addon_build/tpl/package.json +11 -0
  103. package/vendor/node-addon-api/test/addon_data.cc +99 -0
  104. package/vendor/node-addon-api/test/addon_data.js +46 -0
  105. package/vendor/node-addon-api/test/array_buffer.cc +243 -0
  106. package/vendor/node-addon-api/test/array_buffer.js +69 -0
  107. package/vendor/node-addon-api/test/async_context.cc +36 -0
  108. package/vendor/node-addon-api/test/async_context.js +122 -0
  109. package/vendor/node-addon-api/test/async_progress_queue_worker.cc +83 -0
  110. package/vendor/node-addon-api/test/async_progress_queue_worker.js +46 -0
  111. package/vendor/node-addon-api/test/async_progress_worker.cc +134 -0
  112. package/vendor/node-addon-api/test/async_progress_worker.js +61 -0
  113. package/vendor/node-addon-api/test/async_worker.cc +106 -0
  114. package/vendor/node-addon-api/test/async_worker.js +179 -0
  115. package/vendor/node-addon-api/test/async_worker_nocallback.js +13 -0
  116. package/vendor/node-addon-api/test/async_worker_persistent.cc +63 -0
  117. package/vendor/node-addon-api/test/async_worker_persistent.js +24 -0
  118. package/vendor/node-addon-api/test/basic_types/array.cc +40 -0
  119. package/vendor/node-addon-api/test/basic_types/array.js +35 -0
  120. package/vendor/node-addon-api/test/basic_types/boolean.cc +38 -0
  121. package/vendor/node-addon-api/test/basic_types/boolean.js +35 -0
  122. package/vendor/node-addon-api/test/basic_types/number.cc +99 -0
  123. package/vendor/node-addon-api/test/basic_types/number.js +114 -0
  124. package/vendor/node-addon-api/test/basic_types/value.cc +120 -0
  125. package/vendor/node-addon-api/test/basic_types/value.js +133 -0
  126. package/vendor/node-addon-api/test/bigint.cc +91 -0
  127. package/vendor/node-addon-api/test/bigint.js +53 -0
  128. package/vendor/node-addon-api/test/binding-swallowexcept.cc +12 -0
  129. package/vendor/node-addon-api/test/binding.cc +173 -0
  130. package/vendor/node-addon-api/test/binding.gyp +124 -0
  131. package/vendor/node-addon-api/test/buffer.cc +183 -0
  132. package/vendor/node-addon-api/test/buffer.js +69 -0
  133. package/vendor/node-addon-api/test/callbackscope.cc +22 -0
  134. package/vendor/node-addon-api/test/callbackscope.js +49 -0
  135. package/vendor/node-addon-api/test/common/index.js +114 -0
  136. package/vendor/node-addon-api/test/common/test_helper.h +71 -0
  137. package/vendor/node-addon-api/test/dataview/dataview.cc +48 -0
  138. package/vendor/node-addon-api/test/dataview/dataview.js +35 -0
  139. package/vendor/node-addon-api/test/dataview/dataview_read_write.cc +115 -0
  140. package/vendor/node-addon-api/test/dataview/dataview_read_write.js +90 -0
  141. package/vendor/node-addon-api/test/date.cc +44 -0
  142. package/vendor/node-addon-api/test/date.js +18 -0
  143. package/vendor/node-addon-api/test/env_cleanup.cc +88 -0
  144. package/vendor/node-addon-api/test/env_cleanup.js +56 -0
  145. package/vendor/node-addon-api/test/error.cc +287 -0
  146. package/vendor/node-addon-api/test/error.js +81 -0
  147. package/vendor/node-addon-api/test/error_handling_for_primitives.cc +13 -0
  148. package/vendor/node-addon-api/test/error_handling_for_primitives.js +29 -0
  149. package/vendor/node-addon-api/test/error_terminating_environment.js +95 -0
  150. package/vendor/node-addon-api/test/external.cc +81 -0
  151. package/vendor/node-addon-api/test/external.js +88 -0
  152. package/vendor/node-addon-api/test/function.cc +324 -0
  153. package/vendor/node-addon-api/test/function.js +133 -0
  154. package/vendor/node-addon-api/test/function_reference.cc +202 -0
  155. package/vendor/node-addon-api/test/function_reference.js +157 -0
  156. package/vendor/node-addon-api/test/globalObject/global_object.cc +61 -0
  157. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.cc +31 -0
  158. package/vendor/node-addon-api/test/globalObject/global_object_delete_property.js +61 -0
  159. package/vendor/node-addon-api/test/globalObject/global_object_get_property.cc +40 -0
  160. package/vendor/node-addon-api/test/globalObject/global_object_get_property.js +57 -0
  161. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.cc +28 -0
  162. package/vendor/node-addon-api/test/globalObject/global_object_has_own_property.js +48 -0
  163. package/vendor/node-addon-api/test/globalObject/global_object_set_property.cc +31 -0
  164. package/vendor/node-addon-api/test/globalObject/global_object_set_property.js +58 -0
  165. package/vendor/node-addon-api/test/handlescope.cc +60 -0
  166. package/vendor/node-addon-api/test/handlescope.js +14 -0
  167. package/vendor/node-addon-api/test/index.js +159 -0
  168. package/vendor/node-addon-api/test/maybe/check.cc +23 -0
  169. package/vendor/node-addon-api/test/maybe/index.js +38 -0
  170. package/vendor/node-addon-api/test/memory_management.cc +17 -0
  171. package/vendor/node-addon-api/test/memory_management.js +9 -0
  172. package/vendor/node-addon-api/test/movable_callbacks.cc +23 -0
  173. package/vendor/node-addon-api/test/movable_callbacks.js +21 -0
  174. package/vendor/node-addon-api/test/name.cc +108 -0
  175. package/vendor/node-addon-api/test/name.js +59 -0
  176. package/vendor/node-addon-api/test/napi_child.js +14 -0
  177. package/vendor/node-addon-api/test/object/delete_property.cc +38 -0
  178. package/vendor/node-addon-api/test/object/delete_property.js +41 -0
  179. package/vendor/node-addon-api/test/object/finalizer.cc +29 -0
  180. package/vendor/node-addon-api/test/object/finalizer.js +28 -0
  181. package/vendor/node-addon-api/test/object/get_property.cc +34 -0
  182. package/vendor/node-addon-api/test/object/get_property.js +40 -0
  183. package/vendor/node-addon-api/test/object/has_own_property.cc +34 -0
  184. package/vendor/node-addon-api/test/object/has_own_property.js +34 -0
  185. package/vendor/node-addon-api/test/object/has_property.cc +38 -0
  186. package/vendor/node-addon-api/test/object/has_property.js +37 -0
  187. package/vendor/node-addon-api/test/object/object.cc +350 -0
  188. package/vendor/node-addon-api/test/object/object.js +217 -0
  189. package/vendor/node-addon-api/test/object/object_deprecated.cc +66 -0
  190. package/vendor/node-addon-api/test/object/object_deprecated.js +47 -0
  191. package/vendor/node-addon-api/test/object/object_freeze_seal.cc +25 -0
  192. package/vendor/node-addon-api/test/object/object_freeze_seal.js +61 -0
  193. package/vendor/node-addon-api/test/object/set_property.cc +45 -0
  194. package/vendor/node-addon-api/test/object/set_property.js +30 -0
  195. package/vendor/node-addon-api/test/object/subscript_operator.cc +58 -0
  196. package/vendor/node-addon-api/test/object/subscript_operator.js +17 -0
  197. package/vendor/node-addon-api/test/object_reference.cc +219 -0
  198. package/vendor/node-addon-api/test/object_reference.js +259 -0
  199. package/vendor/node-addon-api/test/objectwrap.cc +268 -0
  200. package/vendor/node-addon-api/test/objectwrap.js +284 -0
  201. package/vendor/node-addon-api/test/objectwrap_constructor_exception.cc +26 -0
  202. package/vendor/node-addon-api/test/objectwrap_constructor_exception.js +18 -0
  203. package/vendor/node-addon-api/test/objectwrap_function.cc +45 -0
  204. package/vendor/node-addon-api/test/objectwrap_function.js +22 -0
  205. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.cc +30 -0
  206. package/vendor/node-addon-api/test/objectwrap_multiple_inheritance.js +13 -0
  207. package/vendor/node-addon-api/test/objectwrap_removewrap.cc +45 -0
  208. package/vendor/node-addon-api/test/objectwrap_removewrap.js +40 -0
  209. package/vendor/node-addon-api/test/objectwrap_worker_thread.js +19 -0
  210. package/vendor/node-addon-api/test/promise.cc +29 -0
  211. package/vendor/node-addon-api/test/promise.js +18 -0
  212. package/vendor/node-addon-api/test/reference.cc +24 -0
  213. package/vendor/node-addon-api/test/reference.js +14 -0
  214. package/vendor/node-addon-api/test/run_script.cc +56 -0
  215. package/vendor/node-addon-api/test/run_script.js +45 -0
  216. package/vendor/node-addon-api/test/symbol.cc +79 -0
  217. package/vendor/node-addon-api/test/symbol.js +73 -0
  218. package/vendor/node-addon-api/test/testUtil.js +54 -0
  219. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.cc +195 -0
  220. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function.js +188 -0
  221. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.cc +63 -0
  222. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ctx.js +12 -0
  223. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.cc +115 -0
  224. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_existing_tsfn.js +14 -0
  225. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.cc +26 -0
  226. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_ptr.js +7 -0
  227. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.cc +225 -0
  228. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_sum.js +59 -0
  229. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.cc +42 -0
  230. package/vendor/node-addon-api/test/threadsafe_function/threadsafe_function_unref.js +53 -0
  231. package/vendor/node-addon-api/test/thunking_manual.cc +140 -0
  232. package/vendor/node-addon-api/test/thunking_manual.js +17 -0
  233. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.cc +215 -0
  234. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function.js +188 -0
  235. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.cc +68 -0
  236. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ctx.js +12 -0
  237. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.cc +127 -0
  238. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_existing_tsfn.js +14 -0
  239. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.cc +28 -0
  240. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_ptr.js +7 -0
  241. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.cc +237 -0
  242. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_sum.js +59 -0
  243. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.cc +53 -0
  244. package/vendor/node-addon-api/test/typed_threadsafe_function/typed_threadsafe_function_unref.js +53 -0
  245. package/vendor/node-addon-api/test/typedarray-bigint.js +58 -0
  246. package/vendor/node-addon-api/test/typedarray.cc +216 -0
  247. package/vendor/node-addon-api/test/typedarray.js +69 -0
  248. package/vendor/node-addon-api/test/version_management.cc +27 -0
  249. package/vendor/node-addon-api/test/version_management.js +31 -0
  250. package/vendor/node-addon-api/unit-test/README.md +28 -0
  251. package/vendor/node-addon-api/unit-test/binding-file-template.js +39 -0
  252. package/vendor/node-addon-api/unit-test/binding.gyp +72 -0
  253. package/vendor/node-addon-api/unit-test/exceptions.js +32 -0
  254. package/vendor/node-addon-api/unit-test/generate-binding-cc.js +61 -0
  255. package/vendor/node-addon-api/unit-test/injectTestParams.js +101 -0
  256. package/vendor/node-addon-api/unit-test/listOfTestModules.js +88 -0
  257. package/vendor/node-addon-api/unit-test/matchModules.js +65 -0
  258. package/vendor/node-addon-api/unit-test/setup.js +13 -0
  259. package/vendor/node-addon-api/unit-test/spawnTask.js +26 -0
  260. package/vendor/node-addon-api/unit-test/test.js +30 -0
  261. package/build/qemu/1.1.0-beta.1/koffi_darwin_x64.tar.gz +0 -0
  262. package/build/qemu/1.1.0-beta.1/koffi_freebsd_arm64.tar.gz +0 -0
  263. package/build/qemu/1.1.0-beta.1/koffi_freebsd_ia32.tar.gz +0 -0
  264. package/build/qemu/1.1.0-beta.1/koffi_freebsd_x64.tar.gz +0 -0
  265. package/build/qemu/1.1.0-beta.1/koffi_linux_arm.tar.gz +0 -0
  266. package/build/qemu/1.1.0-beta.1/koffi_linux_arm64.tar.gz +0 -0
  267. package/build/qemu/1.1.0-beta.1/koffi_linux_ia32.tar.gz +0 -0
  268. package/build/qemu/1.1.0-beta.1/koffi_linux_x64.tar.gz +0 -0
  269. package/build/qemu/1.1.0-beta.1/koffi_win32_ia32.tar.gz +0 -0
  270. package/build/qemu/1.1.0-beta.1/koffi_win32_x64.tar.gz +0 -0
@@ -0,0 +1,254 @@
1
+ # Error handling
2
+
3
+ Error handling represents one of the most important considerations when
4
+ implementing a Node.js native add-on. When an error occurs in your C++ code you
5
+ have to handle and dispatch it correctly. **node-addon-api** uses return values and
6
+ JavaScript exceptions for error handling. You can choose return values or
7
+ exception handling based on the mechanism that works best for your add-on.
8
+
9
+ The `Napi::Error` is a persistent reference (for more info see: [`Napi::ObjectReference`](object_reference.md))
10
+ to a JavaScript error object. Use of this class depends on whether C++
11
+ exceptions are enabled at compile time.
12
+
13
+ If C++ exceptions are enabled (for more info see: [Setup](setup.md)), then the
14
+ `Napi::Error` class extends `std::exception` and enables integrated
15
+ error-handling for C++ exceptions and JavaScript exceptions.
16
+
17
+ Note, that due to limitations of the Node-API, if one attempts to cast the error object wrapping a primitive inside a C++ addon, the wrapped object
18
+ will be received instead. (With property `4bda9e7e-4913-4dbc-95de-891cbf66598e-errorVal` containing the primitive value thrown)
19
+
20
+ The following sections explain the approach for each case:
21
+
22
+ - [Handling Errors With C++ Exceptions](#exceptions)
23
+ - [Handling Errors With Maybe Type and C++ Exceptions Disabled](#noexceptions-maybe)
24
+ - [Handling Errors Without C++ Exceptions](#noexceptions)
25
+
26
+ <a name="exceptions"></a>
27
+
28
+ In most cases when an error occurs, the addon should do whatever cleanup is possible
29
+ and then return to JavaScript so that the error can be propagated. In less frequent
30
+ cases the addon may be able to recover from the error, clear the error and then
31
+ continue.
32
+
33
+ ## Handling Errors With C++ Exceptions
34
+
35
+ When C++ exceptions are enabled try/catch can be used to catch exceptions thrown
36
+ from calls to JavaScript and then they can either be handled or rethrown before
37
+ returning from a native method.
38
+
39
+ If a node-addon-api call fails without executing any JavaScript code (for example due to
40
+ an invalid argument), then node-addon-api automatically converts and throws
41
+ the error as a C++ exception of type `Napi::Error`.
42
+
43
+ If a JavaScript function called by C++ code via node-addon-api throws a JavaScript
44
+ exception, then node-addon-api automatically converts and throws it as a C++
45
+ exception of type `Napi::Error` on return from the JavaScript code to the native
46
+ method.
47
+
48
+ If a C++ exception of type `Napi::Error` escapes from a Node-API C++ callback, then
49
+ the Node-API wrapper automatically converts and throws it as a JavaScript exception.
50
+
51
+ On return from a native method, node-addon-api will automatically convert a pending C++
52
+ exception to a JavaScript exception.
53
+
54
+ When C++ exceptions are enabled try/catch can be used to catch exceptions thrown
55
+ from calls to JavaScript and then they can either be handled or rethrown before
56
+ returning from a native method.
57
+
58
+ ## Examples with C++ exceptions enabled
59
+
60
+ ### Throwing a C++ exception
61
+
62
+ ```cpp
63
+ Env env = ...
64
+ throw Napi::Error::New(env, "Example exception");
65
+ // other C++ statements
66
+ // ...
67
+ ```
68
+
69
+ The statements following the throw statement will not be executed. The exception
70
+ will bubble up as a C++ exception of type `Napi::Error`, until it is either caught
71
+ while still in C++, or else automatically propagated as a JavaScript exception
72
+ when returning to JavaScript.
73
+
74
+ ### Propagating a Node-API C++ exception
75
+
76
+ ```cpp
77
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
78
+ Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
79
+ // other C++ statements
80
+ // ...
81
+ ```
82
+
83
+ The C++ statements following the call to the JavaScript function will not be
84
+ executed. The exception will bubble up as a C++ exception of type `Napi::Error`,
85
+ until it is either caught while still in C++, or else automatically propagated as
86
+ a JavaScript exception when returning to JavaScript.
87
+
88
+ ### Handling a Node-API C++ exception
89
+
90
+ ```cpp
91
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
92
+ Napi::Value result;
93
+ try {
94
+ result = jsFunctionThatThrows({ arg1, arg2 });
95
+ } catch (const Error& e) {
96
+ cerr << "Caught JavaScript exception: " + e.what();
97
+ }
98
+ ```
99
+
100
+ Since the exception was caught here, it will not be propagated as a JavaScript
101
+ exception.
102
+
103
+ <a name="noexceptions-maybe"></a>
104
+
105
+ ## Handling Errors With Maybe Type and C++ Exceptions Disabled
106
+
107
+ If C++ exceptions are disabled (for more info see: [Setup](setup.md)), then the
108
+ `Napi::Error` class does not extend `std::exception`. This means that any calls to
109
+ node-addon-api functions do not throw a C++ exceptions. Instead, these node-api
110
+ functions that call into JavaScript are returning with `Maybe` boxed values.
111
+ In that case, the calling side should convert the `Maybe` boxed values with
112
+ checks to ensure that the call did succeed and therefore no exception is pending.
113
+ If the check fails, that is to say, the returning value is _empty_, the calling
114
+ side should determine what to do with `env.GetAndClearPendingException()` before
115
+ attempting to call another node-api (for more info see: [Env](env.md)).
116
+
117
+ The conversion from the `Maybe` boxed value to the actual return value is
118
+ enforced by compilers so that the exceptions must be properly handled before
119
+ continuing.
120
+
121
+ ## Examples with Maybe Type and C++ exceptions disabled
122
+
123
+ ### Throwing a JS exception
124
+
125
+ ```cpp
126
+ Napi::Env env = ...
127
+ Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException();
128
+ return;
129
+ ```
130
+
131
+ After throwing a JavaScript exception, the code should generally return
132
+ immediately from the native callback, after performing any necessary cleanup.
133
+
134
+ ### Propagating a Node-API JS exception
135
+
136
+ ```cpp
137
+ Napi::Env env = ...
138
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
139
+ Maybe<Napi::Value> maybeResult = jsFunctionThatThrows({ arg1, arg2 });
140
+ Napi::Value result;
141
+ if (!maybeResult.To(&result)) {
142
+ // The Maybe is empty, calling into js failed, cleaning up...
143
+ // It is recommended to return an empty Maybe if the procedure failed.
144
+ return result;
145
+ }
146
+ ```
147
+
148
+ If `maybeResult.To(&result)` returns false a JavaScript exception is pending.
149
+ To let the exception propagate, the code should generally return immediately
150
+ from the native callback, after performing any necessary cleanup.
151
+
152
+ ### Handling a Node-API JS exception
153
+
154
+ ```cpp
155
+ Napi::Env env = ...
156
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
157
+ Maybe<Napi::Value> maybeResult = jsFunctionThatThrows({ arg1, arg2 });
158
+ if (maybeResult.IsNothing()) {
159
+ Napi::Error e = env.GetAndClearPendingException();
160
+ cerr << "Caught JavaScript exception: " + e.Message();
161
+ }
162
+ ```
163
+
164
+ Since the exception was cleared here, it will not be propagated as a JavaScript
165
+ exception after the native callback returns.
166
+
167
+ <a name="noexceptions"></a>
168
+
169
+ ## Handling Errors Without C++ Exceptions
170
+
171
+ If C++ exceptions are disabled (for more info see: [Setup](setup.md)), then the
172
+ `Napi::Error` class does not extend `std::exception`. This means that any calls to
173
+ node-addon-api function do not throw a C++ exceptions. Instead, it raises
174
+ _pending_ JavaScript exceptions and returns an _empty_ `Napi::Value`.
175
+ The calling code should check `env.IsExceptionPending()` before attempting to use a
176
+ returned value, and may use methods on the `Napi::Env` class
177
+ to check for, get, and clear a pending JavaScript exception (for more info see: [Env](env.md)).
178
+ If the pending exception is not cleared, it will be thrown when the native code
179
+ returns to JavaScript.
180
+
181
+ ## Examples with C++ exceptions disabled
182
+
183
+ ### Throwing a JS exception
184
+
185
+ ```cpp
186
+ Napi::Env env = ...
187
+ Napi::Error::New(env, "Example exception").ThrowAsJavaScriptException();
188
+ return;
189
+ ```
190
+
191
+ After throwing a JavaScript exception, the code should generally return
192
+ immediately from the native callback, after performing any necessary cleanup.
193
+
194
+ ### Propagating a Node-API JS exception
195
+
196
+ ```cpp
197
+ Napi::Env env = ...
198
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
199
+ Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
200
+ if (env.IsExceptionPending()) {
201
+ Error e = env.GetAndClearPendingException();
202
+ return e.Value();
203
+ }
204
+ ```
205
+
206
+ If env.IsExceptionPending() returns true a JavaScript exception is pending. To
207
+ let the exception propagate, the code should generally return immediately from
208
+ the native callback, after performing any necessary cleanup.
209
+
210
+ ### Handling a Node-API JS exception
211
+
212
+ ```cpp
213
+ Napi::Env env = ...
214
+ Napi::Function jsFunctionThatThrows = someValue.As<Napi::Function>();
215
+ Napi::Value result = jsFunctionThatThrows({ arg1, arg2 });
216
+ if (env.IsExceptionPending()) {
217
+ Napi::Error e = env.GetAndClearPendingException();
218
+ cerr << "Caught JavaScript exception: " + e.Message();
219
+ }
220
+ ```
221
+
222
+ Since the exception was cleared here, it will not be propagated as a JavaScript
223
+ exception after the native callback returns.
224
+
225
+ ## Calling Node-API directly from a **node-addon-api** addon
226
+
227
+ **node-addon-api** provides macros for throwing errors in response to non-OK
228
+ `napi_status` results when calling [Node-API](https://nodejs.org/docs/latest/api/n-api.html)
229
+ functions from within a native addon. These macros are defined differently
230
+ depending on whether C++ exceptions are enabled or not, but are available for
231
+ use in either case.
232
+
233
+ ### `NAPI_THROW(e, ...)`
234
+
235
+ This macro accepts a `Napi::Error`, throws it, and returns the value given as
236
+ the last parameter. If C++ exceptions are enabled (by defining
237
+ `NAPI_CPP_EXCEPTIONS` during the build), the return value will be ignored.
238
+
239
+ ### `NAPI_THROW_IF_FAILED(env, status, ...)`
240
+
241
+ This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
242
+ from the `napi_status`, throws it, and returns the value given as the last
243
+ parameter. If C++ exceptions are enabled (by defining `NAPI_CPP_EXCEPTIONS`
244
+ during the build), the return value will be ignored.
245
+
246
+ ### `NAPI_THROW_IF_FAILED_VOID(env, status)`
247
+
248
+ This macro accepts a `Napi::Env` and a `napi_status`. It constructs an error
249
+ from the `napi_status`, throws it, and returns.
250
+
251
+ ### `NAPI_FATAL_IF_FAILED(status, location, message)`
252
+
253
+ This macro accepts a `napi_status`, a C string indicating the location where the
254
+ error occurred, and a second C string for the message to display.
@@ -0,0 +1,80 @@
1
+ # EscapableHandleScope
2
+
3
+ The `Napi::EscapableHandleScope` class is used to manage the lifetime of object handles
4
+ which are created through the use of node-addon-api. These handles
5
+ keep an object alive in the heap in order to ensure that the objects
6
+ are not collected by the garbage collector while native code is using them.
7
+ A handle may be created when any new node-addon-api Value or one
8
+ of its subclasses is created or returned.
9
+
10
+ The `Napi::EscapableHandleScope` is a special type of `Napi::HandleScope`
11
+ which allows a single handle to be "promoted" to an outer scope.
12
+
13
+ For more details refer to the section titled
14
+ [Object lifetime management](object_lifetime_management.md).
15
+
16
+ ## Methods
17
+
18
+ ### Constructor
19
+
20
+ Creates a new escapable handle scope.
21
+
22
+ ```cpp
23
+ Napi::EscapableHandleScope Napi::EscapableHandleScope::New(Napi::Env env);
24
+ ```
25
+
26
+ - `[in] Env`: The environment in which to construct the `Napi::EscapableHandleScope` object.
27
+
28
+ Returns a new `Napi::EscapableHandleScope`
29
+
30
+ ### Constructor
31
+
32
+ Creates a new escapable handle scope.
33
+
34
+ ```cpp
35
+ Napi::EscapableHandleScope Napi::EscapableHandleScope::New(napi_env env, napi_handle_scope scope);
36
+ ```
37
+
38
+ - `[in] env`: `napi_env` in which the scope passed in was created.
39
+ - `[in] scope`: pre-existing `napi_handle_scope`.
40
+
41
+ Returns a new `Napi::EscapableHandleScope` instance which wraps the
42
+ `napi_escapable_handle_scope` handle passed in. This can be used
43
+ to mix usage of the C Node-API and node-addon-api.
44
+
45
+ ```cpp
46
+ operator Napi::EscapableHandleScope::napi_escapable_handle_scope() const
47
+ ```
48
+
49
+ Returns the Node-API `napi_escapable_handle_scope` wrapped by the `Napi::EscapableHandleScope` object.
50
+ This can be used to mix usage of the C Node-API and node-addon-api by allowing
51
+ the class to be used be converted to a `napi_escapable_handle_scope`.
52
+
53
+ ### Destructor
54
+ ```cpp
55
+ Napi::EscapableHandleScope::~EscapableHandleScope();
56
+ ```
57
+
58
+ Deletes the `Napi::EscapableHandleScope` instance and allows any objects/handles created
59
+ in the scope to be collected by the garbage collector. There is no
60
+ guarantee as to when the garbage collector will do this.
61
+
62
+ ### Escape
63
+
64
+ ```cpp
65
+ napi::Value Napi::EscapableHandleScope::Escape(napi_value escapee);
66
+ ```
67
+
68
+ - `[in] escapee`: `Napi::Value` or `napi_env` to promote to the outer scope
69
+
70
+ Returns `Napi::Value` which can be used in the outer scope. This method can
71
+ be called at most once on a given `Napi::EscapableHandleScope`. If it is called
72
+ more than once an exception will be thrown.
73
+
74
+ ### Env
75
+
76
+ ```cpp
77
+ Napi::Env Napi::EscapableHandleScope::Env() const;
78
+ ```
79
+
80
+ Returns the `Napi::Env` associated with the `Napi::EscapableHandleScope`.
@@ -0,0 +1,63 @@
1
+ # External (template)
2
+
3
+ Class `Napi::External<T>` inherits from class [`Napi::Value`][].
4
+
5
+ The `Napi::External` template class implements the ability to create a `Napi::Value` object with arbitrary C++ data. It is the user's responsibility to manage the memory for the arbitrary C++ data.
6
+
7
+ `Napi::External` objects can be created with an optional Finalizer function and optional Hint value. The Finalizer function, if specified, is called when your `Napi::External` object is released by Node's garbage collector. It gives your code the opportunity to free any dynamically created data. If you specify a Hint value, it is passed to your Finalizer function.
8
+
9
+ ## Methods
10
+
11
+ ### New
12
+
13
+ ```cpp
14
+ template <typename T>
15
+ static Napi::External Napi::External::New(napi_env env, T* data);
16
+ ```
17
+
18
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
19
+ - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
20
+
21
+ Returns the created `Napi::External<T>` object.
22
+
23
+ ### New
24
+
25
+ ```cpp
26
+ template <typename T>
27
+ static Napi::External Napi::External::New(napi_env env,
28
+ T* data,
29
+ Finalizer finalizeCallback);
30
+ ```
31
+
32
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
33
+ - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
34
+ - `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting a T* and returning void.
35
+
36
+ Returns the created `Napi::External<T>` object.
37
+
38
+ ### New
39
+
40
+ ```cpp
41
+ template <typename T>
42
+ static Napi::External Napi::External::New(napi_env env,
43
+ T* data,
44
+ Finalizer finalizeCallback,
45
+ Hint* finalizeHint);
46
+ ```
47
+
48
+ - `[in] env`: The `napi_env` environment in which to construct the `Napi::External` object.
49
+ - `[in] data`: The arbitrary C++ data to be held by the `Napi::External` object.
50
+ - `[in] finalizeCallback`: A function called when the `Napi::External` object is released by the garbage collector accepting T* and Hint* parameters and returning void.
51
+ - `[in] finalizeHint`: A hint value passed to the `finalizeCallback` function.
52
+
53
+ Returns the created `Napi::External<T>` object.
54
+
55
+ ### Data
56
+
57
+ ```cpp
58
+ T* Napi::External::Data() const;
59
+ ```
60
+
61
+ Returns a pointer to the arbitrary C++ data held by the `Napi::External` object.
62
+
63
+ [`Napi::Value`]: ./value.md