nodex_api/scope/
callback.rs

1use crate::{api, prelude::*};
2use std::mem::MaybeUninit;
3
4/// There are cases (for example, resolving promises) where it is necessary to have the equivalent
5/// of the scope associated with a callback in place when making certain Node-API calls. If there
6/// is no other script on the stack the napi_open_callback_scope and napi_close_callback_scope
7/// functions can be used to open/close the required scope.
8#[derive(Clone, Debug)]
9pub struct NapiCallbackScope(NapiEnv, napi_callback_scope);
10
11impl NapiCallbackScope {
12    pub(crate) fn from_raw(env: NapiEnv, scope: napi_callback_scope) -> NapiCallbackScope {
13        NapiCallbackScope(env, scope)
14    }
15
16    pub fn env(&self) -> NapiEnv {
17        self.0
18    }
19
20    pub fn raw(&self) -> napi_callback_scope {
21        self.1
22    }
23
24    pub fn close(&mut self) -> NapiResult<()> {
25        napi_call!(napi_close_callback_scope, self.env(), self.raw())
26    }
27}
28
29impl Drop for NapiCallbackScope {
30    fn drop(&mut self) {
31        if let Err(e) = self.close() {
32            log::warn!("[{}] napi_close_callback_scope failed.", e);
33        }
34    }
35}