2020-09-02 18:05:53 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
use crate::check_status;
|
2020-09-26 18:49:30 +09:00
|
|
|
use crate::{sys, Either, Env, Error, JsUndefined, NapiValue, Result, Status};
|
2020-04-21 01:20:35 +09:00
|
|
|
|
2020-10-31 23:51:40 +09:00
|
|
|
/// Function call context
|
2020-09-26 18:49:30 +09:00
|
|
|
pub struct CallContext<'env> {
|
2020-10-04 17:02:04 +09:00
|
|
|
pub env: &'env mut Env,
|
2020-09-26 18:49:30 +09:00
|
|
|
raw_this: sys::napi_value,
|
2020-09-02 18:05:53 +09:00
|
|
|
callback_info: sys::napi_callback_info,
|
2020-06-21 18:50:38 +09:00
|
|
|
args: &'env [sys::napi_value],
|
2020-04-21 01:20:35 +09:00
|
|
|
arg_len: usize,
|
2020-10-31 23:51:40 +09:00
|
|
|
/// arguments.length
|
|
|
|
pub length: usize,
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
2020-09-26 18:49:30 +09:00
|
|
|
impl<'env> CallContext<'env> {
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-04-26 19:46:56 +09:00
|
|
|
pub fn new(
|
2020-10-04 17:02:04 +09:00
|
|
|
env: &'env mut Env,
|
2020-09-02 18:05:53 +09:00
|
|
|
callback_info: sys::napi_callback_info,
|
2020-09-26 18:49:30 +09:00
|
|
|
raw_this: sys::napi_value,
|
2020-06-21 18:50:38 +09:00
|
|
|
args: &'env [sys::napi_value],
|
2020-04-26 19:46:56 +09:00
|
|
|
arg_len: usize,
|
2020-10-31 23:51:40 +09:00
|
|
|
length: usize,
|
2020-09-26 18:49:30 +09:00
|
|
|
) -> Self {
|
|
|
|
Self {
|
2020-04-26 19:46:56 +09:00
|
|
|
env,
|
2020-09-02 18:05:53 +09:00
|
|
|
callback_info,
|
2020-09-26 18:49:30 +09:00
|
|
|
raw_this,
|
2020-04-26 19:46:56 +09:00
|
|
|
args,
|
|
|
|
arg_len,
|
2020-10-31 23:51:40 +09:00
|
|
|
length,
|
2020-09-26 18:49:30 +09:00
|
|
|
}
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-06-21 20:10:06 +09:00
|
|
|
pub fn get<ArgType: NapiValue>(&self, index: usize) -> Result<ArgType> {
|
2020-04-21 01:20:35 +09:00
|
|
|
if index + 1 > self.arg_len {
|
2020-05-06 00:13:23 +09:00
|
|
|
Err(Error {
|
|
|
|
status: Status::GenericFailure,
|
2020-06-21 20:10:06 +09:00
|
|
|
reason: "Arguments index out of range".to_owned(),
|
2020-05-06 00:13:23 +09:00
|
|
|
})
|
2020-04-21 01:20:35 +09:00
|
|
|
} else {
|
2020-11-20 01:07:13 +09:00
|
|
|
Ok(unsafe { ArgType::from_raw_unchecked(self.env.0, self.args[index]) })
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
}
|
2020-07-18 03:00:48 +09:00
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-07-18 03:00:48 +09:00
|
|
|
pub fn try_get<ArgType: NapiValue>(&self, index: usize) -> Result<Either<ArgType, JsUndefined>> {
|
|
|
|
if index + 1 > self.arg_len {
|
|
|
|
Err(Error {
|
|
|
|
status: Status::GenericFailure,
|
|
|
|
reason: "Arguments index out of range".to_owned(),
|
|
|
|
})
|
2020-12-01 15:55:19 +09:00
|
|
|
} else if index < self.length {
|
|
|
|
unsafe { ArgType::from_raw(self.env.0, self.args[index]) }.map(Either::A)
|
2020-07-18 03:00:48 +09:00
|
|
|
} else {
|
2020-12-01 15:55:19 +09:00
|
|
|
self.env.get_undefined().map(Either::B)
|
2020-07-18 03:00:48 +09:00
|
|
|
}
|
|
|
|
}
|
2020-09-02 18:05:53 +09:00
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-02 18:05:53 +09:00
|
|
|
pub fn get_new_target<V>(&self) -> Result<V>
|
|
|
|
where
|
|
|
|
V: NapiValue,
|
|
|
|
{
|
|
|
|
let mut value = ptr::null_mut();
|
2020-11-25 18:42:14 +09:00
|
|
|
check_status!(unsafe { sys::napi_get_new_target(self.env.0, self.callback_info, &mut value) })?;
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { V::from_raw(self.env.0, value) }
|
2020-09-02 18:05:53 +09:00
|
|
|
}
|
2020-09-26 18:49:30 +09:00
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-09-26 18:49:30 +09:00
|
|
|
pub fn this<T: NapiValue>(&self) -> Result<T> {
|
2020-11-20 01:07:13 +09:00
|
|
|
unsafe { T::from_raw(self.env.0, self.raw_this) }
|
2020-09-26 18:49:30 +09:00
|
|
|
}
|
|
|
|
|
2020-11-25 18:42:14 +09:00
|
|
|
#[inline]
|
2020-11-20 10:09:57 +09:00
|
|
|
pub fn this_unchecked<T: NapiValue>(&self) -> T {
|
|
|
|
unsafe { T::from_raw_unchecked(self.env.0, self.raw_this) }
|
2020-09-26 18:49:30 +09:00
|
|
|
}
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|