napi_h/bindgen_runtime/
env.rs

1use std::ptr;
2
3use crate::{check_status, sys, JsGlobal, JsNull, JsUndefined, NapiValue, Result};
4
5use super::Array;
6
7pub use crate::Env;
8
9impl Env {
10  pub fn create_array(&self, len: u32) -> Result<Array> {
11    Array::new(self.0, len)
12  }
13
14  /// Get [JsUndefined](./struct.JsUndefined.html) value
15  pub fn get_undefined(&self) -> Result<JsUndefined> {
16    let mut raw_value = ptr::null_mut();
17    check_status!(unsafe { sys::napi_get_undefined(self.0, &mut raw_value) })?;
18    let js_undefined = unsafe { JsUndefined::from_raw_unchecked(self.0, raw_value) };
19    Ok(js_undefined)
20  }
21
22  pub fn get_null(&self) -> Result<JsNull> {
23    let mut raw_value = ptr::null_mut();
24    check_status!(unsafe { sys::napi_get_null(self.0, &mut raw_value) })?;
25    let js_null = unsafe { JsNull::from_raw_unchecked(self.0, raw_value) };
26    Ok(js_null)
27  }
28
29  pub fn get_global(&self) -> Result<JsGlobal> {
30    let mut global = std::ptr::null_mut();
31    crate::check_status!(
32      unsafe { sys::napi_get_global(self.0, &mut global) },
33      "Get global object from Env failed"
34    )?;
35    Ok(JsGlobal(crate::Value {
36      value: global,
37      env: self.0,
38      value_type: crate::ValueType::Object,
39    }))
40  }
41}