2020-04-26 19:46:56 +09:00
|
|
|
use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType};
|
2020-04-21 01:20:35 +09:00
|
|
|
|
2020-05-09 16:14:44 +09:00
|
|
|
pub struct CallContext<'env, T: ValueType = Any> {
|
|
|
|
pub env: &'env Env,
|
2020-05-06 00:13:23 +09:00
|
|
|
pub this: Value<T>,
|
2020-04-21 01:20:35 +09:00
|
|
|
args: [sys::napi_value; 8],
|
|
|
|
arg_len: usize,
|
|
|
|
}
|
|
|
|
|
2020-05-09 16:14:44 +09:00
|
|
|
impl<'env, T: ValueType> CallContext<'env, T> {
|
|
|
|
#[inline]
|
2020-04-26 19:46:56 +09:00
|
|
|
pub fn new(
|
2020-05-09 16:14:44 +09:00
|
|
|
env: &'env Env,
|
2020-04-26 19:46:56 +09:00
|
|
|
this: sys::napi_value,
|
|
|
|
args: [sys::napi_value; 8],
|
|
|
|
arg_len: usize,
|
|
|
|
) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
env,
|
2020-05-06 00:13:23 +09:00
|
|
|
this: Value::<T>::from_raw(env.0, this)?,
|
2020-04-26 19:46:56 +09:00
|
|
|
args,
|
|
|
|
arg_len,
|
|
|
|
})
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
|
2020-05-09 16:14:44 +09:00
|
|
|
#[inline]
|
2020-05-06 00:13:23 +09:00
|
|
|
pub fn get<ArgType: ValueType>(&self, index: usize) -> Result<Value<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,
|
|
|
|
reason: Some("Arguments index out of range".to_owned()),
|
|
|
|
})
|
2020-04-21 01:20:35 +09:00
|
|
|
} else {
|
2020-05-06 00:13:23 +09:00
|
|
|
Value::<ArgType>::from_raw(self.env.0, self.args[index])
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|