1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
//! Method registration

use std::borrow::Cow;
use std::marker::PhantomData;
use std::{fmt, ops};

use crate::core_types::{FromVariant, FromVariantError, Variant};
use crate::export::class::NativeClass;
use crate::export::{class_registry, ClassBuilder};
use crate::log::Site;
use crate::object::ownership::Shared;
use crate::object::{Ref, TInstance, TRef};

/// Builder type used to register a method on a `NativeClass`.
#[must_use = "MethodBuilder left unbuilt -- did you forget to call done() or done_stateless()?"]
pub struct MethodBuilder<'a, C, F> {
    class_builder: &'a ClassBuilder<C>,
    name: &'a str,
    method: F,

    rpc_mode: RpcMode,
}

impl<'a, C, F> MethodBuilder<'a, C, F>
where
    C: NativeClass,
    F: Method<C>,
{
    pub(super) fn new(class_builder: &'a ClassBuilder<C>, name: &'a str, method: F) -> Self {
        MethodBuilder {
            class_builder,
            name,
            method,
            rpc_mode: RpcMode::Disabled,
        }
    }

    /// Set a RPC mode for this method.
    #[inline]
    pub fn with_rpc_mode(mut self, rpc_mode: RpcMode) -> Self {
        self.rpc_mode = rpc_mode;
        self
    }

    /// Register the method.
    #[inline]
    pub fn done(self) {
        let method_data = Box::into_raw(Box::new(self.method));

        let script_method = ScriptMethod {
            name: self.name,
            method_ptr: Some(method_wrapper::<C, F>),
            attributes: ScriptMethodAttributes {
                rpc_mode: self.rpc_mode,
            },
            method_data: method_data as *mut libc::c_void,
            free_func: Some(free_func::<F>),
        };

        self.class_builder.add_method(script_method);
    }
}

impl<'a, C, F> MethodBuilder<'a, C, F>
where
    C: NativeClass,
    F: Method<C> + Copy + Default,
{
    /// Register the method as a stateless method. Stateless methods do not have data
    /// pointers and destructors and are thus slightly lighter. This is intended for ZSTs,
    /// but can be used with any `Method` type with `Copy + Default`.
    #[inline]
    pub fn done_stateless(self) {
        let script_method = ScriptMethod {
            name: self.name,
            method_ptr: Some(method_wrapper::<C, Stateless<F>>),
            attributes: ScriptMethodAttributes {
                rpc_mode: self.rpc_mode,
            },

            // Stateless<F> is a ZST for any type F, so we can use any non-zero value as
            // a valid pointer for it.
            method_data: 1 as *mut libc::c_void,
            free_func: None,
        };

        self.class_builder.add_method(script_method);
    }
}

type ScriptMethodFn = unsafe extern "C" fn(
    *mut sys::godot_object,
    *mut libc::c_void,
    *mut libc::c_void,
    libc::c_int,
    *mut *mut sys::godot_variant,
) -> sys::godot_variant;

pub enum RpcMode {
    Disabled,
    Remote,
    RemoteSync,
    Master,
    Puppet,
    MasterSync,
    PuppetSync,
}

impl Default for RpcMode {
    #[inline]
    fn default() -> Self {
        RpcMode::Disabled
    }
}

pub(crate) struct ScriptMethodAttributes {
    pub rpc_mode: RpcMode,
}

pub(crate) struct ScriptMethod<'l> {
    pub name: &'l str,
    pub method_ptr: Option<ScriptMethodFn>,
    pub attributes: ScriptMethodAttributes,

    pub method_data: *mut libc::c_void,
    pub free_func: Option<unsafe extern "C" fn(*mut libc::c_void) -> ()>,
}

/// Safe low-level trait for stateful, variadic methods that can be called on a native script type.
pub trait Method<C: NativeClass>: Send + Sync + 'static {
    /// Calls the method on `this` with `args`.
    fn call(&self, this: TInstance<'_, C>, args: Varargs<'_>) -> Variant;

    /// Returns an optional site where this method is defined. Used for logging errors in FFI wrappers.
    ///
    /// Default implementation returns `None`.
    #[inline]
    fn site() -> Option<Site<'static>> {
        None
    }
}

/// Wrapper for stateless methods that produces values with `Copy` and `Default`.
struct Stateless<F> {
    _marker: PhantomData<F>,
}

