-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation for JsPromise and TaskBuilder
- Loading branch information
1 parent
cb884ee
commit 0803489
Showing
24 changed files
with
704 additions
and
54 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,9 +1,9 @@ | ||
[alias] | ||
# Neon defines mutually exclusive feature flags which prevents using `cargo clippy --all-features` | ||
# The following aliases simplify linting the entire workspace | ||
check-napi = "check --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p electron-tests -p napi-tests --features proc-macros,try-catch-api,napi-experimental" | ||
check-napi = "check --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p electron-tests -p napi-tests --features proc-macros,try-catch-api,napi-experimental,promise-api,task-api" | ||
check-legacy = "check --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p tests -p static_tests --features event-handler-api,proc-macros,try-catch-api,legacy-runtime" | ||
clippy-legacy = "clippy --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p tests -p static_tests --features event-handler-api,proc-macros,try-catch-api,legacy-runtime -- -A clippy::missing_safety_doc" | ||
clippy-napi = "clippy --all-targets --no-default-features -p neon -p neon-runtime -p neon-build -p neon-macros -p electron-tests -p napi-tests --features proc-macros,try-catch-api,napi-experimental -- -A clippy::missing_safety_doc" | ||
neon-test = "test --no-default-features --features napi-experimental" | ||
neon-doc = "rustdoc --no-default-features --features=channel-api,napi-experimental,proc-macros,try-catch-api -- --cfg docsrs" | ||
neon-doc = "rustdoc --no-default-features --features=channel-api,napi-experimental,proc-macros,try-catch-api,promise-api,task-api -- --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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
use std::ffi::c_void; | ||
use std::mem; | ||
use std::ptr; | ||
|
||
use crate::napi::bindings as napi; | ||
use crate::raw::Env; | ||
|
||
type Execute<T, O> = fn(input: T) -> O; | ||
type Complete<O> = fn(env: Env, output: O); | ||
|
||
/// Schedule work to execute on the libuv thread pool | ||
/// | ||
/// # Safety | ||
/// * `env` must be a valid `napi_env` for the current thread | ||
pub unsafe fn schedule<T, O>(env: Env, input: T, execute: Execute<T, O>, complete: Complete<O>) | ||
where | ||
T: Send + 'static, | ||
O: Send + 'static, | ||
{ | ||
let mut data = Box::new(Data { | ||
state: State::Input(input), | ||
execute, | ||
complete, | ||
work: ptr::null_mut(), | ||
}); | ||
|
||
let work = &mut data.work as *mut _; | ||
|
||
assert_eq!( | ||
napi::create_async_work( | ||
env, | ||
ptr::null_mut(), | ||
super::string(env, "neon_async_work"), | ||
Some(call_execute::<T, O>), | ||
Some(call_complete::<T, O>), | ||
Box::into_raw(data).cast(), | ||
work, | ||
), | ||
napi::Status::Ok, | ||
); | ||
|
||
match napi::queue_async_work(env, *work) { | ||
napi::Status::Ok => {} | ||
status => { | ||
napi::delete_async_work(env, *work); | ||
assert_eq!(status, napi::Status::Ok); | ||
} | ||
} | ||
} | ||
|
||
struct Data<T, O> { | ||
state: State<T, O>, | ||
execute: Execute<T, O>, | ||
complete: Complete<O>, | ||
work: napi::AsyncWork, | ||
} | ||
|
||
enum State<T, O> { | ||
Input(T), | ||
Output(O), | ||
Empty, | ||
} | ||
|
||
impl<T, O> State<T, O> { | ||
fn take_input(&mut self) -> Option<T> { | ||
match mem::replace(self, Self::Empty) { | ||
Self::Input(input) => Some(input), | ||
_ => None, | ||
} | ||
} | ||
|
||
fn take_output(&mut self) -> Option<O> { | ||
match mem::replace(self, Self::Empty) { | ||
Self::Output(output) => Some(output), | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
unsafe extern "C" fn call_execute<T, O>(_: Env, data: *mut c_void) { | ||
let data = &mut *data.cast::<Data<T, O>>(); | ||
let input = data.state.take_input().unwrap(); | ||
let output = (data.execute)(input); | ||
|
||
data.state = State::Output(output); | ||
} | ||
|
||
unsafe extern "C" fn call_complete<T, O>(env: Env, status: napi::Status, data: *mut c_void) { | ||
let Data { | ||
mut state, | ||
complete, | ||
work, | ||
.. | ||
} = *Box::<Data<T, O>>::from_raw(data.cast()); | ||
|
||
napi::delete_async_work(env, work); | ||
|
||
match status { | ||
napi::Status::Ok => complete(env, state.take_output().unwrap()), | ||
napi::Status::Cancelled => {} | ||
_ => assert_eq!(status, napi::Status::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
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,43 @@ | ||
use std::mem::MaybeUninit; | ||
use std::ptr; | ||
|
||
use crate::napi::bindings as napi; | ||
use crate::raw::Env; | ||
|
||
pub unsafe fn create(env: Env) -> (napi::Deferred, napi::Value) { | ||
let mut deferred = MaybeUninit::uninit(); | ||
let mut promise = MaybeUninit::uninit(); | ||
|
||
assert_eq!( | ||
napi::create_promise(env, deferred.as_mut_ptr(), promise.as_mut_ptr()), | ||
napi::Status::Ok, | ||
); | ||
|
||
(deferred.assume_init(), promise.assume_init()) | ||
} | ||
|
||
pub unsafe fn resolve(env: Env, deferred: napi::Deferred, resolution: napi::Value) { | ||
assert_eq!( | ||
napi::resolve_deferred(env, deferred, resolution), | ||
napi::Status::Ok, | ||
); | ||
} | ||
|
||
pub unsafe fn reject(env: Env, deferred: napi::Deferred, rejection: napi::Value) { | ||
assert_eq!( | ||
napi::reject_deferred(env, deferred, rejection), | ||
napi::Status::Ok, | ||
); | ||
} | ||
|
||
pub unsafe fn reject_err_message(env: Env, deferred: napi::Deferred, msg: impl AsRef<str>) { | ||
let msg = super::string(env, msg); | ||
let mut err = MaybeUninit::uninit(); | ||
|
||
assert_eq!( | ||
napi::create_error(env, ptr::null_mut(), msg, err.as_mut_ptr()), | ||
napi::Status::Ok, | ||
); | ||
|
||
reject(env, deferred, err.assume_init()); | ||
} |
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
Oops, something went wrong.