napi-rs/napi/src/call_context.rs

33 lines
787 B
Rust
Raw Normal View History

use crate::{sys, Any, Env, Error, Result, Status, Value, ValueType};
2020-04-21 01:20:35 +09:00
pub struct CallContext<'env, T: ValueType = Any> {
pub env: &'env Env,
pub this: Value<'env, T>,
args: [sys::napi_value; 8],
arg_len: usize,
}
impl<'env, T: ValueType> CallContext<'env, T> {
pub fn new(
env: &'env Env,
this: sys::napi_value,
args: [sys::napi_value; 8],
arg_len: usize,
) -> Result<Self> {
Ok(Self {
env,
this: Value::<'env, T>::from_raw(env, this)?,
args,
arg_len,
})
2020-04-21 01:20:35 +09:00
}
pub fn get<ArgType: ValueType>(&'env self, index: usize) -> Result<Value<'env, ArgType>> {
if index + 1 > self.arg_len {
Err(Error::new(Status::GenericFailure))
} else {
Value::<'env, ArgType>::from_raw(&self.env, self.args[index])
}
}
}