1extern crate libc;
2
3#[allow(dead_code, non_snake_case, non_upper_case_globals, non_camel_case_types)]
4mod ffi;
5
6use self::ffi::*;
7use std::ffi::CString;
8
9pub struct Value(VALUE);
10
11impl Value {
12 pub fn from(v: VALUE) -> Value {
13 Value(v)
14 }
15
16 pub fn is_nil(&self) -> bool {
17 NIL_P(self.0)
18 }
19
20 pub fn is_string(&self) -> bool {
21 RB_TYPE_P(self.0, T_STRING)
22 }
23
24 pub fn is_fixnum(&self) -> bool {
25 RB_TYPE_P(self.0, T_FIXNUM)
26 }
27
28 pub fn is_symbol(&self) -> bool {
29 RB_TYPE_P(self.0, T_SYMBOL)
30 }
31
32 pub fn is_float(&self) -> bool {
33 RB_TYPE_P(self.0, T_FLOAT)
34 }
35
36 pub fn is_object(&self) -> bool {
37 RB_TYPE_P(self.0, T_OBJECT)
38 }
39}
40
41pub struct RubyVM;
42
43impl RubyVM {
44 pub fn new() -> RubyVM {
45 unsafe {
46 ruby_init();
47 }
48
49 RubyVM
50 }
51
52 pub fn eval(s: &str) -> Result<Value, ()> {
53 let mut state = 0;
54 let s = try!(CString::new(s).map_err(|_| ()));
55 unsafe {
56 let result = rb_eval_string_protect(s.as_ptr(), &mut state);
57 if state != 0 {
58 return Err(());
59 }
60 Ok(Value::from(result))
61 }
62 }
63}
64
65impl Drop for RubyVM {
66 fn drop(&mut self) {
67 unsafe {
68 ruby_cleanup(0)
69 }
70 }
71}
72
73#[test]
74fn it_works() {
75 let _ = RubyVM::new();
76}