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
//! We need to expose the Wren API in a Rust-y way
use wren_sys::{WrenVM, WrenHandle, WrenConfiguration, WrenErrorType, WrenForeignClassMethods};
use std::sync::mpsc::{channel, Sender, Receiver};
use std::collections::HashMap;
use std::rc::{Rc, Weak};
use std::cell::RefCell;

use std::{mem, ffi, os::raw, any};

#[derive(Debug)]
pub enum WrenError {
    Compile(String, i32, String),
    Runtime(String),
    StackTrace(String, i32, String),
}

// Force Wren to use Rust's allocator to allocate memory
extern "C" fn wren_realloc(memory: *mut ffi::c_void, new_size: wren_sys::size_t) -> *mut ffi::c_void {
    unsafe {
        if memory == std::ptr::null_mut() { // If memory == NULL
            // allocate new memory
            std::alloc::alloc_zeroed(std::alloc::Layout::from_size_align(new_size as usize, 8).unwrap()) as *mut _
        } else {
            // Memory is an actual pointer to a location.
            if new_size == 0 {
                std::alloc::dealloc(memory as *mut _, std::alloc::Layout::from_size_align(0, 8).unwrap());
                std::ptr::null_mut()
            } else {
                std::alloc::realloc(memory as *mut _, std::alloc::Layout::from_size_align(new_size as usize, 8).unwrap(), new_size as usize) as *mut _
            }
        }
    }
}

extern "C" fn wren_error(vm: *mut WrenVM, typ: WrenErrorType, module: *const raw::c_char, line: raw::c_int, message: *const raw::c_char) {
    let conf = unsafe { &mut *(wren_sys::wrenGetUserData(vm) as *mut UserData) };
    match typ {
        wren_sys::WrenErrorType_WREN_ERROR_COMPILE => {
            let module_str = unsafe { ffi::CStr::from_ptr(module) };
            let message_str = unsafe { ffi::CStr::from_ptr(message) };
            conf.error_channel.send(WrenError::Compile(module_str.to_string_lossy().to_string(), line as i32, message_str.to_string_lossy().to_string())).unwrap();
        },
        wren_sys::WrenErrorType_WREN_ERROR_RUNTIME => {
            let message_str = unsafe { ffi::CStr::from_ptr(message) };
            conf.error_channel.send(WrenError::Runtime(message_str.to_string_lossy().to_string())).unwrap();
        },
        wren_sys::WrenErrorType_WREN_ERROR_STACK_TRACE => {
            let module_str = unsafe { ffi::CStr::from_ptr(module) };
            let message_str = unsafe { ffi::CStr::from_ptr(message) };
            conf.error_channel.send(WrenError::StackTrace(module_str.to_string_lossy().to_string(), line as i32, message_str.to_string_lossy().to_string())).unwrap();
        },
        _ => unreachable!()
    }
}

extern "C" fn wren_print(vm: *mut WrenVM, message: *const raw::c_char) {
    let conf = unsafe { &mut *(wren_sys::wrenGetUserData(vm) as *mut UserData) };
    let message_str = unsafe { ffi::CStr::from_ptr(message) };
    conf.printer.print(message_str.to_string_lossy().to_string());
}

extern "C" fn wren_bind_foreign_method(vm: *mut WrenVM, mdl: *const raw::c_char, class: *const raw::c_char, is_static: bool, sgn: *const raw::c_char) -> Option<unsafe extern "C" fn(*mut WrenVM)> {
    let conf = unsafe { &mut *(wren_sys::wrenGetUserData(vm) as *mut UserData) };
    let module = unsafe { ffi::CStr::from_ptr(mdl) };
    let class = unsafe { ffi::CStr::from_ptr(class) };
    let signature = unsafe { ffi::CStr::from_ptr(sgn) };

    if let Some(library) = conf.library {
        if let Some(rc) = library.get_foreign_class(module.to_string_lossy(), class.to_string_lossy()) {
            rc.methods.function_pointers.iter().find(|mp| {
                if mp.signature == signature.to_string_lossy() && mp.is_static == is_static {
                    true
                } else {
                    false
                }
            }).map(|mp| mp.pointer)
        } else {
            None
        }
    } else {
        None
    }
}

