2020-06-21 20:10:06 +09:00
|
|
|
use crate::{sys, Env, Error, JsUnknown, NapiValue, Result, Status};
|
2020-04-21 01:20:35 +09:00
|
|
|
|
2020-06-21 20:10:06 +09:00
|
|
|
pub struct CallContext<'env, T: NapiValue = JsUnknown> {
|
2020-05-09 16:14:44 +09:00
|
|
|
pub env: &'env Env,
|
2020-06-21 20:10:06 +09:00
|
|
|
pub this: T,
|
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-06-21 20:10:06 +09:00
|
|
|
impl<'env, T: NapiValue> CallContext<'env, T> {
|
2020-05-09 16:14:44 +09:00
|
|
|
#[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,
|
2020-06-21 18:50:38 +09:00
|
|
|
args: &'env [sys::napi_value],
|
2020-04-26 19:46:56 +09:00
|
|
|
arg_len: usize,
|
|
|
|
) -> Result<Self> {
|
|
|
|
Ok(Self {
|
|
|
|
env,
|
2020-06-21 20:10:06 +09:00
|
|
|
this: 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-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-06-21 20:10:06 +09:00
|
|
|
ArgType::from_raw(self.env.0, self.args[index])
|
2020-04-21 01:20:35 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|