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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
use std::borrow::Cow;
use std::error;
use std::ffi::c_void;
use std::fmt;
use std::ptr::NonNull;

use crate::class;
use crate::class_registry::ClassRegistry;
use crate::convert::BoxUnboxVmValue;
use crate::core::ConvertMut;
use crate::error::{Error, RubyException};
use crate::extn::core::exception::{NameError, ScriptError};
use crate::module;
use crate::sys;
use crate::Artichoke;

/// Typedef for an mruby free function for an [`mrb_value`](sys::mrb_value) with
/// `tt` [`MRB_TT_DATA`](sys::mrb_vtype::MRB_TT_DATA).
pub type Free = unsafe extern "C" fn(mrb: *mut sys::mrb_state, data: *mut c_void);

/// A generic implementation of a [`Free`] function for
/// [`mrb_value`](sys::mrb_value)s that store an owned copy of a [`Box`] smart
/// pointer.
///
/// This function ultimately calls [`Box::from_raw`] on the data pointer and
/// drops the resulting [`Box`].
///
/// # Safety
///
/// This function assumes that the data pointer is to an [`Box`]`<T>` created by
/// [`Box::into_raw`]. This fuction bounds `T` by [`BoxUnboxVmValue`] which
/// boxes `T` for the mruby VM like this.
///
/// This function assumes it is called by the mruby VM as a free function for
/// an [`MRB_TT_DATA`](sys::mrb_vtype::MRB_TT_DATA).
pub unsafe extern "C" fn box_unbox_free<T>(_mrb: *mut sys::mrb_state, data: *mut c_void)
where
    T: 'static + BoxUnboxVmValue,
{
    if data.is_null() {
        error!(
            "Received null pointer in box_unbox_free<{}>: {:p}",
            T::RUBY_TYPE,
            data
        );
        eprintln!(
            "Received null pointer in box_unbox_free<{}>: {:p}",
            T::RUBY_TYPE,
            data
        );
    }
    T::free(data);
}

#[cfg(test)]
mod free_test {
    use crate::convert::HeapAllocatedData;

    fn prototype(_func: super::Free) {}

    #[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct Data(String);

    impl HeapAllocatedData for Data {
        const RUBY_TYPE: &'static str = "Data";
    }

    #[test]
    fn free_prototype() {
        prototype(super::box_unbox_free::<Data>);
    }
}

/// Typedef for a method exposed in the mruby interpreter.
///
/// This function signature is used for all types of mruby methods, including
/// instance methods, class methods, singleton methods, and global methods.
///
/// `slf` is the method receiver, e.g. `s` in the following invocation of
/// `String#start_with?`.
///
/// ```ruby
/// s = 'artichoke crate'
/// s.start_with?('artichoke')
/// ```
///
/// To extract method arguments, use [`mrb_get_args!`] and the supplied
/// interpreter.
pub type Method =
    unsafe extern "C" fn(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value;

/// Typesafe wrapper for the [`RClass *`](sys::RClass) of the enclosing scope
/// for an mruby `Module` or `Class`.
///
/// In Ruby, classes and modules can be defined inside of another class or
/// module. mruby only supports resolving [`RClass`](sys::RClass) pointers
/// relative to an enclosing scope. This can be the top level with
/// [`mrb_class_get`](sys::mrb_class_get) and
/// [`mrb_module_get`](sys::mrb_module_get) or it can be under another class
/// with [`mrb_class_get_under`](sys::mrb_class_get_under) or module with
/// [`mrb_module_get_under`](sys::mrb_module_get_under).
///
/// Because there is no C API to resolve class and module names directly, each
/// class-like holds a reference to its enclosing scope so it can recursively
/// resolve its enclosing [`RClass *`](sys::RClass).
#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub enum EnclosingRubyScope {
    /// Reference to a Ruby `Class` enclosing scope.
    Class(Box<class::Spec>),
    /// Reference to a Ruby `Module` enclosing scope.
    Module(Box<module::Spec>),
}

impl EnclosingRubyScope {
    /// Factory for [`EnclosingRubyScope::Class`] that clones a [`class::Spec`].
    ///
    /// This function is useful when extracting an enclosing scope from the
    /// class registry.
    #[must_use]
    pub fn class(spec: &class::Spec) -> Self {
        Self::Class(Box::new(spec.clone()))
    }

