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

feat: add support for requiring basic finalizers #1568

Merged
merged 2 commits into from
Sep 19, 2024

Conversation

KevinEady
Copy link
Contributor

@KevinEady KevinEady commented Sep 3, 2024

Introduce NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS preprocessor directive, which adds an always-fail static assertion inside the Wrapper definition (responsible for passing the user's non-basic finalizer through a node_api_post_finalizer call) if this preprocessor directive is defined.

@KevinEady KevinEady force-pushed the add-require-basic-finalizers branch from 4603869 to e21c0f6 Compare September 3, 2024 21:39
@codecov-commenter
Copy link

codecov-commenter commented Sep 3, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 64.40%. Comparing base (b4aeecb) to head (23b57cc).
Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1568   +/-   ##
=======================================
  Coverage   64.40%   64.40%           
=======================================
  Files           3        3           
  Lines        2003     2003           
  Branches      693      693           
=======================================
  Hits         1290     1290           
  Misses        146      146           
  Partials      567      567           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@KevinEady KevinEady force-pushed the add-require-basic-finalizers branch from e21c0f6 to 7c91a10 Compare September 4, 2024 17:34
@KevinEady
Copy link
Contributor Author

When trying to use a non-basic finalizer with this directive defined, you'll get an error like:

../../../../../napi-inl.h:252:5: error: static assertion failed: NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS defined: Finalizer must be basic.
    static_assert(false,
    ^             ~~~~~

(error taken from the test created in this PR)

@KevinEady KevinEady mentioned this pull request Sep 5, 2024
@@ -8,7 +8,9 @@ provide more efficient memory management, optimizations, improved execution, or
other benefits.

In general, it is best to use basic finalizers whenever possible (eg. when
access to JavaScript is _not_ needed).
access to JavaScript is _not_ needed). To ensure that all finalizers are basic
finalizers at compile-time, define the `NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS`
Copy link
Member

Choose a reason for hiding this comment

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

I am wondering if we should add a "best practice" target that enables all possible checks in https://github.com/nodejs/node-addon-api/blob/main/node_addon_api.gyp.

@@ -8,7 +8,9 @@ provide more efficient memory management, optimizations, improved execution, or
other benefits.

In general, it is best to use basic finalizers whenever possible (eg. when
access to JavaScript is _not_ needed).
access to JavaScript is _not_ needed). To ensure that all finalizers are basic
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's make this passive voice: The NODE_ADDON_API_REQUIRE_BASIC_FINALIZERS preprocessor directive can be defined to ensure that all finalizers are basic.

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 in 23b57cc

doc/setup.md Outdated
@@ -90,3 +90,13 @@ provide feedback to the user of the runtime error, as it is impossible to pass
the error to JavaScript when the environment is terminating. In order to bypass
this behavior such that the Node process will not terminate, define the
Copy link
Contributor

Choose a reason for hiding this comment

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

Here too: "The ... can be defined in order to bypass ... "

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 in 23b57cc

napi-inl.h Outdated
@@ -205,14 +205,19 @@ struct FinalizeData {
});
}

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
#if defined(NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER)
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change needed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Whoops, this was a leftover of a different implementation. Addressed in 23b57cc

napi-inl.h Outdated
@@ -235,14 +240,19 @@ struct FinalizeData {
});
}

#ifdef NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER
#if defined(NODE_API_EXPERIMENTAL_HAS_POST_FINALIZER)
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here.

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 in 23b57cc

Copy link
Contributor

@gabrielschulhof gabrielschulhof left a comment

Choose a reason for hiding this comment

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

LGTM with some comments.

@KevinEady KevinEady marked this pull request as draft September 7, 2024 10:48
- Use passive voice in existing and new docs
- Revert unnecessary change
@KevinEady KevinEady marked this pull request as ready for review September 9, 2024 15:30
@KevinEady
Copy link
Contributor Author

LGTM with some comments.

Hi @gabrielschulhof ,

Your changes have been introduced in 23b57cc. PTAL!

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

@legendecas legendecas merged commit 7bcb826 into nodejs:main Sep 19, 2024
48 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants