1 | use std::ptr; |
2 | |
3 | use crate::bindgen_runtime::{FromNapiValue, TypeName}; |
4 | use crate::check_status; |
5 | use crate::{sys, Either, Env, Error, JsUndefined, NapiValue, Result, Status}; |
6 | |
7 | /// Function call context |
8 | pub struct CallContext<'env> { |
9 | pub env: &'env mut Env, |
10 | raw_this: sys::napi_value, |
11 | callback_info: sys::napi_callback_info, |
12 | args: &'env [sys::napi_value], |
13 | /// arguments.length |
14 | pub length: usize, |
15 | } |
16 | |
17 | impl<'env> CallContext<'env> { |
18 | /// The number of N-api obtained values. In practice this is the numeric |
19 | /// parameter provided to the `#[js_function(arg_len)]` macro. |
20 | /// |
21 | /// As a comparison, the (arguments) `.length` represents the actual number |
22 | /// of arguments given at a specific function call. |
23 | /// |
24 | /// If `.length < .arg_len`, then the elements in the `length .. arg_len` |
25 | /// range are just `JsUndefined`s. |
26 | /// |
27 | /// If `.length > .arg_len`, then truncation has happened and some args have |
28 | /// been lost. |
29 | fn arg_len(&self) -> usize { |
30 | self.args.len() |
31 | } |
32 | |
33 | pub fn new( |
34 | env: &'env mut Env, |
35 | callback_info: sys::napi_callback_info, |
36 | raw_this: sys::napi_value, |
37 | args: &'env [sys::napi_value], |
38 | length: usize, |
39 | ) -> Self { |
40 | Self { |
41 | env, |
42 | raw_this, |
43 | callback_info, |
44 | args, |
45 | length, |
46 | } |
47 | } |
48 | |
49 | pub fn get<ArgType: FromNapiValue>(&self, index: usize) -> Result<ArgType> { |
50 | if index >= self.arg_len() { |
51 | Err(Error::new( |
52 | Status::GenericFailure, |
53 | "Arguments index out of range" .to_owned(), |
54 | )) |
55 | } else { |
56 | unsafe { ArgType::from_napi_value(self.env.0, self.args[index]) } |
57 | } |
58 | } |
59 | |
60 | pub fn try_get<ArgType: NapiValue + TypeName + FromNapiValue>( |
61 | &self, |
62 | index: usize, |
63 | ) -> Result<Either<ArgType, JsUndefined>> { |
64 | if index >= self.arg_len() { |
65 | Err(Error::new( |
66 | Status::GenericFailure, |
67 | "Arguments index out of range" .to_owned(), |
68 | )) |
69 | } else if index < self.length { |
70 | unsafe { ArgType::from_raw(self.env.0, self.args[index]) }.map(Either::A) |
71 | } else { |
72 | self.env.get_undefined().map(Either::B) |
73 | } |
74 | } |
75 | |
76 | pub fn get_all(&self) -> Vec<crate::JsUnknown> { |
77 | /* (0 .. self.arg_len()).map(|i| self.get(i).unwrap()).collect() */ |
78 | self |
79 | .args |
80 | .iter() |
81 | .map(|&raw| unsafe { crate::JsUnknown::from_raw_unchecked(self.env.0, raw) }) |
82 | .collect() |
83 | } |
84 | |
85 | pub fn get_new_target<V>(&self) -> Result<V> |
86 | where |
87 | V: NapiValue, |
88 | { |
89 | let mut value = ptr::null_mut(); |
90 | check_status!(unsafe { sys::napi_get_new_target(self.env.0, self.callback_info, &mut value) })?; |
91 | unsafe { V::from_raw(self.env.0, value) } |
92 | } |
93 | |
94 | pub fn this<T: NapiValue>(&self) -> Result<T> { |
95 | unsafe { T::from_raw(self.env.0, self.raw_this) } |
96 | } |
97 | |
98 | pub fn this_unchecked<T: NapiValue>(&self) -> T { |
99 | unsafe { T::from_raw_unchecked(self.env.0, self.raw_this) } |
100 | } |
101 | } |
102 | |