-
Notifications
You must be signed in to change notification settings - Fork 287
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(neon): Add support for async functions in
#[neon::export]
- Loading branch information
1 parent
b61c62c
commit a518907
Showing
16 changed files
with
505 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
[alias] | ||
# Neon defines mutually exclusive feature flags which prevents using `cargo clippy --all-features` | ||
# The following aliases simplify linting the entire workspace | ||
neon-check = " check --all --all-targets --features napi-experimental,futures,external-buffers,serde" | ||
neon-clippy = "clippy --all --all-targets --features napi-experimental,futures,external-buffers,serde -- -A clippy::missing_safety_doc" | ||
neon-test = " test --all --features=doc-dependencies,doc-comment,napi-experimental,futures,external-buffers,serde" | ||
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,futures,external-buffers,sys,serde -- --cfg docsrs" | ||
neon-check = " check --all --all-targets --features napi-experimental,external-buffers,serde,tokio" | ||
neon-clippy = "clippy --all --all-targets --features napi-experimental,external-buffers,serde,tokio -- -A clippy::missing_safety_doc" | ||
neon-test = " test --all --features=doc-dependencies,doc-comment,napi-experimental,external-buffers,serde,tokio" | ||
neon-doc = " rustdoc -p neon --features=doc-dependencies,napi-experimental,external-buffers,sys,serde,tokio -- --cfg docsrs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::{future::Future, pin::Pin}; | ||
|
||
use crate::{context::Cx, thread::LocalKey}; | ||
|
||
#[cfg(feature = "tokio-rt")] | ||
pub(crate) mod tokio; | ||
|
||
type BoxFuture = Pin<Box<dyn Future<Output = ()> + Send + 'static>>; | ||
|
||
pub(crate) static RUNTIME: LocalKey<Box<dyn Runtime>> = LocalKey::new(); | ||
|
||
pub trait Runtime: Send + Sync + 'static { | ||
fn spawn(&self, fut: BoxFuture); | ||
} | ||
|
||
/// Register a [`Future`] executor runtime globally to the addon. | ||
/// | ||
/// Returns `Ok(())` if a global executor has not been set and `Err(runtime)` if it has. | ||
/// | ||
/// If the `tokio` feature flag is enabled and the addon does not provide a | ||
/// [`#[neon::main]`](crate::main) function, a multithreaded tokio runtime will be | ||
/// automatically registered. | ||
/// | ||
/// **Note**: Each instance of the addon will have its own runtime. It is recommended | ||
/// to initialize the async runtime once in a process global and share it across instances. | ||
/// | ||
/// ``` | ||
/// # #[cfg(feature = "tokio-rt-multi-thread")] | ||
/// # fn example() { | ||
/// # use neon::prelude::*; | ||
/// use once_cell::sync::OnceCell; | ||
/// use tokio::runtime::Runtime; | ||
/// | ||
/// static RUNTIME: OnceCell<Runtime> = OnceCell::new(); | ||
/// | ||
/// #[neon::main] | ||
/// fn main(mut cx: ModuleContext) -> NeonResult<()> { | ||
/// let runtime = RUNTIME | ||
/// .get_or_try_init(Runtime::new) | ||
/// .or_else(|err| cx.throw_error(err.to_string()))?; | ||
/// | ||
/// let _ = neon::set_global_executor(&mut cx, runtime); | ||
/// | ||
/// Ok(()) | ||
/// } | ||
/// # } | ||
/// ``` | ||
pub fn set_global_executor<R>(cx: &mut Cx, runtime: R) -> Result<(), R> | ||
where | ||
R: Runtime, | ||
{ | ||
if RUNTIME.get(cx).is_some() { | ||
return Err(runtime); | ||
} | ||
|
||
RUNTIME.get_or_init(cx, || Box::new(runtime)); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
use std::sync::Arc; | ||
|
||
use super::{BoxFuture, Runtime}; | ||
|
||
impl Runtime for tokio::runtime::Runtime { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for Arc<tokio::runtime::Runtime> { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for &'static tokio::runtime::Runtime { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self.handle(), fut); | ||
} | ||
} | ||
|
||
impl Runtime for tokio::runtime::Handle { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self, fut); | ||
} | ||
} | ||
|
||
impl Runtime for &'static tokio::runtime::Handle { | ||
fn spawn(&self, fut: BoxFuture) { | ||
spawn(self, fut); | ||
} | ||
} | ||
|
||
fn spawn(handle: &tokio::runtime::Handle, fut: BoxFuture) { | ||
#[allow(clippy::let_underscore_future)] | ||
let _ = handle.spawn(fut); | ||
} | ||
|
||
#[cfg(feature = "tokio-rt-multi-thread")] | ||
pub(crate) fn init(cx: &mut crate::context::ModuleContext) -> crate::result::NeonResult<()> { | ||
use once_cell::sync::OnceCell; | ||
use tokio::runtime::{Builder, Runtime}; | ||
|
||
use crate::context::Context; | ||
|
||
static RUNTIME: OnceCell<Runtime> = OnceCell::new(); | ||
|
||
super::RUNTIME.get_or_try_init(cx, |cx| { | ||
let runtime = RUNTIME | ||
.get_or_try_init(|| { | ||
#[cfg(feature = "tokio-rt-multi-thread")] | ||
let mut builder = Builder::new_multi_thread(); | ||
|
||
#[cfg(not(feature = "tokio-rt-multi-thread"))] | ||
let mut builder = Builder::new_current_thread(); | ||
|
||
builder.enable_all().build() | ||
}) | ||
.or_else(|err| cx.throw_error(err.to_string()))?; | ||
|
||
Ok(Box::new(runtime)) | ||
})?; | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use std::future::Future; | ||
|
||
use crate::{ | ||
context::{Context, Cx, TaskContext}, | ||
result::JsResult, | ||
types::JsValue, | ||
}; | ||
|
||
pub fn spawn<'cx, F, S>(cx: &mut Cx<'cx>, fut: F, settle: S) -> JsResult<'cx, JsValue> | ||
where | ||
F: Future + Send + 'static, | ||
F::Output: Send, | ||
S: FnOnce(TaskContext, F::Output) -> JsResult<JsValue> + Send + 'static, | ||
{ | ||
let rt = match crate::executor::RUNTIME.get(cx) { | ||
Some(rt) => rt, | ||
None => return cx.throw_error("Must initialize with neon::set_global_executor"), | ||
}; | ||
|
||
let ch = cx.channel(); | ||
let (d, promise) = cx.promise(); | ||
|
||
rt.spawn(Box::pin(async move { | ||
let res = fut.await; | ||
let _ = d.try_settle_with(&ch, move |cx| settle(cx, res)); | ||
})); | ||
|
||
Ok(promise.upcast()) | ||
} |
Oops, something went wrong.