rrplug 4.2.1

framework for R2Northstar plugins
Documentation
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
//! trait definitions and implementations to generalize interacting with squirrel

#![allow(clippy::not_unsafe_ptr_arg_deref)] // maybe remove this later

pub use rrplug_proc::{GetFromSQObject, GetFromSquirrelVm, PushToSquirrelVm, SQVMName};
use std::{mem::MaybeUninit, ptr::NonNull};

use crate::{
    bindings::{
        class_types::{cbaseentity::CBaseEntity, cplayer::CPlayer},
        squirrelclasstypes::SQRESULT,
        squirreldatatypes::{
            SQArray, SQBool, SQClosure, SQFloat, SQFunctionProto, SQInteger, SQNativeClosure,
            SQObject, SQObjectType, SQObjectValue, SQString, SQStructInstance, SQTable,
        },
    },
    high::squirrel::SQHandle,
    mid::{
        squirrel::{
            get_sq_array, get_sq_bool, get_sq_float, get_sq_int, get_sq_object, get_sq_string,
            get_sq_vector, push_sq_array, push_sq_bool, push_sq_float, push_sq_int, push_sq_object,
            push_sq_string, push_sq_vector, sqvm_to_context,
        },
        utils::to_cstring,
    },
    prelude::*,
};

use super::UnsafeHandle;

// Push Trait

macro_rules! push_to_sqvm {
    ( $( $function:ident::<$t:ty> );*; ) => { $(

        impl PushToSquirrelVm for $t {
            #[inline]
            fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
                $function(sqvm, sqfunctions, self)
            }
        }
    )* }
}

/// trait to used to generalize pushing to the sq stack
///
/// # Use cases
/// - returning from native functions
/// - accumulating in arrays and structs
pub trait PushToSquirrelVm {
    /// used for ()
    #[doc(hidden)]
    const DEFAULT_RESULT: SQRESULT = SQRESULT::SQRESULT_NOTNULL;

    /// pushes the value to the stack
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions);
}

push_to_sqvm! {
    push_sq_string::<String>;
    push_sq_string::<&str>;
    push_sq_int::<i32>;
    push_sq_float::<f32>;
    push_sq_bool::<bool>;
    push_sq_vector::<Vector3>;
    push_sq_object::<SQObject>;
}

impl<T> PushToSquirrelVm for Vec<T>
where
    T: PushToSquirrelVm,
{
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
        push_sq_array(sqvm, sqfunctions, self);
    }
}

impl PushToSquirrelVm for () {
    const DEFAULT_RESULT: SQRESULT = SQRESULT::SQRESULT_NULL;

    #[inline]
    fn push_to_sqvm(self, _: NonNull<HSquirrelVM>, _: &SquirrelFunctions) {}
}

impl PushToSquirrelVm for &CPlayer {
    /// SAFETY: the object is stored inside the entity and the entity is not being modified  
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
        unsafe {
            let obj =
                (sqfunctions.sq_create_script_instance)((self as *const CPlayer).cast_mut().cast());
            (sqfunctions.sq_pushobject)(sqvm.as_ptr(), obj);
        }
    }
}

impl PushToSquirrelVm for &CBaseEntity {
    /// SAFETY: the object is stored inside the entity and the entity is not being modified  
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
        unsafe {
            let obj = (sqfunctions.sq_create_script_instance)(
                (self as *const CBaseEntity).cast_mut().cast(),
            );
            (sqfunctions.sq_pushobject)(sqvm.as_ptr(), obj);
        }
    }
}

impl<T: PushToSquirrelVm, const N: usize> PushToSquirrelVm for [T; N] {
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
        push_sq_array(sqvm, sqfunctions, self);
    }
}

impl<T: PushToSquirrelVm> PushToSquirrelVm for UnsafeHandle<T>
where
    T: PushToSquirrelVm,
{
    fn push_to_sqvm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) {
        self.take().push_to_sqvm(sqvm, sqfunctions)
    }
}

// Return Trait

/// trait to return diffrent values to the sqvm from a native closure
///
/// [`Option`] will return a `ornull` type in squirrel
///
/// [`Result`] will return its T type in squirrel and will raise an exception in squirrel if it's an error
/// # Use cases
/// - returning from native functions
pub trait ReturnToVm {
    /// returns a value defined by [`SQRESULT`]
    fn return_to_vm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) -> SQRESULT;
}

