2020-12-01 15:55:19 +09:00
|
|
|
#![deny(clippy::all)]
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
//! High level Node.js [N-API](https://nodejs.org/api/n-api.html) binding
|
2020-07-14 23:57:11 +09:00
|
|
|
//!
|
|
|
|
//! **napi-rs** provides minimal overhead to write N-API modules in `Rust`.
|
2020-09-03 21:38:28 +09:00
|
|
|
//!
|
2020-07-14 23:57:11 +09:00
|
|
|
//! ## Feature flags
|
2020-09-03 21:38:28 +09:00
|
|
|
//!
|
2021-04-21 18:19:45 +09:00
|
|
|
//! ### napi1 ~ napi8
|
2020-07-14 23:57:11 +09:00
|
|
|
//!
|
2021-09-23 02:29:09 +09:00
|
|
|
//! Because `Node.js` N-API has versions. So there are feature flags to choose what version of `N-API` you want to build for.
|
2020-12-03 18:17:40 +09:00
|
|
|
//! For example, if you want build a library which can be used by `node@10.17.0`, you should choose the `napi5` or lower.
|
|
|
|
//!
|
2020-12-22 22:32:50 +09:00
|
|
|
//! The details of N-API versions and support matrix: [n_api_version_matrix](https://nodejs.org/api/n-api.html#n_api_n_api_version_matrix)
|
2020-07-14 23:57:11 +09:00
|
|
|
//!
|
|
|
|
//! ### tokio_rt
|
|
|
|
//! With `tokio_rt` feature, `napi-rs` provides a ***tokio runtime*** in an additional thread.
|
|
|
|
//! And you can easily run tokio `future` in it and return `promise`.
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! use futures::prelude::*;
|
|
|
|
//! use napi::{CallContext, Error, JsObject, JsString, Result, Status};
|
|
|
|
//! use tokio;
|
|
|
|
//!
|
|
|
|
//! #[js_function(1)]
|
|
|
|
//! pub fn tokio_readfile(ctx: CallContext) -> Result<JsObject> {
|
2020-10-14 12:29:51 +09:00
|
|
|
//! let js_filepath = ctx.get::<JsString>(0)?;
|
|
|
|
//! let path_str = js_filepath.as_str()?;
|
|
|
|
//! ctx.env.execute_tokio_future(
|
|
|
|
//! tokio::fs::read(path_str.to_owned())
|
|
|
|
//! .map(|v| v.map_err(|e| Error::new(Status::Unknown, format!("failed to read file, {}", e)))),
|
|
|
|
//! |&mut env, data| env.create_buffer_with_data(data),
|
|
|
|
//! )
|
2020-07-14 23:57:11 +09:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
|
|
|
//! ***Tokio channel in `napi-rs` buffer size is default `100`.***
|
|
|
|
//!
|
|
|
|
//! ***You can adjust it via `NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE` environment variable***
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! NAPI_RS_TOKIO_CHANNEL_BUFFER_SIZE=1000 node ./app.js
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-09-03 21:38:28 +09:00
|
|
|
//! ### latin1
|
|
|
|
//!
|
|
|
|
//! Decode latin1 string from JavaScript using [encoding_rs](https://docs.rs/encoding_rs).
|
|
|
|
//!
|
|
|
|
//! With this feature, you can use `JsString.as_latin1_string` function
|
|
|
|
//!
|
|
|
|
//! ### serde-json
|
|
|
|
//!
|
|
|
|
//! Enable Serialize/Deserialize data cross `JavaScript Object` and `Rust struct`.
|
|
|
|
//!
|
|
|
|
//! ```
|
|
|
|
//! #[derive(Serialize, Debug, Deserialize)]
|
|
|
|
//! struct AnObject {
|
2020-10-14 12:29:51 +09:00
|
|
|
//! a: u32,
|
|
|
|
//! b: Vec<f64>,
|
|
|
|
//! c: String,
|
2020-09-03 21:38:28 +09:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[js_function(1)]
|
|
|
|
//! fn deserialize_from_js(ctx: CallContext) -> Result<JsUndefined> {
|
2020-10-14 12:29:51 +09:00
|
|
|
//! let arg0 = ctx.get::<JsUnknown>(0)?;
|
|
|
|
//! let de_serialized: AnObject = ctx.env.from_js_value(arg0)?;
|
|
|
|
//! ...
|
2020-09-03 21:38:28 +09:00
|
|
|
//! }
|
|
|
|
//!
|
|
|
|
//! #[js_function]
|
|
|
|
//! fn serialize(ctx: CallContext) -> Result<JsUnknown> {
|
2020-10-14 12:29:51 +09:00
|
|
|
//! let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" };
|
|
|
|
//! ctx.env.to_js_value(&value)
|
2020-09-03 21:38:28 +09:00
|
|
|
//! }
|
|
|
|
//! ```
|
|
|
|
//!
|
2020-07-14 23:57:11 +09:00
|
|
|
|
2021-04-21 18:19:45 +09:00
|
|
|
#[cfg(feature = "napi8")]
|
|
|
|
mod async_cleanup_hook;
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-04-21 18:19:45 +09:00
|
|
|
#[cfg(feature = "napi8")]
|
|
|
|
pub use async_cleanup_hook::AsyncCleanupHook;
|
2020-05-12 14:59:20 +09:00
|
|
|
mod async_work;
|
2021-09-23 02:29:09 +09:00
|
|
|
mod bindgen_runtime;
|
2020-04-26 19:46:56 +09:00
|
|
|
mod call_context;
|
2020-11-10 12:09:25 +09:00
|
|
|
#[cfg(feature = "napi3")]
|
2020-10-04 17:02:04 +09:00
|
|
|
mod cleanup_env;
|
2020-06-21 20:10:06 +09:00
|
|
|
mod env;
|
|
|
|
mod error;
|
|
|
|
mod js_values;
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2020-11-10 12:09:25 +09:00
|
|
|
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
|
2020-07-03 01:31:50 +09:00
|
|
|
mod promise;
|
2020-07-01 21:44:29 +09:00
|
|
|
mod status;
|
2020-05-11 01:28:06 +09:00
|
|
|
mod task;
|
2021-10-20 01:02:47 +09:00
|
|
|
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
|
|
|
|
mod tokio_runtime;
|
2021-09-23 02:29:09 +09:00
|
|
|
mod value_type;
|
2020-11-10 12:09:25 +09:00
|
|
|
#[cfg(feature = "napi3")]
|
2020-10-04 17:02:04 +09:00
|
|
|
pub use cleanup_env::CleanupEnvHook;
|
2020-11-10 12:09:25 +09:00
|
|
|
#[cfg(feature = "napi4")]
|
2020-06-19 21:42:18 +09:00
|
|
|
pub mod threadsafe_function;
|
2021-06-03 00:03:47 +09:00
|
|
|
|
2020-05-06 00:13:23 +09:00
|
|
|
mod version;
|
2020-10-31 10:24:19 +09:00
|
|
|
#[cfg(target_os = "windows")]
|
|
|
|
mod win_delay_load_hook;
|
2018-04-28 17:26:38 +09:00
|
|
|
|
2020-07-15 01:43:20 +09:00
|
|
|
pub use napi_sys as sys;
|
|
|
|
|
2020-12-22 22:32:50 +09:00
|
|
|
pub use async_work::AsyncWorkPromise;
|
2020-04-21 01:20:35 +09:00
|
|
|
pub use call_context::CallContext;
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
pub use env::*;
|
2021-09-23 02:29:09 +09:00
|
|
|
pub use error::*;
|
2020-06-21 20:10:06 +09:00
|
|
|
pub use js_values::*;
|
2020-07-01 21:44:29 +09:00
|
|
|
pub use status::Status;
|
2020-05-11 01:28:06 +09:00
|
|
|
pub use task::Task;
|
2021-10-20 01:02:47 +09:00
|
|
|
#[cfg(all(feature = "tokio_rt", feature = "napi4"))]
|
|
|
|
pub use tokio_runtime::shutdown_tokio_rt;
|
2021-09-23 02:29:09 +09:00
|
|
|
pub use value_type::*;
|
2020-05-06 00:13:23 +09:00
|
|
|
pub use version::NodeVersion;
|
2020-08-26 01:07:27 +09:00
|
|
|
#[cfg(feature = "serde-json")]
|
|
|
|
#[macro_use]
|
|
|
|
extern crate serde;
|
|
|
|
|
2020-09-30 15:34:26 +09:00
|
|
|
pub type ContextlessResult<T> = Result<Option<T>>;
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export(local_inner_macros)]
|
|
|
|
macro_rules! type_of {
|
|
|
|
($env:expr, $value:expr) => {{
|
|
|
|
let mut value_type = 0;
|
|
|
|
check_status!($crate::sys::napi_typeof($env, $value, &mut value_type))
|
|
|
|
.and_then(|_| Ok($crate::ValueType::from(value_type)))
|
|
|
|
}};
|
|
|
|
}
|
2020-11-12 12:41:41 +09:00
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
#[doc(hidden)]
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert_type_of {
|
|
|
|
($env: expr, $value:expr, $value_ty: expr) => {
|
|
|
|
$crate::type_of!($env, $value).and_then(|received_type| {
|
|
|
|
if received_type == $value_ty {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err($crate::Error::new(
|
|
|
|
$crate::Status::InvalidArg,
|
|
|
|
format!(
|
|
|
|
"Expect value to be {}, but received {}",
|
|
|
|
$value_ty, received_type
|
|
|
|
),
|
|
|
|
))
|
2020-11-25 18:42:14 +09:00
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
2020-11-25 18:42:14 +09:00
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
pub mod bindgen_prelude {
|
|
|
|
#[cfg(feature = "compat-mode")]
|
|
|
|
pub use crate::bindgen_runtime::register_module_exports;
|
2021-10-25 01:00:31 +09:00
|
|
|
pub use crate::tokio_runtime::*;
|
2021-09-23 02:29:09 +09:00
|
|
|
pub use crate::{
|
|
|
|
assert_type_of, bindgen_runtime::*, check_status, check_status_or_throw, error, error::*, sys,
|
|
|
|
type_of, JsError, Property, PropertyAttributes, Result, Status, Task, ValueType,
|
2018-04-28 17:26:38 +09:00
|
|
|
};
|
|
|
|
}
|