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
#![allow(improper_ctypes)]

extern crate libc;
use libc::{c_void, size_t, c_char, c_int, c_double};

#[repr(C)]
pub struct WrenVM;

#[repr(C)]
pub struct WrenHandle;

pub type WrenReallocateFn = Option<unsafe extern "C" fn(memory: *mut c_void, new_size: size_t)
                                                        -> *mut c_void>;
pub type WrenForeignMethodFn = Option<unsafe extern "C" fn(vm: *mut WrenVM)>;
pub type WrenFinalizerFn = Option<unsafe extern "C" fn(data: *mut c_void)>;
pub type WrenLoadModuleFn = Option<unsafe extern "C" fn(vm: *mut WrenVM, name: *const c_char)
                                                        -> *mut c_char>;
pub type WrenBindForeignMethodFn = Option<unsafe extern "C" fn(vm: *mut WrenVM,
                                                               module: *const c_char,
                                                               class_name: *const c_char,
                                                               is_static: c_int,
                                                               signature: *const c_char)
                                                               -> WrenForeignMethodFn>;
pub type WrenWriteFn = Option<unsafe extern "C" fn(vm: *mut WrenVM, text: *const c_char)>;

#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C)]
pub enum WrenErrorType {
    Compile,
    Runtime,
    StackTrace,
}

pub type WrenErrorFn = Option<unsafe extern "C" fn(vm: *mut WrenVM,
                                                   _type: WrenErrorType,
                                                   module: *const c_char,
                                                   line: c_int,
                                                   message: *const c_char)>;

#[derive(Copy, Clone)]
#[repr(C)]
pub struct WrenForeignClassMethods {
    pub allocate: WrenForeignMethodFn,
    pub finalize: WrenFinalizerFn,
}

pub type WrenBindForeignClassFn = Option<unsafe extern "C" fn(vm: *mut WrenVM,
                                                              module: *const c_char,
                                                              class_name: *const c_char)
                                                              -> WrenForeignClassMethods>;

#[repr(C)]
pub struct WrenConfiguration {
    pub reallocate_fn: WrenReallocateFn,
    pub load_module_fn: WrenLoadModuleFn,
    pub bind_foreign_method_fn: WrenBindForeignMethodFn,
    pub bind_foreign_class_fn: WrenBindForeignClassFn,
    pub write_fn: WrenWriteFn,
    pub error_fn: WrenErrorFn,
    pub initial_heap_size: size_t,
    pub min_heap_size: size_t,
    pub heap_growth_percent: c_int,
    pub user_data: *mut c_void,
}

#[derive(Copy, Clone, PartialEq, Debug)]
#[repr(C)]
pub enum WrenInterpretResult {
    Success,
    CompileError,
    RuntimeError,
}

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

extern "C" {
    pub fn wrenInitConfiguration(configuration: *mut WrenConfiguration);
    pub fn wrenNewVM(configuration: *mut WrenConfiguration) -> *mut WrenVM;
    pub fn wrenFreeVM(vm: *mut WrenVM);
    pub fn wrenCollectGarbage(vm: *mut WrenVM);
    pub fn wrenInterpret(vm: *mut WrenVM, source: *const c_char) -> WrenInterpretResult;
    pub fn wrenMakeCallHandle(vm: *mut WrenVM, signature: *const c_char) -> *mut WrenHandle;
    pub fn wrenCall(vm: *mut WrenVM, method: *mut WrenHandle) -> WrenInterpretResult;
    pub fn wrenReleaseHandle(vm: *mut WrenVM, handle: *mut WrenHandle);

    pub fn wrenGetSlotCount(vm: *mut WrenVM) -> c_int;
    pub fn wrenEnsureSlots(vm: *mut WrenVM, num_slots: c_int);
    pub fn wrenGetSlotType(vm: *mut WrenVM, slot: c_int) -> WrenType;
    pub fn wrenGetSlotBool(vm: *mut WrenVM, slot: c_int) -> c_int;
    pub fn wrenGetSlotBytes(vm: *mut WrenVM, slot: c_int, length: *mut c_int) -> *const c_char;
    pub fn wrenGetSlotDouble(vm: *mut WrenVM, slot: c_int) -> c_double;
    pub fn wrenGetSlotForeign(vm: *mut WrenVM, slot: c_int) -> *mut c_void;
    pub fn wrenGetSlotString(vm: *mut WrenVM, slot: c_int) -> *const c_char;
    pub fn wrenGetSlotHandle(vm: *mut WrenVM, slot: c_int) -> *mut WrenHandle;

    pub fn wrenSetSlotBool(vm: *mut WrenVM, slot: c_int, value: c_int);
    pub fn wrenSetSlotBytes(vm: *mut WrenVM, slot: c_int, bytes: *const c_char, length: size_t);
    pub fn wrenSetSlotDouble(vm: *mut WrenVM, slot: c_int, value: c_double);
    pub fn wrenSetSlotNewForeign(vm: *mut WrenVM,
                                 slot: c_int,
                                 class_slot: c_int,
                                 size: size_t)
                                 -> *mut c_void;
    pub fn wrenSetSlotNewList(vm: *mut WrenVM, slot: c_int);
    pub fn wrenSetSlotNull(vm: *mut WrenVM, slot: c_int);
    pub fn wrenSetSlotString(vm: *mut WrenVM, slot: c_int, text: *const c_char);
    pub fn wrenSetSlotHandle(vm: *mut WrenVM, slot: c_int, handle: *mut WrenHandle);

    pub fn wrenGetListCount(vm: *mut WrenVM, slot: c_int) -> c_int;
    pub fn wrenGetListElement(vm: *mut WrenVM,
                              list_slot: c_int,
                              index: c_int,
                              element_slot: c_int);
    pub fn wrenInsertInList(vm: *mut WrenVM, list_slot: c_int, index: c_int, element_slot: c_int);
    pub fn wrenGetVariable(vm: *mut WrenVM,
                           module: *const c_char,
                           name: *const c_char,
                           slot: c_int);
    pub fn wrenAbortFiber(vm: *mut WrenVM, slot: c_int);
    pub fn wrenGetUserData(vm: *mut WrenVM) -> *mut c_void;
    pub fn wrenSetUserData(vm: *mut WrenVM, user_data: *mut c_void);
}