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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::collections::HashSet;
use std::convert::AsRef;
use std::ffi::{c_void, CString};
use std::fmt;
use std::hash::{Hash, Hasher};
use crate::def::{ClassLike, Define, EnclosingRubyScope, Method};
use crate::method;
use crate::sys;
use crate::value::Value;
use crate::{Mrb, MrbError};
pub struct Spec {
name: String,
cstring: CString,
methods: HashSet<method::Spec>,
enclosing_scope: Option<EnclosingRubyScope>,
}
impl Spec {
pub fn new<T>(name: T, enclosing_scope: Option<EnclosingRubyScope>) -> Self
where
T: AsRef<str>,
{
let cstr = CString::new(name.as_ref()).expect("name for data type");
Self {
name: name.as_ref().to_owned(),
cstring: cstr,
methods: HashSet::new(),
enclosing_scope,
}
}
pub fn value(&self, interp: &Mrb) -> Option<Value> {
let rclass = self.rclass(interp)?;
let module = unsafe { sys::mrb_sys_module_value(rclass) };
Some(Value::new(interp, module))
}
}
impl ClassLike for Spec {
fn add_method(&mut self, name: &str, method: Method, args: sys::mrb_aspec) {
let spec = method::Spec::new(method::Type::Instance, name, method, args);
self.methods.insert(spec);
}
fn add_self_method(&mut self, name: &str, method: Method, args: sys::mrb_aspec) {
let spec = method::Spec::new(method::Type::Module, name, method, args);
self.methods.insert(spec);
}
fn cstring(&self) -> &CString {
&self.cstring
}
fn name(&self) -> &str {
&self.name
}
fn enclosing_scope(&self) -> Option<EnclosingRubyScope> {
self.enclosing_scope.clone()
}
fn rclass(&self, interp: &Mrb) -> Option<*mut sys::RClass> {
let mrb = interp.borrow().mrb;
if let Some(ref scope) = self.enclosing_scope {
if let Some(scope) = scope.rclass(interp) {
let defined = unsafe {
sys::mrb_const_defined_at(
mrb,
sys::mrb_sys_obj_value(scope as *mut c_void),
interp.borrow_mut().sym_intern(self.name.as_str()),
)
};
if defined == 0 {
None
} else {
Some(unsafe { sys::mrb_module_get_under(mrb, scope, self.cstring().as_ptr()) })
}
} else {
None
}
} else {
let defined = unsafe {
sys::mrb_const_defined_at(
mrb,
sys::mrb_sys_obj_value((*mrb).object_class as *mut c_void),
interp.borrow_mut().sym_intern(self.name.as_str()),
)
};
if defined == 0 {
None
} else {
Some(unsafe { sys::mrb_module_get(mrb, self.cstring().as_ptr()) })
}
}
}
}
impl fmt::Debug for Spec {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self)
}
}
impl fmt::Display for Spec {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "mruby module spec -- {}", self.fqname())
}
}
impl Hash for Spec {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name().hash(state);
self.enclosing_scope().hash(state);
}
}
impl Eq for Spec {}
impl PartialEq for Spec {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Define for Spec {
fn define(&self, interp: &Mrb) -> Result<*mut sys::RClass, MrbError> {
let mrb = interp.borrow().mrb;
let rclass = if let Some(ref scope) = self.enclosing_scope {
let scope = scope
.rclass(interp)
.ok_or_else(|| MrbError::NotDefined(scope.fqname()))?;
unsafe { sys::mrb_define_module_under(mrb, scope, self.cstring().as_ptr()) }
} else {
unsafe { sys::mrb_define_module(mrb, self.cstring().as_ptr()) }
};
for method in &self.methods {
unsafe {
method.define(&interp, rclass)?;
}
}
Ok(rclass)
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::rc::Rc;
use crate::class;
use crate::def::{ClassLike, EnclosingRubyScope};
use crate::eval::MrbEval;
use crate::module::Spec;
#[test]
fn rclass_for_undef_root_module() {
let interp = crate::interpreter().expect("mrb init");
let spec = Spec::new("Foo", None);
assert!(spec.rclass(&interp).is_none());
}
#[test]
fn rclass_for_undef_nested_module() {
let interp = crate::interpreter().expect("mrb init");
let scope = Spec::new("Kernel", None);
let scope = EnclosingRubyScope::module(Rc::new(RefCell::new(scope)));
let spec = Spec::new("Foo", Some(scope));
assert!(spec.rclass(&interp).is_none());
}
#[test]
fn rclass_for_root_module() {
let interp = crate::interpreter().expect("mrb init");
let spec = Spec::new("Kernel", None);
assert!(spec.rclass(&interp).is_some());
}
#[test]
fn rclass_for_nested_module() {
let interp = crate::interpreter().expect("mrb init");
interp
.eval("module Foo; module Bar; end; end")
.expect("eval");
let scope = Spec::new("Foo", None);
let scope = EnclosingRubyScope::module(Rc::new(RefCell::new(scope)));
let spec = Spec::new("Bar", Some(scope));
assert!(spec.rclass(&interp).is_some());
}
#[test]
fn rclass_for_nested_module_under_class() {
let interp = crate::interpreter().expect("mrb init");
interp
.eval("class Foo; module Bar; end; end")
.expect("eval");
let scope = class::Spec::new("Foo", None, None);
let scope = EnclosingRubyScope::class(Rc::new(RefCell::new(scope)));
let spec = Spec::new("Bar", Some(scope));
assert!(spec.rclass(&interp).is_some());
}
}