Skip to content

Commit 42b8ce9

Browse files
author
John French
committed
doc: add instance data APIs
Signed-off-by: Gabriel Schulhof <[email protected]> Re: nodejs/node-addon-api#567 (comment) PR-URL: nodejs/node-addon-api#708 Reviewed-By: Nicola Del Gobbo <[email protected]> Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 8799b2a commit 42b8ce9

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

doc/env.md

+55
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,58 @@ The `script` can be any of the following types:
7575
- [`Napi::String`](string.md)
7676
- `const char *`
7777
- `const std::string &`
78+
79+
### GetInstanceData
80+
```cpp
81+
template <typename T> T* GetInstanceData();
82+
```
83+
84+
Returns the instance data that was previously associated with the environment,
85+
or `nullptr` if none was associated.
86+
87+
### SetInstanceData
88+
89+
```cpp
90+
template <typename T> using Finalizer = void (*)(Env, T*);
91+
template <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
92+
void SetInstanceData(T* data);
93+
```
94+
95+
- `[template] fini`: A function to call when the instance data is to be deleted.
96+
Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
97+
not given, the default finalizer will be used, which simply uses the `delete`
98+
operator to destroy `T*` when the addon instance is unloaded.
99+
- `[in] data`: A pointer to data that will be associated with the instance of
100+
the addon for the duration of its lifecycle.
101+
102+
Associates a data item stored at `T* data` with the current instance of the
103+
addon. The item will be passed to the function `fini` which gets called when an
104+
instance of the addon is unloaded.
105+
106+
### SetInstanceData
107+
108+
```cpp
109+
template <typename DataType, typename HintType>
110+
using FinalizerWithHint = void (*)(Env, DataType*, HintType*);
111+
template <typename DataType,
112+
typename HintType,
113+
FinalizerWithHint<DataType, HintType> fini =
114+
Env::DefaultFiniWithHint<DataType, HintType>>
115+
void SetInstanceData(DataType* data, HintType* hint);
116+
```
117+
118+
- `[template] fini`: A function to call when the instance data is to be deleted.
119+
Accepts a function of the form
120+
`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
121+
the default finalizer will be used, which simply uses the `delete` operator to
122+
destroy `T*` when the addon instance is unloaded.
123+
- `[in] data`: A pointer to data that will be associated with the instance of
124+
the addon for the duration of its lifecycle.
125+
- `[in] hint`: A pointer to data that will be associated with the instance of
126+
the addon for the duration of its lifecycle and will be passed as a hint to
127+
`fini` when the addon instance is unloaded.
128+
129+
Associates a data item stored at `T* data` with the current instance of the
130+
addon. The item will be passed to the function `fini` which gets called when an
131+
instance of the addon is unloaded. This overload accepts an additional hint to
132+
be passed to `fini`.

0 commit comments

Comments
 (0)