-
Notifications
You must be signed in to change notification settings - Fork 465
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
Conversation
4ee44c1
to
42968bc
Compare
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
|
|
||
template <typename Hook, typename Arg> | ||
CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg); | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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:
Line 84 in 6697c51
if (napiVersion < 3) { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
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:
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`
36f13d3
to
36ef600
Compare
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}; |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, nervermind.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Add CleanupHook support to Env PR-URL: #1014 Reviewed-By: Michael Dawson <[email protected]>
Landed as 10564a4 |
Add CleanupHook support to Env PR-URL: nodejs#1014 Reviewed-By: Michael Dawson <[email protected]>
Add CleanupHook support to Env PR-URL: nodejs#1014 Reviewed-By: Michael Dawson <[email protected]>
Add CleanupHook support to Env PR-URL: nodejs/node-addon-api#1014 Reviewed-By: Michael Dawson <[email protected]>
Add CleanupHook support to Env PR-URL: nodejs/node-addon-api#1014 Reviewed-By: Michael Dawson <[email protected]>
Add CleanupHook support to Env PR-URL: nodejs/node-addon-api#1014 Reviewed-By: Michael Dawson <[email protected]>
Add CleanupHook support to Env PR-URL: nodejs/node-addon-api#1014 Reviewed-By: Michael Dawson <[email protected]>
Adds wrapping for
napi_add_env_cleanup_hook
andnapi_remove_env_cleanup_hook
.