-
-
Notifications
You must be signed in to change notification settings - Fork 477
feat: add support for nogc types via BasicEnv
#1514
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
Merged
Merged
Changes from 18 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
d90111f
src: introduce `NogcEnv`; support for nogc finalizers
KevinEady 1ddf8f0
Address comments in 8 Jun 2024 Node-API meeting:
KevinEady 812d27c
sfinae FinalizeData::Wrapper
KevinEady 0ad6fcd
fix test/addon_build for experimental
KevinEady 64392b1
sfinae ObjectWrap::Finalize
KevinEady 18495a4
revert tests
KevinEady 70a8302
add finalizer_order test
KevinEady 26972bf
remove unnecessary defines
KevinEady 5d93fc3
add AddPostFinalizer test
KevinEady 94bbe58
renaming ideas
KevinEady c4e72eb
wip on docs for general finalization
KevinEady 4e8b367
switch to "basic" finalizer in docs
KevinEady de20ac4
Address review comments
KevinEady 6ee1ebf
finish renaming async finalizer to basic
KevinEady 13118f7
finish docs
KevinEady 1ad6eda
fix compilation error with duplicate `Finalizer` identifier
KevinEady ec9f521
attempt fix compilation issues #2
KevinEady a8f6380
fix docs
KevinEady 195ec28
Address review comments
KevinEady File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
# BasicEnv | ||
|
||
The opaque data structure containing the environment in which the request is | ||
being run. | ||
|
||
The `Napi::BasicEnv` object is usually created and passed by the Node.js runtime | ||
or node-addon-api infrastructure. | ||
|
||
The `Napi::BasicEnv` object represents an environment that has a limited subset | ||
of APIs when compared to `Napi::Env` and can be used in basic finalizers. See | ||
[Finalization][] for more details. | ||
|
||
## Methods | ||
|
||
### Constructor | ||
|
||
```cpp | ||
Napi::BasicEnv::BasicEnv(node_api_nogc_env env); | ||
``` | ||
|
||
- `[in] env`: The `node_api_nogc_env` environment from which to construct the | ||
`Napi::BasicEnv` object. | ||
|
||
### node_api_nogc_env | ||
|
||
```cpp | ||
operator node_api_nogc_env() const; | ||
``` | ||
|
||
Returns the `node_api_nogc_env` opaque data structure representing the | ||
environment. | ||
|
||
### GetInstanceData | ||
```cpp | ||
template <typename T> T* GetInstanceData() const; | ||
``` | ||
|
||
Returns the instance data that was previously associated with the environment, | ||
or `nullptr` if none was associated. | ||
|
||
### SetInstanceData | ||
|
||
|
||
```cpp | ||
template <typename T> using Finalizer = void (*)(Env, T*); | ||
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>> | ||
void SetInstanceData(T* data) const; | ||
``` | ||
|
||
- `[template] fini`: A function to call when the instance data is to be deleted. | ||
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If | ||
not given, the default finalizer will be used, which simply uses the `delete` | ||
operator to destroy `T*` when the addon instance is unloaded. | ||
- `[in] data`: A pointer to data that will be associated with the instance of | ||
the addon for the duration of its lifecycle. | ||
|
||
Associates a data item stored at `T* data` with the current instance of the | ||
addon. The item will be passed to the function `fini` which gets called when an | ||
instance of the addon is unloaded. | ||
|
||
### SetInstanceData | ||
|
||
```cpp | ||
template <typename DataType, typename HintType> | ||
using FinalizerWithHint = void (*)(Env, DataType*, HintType*); | ||
template <typename DataType, | ||
typename HintType, | ||
FinalizerWithHint<DataType, HintType> fini = | ||
Env::DefaultFiniWithHint<DataType, HintType>> | ||
void SetInstanceData(DataType* data, HintType* hint) const; | ||
``` | ||
|
||
- `[template] fini`: A function to call when the instance data is to be deleted. | ||
Accepts a function of the form `void CleanupData(Napi::Env env, DataType* data, | ||
HintType* hint)`. If not given, the default finalizer will be used, which simply | ||
uses the `delete` operator to destroy `T*` when the addon instance is unloaded. | ||
- `[in] data`: A pointer to data that will be associated with the instance of | ||
the addon for the duration of its lifecycle. | ||
- `[in] hint`: A pointer to data that will be associated with the instance of | ||
the addon for the duration of its lifecycle and will be passed as a hint to | ||
`fini` when the addon instance is unloaded. | ||
KevinEady marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Associates a data item stored at `T* data` with the current instance of the | ||
addon. The item will be passed to the function `fini` which gets called when an | ||
KevinEady marked this conversation as resolved.
Show resolved
Hide resolved
|
||
instance of the addon is unloaded. This overload accepts an additional hint to | ||
KevinEady marked this conversation as resolved.
Show resolved
Hide resolved
|
||
be passed to `fini`. | ||
|
||
### GetModuleFileName | ||
|
||
```cpp | ||
const char* Napi::Env::GetModuleFileName() const; | ||
``` | ||
|
||
Returns a A URL containing the absolute path of the location from which the | ||
KevinEady marked this conversation as resolved.
Show resolved
Hide resolved
|
||
add-on was loaded. For a file on the local file system it will start with | ||
`file://`. The string is null-terminated and owned by env and must thus not be | ||
modified or freed. It is only valid while the add-on is loaded. | ||
|
||
### AddCleanupHook | ||
|
||
```cpp | ||
template <typename Hook> | ||
CleanupHook<Hook> AddCleanupHook(Hook hook); | ||
``` | ||
|
||
- `[in] hook`: A function to call when the environment exits. Accepts a function | ||
of the form `void ()`. | ||
|
||
Registers `hook` as a function to be run once the current Node.js environment | ||
exits. Unlike the underlying C-based Node-API, providing the same `hook` | ||
multiple times **is** allowed. The hooks will be called in reverse order, i.e. | ||
the most recently added one will be called first. | ||
|
||
Returns an `Env::CleanupHook` object, which can be used to remove the hook via | ||
its `Remove()` method. | ||
|
||
### PostFinalizer | ||
|
||
```cpp | ||
template <typename FinalizerType> | ||
inline void PostFinalizer(FinalizerType finalizeCallback) const; | ||
``` | ||
|
||
- `[in] finalizeCallback`: The function to queue for execution outside of the GC | ||
finalization, implementing `operator()(Napi::Env)`. See [Finalization][] for | ||
more details. | ||
|
||
### PostFinalizer | ||
|
||
```cpp | ||
template <typename FinalizerType, typename T> | ||
inline void PostFinalizer(FinalizerType finalizeCallback, T* data) const; | ||
``` | ||
|
||
- `[in] finalizeCallback`: The function to queue for execution outside of the GC | ||
finalization, implementing `operator()(Napi::Env, T*)`. See [Finalization][] | ||
for more details. | ||
- `[in] data`: The data to associate with the object. | ||
|
||
### PostFinalizer | ||
|
||
```cpp | ||
template <typename FinalizerType, typename T, typename Hint> | ||
inline void PostFinalizer(FinalizerType finalizeCallback, | ||
T* data, | ||
Hint* finalizeHint) const; | ||
``` | ||
|
||
- `[in] finalizeCallback`: The function to queue for execution outside of the GC | ||
finalization, implementing `operator()(Napi::Env, T*, Hint*)`. See | ||
[Finalization][] for more details. | ||
- `[in] data`: The data to associate with the object. | ||
- `[in] finalizeHint`: The hint value passed to the `finalizeCallback` function. | ||
|
||
### AddCleanupHook | ||
|
||
```cpp | ||
template <typename Hook, typename Arg> | ||
CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg); | ||
``` | ||
|
||
- `[in] hook`: A function to call when the environment exits. Accepts a function | ||
of the form `void (Arg* arg)`. | ||
- `[in] arg`: A pointer to data that will be passed as the argument to `hook`. | ||
|
||
Registers `hook` as a function to be run with the `arg` parameter once the | ||
current Node.js environment exits. Unlike the underlying C-based Node-API, | ||
providing the same `hook` and `arg` pair multiple times **is** allowed. The | ||
hooks will be called in reverse order, i.e. the most recently added one will be | ||
called first. | ||
|
||
Returns an `Env::CleanupHook` object, which can be used to remove the hook via | ||
its `Remove()` method. | ||
|
||
# Env::CleanupHook | ||
|
||
The `Env::CleanupHook` object allows removal of the hook added via | ||
`Env::AddCleanupHook()` | ||
|
||
## Methods | ||
|
||
### IsEmpty | ||
|
||
```cpp | ||
bool IsEmpty(); | ||
``` | ||
|
||
Returns `true` if the cleanup hook was **not** successfully registered. | ||
|
||
### Remove | ||
|
||
```cpp | ||
bool Remove(Env env); | ||
``` | ||
|
||
Unregisters the hook from running once the current Node.js environment exits. | ||
|
||
Returns `true` if the hook was successfully removed from the Node.js | ||
environment. | ||
|
||
[Finalization]: ./finalization.md |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.