18c339a94Sopenharmony_ci# Env
28c339a94Sopenharmony_ci
38c339a94Sopenharmony_ciThe opaque data structure containing the environment in which the request is being run.
48c339a94Sopenharmony_ci
58c339a94Sopenharmony_ciThe Env object is usually created and passed by the Node.js runtime or node-addon-api infrastructure.
68c339a94Sopenharmony_ci
78c339a94Sopenharmony_ci## Methods
88c339a94Sopenharmony_ci
98c339a94Sopenharmony_ci### Constructor
108c339a94Sopenharmony_ci
118c339a94Sopenharmony_ci```cpp
128c339a94Sopenharmony_ciNapi::Env::Env(napi_env env);
138c339a94Sopenharmony_ci```
148c339a94Sopenharmony_ci
158c339a94Sopenharmony_ci- `[in] env`: The `napi_env` environment from which to construct the `Napi::Env` object.
168c339a94Sopenharmony_ci
178c339a94Sopenharmony_ci### napi_env
188c339a94Sopenharmony_ci
198c339a94Sopenharmony_ci```cpp
208c339a94Sopenharmony_cioperator napi_env() const;
218c339a94Sopenharmony_ci```
228c339a94Sopenharmony_ci
238c339a94Sopenharmony_ciReturns the `napi_env` opaque data structure representing the environment.
248c339a94Sopenharmony_ci
258c339a94Sopenharmony_ci### Global
268c339a94Sopenharmony_ci
278c339a94Sopenharmony_ci```cpp
288c339a94Sopenharmony_ciNapi::Object Napi::Env::Global() const;
298c339a94Sopenharmony_ci```
308c339a94Sopenharmony_ci
318c339a94Sopenharmony_ciReturns the `Napi::Object` representing the environment's JavaScript Global Object.
328c339a94Sopenharmony_ci
338c339a94Sopenharmony_ci### Undefined
348c339a94Sopenharmony_ci
358c339a94Sopenharmony_ci```cpp
368c339a94Sopenharmony_ciNapi::Value Napi::Env::Undefined() const;
378c339a94Sopenharmony_ci```
388c339a94Sopenharmony_ci
398c339a94Sopenharmony_ciReturns the `Napi::Value` representing the environment's JavaScript Undefined Object.
408c339a94Sopenharmony_ci
418c339a94Sopenharmony_ci### Null
428c339a94Sopenharmony_ci
438c339a94Sopenharmony_ci```cpp
448c339a94Sopenharmony_ciNapi::Value Napi::Env::Null() const;
458c339a94Sopenharmony_ci```
468c339a94Sopenharmony_ci
478c339a94Sopenharmony_ciReturns the `Napi::Value` representing the environment's JavaScript Null Object.
488c339a94Sopenharmony_ci
498c339a94Sopenharmony_ci### IsExceptionPending
508c339a94Sopenharmony_ci
518c339a94Sopenharmony_ci```cpp
528c339a94Sopenharmony_cibool Napi::Env::IsExceptionPending() const;
538c339a94Sopenharmony_ci```
548c339a94Sopenharmony_ci
558c339a94Sopenharmony_ciReturns a `bool` indicating if an exception is pending in the environment.
568c339a94Sopenharmony_ci
578c339a94Sopenharmony_ci### GetAndClearPendingException
588c339a94Sopenharmony_ci
598c339a94Sopenharmony_ci```cpp
608c339a94Sopenharmony_ciNapi::Error Napi::Env::GetAndClearPendingException() const;
618c339a94Sopenharmony_ci```
628c339a94Sopenharmony_ci
638c339a94Sopenharmony_ciReturns an `Napi::Error` object representing the environment's pending exception, if any.
648c339a94Sopenharmony_ci
658c339a94Sopenharmony_ci### RunScript
668c339a94Sopenharmony_ci
678c339a94Sopenharmony_ci```cpp
688c339a94Sopenharmony_ciNapi::Value Napi::Env::RunScript(____ script) const;
698c339a94Sopenharmony_ci```
708c339a94Sopenharmony_ci- `[in] script`: A string containing JavaScript code to execute.
718c339a94Sopenharmony_ci
728c339a94Sopenharmony_ciRuns JavaScript code contained in a string and returns its result.
738c339a94Sopenharmony_ci
748c339a94Sopenharmony_ciThe `script` can be any of the following types:
758c339a94Sopenharmony_ci- [`Napi::String`](string.md)
768c339a94Sopenharmony_ci- `const char *`
778c339a94Sopenharmony_ci- `const std::string &`
788c339a94Sopenharmony_ci
798c339a94Sopenharmony_ci### GetInstanceData
808c339a94Sopenharmony_ci```cpp
818c339a94Sopenharmony_citemplate <typename T> T* GetInstanceData() const;
828c339a94Sopenharmony_ci```
838c339a94Sopenharmony_ci
848c339a94Sopenharmony_ciReturns the instance data that was previously associated with the environment,
858c339a94Sopenharmony_cior `nullptr` if none was associated.
868c339a94Sopenharmony_ci
878c339a94Sopenharmony_ci### SetInstanceData
888c339a94Sopenharmony_ci
898c339a94Sopenharmony_ci```cpp
908c339a94Sopenharmony_citemplate <typename T> using Finalizer = void (*)(Env, T*);
918c339a94Sopenharmony_citemplate <typename T, Finalizer<T> fini = Env::DefaultFini<T>>
928c339a94Sopenharmony_civoid SetInstanceData(T* data) const;
938c339a94Sopenharmony_ci```
948c339a94Sopenharmony_ci
958c339a94Sopenharmony_ci- `[template] fini`: A function to call when the instance data is to be deleted.
968c339a94Sopenharmony_ciAccepts a function of the form `void CleanupData(Napi::Env env, T* data)`. If
978c339a94Sopenharmony_cinot given, the default finalizer will be used, which simply uses the `delete`
988c339a94Sopenharmony_cioperator to destroy `T*` when the addon instance is unloaded.
998c339a94Sopenharmony_ci- `[in] data`: A pointer to data that will be associated with the instance of
1008c339a94Sopenharmony_cithe addon for the duration of its lifecycle.
1018c339a94Sopenharmony_ci
1028c339a94Sopenharmony_ciAssociates a data item stored at `T* data` with the current instance of the
1038c339a94Sopenharmony_ciaddon. The item will be passed to the function `fini` which gets called when an
1048c339a94Sopenharmony_ciinstance of the addon is unloaded.
1058c339a94Sopenharmony_ci
1068c339a94Sopenharmony_ci### SetInstanceData
1078c339a94Sopenharmony_ci
1088c339a94Sopenharmony_ci```cpp
1098c339a94Sopenharmony_citemplate <typename DataType, typename HintType>
1108c339a94Sopenharmony_ciusing FinalizerWithHint = void (*)(Env, DataType*, HintType*);
1118c339a94Sopenharmony_citemplate <typename DataType,
1128c339a94Sopenharmony_ci          typename HintType,
1138c339a94Sopenharmony_ci          FinalizerWithHint<DataType, HintType> fini =
1148c339a94Sopenharmony_ci            Env::DefaultFiniWithHint<DataType, HintType>>
1158c339a94Sopenharmony_civoid SetInstanceData(DataType* data, HintType* hint) const;
1168c339a94Sopenharmony_ci```
1178c339a94Sopenharmony_ci
1188c339a94Sopenharmony_ci- `[template] fini`: A function to call when the instance data is to be deleted.
1198c339a94Sopenharmony_ciAccepts a function of the form
1208c339a94Sopenharmony_ci`void CleanupData(Napi::Env env, DataType* data, HintType* hint)`. If not given,
1218c339a94Sopenharmony_cithe default finalizer will be used, which simply uses the `delete` operator to
1228c339a94Sopenharmony_cidestroy `T*` when the addon instance is unloaded.
1238c339a94Sopenharmony_ci- `[in] data`: A pointer to data that will be associated with the instance of
1248c339a94Sopenharmony_cithe addon for the duration of its lifecycle.
1258c339a94Sopenharmony_ci- `[in] hint`: A pointer to data that will be associated with the instance of
1268c339a94Sopenharmony_cithe addon for the duration of its lifecycle and will be passed as a hint to
1278c339a94Sopenharmony_ci`fini` when the addon instance is unloaded.
1288c339a94Sopenharmony_ci
1298c339a94Sopenharmony_ciAssociates a data item stored at `T* data` with the current instance of the
1308c339a94Sopenharmony_ciaddon. The item will be passed to the function `fini` which gets called when an
1318c339a94Sopenharmony_ciinstance of the addon is unloaded. This overload accepts an additional hint to
1328c339a94Sopenharmony_cibe passed to `fini`.
1338c339a94Sopenharmony_ci
1348c339a94Sopenharmony_ci### GetModuleFileName
1358c339a94Sopenharmony_ci
1368c339a94Sopenharmony_ci```cpp
1378c339a94Sopenharmony_ciconst char* Napi::Env::GetModuleFileName() const;
1388c339a94Sopenharmony_ci```
1398c339a94Sopenharmony_ci
1408c339a94Sopenharmony_ciReturns a A URL containing the absolute path of the location from which the
1418c339a94Sopenharmony_ciadd-on was loaded. For a file on the local file system it will start with
1428c339a94Sopenharmony_ci`file://`. The string is null-terminated and owned by env and must thus not be
1438c339a94Sopenharmony_cimodified or freed. It is only valid while the add-on is loaded.
1448c339a94Sopenharmony_ci
1458c339a94Sopenharmony_ci### AddCleanupHook
1468c339a94Sopenharmony_ci
1478c339a94Sopenharmony_ci```cpp
1488c339a94Sopenharmony_citemplate <typename Hook>
1498c339a94Sopenharmony_ciCleanupHook<Hook> AddCleanupHook(Hook hook);
1508c339a94Sopenharmony_ci```
1518c339a94Sopenharmony_ci
1528c339a94Sopenharmony_ci- `[in] hook`: A function to call when the environment exits. Accepts a
1538c339a94Sopenharmony_ci  function of the form `void ()`.
1548c339a94Sopenharmony_ci
1558c339a94Sopenharmony_ciRegisters `hook` as a function to be run once the current Node.js environment
1568c339a94Sopenharmony_ciexits. Unlike the underlying C-based Node-API, providing the same `hook`
1578c339a94Sopenharmony_cimultiple times **is** allowed. The hooks will be called in reverse order, i.e.
1588c339a94Sopenharmony_cithe most recently added one will be called first.
1598c339a94Sopenharmony_ci
1608c339a94Sopenharmony_ciReturns an `Env::CleanupHook` object, which can be used to remove the hook via
1618c339a94Sopenharmony_ciits `Remove()` method.
1628c339a94Sopenharmony_ci
1638c339a94Sopenharmony_ci### AddCleanupHook
1648c339a94Sopenharmony_ci
1658c339a94Sopenharmony_ci```cpp
1668c339a94Sopenharmony_citemplate <typename Hook, typename Arg>
1678c339a94Sopenharmony_ciCleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg);
1688c339a94Sopenharmony_ci```
1698c339a94Sopenharmony_ci
1708c339a94Sopenharmony_ci- `[in] hook`: A function to call when the environment exits. Accepts a
1718c339a94Sopenharmony_ci  function of the form `void (Arg* arg)`.
1728c339a94Sopenharmony_ci- `[in] arg`: A pointer to data that will be passed as the argument to `hook`.
1738c339a94Sopenharmony_ci
1748c339a94Sopenharmony_ciRegisters `hook` as a function to be run with the `arg` parameter once the
1758c339a94Sopenharmony_cicurrent Node.js environment exits. Unlike the underlying C-based Node-API,
1768c339a94Sopenharmony_ciproviding the same `hook` and `arg` pair multiple times **is** allowed. The
1778c339a94Sopenharmony_cihooks will be called in reverse order, i.e. the most recently added one will be
1788c339a94Sopenharmony_cicalled first.
1798c339a94Sopenharmony_ci
1808c339a94Sopenharmony_ciReturns an `Env::CleanupHook` object, which can be used to remove the hook via
1818c339a94Sopenharmony_ciits `Remove()` method.
1828c339a94Sopenharmony_ci
1838c339a94Sopenharmony_ci# Env::CleanupHook
1848c339a94Sopenharmony_ci
1858c339a94Sopenharmony_ciThe `Env::CleanupHook` object allows removal of the hook added via
1868c339a94Sopenharmony_ci`Env::AddCleanupHook()`
1878c339a94Sopenharmony_ci
1888c339a94Sopenharmony_ci## Methods
1898c339a94Sopenharmony_ci
1908c339a94Sopenharmony_ci### IsEmpty
1918c339a94Sopenharmony_ci
1928c339a94Sopenharmony_ci```cpp
1938c339a94Sopenharmony_cibool IsEmpty();
1948c339a94Sopenharmony_ci```
1958c339a94Sopenharmony_ci
1968c339a94Sopenharmony_ciReturns `true` if the cleanup hook was **not** successfully registered.
1978c339a94Sopenharmony_ci
1988c339a94Sopenharmony_ci### Remove
1998c339a94Sopenharmony_ci
2008c339a94Sopenharmony_ci```cpp
2018c339a94Sopenharmony_cibool Remove(Env env);
2028c339a94Sopenharmony_ci```
2038c339a94Sopenharmony_ci
2048c339a94Sopenharmony_ciUnregisters the hook from running once the current Node.js environment exits.
2058c339a94Sopenharmony_ci
2068c339a94Sopenharmony_ciReturns `true` if the hook was successfully removed from the Node.js
2078c339a94Sopenharmony_cienvironment.
208