Environment for Node-API values and operations. All Node-API values and operations must be associated with an environment. An environment instance is always provided to callback functions; that environment must then be used for any creation of Node-API values or other Node-API operations within the callback. (Many methods infer the environment from the `this` instance that the method is called on
| 340 | /// In the V8 JavaScript engine, a Node-API environment approximately |
| 341 | /// corresponds to an Isolate. |
| 342 | class BasicEnv { |
| 343 | private: |
| 344 | node_addon_api_basic_env _env; |
| 345 | #if NAPI_VERSION > 5 |
| 346 | template <typename T> |
| 347 | static void DefaultFini(Env, T* data); |
| 348 | template <typename DataType, typename HintType> |
| 349 | static void DefaultFiniWithHint(Env, DataType* data, HintType* hint); |
| 350 | #endif // NAPI_VERSION > 5 |
| 351 | public: |
| 352 | BasicEnv(node_addon_api_basic_env env); |
| 353 | |
| 354 | operator node_addon_api_basic_env() const; |
| 355 | |
| 356 | // Without these operator overloads, the error: |
| 357 | // |
| 358 | // Use of overloaded operator '==' is ambiguous (with operand types |
| 359 | // 'Napi::Env' and 'Napi::Env') |
| 360 | // |
| 361 | // ... occurs when comparing foo.Env() == bar.Env() or foo.Env() == nullptr |
| 362 | bool operator==(const BasicEnv& other) const { |
| 363 | return _env == other._env; |
| 364 | } |
| 365 | bool operator==(std::nullptr_t /*other*/) const { |
| 366 | return _env == nullptr; |
| 367 | } |
| 368 | |
| 369 | #if NAPI_VERSION > 2 |
| 370 | template <typename Hook, typename Arg = void> |
| 371 | class CleanupHook; |
| 372 | |
| 373 | template <typename Hook> |
| 374 | CleanupHook<Hook> AddCleanupHook(Hook hook); |
| 375 | |
| 376 | template <typename Hook, typename Arg> |
| 377 | CleanupHook<Hook, Arg> AddCleanupHook(Hook hook, Arg* arg); |
| 378 | #endif // NAPI_VERSION > 2 |
| 379 | |
| 380 | #if NAPI_VERSION > 5 |
| 381 | template <typename T> |
| 382 | T* GetInstanceData() const; |
| 383 | |
| 384 | template <typename T> |
| 385 | using Finalizer = void (*)(Env, T*); |
| 386 | template <typename T, Finalizer<T> fini = BasicEnv::DefaultFini<T>> |
| 387 | void SetInstanceData(T* data) const; |
| 388 | |
| 389 | template <typename DataType, typename HintType> |
| 390 | using FinalizerWithHint = void (*)(Env, DataType*, HintType*); |
| 391 | template <typename DataType, |
| 392 | typename HintType, |
| 393 | FinalizerWithHint<DataType, HintType> fini = |
| 394 | BasicEnv::DefaultFiniWithHint<DataType, HintType>> |
| 395 | void SetInstanceData(DataType* data, HintType* hint) const; |
| 396 | #endif // NAPI_VERSION > 5 |
| 397 | |
| 398 | #if NAPI_VERSION > 2 |
| 399 | template <typename Hook, typename Arg> |