impl<T: PushToSquirrelVm> ReturnToVm for Option<T> {
    /// returns a `ornull T` to the sqvm
    fn return_to_vm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) -> SQRESULT {
        match self {
            Some(rtrn) => {
                rtrn.push_to_sqvm(sqvm, sqfunctions);
                T::DEFAULT_RESULT
            }
            None => {
                unsafe { (sqfunctions.sq_pushnull)(sqvm.as_ptr()) };
                SQRESULT::SQRESULT_NULL
            }
        }
    }
}

impl<T: PushToSquirrelVm, E: ToString> ReturnToVm for Result<T, E> {
    /// will raise a squirrel exception if it's an error
    ///
    /// result returns of T,R are identical to non result returns of T
    fn return_to_vm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) -> SQRESULT {
        match self {
            Ok(rtrn) => {
                rtrn.push_to_sqvm(sqvm, sqfunctions);
                T::DEFAULT_RESULT
            }
            Err(err) => {
                let err = to_cstring(err.to_string().as_str());
                unsafe { (sqfunctions.sq_raiseerror)(sqvm.as_ptr(), err.as_ptr()) };
                SQRESULT::SQRESULT_ERROR
            }
        }
    }
}

impl<T: PushToSquirrelVm> ReturnToVm for T {
    /// any return for types simply pushes it and returns NonNull
    fn return_to_vm(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &SquirrelFunctions) -> SQRESULT {
        self.push_to_sqvm(sqvm, sqfunctions);
        T::DEFAULT_RESULT
    }
}

// IntoSquirrelArgs Trait

/// closure that simplies pushing groups of items to the squirrel vm; asynchronously or immediately
pub trait IntoSquirrelArgs {
    /// converts a implemenator of this trait into a closure that pushes it to the squirrel stack when ran
    fn into_function(
        self,
    ) -> Box<
        dyn FnOnce(NonNull<HSquirrelVM>, &'static SquirrelFunctions) -> i32 + 'static + Send + Sync,
    >
    where
        Self: Sized + Send + Sync + 'static,
    {
        Box::new(
            move |sqvm: NonNull<HSquirrelVM>, sqfunctions: &'static SquirrelFunctions| {
                self.into_push(sqvm, sqfunctions)
            },
        )
    }

    /// pushes the args to the sqvm and returns the amount pushed
    fn into_push(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &'static SquirrelFunctions) -> i32;
}

impl<T: PushToSquirrelVm> IntoSquirrelArgs for T {
    fn into_push(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &'static SquirrelFunctions) -> i32 {
        // hack :(
        // no specialization
        if T::DEFAULT_RESULT != SQRESULT::SQRESULT_NULL {
            self.push_to_sqvm(sqvm, sqfunctions);
            1
        } else {
            0
        }
    }
}

// TODO: check for correctness
macro_rules! into_squirrel_args_impl{
    ( $( ($($ty_name: ident : $tuple_index:tt),*) );*; ) => { $(
        impl<$($ty_name: PushToSquirrelVm,)*> IntoSquirrelArgs for ($($ty_name,)*) {
            fn into_push(self, sqvm: NonNull<HSquirrelVM>, sqfunctions: &'static SquirrelFunctions) -> i32 {
                $(
                    self.$tuple_index.push_to_sqvm(sqvm, sqfunctions);
                )*
                $crate::macros::sq_utils::__arg_count_helper([$($crate::__replace_expr!($ty_name)),*]) as i32
            }
        }
    )* }
}

into_squirrel_args_impl! {
    (T1: 0);
    (T1: 0, T2: 1);
    (T1: 0, T2: 1, T3: 2);
    (T1: 0, T2: 1, T3: 2, T4: 3);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8);
    (T1: 0, T2: 1, T3: 2, T4: 3, T5: 4, T6: 5, T7: 6, T8: 7, T9: 8, T10: 9);
}

// Get Trait

macro_rules! get_from_sqvm {
    ( $( $function:ident::<$t:ty> );*; ) => { $(

        impl GetFromSquirrelVm for $t {
            #[inline]
            fn get_from_sqvm(
                sqvm: NonNull<HSquirrelVM>,
                sqfunctions: &SquirrelFunctions,
                stack_pos: i32,
            ) -> Self {
                $function(sqvm, sqfunctions, stack_pos)
            }
        }
    )* };

    ( $( ($($ty_name: ident : $var_name:ident),*) );*; ) => { $(
        impl<$($ty_name: PushToSquirrelVm,)*> GetFromSquirrelVm for Box<dyn Fn($($ty_name,)*)> {
            fn get_from_sqvm(
                sqvm: NonNull<HSquirrelVM>,
                sqfunctions: &'static SquirrelFunctions,
                stack_pos: i32,
            ) -> Self {
                Box::new(move |$($var_name: $ty_name,)*| { _ =
                    call_sq_object_function!(
                        sqvm,
                        sqfunctions,
                        SQHandle::<SQClosure>::get_from_sqvm(sqvm, sqfunctions, stack_pos),
                        $($var_name),*
                    );
                })
            }
        }
    )* }
}

/// trait to get values out of the squrriel stack
///
/// # Use cases
/// - getting the arguments in native closures
pub trait GetFromSquirrelVm: Sized {
    /// tries to get the value out of the squirrel stack but it cannot fail
    /// so this can panic
    ///
    /// this is the user function do not overwrite the other one
    fn get_from_sqvm(
        sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &'static SquirrelFunctions,
        stack_pos: i32,
    ) -> Self;

    /// this is only for certain internal apis
    ///
    /// don't use this only for internal apis
    #[doc(hidden)]
    #[inline]
    fn get_from_sqvm_internal(
        sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &'static SquirrelFunctions,
        stack_pos: &mut i32,
    ) -> Self {
        let s = Self::get_from_sqvm(sqvm, sqfunctions, *stack_pos);

        // increament by the size this thing in the stack
        // for some reason userdata also has a table pushed with it; quite annoying
        *stack_pos += 1;

        s
    }
}

get_from_sqvm! {
    get_sq_string::<String>;
    get_sq_int::<i32>;
    get_sq_float::<f32>;
    get_sq_bool::<bool>;
    get_sq_vector::<Vector3>;
    get_sq_object::<SQObject>;
}

impl<T> GetFromSquirrelVm for Vec<T>
where
    T: GetFromSQObject,
{
    fn get_from_sqvm(
        sqvm: NonNull<HSquirrelVM>,
        _: &'static SquirrelFunctions,
        stack_pos: i32,
    ) -> Self {
        get_sq_array(sqvm, stack_pos)
    }
}

impl GetFromSquirrelVm for Option<&mut CPlayer> {
    fn get_from_sqvm(
        mut sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &SquirrelFunctions,
        stack_pos: i32,
    ) -> Self {
        unsafe {
            debug_assert_eq!(
                sqvm_to_context(sqvm),
                ScriptContext::SERVER,
                "CPlayer only exists on server vm use C_Player for CLIENT and UI"
            );

            let sqvm = sqvm.as_mut();
            let cs_sqvm = sqvm
                .sharedState
                .as_ref()
                .expect("shared state was invalid")
                .cSquirrelVM;

            let mut obj = MaybeUninit::<SQObject>::uninit();
            (sqfunctions.sq_getobject)(sqvm, stack_pos, obj.as_mut_ptr());

            (sqfunctions.sq_getentityfrominstance)(
                cs_sqvm,
                obj.as_mut_ptr(),
                (sqfunctions.sq_get_entity_constant_cbase_entity)(),
            )
            .cast::<CBaseEntity>()
            .as_mut()?
            .dynamic_cast_mut()
        }
    }
}

impl GetFromSquirrelVm for Option<&mut CBaseEntity> {
    fn get_from_sqvm(
        mut sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &SquirrelFunctions,
        stack_pos: i32,
    ) -> Self {
        unsafe {
            debug_assert_eq!(
                sqvm_to_context(sqvm),
                ScriptContext::SERVER,
                "CBaseEnity only exists on server vm"
            );

            let sqvm = sqvm.as_mut();
            let cs_sqvm = sqvm
                .sharedState
                .as_ref()
                .expect("shared state was invalid")
                .cSquirrelVM;

            let mut obj = MaybeUninit::<SQObject>::uninit();
            (sqfunctions.sq_getobject)(sqvm, stack_pos, obj.as_mut_ptr());

            let ent = (sqfunctions.sq_getentityfrominstance)(
                cs_sqvm,
                obj.as_mut_ptr(),
                (sqfunctions.sq_get_entity_constant_cbase_entity)(),
            )
            .cast::<CBaseEntity>()
            .as_mut()?;

            Some(ent)
        }
    }
}

impl<'a, T: IsSQObject<'a>> GetFromSquirrelVm for SQHandle<'a, T> {
    fn get_from_sqvm(
        sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &SquirrelFunctions,
        stack_pos: i32,
    ) -> Self {
        unsafe {
            let mut obj = std::mem::MaybeUninit::<SQObject>::uninit();
            (sqfunctions.sq_getobject)(sqvm.as_ptr(), stack_pos, obj.as_mut_ptr());

            match Self::try_new(obj.assume_init()) {
                Ok(handle) => handle,
                Err(_) => {
                    panic!(
                        "the object wasn't the correct type got {:X} expected {}",
                        obj.assume_init()._Type as i32,
                        std::any::type_name::<T>()
                    );
                }
            }
        }
    }
}

impl<T: IntoSquirrelArgs> GetFromSquirrelVm for SquirrelFn<'_, T> {
    #[inline]
    fn get_from_sqvm(
        sqvm: NonNull<HSquirrelVM>,
        sqfunctions: &'static SquirrelFunctions,
        stack_pos: i32,
    ) -> Self {
        SquirrelFn {
            func: GetFromSquirrelVm::get_from_sqvm(sqvm, sqfunctions, stack_pos),
            phantom: std::marker::PhantomData,
        }
    }
}

impl GetFromSquirrelVm for () {
    /// exists for dynamic returns of some functions
    fn get_from_sqvm(_: NonNull<HSquirrelVM>, _: &SquirrelFunctions, _: i32) -> Self {}
}

// Get From SQObject Trait

/// gets the value out of a sqobject
///
/// most implementations don't check the type
///
/// so this can panic if it's not the correct type
///
/// # Use cases
/// - getting fields of arrays and structs
pub trait GetFromSQObject {
    /// gets the value out of a sqobject
    ///
    /// halts if the type is incorrect
    fn get_from_sqobject(obj: &SQObject) -> Self;
}

impl GetFromSQObject for () {
    #[inline]
    fn get_from_sqobject(_: &SQObject) -> Self {}
}

impl GetFromSQObject for String {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        unsafe {
            std::ffi::CStr::from_ptr(
                (&obj._VAL.asString.as_ref().unwrap_unchecked()._val) as *const i8,
            )
            .to_string_lossy()
            .into()
        }
    }
}

impl GetFromSQObject for i32 {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        unsafe { obj._VAL.asInteger }
    }
}

impl GetFromSQObject for f32 {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        unsafe { obj._VAL.asFloat }
    }
}

impl GetFromSQObject for bool {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        unsafe { obj._VAL.asInteger != 0 }
    }
}

impl GetFromSQObject for Vector3 {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        (obj as *const SQObject).into()
    }
}

impl GetFromSQObject for SQObject {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        *obj
    }
}

impl<'a, T: IsSQObject<'a>> GetFromSQObject for SQHandle<'a, T> {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        match Self::try_new(*obj) {
            Ok(handle) => handle,
            Err(_) => {
                panic!(
                    "the object wasn't the correct type got {:X} expected {}",
                    obj._Type as i32,
                    std::any::type_name::<T>()
                );
            }
        }
    }
}

impl<T: IntoSquirrelArgs> GetFromSQObject for SquirrelFn<'_, T> {
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        SquirrelFn {
            func: SQHandle::try_new(obj.to_owned())
                .expect("the squirrel object wasn't a function lol L"),
            phantom: std::marker::PhantomData,
        }
    }
}

impl<T> GetFromSQObject for Vec<T>
where
    T: GetFromSQObject,
{
    #[inline]
    fn get_from_sqobject(obj: &SQObject) -> Self {
        unsafe {
            let array = obj
                ._VAL
                .asArray
                .as_ref()
                .expect("the sq object may be invalid");

            (0..array._usedSlots as usize)
                .map(|i| array._values.add(i))
                .filter_map(|obj| obj.as_ref())
                .map(T::get_from_sqobject)
                .collect()
        }
    }
}

// sqvm name

macro_rules! sqvm_name {
    ($( ($($ty_name:ident : $var_name:ident),*) );*;)  => {
        $(
            impl<$($ty_name: SQVMName,)*> SQVMName for ($($ty_name,)*) {
                fn get_sqvm_name() -> String {
                    let mut name = String::new();

                    $(
                        if !name.is_empty() { // bad solution but this will run only once for each use
                            name.push(',');
                            name.push(' ');
                        }
                        name.push_str(&$ty_name::get_sqvm_name());
                    )*

                    name
                }
            }
        )*
    };

    ( $( $t:ty = $sqty:literal );*; ) => {
        $(
            impl SQVMName for $t {
                #[inline]
                fn get_sqvm_name() -> String {
                     $sqty.to_string()
                }
            }
        )*
    };

    ( $( LIFE $t:ty = $sqty:literal );*; ) => {
        $(
            impl<'a> SQVMName for $t {
                #[inline]
                fn get_sqvm_name() -> String {
                     $sqty.to_string()
                }
            }
        )*
    };
}

/// the sqvm name of a type in rust
///
/// used to map a rust function into a sq native function
///
/// # Use cases
/// - translating rust types to squirrel types
pub trait SQVMName {
    /// the name on the sqvm of a type
    ///
    /// the default is "var" which is any type
    fn get_sqvm_name() -> String;
}

sqvm_name! {
    String = "string";
    &str = "string";
    i32 = "int";
    f32 = "float";
    bool = "bool";
    Vector3 = "vector";
    Option<&mut CPlayer> = "entity";
    Option<&mut CBaseEntity> = "entity";
    SQObject = "var";
    () = "void";
}

sqvm_name! {
    LIFE SQHandle<'a, SQClosure> = "var";
    LIFE SQHandle<'a, SQTable> = "table";
    LIFE SQHandle<'a, SQString> = "string";
    LIFE SQHandle<'a, SQArray> = "array";
    LIFE SQHandle<'a, SQFloat> = "float";
    LIFE SQHandle<'a, SQInteger> = "int";
    LIFE SQHandle<'a, SQFunctionProto> = "var";
    LIFE SQHandle<'a, SQStructInstance> = "var";
    LIFE SQHandle<'a, SQBool> = "bool";
    LIFE SQHandle<'a, SQNativeClosure> = "var";
}

sqvm_name! {
    (T1: v2);
    (T1: v1, T2: v2);
    (T1: v1, T2: v2, T3: v3);
    (T1: v1, T2: v2, T3: v3, T4: v4);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5, T6: v6);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5, T6: v6, T7: v7);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5, T6: v6, T7: v7, T8: v8);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5, T6: v6, T7: v7, T8: v8, T9: v9);
    (T1: v1, T2: v2, T3: v3, T4: v4, T5: v5, T6: v6, T7: v7, T8: v8, T9: v9, T10: v10);
}

impl<T: SQVMName + IntoSquirrelArgs> SQVMName for SquirrelFn<'_, T> {
    fn get_sqvm_name() -> String {
        format!("void functionref({})", T::get_sqvm_name())
    }
}

impl<T: SQVMName> SQVMName for Vec<T> {
    fn get_sqvm_name() -> String {
        format!("array<{}>", T::get_sqvm_name())
    }
}

// because of this `void ornull` is possible oops
impl<T: SQVMName> SQVMName for Option<T> {
    fn get_sqvm_name() -> String {
        format!("{} ornull", T::get_sqvm_name())
    }
}

impl<T: SQVMName, E> SQVMName for Result<T, E> {
    fn get_sqvm_name() -> String {
        T::get_sqvm_name() // yeah squirrel doesn't have a way in the type system to sepecify a possible error :|
    }
}

// specialization is not as strong as I though :(
// impl SQVMName for Option<()> {
//     fn get_sqvm_name() -> String {
//         "void".to_string()
//     }
// }

// Markers

