pixelscript 0.5.9

Multi language scripting runtime
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
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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
// Copyright 2026 Jordan Castro <jordan@grupojvm.com>
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
//
use std::{
    cell::Cell,
    collections::HashMap,
    ffi::{CStr, CString, c_char, c_void},
    hash::Hash,
    ptr,
};

use anyhow::{Error, anyhow};

use crate::{
    borrow_string, create_raw_string,
    shared::{PtrMagic, object::{apply_ref_count_alloc, apply_ref_count_delete, get_object}, pxs_Runtime},
};

/// Macro for writing out the Var:: get methods.
macro_rules! write_func {
    ($ (($func_name:ident, $field_name:ident, $ret_type:ty, $tag_variant:path) ),* $(,)?) => {
        $(
            #[doc = concat!("Returns the ", stringify!($ret_type), " value if the tag is ", stringify!($tag_variant), ".")]
            pub fn $func_name(&self) -> Result<$ret_type, Error> {
                if self.tag == $tag_variant {
                    unsafe {
                        Ok(self.value.$field_name)
                    }
                } else {
                    Err(anyhow!("Var is not the expected type of {:#?}. It is instead a: {:#?}", $tag_variant, self.tag))
                }
            }
        )*
    };
}

/// Macro for writing out the new_t methods in Var
macro_rules! write_new_methods {
    ($($t:ty, $func:ident, $vt:expr, $vn:ident);*) => {
        $(
            pub fn $func(val:$t) -> Self {
                Self {
                    tag: $vt,
                    value: pxs_VarValue { $vn: val },
                    deleter: Cell::new(default_deleter)
                }
            }
        )*
    };
}

/// Macro for writing out the is_t methods in Var
macro_rules! write_is_methods {
    ($($func:ident, $vt:expr);*) => {
        $(
            pub fn $func(&self) -> bool {
                self.tag == $vt
            }
        )*
    };
}

// // Macro for writing out the FromVars
// macro_rules! implement_from_var {
//     ($($t:ty, $func:ident);*) => {
//         $(
//             impl FromVar for $t {
//                 fn from_var(var:&Var) -> Result<Self, Error> {
//                     var.$func()
//                 }
//             }
//         )*
//     };
// }

/// This represents the variable type that is being read or created.
#[repr(C)]
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(non_camel_case_types)]
pub enum pxs_VarType {
    pxs_Int64,
    pxs_UInt64,
    pxs_String,
    pxs_Bool,
    pxs_Float64,
    /// Lua (nil), Python (None), JS/easyjs (null/undefined)
    pxs_Null,
    /// Lua (Tree), Python (Class), JS/easyjs (Prototype)
    pxs_Object,
    /// Host object converted when created.
    /// Lua (Tree), Python (object), JS/easyjs (Prototype think '{}')
    pxs_HostObject,
    /// Lua (Tree), Python (list), JS/easyjs (Array)
    pxs_List,
    /// Lua (Value), Python (def or lambda), JS/easyjs (anon function)
    pxs_Function,
    /// Internal object only. It will get converted into the result before hitting the runtime
    pxs_Factory,
    /// Exception is any exception happening at the language level. Pixel Script errors will be caught with pxs_Error in a future release
    pxs_Exception,
    /// A Map Type that ONLY goes from PixelScript to scripting language. You will NEVER receive a Map from a scripting language. It will
    /// always default to `pxs_Object`. Does not support all `pxs_VarType`s.
    pxs_Map,
}

/// A `Object` in pixelscript is wrapped with a potential host_ptr. This allows for non language specific ref counting.
/// 
/// To access the raw pointer, use `get_raw()`. Reference counting is automatically applied when this struct is dropped.
#[allow(non_camel_case_types)]
pub struct pxs_VarObject {
    object_val: *mut c_void,
    host_ptr: i32
}

impl PtrMagic for pxs_VarObject {}

impl pxs_VarObject {
    pub fn new(val: *mut c_void, host_ptr: i32) -> Self {
        Self {
            object_val: val,
            host_ptr: host_ptr
        }
    }

    /// Get the raw object pointer.
    pub fn get_raw(&self) -> *mut c_void {
        self.object_val
    }

    /// New HostObject Object
    pub fn new_as_host(val: *mut c_void, host_ptr: i32) -> Self {
        Self::new(val, host_ptr)
    }

    /// New Non host object.
    pub fn new_lang_only(val: *mut c_void) -> Self {
        Self::new(val, -1)
    }
}

impl Clone for pxs_VarObject {
    fn clone(&self) -> Self {
        // Do ref counting
        if self.host_ptr != -1 {
            apply_ref_count_alloc(self.host_ptr);
        }

        Self {
            object_val: self.object_val,
            host_ptr: self.host_ptr
        }
    }
}

impl Drop for pxs_VarObject {
    fn drop(&mut self) {
        if self.host_ptr == -1 {
            return;
        }

        // Ref counting
        apply_ref_count_delete(self.host_ptr);
    }
}

/// A `Map` in pixelscript is very simply a Key (pxs_Var) to Value (pxs_Var) pair.
/// 
/// In Python it's a dictionary, in Lua it's a table, and in JS it's a object.
#[allow(non_camel_case_types)]
pub struct pxs_VarMap {
    /// Key of pxs_Var => value of pxs_Var.
    map: HashMap<pxs_Var, pxs_Var>,
}

impl PtrMagic for pxs_VarMap {}

impl pxs_VarMap {
    /// A new map
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    /// Add a new item.
    ///
    /// Old value (if any) gets dropped.
    pub fn add_item(&mut self, key: pxs_Var, value: pxs_Var) {
        // Drop old value.
        let _ = self.map.insert(key, value);
    }

    /// Remove a item by key.
    /// 
    /// Old value gets dropped.
    pub fn del_item(&mut self, key: &pxs_Var) {
        let _ = self.map.remove(key);
    }

    /// Current length of map
    pub fn len(&self) -> usize {
        self.map.len()
    }

    /// Get value from key.
    pub fn get_item(&self, key: &pxs_Var) -> Option<&pxs_Var> {
        self.map.get(key)
    }

    /// Get keys
    pub fn keys(&self) -> Vec<&pxs_Var> {
        self.map.keys().collect()
    }
}

/// A Factory variable data holder.
///
/// Holds a callback for creation. And the arguments to be supplied.
/// Runtime will be supplied automatically.
#[allow(non_camel_case_types)]
pub struct pxs_FactoryHolder {
    pub callback: super::func::pxs_Func,
    pub args: *mut pxs_Var,
}

impl pxs_FactoryHolder {
    /// Get a cloned list of args. This list will include the runtime passed as the
    /// first item. Returns a owned pxs_Var not a pointer.
    pub fn get_args(&self, rt: pxs_Runtime) -> pxs_Var {
        let args = self.args;
        // Check null
        assert!(!args.is_null(), "Factory args must not be null");
        unsafe {
            // Clone
            let args_clone = pxs_Var::from_borrow(self.args).clone();
            // Check list
            assert!(args_clone.is_list(), "Factory args must be a list");
            // Get list and add runtime
            let args_list = args_clone.get_list().unwrap();
            args_list.vars.insert(0, pxs_Var::new_i64(rt.into_i64()));

            args_clone
        }
    }

    /// Call the FactoryHolder function with the args!
    pub fn call(&self, rt: pxs_Runtime) -> pxs_Var {
        let args = self.get_args(rt);
        // Create raw memory that lasts for the direction of this call.
        let args_raw = args.into_raw();

        let res = unsafe { (self.callback)(args_raw) };
        let var = if res.is_null() {
            pxs_Var::new_null()
        } else {
            pxs_Var::from_raw(res)
        };
        // Drop raw args memory
        let _ = pxs_Var::from_raw(args_raw);

        // Return the variable result
        var
    }
}

impl PtrMagic for pxs_FactoryHolder {}

impl Drop for pxs_FactoryHolder {
    fn drop(&mut self) {
        if self.args.is_null() {
            return;
        }

        // Drop args
        let _ = pxs_Var::from_raw(self.args);
    }
}

/// Holds data for a pxs_Var of list.
///
/// It holds multiple pxsVar within.
///
/// When creating call:
///
/// `pixelscript_var_newlist()`.
///
/// To add items
///
/// `pixelscript_var_list_add(list_ptr, item_ptr)`
///
/// To get items
///
/// `pixelscript_var_list_get(list_ptr, index)`
///
/// A full example looks like:
/// ```c
/// // Create a new list (you never interact with pxs_VarList directly...)
/// pxs_Var* list = pixelscript_var_newlist();
///
/// // Add a item
/// pxs_Var* number = pixelscript_var_newint(1);
/// pixelscript_var_list_add(list, number);
///
/// // Get a item
/// pxs_Var* item_got = pixelscript_var_list_get(list, 0);
/// ```
#[allow(non_camel_case_types)]
pub struct pxs_VarList {
    pub vars: Vec<pxs_Var>,
}

impl PtrMagic for pxs_VarList {}

impl pxs_VarList {
    /// Create a new VarList
    pub fn new() -> Self {
        pxs_VarList { vars: vec![] }
    }

    fn get_rindex(&self, index: i32) -> i32 {
        if index < 0 {
            (self.vars.len() as i32) + index
        } else {
            index
        }
    }

    /// Add a Var to the list. List will take ownership.
    pub fn add_item(&mut self, item: pxs_Var) {
        self.vars.push(item);
    }

    /// Get a Var from the list. Supports negative based indexes.
    pub fn get_item(&self, index: i32) -> Option<&pxs_Var> {
        // Get correct negative index.
        let r_index = self.get_rindex(index);

        if r_index < 0 {
            None
        } else {
            self.vars.get(r_index as usize)
        }
    }