impl<C: NativeClass, F: Method<C> + Copy + Default> Method<C> for Stateless<F> {
    fn call(&self, this: TInstance<'_, C>, args: Varargs<'_>) -> Variant {
        let f = F::default();
        f.call(this, args)
    }
}

/// Adapter for methods whose arguments are statically determined. If the arguments would fail to
/// type check, the method will print the errors to Godot's debug console and return `null`.
#[derive(Clone, Copy, Default, Debug)]
pub struct StaticArgs<F> {
    f: F,
}

impl<F> StaticArgs<F> {
    /// Wrap `f` in an adapter that implements `Method`.
    #[inline]
    pub fn new(f: F) -> Self {
        StaticArgs { f }
    }
}

/// Trait for methods whose argument lists are known at compile time. Not to be confused with a
/// "static method".
pub trait StaticArgsMethod<C: NativeClass>: Send + Sync + 'static {
    type Args: FromVarargs;
    fn call(&self, this: TInstance<'_, C>, args: Self::Args) -> Variant;

    /// Returns an optional site where this method is defined. Used for logging errors in FFI wrappers.
    ///
    /// Default implementation returns `None`.
    #[inline]
    fn site() -> Option<Site<'static>> {
        None
    }
}

impl<C: NativeClass, F: StaticArgsMethod<C>> Method<C> for StaticArgs<F> {
    #[inline]
    fn call(&self, this: TInstance<'_, C>, mut args: Varargs<'_>) -> Variant {
        match args.read_many::<F::Args>() {
            Ok(parsed) => {
                if let Err(err) = args.done() {
                    err.with_site(F::site().unwrap_or_default()).log_error();
                    return Variant::nil();
                }
                F::call(&self.f, this, parsed)
            }
            Err(errors) => {
                for err in errors {
                    err.with_site(F::site().unwrap_or_default()).log_error();
                }
                Variant::nil()
            }
        }
    }

    #[inline]
    fn site() -> Option<Site<'static>> {
        F::site()
    }
}

/// Safe interface to a list of borrowed method arguments with a convenient API
/// for common operations with them.
///
/// **Note:** the `impl Iterator` is deprecated, `Varargs` itself should not be treated as a consumable iterator.
/// Instead, use [`Self::as_slice()`].
///
/// This type can be destructured into tuples:
/// ```no_run
/// use gdnative::prelude::*;
/// use gdnative::export::Varargs;
///
/// #[derive(NativeClass)]
/// #[no_constructor]
/// struct MyClass {}
///
/// struct CalcMethod;
/// impl Method<MyClass> for CalcMethod {
///     fn call(
///         &self,
///         _this: TInstance<'_, MyClass>,
///         args: Varargs<'_>,
///     ) -> Variant {
///         // Destructure via try_into():
///         let (a, b): (i64, i64) = args.try_into().expect("signature mismatch");
///         let ret = a * b;
///         ret.to_variant()
///     }
/// }
/// ```
pub struct Varargs<'a> {
    idx: usize,
    args: &'a [&'a Variant],
    offset_index: usize,
}

impl<'a> Varargs<'a> {
    /// Returns the amount of arguments left.
    #[inline]
    pub fn len(&self) -> usize {
        self.args.len() - self.idx
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns a builder for reading the next argument, that can be used to refine
    /// the error message on failure.
    #[inline]
    pub fn read<T: FromVariant>(&mut self) -> ArgBuilder<'_, 'a, T> {
        ArgBuilder {
            args: self,
            name: None,
            ty: None,
            site: None,
            _marker: PhantomData,
        }
    }

    /// Parses a structure that implements `FromVarargs` incrementally from the
    /// remaining arguments.
    #[inline]
    pub fn read_many<T: FromVarargs>(&mut self) -> Result<T, Vec<ArgumentError<'a>>> {
        T::read(self)
    }

