nodex_api/
callback.rs

1use crate::{api, prelude::*};
2
3#[derive(Copy, Clone, Debug)]
4pub struct CallbackInfo(NapiEnv, napi_callback_info);
5
6impl CallbackInfo {
7    pub(crate) fn from_raw(env: NapiEnv, info: napi_callback_info) -> Self {
8        CallbackInfo(env, info)
9    }
10
11    pub fn env(&self) -> NapiEnv {
12        self.0
13    }
14
15    pub fn raw(&self) -> napi_callback_info {
16        self.1
17    }
18
19    /// This API returns the new.target of the constructor call. If the current callback is not a
20    /// constructor call, the result is NULL.
21    pub fn get_new_target(&self) -> NapiResult<Option<JsObject>> {
22        let value = napi_call!(=napi_get_new_target, self.env(), self.raw());
23        if value.is_null() {
24            Ok(None)
25        } else {
26            Ok(Some(JsObject::from_raw(self.env(), value)))
27        }
28    }
29}