2020-05-12 14:59:20 +09:00
|
|
|
mod async_work;
|
2020-04-26 19:46:56 +09:00
|
|
|
mod call_context;
|
2020-06-21 20:10:06 +09:00
|
|
|
mod env;
|
|
|
|
mod error;
|
|
|
|
mod js_values;
|
|
|
|
mod module;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(all(feature = "libuv", napi4))]
|
|
|
|
mod promise;
|
2020-07-01 21:44:29 +09:00
|
|
|
mod status;
|
2018-04-28 17:26:38 +09:00
|
|
|
pub mod sys;
|
2020-05-11 01:28:06 +09:00
|
|
|
mod task;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(napi4)]
|
2020-06-19 21:42:18 +09:00
|
|
|
pub mod threadsafe_function;
|
2020-07-03 01:31:50 +09:00
|
|
|
#[cfg(all(feature = "libuv", napi4))]
|
|
|
|
mod uv;
|
2020-05-06 00:13:23 +09:00
|
|
|
mod version;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
pub use async_work::AsyncWork;
|
2020-04-21 01:20:35 +09:00
|
|
|
pub use call_context::CallContext;
|
2020-06-21 20:10:06 +09:00
|
|
|
pub use env::*;
|
|
|
|
pub use error::{Error, Result};
|
|
|
|
pub use js_values::*;
|
|
|
|
pub use module::Module;
|
2020-07-01 21:44:29 +09:00
|
|
|
pub use status::Status;
|
|
|
|
pub use sys::napi_valuetype;
|
2020-05-11 01:28:06 +09:00
|
|
|
pub use task::Task;
|
2020-05-06 00:13:23 +09:00
|
|
|
pub use version::NodeVersion;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! register_module {
|
|
|
|
($module_name:ident, $init:ident) => {
|
|
|
|
#[no_mangle]
|
|
|
|
#[cfg_attr(target_os = "linux", link_section = ".ctors")]
|
|
|
|
#[cfg_attr(target_os = "macos", link_section = "__DATA,__mod_init_func")]
|
|
|
|
#[cfg_attr(target_os = "windows", link_section = ".CRT$XCU")]
|
|
|
|
pub static __REGISTER_MODULE: extern "C" fn() = {
|
|
|
|
use std::io::Write;
|
|
|
|
use std::os::raw::c_char;
|
|
|
|
use std::ptr;
|
2020-06-21 20:10:06 +09:00
|
|
|
use $crate::{sys, Env, JsObject, Module, NapiValue};
|
2018-04-28 17:26:38 +09:00
|
|
|
|
|
|
|
extern "C" fn register_module() {
|
|
|
|
static mut MODULE_DESCRIPTOR: Option<sys::napi_module> = None;
|
|
|
|
unsafe {
|
|
|
|
MODULE_DESCRIPTOR = Some(sys::napi_module {
|
|
|
|
nm_version: 1,
|
|
|
|
nm_flags: 0,
|
|
|
|
nm_filename: concat!(file!(), "\0").as_ptr() as *const c_char,
|
|
|
|
nm_register_func: Some(init_module),
|
|
|
|
nm_modname: concat!(stringify!($module_name), "\0").as_ptr() as *const c_char,
|
|
|
|
nm_priv: 0 as *mut _,
|
|
|
|
reserved: [0 as *mut _; 4],
|
|
|
|
});
|
|
|
|
|
|
|
|
sys::napi_module_register(MODULE_DESCRIPTOR.as_mut().unwrap() as *mut sys::napi_module);
|
|
|
|
}
|
|
|
|
|
|
|
|
extern "C" fn init_module(
|
|
|
|
raw_env: sys::napi_env,
|
|
|
|
raw_exports: sys::napi_value,
|
|
|
|
) -> sys::napi_value {
|
|
|
|
let env = Env::from_raw(raw_env);
|
2020-06-21 20:10:06 +09:00
|
|
|
let mut exports: JsObject = JsObject::from_raw_unchecked(raw_env, raw_exports);
|
|
|
|
let mut cjs_module = Module { env, exports };
|
|
|
|
let result = $init(&mut cjs_module);
|
2018-04-28 17:26:38 +09:00
|
|
|
|
|
|
|
match result {
|
2020-05-09 16:14:44 +09:00
|
|
|
Ok(_) => exports.into_raw(),
|
2018-04-28 17:26:38 +09:00
|
|
|
Err(e) => {
|
2020-05-09 16:14:44 +09:00
|
|
|
unsafe {
|
|
|
|
sys::napi_throw_error(
|
|
|
|
raw_env,
|
|
|
|
ptr::null(),
|
|
|
|
format!("Error initializing module: {:?}", e).as_ptr() as *const _,
|
|
|
|
)
|
|
|
|
};
|
2018-04-28 17:26:38 +09:00
|
|
|
ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
register_module
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|