rquickjs_core/value/
function.rs

1//! JavaScript function functionality
2
3use crate::{
4    atom::PredefinedAtom,
5    class::{Class, JsClass},
6    function::ffi::RustFunc,
7    qjs, Ctx, Error, FromJs, IntoJs, Object, Result, Value,
8};
9
10mod args;
11mod ffi;
12mod into_func;
13mod params;
14mod types;
15
16use alloc::{borrow::ToOwned as _, boxed::Box};
17pub use args::{Args, IntoArg, IntoArgs};
18pub use ffi::RustFunction;
19pub use params::{FromParam, FromParams, ParamRequirement, Params, ParamsAccessor};
20#[cfg(feature = "futures")]
21pub use types::Async;
22pub use types::{Exhaustive, Flat, Func, FuncArg, MutFn, Null, OnceFn, Opt, Rest, This};
23
24/// A trait for converting a Rust function to a JavaScript function.
25pub trait IntoJsFunc<'js, P> {
26    /// Returns the requirements this function has for the set of arguments used to call this
27    /// function.
28    fn param_requirements() -> ParamRequirement;
29
30    /// Call the function with the given parameters.
31    fn call<'a>(&self, params: Params<'a, 'js>) -> Result<Value<'js>>;
32}
33
34/// A trait for functions callable from JavaScript but static,
35/// Used for implementing callable objects.
36pub trait StaticJsFunction {
37    fn call<'a, 'js>(params: Params<'a, 'js>) -> Result<Value<'js>>;
38}
39
40/// A JavaScript function.
41#[derive(Clone, Debug, PartialEq, Eq, Hash)]
42#[repr(transparent)]
43pub struct Function<'js>(pub(crate) Object<'js>);
44
45impl<'js> Function<'js> {
46    /// Create a new function from a Rust function which implements [`IntoJsFunc`].
47    pub fn new<P, F>(ctx: Ctx<'js>, f: F) -> Result<Self>
48    where
49        F: IntoJsFunc<'js, P> + 'js,
50    {
51        let func = Box::new(move |params: Params<'_, 'js>| {
52            params.check_params(F::param_requirements())?;
53            f.call(params)
54        }) as Box<dyn RustFunc<'js> + 'js>;
55
56        let cls = Class::instance(ctx, RustFunction(func))?;
57        debug_assert!(cls.is_function());
58        Function(cls.into_inner()).with_length(F::param_requirements().min())
59    }
60
61    /// Call the function with given arguments.
62    pub fn call<A, R>(&self, args: A) -> Result<R>
63    where
64        A: IntoArgs<'js>,
65        R: FromJs<'js>,
66    {
67        let ctx = self.ctx();
68        let num = args.num_args();
69        let mut accum_args = Args::new(ctx.clone(), num);
70        args.into_args(&mut accum_args)?;
71        self.call_arg(accum_args)
72    }
73
74    /// Call the function with given arguments in the form of an [`Args`] object.
75    pub fn call_arg<R>(&self, args: Args<'js>) -> Result<R>
76    where
77        R: FromJs<'js>,
78    {
79        args.apply(self)
80    }
81
82    /// Defer call the function with given arguments.
83    ///
84    /// Calling a function with defer is equivalent to calling a JavaScript function with
85    /// `queueMicrotask()`.
86    pub fn defer<A>(&self, args: A) -> Result<()>
87    where
88        A: IntoArgs<'js>,
89    {
90        let ctx = self.ctx();
91        let num = args.num_args();
92        let mut accum_args = Args::new(ctx.clone(), num);
93        args.into_args(&mut accum_args)?;
94        self.defer_arg(accum_args)?;
95        Ok(())
96    }
97
98    /// Defer a function call with given arguments.
99    pub fn defer_arg(&self, args: Args<'js>) -> Result<()> {
100        args.defer(self.clone())
101    }
102
103    /// Set the `name` property of this function
104    pub fn set_name<S: AsRef<str>>(&self, name: S) -> Result<()> {
105        let name = name.as_ref().into_js(self.ctx())?;
106        unsafe {
107            let res = qjs::JS_DefinePropertyValue(
108                self.0.ctx.as_ptr(),
109                self.0.as_js_value(),
110                PredefinedAtom::Name as qjs::JSAtom,
111                name.into_js_value(),
112                (qjs::JS_PROP_CONFIGURABLE | qjs::JS_PROP_THROW) as _,
113            );
114            if res < 0 {
115                return Err(self.0.ctx.raise_exception());
116            }
117        };
118        Ok(())
119    }
120
121    /// Set the `name` property of this function and then return self.
122    pub fn with_name<S: AsRef<str>>(self, name: S) -> Result<Self> {
123        self.set_name(name)?;
124        Ok(self)
125    }
126
127    /// Sets the `length` property of the function.
128    pub fn set_length(&self, len: usize) -> Result<()> {
129        let len = len.into_js(self.ctx())?;
130        unsafe {
131            let res = qjs::JS_DefinePropertyValue(
132                self.0.ctx.as_ptr(),
133                self.0.as_js_value(),
134                PredefinedAtom::Length as qjs::JSAtom,
135                len.into_js_value(),
136                (qjs::JS_PROP_CONFIGURABLE | qjs::JS_PROP_THROW) as _,
137            );
138            if res < 0 {
139                return Err(self.0.ctx.raise_exception());
140            }
141        };
142        Ok(())
143    }
144
145    /// Sets the `length` property of the function and return self.
146    pub fn with_length(self, len: usize) -> Result<Self> {
147        self.set_length(len)?;
148        Ok(self)
149    }
150
151    /// Returns the prototype which all JavaScript function by default have as its prototype, i.e.
152    /// `Function.prototype`.
153    pub fn prototype(ctx: Ctx<'js>) -> Object<'js> {
154        let res = unsafe {
155            let v = qjs::JS_GetFunctionProto(ctx.as_ptr());
156            Value::from_js_value(ctx, v)
157        };
158        // as far is I know this should always be an object.
159        res.into_object()
160            .expect("`Function.prototype` wasn't an object")
161    }
162
163    /// Returns whether this function is an constructor.
164    pub fn is_constructor(&self) -> bool {
165        ((unsafe { qjs::JS_IsConstructor(self.ctx().as_ptr(), self.0.as_js_value()) }) as i32) != 0
166    }
167
168    /// Set whether this function is a constructor or not.
169    pub fn set_constructor(&self, is_constructor: bool) {
170        unsafe {
171            qjs::JS_SetConstructorBit(self.ctx().as_ptr(), self.0.as_js_value(), is_constructor)
172        };
173    }
174
175    /// Set whether this function is a constructor or not then return self.
176    pub fn with_constructor(self, is_constructor: bool) -> Self {
177        self.set_constructor(is_constructor);
178        self
179    }
180}
181
182/// A function which can be used as a constructor.
183///
184/// Is a subtype of function.
185#[derive(Debug, Clone)]
186#[repr(transparent)]
187pub struct Constructor<'js>(pub(crate) Function<'js>);
188
189impl<'js> Constructor<'js> {
190    /// Creates a Rust constructor function for a Rust class.
191    ///
192    /// Note that this function creates a constructor from a given function, the returned constructor
193    /// is thus not the same as the one returned from [`JsClass::constructor`].
194    pub fn new_class<C, F, P>(ctx: Ctx<'js>, f: F) -> Result<Self>
195    where
196        F: IntoJsFunc<'js, P> + 'js,
197        C: JsClass<'js>,
198    {
199        let func = Box::new(move |params: Params<'_, 'js>| -> Result<Value<'js>> {
200            params.check_params(F::param_requirements())?;
201            let this = params.this();
202            let ctx = params.ctx().clone();
203
204            // get the prototype of thie class from itself or the inate class prototype.
205            let proto = this
206                .into_function()
207                .map(|func| func.get(PredefinedAtom::Prototype))
208                .unwrap_or_else(|| Class::<C>::prototype(&ctx))?;
209
210            let res = f.call(params)?;
211            res.as_object()
212                .ok_or_else(|| Error::IntoJs {
213                    from: res.type_of().as_str(),
214                    to: "object",
215                    message: Some("rust constructor function did not return a object".to_owned()),
216                })?
217                .set_prototype(proto.as_ref())?;
218            Ok(res)
219        });
220        let func = Function(Class::instance(ctx.clone(), RustFunction(func))?.into_inner())
221            .with_name(C::NAME)?
222            .with_constructor(true);
223        unsafe {
224            qjs::JS_SetConstructor(
225                ctx.as_ptr(),
226                func.as_js_value(),
227                Class::<C>::prototype(&ctx)?
228                    .as_ref()
229                    .map(|x| x.as_js_value())
230                    .unwrap_or(qjs::JS_NULL),
231            )
232        };
233        Ok(Constructor(func))
234    }
235
236    /// Create a new Rust constructor function with a given prototype.
237    ///
238    /// Useful if the function does not return a Rust class.
239    pub fn new_prototype<F, P>(ctx: &Ctx<'js>, prototype: Object<'js>, f: F) -> Result<Self>
240    where
241        F: IntoJsFunc<'js, P> + 'js,
242    {
243        let proto_clone = prototype.clone();
244        let func = Box::new(move |params: Params<'_, 'js>| -> Result<Value<'js>> {
245            params.check_params(F::param_requirements())?;
246            let this = params.this();
247            let proto = this
248                .as_function()
249                .map(|func| func.get(PredefinedAtom::Prototype))
250                .unwrap_or_else(|| Ok(Some(proto_clone.clone())))?;
251
252            let res = f.call(params)?;
253            res.as_object()
254                .ok_or_else(|| Error::IntoJs {
255                    from: res.type_of().as_str(),
256                    to: "object",
257                    message: Some("rust constructor function did not return a object".to_owned()),
258                })?
259                .set_prototype(proto.as_ref())?;
260            Ok(res)
261        });
262        let func = Function(Class::instance(ctx.clone(), RustFunction(func))?.into_inner())
263            .with_constructor(true);
264        unsafe {
265            qjs::JS_SetConstructor(ctx.as_ptr(), func.as_js_value(), prototype.as_js_value())
266        };
267        Ok(Constructor(func))
268    }
269
270    /// Call the constructor as a constructor.
271    ///
272    /// Equivalent to calling any constructor function with the new keyword.
273    pub fn construct<A, R>(&self, args: A) -> Result<R>
274    where
275        A: IntoArgs<'js>,
276        R: FromJs<'js>,
277    {
278        let ctx = self.ctx();
279        let num = args.num_args();
280        let mut accum_args = Args::new(ctx.clone(), num);
281        args.into_args(&mut accum_args)?;
282        self.construct_args(accum_args)
283    }
284
285    /// Call the constructor as a constructor with an [`Args`] object.
286    ///
287    /// Equivalent to calling any constructor function with the new keyword.
288    pub fn construct_args<R>(&self, args: Args<'js>) -> Result<R>
289    where
290        R: FromJs<'js>,
291    {
292        args.construct(self)
293    }
294}
295
296#[cfg(test)]
297mod test {
298    use crate::{prelude::*, *};
299    use approx::assert_abs_diff_eq as assert_approx_eq;
300
301    #[test]
302    fn call_js_fn_with_no_args_and_no_return() {
303        test_with(|ctx| {
304            let f: Function = ctx.eval("() => {}").unwrap();
305
306            let _: () = ().apply(&f).unwrap();
307            let _: () = f.call(()).unwrap();
308        })
309    }
310
311    #[test]
312    fn call_js_fn_with_no_args_and_return() {
313        test_with(|ctx| {
314            let f: Function = ctx.eval("() => 42").unwrap();
315
316            let res: i32 = ().apply(&f).unwrap();
317            assert_eq!(res, 42);
318
319            let res: i32 = f.call(()).unwrap();
320            assert_eq!(res, 42);
321        })
322    }
323
324    #[test]
325    fn call_js_fn_with_1_arg_and_return() {
326        test_with(|ctx| {
327            let f: Function = ctx.eval("a => a + 4").unwrap();
328
329            let res: i32 = (3,).apply(&f).unwrap();
330            assert_eq!(res, 7);
331
332            let res: i32 = f.call((1,)).unwrap();
333            assert_eq!(res, 5);
334        })
335    }
336
337    #[test]
338    fn call_js_fn_with_2_args_and_return() {
339        test_with(|ctx| {
340            let f: Function = ctx.eval("(a, b) => a * b + 4").unwrap();
341
342            let res: i32 = (3, 4).apply(&f).unwrap();
343            assert_eq!(res, 16);
344
345            let res: i32 = f.call((5, 1)).unwrap();
346            assert_eq!(res, 9);
347        })
348    }
349
350    #[test]
351    fn call_js_fn_with_var_args_and_return() {
352        let res: Vec<i8> = test_with(|ctx| {
353            let func: Function = ctx
354                .eval(
355                    r#"
356                  (...x) => [x.length, ...x]
357                "#,
358                )
359                .unwrap();
360            func.call((Rest(vec![1, 2, 3]),)).unwrap()
361        });
362        assert_eq!(res.len(), 4);
363        assert_eq!(res[0], 3);
364        assert_eq!(res[1], 1);
365        assert_eq!(res[2], 2);
366        assert_eq!(res[3], 3);
367    }
368
369    #[test]
370    fn call_js_fn_with_rest_args_and_return() {
371        let res: Vec<i8> = test_with(|ctx| {
372            let func: Function = ctx
373                .eval(
374                    r#"
375                  (a, b, ...x) => [a, b, x.length, ...x]
376                "#,
377                )
378                .unwrap();
379            func.call((-2, -1, Rest(vec![1, 2]))).unwrap()
380        });
381        assert_eq!(res.len(), 5);
382        assert_eq!(res[0], -2);
383        assert_eq!(res[1], -1);
384        assert_eq!(res[2], 2);
385        assert_eq!(res[3], 1);
386        assert_eq!(res[4], 2);
387    }
388
389    #[test]
390    fn call_js_fn_with_no_args_and_throw() {
391        test_with(|ctx| {
392            let f: Function = ctx
393                .eval("() => { throw new Error('unimplemented'); }")
394                .unwrap();
395
396            if let Err(Error::Exception) = f.call::<_, ()>(()) {
397                let exception = Exception::from_js(&ctx, ctx.catch()).unwrap();
398                assert_eq!(exception.message().as_deref(), Some("unimplemented"));
399            } else {
400                panic!("Should throws");
401            }
402        })
403    }
404
405    #[test]
406    fn call_js_fn_with_this_and_no_args_and_return() {
407        test_with(|ctx| {
408            let f: Function = ctx.eval("function f() { return this.val; } f").unwrap();
409            let obj = Object::new(ctx).unwrap();
410            obj.set("val", 42).unwrap();
411
412            let res: i32 = (This(obj.clone()),).apply(&f).unwrap();
413            assert_eq!(res, 42);
414            let res: i32 = f.call((This(obj),)).unwrap();
415            assert_eq!(res, 42);
416        })
417    }
418
419    #[test]
420    fn call_js_fn_with_this_and_1_arg_and_return() {
421        test_with(|ctx| {
422            let f: Function = ctx
423                .eval("function f(a) { return this.val * a; } f")
424                .unwrap();
425            let obj = Object::new(ctx).unwrap();
426            obj.set("val", 3).unwrap();
427
428            let res: i32 = (This(obj.clone()), 2).apply(&f).unwrap();
429            assert_eq!(res, 6);
430            let res: i32 = f.call((This(obj), 3)).unwrap();
431            assert_eq!(res, 9);
432        })
433    }
434
435    #[test]
436    fn call_js_fn_with_1_arg_deferred() {
437        let rt = Runtime::new().unwrap();
438        let ctx = Context::full(&rt).unwrap();
439        assert!(!rt.is_job_pending());
440        ctx.with(|ctx| {
441            let g = ctx.globals();
442            let f: Function = ctx.eval("(obj) => { obj.called = true; }").unwrap();
443            f.defer((g.clone(),)).unwrap();
444            let c: Value = g.get("called").unwrap();
445            assert_eq!(c.type_of(), Type::Undefined);
446        });
447        assert!(rt.is_job_pending());
448        rt.execute_pending_job().unwrap();
449        ctx.with(|ctx| {
450            let g = ctx.globals();
451            let c: Value = g.get("called").unwrap();
452            assert_eq!(c.type_of(), Type::Bool);
453        });
454    }
455
456    fn test() {
457        println!("test");
458    }
459
460    #[test]
461    fn static_callback() {
462        test_with(|ctx| {
463            let f = Function::new(ctx.clone(), test).unwrap();
464            f.set_name("test").unwrap();
465            let eval: Function = ctx.eval("a => { a() }").unwrap();
466            (f.clone(),).apply::<()>(&eval).unwrap();
467            f.call::<_, ()>(()).unwrap();
468
469            let name: StdString = f.clone().into_inner().get("name").unwrap();
470            assert_eq!(name, "test");
471
472            let get_name: Function = ctx.eval("a => a.name").unwrap();
473            let name: StdString = get_name.call((f.clone(),)).unwrap();
474            assert_eq!(name, "test");
475        })
476    }
477
478    #[test]
479    fn const_callback() {
480        use std::sync::{Arc, Mutex};
481        test_with(|ctx| {
482            #[allow(clippy::mutex_atomic)]
483            let called = Arc::new(Mutex::new(false));
484            let called_clone = called.clone();
485            let f = Function::new(ctx.clone(), move || {
486                (*called_clone.lock().unwrap()) = true;
487            })
488            .unwrap();
489            f.set_name("test").unwrap();
490
491            let eval: Function = ctx.eval("a => { a() }").unwrap();
492            eval.call::<_, ()>((f.clone(),)).unwrap();
493            f.call::<_, ()>(()).unwrap();
494            assert!(*called.lock().unwrap());
495
496            let name: StdString = f.clone().into_inner().get("name").unwrap();
497            assert_eq!(name, "test");
498
499            let get_name: Function = ctx.eval("a => a.name").unwrap();
500            let name: StdString = get_name.call((f.clone(),)).unwrap();
501            assert_eq!(name, "test");
502        })
503    }
504
505    #[test]
506    fn mutable_callback() {
507        test_with(|ctx| {
508            let mut v = 0;
509            let f = Function::new(
510                ctx.clone(),
511                MutFn::new(move || {
512                    v += 1;
513                    v
514                }),
515            )
516            .unwrap();
517            f.set_name("test").unwrap();
518
519            let eval: Function = ctx.eval("a => a()").unwrap();
520            assert_eq!(eval.call::<_, i32>((f.clone(),)).unwrap(), 1);
521            assert_eq!(eval.call::<_, i32>((f.clone(),)).unwrap(), 2);
522            assert_eq!(eval.call::<_, i32>((f.clone(),)).unwrap(), 3);
523
524            let name: StdString = f.clone().into_inner().get("name").unwrap();
525            assert_eq!(name, "test");
526
527            let get_name: Function = ctx.eval("a => a.name").unwrap();
528            let name: StdString = get_name.call((f.clone(),)).unwrap();
529            assert_eq!(name, "test");
530        })
531    }
532
533    #[test]
534    #[should_panic(
535        expected = "Error borrowing function: can't borrow a value as it is already borrowed"
536    )]
537    fn recursively_called_mutable_callback() {
538        test_with(|ctx| {
539            let mut v = 0;
540            let f = Function::new(
541                ctx.clone(),
542                MutFn::new(move |ctx: Ctx| {
543                    v += 1;
544                    ctx.globals()
545                        .get::<_, Function>("foo")
546                        .unwrap()
547                        .call::<_, ()>(())
548                        .catch(&ctx)
549                        .unwrap();
550                    v
551                }),
552            )
553            .unwrap();
554            ctx.globals().set("foo", f.clone()).unwrap();
555            f.call::<_, ()>(()).unwrap();
556        })
557    }
558
559    #[test]
560    #[should_panic(
561        expected = "Error borrowing function: tried to use a value, which can only be used once, again."
562    )]
563    fn repeatedly_called_once_callback() {
564        test_with(|ctx| {
565            let mut v = 0;
566            let f = Function::new(
567                ctx.clone(),
568                OnceFn::from(move || {
569                    v += 1;
570                    v
571                }),
572            )
573            .unwrap();
574            ctx.globals().set("foo", f.clone()).unwrap();
575            f.call::<_, ()>(()).catch(&ctx).unwrap();
576            f.call::<_, ()>(()).catch(&ctx).unwrap();
577        })
578    }
579
580    #[test]
581    fn multiple_const_callbacks() {
582        test_with(|ctx| {
583            let globals = ctx.globals();
584            globals.set("one", Func::new(|| 1f64)).unwrap();
585            globals.set("neg", Func::new(|a: f64| -a)).unwrap();
586            globals
587                .set("add", Func::new(|a: f64, b: f64| a + b))
588                .unwrap();
589
590            let r: f64 = ctx.eval("neg(add(one(), 2))").unwrap();
591            assert_approx_eq!(r, -3.0);
592        })
593    }
594
595    #[test]
596    fn mutable_callback_which_can_fail() {
597        test_with(|ctx| {
598            let globals = ctx.globals();
599            let mut id_alloc = 0;
600            globals
601                .set(
602                    "new_id",
603                    Func::from(MutFn::from(move || {
604                        id_alloc += 1;
605                        if id_alloc < 4 {
606                            Ok(id_alloc)
607                        } else {
608                            Err(Error::Unknown)
609                        }
610                    })),
611                )
612                .unwrap();
613
614            let id: u32 = ctx.eval("new_id()").unwrap();
615            assert_eq!(id, 1);
616            let id: u32 = ctx.eval("new_id()").unwrap();
617            assert_eq!(id, 2);
618            let id: u32 = ctx.eval("new_id()").unwrap();
619            assert_eq!(id, 3);
620            let _err = ctx.eval::<u32, _>("new_id()").unwrap_err();
621        })
622    }
623
624    #[test]
625    fn mutable_callback_with_ctx_which_reads_globals() {
626        test_with(|ctx| {
627            let globals = ctx.globals();
628            let mut id_alloc = 0;
629            globals
630                .set(
631                    "new_id",
632                    Func::from(MutFn::from(move |ctx: Ctx| {
633                        let initial: Option<u32> = ctx.globals().get("initial_id")?;
634                        if let Some(initial) = initial {
635                            id_alloc += 1;
636                            Ok(id_alloc + initial)
637                        } else {
638                            Err(Error::Unknown)
639                        }
640                    })),
641                )
642                .unwrap();
643
644            let _err = ctx.eval::<u32, _>("new_id()").unwrap_err();
645            globals.set("initial_id", 10).unwrap();
646
647            let id: u32 = ctx.eval("new_id()").unwrap();
648            assert_eq!(id, 11);
649            let id: u32 = ctx.eval("new_id()").unwrap();
650            assert_eq!(id, 12);
651            let id: u32 = ctx.eval("new_id()").unwrap();
652            assert_eq!(id, 13);
653        })
654    }
655
656    #[test]
657    fn call_rust_fn_with_ctx_and_value() {
658        test_with(|ctx| {
659            let func = Func::from(|ctx, val| {
660                struct Args<'js>(Ctx<'js>, Value<'js>);
661                let Args(ctx, val) = Args(ctx, val);
662                ctx.globals().set("test_str", val).unwrap();
663            });
664            ctx.globals().set("test_fn", func).unwrap();
665            ctx.eval::<(), _>(
666                r#"
667                  test_fn("test_str")
668                "#,
669            )
670            .unwrap();
671            let val: StdString = ctx.globals().get("test_str").unwrap();
672            assert_eq!(val, "test_str");
673        });
674    }
675
676    #[test]
677    fn call_rust_fn_with_this_and_args() {
678        let res: f64 = test_with(|ctx| {
679            let func = Function::new(ctx.clone(), |this: This<Object>, a: f64, b: f64| {
680                let x: f64 = this.get("x").unwrap();
681                let y: f64 = this.get("y").unwrap();
682                this.set("r", a * x + b * y).unwrap();
683            })
684            .unwrap();
685            ctx.globals().set("test_fn", func).unwrap();
686            ctx.eval(
687                r#"
688                  let test_obj = { x: 1, y: 2 };
689                  test_fn.call(test_obj, 3, 4);
690                  test_obj.r
691                "#,
692            )
693            .unwrap()
694        });
695        assert_eq!(res, 11.0);
696    }
697
698    #[test]
699    fn apply_rust_fn_with_this_and_args() {
700        let res: f32 = test_with(|ctx| {
701            let func = Function::new(ctx.clone(), |this: This<Object>, x: f32, y: f32| {
702                let a: f32 = this.get("a").unwrap();
703                let b: f32 = this.get("b").unwrap();
704                a * x + b * y
705            })
706            .unwrap();
707            ctx.globals().set("test_fn", func).unwrap();
708            ctx.eval(
709                r#"
710                  let test_obj = { a: 1, b: 2 };
711                  test_fn.apply(test_obj, [3, 4])
712                "#,
713            )
714            .unwrap()
715        });
716        assert_eq!(res, 11.0);
717    }
718
719    #[test]
720    fn bind_rust_fn_with_this_and_call_with_args() {
721        let res: f32 = test_with(|ctx| {
722            let func = Function::new(ctx.clone(), |this: This<Object>, x: f32, y: f32| {
723                let a: f32 = this.get("a").unwrap();
724                let b: f32 = this.get("b").unwrap();
725                a * x + b * y
726            })
727            .unwrap();
728            ctx.globals().set("test_fn", func).unwrap();
729            ctx.eval(
730                r#"
731                  let test_obj = { a: 1, b: 2 };
732                  test_fn.bind(test_obj)(3, 4)
733                "#,
734            )
735            .unwrap()
736        });
737        assert_eq!(res, 11.0);
738    }
739
740    #[test]
741    fn call_rust_fn_with_var_args() {
742        let res: Vec<i8> = test_with(|ctx| {
743            let func = Function::new(ctx.clone(), |args: Rest<i8>| {
744                use std::iter::once;
745                once(args.len() as i8)
746                    .chain(args.iter().cloned())
747                    .collect::<Vec<_>>()
748            })
749            .unwrap();
750            ctx.globals().set("test_fn", func).unwrap();
751            ctx.eval(
752                r#"
753                  test_fn(1, 2, 3)
754                "#,
755            )
756            .unwrap()
757        });
758        assert_eq!(res.len(), 4);
759        assert_eq!(res[0], 3);
760        assert_eq!(res[1], 1);
761        assert_eq!(res[2], 2);
762        assert_eq!(res[3], 3);
763    }
764
765    #[test]
766    fn call_rust_fn_with_rest_args() {
767        let res: Vec<i8> = test_with(|ctx| {
768            let func = Function::new(ctx.clone(), |arg1: i8, arg2: i8, args: Rest<i8>| {
769                use std::iter::once;
770                once(arg1)
771                    .chain(once(arg2))
772                    .chain(once(args.len() as i8))
773                    .chain(args.iter().cloned())
774                    .collect::<Vec<_>>()
775            })
776            .unwrap();
777            ctx.globals().set("test_fn", func).unwrap();
778            ctx.eval(
779                r#"
780                  test_fn(-2, -1, 1, 2)
781                "#,
782            )
783            .unwrap()
784        });
785        assert_eq!(res.len(), 5);
786        assert_eq!(res[0], -2);
787        assert_eq!(res[1], -1);
788        assert_eq!(res[2], 2);
789        assert_eq!(res[3], 1);
790        assert_eq!(res[4], 2);
791    }
792
793    #[test]
794    fn js_fn_wrappers() {
795        test_with(|ctx| {
796            let global = ctx.globals();
797            global
798                .set(
799                    "cat",
800                    Func::from(|a: StdString, b: StdString| format!("{a}{b}")),
801                )
802                .unwrap();
803            let res: StdString = ctx.eval("cat(\"foo\", \"bar\")").unwrap();
804            assert_eq!(res, "foobar");
805
806            let mut log = Vec::<StdString>::new();
807            global
808                .set(
809                    "log",
810                    Func::from(MutFn::from(move |msg: StdString| {
811                        log.push(msg);
812                        log.len() as u32
813                    })),
814                )
815                .unwrap();
816            let n: u32 = ctx.eval("log(\"foo\") + log(\"bar\")").unwrap();
817            assert_eq!(n, 3);
818        });
819    }
820}