    /// Factory for [`EnclosingRubyScope::Module`] that clones a
    /// [`module::Spec`].
    ///
    /// This function is useful when extracting an enclosing scope from the
    /// module registry.
    #[must_use]
    pub fn module(spec: &module::Spec) -> Self {
        Self::Module(Box::new(spec.clone()))
    }

    /// Resolve the [`RClass *`](sys::RClass) of the wrapped class or module.
    ///
    /// Return [`None`] if the class-like has no [`EnclosingRubyScope`].
    ///
    /// The current implemention results in recursive calls to this function
    /// for each enclosing scope.
    ///
    /// # 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 rclass(&self, mrb: *mut sys::mrb_state) -> Option<NonNull<sys::RClass>> {
        match self {
            Self::Class(spec) => spec.rclass().resolve(mrb),
            Self::Module(spec) => spec.rclass().resolve(mrb),
        }
    }

    /// Get the fully qualified name of the wrapped class or module.
    ///
    /// For example, in the following Ruby code, `C` has an fqname of `A::B::C`.
    ///
    /// ```ruby
    /// module A
    ///   class B
    ///     module C
    ///       CONST = 1
    ///     end
    ///   end
    /// end
    /// ```
    ///
    /// The current implemention results in recursive calls to this function
    /// for each enclosing scope.
    #[must_use]
    pub fn fqname(&self) -> Cow<'_, str> {
        match self {
            Self::Class(spec) => spec.fqname(),
            Self::Module(spec) => spec.fqname(),
        }
    }
}

#[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct ConstantNameError(Cow<'static, str>);

impl From<&'static str> for ConstantNameError {
    fn from(name: &'static str) -> Self {
        Self(name.into())
    }
}

impl From<String> for ConstantNameError {
    fn from(name: String) -> Self {
        Self(name.into())
    }
}

impl From<Cow<'static, str>> for ConstantNameError {
    fn from(name: Cow<'static, str>) -> Self {
        Self(name)
    }
}

impl ConstantNameError {
    #[must_use]
    pub const fn new() -> Self {
        Self(Cow::Borrowed(""))
    }
}

impl fmt::Display for ConstantNameError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("Invalid constant name contained a NUL byte")
    }
}

impl error::Error for ConstantNameError {}

impl RubyException for ConstantNameError {
    fn message(&self) -> Cow<'_, [u8]> {
        Cow::Borrowed(b"Invalid constant name contained a NUL byte")
    }

    fn name(&self) -> Cow<'_, str> {
        "NameError".into()
    }

    fn vm_backtrace(&self, interp: &mut Artichoke) -> Option<Vec<Vec<u8>>> {
        let _ = interp;
        None
    }

    fn as_mrb_value(&self, interp: &mut Artichoke) -> Option<sys::mrb_value> {
        let message = interp.convert_mut(self.message());
        let value = interp
            .new_instance::<NameError>(&[message])
            .ok()
            .flatten()?;
        Some(value.inner())
    }
}

