rquickjs_core/value/function/
args.rs

1use crate::{
2    function::{Flat, Opt, Rest, This},
3    qjs, Ctx, FromJs, Function, IntoJs, Result, Value,
4};
5use alloc::vec::Vec;
6
7use super::{ffi::defer_call_job, Constructor};
8
9const ARGS_ON_STACK: usize = 4;
10
11pub enum ArgsSlice {
12    Stack {
13        slice: [qjs::JSValue; ARGS_ON_STACK],
14        offset: u8,
15    },
16    Heap(Vec<qjs::JSValue>),
17}
18
19/// Argument input for a functions
20///
21/// Arguments on the Rust side for calling into the JavaScript context.
22pub struct Args<'js> {
23    ctx: Ctx<'js>,
24    pub(crate) this: qjs::JSValue,
25    pub(crate) args: ArgsSlice,
26}
27
28impl<'js> Args<'js> {
29    /// Returns a new args with space for a give number of arguments
30    pub fn new(ctx: Ctx<'js>, args: usize) -> Args<'js> {
31        Args {
32            ctx,
33            this: qjs::JS_UNDEFINED,
34            args: if args <= ARGS_ON_STACK {
35                ArgsSlice::Stack {
36                    slice: [qjs::JS_UNDEFINED; ARGS_ON_STACK],
37                    offset: 0,
38                }
39            } else {
40                ArgsSlice::Heap(Vec::with_capacity(args))
41            },
42        }
43    }
44
45    /// Returns a new args with space for any number of arguments
46    pub fn new_unsized(ctx: Ctx<'js>) -> Args<'js> {
47        Args {
48            ctx,
49            this: qjs::JS_UNDEFINED,
50            args: ArgsSlice::Heap(Vec::new()),
51        }
52    }
53
54    /// Returns the context associated with these arguments.
55    pub fn ctx(&self) -> &Ctx<'js> {
56        &self.ctx
57    }
58
59    /// Add an argument to the list.
60    pub fn push_arg<T: IntoJs<'js>>(&mut self, arg: T) -> Result<()> {
61        let v = arg.into_js(&self.ctx)?;
62
63        match self.args {
64            ArgsSlice::Stack {
65                ref mut slice,
66                ref mut offset,
67            } => {
68                if *offset >= 8 {
69                    panic!("pushed more arguments than num_args returned");
70                }
71                slice[*offset as usize] = v.into_js_value();
72                *offset += 1;
73            }
74            ArgsSlice::Heap(ref mut h) => h.push(v.into_js_value()),
75        }
76
77        Ok(())
78    }
79
80    /// Add multiple arguments to the list.
81    pub fn push_args<T, I>(&mut self, iter: I) -> Result<()>
82    where
83        T: IntoJs<'js>,
84        I: IntoIterator<Item = T>,
85    {
86        for a in iter.into_iter() {
87            self.push_arg(a)?
88        }
89        Ok(())
90    }
91
92    /// Add a this arguments.
93    pub fn this<T>(&mut self, this: T) -> Result<()>
94    where
95        T: IntoJs<'js>,
96    {
97        let v = this.into_js(&self.ctx)?;
98        let v = core::mem::replace(&mut self.this, v.into_js_value());
99        unsafe { qjs::JS_FreeValue(self.ctx.as_ptr(), v) };
100        Ok(())
101    }
102
103    /// Replace the this value with 'Undefined' and return the original value.
104    pub fn take_this(&mut self) -> Value<'js> {
105        let value = core::mem::replace(&mut self.this, qjs::JS_UNDEFINED);
106        Value {
107            ctx: self.ctx().clone(),
108            value,
109        }
110    }
111
112    /// The number of arguments currently in the list.
113    fn len(&self) -> usize {
114        match self.args {
115            ArgsSlice::Stack { offset, .. } => offset as usize,
116            ArgsSlice::Heap(ref h) => h.len(),
117        }
118    }
119
120    fn as_ptr(&self) -> *const qjs::JSValue {
121        match self.args {
122            ArgsSlice::Stack { ref slice, .. } => slice.as_ptr(),
123            ArgsSlice::Heap(ref h) => h.as_ptr(),
124        }
125    }
126
127    /// Call a function with the current set of arguments.
128    pub fn apply<R>(self, func: &Function<'js>) -> Result<R>
129    where
130        R: FromJs<'js>,
131    {
132        let val = unsafe {
133            let val = qjs::JS_Call(
134                self.ctx.as_ptr(),
135                func.as_js_value(),
136                self.this,
137                self.len() as _,
138                self.as_ptr() as _,
139            );
140            let val = self.ctx.handle_exception(val)?;
141            Value::from_js_value(self.ctx.clone(), val)
142        };
143        R::from_js(&self.ctx, val)
144    }
145
146    pub fn defer(mut self, func: Function<'js>) -> Result<()> {
147        let this = self.take_this();
148        self.push_arg(this)?;
149        self.push_arg(func)?;
150        let ctx = self.ctx();
151        unsafe {
152            if qjs::JS_EnqueueJob(
153                ctx.as_ptr(),
154                Some(defer_call_job),
155                self.len() as _,
156                self.as_ptr() as _,
157            ) < 0
158            {
159                return Err(ctx.raise_exception());
160            }
161        }
162        Ok(())
163    }
164
165    pub fn construct<R>(self, constructor: &Constructor<'js>) -> Result<R>
166    where
167        R: FromJs<'js>,
168    {
169        let value = if unsafe { qjs::JS_VALUE_GET_TAG(self.this) != qjs::JS_TAG_UNDEFINED } {
170            unsafe {
171                qjs::JS_CallConstructor2(
172                    self.ctx.as_ptr(),
173                    constructor.as_js_value(),
174                    self.this,
175                    self.len() as _,
176                    self.as_ptr() as _,
177                )
178            }
179        } else {
180            unsafe {
181                qjs::JS_CallConstructor(
182                    self.ctx.as_ptr(),
183                    constructor.as_js_value(),
184                    self.len() as _,
185                    self.as_ptr() as _,
186                )
187            }
188        };
189        let value = unsafe { self.ctx.handle_exception(value)? };
190        let v = unsafe { Value::from_js_value(self.ctx.clone(), value) };
191        R::from_js(&self.ctx, v)
192    }
193}
194
195impl Drop for Args<'_> {
196    fn drop(&mut self) {
197        match self.args {
198            ArgsSlice::Heap(ref h) => h.iter().for_each(|v| {
199                unsafe { qjs::JS_FreeValue(self.ctx.as_ptr(), *v) };
200            }),
201            ArgsSlice::Stack { ref slice, offset } => {
202                slice[..(offset as usize)].iter().for_each(|v| {
203                    unsafe { qjs::JS_FreeValue(self.ctx.as_ptr(), *v) };
204                })
205            }
206        }
207        unsafe { qjs::JS_FreeValue(self.ctx.as_ptr(), self.this) };
208    }
209}
210
211/// A trait for converting values into arguments.
212pub trait IntoArg<'js> {
213    /// The number of arguments this value produces.
214    fn num_args(&self) -> usize;
215
216    /// Convert the value into an argument.
217    fn into_arg(self, args: &mut Args<'js>) -> Result<()>;
218}
219
220/// A trait for converting a tuple of values into a list arguments.
221pub trait IntoArgs<'js> {
222    /// The number of arguments this value produces.
223    fn num_args(&self) -> usize;
224
225    /// Convert the value into an argument.
226    fn into_args(self, args: &mut Args<'js>) -> Result<()>;
227
228    fn apply<R>(self, function: &Function<'js>) -> Result<R>
229    where
230        R: FromJs<'js>,
231        Self: Sized,
232    {
233        let mut args = Args::new(function.ctx().clone(), self.num_args());
234        self.into_args(&mut args)?;
235        args.apply(function)
236    }
237
238    fn defer<R>(self, function: Function<'js>) -> Result<()>
239    where
240        Self: Sized,
241    {
242        let mut args = Args::new(function.ctx().clone(), self.num_args());
243        self.into_args(&mut args)?;
244        args.defer(function)
245    }
246
247    fn construct<R>(self, function: &Constructor<'js>) -> Result<()>
248    where
249        Self: Sized,
250    {
251        let mut args = Args::new(function.ctx().clone(), self.num_args());
252        self.into_args(&mut args)?;
253        args.construct(function)
254    }
255}
256
257impl<'js, T: IntoJs<'js>> IntoArg<'js> for T {
258    fn num_args(&self) -> usize {
259        1
260    }
261
262    fn into_arg(self, args: &mut Args<'js>) -> Result<()> {
263        args.push_arg(self)
264    }
265}
266
267impl<'js, T: IntoJs<'js>> IntoArg<'js> for This<T> {
268    fn num_args(&self) -> usize {
269        0
270    }
271
272    fn into_arg(self, args: &mut Args<'js>) -> Result<()> {
273        args.this(self.0)
274    }
275}
276
277impl<'js, T: IntoJs<'js>> IntoArg<'js> for Opt<T> {
278    fn num_args(&self) -> usize {
279        self.0.is_some() as usize
280    }
281
282    fn into_arg(self, args: &mut Args<'js>) -> Result<()> {
283        if let Some(x) = self.0 {
284            args.push_arg(x)?
285        }
286        Ok(())
287    }
288}
289
290impl<'js, T: IntoJs<'js>> IntoArg<'js> for Rest<T> {
291    fn num_args(&self) -> usize {
292        self.0.len()
293    }
294
295    fn into_arg(self, args: &mut Args<'js>) -> Result<()> {
296        args.push_args(self.0)
297    }
298}
299
300impl<'js, T: IntoArgs<'js>> IntoArg<'js> for Flat<T> {
301    fn num_args(&self) -> usize {
302        self.0.num_args()
303    }
304
305    fn into_arg(self, args: &mut Args<'js>) -> Result<()> {
306        self.0.into_args(args)
307    }
308}
309
310macro_rules! impl_into_args {
311    ($($t:ident),*) => {
312        #[allow(non_snake_case)]
313        impl<'js $(,$t)*> IntoArgs<'js> for ($($t,)*)
314        where
315            $($t : IntoArg<'js>,)*
316        {
317            fn num_args(&self) -> usize{
318                let ($(ref $t,)*) = *self;
319                0 $(+ $t.num_args())*
320            }
321
322            fn into_args(self, _args: &mut Args<'js>) -> Result<()>{
323                let ($($t,)*) = self;
324                $($t.into_arg(_args)?;)*
325                Ok(())
326            }
327        }
328    };
329}
330
331impl_into_args!();
332impl_into_args!(A);
333impl_into_args!(A, B);
334impl_into_args!(A, B, C);
335impl_into_args!(A, B, C, D);
336impl_into_args!(A, B, C, D, E);
337impl_into_args!(A, B, C, D, E, F);
338impl_into_args!(A, B, C, D, E, F, G);