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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::any::Any;
use std::borrow::Cow;
use std::collections::HashSet;
use std::ffi::{CStr, CString};
use std::hash::{Hash, Hasher};
use std::ptr::NonNull;

use crate::def::{ConstantNameError, EnclosingRubyScope, Free, Method, NotDefinedError};
use crate::error::Error;
use crate::ffi::InterpreterExtractError;
use crate::method;
use crate::sys;
use crate::Artichoke;

mod registry;

pub use registry::Registry;

#[derive(Debug)]
pub struct Builder<'a> {
    interp: &'a mut Artichoke,
    spec: &'a Spec,
    is_mrb_tt_data: bool,
    super_class: Option<NonNull<sys::RClass>>,
    methods: HashSet<method::Spec>,
}

impl<'a> Builder<'a> {
    #[must_use]
    pub fn for_spec(interp: &'a mut Artichoke, spec: &'a Spec) -> Self {
        Self {
            interp,
            spec,
            is_mrb_tt_data: false,
            super_class: None,
            methods: HashSet::default(),
        }
    }

    #[must_use]
    pub fn value_is_rust_object(mut self) -> Self {
        self.is_mrb_tt_data = true;
        self
    }

    pub fn with_super_class<T, U>(mut self, classname: U) -> Result<Self, Error>
    where
        T: Any,
        U: Into<Cow<'static, str>>,
    {
        let state = self.interp.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let rclass = if let Some(spec) = state.classes.get::<T>() {
            spec.rclass()
        } else {
            return Err(NotDefinedError::super_class(classname.into()).into());
        };
        let rclass = unsafe { self.interp.with_ffi_boundary(|mrb| rclass.resolve(mrb))? };
        if let Some(rclass) = rclass {
            self.super_class = Some(rclass);
            Ok(self)
        } else {
            Err(NotDefinedError::super_class(classname.into()).into())
        }
    }

    pub fn add_method<T>(mut self, name: T, method: Method, args: sys::mrb_aspec) -> Result<Self, ConstantNameError>
    where
        T: Into<Cow<'static, str>>,
    {
        let spec = method::Spec::new(method::Type::Instance, name.into(), method, args)?;
        self.methods.insert(spec);
        Ok(self)
    }

    pub fn add_self_method<T>(
        mut self,
        name: T,
        method: Method,
        args: sys::mrb_aspec,
    ) -> Result<Self, ConstantNameError>
    where
        T: Into<Cow<'static, str>>,
    {
        let spec = method::Spec::new(method::Type::Class, name.into(), method, args)?;
        self.methods.insert(spec);
        Ok(self)
    }