    /// Returns the remaining arguments as a slice of `Variant`s.
    #[inline]
    pub fn as_slice(&self) -> &'a [&'a Variant] {
        &self.args[self.idx..]
    }

    /// Discard the rest of the arguments, and return an error if there is any.
    ///
    /// # Errors
    ///
    /// If there are any excess arguments left.
    #[inline]
    pub fn done(self) -> Result<(), ArgumentError<'a>> {
        if self.is_empty() {
            Ok(())
        } else {
            Err(ArgumentError {
                site: None,
                kind: ArgumentErrorKind::ExcessArguments {
                    rest: self.as_slice(),
                },
            })
        }
    }

    /// Create a typed interface from raw pointers. This is an internal interface.
    ///
    /// # Safety
    ///
    /// `args` must point to an array of valid `godot_variant` pointers of at least `num_args` long.
    #[doc(hidden)]
    #[inline]
    pub unsafe fn from_sys(num_args: libc::c_int, args: *mut *mut sys::godot_variant) -> Self {
        let args = std::slice::from_raw_parts(args, num_args as usize);
        let args = std::mem::transmute::<&[*mut sys::godot_variant], &[&Variant]>(args);
        Self {
            idx: 0,
            args,
            offset_index: 0,
        }
    }

    /// Check the length of arguments.
    ///
    /// See [`Self::get()`] or [`Self::get_opt()`] for examples.
    ///
    /// # Errors
    /// Returns an [`VarargsError::InvalidLength`] if the length of arguments is outside the specified range.
    #[inline]
    pub fn check_length(&self, expected: impl Into<IndexBounds>) -> Result<(), VarargsError> {
        let passed = self.args.len();
        let expected = expected.into();
        if expected.contains(passed) {
            Ok(())
        } else {
            // Note: cannot use Box<dyn RangeBounds<usize>> because trait is not object-safe due to _unrelated_ method contains()
            Err(VarargsError::InvalidLength {
                length: passed,
                expected,
            })
        }
    }

    /// Returns the type-converted value at the specified argument position.
    ///
    /// # Errors
    /// Returns a [`VarargsError::InvalidArgumentType`] if the conversion fails or the argument is not set.
    ///
    /// # Examples
    /// ```
    /// # fn call(args: gdnative::export::Varargs) -> Result<(), Box<dyn std::error::Error>> {
    ///     args.check_length(2)?;
    ///     let a: usize = args.get(0)?;
    ///     let rest: i64 = args.get(1)?;
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn get<T: FromVariant>(&self, index: usize) -> Result<T, VarargsError> {
        // Note: disregards iterator offset, since that representation is deprecated

        match self.args.get(index) {
            Some(v) => match T::from_variant(v) {
                Ok(ok) => Ok(ok),
                Err(error) => Err(VarargsError::InvalidArgumentType { index, error }),
            },
            None => {
                let error = FromVariantError::Custom("Argument is not set".to_owned());
                Err(VarargsError::InvalidArgumentType { index, error })
            }
        }
    }

    /// Returns the type-converted value at the specified argument position.
    /// Returns `None` if the argument is not set.
    ///
    /// # Errors
    /// Returns a [`VarargsError::InvalidArgumentType`] if the conversion fails.
    ///
    /// # Examples
    /// ```
    /// # fn call(args: gdnative::export::Varargs) -> Result<(), Box<dyn std::error::Error>> {
    ///     args.check_length(1..=2)?;
    ///     let a: usize = args.get(0)?;
    ///     let rest: i64 = args.get_opt(1)?.unwrap_or(72);
    /// # Ok(())
    /// # }
    /// ```
    #[inline]
    pub fn get_opt<T: FromVariant>(&self, index: usize) -> Result<Option<T>, VarargsError> {
        // Note: disregards iterator offset, since that representation is deprecated

        match self.args.get(index) {
            Some(v) => match T::from_variant(v) {
                Ok(ok) => Ok(Some(ok)),
                Err(error) => Err(VarargsError::InvalidArgumentType { index, error }),
            },
            None => Ok(None),
        }
    }
}

impl<'a> Iterator for Varargs<'a> {
    type Item = &'a Variant;
    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        let ret = self.args.get(self.idx);
        ret.map(|&v| {
            self.idx += 1;
            v
        })
    }
}

// Return a second token.
macro_rules! replace_expr {
    ($_t:tt $sub:expr) => {
        $sub
    };
}

// Count parameters.
macro_rules! count_tts {
    ($($tts:tt)*) => {
        0usize $(+ replace_expr!($tts 1usize))*
    };
}

// Convert from Varargs to tuples, implement traits.
macro_rules! varargs_into_tuple {
    ($($params:ident),*) => {
        impl<'a, $($params: FromVariant),*> std::convert::TryFrom<Varargs<'a>> for ($($params,)*) {
            type Error = VarargsError;

            #[inline]
            fn try_from(args: Varargs<'a>) -> Result<Self, Self::Error> {
                const EXPECTED: usize = count_tts!($($params)*);
                args.check_length(EXPECTED)?;
                let mut i: usize = 0;
                #[allow(unused_variables, unused_mut)]
                let mut inc = || {
                    let ret = i;
                    i += 1;
                    ret
                };
                Ok((
                    $(args.get::<$params>(inc())?,)*
                ))
            }
        }
    };
}

// Define up to the length supported by standard library.
varargs_into_tuple!();
varargs_into_tuple!(A);
varargs_into_tuple!(A, B);
varargs_into_tuple!(A, B, C);
varargs_into_tuple!(A, B, C, D);
varargs_into_tuple!(A, B, C, D, E);
varargs_into_tuple!(A, B, C, D, E, F);
varargs_into_tuple!(A, B, C, D, E, F, G);
varargs_into_tuple!(A, B, C, D, E, F, G, H);
varargs_into_tuple!(A, B, C, D, E, F, G, H, I);
varargs_into_tuple!(A, B, C, D, E, F, G, H, I, J);
varargs_into_tuple!(A, B, C, D, E, F, G, H, I, J, K);
varargs_into_tuple!(A, B, C, D, E, F, G, H, I, J, K, L);

/// All possible errors that can occur when converting from Varargs.
///
/// For methods that return this error, see [`Varargs::check_length()`], [`Varargs::get()`] or [`Varargs::get_opt()`].
/// Another context where this type is used is when destructuring `Varargs` into tuples.
#[derive(Debug)]
pub enum VarargsError {
    /// At least one argument type mismatches.
    InvalidArgumentType {
        index: usize,
        error: FromVariantError,
    },
    /// Number of arguments doesn't match expectations.
    InvalidLength {
        /// The number of arguments actually passed.
        length: usize,
        expected: IndexBounds,
    },
}

impl std::error::Error for VarargsError {}

impl fmt::Display for VarargsError {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            VarargsError::InvalidArgumentType { index, error } => {
                write!(f, "type error for argument #{index}: {error}")?
            }
            VarargsError::InvalidLength { expected, length } => write!(
                f,
                "length mismatch: expected range {expected}, actual {length}"
            )?,
        }

        Ok(())
    }
}

/// Defines which number of arguments is valid.
///
/// Convertible from an exact value `a` as well as inclusive range expressions `a..`, `..=b`, `a..=b`.
pub struct IndexBounds {
    /// The lower (inclusive) bound of the expected number of arguments, or `None` if unbounded.
    pub start: Option<usize>,

    /// The upper (inclusive) bound of the expected number of arguments, or `None` if unbounded.
    pub end: Option<usize>,
}

impl IndexBounds {
    #[inline]
    pub fn contains(&self, value: usize) -> bool {
        match (self.start, self.end) {
            (Some(s), Some(e)) => value >= s && value <= e,
            (Some(s), None) => value >= s,
            (None, Some(e)) => value <= e,
            (None, None) => false, // unreachable in this context, but can be constructed by user
        }
    }
}

/// `a`
impl From<usize> for IndexBounds {
    #[inline]
    fn from(exact_value: usize) -> Self {
        Self {
            start: Some(exact_value),
            end: Some(exact_value),
        }
    }
}

/// `a..=b`
impl From<ops::RangeInclusive<usize>> for IndexBounds {
    #[inline]
    fn from(range: ops::RangeInclusive<usize>) -> Self {
        Self {
            start: Some(*range.start()),
            end: Some(*range.end()),
        }
    }
}

/// `a..`
impl From<ops::RangeFrom<usize>> for IndexBounds {
    #[inline]
    fn from(range: ops::RangeFrom<usize>) -> Self {
        Self {
            start: Some(range.start),
            end: None,
        }
    }
}

/// `..=b`
impl From<ops::RangeToInclusive<usize>> for IndexBounds {
    #[inline]
    fn from(range: ops::RangeToInclusive<usize>) -> Self {
        Self {
            start: None,
            end: Some(range.end),
        }
    }
}

impl fmt::Debug for IndexBounds {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "IndexBounds({self})")
    }
}

