Skip to content

Commit 4a4f280

Browse files
legendecasruyadorno
authored andcommitted
node-api: declare type napi_cleanup_hook
Declare type `napi_cleanup_hook` so that the function signature can be shared across the codebase. PR-URL: #45391 Reviewed-By: Michael Dawson <midawson@redhat.com> Reviewed-By: Daeyeon Jeong <daeyeon.dev@gmail.com>
1 parent e0a271e commit 4a4f280

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
@@ -898,6 +898,24 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
898898
Unless for reasons discussed in [Object Lifetime Management][], creating a
899899
handle and/or callback scope inside the function body is not necessary.
900900

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

903921
<!-- YAML
@@ -1799,7 +1817,7 @@ napiVersion: 3
17991817

18001818
```c
18011819
NODE_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
1802-
void (*fun)(void* arg),
1820+
napi_cleanup_hook fun,
18031821
void* arg);
18041822
```
18051823

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
@@ -208,11 +208,11 @@ napi_get_uv_event_loop(napi_env env, struct uv_loop_s** loop);
208208
NAPI_EXTERN napi_status NAPI_CDECL napi_fatal_exception(napi_env env,
209209
napi_value err);
210210

211-
NAPI_EXTERN napi_status NAPI_CDECL napi_add_env_cleanup_hook(
212-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
211+
NAPI_EXTERN napi_status NAPI_CDECL
212+
napi_add_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);
213213

214-
NAPI_EXTERN napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
215-
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
214+
NAPI_EXTERN napi_status NAPI_CDECL
215+
napi_remove_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);
216216

217217
NAPI_EXTERN napi_status NAPI_CDECL
218218
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)