    pub fn define(self) -> Result<(), NotDefinedError> {
        use sys::mrb_vtype::MRB_TT_DATA;

        let name = self.spec.name_c_str().as_ptr();

        let mut super_class = if let Some(super_class) = self.super_class {
            super_class
        } else {
            // Safety:
            //
            // This direct access of the `mrb` property on `Artichoke` does not
            // go through `Artichoke::with_ffi_boundary`. This is safe because
            // no `MRB_API` functions are called, which means it is not required
            // to re-box the Artichoke `State` into the `mrb_state->ud` pointer.
            //
            // This code only performs a memory access to read a field from the
            // `mrb_state`.
            let rclass = unsafe { self.interp.mrb.as_ref().object_class };
            NonNull::new(rclass).ok_or_else(|| NotDefinedError::super_class("Object"))?
        };

        let rclass = self.spec.rclass();
        let rclass = unsafe { self.interp.with_ffi_boundary(|mrb| rclass.resolve(mrb)) };

        let mut rclass = if let Ok(Some(rclass)) = rclass {
            rclass
        } else if let Some(enclosing_scope) = self.spec.enclosing_scope() {
            let scope = unsafe { self.interp.with_ffi_boundary(|mrb| enclosing_scope.rclass(mrb)) };
            if let Ok(Some(mut scope)) = scope {
                let rclass = unsafe {
                    self.interp.with_ffi_boundary(|mrb| {
                        sys::mrb_define_class_under(mrb, scope.as_mut(), name, super_class.as_mut())
                    })
                };
                let rclass = rclass.map_err(|_| NotDefinedError::class(self.spec.name()))?;
                NonNull::new(rclass).ok_or_else(|| NotDefinedError::class(self.spec.name()))?
            } else {
                return Err(NotDefinedError::enclosing_scope(enclosing_scope.fqname().into_owned()));
            }
        } else {
            let rclass = unsafe {
                self.interp
                    .with_ffi_boundary(|mrb| sys::mrb_define_class(mrb, name, super_class.as_mut()))
            };
            let rclass = rclass.map_err(|_| NotDefinedError::class(self.spec.name()))?;
            NonNull::new(rclass).ok_or_else(|| NotDefinedError::class(self.spec.name()))?
        };

        for method in &self.methods {
            unsafe {
                method.define(self.interp, rclass.as_mut())?;
            }
        }

        // If a `Spec` defines a `Class` whose isntances own a pointer to a
        // Rust object, mark them as `MRB_TT_DATA`.
        if self.is_mrb_tt_data {
            unsafe {
                sys::mrb_sys_set_instance_tt(rclass.as_mut(), MRB_TT_DATA);
            }
        }
        Ok(())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Rclass {
    name: Box<CStr>,
    enclosing_scope: Option<EnclosingRubyScope>,
}

impl Rclass {
    #[must_use]
    pub const fn new(name: Box<CStr>, enclosing_scope: Option<EnclosingRubyScope>) -> Self {
        Self { name, enclosing_scope }
    }

    /// Resolve a type's [`sys::RClass`] using its enclosing scope and name.
    ///
    /// # Safety
    ///
    /// This function must be called within an [`Artichoke::with_ffi_boundary`]
    /// closure because the FFI APIs called in this function may require access
    /// to the Artichoke [`State](crate::state::State).
    pub unsafe fn resolve(&self, mrb: *mut sys::mrb_state) -> Option<NonNull<sys::RClass>> {
        let class_name = self.name.as_ptr();
        if let Some(ref scope) = self.enclosing_scope {
            // short circuit if enclosing scope does not exist.
            let mut scope = scope.rclass(mrb)?;
            let is_defined_under = sys::mrb_class_defined_under(mrb, scope.as_mut(), class_name);
            if is_defined_under == 0 {
                // Enclosing scope exists.
                // Class is not defined under the enclosing scope.
                None
            } else {
                // Enclosing scope exists.
                // Class is defined under the enclosing scope.
                let class = sys::mrb_class_get_under(mrb, scope.as_mut(), class_name);
                NonNull::new(class)
            }
        } else {
            let is_defined = sys::mrb_class_defined(mrb, class_name);
            if is_defined == 0 {
                // Class does not exist in root scope.
                None
            } else {
                // Class exists in root scope.
                let class = sys::mrb_class_get(mrb, class_name);
                NonNull::new(class)
            }
        }
    }
}

#[derive(Debug)]
pub struct Spec {
    name: Cow<'static, str>,
    cstring: Box<CStr>,
    data_type: Box<sys::mrb_data_type>,
    enclosing_scope: Option<EnclosingRubyScope>,
}

impl Spec {
    pub fn new<T>(
        name: T,
        enclosing_scope: Option<EnclosingRubyScope>,
        free: Option<Free>,
    ) -> Result<Self, ConstantNameError>
    where
        T: Into<Cow<'static, str>>,
    {
        let name = name.into();
        if let Ok(cstring) = CString::new(name.as_ref()) {
            let cstring = cstring.into_boxed_c_str();
            // Safety:
            //
            // `data_type` and `cstring` have the same lifetime since they are
            // stored in the same struct.
            //
            // `Spec` does not offer mutable access to these fields.
            //
            // This, the `struct_name` pointer in `data_type` will point to
            // valid memory as long as this `Spec` is not dropped.
            //
            // This has implications on drop order of components in the `State`.
            let data_type = sys::mrb_data_type {
                struct_name: cstring.as_ptr(),
                dfree: free,
            };
            let data_type = Box::new(data_type);
            Ok(Self {
                name,
                cstring,
                data_type,
                enclosing_scope,
            })
        } else {
            Err(name.into())
        }
    }

    #[must_use]
    pub fn data_type(&self) -> *const sys::mrb_data_type {
        self.data_type.as_ref()
    }

    #[must_use]
    pub fn name(&self) -> Cow<'static, str> {
        match &self.name {
            Cow::Borrowed(name) => Cow::Borrowed(name),
            Cow::Owned(name) => name.clone().into(),
        }
    }

    #[must_use]
    pub fn name_c_str(&self) -> &CStr {
        self.cstring.as_ref()
    }

    #[must_use]
    pub fn enclosing_scope(&self) -> Option<&EnclosingRubyScope> {
        self.enclosing_scope.as_ref()
    }

    #[must_use]
    pub fn fqname(&self) -> Cow<'_, str> {
        if let Some(scope) = self.enclosing_scope() {
            let mut fqname = String::from(scope.fqname());
            fqname.push_str("::");
            fqname.push_str(self.name.as_ref());
            fqname.into()
        } else {
            self.name.as_ref().into()
        }
    }

