napi-rs/napi/src/call_context.rs

38 lines
871 B
Rust
Raw Normal View History

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,
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]
pub fn new(
2020-05-09 16:14:44 +09:00
env: &'env Env,
this: sys::napi_value,
args: [sys::napi_value; 8],
arg_len: usize,
) -> Result<Self> {
Ok(Self {
env,
this: Value::<T>::from_raw(env.0, this)?,
args,
arg_len,
})
2020-04-21 01:20:35 +09:00
}
2020-05-09 16:14:44 +09:00
#[inline]
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 {
Err(Error {
status: Status::GenericFailure,
reason: Some("Arguments index out of range".to_owned()),
})
2020-04-21 01:20:35 +09:00
} else {
Value::<ArgType>::from_raw(self.env.0, self.args[index])
2020-04-21 01:20:35 +09:00
}
}
}