napi-rs/crates/napi/src/promise.rs

137 lines
4.1 KiB
Rust
Raw Normal View History

use std::ffi::CStr;
use std::future::Future;
use std::marker::PhantomData;
use std::os::raw::c_void;
use std::ptr;
2021-10-20 01:02:47 +09:00
use crate::{check_status, sys, JsError, Result};
2021-10-20 01:02:47 +09:00
pub struct FuturePromise<Data, Resolver: FnOnce(sys::napi_env, Data) -> Result<sys::napi_value>> {
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>,
}
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>
{
}
2021-10-20 01:02:47 +09:00
impl<Data, Resolver: FnOnce(sys::napi_env, Data) -> Result<sys::napi_value>>
FuturePromise<Data, Resolver>
{
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();
let s = unsafe { CStr::from_bytes_with_nul_unchecked(b"napi_resolve_promise_from_future\0") };
check_status!(unsafe {
sys::napi_create_string_utf8(env, s.as_ptr(), 32, &mut async_resource_name)
2020-07-08 01:59:09 +09:00
})?;
Ok(FuturePromise {
deferred,
2020-07-08 01:59:09 +09:00
resolver,
env,
2020-07-08 01:59:09 +09:00
tsfn: ptr::null_mut(),
async_resource_name,
_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));
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,
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;
check_status!(unsafe {
sys::napi_call_threadsafe_function(
2020-07-08 01:59:09 +09:00
tsfn_value.0,
Box::into_raw(Box::from(val)) as *mut c_void,
2021-11-29 13:52:42 +09:00
sys::ThreadsafeFunctionCallMode::nonblocking,
)
})
2020-07-08 01:59:09 +09:00
.expect("Failed to call thread safe function");
check_status!(unsafe {
2021-11-29 13:52:42 +09:00
sys::napi_release_threadsafe_function(tsfn_value.0, sys::ThreadsafeFunctionReleaseMode::release)
})
.expect("Failed to release thread safe function");
}
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,
_js_callback: sys::napi_value,
context: *mut c_void,
data: *mut c_void,
) {
2022-01-13 12:15:02 +09:00
let future_promise = unsafe { Box::from_raw(context as *mut FuturePromise<Data, Resolver>) };
let value = unsafe { Box::from_raw(data as *mut Result<Data>) };
2020-07-08 01:59:09 +09:00
let resolver = future_promise.resolver;
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));
match js_value_to_resolve {
Ok(v) => {
2022-01-13 12:15:02 +09:00
let status = unsafe { sys::napi_resolve_deferred(env, deferred, v) };
debug_assert!(status == sys::Status::napi_ok, "Resolve promise failed");
}
Err(e) => {
2022-01-13 12:15:02 +09:00
let status = unsafe {
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
},
)
};
debug_assert!(status == sys::Status::napi_ok, "Reject promise failed");
}
};
}