macro_rules! is_sq_object {
    ( $( $object:ty,RT: $rt:expr,OT: $ot:expr, EXTRACT: * $extract:ident );*; ) => {
        $(
            impl<'a> IsSQObject<'a> for $object {
                const OT_TYPE: SQObjectType = $ot;
                const RT_TYPE: SQObjectType = $rt;

                fn extract_mut(val: &'a mut SQObjectValue) -> &'a mut Self {
                    unsafe { &mut *val.$extract } // asummed to be init
                }

                fn extract(val: &'a SQObjectValue) -> &'a Self {
                    unsafe { &*val.$extract } // asummed to be init
                }
            }
        )*
    };

    ( $( $object:ty,RT: $rt:expr,OT: $ot:expr, EXTRACT: $extract:ident );*; ) => {
        $(
            impl<'a> IsSQObject<'a> for $object {
                const OT_TYPE: SQObjectType = $ot;
                const RT_TYPE: SQObjectType = $rt;

                fn extract_mut(val: &'a mut SQObjectValue) -> &'a mut Self {
                    unsafe { std::mem::transmute(&mut val.$extract) } // asummed to be init
                }

                fn extract(val: &'a SQObjectValue) -> &'a Self {
                    unsafe { std::mem::transmute(&val.$extract) } // asummed to be init
                }
            }
        )*
    }
}

/// trait to define SQObject types
pub trait IsSQObject<'a> {
    /// ot type
    const OT_TYPE: SQObjectType;
    /// return type
    const RT_TYPE: SQObjectType;

    /// extracts the `Self` out of the SQObjectValue
    ///
    /// this is unsafe if [`SQHandle`] wasn't used
    fn extract(val: &'a SQObjectValue) -> &'a Self;

    /// extracts the `Self` out of the SQObjectValue
    ///
    /// this is unsafe if [`SQHandle`] wasn't used
    fn extract_mut(val: &'a mut SQObjectValue) -> &'a mut Self;
}

is_sq_object! {
    SQTable, RT: SQObjectType::RT_TABLE, OT: SQObjectType::OT_TABLE, EXTRACT: * asTable;
    SQString, RT: SQObjectType::RT_STRING, OT: SQObjectType::OT_STRING, EXTRACT: * asString;
    SQFunctionProto, RT: SQObjectType::RT_FUNCPROTO, OT: SQObjectType::OT_FUNCPROTO, EXTRACT: * asFuncProto;
    SQClosure, RT: SQObjectType::RT_CLOSURE, OT: SQObjectType::OT_CLOSURE, EXTRACT: * asClosure;
    SQStructInstance, RT: SQObjectType::RT_INSTANCE, OT: SQObjectType::OT_INSTANCE, EXTRACT: * asStructInstance;
    SQNativeClosure, RT: SQObjectType::RT_NATIVECLOSURE, OT: SQObjectType::OT_NATIVECLOSURE, EXTRACT: * asNativeClosure;
    SQArray, RT: SQObjectType::RT_ARRAY, OT: SQObjectType::OT_ARRAY, EXTRACT: * asArray;
}
is_sq_object! {
    SQFloat, RT: SQObjectType::RT_FLOAT, OT: SQObjectType::OT_FLOAT, EXTRACT: asFloat;
    SQInteger, RT: SQObjectType::RT_INTEGER, OT: SQObjectType::OT_INTEGER, EXTRACT: asInteger;
    SQBool, RT: SQObjectType::RT_BOOL, OT: SQObjectType::OT_BOOL, EXTRACT: asInteger;
} // not a thing? SQStructDef, RT: SQObjectType::, OT: SQObjectType::;

// TODO: so here is the idea
// have add_sqfunction be generic over extern "C" fn s and have traits to diffrenciate client/server/ui sqfunctions
// the generic would cover mutitple implementation
// but with this version the user would have to specifically ask for a sqvm and sqfunctions
// now that I writing this the biggest problem is the return ...
// but since it's a int we could have a C struct with a i32 and it would be transparent
// this would allow the user to return anything that can become that sturct
// so this is figured out :)
// also the input could be generic over *mut sqvm
// but then it would have to be a tuple :pain:
// maybe a combination of this and proc macro would be better?

// TODO: another thing to think about is the fact that there 5 traits for interacting with the sqvm
// they are all required for everything so why not just combine most of them into one large trait