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
168
169
170
171
172
173
174
175
176
177
178
use std::convert::From;
use crate::{
binding::{rproc, vm},
types::Value,
util, AnyException, AnyObject, Fixnum, Object, Proc, VerifiedObject,
};
/// `Method`, a method bound to its receiver, as returned by
/// [`Object::method`](trait.Object.html#method.method).
#[derive(Debug)]
#[repr(C)]
pub struct Method {
value: Value,
}
impl Method {
/// Calls the method with `arguments` (`rb_method_call`).
///
/// Raises whatever the method raises; see
/// [`protect_call`](#method.protect_call).
///
/// # Examples
///
/// ```
/// use rutie::{Fixnum, Object, VM};
/// # VM::init();
///
/// let plus = Fixnum::new(40).method("+").unwrap();
///
/// assert_eq!(plus.call(&[Fixnum::new(2).into()]).try_convert_to::<Fixnum>(), Ok(Fixnum::new(42)));
/// ```
pub fn call(&self, arguments: &[AnyObject]) -> AnyObject {
let arguments = util::arguments_to_values(arguments);
AnyObject::from(rproc::method_call(self.value(), &arguments))
}
/// Like [`call`](#method.call), but returns the exception raised by the
/// method instead of propagating it.
///
/// # Examples
///
/// ```
/// use rutie::{Class, Fixnum, Object, VM};
/// # VM::init();
///
/// let divide = Fixnum::new(1).method("/").unwrap();
/// let error = divide.protect_call(&[Fixnum::new(0).into()]).unwrap_err();
///
/// assert!(Class::from_existing("ZeroDivisionError").case_equals(&error));
/// ```
pub fn protect_call(&self, arguments: &[AnyObject]) -> Result<AnyObject, AnyException> {
let method = self.value();
let arguments = util::arguments_to_values(arguments);
vm::protect_value(|| rproc::method_call(method, &arguments))
.map(AnyObject::from)
.map_err(AnyException::from)
}
/// Returns the method's arity, like [`Proc::arity`](struct.Proc.html#method.arity).
///
/// # Examples
///
/// ```
/// use rutie::{Object, RString, VM};
/// # VM::init();
///
/// assert_eq!(RString::new_utf8("").method("upcase").unwrap().arity(), -1);
/// assert_eq!(RString::new_utf8("").method("start_with?").unwrap().arity(), -1);
/// assert_eq!(RString::new_utf8("").method("length").unwrap().arity(), 0);
/// ```
pub fn arity(&self) -> i32 {
Fixnum::from(vm::call_method(self.value(), "arity", &[])).to_i64() as i32
}
/// Returns the method as a `Proc` (Ruby's `to_proc`).
///
/// # Examples
///
/// ```
/// use rutie::{Fixnum, Object, VM};
/// # VM::init();
///
/// let to_s = Fixnum::new(42).method("to_s").unwrap().to_proc();
///
/// assert!(to_s.is_lambda());
/// ```
pub fn to_proc(&self) -> Proc {
Proc::from(vm::call_method(self.value(), "to_proc", &[]))
}
}
impl From<Value> for Method {
fn from(value: Value) -> Self {
Method { value }
}
}
impl Into<Value> for Method {
fn into(self) -> Value {
self.value
}
}
impl Into<AnyObject> for Method {
fn into(self) -> AnyObject {
AnyObject::from(self.value)
}
}
impl Object for Method {
#[inline]
fn value(&self) -> Value {
self.value
}
}
impl VerifiedObject for Method {
fn is_correct_type<T: Object>(object: &T) -> bool {
util::is_method(object.value())
}
fn error_message() -> &'static str {
"Error converting to Method"
}
}
impl PartialEq for Method {
fn eq(&self, other: &Self) -> bool {
self.equals(other)
}
}
#[cfg(test)]
mod tests {
use crate::{Array, Class, Fixnum, Method, Object, RString, VM};
#[test]
fn test_method() {
crate::on_ruby_thread(|| {
let array = Array::new().push(Fixnum::new(1));
let push = array.method("push").unwrap();
push.call(&[Fixnum::new(2).into()]);
assert_eq!(array.length(), 2);
assert_eq!(push.arity(), -1);
assert_eq!(array.method_arity("push"), -1);
assert_eq!(array.method_arity("no_such_method"), 0);
let frozen = Array::new().freeze();
let error = frozen
.method("push")
.unwrap()
.protect_call(&[Fixnum::new(1).into()])
.unwrap_err();
assert!(Class::from_existing("FrozenError").case_equals(&error));
let upcase = RString::new_utf8("abc").method("upcase").unwrap().to_proc();
let result = upcase.call(&[]);
assert_eq!(result.try_convert_to::<RString>().unwrap().to_str(), "ABC");
assert!(VM::eval("1.method(:+)")
.unwrap()
.try_convert_to::<Method>()
.is_ok());
assert!(VM::eval("proc {}")
.unwrap()
.try_convert_to::<Method>()
.is_err());
assert_eq!(
Class::from_existing("Array").instance_method_arity("push"),
-1
);
});
}
}