2020-05-12 14:59:20 +09:00
|
|
|
use std::mem;
|
|
|
|
use std::os::raw::{c_char, c_void};
|
|
|
|
use std::ptr;
|
|
|
|
|
2020-12-02 15:10:48 +09:00
|
|
|
use crate::{check_status, js_values::NapiValue, sys, Env, JsError, JsObject, Result, Task};
|
2020-05-12 14:59:20 +09:00
|
|
|
|
2020-09-26 19:12:52 +09:00
|
|
|
struct AsyncWork<T: Task> {
|
2020-05-12 14:59:20 +09:00
|
|
|
inner_task: T,
|
|
|
|
deferred: sys::napi_deferred,
|
2020-09-26 19:12:52 +09:00
|
|
|
value: Result<mem::MaybeUninit<T::Output>>,
|
|
|
|
napi_async_work: sys::napi_async_work,
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
|
2020-09-26 19:12:52 +09:00
|
|
|
pub struct AsyncWorkPromise<'env> {
|
|
|
|
napi_async_work: sys::napi_async_work,
|
|
|
|
raw_promise: sys::napi_value,
|
|
|
|
env: &'env Env,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'env> AsyncWorkPromise<'env> {
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-26 19:12:52 +09:00
|
|
|
pub fn promise_object(&self) -> JsObject {
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { JsObject::from_raw_unchecked(self.env.0, self.raw_promise) }
|
2020-09-26 19:12:52 +09:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-26 19:12:52 +09:00
|
|
|
pub fn cancel(self) -> Result<()> {
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_cancel_async_work(self.env.0, self.napi_async_work) })
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-12-01 15:55:19 +09:00
|
|
|
pub fn run<T: Task>(env: &Env, task: T) -> Result<AsyncWorkPromise<'_>> {
|
2020-09-26 19:12:52 +09:00
|
|
|
let mut raw_resource = ptr::null_mut();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_create_object(env.0, &mut raw_resource) })?;
|
2020-09-26 19:12:52 +09:00
|
|
|
let mut raw_promise = ptr::null_mut();
|
|
|
|
let mut deferred = ptr::null_mut();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_create_promise(env.0, &mut deferred, &mut raw_promise) })?;
|
2020-09-26 19:12:52 +09:00
|
|
|
let mut raw_name = ptr::null_mut();
|
|
|
|
let s = "napi_rs_async_work";
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-12-02 19:39:20 +09:00
|
|
|
sys::napi_create_string_utf8(env.0, s.as_ptr() as *const c_char, s.len(), &mut raw_name)
|
2020-09-26 19:12:52 +09:00
|
|
|
})?;
|
|
|
|
let result = Box::leak(Box::new(AsyncWork {
|
|
|
|
inner_task: task,
|
|
|
|
deferred,
|
|
|
|
value: Ok(mem::MaybeUninit::zeroed()),
|
|
|
|
napi_async_work: ptr::null_mut(),
|
|
|
|
}));
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-09-26 19:12:52 +09:00
|
|
|
sys::napi_create_async_work(
|
|
|
|
env.0,
|
|
|
|
raw_resource,
|
|
|
|
raw_name,
|
|
|
|
Some(execute::<T> as unsafe extern "C" fn(env: sys::napi_env, data: *mut c_void)),
|
|
|
|
Some(
|
|
|
|
complete::<T>
|
|
|
|
as unsafe extern "C" fn(env: sys::napi_env, status: sys::napi_status, data: *mut c_void),
|
|
|
|
),
|
|
|
|
result as *mut _ as *mut c_void,
|
|
|
|
&mut result.napi_async_work,
|
|
|
|
)
|
|
|
|
})?;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_queue_async_work(env.0, result.napi_async_work) })?;
|
2020-09-26 19:12:52 +09:00
|
|
|
Ok(AsyncWorkPromise {
|
|
|
|
napi_async_work: result.napi_async_work,
|
|
|
|
raw_promise,
|
|
|
|
env,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:59:20 +09:00
|
|
|
unsafe impl<T: Task> Send for AsyncWork<T> {}
|
|
|
|
|
|
|
|
unsafe impl<T: Task> Sync for AsyncWork<T> {}
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
/// env here is the same with the one in `CallContext`.
|
|
|
|
/// So it actually could do nothing here, because `execute` function is called in the other thread mostly.
|
2020-05-12 14:59:20 +09:00
|
|
|
unsafe extern "C" fn execute<T: Task>(_env: sys::napi_env, data: *mut c_void) {
|
|
|
|
let mut work = Box::from_raw(data as *mut AsyncWork<T>);
|
2020-09-26 19:12:52 +09:00
|
|
|
let _ = mem::replace(
|
|
|
|
&mut work.value,
|
2020-12-01 15:55:19 +09:00
|
|
|
work.inner_task.compute().map(mem::MaybeUninit::new),
|
2020-09-26 19:12:52 +09:00
|
|
|
);
|
2020-05-12 14:59:20 +09:00
|
|
|
Box::leak(work);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn complete<T: Task>(
|
|
|
|
env: sys::napi_env,
|
|
|
|
status: sys::napi_status,
|
|
|
|
data: *mut c_void,
|
|
|
|
) {
|
|
|
|
let mut work = Box::from_raw(data as *mut AsyncWork<T>);
|
2020-09-26 19:12:52 +09:00
|
|
|
let value_ptr = mem::replace(&mut work.value, Ok(mem::MaybeUninit::zeroed()));
|
2020-05-12 14:59:20 +09:00
|
|
|
let deferred = mem::replace(&mut work.deferred, ptr::null_mut());
|
2020-09-26 19:12:52 +09:00
|
|
|
let napi_async_work = mem::replace(&mut work.napi_async_work, ptr::null_mut());
|
2020-12-18 12:32:07 +09:00
|
|
|
let value = match value_ptr {
|
|
|
|
Ok(v) => {
|
|
|
|
let output = v.assume_init();
|
|
|
|
work.inner_task.resolve(Env::from_raw(env), output)
|
|
|
|
}
|
|
|
|
Err(e) => work.inner_task.reject(Env::from_raw(env), e),
|
|
|
|
};
|
2020-11-25 18:42:14 +09:00
|
|
|
match check_status!(status).and_then(move |_| value) {
|
2020-05-12 14:59:20 +09:00
|
|
|
Ok(v) => {
|
2020-09-28 01:27:37 +09:00
|
|
|
let status = sys::napi_resolve_deferred(env, deferred, v.raw());
|
2020-11-11 13:15:13 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2020-12-02 15:10:48 +09:00
|
|
|
let status = sys::napi_reject_deferred(env, deferred, JsError::from(e).into_value(env));
|
2020-11-11 13:15:13 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|
|
|
|
};
|
2020-09-26 19:12:52 +09:00
|
|
|
let delete_status = sys::napi_delete_async_work(env, napi_async_work);
|
|
|
|
debug_assert!(
|
2020-11-11 13:15:13 +09:00
|
|
|
delete_status == sys::Status::napi_ok,
|
2020-09-26 19:12:52 +09:00
|
|
|
"Delete async work failed"
|
|
|
|
);
|
2020-05-12 14:59:20 +09:00
|
|
|
}
|