extern "C" fn wren_bind_foreign_class(vm: *mut WrenVM, mdl: *const raw::c_char, class: *const raw::c_char) -> WrenForeignClassMethods {
    let mut fcm = WrenForeignClassMethods {
        allocate: None,
        finalize: None
    };

    let conf = unsafe { &mut *(wren_sys::wrenGetUserData(vm) as *mut UserData) };
    let module = unsafe { ffi::CStr::from_ptr(mdl) };
    let class = unsafe { ffi::CStr::from_ptr(class) };

    if let Some(library) = conf.library {
        let rc = library.get_foreign_class(module.to_string_lossy(), class.to_string_lossy());
        if let Some(rc) = rc {
            fcm.allocate = Some(rc.construct);
            fcm.finalize = Some(rc.destruct);
        }
    }
    fcm
}

extern "C" fn wren_load_module(vm: *mut WrenVM, name: *const raw::c_char) -> *mut raw::c_char {
    // The whoooole reason we wrote wren_realloc - to force Wren into Rust's allocation space
    let conf = unsafe { &mut *(wren_sys::wrenGetUserData(vm) as *mut UserData) };
    let module_name = unsafe { ffi::CStr::from_ptr(name) };
    match conf.loader.load_script(module_name.to_string_lossy().to_string()) {
        Some(string) => {
            ffi::CString::new(string).ok().map(|strg| strg.into_raw()).expect(&format!("Failed to convert source to C string for {}", module_name.to_string_lossy()))
        },
        None => std::ptr::null_mut()
    }
}

#[derive(Debug, Clone)]
pub enum VMError {
    Compile {
        module: String,
        line: i32,
        error: String
    },
    Runtime {
        error: String,
        frames: Vec<VMStackFrameError>
    }
}

#[derive(Debug, Clone)]
pub struct VMStackFrameError {
    module: String,
    line: i32,
    function: String
}


impl std::fmt::Display for VMError {
    fn fmt(&self, fmt: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            VMError::Compile { module, line, error } => write!(fmt, "Compile Error ({}:{}): {}", module, line, error),
            VMError::Runtime { error, frames } => {
                writeln!(fmt, "Runtime Error: {}", error)?;
                for frame in frames {
                    if frame.function == "" {
                        writeln!(fmt, "\tin {}:{}: <constructor>", frame.module, frame.line)?;
                    } else {
                        writeln!(fmt, "\tin {}:{}: {}", frame.module, frame.line, frame.function)?;
                    }
                }
                Ok(())
            },
        }
    }
}

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

pub struct Handle<'a> {
    handle: *mut WrenHandle,
    vm: &'a VM<'a>
}

impl<'a> Drop for Handle<'a> {
    fn drop(&mut self) {
        unsafe {
            wren_sys::wrenReleaseHandle(self.vm.vm, self.handle);
        }
    }
}

/// Simulates a module structure for foreign functions
#[derive(Debug)]
pub struct ModuleLibrary {
    modules: HashMap<String, Module>,
}

impl ModuleLibrary {
    pub fn new() -> ModuleLibrary {
        ModuleLibrary {
            modules: HashMap::new()
        }
    }

    pub fn module<N: AsRef<str>>(&mut self, name: N, modl: Module) {
        self.modules.insert(name.as_ref().to_string(), modl);
    }

    fn get_foreign_class<M: AsRef<str>, C: AsRef<str>>(&self, module: M, class: C) -> Option<&RuntimeClass> {
        self.modules.get(module.as_ref()).and_then(|md| md.classes.get(class.as_ref()))
    }
}

#[derive(Debug)]
struct RuntimeClass {
    construct: extern "C" fn(*mut WrenVM),
    destruct: extern "C" fn(*mut ffi::c_void),
    methods: ClassObjectPointers
}

#[derive(Debug)]
pub struct Module {
    classes: HashMap<String, RuntimeClass>,
}

#[derive(Debug)]
pub struct ClassObjectPointers {
    function_pointers: Vec<MethodPointer>,
}

#[derive(Debug)]
struct MethodPointer {
    is_static: bool,
    signature: String,
    pointer: unsafe extern "C" fn(*mut WrenVM),
}

impl Module {
    pub fn new() -> Module {
        Module {
            classes: HashMap::new()
        }
    }

    pub fn class<C: ClassObject, S: AsRef<str>>(&mut self, name: S) -> &mut Self {
        let cp = C::generate_pointers();
        let init = C::initialize_pointer();
        let deinit = C::finalize_pointer();
        self.classes.insert(name.as_ref().to_string(), RuntimeClass {
            construct: init,
            destruct: deinit,
            methods: cp
        });
        self
    }
}

// Trait that all Wren "class" objects implement
pub trait Class {
    fn initialize(_: &VM) -> Self;
}

trait FuncReferent {
    fn refer(&self) -> extern "C" fn(*mut WrenVM);
}

pub trait ClassObject: Class {
    fn initialize_pointer() -> extern "C" fn(*mut WrenVM);
    fn finalize_pointer() -> extern "C" fn(*mut ffi::c_void);
    fn generate_pointers() -> ClassObjectPointers;
}

struct ForeignObject<T> {
    object: *mut T,
    type_id: any::TypeId,
}

#[macro_export]
macro_rules! create_class_objects {
    ($(
        class $name:ty => $md:ident {
            $(
                static($sgns:expr) $sf:ident 
            ),*
            $(
                instance($sgni:expr) $inf:ident
            ),*
        }
    )+) => {
        $(
            mod $md {
                use std::panic::{take_hook, set_hook, catch_unwind, AssertUnwindSafe};
                pub(in super) extern "C" fn _constructor(vm: *mut $crate::WrenVM) {
                    use $crate::Class;
                    unsafe {
                        let conf = &mut *(wren_sys::wrenGetUserData(vm) as *mut $crate::UserData);
                        let vm = std::rc::Weak::upgrade(&conf.vm).expect(&format!("Failed to access VM at {:p}", &conf.vm));
                        let wptr = wren_sys::wrenSetSlotNewForeign(vm.borrow().vm, 0, 0, std::mem::size_of::<$crate::ForeignObject<$name>>() as wren_sys::size_t);
                        // Allocate a new object, and move it onto the heap
                        // TODO Include panic -> runtime error
                        set_hook(Box::new(|_| {}));
                        let vm_borrow = AssertUnwindSafe(vm.borrow());
                        let object = match catch_unwind(|| <$name as Class>::initialize(&*vm_borrow)) {
                            Ok(obj) => Some(obj),
                            Err(err) => {
                                let err_string = if let Some(strg) = err.downcast_ref::<String>() {
                                    strg.clone()
                                } else if let Some(strg) = err.downcast_ref::<&str>() {
                                    strg.to_string()
                                } else {
                                    "Non-string panic message".into()
                                };

                                vm_borrow.set_slot_string(0, err_string);
                                vm_borrow.abort_fiber(0);
                                None
                            }
                        };
                        drop(take_hook());
                        if let Some(object) = object {
                            let new_obj = Box::new($crate::ForeignObject {
                                object: Box::into_raw(Box::new(object)),
                                type_id: std::any::TypeId::of::<$name>(),
                            });
                            std::ptr::copy_nonoverlapping(Box::leak(new_obj), wptr as *mut _, 1);
                        }
                    }
                }

                pub(in super) extern "C" fn _destructor(data: *mut std::ffi::c_void) {
                    unsafe {
                        let fo: &mut $crate::ForeignObject<$name> = &mut *(data as *mut $crate::ForeignObject<$name>);
                        drop(Box::from_raw(fo.object));
                        fo.object = std::ptr::null_mut();
                    }
                }

                $(
                    create_class_objects!(@fn static $name => $sf);
                )*

                $(
                    create_class_objects!(@fn instance $name => $inf);
                )*
            }

            impl $crate::ClassObject for $name {
                fn initialize_pointer() -> extern "C" fn(*mut wren_sys::WrenVM) { $md::_constructor }
                fn finalize_pointer() -> extern "C" fn(*mut std::ffi::c_void) { $md::_destructor }
                fn generate_pointers() -> $crate::ClassObjectPointers {
                    $crate::ClassObjectPointers {
                        function_pointers: vec![
                            $(
                                $crate::MethodPointer {
                                    pointer: $md::$sf,
                                    signature: $sgns.into(),
                                    is_static: true,
                                }
                            ),*
                            $(
                                $crate::MethodPointer {
                                    pointer: $md::$inf,
                                    signature: $sgni.into(),
                                    is_static: false,
                                }
                            ),*
                        ]
                    }
                }
            }
        )+
    };

    (@fn static $name:ty => $s:ident) => {
        pub(in super) unsafe extern "C" fn $s(vm: *mut wren_sys::WrenVM) {
            use std::panic::{take_hook, set_hook, catch_unwind, AssertUnwindSafe};

            let conf = &mut *(wren_sys::wrenGetUserData(vm) as *mut $crate::UserData);
            let vm = std::rc::Weak::upgrade(&conf.vm).expect(&format!("Failed to access VM at {:p}", &conf.vm));
            set_hook(Box::new(|_| {}));
            let vm_borrow = AssertUnwindSafe(vm.borrow());
            match catch_unwind(|| <$name>::$s(&*vm_borrow)) {
                Ok(_) => (),
                Err(err) => {
                    let err_string = if let Some(strg) = err.downcast_ref::<String>() {
                        strg.clone()
                    } else if let Some(strg) = err.downcast_ref::<&str>() {
                        strg.to_string()
                    } else {
                        "Non-string panic message".into()
                    };

                    vm_borrow.set_slot_string(0, err_string);
                    vm_borrow.abort_fiber(0);
                }
            };
            drop(take_hook());
        }
    };

    (@fn instance $name:ty => $inf:ident) => {
        pub(in super) unsafe extern "C" fn $inf(vm: *mut wren_sys::WrenVM) {
            use std::panic::{take_hook, set_hook, catch_unwind, AssertUnwindSafe};
            
            let conf = &mut *(wren_sys::wrenGetUserData(vm) as *mut $crate::UserData);
            let vm = std::rc::Weak::upgrade(&conf.vm).expect(&format!("Failed to access VM at {:p}", &conf.vm));
            set_hook(Box::new(|_| {}));
            let vm_borrow = AssertUnwindSafe(vm.borrow());
            match catch_unwind(|| <$name>::$inf(&*vm_borrow)) {
                Ok(_) => (),
                Err(err) => {
                    let err_string = if let Some(strg) = err.downcast_ref::<String>() {
                        strg.clone()
                    } else if let Some(strg) = err.downcast_ref::<&str>() {
                        strg.to_string()
                    } else {
                        "Non-string panic message".into()
                    };

                    vm_borrow.set_slot_string(0, err_string);
                    vm_borrow.abort_fiber(0);
                }
            };
            drop(take_hook());
        }
    }
}

/// Enables one to plug-in a module loader for Wren
pub trait ModuleScriptLoader {
    fn load_script(&mut self, name: String) -> Option<String>;
}

pub type EVM<'a> = Rc<RefCell<VM<'a>>>;

pub trait Executor {
    fn execute<F>(&self, function: F) where F: FnMut(&VM);
}

impl<'a> Executor for EVM<'a> {
    fn execute<F>(&self, mut function: F) where F: FnMut(&VM) {
        function(&*self.borrow())
    }
}

pub trait Printer {
    fn print(&mut self, s: String);
}

pub struct PrintlnPrinter;
impl Printer for PrintlnPrinter {
    fn print(&mut self, s: String) {
        print!("{}", s);
    }
}

pub struct NullLoader;
impl ModuleScriptLoader for NullLoader {
    fn load_script(&mut self, _: String) -> Option<String> { None }
}

pub struct VM<'l> {
    vm: *mut WrenVM,
    error_recv: Receiver<WrenError>,
    module: std::marker::PhantomData<&'l ModuleLibrary>
}

struct UserData<'a> {
    error_channel: Sender<WrenError>,
    printer: Box<dyn Printer>,
    #[allow(dead_code)] 
    vm: Weak<RefCell<VM<'a>>>, // is used a *lot* by generated code.
    library: Option<&'a ModuleLibrary>,
    loader: Box<dyn ModuleScriptLoader>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SlotType {
    Num,
    Bool,
    List,
    Null,
    String,
    Foreign,
    Unknown
}

