Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

src: add Env::AddCleanupHook #1014

Closed
wants to merge 9 commits into from

Conversation

KevinEady
Copy link
Contributor

@KevinEady KevinEady commented Jun 25, 2021

Adds wrapping for napi_add_env_cleanup_hook and napi_remove_env_cleanup_hook.

napi.h Outdated Show resolved Hide resolved
@KevinEady KevinEady force-pushed the add-env-cleanup-hook branch from 4ee44c1 to 42968bc Compare June 26, 2021 09:31
@KevinEady KevinEady marked this pull request as ready for review June 26, 2021 10:01
@KevinEady
Copy link
Contributor Author

KevinEady commented Jun 26, 2021

Hi team. This is ready for review. I have fixed the API privatization, see https://github.com/nodejs/node-addon-api/pull/1014/files#diff-6294f8ee1d8cd7e4e94cfc22a083380fb80058e34f53100d895b9b3b295dd801

  • Env::CleanupHook is a private class in Env with public constructors, so only Env can construct an instance and eg. return it to the caller in Env::AddCleanupHook.
  • Env::CleanupHook exposes one public method, Remove.
  • Env::CleanupHook has the two private static wrapper methods (one for calling hook() and one for hook(arg)). They are referenced inside the constructor.


template <typename Hook, typename Arg>
CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these need to be guarded by NAPI_VERSION > 3 as the cleanup hooks were only added in version 3 as per: https://nodejs.org/api/n-api.html#n_api_napi_add_env_cleanup_hook

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about this, but we dropped support for node 8, and Node-API version is present in 10+. Do we still need a guard?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, people can still ask for a particular Node-API version and I think we should respect that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note I did not add guards to the tests -- we will always test Node-API 3+ right?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In terms of guarding the tests its only a 1 line addition here:

if (napiVersion < 3) {
so I think we might as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking more I guess there is more to it than that. You also need to guard the compilation but that is pretty easy too. Unlikely anybody will test with napiVersion <3 but for consistency I'd still prefer if we guard since it's not a lot of effort.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed.

napi-inl.h Outdated
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
data = new CleanupData{std::move(hook), nullptr};
napi_status status = napi_add_env_cleanup_hook(env, wrapper, data);
NAPI_FATAL_IF_FAILED(status,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would cause napi_add_env_cleanup_hook to fail ? I'm wondering if we can let the caller trying add a hook know it failed versus causing a FATAL error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the implementation, it looks like it should always return napi_ok ... https://github.com/nodejs/node/blob/663d7e9fb27f017f38b79382a95fec10e5f66f9a/src/node_api.cc#L656-L676

According to the docs it can just abort the process with invalid arguments:

Providing the same fun and arg values multiple times is not allowed and will lead the process to abort.

But we would never provide the same arg value (as we always create a new CallbackData)

Maybe we don't need the check at all...? However napi_add_env_cleanup_hook always returning napi_ok is an implementation detail, maybe we should keep it blackboxed 🤷

How would you recommend "add a hook know it failed" ...?

Thanks, Kevin

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you recommend "add a hook know it failed" . What I had in mind was some kind of return value the caller could check. For example if it was documented that the callers should check that the result from AddCleanupHook was non-null would that make any sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed. Added a bool IsEmpty() method on Env::CleanupHook

node-addon-api/napi-inl.h

Lines 5756 to 5759 in 36ef600

template <class Hook, class Arg>
bool Env::CleanupHook<Hook, Arg>::IsEmpty() const {
return data == nullptr;
}

data is nullptr if adding the hook failed:

node-addon-api/napi-inl.h

Lines 5737 to 5746 in 36ef600

template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook, Arg* arg)
: wrapper(Env::CleanupHook<Hook, Arg>::WrapperWithArg) {
data = new CleanupData{std::move(hook), arg};
napi_status status = napi_add_env_cleanup_hook(env, wrapper, data);
if (status != napi_ok) {
delete data;
data = nullptr;
}
}

- add `NAPI_VERSION` guard
- add `IsEmpty`
@KevinEady KevinEady force-pushed the add-env-cleanup-hook branch from 36f13d3 to 36ef600 Compare July 16, 2021 14:47
template <typename Hook, typename Arg>
Env::CleanupHook<Hook, Arg>::CleanupHook(Napi::Env env, Hook hook)
: wrapper(Env::CleanupHook<Hook, Arg>::Wrapper) {
data = new CleanupData{std::move(hook), nullptr};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could this version just call the other one and pass nullptr for data ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mhdawson no because each constructor initializes the instance member wrapper to a different value, Env::CleanupHook<Hook, Arg>::Wrapper (this one) or Env::CleanupHook<Hook, Arg>::WrapperWithArg

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, nervermind.

Copy link
Member

@mhdawson mhdawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

mhdawson pushed a commit that referenced this pull request Jul 27, 2021
Add CleanupHook support to Env

PR-URL: #1014
Reviewed-By: Michael Dawson <[email protected]>
@mhdawson
Copy link
Member

Landed as 10564a4

@mhdawson mhdawson closed this Jul 27, 2021
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this pull request Sep 23, 2021
Add CleanupHook support to Env

PR-URL: nodejs#1014
Reviewed-By: Michael Dawson <[email protected]>
deepakrkris pushed a commit to deepakrkris/node-addon-api that referenced this pull request Oct 15, 2021
Add CleanupHook support to Env

PR-URL: nodejs#1014
Reviewed-By: Michael Dawson <[email protected]>
kevindavies8 added a commit to kevindavies8/node-addon-api-Develop that referenced this pull request Aug 24, 2022
Add CleanupHook support to Env

PR-URL: nodejs/node-addon-api#1014
Reviewed-By: Michael Dawson <[email protected]>
Marlyfleitas added a commit to Marlyfleitas/node-api-addon-Development that referenced this pull request Aug 26, 2022
Add CleanupHook support to Env

PR-URL: nodejs/node-addon-api#1014
Reviewed-By: Michael Dawson <[email protected]>
wroy7860 added a commit to wroy7860/addon-api-benchmark-node that referenced this pull request Sep 19, 2022
Add CleanupHook support to Env

PR-URL: nodejs/node-addon-api#1014
Reviewed-By: Michael Dawson <[email protected]>
johnfrench3 pushed a commit to johnfrench3/node-addon-api-git that referenced this pull request Aug 11, 2023
Add CleanupHook support to Env

PR-URL: nodejs/node-addon-api#1014
Reviewed-By: Michael Dawson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants