2021-06-03 00:03:47 +09:00
|
|
|
use std::future::Future;
|
2020-07-03 01:31:50 +09:00
|
|
|
use std::os::raw::{c_char, c_void};
|
|
|
|
use std::ptr;
|
|
|
|
|
2021-05-17 18:29:38 +09:00
|
|
|
use crate::{check_status, sys, Env, JsError, NapiRaw, Result};
|
2020-07-03 01:31:50 +09:00
|
|
|
|
2021-05-17 18:29:38 +09:00
|
|
|
pub struct FuturePromise<T, V: NapiRaw> {
|
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,
|
2020-07-03 01:31:50 +09:00
|
|
|
resolver: Box<dyn FnOnce(&mut Env, T) -> Result<V>>,
|
|
|
|
}
|
|
|
|
|
2021-05-17 18:29:38 +09:00
|
|
|
unsafe impl<T, V: NapiRaw> Send for FuturePromise<T, V> {}
|
2020-07-03 01:31:50 +09:00
|
|
|
|
2021-05-17 18:29:38 +09:00
|
|
|
impl<T, V: NapiRaw> FuturePromise<T, V> {
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-07-08 01:59:09 +09:00
|
|
|
pub fn create(
|
|
|
|
env: sys::napi_env,
|
|
|
|
raw_deferred: sys::napi_deferred,
|
|
|
|
resolver: Box<dyn FnOnce(&mut Env, T) -> Result<V>>,
|
|
|
|
) -> Result<Self> {
|
|
|
|
let mut async_resource_name = ptr::null_mut();
|
|
|
|
let s = "napi_resolve_promise_from_future";
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-07-08 01:59:09 +09:00
|
|
|
sys::napi_create_string_utf8(
|
|
|
|
env,
|
|
|
|
s.as_ptr() as *const c_char,
|
2020-12-02 19:39:20 +09:00
|
|
|
s.len(),
|
2020-07-08 01:59:09 +09:00
|
|
|
&mut async_resource_name,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
Ok(FuturePromise {
|
|
|
|
deferred: raw_deferred,
|
|
|
|
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,
|
2020-07-08 01:59:09 +09:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
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-05-27 21:18:02 +09:00
|
|
|
self_ref as *mut FuturePromise<T, V> as *mut c_void,
|
2020-07-08 01:59:09 +09:00
|
|
|
Some(call_js_cb::<T, V>),
|
|
|
|
&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-01-04 15:56:32 +09:00
|
|
|
#[inline(always)]
|
2020-07-08 01:59:09 +09:00
|
|
|
pub(crate) async fn resolve_from_future<T: Send, F: Future<Output = Result<T>>>(
|
|
|
|
tsfn_value: TSFNValue,
|
|
|
|
fut: F,
|
|
|
|
) {
|
|
|
|
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-05-27 21:18:02 +09:00
|
|
|
Box::into_raw(Box::from(val)) as *mut T 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-05-17 18:29:38 +09:00
|
|
|
unsafe extern "C" fn call_js_cb<T, V: NapiRaw>(
|
2020-07-03 01:31:50 +09:00
|
|
|
raw_env: sys::napi_env,
|
|
|
|
_js_callback: sys::napi_value,
|
|
|
|
context: *mut c_void,
|
|
|
|
data: *mut c_void,
|
|
|
|
) {
|
|
|
|
let mut env = Env::from_raw(raw_env);
|
|
|
|
let future_promise = Box::from_raw(context as *mut FuturePromise<T, V>);
|
2021-05-27 21:18:02 +09:00
|
|
|
let value = Box::from_raw(data as *mut Result<T>);
|
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;
|
2020-07-08 01:59:09 +09:00
|
|
|
let js_value_to_resolve = value.and_then(move |v| (resolver)(&mut env, v));
|
2020-07-03 01:31:50 +09:00
|
|
|
match js_value_to_resolve {
|
|
|
|
Ok(v) => {
|
2020-09-28 01:27:37 +09:00
|
|
|
let status = sys::napi_resolve_deferred(raw_env, deferred, v.raw());
|
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) => {
|
2020-12-02 15:10:48 +09:00
|
|
|
let status =
|
|
|
|
sys::napi_reject_deferred(raw_env, deferred, JsError::from(e).into_value(raw_env));
|
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
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|