// TODO Expose more VM configuration (initalHeap, maxHeap, etc.)
impl<'a> VM<'a> {
    pub fn new_terminal(library: Option<&ModuleLibrary>) -> EVM {
        VM::new(PrintlnPrinter, NullLoader, library)
    }

    pub fn new<P: 'static + Printer, L: 'static + ModuleScriptLoader>(p: P, l: L, library: Option<&ModuleLibrary>) -> EVM {
        let (etx, erx) = channel();

        // Have an uninitialized VM...
        let wvm = Rc::new(RefCell::new(VM {
            vm: std::ptr::null_mut(),
            error_recv: erx,
            module: std::marker::PhantomData
        }));

        let vm_config = Box::into_raw(Box::new(UserData {
            error_channel: etx,
            printer: Box::new(p),
            vm: Rc::downgrade(&wvm),
            loader: Box::new(l),
            library,
        }));

        // Configure the Wren side of things
        let mut config = unsafe {
            let mut uconfig = mem::MaybeUninit::<WrenConfiguration>::zeroed();
            wren_sys::wrenInitConfiguration(uconfig.as_mut_ptr());
            let mut config = uconfig.assume_init();
            // Stuff the callbacks into user data
            config.errorFn = Some(wren_error);
            config.writeFn = Some(wren_print);
            config.reallocateFn = Some(wren_realloc);
            config.bindForeignMethodFn = Some(wren_bind_foreign_method);
            config.bindForeignClassFn = Some(wren_bind_foreign_class);
            config.loadModuleFn = Some(wren_load_module);
            config.userData = vm_config as *mut ffi::c_void;
            config
        };

        let vm = unsafe { wren_sys::wrenNewVM(&mut config) };
        wvm.borrow_mut().vm = vm;
        wvm
    }

    pub fn call(&self, handle: &Handle) -> Result<(), VMError> {
        // TODO Do we need to check if this handle came from this VM, or does Wren do that for us?
        match unsafe { wren_sys::wrenCall(self.vm, handle.handle) } {
            wren_sys::WrenInterpretResult_WREN_RESULT_SUCCESS => Ok(()),
            wren_sys::WrenInterpretResult_WREN_RESULT_COMPILE_ERROR => unreachable!("wrenCall doesn't compile anything"),
            wren_sys::WrenInterpretResult_WREN_RESULT_RUNTIME_ERROR => {
                let mut error = "".to_string();
                let mut frames = vec![];
                while let Ok(err) = self.error_recv.try_recv() {
                    match err {
                        WrenError::Runtime(msg) => {error = msg; },
                        WrenError::StackTrace(module, line, msg) => {frames.push(VMStackFrameError {
                            module, line, function: msg
                        }); },
                        _ => unreachable!()
                    }
                }
                Err(VMError::Runtime{
                    error,
                    frames
                })
            },
            _ => unreachable!()
        }
    }

    pub fn interpret<M: AsRef<str>, C: AsRef<str>>(&self, module: M, code: C) -> Result<(), VMError> {
        let module = ffi::CString::new(module.as_ref()).expect("module name conversion failed");
        let code = ffi::CString::new(code.as_ref()).expect("code conversion failed");
        match unsafe { wren_sys::wrenInterpret(self.vm, module.as_ptr() as *const i8, code.as_ptr() as *const i8) } {
            wren_sys::WrenInterpretResult_WREN_RESULT_SUCCESS => Ok(()),
            wren_sys::WrenInterpretResult_WREN_RESULT_COMPILE_ERROR => match self.error_recv.try_recv() {
                Ok(WrenError::Compile(module, line, msg)) => {
                    Err(VMError::Compile { module, line, error: msg })
                }
                _ => unreachable!()
            },
            wren_sys::WrenInterpretResult_WREN_RESULT_RUNTIME_ERROR => {
                let mut error = "".to_string();
                let mut frames = vec![];
                while let Ok(err) = self.error_recv.try_recv() {
                    match err {
                        WrenError::Runtime(msg) => {error = msg; },
                        WrenError::StackTrace(module, line, msg) => {frames.push(VMStackFrameError {
                            module, line, function: msg
                        }); },
                        _ => unreachable!()
                    }
                }
                Err(VMError::Runtime{
                    error,
                    frames
                })
            },
            _ => unreachable!()
        }
    }

