~tim/scheme-vm

ref: 82e799de37eb74f44fef9422a1cc72e4a56ac1fa scheme-vm/src/rb.rs -rw-r--r-- 1.7 KiB
82e799deTim Morgan Use ruby-sys to integrate better with Ruby 4 years ago
                                                                                
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
// borrowed from https://github.com/ubcsanskrit/sanscript.rb (MIT)

use ruby_sys::class;
use ruby_sys::value::RubySpecialConsts::{Nil, True, False};

pub use ruby_sys::types::{CallbackPtr, Value};

pub const RB_NIL: Value = Value { value: Nil as usize };
pub const RB_TRUE: Value = Value { value: True as usize };
pub const RB_FALSE: Value = Value { value: False as usize };

//
// Helper functions/macros for dealing with Ruby and CStrings
//

macro_rules! str2cstr {
  ($s:expr) => { ::std::ffi::CString::new($s).unwrap() }
}

macro_rules! str2cstrp {
  ($s:expr) => { str2cstr!($s).as_ptr() }
}

macro_rules! rbstr2cstrp {
  ($s:expr) => { ::ruby_sys::string::rb_string_value_cstr($s) }
}

macro_rules! rbstr2str {
  ($s:expr) => {
    unsafe { ::std::ffi::CStr::from_ptr(rbstr2cstrp!($s)).to_str().unwrap() }
  }
}

macro_rules! str2rbid {
  ($s:expr) => { ::ruby_sys::util::rb_intern(str2cstrp!($s)) }
}

macro_rules! str2sym {
  ($s:expr) => {
    unsafe { ::ruby_sys::symbol::rb_id2sym(str2rbid!($s)) }
  }
}

pub fn define_module(name: &str) -> Value {
  unsafe { class::rb_define_module(str2cstrp!(name)) }
}

pub fn define_module_under(parent: &Value, name: &str) -> Value {
  unsafe { class::rb_define_module_under(*parent, str2cstrp!(name)) }
}

pub fn define_method(module: &Value, name: &str, method: CallbackPtr, argc: i32) {
  unsafe { class::rb_define_method(*module, str2cstrp!(name), method, argc) }
}

pub fn define_singleton_method(module: &Value, name: &str, method: CallbackPtr, argc: i32) {
  unsafe { class::rb_define_singleton_method(*module, str2cstrp!(name), method, argc) }
}

pub fn extend_object(object: &Value, module: &Value) {
  unsafe { class::rb_extend_object(*object, *module) }
}