nj_core/
lib.rs

1mod basic;
2mod error;
3mod thread_fn;
4mod property;
5mod class;
6mod worker;
7mod convert;
8mod module;
9pub mod buffer;
10pub mod bigint;
11pub mod stream;
12pub mod safebuffer;
13
14pub use thread_fn::ThreadSafeFunction;
15pub use error::*;
16pub use property::Property;
17pub use property::PropertiesBuilder;
18pub use class::JSClass;
19pub use worker::create_promise;
20pub use worker::JsPromiseFuture;
21pub use worker::NjFutureExt;
22pub use convert::*;
23pub use ctor::ctor;
24pub use module::submit_property;
25pub use module::submit_register_callback;
26
27use class::JSObjectWrapper;
28
29pub mod sys {
30    pub use nj_sys::*;
31}
32pub mod init {
33    pub use ctor::ctor as node_bindgen_init_once;
34}
35
36pub mod future {
37    pub use fluvio_future::task::spawn;
38}
39
40pub mod val {
41    pub use crate::basic::*;
42}
43
44pub mod log {
45    pub use tracing::*;
46}
47
48/// call napi and assert
49/// used only in this crate
50#[macro_export]
51macro_rules! napi_call_assert {
52    ($napi_expr:expr) => {{
53        #[allow(clippy::macro_metavars_in_unsafe)]
54        let status = unsafe { $napi_expr };
55        if status != $crate::sys::napi_status_napi_ok {
56            let nj_status: $crate::NapiStatus = status.into();
57            tracing::error!("error executing napi call {:#?}", nj_status);
58        }
59    }};
60}
61
62/// call napi and wrap into result
63/// used only in this crate
64#[macro_export]
65macro_rules! napi_call_result {
66    ($napi_expr:expr) => {{
67        #[allow(clippy::macro_metavars_in_unsafe)]
68        let status = unsafe { $napi_expr };
69        if status == $crate::sys::napi_status_napi_ok {
70            Ok(())
71        } else {
72            let nj_status: $crate::NapiStatus = status.into();
73            tracing::error!("node-bindgen error {:#?}", nj_status);
74            Err(NjError::NapiCall(nj_status))
75        }
76    }};
77}
78
79/// convert result into napi value if ok otherwise convert to error
80#[macro_export]
81macro_rules! result_to_napi {
82    ($result:expr) => {
83        match $result {
84            Ok(val) => val,
85            Err(err) => return err.into(),
86        }
87    };
88}
89
90#[macro_export]
91macro_rules! callback_to_napi {
92    ($result:expr,$js_env:expr) => {
93        match $result {
94            Ok(val) => val,
95            Err(err) => {
96                return;
97            }
98        }
99    };
100}
101
102/// assert the napi call
103#[macro_export]
104macro_rules! assert_napi {
105    ($result:expr) => {
106        match $result {
107            Ok(val) => val,
108            Err(err) => {
109                panic!("napi call failed: {}", err)
110            }
111        }
112    };
113}
114
115#[macro_export]
116macro_rules! c_str {
117    ($string:literal) => {{
118        const _C_STRING: &'static str = concat!($string, "\0");
119        _C_STRING
120    }};
121}
122
123#[macro_export]
124macro_rules! method {
125    ($name:literal,$rs_method:expr) => {{
126        nj::core::Property::new($name).method($rs_method)
127    }};
128}