Skip to content

Commit 43b8a91

Browse files
committed
node-api: declare type napi_cleanup_hook
Declare type `napi_cleanup_hook` so that the function signature can be shared across the codebase.
1 parent 01e673c commit 43b8a91

File tree

4 files changed

+34
-9
lines changed

4 files changed

+34
-9
lines changed

doc/api/n-api.md

+19-1
Original file line numberDiff line numberDiff line change
@@ -897,6 +897,24 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
897897
Unless for reasons discussed in [Object Lifetime Management][], creating a
898898
handle and/or callback scope inside the function body is not necessary.
899899

900+
#### `napi_cleanup_hook`
901+
902+
<!-- YAML
903+
added: REPLACEME
904+
napiVersion: 3
905+
-->
906+
907+
Function pointer used with [`napi_add_env_cleanup_hook`][]. It will be called
908+
when the environment is being torn down.
909+
910+
Callback functions must satisfy the following signature:
911+
912+
```c
913+
typedef void (*napi_cleanup_hook)(void* data);
914+
```
915+
916+
* `[in] data`: The data that was passed to [`napi_add_env_cleanup_hook`][].
917+
900918
#### `napi_async_cleanup_hook`
901919

902920
<!-- YAML
@@ -1798,7 +1816,7 @@ napiVersion: 3
17981816

17991817
```c
18001818
NODE_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
1801-
void (*fun)(void* arg),
1819+
napi_cleanup_hook fun,
18021820
void* arg);
18031821
```
18041822

src/node_api.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,9 @@ void NAPI_CDECL napi_module_register(napi_module* mod) {
671671
node::node_module_register(nm);
672672
}
673673

674-
napi_status NAPI_CDECL napi_add_env_cleanup_hook(
675-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg) {
674+
napi_status NAPI_CDECL napi_add_env_cleanup_hook(napi_env env,
675+
napi_cleanup_hook fun,
676+
void* arg) {
676677
CHECK_ENV(env);
677678
CHECK_ARG(env, fun);
678679

@@ -681,8 +682,9 @@ napi_status NAPI_CDECL napi_add_env_cleanup_hook(
681682
return napi_ok;
682683
}
683684

684-
napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
685-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg) {
685+
napi_status NAPI_CDECL napi_remove_env_cleanup_hook(napi_env env,
686+
napi_cleanup_hook fun,
687+
void* arg) {
686688
CHECK_ENV(env);
687689
CHECK_ARG(env, fun);
688690

src/node_api.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -206,11 +206,11 @@ napi_get_uv_event_loop(napi_env env, struct uv_loop_s** loop);
206206
NAPI_EXTERN napi_status NAPI_CDECL napi_fatal_exception(napi_env env,
207207
napi_value err);
208208

209-
NAPI_EXTERN napi_status NAPI_CDECL napi_add_env_cleanup_hook(
210-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
209+
NAPI_EXTERN napi_status NAPI_CDECL
210+
napi_add_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);
211211

212-
NAPI_EXTERN napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
213-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
212+
NAPI_EXTERN napi_status NAPI_CDECL
213+
napi_remove_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);
214214

215215
NAPI_EXTERN napi_status NAPI_CDECL
216216
napi_open_callback_scope(napi_env env,

src/node_api_types.h

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66
typedef struct napi_callback_scope__* napi_callback_scope;
77
typedef struct napi_async_context__* napi_async_context;
88
typedef struct napi_async_work__* napi_async_work;
9+
10+
#if NAPI_VERSION >= 3
11+
typedef void(NAPI_CDECL* napi_cleanup_hook)(void* arg);
12+
#endif // NAPI_VERSION >= 3
13+
914
#if NAPI_VERSION >= 4
1015
typedef struct napi_threadsafe_function__* napi_threadsafe_function;
1116
#endif // NAPI_VERSION >= 4

0 commit comments

Comments
 (0)