libcruby_sys/
lib.rs

1#![allow(non_snake_case)]
2#![allow(non_camel_case_types)]
3
4extern crate libc;
5
6use std::ffi::CStr;
7use std::mem::size_of;
8
9pub const PKG_VERSION: &'static str = env!("CARGO_PKG_VERSION");
10
11pub fn check_version() {
12    let raw_version = unsafe { CStr::from_ptr(HELIX_RUNTIME_VERSION) };
13    let version = raw_version.to_str().expect("HELIX_RUNTIME_VERSION must be defined");
14
15    if PKG_VERSION != version {
16        panic!("libcsys-ruby version ({}) doesn't match helix_runtime version ({}).", PKG_VERSION, version);
17    }
18
19    if size_of::<usize>() != size_of::<u32>() && size_of::<usize>() != size_of::<u64>() {
20        panic!("unsupported architecture, size_of::<usize>() = {}", size_of::<usize>());
21    }
22}
23
24pub type void = libc::c_void;
25pub type c_func = *const void;
26pub type c_string = *const libc::c_char;
27// pub type c_func = extern "C" fn(...);
28
29#[repr(C)]
30#[derive(Eq, PartialEq, Hash, Copy, Clone, Debug)]
31pub struct ID(*mut void);
32
33unsafe impl Sync for ID {}
34
35#[repr(C)]
36#[derive(Eq, PartialEq, Copy, Clone, Debug)]
37pub struct VALUE(*mut void);
38
39#[repr(C)]
40#[derive(Eq, PartialEq, Copy, Clone, Debug)]
41pub struct RubyException(isize);
42
43impl RubyException {
44    pub fn new() -> RubyException {
45        RubyException(0)
46    }
47
48    pub fn empty() -> RubyException {
49        RubyException(0)
50    }
51
52    pub fn for_tag(tag: isize) -> RubyException {
53        RubyException(tag)
54    }
55}
56
57pub const EMPTY_EXCEPTION: RubyException = RubyException(0);
58
59#[repr(C)]
60pub enum st_retval {
61    ST_CONTINUE,
62    ST_STOP,
63    ST_DELETE,
64    // ST_CHECK
65}
66
67#[cfg_attr(windows, link(name="helix-runtime"))]
68extern "C" {
69    #[link_name = "HELIX_RUNTIME_VERSION"]
70    pub static HELIX_RUNTIME_VERSION: c_string;
71
72    #[link_name = "HELIX_Qfalse"]
73    pub static Qfalse: VALUE;
74
75    #[link_name = "HELIX_Qtrue"]
76    pub static Qtrue: VALUE;
77
78    #[link_name = "HELIX_Qnil"]
79    pub static Qnil: VALUE;
80
81    #[link_name = "HELIX_PRIsVALUE"]
82    pub static PRIsVALUE: c_string;
83
84    #[link_name = "HELIX_SPRINTF_TO_S"]
85    pub static SPRINTF_TO_S: c_string;
86
87    #[link_name = "HELIX_SPRINTF_INSPECT"]
88    pub static SPRINTF_INSPECT: c_string;
89
90    #[link_name = "rb_cObject"]
91    pub static rb_cObject: VALUE;
92
93    #[link_name = "rb_cBasicObject"]
94    pub static rb_cBasicObject: VALUE;
95
96    #[link_name = "rb_eRuntimeError"]
97    pub static rb_eRuntimeError: VALUE;
98
99    #[link_name = "rb_eTypeError"]
100    pub static rb_eTypeError: VALUE;
101
102    #[link_name = "HELIX_RSTRING_LEN"]
103    pub fn RSTRING_LEN(string: VALUE) -> isize;
104
105    #[link_name = "HELIX_RSTRING_PTR"]
106    pub fn RSTRING_PTR(string: VALUE) -> c_string;
107
108    #[link_name = "HELIX_rb_utf8_str_new"]
109    pub fn rb_utf8_str_new(string: c_string, len: libc::c_long) -> VALUE;
110
111    #[link_name = "HELIX_RARRAY_LEN"]
112    pub fn RARRAY_LEN(array: VALUE) -> isize;
113
114    #[link_name = "HELIX_RARRAY_PTR"]
115    pub fn RARRAY_PTR(array: VALUE) -> *mut VALUE;
116
117    #[link_name = "HELIX_RARRAY_CONST_PTR"]
118    pub fn RARRAY_CONST_PTR(array: VALUE) -> *const VALUE;
119
120    #[link_name = "HELIX_RHASH_SIZE"]
121    pub fn RHASH_SIZE(hash: VALUE) -> isize;
122
123    #[link_name = "HELIX_RB_TYPE_P"]
124    pub fn RB_TYPE_P(val: VALUE, rb_type: isize) -> bool;
125
126    #[link_name = "HELIX_RB_NIL_P"]
127    pub fn RB_NIL_P(val: VALUE) -> bool;
128
129    #[link_name = "HELIX_RTEST"]
130    pub fn RTEST(val: VALUE) -> bool;
131
132    #[link_name = "HELIX_TYPE"]
133    pub fn TYPE(val: VALUE) -> isize;
134
135    pub fn rb_check_type(v: VALUE, rb_type: isize);
136
137    #[link_name = "HELIX_NUM2U64"]
138    pub fn NUM2U64(v: VALUE) -> u64;
139
140    #[link_name = "HELIX_U642NUM"]
141    pub fn U642NUM(num: u64) -> VALUE;
142
143    #[link_name = "HELIX_NUM2I64"]
144    pub fn NUM2I64(v: VALUE) -> i64;
145
146    #[link_name = "HELIX_I642NUM"]
147    pub fn I642NUM(num: i64) -> VALUE;
148
149    #[link_name = "HELIX_NUM2U32"]
150    pub fn NUM2U32(v: VALUE) -> u32;
151
152    #[link_name = "HELIX_U322NUM"]
153    pub fn U322NUM(num: u32) -> VALUE;
154
155    #[link_name = "HELIX_NUM2I32"]
156    pub fn NUM2I32(v: VALUE) -> i32;
157
158    #[link_name = "HELIX_I322NUM"]
159    pub fn I322NUM(num: i32) -> VALUE;
160
161    #[link_name = "HELIX_NUM2F64"]
162    pub fn NUM2F64(v: VALUE) -> f64;
163
164    #[link_name = "HELIX_F642NUM"]
165    pub fn F642NUM(num: f64) -> VALUE;
166
167    #[link_name = "HELIX_OBJ_FROZEN"]
168    pub fn OBJ_FROZEN(v: VALUE) -> bool;
169
170    #[link_name = "HELIX_T_OBJECT"]
171    pub static T_OBJECT: isize;
172
173    #[link_name = "HELIX_T_STRING"]
174    pub static T_STRING: isize;
175
176    #[link_name = "HELIX_T_ARRAY"]
177    pub static T_ARRAY: isize;
178
179    #[link_name = "HELIX_T_HASH"]
180    pub static T_HASH: isize;
181
182    #[link_name = "HELIX_T_TRUE"]
183    pub static T_TRUE: isize;
184
185    #[link_name = "HELIX_T_FALSE"]
186    pub static T_FALSE: isize;
187
188    #[link_name = "HELIX_T_SYMBOL"]
189    pub static T_SYMBOL: isize;
190
191    #[link_name = "HELIX_T_FIXNUM"]
192    pub static T_FIXNUM: isize;
193
194    #[link_name = "HELIX_T_FLOAT"]
195    pub static T_FLOAT: isize;
196
197    #[link_name = "HELIX_T_BIGNUM"]
198    pub static T_BIGNUM: isize;
199
200    #[link_name = "HELIX_T_DATA"]
201    pub static T_DATA: isize;
202
203    // unknown if working?
204    // fn rb_define_variable(name: c_string, value: *const VALUE);
205    pub fn rb_obj_class(obj: VALUE) -> VALUE;
206    pub fn rb_obj_classname(obj: VALUE) -> c_string;
207    pub fn rb_const_get(class: VALUE, name: ID) -> VALUE;
208    pub fn rb_define_global_const(name: c_string, value: VALUE);
209    pub fn rb_define_module(name: c_string) -> VALUE;
210    pub fn rb_define_module_under(namespace: VALUE, name: c_string) -> VALUE;
211    pub fn rb_define_class(name: c_string, superclass: VALUE) -> VALUE;
212    pub fn rb_define_class_under(namespace: VALUE, name: c_string, superclass: VALUE) -> VALUE;
213    pub fn rb_define_alloc_func(klass: VALUE, func: extern "C" fn(klass: VALUE) -> VALUE);
214    pub fn rb_define_method(class: VALUE, name: c_string, func: c_func, arity: isize);
215    pub fn rb_define_singleton_method(class: VALUE, name: c_string, func: c_func, arity: isize);
216    pub fn rb_enc_get_index(obj: VALUE) -> isize;
217    pub fn rb_utf8_encindex() -> isize;
218    pub fn rb_sprintf(specifier: c_string, ...) -> VALUE;
219    pub fn rb_inspect(value: VALUE) -> VALUE;
220    pub fn rb_intern(string: c_string) -> ID;
221    pub fn rb_intern_str(string: VALUE) -> ID;
222    pub fn rb_sym2id(symbol: VALUE) -> ID;
223    pub fn rb_id2sym(id: ID) -> VALUE;
224    pub fn rb_id2str(id: ID) -> VALUE;
225    pub fn rb_ary_new() -> VALUE;
226    pub fn rb_ary_new_capa(capa: isize) -> VALUE;
227    pub fn rb_ary_entry(ary: VALUE, offset: isize) -> VALUE;
228    pub fn rb_ary_push(ary: VALUE, item: VALUE) -> VALUE;
229    pub fn rb_hash_new() -> VALUE;
230    pub fn rb_hash_aref(hash: VALUE, key: VALUE) -> VALUE;
231    pub fn rb_hash_aset(hash: VALUE, key: VALUE, value: VALUE) -> VALUE;
232    pub fn rb_hash_foreach(hash: VALUE, f: extern "C" fn(key: VALUE, value: VALUE, farg: *mut void) -> st_retval, farg: *mut void);
233    pub fn rb_gc_mark(value: VALUE);
234    pub fn rb_funcall(value: VALUE, mid: ID, argc: libc::c_int, ...) -> VALUE;
235    pub fn rb_funcallv(value: VALUE, mid: ID, argc: libc::c_int, argv: *const VALUE) -> VALUE;
236    pub fn rb_scan_args(argc: libc::c_int, argv: *const VALUE, fmt: c_string, ...);
237    pub fn rb_block_given_p() -> bool;
238    pub fn rb_yield(value: VALUE) -> VALUE;
239    pub fn rb_obj_dup(value: VALUE) -> VALUE;
240    pub fn rb_obj_init_copy(value: VALUE, orig: VALUE) -> VALUE;
241
242    pub fn rb_raise(exc: VALUE, string: c_string, ...) -> !;
243    pub fn rb_jump_tag(state: RubyException) -> !;
244    pub fn rb_protect(try: extern "C" fn(v: *mut void) -> VALUE,
245                      arg: *mut void,
246                      state: *mut RubyException)
247                      -> VALUE;
248
249    #[link_name = "HELIX_rb_str_valid_encoding_p"]
250    pub fn rb_str_valid_encoding_p(string: VALUE) -> bool;
251
252    #[link_name = "HELIX_rb_str_ascii_only_p"]
253    pub fn rb_str_ascii_only_p(string: VALUE) -> bool;
254
255    #[link_name = "HELIX_Data_Wrap_Struct"]
256    pub fn Data_Wrap_Struct(klass: VALUE, mark: extern "C" fn(*mut void), free: extern "C" fn(*mut void), data: *mut void) -> VALUE;
257
258    #[link_name = "HELIX_Data_Get_Struct_Value"]
259    pub fn Data_Get_Struct_Value(obj: VALUE) -> *mut void;
260
261    #[link_name = "HELIX_Data_Set_Struct_Value"]
262    pub fn Data_Set_Struct_Value(obj: VALUE, data: *mut void);
263}
264
265#[inline]
266pub unsafe fn NUM2USIZE(v: VALUE) -> usize {
267    if size_of::<usize>() == size_of::<u32>() {
268        NUM2U32(v) as usize
269    } else {
270        NUM2U64(v) as usize
271    }
272}
273
274#[inline]
275pub unsafe fn USIZE2NUM(num: usize) -> VALUE {
276    if size_of::<usize>() == size_of::<u32>() {
277        U322NUM(num as u32)
278    } else {
279        U642NUM(num as u64)
280    }
281}
282
283#[inline]
284pub unsafe fn NUM2ISIZE(v: VALUE) -> isize {
285    if size_of::<isize>() == size_of::<i32>() {
286        NUM2I32(v) as isize
287    } else {
288        NUM2I64(v) as isize
289    }
290}
291
292#[inline]
293pub unsafe fn ISIZE2NUM(num: isize) -> VALUE {
294    if size_of::<isize>() == size_of::<i32>() {
295        I322NUM(num as i32)
296    } else {
297        I642NUM(num as i64)
298    }
299}