2020-06-21 20:10:06 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use super::Value;
|
|
|
|
use crate::error::check_status;
|
|
|
|
use crate::{sys, Env, Error, JsObject, JsUnknown, NapiValue, Result, Status};
|
|
|
|
|
2020-09-28 01:27:37 +09:00
|
|
|
#[repr(transparent)]
|
2020-08-10 13:37:58 +09:00
|
|
|
#[derive(Debug)]
|
2020-06-21 20:10:06 +09:00
|
|
|
pub struct JsFunction(pub(crate) Value);
|
|
|
|
|
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> {
|
|
|
|
/// 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-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)
|
2020-06-21 20:10:06 +09:00
|
|
|
pub fn call(&self, this: Option<&JsObject>, args: &[JsUnknown]) -> Result<JsUnknown> {
|
|
|
|
let raw_this = this
|
2020-09-28 01:27:37 +09:00
|
|
|
.map(|v| v.raw())
|
2020-06-21 20:10:06 +09:00
|
|
|
.or_else(|| {
|
|
|
|
Env::from_raw(self.0.env)
|
|
|
|
.get_undefined()
|
|
|
|
.ok()
|
2020-09-28 01:27:37 +09:00
|
|
|
.map(|u| u.raw())
|
2020-06-21 20:10:06 +09:00
|
|
|
})
|
|
|
|
.ok_or(Error::new(
|
|
|
|
Status::Unknown,
|
|
|
|
"Get raw this failed".to_owned(),
|
|
|
|
))?;
|
2020-10-03 00:23:21 +09:00
|
|
|
let raw_args = args
|
|
|
|
.iter()
|
|
|
|
.map(|arg| arg.0.value)
|
|
|
|
.collect::<Vec<sys::napi_value>>();
|
2020-06-21 20:10:06 +09:00
|
|
|
let mut return_value = ptr::null_mut();
|
2020-09-02 18:05:53 +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,
|
|
|
|
args.len() as u64,
|
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
|
|
|
|
|
|
|
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
|
|
|
|
/// This method is used to instantiate a new `JavaScript` value using a given `JsFunction` that represents the constructor for the object.
|
|
|
|
pub fn new<V>(&self, args: &[V]) -> Result<JsObject>
|
|
|
|
where
|
|
|
|
V: NapiValue,
|
|
|
|
{
|
|
|
|
let mut js_instance = ptr::null_mut();
|
|
|
|
let length = args.len() as u64;
|
|
|
|
let raw_args = args
|
|
|
|
.iter()
|
|
|
|
.map(|arg| arg.raw())
|
|
|
|
.collect::<Vec<sys::napi_value>>();
|
|
|
|
check_status(unsafe {
|
|
|
|
sys::napi_new_instance(
|
|
|
|
self.0.env,
|
|
|
|
self.0.value,
|
|
|
|
length,
|
|
|
|
raw_args.as_ptr(),
|
|
|
|
&mut js_instance,
|
|
|
|
)
|
|
|
|
})?;
|
|
|
|
Ok(JsObject::from_raw_unchecked(self.0.env, js_instance))
|
|
|
|
}
|
2020-06-21 20:10:06 +09:00
|
|
|
}
|