    /// Set at a specific index a item.
    ///
    /// Index must already be filled.
    pub fn set_item(&mut self, item: pxs_Var, index: i32) -> bool {
        // Get correct negative index.
        let r_index = self.get_rindex(index);

        if r_index < 0 {
            return false;
        }

        if self.vars.len() < r_index as usize {
            false
        } else {
            self.vars[r_index as usize] = item;
            true
        }
    }

    /// Remove a item at a specific index.
    pub fn del_item(&mut self, index: i32) -> bool {
        let r_index = self.get_rindex(index);

        if r_index < 0 {
            return false;
        }

        if self.vars.len() < r_index as usize {
            false
        } else {
            self.vars.remove(index as usize);
            true
        }
    }

    /// Get length
    pub fn len(&self) -> usize {
        self.vars.len()
    }

    /// Insert a item moving all the rest to the right.
    pub fn insert_item(&mut self, index: usize, item: pxs_Var) {
        self.vars.insert(index, item);
    }
}

/// The Variables actual value union.
#[repr(C)]
#[allow(non_camel_case_types)]
pub union pxs_VarValue {
    pub i64_val: i64,
    pub u64_val: u64,
    pub string_val: *mut c_char,
    pub bool_val: bool,
    pub f64_val: f64,
    pub null_val: *const c_void,
    pub object_val: *mut pxs_VarObject,
    pub host_object_val: i32,
    pub list_val: *mut pxs_VarList,
    pub function_val: *mut c_void,
    pub factory_val: *mut pxs_FactoryHolder,
    pub map_val: *mut pxs_VarMap,
}

#[allow(non_camel_case_types)]
pub type pxs_DeleterFn = unsafe extern "C" fn(*mut c_void);

/// Default Var deleter fn.
/// Use it when you don't want to delete memory.
pub unsafe extern "C" fn default_deleter(_ptr: *mut c_void) {
    // Empty OP
}

/// A PixelScript Var(iable).
///
/// This is the universal truth between all languages PixelScript supports.
///
/// Currently supports:
/// - int (i32, i64, u32, u64)
/// - float (f32, f64)
/// - string
/// - boolean
/// - Objects
/// - HostObjects (C structs acting as pseudo-classes) This in the Host can also be a Int or Uint.
/// - List
/// - Functions (First class functions)
///
/// When working with objects you must use the C-api:
/// ```c
/// // Calls a method on a object.
/// pixelscript_object_call(var)
/// ```
///
/// When using within a callback, if said callback was attached to a Class, the first *mut Var will be the class/object.
///
/// When using ints or floats, if (i32, u32, u64, f32) there is no gurantee that the supported language uses
/// those types. Usually it defaults to i64 and f64.
///
/// When creating a object, this is a bit tricky but essentially you have to first create a pointer via the pixel script runtime.
#[repr(C)]
#[allow(non_camel_case_types)]
pub struct pxs_Var {
    /// A tag for the variable type.
    pub tag: pxs_VarType,
    /// A value as a union.
    pub value: pxs_VarValue,

    /// Optional delete method. This is used for Pointers in Objects, and Functions.
    pub deleter: Cell<pxs_DeleterFn>,
}

