2020-09-02 18:05:53 +09:00
|
|
|
use std::ptr;
|
|
|
|
|
|
|
|
use crate::error::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-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-07-18 03:00:48 +09:00
|
|
|
actual_arg_length: usize,
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
2020-09-26 18:49:30 +09:00
|
|
|
impl<'env> CallContext<'env> {
|
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-07-18 03:00:48 +09:00
|
|
|
actual_arg_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-07-18 03:00:48 +09:00
|
|
|
actual_arg_length,
|
2020-09-26 18:49:30 +09:00
|
|
|
}
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
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-09-26 18:49:30 +09:00
|
|
|
Ok(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
|
|
|
|
|
|
|
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(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if index < self.actual_arg_length {
|
|
|
|
ArgType::from_raw(self.env.0, self.args[index]).map(Either::A)
|
|
|
|
} else {
|
|
|
|
self.env.get_undefined().map(Either::B)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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();
|
|
|
|
check_status(unsafe { sys::napi_get_new_target(self.env.0, self.callback_info, &mut value) })?;
|
|
|
|
V::from_raw(self.env.0, value)
|
|
|
|
}
|
2020-09-26 18:49:30 +09:00
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn this<T: NapiValue>(&self) -> Result<T> {
|
|
|
|
T::from_raw(self.env.0, self.raw_this)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline(always)]
|
|
|
|
pub fn this_unchecked<T: NapiValue>(&self) -> T {
|
|
|
|
T::from_raw_unchecked(self.env.0, self.raw_this)
|
|
|
|
}
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|