2021-11-13 21:51:14 +09:00
|
|
|
use std::ffi::CString;
|
2021-06-03 00:03:47 +09:00
|
|
|
use std::future::Future;
|
2021-06-22 00:09:13 +09:00
|
|
|
use std::marker::PhantomData;
|
2021-11-13 21:51:14 +09:00
|
|
|
use std::os::raw::c_void;
|
2020-07-03 01:31:50 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
use crate::{check_status, sys, JsError, Result};
|
2020-07-03 01:31:50 +09:00
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
pub struct FuturePromise<Data, Resolver: FnOnce(sys::napi_env, Data) -> Result<sys::napi_value>> {
|
2020-07-03 01:31:50 +09:00
|
|
|
deferred: sys::napi_deferred,
|
2020-07-08 01:59:09 +09:00
|
|
|
env: sys::napi_env,
|
|
|
|
tsfn: sys::napi_threadsafe_function,
|
|
|
|
async_resource_name: sys::napi_value,
|
2021-10-20 01:02:47 +09:00
|
|
|
resolver: Resolver,
|
|
|
|
_data: PhantomData<Data>,
|
2020-07-03 01:31:50 +09:00
|
|
|
}
|
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
unsafe impl<T, F: FnOnce(sys::napi_env, T) -> Result<sys::napi_value>> Send
|
|
|
|
for FuturePromise<T, F>
|
|
|
|
{
|
|
|
|
}
|
2020-07-03 01:31:50 +09:00
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
impl<Data, Resolver: FnOnce(sys::napi_env, Data) -> Result<sys::napi_value>>
|
|
|
|
FuturePromise<Data, Resolver>
|
|
|
|
{
|
2021-11-13 21:51:14 +09:00
|
|
|
pub fn new(env: sys::napi_env, deferred: sys::napi_deferred, resolver: Resolver) -> Result<Self> {
|
2020-07-08 01:59:09 +09:00
|
|
|
let mut async_resource_name = ptr::null_mut();
|
2021-11-13 21:51:14 +09:00
|
|
|
let s = CString::new("napi_resolve_promise_from_future")?;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2021-11-13 21:51:14 +09:00
|
|
|
sys::napi_create_string_utf8(env, s.as_ptr(), 32, &mut async_resource_name)
|
2020-07-08 01:59:09 +09:00
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(FuturePromise {
|
2021-11-13 21:51:14 +09:00
|
|
|
deferred,
|
2020-07-08 01:59:09 +09:00
|
|
|
resolver,
|
2020-07-03 01:31:50 +09:00
|
|
|
env,
|
2020-07-08 01:59:09 +09:00
|
|
|
tsfn: ptr::null_mut(),
|
2020-07-03 01:31:50 +09:00
|
|
|
async_resource_name,
|
2021-06-22 00:09:13 +09:00
|
|
|
_data: PhantomData,
|
2020-07-08 01:59:09 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) fn start(self) -> Result<TSFNValue> {
|
|
|
|
let mut tsfn_value = ptr::null_mut();
|
|
|
|
let async_resource_name = self.async_resource_name;
|
|
|
|
let env = self.env;
|
|
|
|
let self_ref = Box::leak(Box::from(self));
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-07-08 01:59:09 +09:00
|
|
|
sys::napi_create_threadsafe_function(
|
|
|
|
env,
|
|
|
|
ptr::null_mut(),
|
|
|
|
ptr::null_mut(),
|
|
|
|
async_resource_name,
|
|
|
|
0,
|
2021-01-04 15:56:32 +09:00
|
|
|
1,
|
2020-07-08 01:59:09 +09:00
|
|
|
ptr::null_mut(),
|
|
|
|
None,
|
2021-10-20 01:02:47 +09:00
|
|
|
self_ref as *mut FuturePromise<Data, Resolver> as *mut c_void,
|
|
|
|
Some(call_js_cb::<Data, Resolver>),
|
2020-07-08 01:59:09 +09:00
|
|
|
&mut tsfn_value,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
self_ref.tsfn = tsfn_value;
|
|
|
|
Ok(TSFNValue(tsfn_value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub(crate) struct TSFNValue(sys::napi_threadsafe_function);
|
|
|
|
|
|
|
|
unsafe impl Send for TSFNValue {}
|
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
pub(crate) async fn resolve_from_future<Data: Send, Fut: Future<Output = Result<Data>>>(
|
2020-07-08 01:59:09 +09:00
|
|
|
tsfn_value: TSFNValue,
|
2021-10-20 01:02:47 +09:00
|
|
|
fut: Fut,
|
2020-07-08 01:59:09 +09:00
|
|
|
) {
|
|
|
|
let val = fut.await;
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-07-03 01:31:50 +09:00
|
|
|
sys::napi_call_threadsafe_function(
|
2020-07-08 01:59:09 +09:00
|
|
|
tsfn_value.0,
|
2021-11-13 21:51:14 +09:00
|
|
|
Box::into_raw(Box::from(val)) as *mut c_void,
|
2020-07-03 01:31:50 +09:00
|
|
|
sys::napi_threadsafe_function_call_mode::napi_tsfn_nonblocking,
|
|
|
|
)
|
|
|
|
})
|
2020-07-08 01:59:09 +09:00
|
|
|
.expect("Failed to call thread safe function");
|
2021-01-04 15:56:32 +09:00
|
|
|
check_status!(unsafe {
|
|
|
|
sys::napi_release_threadsafe_function(
|
|
|
|
tsfn_value.0,
|
|
|
|
sys::napi_threadsafe_function_release_mode::napi_tsfn_release,
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.expect("Failed to release thread safe function");
|
2020-07-03 01:31:50 +09:00
|
|
|
}
|
|
|
|
|
2021-10-20 01:02:47 +09:00
|
|
|
unsafe extern "C" fn call_js_cb<
|
|
|
|
Data,
|
|
|
|
Resolver: FnOnce(sys::napi_env, Data) -> Result<sys::napi_value>,
|
|
|
|
>(
|
|
|
|
env: sys::napi_env,
|
2020-07-03 01:31:50 +09:00
|
|
|
_js_callback: sys::napi_value,
|
|
|
|
context: *mut c_void,
|
|
|
|
data: *mut c_void,
|
|
|
|
) {
|
2021-10-20 01:02:47 +09:00
|
|
|
let future_promise = Box::from_raw(context as *mut FuturePromise<Data, Resolver>);
|
|
|
|
let value = Box::from_raw(data as *mut Result<Data>);
|
2020-07-08 01:59:09 +09:00
|
|
|
let resolver = future_promise.resolver;
|
2020-07-03 01:31:50 +09:00
|
|
|
let deferred = future_promise.deferred;
|
2021-10-20 01:02:47 +09:00
|
|
|
let js_value_to_resolve = value.and_then(move |v| (resolver)(env, v));
|
2020-07-03 01:31:50 +09:00
|
|
|
match js_value_to_resolve {
|
|
|
|
Ok(v) => {
|
2021-10-20 01:02:47 +09:00
|
|
|
let status = sys::napi_resolve_deferred(env, deferred, v);
|
2020-11-11 13:15:13 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Resolve promise failed");
|
2020-07-03 01:31:50 +09:00
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-11-13 21:51:14 +09:00
|
|
|
let status = sys::napi_reject_deferred(
|
|
|
|
env,
|
|
|
|
deferred,
|
|
|
|
if e.maybe_raw.is_null() {
|
|
|
|
JsError::from(e).into_value(env)
|
|
|
|
} else {
|
|
|
|
let mut err = ptr::null_mut();
|
|
|
|
let get_err_status = sys::napi_get_reference_value(env, e.maybe_raw, &mut err);
|
|
|
|
debug_assert!(
|
|
|
|
get_err_status == sys::Status::napi_ok,
|
|
|
|
"Get Error from Reference failed"
|
|
|
|
);
|
|
|
|
let delete_reference_status = sys::napi_delete_reference(env, e.maybe_raw);
|
|
|
|
debug_assert!(
|
|
|
|
delete_reference_status == sys::Status::napi_ok,
|
|
|
|
"Delete Error Reference failed"
|
|
|
|
);
|
|
|
|
err
|
|
|
|
},
|
|
|
|
);
|
2020-11-11 13:15:13 +09:00
|
|
|
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
|
2020-07-03 01:31:50 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|