impl fmt::Display for IndexBounds {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(start) = self.start {
            write!(f, "{start}")?
        }

        write!(f, "..=")?;

        if let Some(end) = self.end {
            write!(f, "{end}")?
        }

        Ok(())
    }
}

/// Trait for structures that can be parsed from `Varargs`.
///
/// This trait can be derived for structure types where each type implements `FromVariant`.
/// The order of fields matter for this purpose:
///
/// ```ignore
/// #[derive(FromVarargs)]
/// struct MyArgs {
///     foo: i32,
///     bar: String,
///     #[opt] baz: Option<Ref<Node>>,
/// }
/// ```
pub trait FromVarargs: Sized {
    fn read<'a>(args: &mut Varargs<'a>) -> Result<Self, Vec<ArgumentError<'a>>>;
}

/// Builder for providing additional argument information for error reporting.
pub struct ArgBuilder<'r, 'a, T> {
    args: &'r mut Varargs<'a>,
    name: Option<Cow<'a, str>>,
    ty: Option<Cow<'a, str>>,
    site: Option<Site<'a>>,
    _marker: PhantomData<T>,
}

impl<'r, 'a, T> ArgBuilder<'r, 'a, T> {
    /// Provides a name for this argument. If an old name is already set, it is
    /// silently replaced. The name can either be borrowed from the environment
    /// or owned.
    #[inline]
    pub fn with_name<S: Into<Cow<'a, str>>>(mut self, name: S) -> Self {
        self.name = Some(name.into());
        self
    }

    /// Provides a more readable type name for this argument. If an old name is
    /// already set, it is silently replaced. If no type name is given, a value
    /// from `std::any::type_name` is used. The name can either be borrowed from
    /// the environment or owned.
    #[inline]
    pub fn with_type_name<S: Into<Cow<'a, str>>>(mut self, ty: S) -> Self {
        self.ty = Some(ty.into());
        self
    }

    /// Provides a call site for this argument. If an old call site is already set,
    /// it is silently replaced. If given, the site will be used in case of error.
    #[inline]
    pub fn with_site(mut self, site: Site<'a>) -> Self {
        self.site = Some(site);
        self
    }
}

impl<'r, 'a, T: FromVariant> ArgBuilder<'r, 'a, T> {
    /// Get the converted argument value.
    ///
    /// # Errors
    ///
    /// If the argument is missing, or cannot be converted to the desired type.
    #[inline]
    pub fn get(mut self) -> Result<T, ArgumentError<'a>> {
        self.get_optional_internal().and_then(|arg| {
            let actual_index = self.args.idx + self.args.offset_index;
            arg.ok_or(ArgumentError {
                site: self.site,
                kind: ArgumentErrorKind::Missing {
                    idx: actual_index,
                    name: self.name,
                },
            })
        })
    }

    /// Get the argument as optional.
    ///
    /// # Errors
    ///
    /// If the argument is present, but cannot be converted to the desired type.
    #[inline]
    pub fn get_optional(mut self) -> Result<Option<T>, ArgumentError<'a>> {
        self.get_optional_internal()
    }

    fn get_optional_internal(&mut self) -> Result<Option<T>, ArgumentError<'a>> {
        let Self {
            site,
            args,
            name,
            ty,
            ..
        } = self;
        let actual_index = args.idx + args.offset_index;

        if let Some(arg) = args.next() {
            T::from_variant(arg).map(Some).map_err(|err| ArgumentError {
                site: *site,
                kind: ArgumentErrorKind::CannotConvert {
                    idx: actual_index,
                    name: name.take(),
                    value: arg,
                    ty: ty
                        .take()
                        .unwrap_or_else(|| Cow::Borrowed(std::any::type_name::<T>())),
                    err,
                },
            })
        } else {
            Ok(None)
        }
    }
}

/// Error during argument parsing.
#[derive(Debug)]
pub struct ArgumentError<'a> {
    site: Option<Site<'a>>,
    kind: ArgumentErrorKind<'a>,
}

impl<'a> std::error::Error for ArgumentError<'a> {}
impl<'a> fmt::Display for ArgumentError<'a> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if let Some(site) = &self.site {
            write!(f, "at {site}: ")?;
        }
        write!(f, "{}", self.kind)
    }
}