// Rust specific functions
impl pxs_Var {
    pub unsafe fn slice_raw(argv: *mut *mut Self, argc: usize) -> &'static [*mut pxs_Var] {
        unsafe { std::slice::from_raw_parts(argv, argc) }
    }

    /// Get the direct host pointer. (Not the idx) OR null if not found!
    pub fn get_host_ptr(&self) -> *mut c_void {
        let object = get_object(self.get_host_idx());
        if let Some(obj) = object {
            obj.ptr
        } else {
            std::ptr::null_mut()
        }
    }

    /// Get the Rust string from the Var.
    ///
    /// Works for pxs_String and pxs_Exception
    pub fn get_string(&self) -> Result<String, Error> {
        if self.tag == pxs_VarType::pxs_String || self.tag == pxs_VarType::pxs_Exception {
            unsafe {
                if self.value.string_val.is_null() {
                    return Err(anyhow!("String pointer is null"));
                }

                let c_str = CStr::from_ptr(self.value.string_val);
                let res = c_str.to_str();
                if res.is_err() {
                    return Err(anyhow!(res.err().unwrap()));
                }

                Ok(res.unwrap().to_string())
            }
        } else {
            Err(anyhow!("Var is not a string."))
        }
    }

    /// Create a new String var.
    ///
    /// The memory is leaked and needs to be freed eventually. It is freed by Var::free_var(). And done so automatically
    /// by the library.
    pub fn new_string(val: String) -> Self {
        let cstr = CString::new(val).expect("Could not create CString.");

        pxs_Var {
            tag: pxs_VarType::pxs_String,
            value: pxs_VarValue {
                string_val: cstr.into_raw(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Creates a new Null var.
    ///
    /// No need to free, or any of that. It cretes a *const c_void
    pub fn new_null() -> Self {
        pxs_Var {
            tag: pxs_VarType::pxs_Null,
            value: pxs_VarValue {
                null_val: ptr::null(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new HostObject var.
    pub fn new_host_object(ptr: i32) -> Self {
        pxs_Var {
            tag: pxs_VarType::pxs_HostObject,
            value: pxs_VarValue {
                host_object_val: ptr,
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new Object var.
    pub fn new_object(ptr: pxs_VarObject, deleter: Option<pxs_DeleterFn>) -> Self {
        let deleter = if let Some(d) = deleter {
            d
        } else {
            default_deleter
        };
        pxs_Var {
            tag: pxs_VarType::pxs_Object,
            value: pxs_VarValue { object_val: ptr.into_raw() },
            deleter: Cell::new(deleter),
        }
    }

    /// Create a new pxs_VarList var.
    pub fn new_list() -> Self {
        pxs_Var {
            tag: pxs_VarType::pxs_List,
            value: pxs_VarValue {
                list_val: pxs_VarList::new().into_raw(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new pxs_VarList var with values.
    pub fn new_list_with(vars: Vec<pxs_Var>) -> Self {
        let mut list = pxs_VarList::new();
        list.vars = vars;
        pxs_Var {
            tag: pxs_VarType::pxs_List,
            value: pxs_VarValue {
                list_val: list.into_raw(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new Function var.
    pub fn new_function(ptr: *mut c_void, deleter: Option<pxs_DeleterFn>) -> Self {
        let deleter = if let Some(d) = deleter {
            d
        } else {
            default_deleter
        };

        pxs_Var {
            tag: pxs_VarType::pxs_Function,
            value: pxs_VarValue { function_val: ptr },
            deleter: Cell::new(deleter),
        }
    }

    /// Create a new Factory var.
    pub fn new_factory(func: super::func::pxs_Func, args: pxs_VarT) -> Self {
        let factory = pxs_FactoryHolder {
            callback: func,
            args,
        };
        pxs_Var {
            tag: pxs_VarType::pxs_Factory,
            value: pxs_VarValue {
                factory_val: factory.into_raw(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new Exception var.
    pub fn new_exception<T: ToString>(msg: T) -> Self {
        pxs_Var {
            tag: pxs_VarType::pxs_Exception,
            value: pxs_VarValue {
                string_val: create_raw_string!(msg.to_string()),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Create a new Map var.
    pub fn new_map() -> Self {
        pxs_Var {
            tag: pxs_VarType::pxs_Map,
            value: pxs_VarValue {
                map_val: pxs_VarMap::new().into_raw(),
            },
            deleter: Cell::new(default_deleter),
        }
    }

    /// Get the IDX of the object if Host, i64, u64
    pub fn get_host_idx(&self) -> i32 {
        match self.tag {
            pxs_VarType::pxs_Int64 => self.get_i64().unwrap() as i32,
            pxs_VarType::pxs_UInt64 => self.get_u64().unwrap() as i32,
            pxs_VarType::pxs_HostObject => unsafe { self.value.host_object_val },
            _ => -1,
        }
    }

    /// Get the raw pointer to a `pxs_Object` type
    pub fn get_object_ptr(&self) -> *mut c_void {
        match self.tag {
            pxs_VarType::pxs_Object => unsafe {
                let object = pxs_VarObject::from_borrow(self.value.object_val);
                object.get_raw()
            },
            _ => std::ptr::null_mut(),
        }
    }

    /// Get the pxs_VarList as a &mut pxs_VarList.
    pub fn get_list(&self) -> Option<&mut pxs_VarList> {
        if !self.is_list() {
            None
        } else {
            unsafe { Some(pxs_VarList::from_borrow(self.value.list_val)) }
        }
    }

    /// Get the pxs_FactoryHolder as a &mut pxs_FactoryHolder
    pub fn get_factory(&self) -> Option<&mut pxs_FactoryHolder> {
        if !self.is_factory() {
            None
        } else {
            unsafe {
                if self.value.factory_val.is_null() {
                    None
                } else {
                    Some(pxs_FactoryHolder::from_borrow(self.value.factory_val))
                }
            }
        }
    }

    /// Get the pxs_VarMap as a &mut pxs_VarMap
    pub fn get_map(&self) -> Option<&mut pxs_VarMap> {
        if !self.is_map() {
            None
        } else {
            unsafe { Some(pxs_VarMap::from_borrow(self.value.map_val)) }
        }
    }

    // ///
    // pub unsafe fn from_argv(argc: usize, argv: *mut *mut pxs_Var) -> Vec<pxs_Var> {
    //     // First create a slice
    //     let argv_borrow = unsafe { pxs_Var::slice_raw(argv, argc) };
    //     // Now clone them!
    //     let cloned: Vec<pxs_Var> = argv_borrow
    //         .iter()
    //         .filter(|ptr| !ptr.is_null())
    //         .map(|&ptr| unsafe { (*ptr).clone() })
    //         .collect();

    //     cloned
    // }

    /// Debug struct
    unsafe fn dbg(&self) -> String {
        unsafe {
            let details = match self.tag {
                pxs_VarType::pxs_Int64 => self.value.i64_val.to_string(),
                pxs_VarType::pxs_UInt64 => self.value.u64_val.to_string(),
                pxs_VarType::pxs_String => borrow_string!(self.value.string_val).to_string(),
                pxs_VarType::pxs_Bool => self.value.bool_val.to_string(),
                pxs_VarType::pxs_Float64 => self.value.f64_val.to_string(),
                pxs_VarType::pxs_Null => "Null".to_string(),
                pxs_VarType::pxs_Object => "Object".to_string(),
                pxs_VarType::pxs_HostObject => {
                    let idx = self.get_host_idx();
                    let object = get_object(idx).unwrap();
                    object.type_name.to_string()
                }
                pxs_VarType::pxs_List => {
                    let list = self.get_list().unwrap();
                    let t: String = list.vars.iter().map(|v| format!("{},", v.dbg())).collect();
                    format!("[{t}]")
                }
                pxs_VarType::pxs_Function => "Function".to_string(),
                pxs_VarType::pxs_Factory => "Factory".to_string(),
                pxs_VarType::pxs_Exception => borrow_string!(self.value.string_val).to_string(),
                pxs_VarType::pxs_Map => {
                    let map = self.get_map().unwrap();
                    let keys = map.keys();
                    let mut res = String::from("{");
                    for k in keys {
                        let value = map.get_item(k);
                        res.push_str(format!("{:#?}: {:#?},\n", k, value).as_str());
                    }
                    res.push_str("}");

                    res
                }
            };

            format!("{details} :: {:p}", self)
        }
    }

    // /// Remove the deleter on current pxs_Var.
    // pub fn remove_deleter(mut self) -> Self {
    //     self.deleter = Cell::new(default_deleter);
    //     self
    // }

    write_func!(
        (get_i64, i64_val, i64, pxs_VarType::pxs_Int64),
        (get_u64, u64_val, u64, pxs_VarType::pxs_UInt64),
        (get_bool, bool_val, bool, pxs_VarType::pxs_Bool),
        (get_f64, f64_val, f64, pxs_VarType::pxs_Float64),
        (
            get_function,
            function_val,
            *mut c_void,
            pxs_VarType::pxs_Function
        )
    );

    // $t:ty, $func:ident, $vt:expr, $vn:ident
    write_new_methods! {
        i64, new_i64, pxs_VarType::pxs_Int64, i64_val;
        u64, new_u64, pxs_VarType::pxs_UInt64, u64_val;
        f64, new_f64, pxs_VarType::pxs_Float64, f64_val;
        bool, new_bool, pxs_VarType::pxs_Bool, bool_val
    }

    write_is_methods! {
        is_i64, pxs_VarType::pxs_Int64;
        is_u64, pxs_VarType::pxs_UInt64;
        is_f64, pxs_VarType::pxs_Float64;
        is_bool, pxs_VarType::pxs_Bool;
        is_string, pxs_VarType::pxs_String;
        is_null, pxs_VarType::pxs_Null;
        is_object, pxs_VarType::pxs_Object;
        is_host_object, pxs_VarType::pxs_HostObject;
        is_list, pxs_VarType::pxs_List;
        is_function, pxs_VarType::pxs_Function;
        is_factory, pxs_VarType::pxs_Factory;
        is_exception, pxs_VarType::pxs_Exception;
        is_map, pxs_VarType::pxs_Map
    }

    /// Do a shallow copy on this variable.
    /// 
    /// Int/Uint/Float/String/Bool/Null/Exception/HostObject are cloned.
    /// 
    /// List/Maps/Objects/Functions/Factories are cloned without deleters.
    pub fn shallow_copy(&self) -> pxs_Var {
        unsafe{
            match self.tag {
                pxs_VarType::pxs_Int64 => self.clone(),
                pxs_VarType::pxs_UInt64 => self.clone(),
                pxs_VarType::pxs_String => self.clone(),
                pxs_VarType::pxs_Bool => self.clone(),
                pxs_VarType::pxs_Float64 => self.clone(),
                pxs_VarType::pxs_Null => self.clone(),
                pxs_VarType::pxs_Exception => self.clone(),
                pxs_VarType::pxs_HostObject => self.clone(),
                pxs_VarType::pxs_Object => {
                    let object = pxs_VarObject::from_borrow(self.value.object_val);
                    // Copy without deleter
                    pxs_Var {
                        tag: pxs_VarType::pxs_Object,
                        value: pxs_VarValue {
                            object_val: object.clone().into_raw()
                        },
                        deleter: Cell::new(default_deleter)
                    }
                },
                pxs_VarType::pxs_List => {
                    let mut list = pxs_VarList::new();
                    let og_list_val = pxs_VarList::from_borrow(self.value.list_val);

                    // Shallow copy old items into new list
                    for item in og_list_val.vars.iter() {
                        list.add_item(item.shallow_copy());
                    }

                    pxs_Var {
                        tag: pxs_VarType::pxs_List,
                        value: pxs_VarValue {
                            list_val: list.into_raw()
                        },
                        deleter: Cell::new(default_deleter)
                    }
                },
                pxs_VarType::pxs_Function => {
                    pxs_Var {
                        tag: pxs_VarType::pxs_Function,
                        value: pxs_VarValue {
                            function_val: self.value.function_val
                        },
                        deleter: Cell::new(default_deleter)
                    }
                },
                pxs_VarType::pxs_Factory => {
                    let og = pxs_FactoryHolder::from_borrow(self.value.factory_val);
                    if og.args.is_null() {
                        return pxs_Var::new_null();
                    }

                    // Shallow Copy args
                    let new_args = pxs_Var::new_list();
                    let old_args = pxs_Var::from_borrow(og.args);
                    if !old_args.is_list() {
                        return pxs_Var::new_null();
                    }
                    let old_list = old_args.get_list().unwrap();
                    let new_list = new_args.get_list().unwrap();
                    for var in old_list.vars.iter() {
                        new_list.add_item(var.shallow_copy());
                    }
                    let f = pxs_FactoryHolder {
                        args: new_args.into_raw(),
                        callback: og.callback,
                    };
                    pxs_Var {
                        tag: pxs_VarType::pxs_Factory,
                        value: pxs_VarValue {
                            factory_val: f.into_raw(),
                        },
                        deleter: Cell::new(default_deleter),
                    }
                },
                pxs_VarType::pxs_Map => {
                                        // Our new map
                    let mut map = pxs_VarMap::new();
                    // OG map yo!
                    let og_map = self.get_map().unwrap();
                    // Get them keys dog
                    let keys = og_map.keys();
                    for k in keys {
                        let v = og_map.get_item(k);
                        if let Some(v) = v {
                            //  shallow copy
                            map.add_item(k.shallow_copy(), v.shallow_copy());
                        }
                    }

                    // Follows a similar structure to pxs_List shallow copy
                    pxs_Var {
                        tag: pxs_VarType::pxs_Map,
                        value: pxs_VarValue {
                            map_val: map.into_raw()
                        },
                        deleter: Cell::new(default_deleter)
                    }
                },
            }
        }
    }
}

unsafe impl Send for pxs_Var {}
unsafe impl Sync for pxs_Var {}

impl Drop for pxs_Var {
    fn drop(&mut self) {
        // pxs_debug!("|psx_Var DROP| {}", unsafe {self.dbg() });
        if self.tag == pxs_VarType::pxs_String || self.tag == pxs_VarType::pxs_Exception {
            unsafe {
                // Free the mem
                if !self.value.string_val.is_null() {
                    let _ = CString::from_raw(self.value.string_val);
                    self.value.string_val = ptr::null_mut();
                }
            }
        } else if self.tag == pxs_VarType::pxs_List {
            let _ = unsafe {
                // This will automatically drop
                pxs_VarList::from_raw(self.value.list_val)
            };
        } else if self.tag == pxs_VarType::pxs_Object {
            unsafe {
                if self.value.object_val.is_null() {
                    return;
                }
                let object = pxs_VarObject::from_raw(self.value.object_val);
                (self.deleter.get())(object.get_raw());
                // object dropped here.
            };
        } else if self.tag == pxs_VarType::pxs_Function {
            unsafe {
                if self.value.object_val.is_null() {
                    return;
                }
                (self.deleter.get())(self.value.function_val)
            };
        } else if self.tag == pxs_VarType::pxs_Factory {
            // Free args
            unsafe {
                if self.value.factory_val.is_null() {
                    return;
                }
                let _ = pxs_FactoryHolder::from_raw(self.value.factory_val);
                // if val.args.is_null() {
                //     return;
                // }
                // // Drop list
                // let _ = pxs_Var::from_raw(val.args);
            }
        } else if self.tag == pxs_VarType::pxs_Map {
            let _ = unsafe {
                pxs_VarMap::from_raw(self.value.map_val)
            };
        }
    }
}

impl PtrMagic for pxs_Var {}

impl Clone for pxs_Var {
    fn clone(&self) -> Self {
        unsafe {
            match self.tag {
                pxs_VarType::pxs_Int64 => pxs_Var::new_i64(self.value.i64_val),
                pxs_VarType::pxs_UInt64 => pxs_Var::new_u64(self.value.u64_val),
                pxs_VarType::pxs_String => {
                    let string = borrow_string!(self.value.string_val);
                    let cloned_string = string.to_string().clone();
                    let new_string = create_raw_string!(cloned_string);
                    pxs_Var {
                        tag: pxs_VarType::pxs_String,
                        value: pxs_VarValue {
                            string_val: new_string,
                        },
                        deleter: Cell::new(default_deleter),
                    }
                }
                pxs_VarType::pxs_Bool => pxs_Var::new_bool(self.value.bool_val),
                pxs_VarType::pxs_Float64 => pxs_Var::new_f64(self.value.f64_val),
                pxs_VarType::pxs_Null => pxs_Var::new_null(),
                pxs_VarType::pxs_Object => {
                    let object = pxs_VarObject::from_borrow(self.value.object_val);

                    let r = pxs_Var {
                        tag: pxs_VarType::pxs_Object,
                        value: pxs_VarValue {
                            object_val: object.clone().into_raw(),
                        },
                        deleter: Cell::new(self.deleter.get()),
                    };

                    self.deleter.set(default_deleter);

                    r
                }
                pxs_VarType::pxs_HostObject => pxs_Var::new_host_object(self.value.host_object_val),
                pxs_VarType::pxs_List => {
                    let mut list = pxs_VarList::new();
                    // let mut list = pxs_Var::new_list();
                    let og_list_val = pxs_VarList::from_borrow(self.value.list_val);

                    // Add items of current list. i.e. transfer ownership...
                    for item in og_list_val.vars.iter() {
                        // Clone into new list
                        list.add_item(item.clone());
                    }

                    pxs_Var {
                        tag: pxs_VarType::pxs_List,
                        value: pxs_VarValue {
                            list_val: list.into_raw(),
                        },
                        deleter: Cell::new(default_deleter),
                    }
                }
                pxs_VarType::pxs_Function => {
                    let r = pxs_Var {
                        tag: pxs_VarType::pxs_Function,
                        value: pxs_VarValue {
                            function_val: self.value.function_val,
                        },
                        deleter: Cell::new(self.deleter.get()),
                    };

                    self.deleter.set(default_deleter);
                    r
                }
                pxs_VarType::pxs_Factory => {
                    let og = pxs_FactoryHolder::from_borrow(self.value.factory_val);
                    if og.args.is_null() {
                        return pxs_Var::new_null();
                    }
                    // Clone args
                    let new_args = pxs_Var::new_list();
                    let old_args = pxs_Var::from_borrow(og.args);
                    if !old_args.is_list() {
                        return pxs_Var::new_null();
                    }
                    let old_list = old_args.get_list().unwrap();
                    let new_list = new_args.get_list().unwrap();
                    for var in old_list.vars.iter() {
                        new_list.add_item(var.clone());
                    }
                    let f = pxs_FactoryHolder {
                        args: new_args.into_raw(),
                        callback: og.callback,
                    };
                    pxs_Var {
                        tag: pxs_VarType::pxs_Factory,
                        value: pxs_VarValue {
                            factory_val: f.into_raw(),
                        },
                        deleter: Cell::new(default_deleter),
                    }
                }
                pxs_VarType::pxs_Exception => {
                    let string = borrow_string!(self.value.string_val);
                    let cloned_string = string.to_string().clone();
                    let new_string = create_raw_string!(cloned_string);
                    pxs_Var {
                        tag: pxs_VarType::pxs_Exception,
                        value: pxs_VarValue {
                            string_val: new_string,
                        },
                        deleter: Cell::new(default_deleter),
                    }
                }
                pxs_VarType::pxs_Map => {
                    // Our new map
                    let mut map = pxs_VarMap::new();
                    // OG map yo!
                    let og_map = self.get_map().unwrap();
                    // Get them keys dog
                    let keys = og_map.keys();
                    for k in keys {
                        let v = og_map.get_item(k);
                        if let Some(v) = v {
                            map.add_item(k.clone(), v.clone());
                        }
                    }

                    // Follows a similar structure to pxs_List cloning
                    pxs_Var {
                        tag: pxs_VarType::pxs_Map,
                        value: pxs_VarValue {
                            map_val: map.into_raw()
                        },
                        deleter: Cell::new(default_deleter)
                    }
                }
            }
        }
    }
}

impl std::fmt::Debug for pxs_Var {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let debug_val: &dyn std::fmt::Debug = unsafe { &self.dbg() };
        f.debug_struct("pxs_Var")
            .field("tag", &self.tag)
            .field("value", debug_val)
            .finish()
    }
}

impl PartialEq for pxs_Var {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            match (&self.tag, &other.tag) {
                (pxs_VarType::pxs_Int64, pxs_VarType::pxs_Int64) => {
                    self.value.i64_val == other.value.i64_val
                }
                (pxs_VarType::pxs_Int64, _) => false,
                (pxs_VarType::pxs_UInt64, pxs_VarType::pxs_UInt64) => {
                    self.value.u64_val == other.value.u64_val
                }
                (pxs_VarType::pxs_UInt64, _) => false,
                (pxs_VarType::pxs_String, pxs_VarType::pxs_String) => {
                    self.get_string().unwrap_or(String::new())
                        == other.get_string().unwrap_or(String::new())
                }
                (pxs_VarType::pxs_String, _) => false,
                (pxs_VarType::pxs_Bool, pxs_VarType::pxs_Bool) => {
                    self.value.bool_val == other.value.bool_val
                }
                (pxs_VarType::pxs_Bool, _) => false,
                (pxs_VarType::pxs_Float64, pxs_VarType::pxs_Float64) => {
                    self.value.f64_val == other.value.f64_val
                }
                (pxs_VarType::pxs_Float64, _) => false,
                (pxs_VarType::pxs_Null, _) => false,
                (pxs_VarType::pxs_Object, _) => false,
                (pxs_VarType::pxs_HostObject, _) => false,
                (pxs_VarType::pxs_List, _) => false,
                (pxs_VarType::pxs_Function, _) => false,
                (pxs_VarType::pxs_Factory, _) => false,
                (pxs_VarType::pxs_Exception, _) => false,
                (pxs_VarType::pxs_Map, _) => false,
            }
        }
    }
}

impl Eq for pxs_Var {}

impl Hash for pxs_Var {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        unsafe {
            match self.tag {
                pxs_VarType::pxs_Int64 => self.value.i64_val.hash(state),
                pxs_VarType::pxs_UInt64 => self.value.u64_val.hash(state),
                pxs_VarType::pxs_String => self.get_string().unwrap_or(String::new()).hash(state),
                pxs_VarType::pxs_Bool => self.value.bool_val.hash(state),
                pxs_VarType::pxs_Float64 => self.value.f64_val.to_bits().hash(state),
                _ => panic!("Can not Hash none basic pxs_VarType")
            }
        }
    }
}

impl pxs_Var {
    /// Return a error saying that the found type was not expected.
    pub fn incorrect_type_ep(expected: pxs_VarType, found: pxs_VarType) -> Self {
        Self::new_exception(format!("Expected {:#?}, found {:#?}", expected, found))
    }
    
    /// Return a error saying that the found type was not expected of a vector of types
    pub fn incorrect_types_ep(allowed: Vec<pxs_VarType>, found: pxs_VarType) -> Self {
        Self::new_exception(format!("Allowed types: {:#?}, found: {:#?}", allowed, found))
    }

    /// A general error saying that paramaters passed in to the function were null.
    pub fn null_params_ep() -> Self {
        Self::new_exception("Paramaters are null")
    }

    /// A specific error that a specific param was null.
    pub fn null_param_ep<T: ToString>(name:T) -> Self {
        Self::new_exception(format!("{} is null", name.to_string()))
    }

    /// Unkownn runtime exception
    pub fn unkown_runtime_ep(runtime: i64) -> Self {
        Self::new_exception(format!("Unkown runtime: {runtime}"))
    }

    /// Unkown runtime var exception
    pub fn unkown_runtime_var_ep(runtime: pxs_VarT) -> Self {
        let var = unsafe{Self::from_borrow(runtime)};
        Self::new_exception(format!("Unkown runtime: {:#?}", var))
    }

    /// When a item is not found in list or map.
    pub fn item_not_found_ep() -> Self {
        Self::new_exception("Item not found")
    }

    /// Feature not enabled
    pub fn feature_not_enabled_ep(feature: &str) -> Self {
        Self::new_exception(format!("{feature} is not enabled."))
    }
}

/// Methods for interacting with objects and callbacks from the runtime.
pub trait ObjectMethods {
    /// Call a method on a object.
    fn object_call(var: &pxs_Var, method: &str, args: &mut pxs_VarList) -> Result<pxs_Var, Error>;

    /// Call a method and pass in args
    fn call_method(method: &str, args: &mut pxs_VarList) -> Result<pxs_Var, Error>;

    /// Call a pxs_Var function.
    fn var_call(method: &pxs_Var, args: &mut pxs_VarList) -> Result<pxs_Var, Error>;

    /// Getter
    fn get(var: &pxs_Var, key: &str) -> Result<pxs_Var, Error>;

    /// Setter
    fn set(var: &pxs_Var, key: &str, value: &pxs_Var) -> Result<pxs_Var, Error>;

    /// Get a object/function based off their name
    fn get_from_name(name: &str) -> Result<pxs_Var, Error>;
}

/// Type Helper for a pxs_Var
/// Use this instead of writing out pxs_Var*
#[allow(non_camel_case_types)]
pub type pxs_VarT = *mut pxs_Var;