2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use super::Value;
|
2021-11-02 21:36:34 +09:00
|
|
|
use crate::bindgen_runtime::TypeName;
|
2021-11-12 18:22:57 +09:00
|
|
|
#[cfg(feature = "napi4")]
|
|
|
|
use crate::threadsafe_function::{ThreadSafeCallContext, ThreadsafeFunction};
|
2021-11-02 21:36:34 +09:00
|
|
|
use crate::{check_status, ValueType};
|
2021-04-01 19:50:38 +09:00
|
|
|
use crate::{sys, Env, Error, JsObject, JsUnknown, NapiRaw, NapiValue, Result, Status};
|
2020-06-21 20:10:06 +09:00
|
|
|
|
|
|
|
pub struct JsFunction(pub(crate) Value);
|
|
|
|
|
2021-11-02 21:36:34 +09:00
|
|
|
impl TypeName for JsFunction {
|
|
|
|
fn type_name() -> &'static str {
|
|
|
|
"Function"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn value_type() -> crate::ValueType {
|
|
|
|
ValueType::Function
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-03 01:36:45 +09:00
|
|
|
/// See [Working with JavaScript Functions](https://nodejs.org/api/n-api.html#n_api_working_with_javascript_functions).
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
/// ```
|
|
|
|
/// use napi::{JsFunction, CallContext, JsNull, Result};
|
|
|
|
///
|
|
|
|
/// #[js_function(1)]
|
|
|
|
/// pub fn call_function(ctx: CallContext) -> Result<JsNull> {
|
2020-12-28 00:45:54 +09:00
|
|
|
/// let js_func = ctx.get::<JsFunction>(0)?;
|
|
|
|
/// let js_string = ctx.env.create_string("hello".as_ref())?.into_unknown()?;
|
|
|
|
/// js_func.call(None, &[js_string])?;
|
|
|
|
/// Ok(ctx.env.get_null()?)
|
2020-07-03 01:36:45 +09:00
|
|
|
/// }
|
|
|
|
/// ```
|
2020-06-21 20:10:06 +09:00
|
|
|
impl JsFunction {
|
2020-07-03 01:36:45 +09:00
|
|
|
/// [napi_call_function](https://nodejs.org/api/n-api.html#n_api_napi_call_function)
|
2021-04-01 19:50:38 +09:00
|
|
|
pub fn call<V>(&self, this: Option<&JsObject>, args: &[V]) -> Result<JsUnknown>
|
|
|
|
where
|
|
|
|
V: NapiRaw,
|
|
|
|
{
|
2020-06-21 20:10:06 +09:00
|
|
|
let raw_this = this
|
2020-11-20 01:07:13 +09:00
|
|
|
.map(|v| unsafe { v.raw() })
|
2020-06-21 20:10:06 +09:00
|
|
|
.or_else(|| {
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { Env::from_raw(self.0.env) }
|
2020-06-21 20:10:06 +09:00
|
|
|
.get_undefined()
|
|
|
|
.ok()
|
2020-11-20 01:07:13 +09:00
|
|
|
.map(|u| unsafe { u.raw() })
|
2020-06-21 20:10:06 +09:00
|
|
|
})
|
2020-12-01 15:55:19 +09:00
|
|
|
.ok_or_else(|| Error::new(Status::GenericFailure, "Get raw this failed".to_owned()))?;
|
2020-10-03 00:23:21 +09:00
|
|
|
let raw_args = args
|
|
|
|
.iter()
|
2021-04-01 19:50:38 +09:00
|
|
|
.map(|arg| unsafe { arg.raw() })
|
2020-10-03 00:23:21 +09:00
|
|
|
.collect::<Vec<sys::napi_value>>();
|
2020-06-21 20:10:06 +09:00
|
|
|
let mut return_value = ptr::null_mut();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-06-21 20:10:06 +09:00
|
|
|
sys::napi_call_function(
|
|
|
|
self.0.env,
|
|
|
|
raw_this,
|
|
|
|
self.0.value,
|
2020-12-02 19:39:20 +09:00
|
|
|
args.len(),
|
2020-10-03 00:23:21 +09:00
|
|
|
raw_args.as_ptr(),
|
2020-06-21 20:10:06 +09:00
|
|
|
&mut return_value,
|
|
|
|
)
|
2020-09-02 18:05:53 +09:00
|
|
|
})?;
|
2020-06-21 20:10:06 +09:00
|
|
|
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { JsUnknown::from_raw(self.0.env, return_value) }
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|
2020-10-27 15:19:40 +09:00
|
|
|
|
2021-04-01 19:50:38 +09:00
|
|
|
/// [napi_call_function](https://nodejs.org/api/n-api.html#n_api_napi_call_function)
|
|
|
|
/// The same with `call`, but without arguments
|
|
|
|
pub fn call_without_args(&self, this: Option<&JsObject>) -> Result<JsUnknown> {
|
|
|
|
let raw_this = this
|
|
|
|
.map(|v| unsafe { v.raw() })
|
|
|
|
.or_else(|| {
|
|
|
|
unsafe { Env::from_raw(self.0.env) }
|
|
|
|
.get_undefined()
|
|
|
|
.ok()
|
|
|
|
.map(|u| unsafe { u.raw() })
|
|
|
|
})
|
|
|
|
.ok_or_else(|| Error::new(Status::GenericFailure, "Get raw this failed".to_owned()))?;
|
|
|
|
let mut return_value = ptr::null_mut();
|
|
|
|
check_status!(unsafe {
|
|
|
|
sys::napi_call_function(
|
|
|
|
self.0.env,
|
|
|
|
raw_this,
|
|
|
|
self.0.value,
|
|
|
|
0,
|
|
|
|
ptr::null_mut(),
|
|
|
|
&mut return_value,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
unsafe { JsUnknown::from_raw(self.0.env, return_value) }
|
|
|
|
}
|
|
|
|
|
2020-10-27 15:19:40 +09:00
|
|
|
/// https://nodejs.org/api/n-api.html#n_api_napi_new_instance
|
2020-12-28 00:45:54 +09:00
|
|
|
///
|
2020-10-27 15:19:40 +09:00
|
|
|
/// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object.
|
2021-11-12 18:22:57 +09:00
|
|
|
pub fn new_instance<V>(&self, args: &[V]) -> Result<JsObject>
|
2020-10-27 15:19:40 +09:00
|
|
|
where
|
2021-05-14 00:36:39 +09:00
|
|
|
V: NapiRaw,
|
2020-10-27 15:19:40 +09:00
|
|
|
{
|
|
|
|
let mut js_instance = ptr::null_mut();
|
2020-11-04 23:52:13 +09:00
|
|
|
let length = args.len();
|
2020-10-27 15:19:40 +09:00
|
|
|
let raw_args = args
|
|
|
|
.iter()
|
2020-11-20 01:07:13 +09:00
|
|
|
.map(|arg| unsafe { arg.raw() })
|
2020-10-27 15:19:40 +09:00
|
|
|
.collect::<Vec<sys::napi_value>>();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe {
|
2020-10-27 15:19:40 +09:00
|
|
|
sys::napi_new_instance(
|
|
|
|
self.0.env,
|
|
|
|
self.0.value,
|
2020-12-02 19:39:20 +09:00
|
|
|
length,
|
2020-10-27 15:19:40 +09:00
|
|
|
raw_args.as_ptr(),
|
|
|
|
&mut js_instance,
|
|
|
|
)
|
|
|
|
})?;
|
2020-11-20 01:07:13 +09:00
|
|
|
Ok(unsafe { JsObject::from_raw_unchecked(self.0.env, js_instance) })
|
2020-10-27 15:19:40 +09:00
|
|
|
}
|
2021-11-12 18:22:57 +09:00
|
|
|
|
|
|
|
#[cfg(feature = "napi4")]
|
|
|
|
pub fn create_threadsafe_function<T, V, F>(
|
|
|
|
&self,
|
|
|
|
max_queue_size: usize,
|
|
|
|
callback: F,
|
|
|
|
) -> Result<ThreadsafeFunction<T>>
|
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
V: NapiRaw,
|
|
|
|
F: 'static + Send + FnMut(ThreadSafeCallContext<T>) -> Result<Vec<V>>,
|
|
|
|
{
|
|
|
|
ThreadsafeFunction::create(self.0.env, self.0.value, max_queue_size, callback)
|
|
|
|
}
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|