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
extern crate libc;

#[allow(dead_code, non_snake_case, non_upper_case_globals, non_camel_case_types)]
mod ffi;

use self::ffi::*;
use std::ffi::CString;

pub struct Value(VALUE);

impl Value {
    pub fn from(v: VALUE) -> Value {
        Value(v)
    }

    pub fn is_nil(&self) -> bool {
        NIL_P(self.0)
    }

    pub fn is_string(&self) -> bool {
        RB_TYPE_P(self.0, T_STRING)
    }

    pub fn is_fixnum(&self) -> bool {
        RB_TYPE_P(self.0, T_FIXNUM)
    }

    pub fn is_symbol(&self) -> bool {
        RB_TYPE_P(self.0, T_SYMBOL)
    }

    pub fn is_float(&self) -> bool {
        RB_TYPE_P(self.0, T_FLOAT)
    }

    pub fn is_object(&self) -> bool {
        RB_TYPE_P(self.0, T_OBJECT)
    }
}

pub struct RubyVM;

impl RubyVM {
    pub fn new() -> RubyVM {
        unsafe {
            ruby_init();
        }

        RubyVM
    }

    pub fn eval(s: &str) -> Result<Value, ()> {
        let mut state = 0;
        let s = try!(CString::new(s).map_err(|_| ()));
        unsafe {
            let result = rb_eval_string_protect(s.as_ptr(), &mut state);
            if state != 0 {
                return Err(());
            }
            Ok(Value::from(result))
        }
    }
}

impl Drop for RubyVM {
    fn drop(&mut self) {
        unsafe {
            ruby_cleanup(0)
        }
    }
}

#[test]
fn it_works() {
    let _ = RubyVM::new();
}