feat(napi): re-export tokio and features
This commit is contained in:
parent
16f808276d
commit
13533d1a37
5 changed files with 43 additions and 15 deletions
|
@ -31,7 +31,19 @@ napi6 = ["napi5", "napi-sys/napi6"]
|
||||||
napi7 = ["napi6", "napi-sys/napi7"]
|
napi7 = ["napi6", "napi-sys/napi7"]
|
||||||
napi8 = ["napi7", "napi-sys/napi8"]
|
napi8 = ["napi7", "napi-sys/napi8"]
|
||||||
serde-json = ["serde", "serde_json"]
|
serde-json = ["serde", "serde_json"]
|
||||||
|
tokio_fs = ["tokio/fs"]
|
||||||
|
tokio_full = ["tokio/full"]
|
||||||
|
tokio_io_std = ["tokio/io-std"]
|
||||||
|
tokio_io_util = ["tokio/io-util"]
|
||||||
|
tokio_macros = ["tokio/macros"]
|
||||||
|
tokio_net = ["tokio/net"]
|
||||||
|
tokio_process = ["tokio/process"]
|
||||||
tokio_rt = ["tokio", "napi4"]
|
tokio_rt = ["tokio", "napi4"]
|
||||||
|
tokio_signal = ["tokio/signal"]
|
||||||
|
tokio_stats = ["tokio/stats"]
|
||||||
|
tokio_sync = ["tokio/sync"]
|
||||||
|
tokio_test_util = ["tokio/test-util"]
|
||||||
|
tokio_time = ["tokio/time"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
ctor = "0.1"
|
ctor = "0.1"
|
||||||
|
@ -56,4 +68,4 @@ optional = true
|
||||||
version = "1"
|
version = "1"
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
windows = {version = "0.29", features = ["Win32_System_WindowsProgramming", "Win32_System_LibraryLoader", "Win32_Foundation"]}
|
windows = {version = "0.30", features = ["Win32_System_WindowsProgramming", "Win32_System_LibraryLoader", "Win32_Foundation"]}
|
||||||
|
|
|
@ -161,6 +161,23 @@ pub fn register_class(
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
/// Get `JsFunction` from defined Rust `fn`
|
||||||
|
/// ```rust
|
||||||
|
/// #[napi]
|
||||||
|
/// fn some_fn() -> u32 {
|
||||||
|
/// 1
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// #[napi]
|
||||||
|
/// fn return_some_fn() -> Result<JsFunction> {
|
||||||
|
/// get_js_function(some_fn_js_function)
|
||||||
|
/// }
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// ```js
|
||||||
|
/// returnSomeFn()(); // 1
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
pub fn get_js_function(raw_fn: ExportRegisterCallback) -> Result<JsFunction> {
|
pub fn get_js_function(raw_fn: ExportRegisterCallback) -> Result<JsFunction> {
|
||||||
FN_REGISTER_MAP
|
FN_REGISTER_MAP
|
||||||
.borrow_mut()
|
.borrow_mut()
|
||||||
|
|
|
@ -23,12 +23,10 @@
|
||||||
//! use napi::{CallContext, Error, JsObject, JsString, Result, Status};
|
//! use napi::{CallContext, Error, JsObject, JsString, Result, Status};
|
||||||
//! use tokio;
|
//! use tokio;
|
||||||
//!
|
//!
|
||||||
//! #[js_function(1)]
|
//! #[napi]
|
||||||
//! pub fn tokio_readfile(ctx: CallContext) -> Result<JsObject> {
|
//! pub async fn tokio_readfile(js_filepath: String) -> Result<JsBuffer> {
|
||||||
//! let js_filepath = ctx.get::<JsString>(0)?;
|
|
||||||
//! let path_str = js_filepath.as_str()?;
|
|
||||||
//! ctx.env.execute_tokio_future(
|
//! ctx.env.execute_tokio_future(
|
||||||
//! tokio::fs::read(path_str.to_owned())
|
//! tokio::fs::read(js_filepath)
|
||||||
//! .map(|v| v.map_err(|e| Error::new(Status::Unknown, format!("failed to read file, {}", e)))),
|
//! .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),
|
//! |&mut env, data| env.create_buffer_with_data(data),
|
||||||
//! )
|
//! )
|
||||||
|
@ -61,17 +59,16 @@
|
||||||
//! c: String,
|
//! c: String,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! #[js_function(1)]
|
//! #[napi]
|
||||||
//! fn deserialize_from_js(ctx: CallContext) -> Result<JsUndefined> {
|
//! fn deserialize_from_js(arg0: JsUnknown) -> Result<JsUndefined> {
|
||||||
//! let arg0 = ctx.get::<JsUnknown>(0)?;
|
|
||||||
//! let de_serialized: AnObject = ctx.env.from_js_value(arg0)?;
|
//! let de_serialized: AnObject = ctx.env.from_js_value(arg0)?;
|
||||||
//! ...
|
//! ...
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! #[js_function]
|
//! #[napi]
|
||||||
//! fn serialize(ctx: CallContext) -> Result<JsUnknown> {
|
//! fn serialize(env: Env) -> Result<JsUnknown> {
|
||||||
//! let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" };
|
//! let value = AnyObject { a: 1, b: vec![0.1, 2.22], c: "hello" };
|
||||||
//! ctx.env.to_js_value(&value)
|
//! env.to_js_value(&value)
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
@ -201,3 +198,6 @@ pub mod bindgen_prelude {
|
||||||
type_of, JsError, Property, PropertyAttributes, Result, Status, Task, ValueType,
|
type_of, JsError, Property, PropertyAttributes, Result, Status, Task, ValueType,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "tokio_rt")]
|
||||||
|
pub extern crate tokio;
|
||||||
|
|
|
@ -10,12 +10,11 @@ crate-type = ["cdylib"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
napi = {path = "../../crates/napi", default-features = false, features = ["napi8", "tokio_rt", "serde-json", "async", "experimental", "latin1"]}
|
napi = {path = "../../crates/napi", default-features = false, features = ["tokio_fs", "napi8", "tokio_rt", "serde-json", "async", "experimental", "latin1"]}
|
||||||
napi-derive = {path = "../../crates/macro", features = ["type-def"]}
|
napi-derive = {path = "../../crates/macro", features = ["type-def"]}
|
||||||
serde = "1"
|
serde = "1"
|
||||||
serde_derive = "1"
|
serde_derive = "1"
|
||||||
serde_json = "1"
|
serde_json = "1"
|
||||||
tokio = {version = "1", features = ["default", "fs"]}
|
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
napi-build = {path = "../../crates/build"}
|
napi-build = {path = "../../crates/build"}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use futures::prelude::*;
|
use futures::prelude::*;
|
||||||
use napi::bindgen_prelude::*;
|
use napi::bindgen_prelude::*;
|
||||||
use tokio::fs;
|
use napi::tokio::{self, fs};
|
||||||
|
|
||||||
#[napi]
|
#[napi]
|
||||||
async fn read_file_async(path: String) -> Result<Buffer> {
|
async fn read_file_async(path: String) -> Result<Buffer> {
|
||||||
|
|
Loading…
Reference in a new issue