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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
mod basic;
mod error;
mod thread_fn;
mod property;
mod class;
mod worker;
mod convert;
mod module;
pub mod buffer;
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 future {
    pub use fluvio_future::task::spawn;
}

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

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


/// 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();
                log::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();
                log::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)
        }
    }
}



pub fn init_logger() {
    fluvio_future::subscriber::init_logger();
}