edon/napi/
mod.rs

1#![allow(non_upper_case_globals)]
2
3mod async_cleanup_hook;
4pub use async_cleanup_hook::AsyncCleanupHook;
5mod async_work;
6mod bindgen_runtime;
7mod call_context;
8mod cleanup_env;
9mod env;
10mod error;
11pub mod js_values;
12mod napi_ext;
13mod status;
14mod task;
15pub mod threadsafe_function;
16mod value_type;
17mod version;
18
19pub use cleanup_env::CleanupEnvHook;
20
21pub use self::async_work::AsyncWorkPromise;
22pub use self::bindgen_runtime::iterator;
23pub use self::bindgen_runtime::*;
24pub use self::call_context::CallContext;
25pub use self::env::*;
26pub use self::error::*;
27pub use self::js_values::*;
28pub use self::napi_ext::*;
29pub use self::status::Status;
30pub use self::task::Task;
31pub use self::value_type::*;
32pub use self::version::NodeVersion;
33
34#[cfg(feature = "serde-json")]
35#[macro_use]
36extern crate serde;
37
38pub type ContextlessResult<T> = Result<Option<T>>;
39
40#[doc(hidden)]
41#[macro_export(local_inner_macros)]
42macro_rules! _type_of {
43  ($env:expr, $value:expr) => {{
44    let mut value_type = 0;
45    #[allow(unused_unsafe)]
46    crate::napi::error::check_status!(unsafe {
47      libnode_sys::napi_typeof($env, $value, &mut value_type)
48    })
49    .and_then(|_| Ok($crate::napi::ValueType::from(value_type)))
50  }};
51}
52pub use _type_of as type_of;
53
54#[doc(hidden)]
55#[macro_export]
56macro_rules! _assert_type_of {
57  ($env: expr, $value:expr, $value_ty: expr) => {
58    $crate::napi::type_of!($env, $value).and_then(|received_type| {
59      if received_type == $value_ty {
60        Ok(())
61      } else {
62        Err($crate::napi::Error::new(
63          $crate::napi::Status::InvalidArg,
64          format!(
65            "Expect value to be {}, but received {}",
66            $value_ty, received_type
67          ),
68        ))
69      }
70    })
71  };
72}
73pub use _assert_type_of as assert_type_of;
74
75pub use crate::napi::bindgen_runtime::ctor as module_init;
76
77pub mod bindgen_prelude {
78  pub use self::check_status_or_throw;
79  pub use super::assert_type_of;
80  pub use super::bindgen_runtime::*;
81  pub use crate::napi::check_pending_exception;
82  pub use crate::napi::check_status;
83  pub use crate::napi::error;
84  pub use crate::napi::error::*;
85  pub use crate::napi::type_of;
86  pub use crate::napi::JsError;
87  pub use crate::napi::Property;
88  pub use crate::napi::PropertyAttributes;
89  pub use crate::napi::Result;
90  pub use crate::napi::Status;
91  pub use crate::napi::Task;
92  pub use crate::napi::ValueType;
93}
94
95#[doc(hidden)]
96pub mod __private {
97  use libnode_sys;
98
99  pub use crate::napi::bindgen_runtime::get_class_constructor;
100  pub use crate::napi::bindgen_runtime::iterator::create_iterator;
101  pub use crate::napi::bindgen_runtime::register_class;
102  pub use crate::napi::bindgen_runtime::___CALL_FROM_FACTORY;
103
104  pub unsafe fn log_js_value<V: AsRef<[libnode_sys::napi_value]>>(
105    // `info`, `log`, `warning` or `error`
106    method: &str,
107    env: libnode_sys::napi_env,
108    values: V,
109  ) {
110    use std::ffi::CString;
111    use std::ptr;
112
113    let mut g = ptr::null_mut();
114    unsafe { libnode_sys::napi_get_global(env, &mut g) };
115    let mut console = ptr::null_mut();
116    let console_c_string = CString::new("console").unwrap();
117    let method_c_string = CString::new(method).unwrap();
118    unsafe {
119      libnode_sys::napi_get_named_property(env, g, console_c_string.as_ptr(), &mut console)
120    };
121    let mut method_js_fn = ptr::null_mut();
122    unsafe {
123      libnode_sys::napi_get_named_property(
124        env,
125        console,
126        method_c_string.as_ptr(),
127        &mut method_js_fn,
128      )
129    };
130    unsafe {
131      libnode_sys::napi_call_function(
132        env,
133        console,
134        method_js_fn,
135        values.as_ref().len(),
136        values.as_ref().as_ptr(),
137        ptr::null_mut(),
138      )
139    };
140  }
141}
142
143#[cfg(feature = "error-anyhow")]
144pub extern crate anyhow;