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