impl From<ConstantNameError> for Error {
    fn from(exception: ConstantNameError) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<Box<ConstantNameError>> for Error {
    fn from(exception: Box<ConstantNameError>) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<ConstantNameError> for Box<dyn RubyException> {
    fn from(exception: ConstantNameError) -> Box<dyn RubyException> {
        Box::new(exception)
    }
}

impl From<Box<ConstantNameError>> for Box<dyn RubyException> {
    fn from(exception: Box<ConstantNameError>) -> Box<dyn RubyException> {
        exception
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum NotDefinedError {
    EnclosingScope(Cow<'static, str>),
    Super(Cow<'static, str>),
    Class(Cow<'static, str>),
    Method(Cow<'static, str>),
    Module(Cow<'static, str>),
    GlobalConstant(Cow<'static, str>),
    ClassConstant(Cow<'static, str>),
    ModuleConstant(Cow<'static, str>),
}

impl NotDefinedError {
    pub fn enclosing_scope<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::EnclosingScope(item.into())
    }

    pub fn super_class<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::Super(item.into())
    }

    pub fn class<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::Class(item.into())
    }

    pub fn method<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::Method(item.into())
    }

    pub fn module<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::Module(item.into())
    }

    pub fn global_constant<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::GlobalConstant(item.into())
    }

    pub fn class_constant<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::ClassConstant(item.into())
    }

    pub fn module_constant<T>(item: T) -> Self
    where
        T: Into<Cow<'static, str>>,
    {
        Self::ModuleConstant(item.into())
    }

    #[must_use]
    pub fn fqdn(&self) -> &str {
        match self {
            Self::EnclosingScope(ref fqdn)
            | Self::Super(ref fqdn)
            | Self::Class(ref fqdn)
            | Self::Module(ref fqdn) => fqdn.as_ref(),
            Self::GlobalConstant(ref name)
            | Self::ClassConstant(ref name)
            | Self::Method(ref name)
            | Self::ModuleConstant(ref name) => name.as_ref(),
        }
    }

    #[must_use]
    pub const fn item_type(&self) -> &str {
        match self {
            Self::EnclosingScope(_) => "enclosing scope",
            Self::Super(_) => "super class",
            Self::Class(_) => "class",
            Self::Method(_) => "method",
            Self::Module(_) => "module",
            Self::GlobalConstant(_) => "global constant",
            Self::ClassConstant(_) => "class constant",
            Self::ModuleConstant(_) => "module constant",
        }
    }
}

impl fmt::Display for NotDefinedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.item_type())?;
        f.write_str(" '")?;
        f.write_str(self.fqdn())?;
        f.write_str("' not defined")?;
        Ok(())
    }
}

impl error::Error for NotDefinedError {}

impl RubyException for NotDefinedError {
    fn message(&self) -> Cow<'_, [u8]> {
        let mut message = String::from(self.item_type());
        message.push(' ');
        message.push_str(self.fqdn());
        message.push_str(" not defined");
        message.into_bytes().into()
    }

    fn name(&self) -> Cow<'_, str> {
        "ScriptError".into()
    }

    fn vm_backtrace(&self, interp: &mut Artichoke) -> Option<Vec<Vec<u8>>> {
        let _ = interp;
        None
    }

    fn as_mrb_value(&self, interp: &mut Artichoke) -> Option<sys::mrb_value> {
        let message = interp.convert_mut(self.message());
        let value = interp
            .new_instance::<ScriptError>(&[message])
            .ok()
            .flatten()?;
        Some(value.inner())
    }
}

impl From<NotDefinedError> for Error {
    fn from(exception: NotDefinedError) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<Box<NotDefinedError>> for Error {
    fn from(exception: Box<NotDefinedError>) -> Self {
        Self::from(Box::<dyn RubyException>::from(exception))
    }
}

impl From<NotDefinedError> for Box<dyn RubyException> {
    fn from(exception: NotDefinedError) -> Box<dyn RubyException> {
        Box::new(exception)
    }
}

impl From<Box<NotDefinedError>> for Box<dyn RubyException> {
    fn from(exception: Box<NotDefinedError>) -> Box<dyn RubyException> {
        exception
    }
}

#[cfg(test)]
mod tests {
    mod fqname {
        use crate::test::prelude::*;

        /// A
        #[derive(Debug)]
        struct Root;

        /// A::B
        #[derive(Debug)]
        struct ModuleUnderRoot;

        /// A::C
        #[derive(Debug)]
        struct ClassUnderRoot;

        /// A::B::D
        #[derive(Debug)]
        struct ClassUnderModule;

        /// A::C::E
        #[derive(Debug)]
        struct ModuleUnderClass;

        /// A::C::F
        #[derive(Debug)]
        struct ClassUnderClass;

