~tim/scheme-vm

scheme-vm/src/rb.rs -rw-r--r-- 4.2 KiB
00cdee12Tim Morgan Remove old code 3 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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// borrowed from https://github.com/ubcsanskrit/sanscript.rb (MIT)

use ruby_sys::{class, array, string};
use ruby_sys::types::{c_char, c_int, c_long};
use ruby_sys::util::{rb_const_get};
use ruby_sys::vm::{rb_raise};
use ruby_sys::value::RubySpecialConsts::{Nil, True, False};
use ruby_sys::rb_cObject;

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

pub const RB_NIL: Value = Value { value: Nil as usize };

#[allow(dead_code)]
pub const RB_TRUE: Value = Value { value: True as usize };

#[allow(dead_code)]
pub const RB_FALSE: Value = Value { value: False as usize };

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

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

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)) }
}

#[allow(unused_macros)]
macro_rules! str2sym {
    ($s:expr) => {
        unsafe { ::ruby_sys::symbol::rb_id2sym(str2rbid!($s)) }
    }
}

macro_rules! int2rbnum {
    ($s:expr) => {
        unsafe { ::ruby_sys::fixnum::rb_int2inum($s as isize) }
    }
}

#[allow(dead_code)]
pub fn define_module(name: &str) -> Value {
    unsafe { class::rb_define_module(str2cstrp!(name)) }
}

#[allow(dead_code)]
pub fn define_class(name: &str, superclass: &Value) -> Value {
    let parent = if *superclass == RB_NIL {
        unsafe { rb_cObject }
    } else {
        *superclass
    };
    unsafe { class::rb_define_class(str2cstrp!(name), parent) }
}

#[allow(dead_code)]
pub fn define_class_under(outer: &Value, name: &str, superclass: &Value) -> Value {
    let parent = if *superclass == RB_NIL {
        unsafe { rb_cObject }
    } else {
        *superclass
    };
    unsafe { class::rb_define_class_under(*outer, str2cstrp!(name), parent) }
}

pub fn class_new_instance(klass: &Value, args: Vec<Value>) -> Value {
    let argc = args.len() as c_int;
    let instance = unsafe { class::rb_class_new_instance(argc, args.as_ptr(), *klass) };
    instance
}

#[allow(dead_code)]
pub fn ivar_set(object: &Value, name: &str, value: &Value) -> Value {
    unsafe { class::rb_ivar_set(*object, str2rbid!(name), *value) }
}

pub fn ivar_get(object: &Value, name: &str) -> Value {
    unsafe { class::rb_ivar_get(*object, str2rbid!(name)) }
}

#[allow(dead_code)]
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) }
}

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

#[allow(dead_code)]
pub fn extend_object(object: &Value, module: &Value) {
    unsafe { class::rb_extend_object(*object, *module) }
}

pub fn ary_new() -> Value {
    unsafe { array::rb_ary_new() }
}

pub fn ary_push(array: Value, item: Value) -> Value {
    let new_array = unsafe { array::rb_ary_push(array, item) };
    new_array
}

pub fn str_new(string: &str) -> Value {
    let str = string.as_ptr() as *const c_char;
    let len = string.len() as c_long;
    unsafe { string::rb_str_new(str, len) }
}

pub fn vec2rbarr(vec: Vec<Value>) -> Value {
    let mut arr = ary_new();
    for item in vec {
        arr = ary_push(arr, item);
    }
    arr
}

pub fn const_get(name: &str, class: &Value) -> Value {
    let parent = if *class == RB_NIL {
        unsafe { rb_cObject }
    } else {
        *class
    };
    unsafe { rb_const_get(parent, str2rbid!(name)) }
}

extern "C" {
    pub fn rb_exc_raise(object: Value);
    pub fn rb_gc_enable();
    pub fn rb_gc_disable();
}

#[allow(dead_code)]
pub fn raise(exception: &Value, message: &str) {
    unsafe { rb_raise(*exception, str2cstrp!(message)) };
}

pub fn raise_instance(object: &Value) {
    unsafe { rb_exc_raise(*object) };
}

pub fn gc_enable() {
    unsafe { rb_gc_enable() };
}

pub fn gc_disable() {
    unsafe { rb_gc_disable() };
}