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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
mod basic;
mod error;
mod thread_fn;
mod property;
mod class;
mod worker;
mod convert;
mod module;
pub mod buffer;
pub mod bigint;
pub mod stream;

pub use thread_fn::ThreadSafeFunction;
pub use error::*;
pub use property::Property;
pub use property::PropertiesBuilder;
pub use class::JSClass;
pub use worker::create_promise;
pub use worker::JsPromiseFuture;
pub use worker::NjFutureExt;
pub use convert::*;
pub use ctor::ctor;
pub use module::submit_property;
pub use module::submit_register_callback;

use class::JSObjectWrapper;

pub mod sys {
    pub use nj_sys::*;
}
pub mod init {
    pub use ctor::ctor as node_bindgen_init_once;
}

pub mod future {
    pub use fluvio_future::task::spawn;
}

pub mod val {
    pub use crate::basic::*;
}

pub mod log {
    pub use tracing::*;
}

/// call napi and assert
/// used only in this crate
#[macro_export]
macro_rules! napi_call_assert {
    ($napi_expr:expr) => {{
        let status = unsafe { $napi_expr };
        if status != $crate::sys::napi_status_napi_ok {
            let nj_status: $crate::NapiStatus = status.into();
            tracing::error!("error executing napi call {:#?}", nj_status);
        }
    }};
}

/// call napi and wrap into result
/// used only in this crate
#[macro_export]
macro_rules! napi_call_result {
    ($napi_expr:expr) => {{
        let status = unsafe { $napi_expr };
        if status == $crate::sys::napi_status_napi_ok {
            Ok(())
        } else {
            let nj_status: $crate::NapiStatus = status.into();
            tracing::error!("node-bindgen error {:#?}", nj_status);
            Err(NjError::NapiCall(nj_status))
        }
    }};
}

/// convert result into napi value if ok otherwise convert to error
#[macro_export]
macro_rules! result_to_napi {
    ($result:expr) => {
        match $result {
            Ok(val) => val,
            Err(err) => return err.into(),
        }
    };
}

#[macro_export]
macro_rules! callback_to_napi {
    ($result:expr,$js_env:expr) => {
        match $result {
            Ok(val) => val,
            Err(err) => {
                return;
            }
        }
    };
}

/// assert the napi call
#[macro_export]
macro_rules! assert_napi {
    ($result:expr) => {
        match $result {
            Ok(val) => val,
            Err(err) => {
                panic!("napi call failed: {}", err)
            }
        }
    };
}

#[macro_export]
macro_rules! c_str {
    ($string:literal) => {{
        const _C_STRING: &'static str = concat!($string, "\0");
        _C_STRING
    }};
}

#[macro_export]
macro_rules! method {
    ($name:literal,$rs_method:expr) => {{
        nj::core::Property::new($name).method($rs_method)
    }};
}