    // manual GC trigger
    pub fn collect_garbage(&self) {
        unsafe {
            wren_sys::wrenCollectGarbage(self.vm)
        }
    }

    // Slot and Handle API
    pub fn ensure_slots(&self, count: usize) {
        unsafe {
            wren_sys::wrenEnsureSlots(self.vm, count as raw::c_int)
        }
    }

    pub fn get_slot_count(&self) -> usize {
        unsafe {
            wren_sys::wrenGetSlotCount(self.vm) as usize
        }
    }

    // TODO Change slots from i32 to usize
    // TODO Make a value-agnostic slot API too.

    pub fn set_slot_bool(&self, slot: i32, val: bool) {
        unsafe {
            wren_sys::wrenSetSlotBool(self.vm, slot as raw::c_int, val)
        }
    }

    pub fn set_slot_double(&self, slot: i32, val: f64) {
        unsafe {
            wren_sys::wrenSetSlotDouble(self.vm, slot as raw::c_int, val)
        }
    }

    pub fn set_slot_null(&self, slot: i32) {
        unsafe {
            wren_sys::wrenSetSlotNull(self.vm, slot as raw::c_int)
        }
    }

    pub fn set_slot_bytes(&self, slot: i32, bytes: &[u8]) {
        unsafe {
            wren_sys::wrenSetSlotBytes(self.vm, slot as raw::c_int, bytes as *const _ as *const raw::c_char, bytes.len() as wren_sys::size_t);
        }
    }

    pub fn set_slot_string<S: AsRef<str>>(&self, slot: i32, string: S) {
        let string = string.as_ref();
        unsafe {
            wren_sys::wrenSetSlotBytes(self.vm, slot as raw::c_int, string.as_ptr() as *const _, string.len() as wren_sys::size_t);
        }
    }

    pub fn get_slot_bool(&self, slot: i32) -> bool {
        unsafe {
            wren_sys::wrenGetSlotBool(self.vm, slot as raw::c_int)
        }
    }

    pub fn get_slot_double(&self, slot: i32) -> f64 {
        unsafe {
            wren_sys::wrenGetSlotDouble(self.vm, slot as raw::c_int)
        }
    }

    pub fn get_slot_bytes(&self, slot: i32) -> Vec<u8> {
        let mut length = 0 as raw::c_int;
        let ptr = unsafe {
            wren_sys::wrenGetSlotBytes(self.vm, slot as raw::c_int, &mut length as *mut _)
        };
        let mut bytes = vec![];

        // Do some pointer maths to get the vector. Hurrah!
        for offset in 0..length {
            unsafe {
                bytes.push(*ptr.offset(offset as isize) as u8)
            }
        }

        bytes
    }

    pub fn get_slot_string(&self, slot: i32) -> String {
        let ptr = unsafe {
            wren_sys::wrenGetSlotString(self.vm, slot as raw::c_int)
        };

        let cstr = unsafe{ ffi::CStr::from_ptr(ptr) };

        cstr.to_string_lossy().to_string()
    }

    pub fn get_slot_type(&self, slot: i32) -> SlotType {
        match unsafe { wren_sys::wrenGetSlotType(self.vm, slot as raw::c_int) } {
            wren_sys::WrenType_WREN_TYPE_NUM => SlotType::Num,
            wren_sys::WrenType_WREN_TYPE_BOOL => SlotType::Bool,
            wren_sys::WrenType_WREN_TYPE_LIST => SlotType::List,
            wren_sys::WrenType_WREN_TYPE_NULL => SlotType::Null,
            wren_sys::WrenType_WREN_TYPE_STRING => SlotType::String,
            wren_sys::WrenType_WREN_TYPE_FOREIGN => SlotType::Foreign,
            wren_sys::WrenType_WREN_TYPE_UNKNOWN => SlotType::Unknown,
            _ => unreachable!()
        }
    }

    pub fn get_variable<M: AsRef<str>, N: AsRef<str>>(&self, module: M, name: N, slot: i32) {
        let module = ffi::CString::new(module.as_ref()).expect("module name conversion failed");
        let name = ffi::CString::new(name.as_ref()).expect("variable name conversion failed");
        unsafe {
            wren_sys::wrenGetVariable(self.vm, module.as_ptr(), name.as_ptr(), slot as raw::c_int)
        }
    }

    pub fn set_slot_new_list(&self, slot: i32) {
        unsafe {
            wren_sys::wrenSetSlotNewList(self.vm, slot as raw::c_int)
        }
    }

    pub fn insert_in_list(&self, list_slot: i32, index: i32, element_slot: i32) {
        unsafe {
            wren_sys::wrenInsertInList(
                self.vm, 
                list_slot as raw::c_int,
                index as raw::c_int,
                element_slot as raw::c_int
            )
        }
    }

    pub fn get_slot_handle(&self, slot: i32) -> Handle {
        Handle {
            handle: unsafe {
                wren_sys::wrenGetSlotHandle(self.vm, slot as raw::c_int)
            },
            vm: self
        }
    }

    pub fn set_slot_handle(&self, slot: i32, handle: &Handle) {
        unsafe {
            wren_sys::wrenSetSlotHandle(self.vm, slot as raw::c_int, handle.handle)
        }
    }

    pub fn get_slot_foreign<T: 'static>(&self, slot: i32) -> Option<&T> {
        self.get_slot_foreign_mut(slot).map(|mr| &*mr)
    }

    pub fn get_slot_foreign_mut<T: 'static>(&self, slot: i32) -> Option<&mut T> {
        unsafe {
            let ptr = wren_sys::wrenGetSlotForeign(self.vm, slot as raw::c_int);
            if ptr != std::ptr::null_mut() {
                let fo = &mut *(ptr as *mut ForeignObject<T>);
                if fo.type_id == any::TypeId::of::<T>() {
                    // Safe to downcast
                    fo.object.as_mut()
                } else {
                    // Incorrect type, unsafe to downcast
                    None
                }
            } else {
                None
            }
        }
    }

    pub fn make_call_handle<S: AsRef<str>>(&self, signature: S) -> Handle {
        let signature = ffi::CString::new(signature.as_ref()).expect("signature conversion failed");
        Handle {
            handle: unsafe {
                wren_sys::wrenMakeCallHandle(self.vm, signature.as_ptr())
            },
            vm: self
        }
    }

    pub fn abort_fiber(&self, slot: i32) {
        unsafe {
            wren_sys::wrenAbortFiber(self.vm, slot as raw::c_int)
        }
    }
}

