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.
  • Loading branch information
legendecas committed Nov 9, 2022
1 parent 01e673c commit 2368d67
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
20 changes: 19 additions & 1 deletion doc/api/n-api.md
Expand Up @@ -897,6 +897,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: v10.2.0
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 @@ -1798,7 +1816,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
4 changes: 2 additions & 2 deletions src/node_api.cc
Expand Up @@ -672,7 +672,7 @@ void NAPI_CDECL napi_module_register(napi_module* mod) {
}

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

Expand All @@ -682,7 +682,7 @@ napi_status NAPI_CDECL napi_add_env_cleanup_hook(
}

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

Expand Down
4 changes: 2 additions & 2 deletions src/node_api.h
Expand Up @@ -207,10 +207,10 @@ 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_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_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
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 2368d67

Please sign in to comment.