        #[test]
        fn integration_test() {
            // Setup: define module and class hierarchy
            let mut interp = interpreter().unwrap();
            let root = module::Spec::new(&mut interp, "A", None).unwrap();
            let mod_under_root =
                module::Spec::new(&mut interp, "B", Some(EnclosingRubyScope::module(&root)))
                    .unwrap();
            let cls_under_root =
                class::Spec::new("C", Some(EnclosingRubyScope::module(&root)), None).unwrap();
            let cls_under_mod =
                class::Spec::new("D", Some(EnclosingRubyScope::module(&mod_under_root)), None)
                    .unwrap();
            let mod_under_cls = module::Spec::new(
                &mut interp,
                "E",
                Some(EnclosingRubyScope::class(&cls_under_root)),
            )
            .unwrap();
            let cls_under_cls =
                class::Spec::new("F", Some(EnclosingRubyScope::class(&cls_under_root)), None)
                    .unwrap();
            module::Builder::for_spec(&mut interp, &root)
                .define()
                .unwrap();
            module::Builder::for_spec(&mut interp, &mod_under_root)
                .define()
                .unwrap();
            class::Builder::for_spec(&mut interp, &cls_under_root)
                .define()
                .unwrap();
            class::Builder::for_spec(&mut interp, &cls_under_mod)
                .define()
                .unwrap();
            module::Builder::for_spec(&mut interp, &mod_under_cls)
                .define()
                .unwrap();
            class::Builder::for_spec(&mut interp, &cls_under_cls)
                .define()
                .unwrap();
            interp.def_module::<Root>(root).unwrap();
            interp
                .def_module::<ModuleUnderRoot>(mod_under_root)
                .unwrap();
            interp.def_class::<ClassUnderRoot>(cls_under_root).unwrap();
            interp.def_class::<ClassUnderModule>(cls_under_mod).unwrap();
            interp
                .def_module::<ModuleUnderClass>(mod_under_cls)
                .unwrap();
            interp.def_class::<ClassUnderClass>(cls_under_cls).unwrap();

            let root = interp.module_spec::<Root>().unwrap().unwrap();
            assert_eq!(root.fqname().as_ref(), "A");
            let mod_under_root = interp.module_spec::<ModuleUnderRoot>().unwrap().unwrap();
            assert_eq!(mod_under_root.fqname().as_ref(), "A::B");
            let cls_under_root = interp.class_spec::<ClassUnderRoot>().unwrap().unwrap();
            assert_eq!(cls_under_root.fqname().as_ref(), "A::C");
            let cls_under_mod = interp.class_spec::<ClassUnderModule>().unwrap().unwrap();
            assert_eq!(cls_under_mod.fqname().as_ref(), "A::B::D");
            let mod_under_cls = interp.module_spec::<ModuleUnderClass>().unwrap().unwrap();
            assert_eq!(mod_under_cls.fqname().as_ref(), "A::C::E");
            let cls_under_cls = interp.class_spec::<ClassUnderClass>().unwrap().unwrap();
            assert_eq!(cls_under_cls.fqname().as_ref(), "A::C::F");
        }
    }

    mod functional {
        use crate::test::prelude::*;

        #[derive(Debug)]
        struct Class;

        #[derive(Debug)]
        struct Module;

        extern "C" fn value(_mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
            unsafe {
                match slf.tt {
                    sys::mrb_vtype::MRB_TT_CLASS => sys::mrb_sys_fixnum_value(8),
                    sys::mrb_vtype::MRB_TT_MODULE => sys::mrb_sys_fixnum_value(27),
                    sys::mrb_vtype::MRB_TT_OBJECT => sys::mrb_sys_fixnum_value(64),
                    _ => sys::mrb_sys_fixnum_value(125),
                }
            }
        }

        #[test]
        fn define_method() {
            let mut interp = interpreter().unwrap();
            let class = class::Spec::new("DefineMethodTestClass", None, None).unwrap();
            class::Builder::for_spec(&mut interp, &class)
                .add_method("value", value, sys::mrb_args_none())
                .unwrap()
                .add_self_method("value", value, sys::mrb_args_none())
                .unwrap()
                .define()
                .unwrap();
            interp.def_class::<Class>(class).unwrap();
            let module = module::Spec::new(&mut interp, "DefineMethodTestModule", None).unwrap();
            module::Builder::for_spec(&mut interp, &module)
                .add_method("value", value, sys::mrb_args_none())
                .unwrap()
                .add_self_method("value", value, sys::mrb_args_none())
                .unwrap()
                .define()
                .unwrap();
            interp.def_module::<Module>(module).unwrap();

            let _ = interp
                .eval(b"class DynamicTestClass; include DefineMethodTestModule; extend DefineMethodTestModule; end")
                .unwrap();
            let _ = interp
                .eval(b"module DynamicTestModule; extend DefineMethodTestModule; end")
                .unwrap();

            let result = interp.eval(b"DefineMethodTestClass.new.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 64);
            let result = interp.eval(b"DefineMethodTestClass.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 8);
            let result = interp.eval(b"DefineMethodTestModule.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 27);
            let result = interp.eval(b"DynamicTestClass.new.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 64);
            let result = interp.eval(b"DynamicTestClass.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 8);
            let result = interp.eval(b"DynamicTestModule.value").unwrap();
            let result = result.try_into::<i64>(&interp).unwrap();
            assert_eq!(result, 27);
        }
    }
}