|
| 1 | +# BasicEnv |
| 2 | + |
| 3 | +The data structure containing the environment in which the request is being run. |
| 4 | + |
| 5 | +The `Napi::BasicEnv` object is usually created and passed by the Node.js runtime |
| 6 | +or node-addon-api infrastructure. |
| 7 | + |
| 8 | +The `Napi::BasicEnv` object represents an environment that has a limited subset |
| 9 | +of APIs when compared to `Napi::Env` and can be used in basic finalizers. See |
| 10 | +[Finalization][] for more details. |
| 11 | + |
| 12 | +## Methods |
| 13 | + |
| 14 | +### Constructor |
| 15 | + |
| 16 | +```cpp |
| 17 | +Napi::BasicEnv::BasicEnv(node_api_nogc_env env); |
| 18 | +``` |
| 19 | +
|
| 20 | +- `[in] env`: The `node_api_nogc_env` environment from which to construct the |
| 21 | + `Napi::BasicEnv` object. |
| 22 | +
|
| 23 | +### node_api_nogc_env |
| 24 | +
|
| 25 | +```cpp |
| 26 | +operator node_api_nogc_env() const; |
| 27 | +``` |
| 28 | + |
| 29 | +Returns the `node_api_nogc_env` opaque data structure representing the |
| 30 | +environment. |
| 31 | + |
| 32 | +### GetInstanceData |
| 33 | +```cpp |
| 34 | +template <typename T> T* GetInstanceData() const; |
| 35 | +``` |
| 36 | + |
| 37 | +Returns the instance data that was previously associated with the environment, |
| 38 | +or `nullptr` if none was associated. |
| 39 | + |
| 40 | +### SetInstanceData |
| 41 | + |
| 42 | + |
| 43 | +```cpp |
| 44 | +template <typename T> using Finalizer = void (*)(Env, T*); |
| 45 | +template <typename T, Finalizer<T> fini = Env::DefaultFini<T>> |
| 46 | +void SetInstanceData(T* data) const; |
| 47 | +``` |
| 48 | +
|
| 49 | +- `[template] fini`: A function to call when the instance data is to be deleted. |
| 50 | +Accepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If |
| 51 | +not given, the default finalizer will be used, which simply uses the `delete` |
| 52 | +operator to destroy `T*` when the add-on instance is unloaded. |
| 53 | +- `[in] data`: A pointer to data that will be associated with the instance of |
| 54 | +the add-on for the duration of its lifecycle. |
| 55 | +
|
| 56 | +Associates a data item stored at `T* data` with the current instance of the |
| 57 | +add-on. The item will be passed to the function `fini` which gets called when an |
| 58 | +instance of the add-on is unloaded. |
| 59 | +
|
| 60 | +### SetInstanceData |
| 61 | +
|
| 62 | +```cpp |
| 63 | +template <typename DataType, typename HintType> |
| 64 | +using FinalizerWithHint = void (*)(Env, DataType*, HintType*); |
| 65 | +template <typename DataType, |
| 66 | + typename HintType, |
| 67 | + FinalizerWithHint<DataType, HintType> fini = |
| 68 | + Env::DefaultFiniWithHint<DataType, HintType>> |
| 69 | +void SetInstanceData(DataType* data, HintType* hint) const; |
| 70 | +``` |
| 71 | + |
| 72 | +- `[template] fini`: A function to call when the instance data is to be deleted. |
| 73 | +Accepts a function of the form `void CleanupData(Napi::Env env, DataType* data, |
| 74 | +HintType* hint)`. If not given, the default finalizer will be used, which simply |
| 75 | +uses the `delete` operator to destroy `T*` when the add-on instance is unloaded. |
| 76 | +- `[in] data`: A pointer to data that will be associated with the instance of |
| 77 | +the add-on for the duration of its lifecycle. |
| 78 | +- `[in] hint`: A pointer to data that will be associated with the instance of |
| 79 | +the add-on for the duration of its lifecycle and will be passed as a hint to |
| 80 | +`fini` when the add-on instance is unloaded. |
| 81 | + |
| 82 | +Associates a data item stored at `T* data` with the current instance of the |
| 83 | +add-on. The item will be passed to the function `fini` which gets called when an |
| 84 | +instance of the add-on is unloaded. This overload accepts an additional hint to |
| 85 | +be passed to `fini`. |
| 86 | + |
| 87 | +### GetModuleFileName |
| 88 | + |
| 89 | +```cpp |
| 90 | +const char* Napi::Env::GetModuleFileName() const; |
| 91 | +``` |
| 92 | + |
| 93 | +Returns a URL containing the absolute path of the location from which the add-on |
| 94 | +was loaded. For a file on the local file system it will start with `file://`. |
| 95 | +The string is null-terminated and owned by env and must thus not be modified or |
| 96 | +freed. It is only valid while the add-on is loaded. |
| 97 | + |
| 98 | +### AddCleanupHook |
| 99 | + |
| 100 | +```cpp |
| 101 | +template <typename Hook> |
| 102 | +CleanupHook<Hook> AddCleanupHook(Hook hook); |
| 103 | +``` |
| 104 | +
|
| 105 | +- `[in] hook`: A function to call when the environment exits. Accepts a function |
| 106 | + of the form `void ()`. |
| 107 | +
|
| 108 | +Registers `hook` as a function to be run once the current Node.js environment |
| 109 | +exits. Unlike the underlying C-based Node-API, providing the same `hook` |
| 110 | +multiple times **is** allowed. The hooks will be called in reverse order, i.e. |
| 111 | +the most recently added one will be called first. |
| 112 | +
|
| 113 | +Returns an `Env::CleanupHook` object, which can be used to remove the hook via |
| 114 | +its `Remove()` method. |
| 115 | +
|
| 116 | +### PostFinalizer |
| 117 | +
|
| 118 | +```cpp |
| 119 | +template <typename FinalizerType> |
| 120 | +inline void PostFinalizer(FinalizerType finalizeCallback) const; |
| 121 | +``` |
| 122 | + |
| 123 | +- `[in] finalizeCallback`: The function to queue for execution outside of the GC |
| 124 | + finalization, implementing `operator()(Napi::Env)`. See [Finalization][] for |
| 125 | + more details. |
| 126 | + |
| 127 | +### PostFinalizer |
| 128 | + |
| 129 | +```cpp |
| 130 | +template <typename FinalizerType, typename T> |
| 131 | +inline void PostFinalizer(FinalizerType finalizeCallback, T* data) const; |
| 132 | +``` |
| 133 | +
|
| 134 | +- `[in] finalizeCallback`: The function to queue for execution outside of the GC |
| 135 | + finalization, implementing `operator()(Napi::Env, T*)`. See [Finalization][] |
| 136 | + for more details. |
| 137 | +- `[in] data`: The data to associate with the object. |
| 138 | +
|
| 139 | +### PostFinalizer |
| 140 | +
|
| 141 | +```cpp |
| 142 | +template <typename FinalizerType, typename T, typename Hint> |
| 143 | +inline void PostFinalizer(FinalizerType finalizeCallback, |
| 144 | + T* data, |
| 145 | + Hint* finalizeHint) const; |
| 146 | +``` |
| 147 | + |
| 148 | +- `[in] finalizeCallback`: The function to queue for execution outside of the GC |
| 149 | + finalization, implementing `operator()(Napi::Env, T*, Hint*)`. See |
| 150 | + [Finalization][] for more details. |
| 151 | +- `[in] data`: The data to associate with the object. |
| 152 | +- `[in] finalizeHint`: The hint value passed to the `finalizeCallback` function. |
| 153 | + |
| 154 | +### AddCleanupHook |
| 155 | + |
| 156 | +```cpp |
| 157 | +template <typename Hook, typename Arg> |
| 158 | +CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg); |
| 159 | +``` |
| 160 | +
|
| 161 | +- `[in] hook`: A function to call when the environment exits. Accepts a function |
| 162 | + of the form `void (Arg* arg)`. |
| 163 | +- `[in] arg`: A pointer to data that will be passed as the argument to `hook`. |
| 164 | +
|
| 165 | +Registers `hook` as a function to be run with the `arg` parameter once the |
| 166 | +current Node.js environment exits. Unlike the underlying C-based Node-API, |
| 167 | +providing the same `hook` and `arg` pair multiple times **is** allowed. The |
| 168 | +hooks will be called in reverse order, i.e. the most recently added one will be |
| 169 | +called first. |
| 170 | +
|
| 171 | +Returns an `Env::CleanupHook` object, which can be used to remove the hook via |
| 172 | +its `Remove()` method. |
| 173 | +
|
| 174 | +# Env::CleanupHook |
| 175 | +
|
| 176 | +The `Env::CleanupHook` object allows removal of the hook added via |
| 177 | +`Env::AddCleanupHook()` |
| 178 | +
|
| 179 | +## Methods |
| 180 | +
|
| 181 | +### IsEmpty |
| 182 | +
|
| 183 | +```cpp |
| 184 | +bool IsEmpty(); |
| 185 | +``` |
| 186 | + |
| 187 | +Returns `true` if the cleanup hook was **not** successfully registered. |
| 188 | + |
| 189 | +### Remove |
| 190 | + |
| 191 | +```cpp |
| 192 | +bool Remove(Env env); |
| 193 | +``` |
| 194 | +
|
| 195 | +Unregisters the hook from running once the current Node.js environment exits. |
| 196 | +
|
| 197 | +Returns `true` if the hook was successfully removed from the Node.js |
| 198 | +environment. |
| 199 | +
|
| 200 | +[Finalization]: ./finalization.md |
0 commit comments