Skip to content

Commit

Permalink
node-api: declare type napi_cleanup_hook
Browse files Browse the repository at this point in the history
Declare type `napi_cleanup_hook` so that the function signature can be
shared across the codebase.

PR-URL: #45391
Reviewed-By: Michael Dawson <[email protected]>
Reviewed-By: Daeyeon Jeong <[email protected]>
  • Loading branch information
legendecas authored and danielleadams committed Jan 3, 2023
1 parent 9ab00f5 commit 581b38a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
20 changes: 19 additions & 1 deletion doc/api/n-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,24 @@ typedef void (*napi_threadsafe_function_call_js)(napi_env env,
Unless for reasons discussed in [Object Lifetime Management][], creating a
handle and/or callback scope inside the function body is not necessary.

#### `napi_cleanup_hook`

<!-- YAML
added: REPLACEME
napiVersion: 3
-->

Function pointer used with [`napi_add_env_cleanup_hook`][]. It will be called
when the environment is being torn down.

Callback functions must satisfy the following signature:

```c
typedef void (*napi_cleanup_hook)(void* data);
```

* `[in] data`: The data that was passed to [`napi_add_env_cleanup_hook`][].

#### `napi_async_cleanup_hook`

<!-- YAML
Expand Down Expand Up @@ -1799,7 +1817,7 @@ napiVersion: 3

```c
NODE_EXTERN napi_status napi_add_env_cleanup_hook(napi_env env,
void (*fun)(void* arg),
napi_cleanup_hook fun,
void* arg);
```

Expand Down
10 changes: 6 additions & 4 deletions src/node_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,8 +671,9 @@ void NAPI_CDECL napi_module_register(napi_module* mod) {
node::node_module_register(nm);
}

napi_status NAPI_CDECL napi_add_env_cleanup_hook(
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg) {
napi_status NAPI_CDECL napi_add_env_cleanup_hook(napi_env env,
napi_cleanup_hook fun,
void* arg) {
CHECK_ENV(env);
CHECK_ARG(env, fun);

Expand All @@ -681,8 +682,9 @@ napi_status NAPI_CDECL napi_add_env_cleanup_hook(
return napi_ok;
}

napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg) {
napi_status NAPI_CDECL napi_remove_env_cleanup_hook(napi_env env,
napi_cleanup_hook fun,
void* arg) {
CHECK_ENV(env);
CHECK_ARG(env, fun);

Expand Down
8 changes: 4 additions & 4 deletions src/node_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ napi_get_uv_event_loop(napi_env env, struct uv_loop_s** loop);
NAPI_EXTERN napi_status NAPI_CDECL napi_fatal_exception(napi_env env,
napi_value err);

NAPI_EXTERN napi_status NAPI_CDECL napi_add_env_cleanup_hook(
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
NAPI_EXTERN napi_status NAPI_CDECL
napi_add_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);

NAPI_EXTERN napi_status NAPI_CDECL napi_remove_env_cleanup_hook(
napi_env env, void(NAPI_CDECL* fun)(void* arg), void* arg);
NAPI_EXTERN napi_status NAPI_CDECL
napi_remove_env_cleanup_hook(napi_env env, napi_cleanup_hook fun, void* arg);

NAPI_EXTERN napi_status NAPI_CDECL
napi_open_callback_scope(napi_env env,
Expand Down
5 changes: 5 additions & 0 deletions src/node_api_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
typedef struct napi_callback_scope__* napi_callback_scope;
typedef struct napi_async_context__* napi_async_context;
typedef struct napi_async_work__* napi_async_work;

#if NAPI_VERSION >= 3
typedef void(NAPI_CDECL* napi_cleanup_hook)(void* arg);
#endif // NAPI_VERSION >= 3

#if NAPI_VERSION >= 4
typedef struct napi_threadsafe_function__* napi_threadsafe_function;
#endif // NAPI_VERSION >= 4
Expand Down

0 comments on commit 581b38a

Please sign in to comment.