impl<'a> Drop for VM<'a> {
    fn drop(&mut self) {
        unsafe {
            let conf = wren_sys::wrenGetUserData(self.vm);
            Box::from_raw(conf); // Drop the userdata
            wren_sys::wrenFreeVM(self.vm);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Executor, create_class_objects};

    struct Point {
        x: f64,
    }

    impl Point {
        fn x(vm: &super::VM) {
            vm.ensure_slots(1);
            let obj = vm.get_slot_foreign::<Point>(0).expect("Can only be called on Point class");
            vm.set_slot_double(0, obj.x);
        }

        fn set_x(vm: &super::VM) {
            vm.ensure_slots(2);
            let obj = vm.get_slot_foreign_mut::<Point>(0).expect("Can only be called on Point class");
            if vm.get_slot_type(1) != super::SlotType::Num { panic!("x must be a number"); }
            let new_x = vm.get_slot_double(1);
            obj.x = new_x;
        }
    }

    impl super::Class for Point {
        fn initialize(vm: &super::VM) -> Point {
            vm.ensure_slots(2);
            if vm.get_slot_type(1) != super::SlotType::Num { panic!("constructor must be (<num>)")};
            let x = vm.get_slot_double(1);
            Point {
                x,
            }
        }
    }

    struct Math;

    impl super::Class for Math {
        fn initialize(_: &super::VM) -> Math { Math }
    }

    impl Math {
        fn add5(vm: &super::VM) {
            vm.ensure_slots(2);
            assert_eq!(vm.get_slot_type(1), super::SlotType::Num);
            let i = vm.get_slot_double(1);
            vm.set_slot_double(0, i + 5.0);
        }
    }

    // Why does this require the fully qualified paths? IDK...
    create_class_objects! {
        class crate::tests::Point => point {
            instance("x()") x,
            instance("set_x(_)") set_x
        }

        class crate::tests::Math => math {
            static("add5(_)") add5
        }
    }

    #[test]
    fn init_vm() {
        let _ = super::VM::new_terminal(None);
    }

    #[test]
    fn test_small_wren_program() {
        let vm = super::VM::new_terminal(None);
        vm.execute(|vm| {
            let interp = vm.interpret("main", "System.print(\"I am running in a VM!\")");
            println!("{:?}", interp);
            assert!(interp.is_ok());
        });
    }

    #[test]
    fn test_small_wren_program_call() {
        let vm = super::VM::new_terminal(None);

        vm.execute(|vm| {
            let source = vm.interpret("main", r"
            class GameEngine {
                static update(elapsedTime) {
                    System.print(elapsedTime)
                    return 16.45
                }
            }
            ");
            assert!(source.is_ok());
        });

        vm.execute(|vm| {
            vm.ensure_slots(2);
            vm.get_variable("main", "GameEngine", 0);
            let _ = vm.get_slot_handle(0);
            vm.set_slot_double(1, 32.2);
            let update_handle = vm.make_call_handle("update(_)");
            let interp = vm.call(&update_handle);
            assert!(interp.is_ok());
            assert_eq!(vm.get_slot_type(0), super::SlotType::Num);
            assert_eq!(vm.get_slot_double(0), 16.45);
        });
    }

    #[test]
    fn test_external_module() {
        let mut lib = super::ModuleLibrary::new();
        let mut module = super::Module::new();
        module
            .class::<Math, _>("Math")
            .class::<Point, _>("RawPoint");
        lib.module("main", module);
        let vm = super::VM::new_terminal(Some(&lib));
        vm.execute(|vm| {
            let source = vm.interpret("main", "
            class Math {
                foreign static add5(a)
            }

            foreign class RawPoint {
                construct new(x) {}

                foreign x()
                foreign set_x(val)
            }

            class Point {
                construct new(x) {
                    _rp = RawPoint.new(x)
                }

                x { _rp.x() }
                x=(val) { _rp.set_x(val) }
            }

            class GameEngine {
                static update(elapsedTime) {
                    System.print(elapsedTime)
                    var p = Point.new(3)
                    System.print(p.x)
                    p.x = 10
                    System.print(p.x)
                    return Math.add5(16.45)
                }
            }
            ");
            println!("{:?}", source);
            assert!(source.is_ok());
        });

        vm.execute(|vm| {
            vm.ensure_slots(2);
            vm.get_variable("main", "GameEngine", 0);
            let _ = vm.get_slot_handle(0);
            vm.set_slot_double(1, 32.2);
            let update_handle = vm.make_call_handle("update(_)");
            let interp = vm.call(&update_handle);
            if let Err(e) = interp.clone() {
                eprintln!("{}", e);
            }
            assert!(interp.is_ok());
            assert_eq!(vm.get_slot_type(0), super::SlotType::Num);
            assert_eq!(vm.get_slot_double(0), 21.45);
        });
    }

    #[test]
    fn test_script_module() {
        struct TestLoader;

        impl super::ModuleScriptLoader for TestLoader {
            fn load_script(&mut self, name: String) -> Option<String> {
                if name == "math" {
                    Some("
                    class Math {
                        static add5(val) {
                            return val + 5
                        }
                    }
                    ".into())
                } else {
                    None
                }
            }
        }

        let vm = super::VM::new(super::PrintlnPrinter, TestLoader, None);
        vm.execute(|vm| {
            let source = vm.interpret("main", "
            import \"math\" for Math

            class GameEngine {
                static update(elapsedTime) {
                    System.print(elapsedTime)
                    return Math.add5(16.45)
                }
            }
            ");
            assert!(source.is_ok());
        });

        vm.execute(|vm| {
            vm.ensure_slots(2);
            vm.get_variable("main", "GameEngine", 0);
            let _ = vm.get_slot_handle(0);
            vm.set_slot_double(1, 32.2);
            let update_handle = vm.make_call_handle("update(_)");
            let interp = vm.call(&update_handle);
            assert!(interp.is_ok());
            assert_eq!(vm.get_slot_type(0), super::SlotType::Num);
            assert_eq!(vm.get_slot_double(0), 21.45);
        });
    }
}