    #[must_use]
    pub fn rclass(&self) -> Rclass {
        Rclass::new(self.cstring.clone(), self.enclosing_scope.clone())
    }
}

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.fqname() == other.fqname()
    }
}

#[cfg(test)]
mod tests {
    use crate::extn::core::exception::StandardError;
    use crate::extn::core::kernel::Kernel;
    use crate::test::prelude::*;

    struct RustError;

    #[test]
    fn super_class() {
        let mut interp = interpreter().unwrap();
        let spec = class::Spec::new("RustError", None, None).unwrap();
        class::Builder::for_spec(&mut interp, &spec)
            .with_super_class::<StandardError, _>("StandardError")
            .unwrap()
            .define()
            .unwrap();
        interp.def_class::<RustError>(spec).unwrap();

        let result = interp.eval(b"RustError.new.is_a?(StandardError)").unwrap();
        let result = result.try_into::<bool>(&interp).unwrap();
        assert!(result, "RustError instances are instance of StandardError");

        let result = interp.eval(b"RustError < StandardError").unwrap();
        let result = result.try_into::<bool>(&interp).unwrap();
        assert!(result, "RustError inherits from StandardError");
    }

    #[test]
    fn rclass_for_undef_root_class() {
        let mut interp = interpreter().unwrap();
        let spec = class::Spec::new("Foo", None, None).unwrap();
        let rclass = unsafe { interp.with_ffi_boundary(|mrb| spec.rclass().resolve(mrb)) }.unwrap();
        assert!(rclass.is_none());
    }

    #[test]
    fn rclass_for_undef_nested_class() {
        let mut interp = interpreter().unwrap();
        let scope = interp.module_spec::<Kernel>().unwrap().unwrap();
        let spec = class::Spec::new("Foo", Some(EnclosingRubyScope::module(scope)), None).unwrap();
        let rclass = unsafe { interp.with_ffi_boundary(|mrb| spec.rclass().resolve(mrb)) }.unwrap();
        assert!(rclass.is_none());
    }

    #[test]
    fn rclass_for_nested_class() {
        let mut interp = interpreter().unwrap();
        let _ = interp.eval(b"module Foo; class Bar; end; end").unwrap();
        let spec = module::Spec::new(&mut interp, "Foo", None).unwrap();
        let spec = class::Spec::new("Bar", Some(EnclosingRubyScope::module(&spec)), None).unwrap();
        let rclass = unsafe { interp.with_ffi_boundary(|mrb| spec.rclass().resolve(mrb)) }.unwrap();
        assert!(rclass.is_some());
    }

    #[test]
    fn rclass_for_nested_class_under_class() {
        let mut interp = interpreter().unwrap();
        let _ = interp.eval(b"class Foo; class Bar; end; end").unwrap();
        let spec = class::Spec::new("Foo", None, None).unwrap();
        let spec = class::Spec::new("Bar", Some(EnclosingRubyScope::class(&spec)), None).unwrap();
        let rclass = unsafe { interp.with_ffi_boundary(|mrb| spec.rclass().resolve(mrb)) }.unwrap();
        assert!(rclass.is_some());
    }
}