impl<'a> ArgumentError<'a> {
    /// Assign a call site for this error. If an old one is already set, it is silently
    /// replaced.
    #[inline]
    pub fn with_site(mut self, site: Site<'a>) -> Self {
        self.site = Some(site);
        self
    }

    /// Print this error in the Godot debug console as a warning.
    ///
    /// # Panics
    ///
    /// If the API isn't initialized.
    #[inline]
    pub fn log_warn(&self) {
        crate::log::warn(self.site.unwrap_or_default(), &self.kind);
    }

    /// Print this error in the Godot debug console as an error.
    ///
    /// # Panics
    ///
    /// If the API isn't initialized.
    #[inline]
    pub fn log_error(&self) {
        crate::log::error(self.site.unwrap_or_default(), &self.kind);
    }
}

/// Error during argument parsing.
#[derive(Debug)]
enum ArgumentErrorKind<'a> {
    Missing {
        idx: usize,
        name: Option<Cow<'a, str>>,
    },
    CannotConvert {
        idx: usize,
        name: Option<Cow<'a, str>>,
        ty: Cow<'a, str>,
        value: &'a Variant,
        err: FromVariantError,
    },
    ExcessArguments {
        rest: &'a [&'a Variant],
    },
}

impl<'a> std::error::Error for ArgumentErrorKind<'a> {}

impl<'a> fmt::Display for ArgumentErrorKind<'a> {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use ArgumentErrorKind as E;

        match self {
            E::Missing {
                idx,
                name: Some(name),
            } => {
                write!(f, "missing non-optional parameter `{name}` (#{idx})")
            }
            E::Missing { idx, name: None } => {
                write!(f, "missing non-optional parameter #{idx}")
            }
            E::CannotConvert {
                idx,
                name: Some(name),
                value,
                ty,
                err,
            } => {
                write!(f,
                    "cannot convert argument `{name}` (#{idx}, {value:?}) to {ty}: {err} (non-primitive types may impose structural checks)"
                )
            }
            E::CannotConvert {
                idx,
                name: None,
                value,
                ty,
                err,
            } => {
                write!(f,
                    "cannot convert argument #{idx} ({value:?}) to {ty}: {err} (non-primitive types may impose structural checks)"
                )
            }
            E::ExcessArguments { rest } => {
                if rest.len() > 1 {
                    write!(
                        f,
                        "{} excessive arguments are given: {:?}",
                        rest.len(),
                        rest
                    )
                } else {
                    write!(f, "an excessive argument is given: {:?}", rest[0])
                }
            }
        }
    }
}

unsafe extern "C" fn method_wrapper<C: NativeClass, F: Method<C>>(
    this: *mut sys::godot_object,
    method_data: *mut libc::c_void,
    user_data: *mut libc::c_void,
    num_args: libc::c_int,
    args: *mut *mut sys::godot_variant,
) -> sys::godot_variant {
    if user_data.is_null() {
        crate::log::error(
            F::site().unwrap_or_default(),
            format_args!(
                "gdnative-core: user data pointer for {} is null (did the constructor fail?)",
                class_registry::class_name_or_default::<C>(),
            ),
        );
        return Variant::nil().leak();
    }

    let this = match std::ptr::NonNull::new(this) {
        Some(this) => this,
        None => {
            crate::log::error(
                F::site().unwrap_or_default(),
                format_args!(
                    "gdnative-core: base object pointer for {} is null (probably a bug in Godot)",
                    class_registry::class_name_or_default::<C>(),
                ),
            );
            return Variant::nil().leak();
        }
    };

    let result = std::panic::catch_unwind(move || {
        let method = &*(method_data as *const F);

        let this: Ref<C::Base, Shared> = Ref::from_sys(this);
        let this: TRef<'_, C::Base, _> = this.assume_safe_unchecked();
        let this: TInstance<'_, C, _> = TInstance::from_raw_unchecked(this, user_data);

        let args = Varargs::from_sys(num_args, args);

        F::call(method, this, args)
    });

    result
        .unwrap_or_else(|e| {
            crate::log::error(
                F::site().unwrap_or_default(),
                "gdnative-core: method panicked (check stderr for output)",
            );
            crate::private::print_panic_error(e);
            Variant::nil()
        })
        .leak()
}

unsafe extern "C" fn free_func<F>(method_data: *mut libc::c_void) {
    drop(Box::from_raw(method_data as *mut F))
}