1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
use std::ptr; use env::NapiEnv; use result::{NapiError, NapiResult}; use sys; use super::{NapiAny, NapiString, NapiValue, NapiValueInternal}; #[derive(Clone, Copy, Debug)] pub struct NapiNull<'env> { value: sys::napi_value, env: &'env NapiEnv, } impl<'env> NapiNull<'env> { pub fn new(env: &'env NapiEnv) -> NapiResult<Self> { let mut value = ptr::null_mut(); env.handle_status( unsafe { sys::napi_get_null(env.as_sys_env(), &mut value) }, )?; Ok(Self { value, env }) } } impl<'env> NapiValue<'env> for NapiNull<'env> { fn as_sys_value(&self) -> sys::napi_value { self.value } fn env(&self) -> &'env NapiEnv { self.env } fn from_sys_checked( env: &'env NapiEnv, value: sys::napi_value, ) -> NapiResult<Self> { let null = NapiNull::new(env)?; if !NapiAny::with_value(env, value).strict_equals(&null)? { let message = NapiString::from_str(env, "Null expected")?; return Err(NapiError::type_error(env, &message)); } Ok(Self { env, value }) } } impl<'env> NapiValueInternal<'env> for NapiNull<'env> { fn construct(env: &'env NapiEnv, value: sys::napi_value) -> Self { Self { env, value } } }