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
//! Ruby mixins.

use crate::{
    prelude::*,
    ruby,
    util::Sealed,
};

mod class;
mod method;
mod module;
pub use self::{class::*, method::*, module::*};

#[inline]
fn _get_const(m: impl Mixin, name: SymbolId) -> Option<AnyObject> {
    unsafe {
        if ruby::rb_const_defined(m.raw(), name.raw()) != 0 {
            Some(_get_const_unchecked(m, name))
        } else {
            None
        }
    }
}

#[inline]
unsafe fn _get_const_unchecked(m: impl Mixin, name: impl Into<SymbolId>) -> AnyObject {
    AnyObject::from_raw(ruby::rb_const_get(m.raw(), name.into().raw()))
}

#[inline]
unsafe fn _attr(m: ruby::VALUE, name: SymbolId, read: bool, write: bool) {
    ruby::rb_attr(m, name.raw(), read as _, write as _, 0);
}

/// A type that supports mixins (see [`Class`](struct.Class.html) and
/// [`Module`](struct.Module.html)).
pub trait Mixin: Object + Sealed {
    /// Returns `self` as a `Class` if it is one or a `Module` otherwise.
    fn to_class(self) -> Result<Class, Module>;

    /// Returns `self` as a `Module` if it is one or a `Class` otherwise.
    fn to_module(self) -> Result<Module, Class>;

    /// Embeds the contents of `module` in `self`.
    #[inline]
    fn include(self, module: Module) {
        unsafe { ruby::rb_include_module(self.raw(), module.raw()) };
    }

    /// Returns whether `self` or one of its ancestors includes `module`.
    ///
    /// This is equivalent to the `include?` method.
    #[inline]
    #[must_use]
    fn includes(self, module: Module) -> bool {
        unsafe { ruby::rb_mod_include_p(self.raw(), module.raw()) != 0 }
    }

    /// Returns an array of the modules included in `self`.
    #[inline]
    fn included_modules(self) -> Array<Module> {
        unsafe { Array::from_raw(ruby::rb_mod_included_modules(self.raw())) }
    }

    /// Prepends `module` in `self`.
    #[inline]
    fn prepend(self, module: Module) {
        unsafe { ruby::rb_prepend_module(self.raw(), module.raw()) };
    }

    /// Defines a new class under `self` with `name`.
    #[inline]
    fn def_class(
        self,
        name: impl Into<SymbolId>,
    ) -> Result<Class, DefMixinError> {
        Class::_def_under(self, Class::object(), name.into())
    }

    /// Defines a new subclass of `superclass` under `self` with `name`.
    #[inline]
    fn def_subclass(
        self,
        superclass: Class,
        name: impl Into<SymbolId>,
    ) -> Result<Class, DefMixinError> {
        Class::_def_under(self, superclass, name.into())
    }

    /// Returns the existing `Class` with `name` in `self`.
    #[inline]
    fn get_class(
        self,
        name: impl Into<SymbolId>,
    ) -> Option<Class> {
        _get_const(self, name.into())?.to_class()
    }

    /// Returns the existing `Class` with `name` in `self`.
    ///
    /// # Safety
    ///
    /// This method does not:
    /// - Check whether an item for `name` exists (an exception will be thrown
    ///   if this is the case)
    /// - Check whether the returned item for `name` is actually a `Class`
    #[inline]
    unsafe fn get_class_unchecked(
        self,
        name: impl Into<SymbolId>,
    ) -> Class {
        Class::cast_unchecked(_get_const_unchecked(self, name))
    }

    /// Defines a new module under `self` with `name`.
    #[inline]
    fn def_module(
        self,
        name: impl Into<SymbolId>,
    ) -> Result<Module, DefMixinError> {
        Module::_def_under(self, name.into())
    }

    /// Returns the existing `Module` with `name` in `self`.
    #[inline]
    fn get_module(
        self,
        name: impl Into<SymbolId>,
    ) -> Option<Module> {
        _get_const(self, name.into())?.to_module()
    }

    /// Returns the existing `Module` with `name` in `self`.
    ///
    /// # Safety
    ///
    /// This method does not:
    /// - Check whether an item for `name` exists (an exception will be thrown
    ///   if this is the case)
    /// - Check whether the returned item for `name` is actually a `Module`
    #[inline]
    unsafe fn get_module_unchecked(
        self,
        name: impl Into<SymbolId>,
    ) -> Module {
        Module::cast_unchecked(_get_const_unchecked(self, name))
    }

    /// Returns whether a constant for `name` is defined in `self`, or in some
    /// parent class if not `self`.
    #[inline]
    fn has_const(self, name: impl Into<SymbolId>) -> bool {
        unsafe { ruby::rb_const_defined(self.raw(), name.into().raw()) != 0 }
    }

    /// Returns the constant value for `name` in `self`, or in some parent class
    /// if not `self`.
    ///
    /// # Exception Handling
    ///
    /// If `name` is an uninitialized variable, a `NameError` exception will be
    /// raised. If you're unsure whether `name` exists, either check
    /// [`has_const`](#method.has_const) or surround a call to this method in a
    /// `protected` closure.
    #[inline]
    fn get_const(self, name: impl Into<SymbolId>) -> AnyObject {
        let name = name.into().raw();
        unsafe { AnyObject::from_raw(ruby::rb_const_get(self.raw(), name)) }
    }

    /// Sets the value a constant for `name` in `self` to `val`.
    #[inline]
    fn set_const(self, name: impl Into<SymbolId>, val: impl Object) {
        unsafe { ruby::rb_const_set(self.raw(), name.into().raw(), val.raw()) };
    }

    /// Removes the constant value for `name`, returning it.
    ///
    /// # Exception Handling
    ///
    /// If the constant for `name` cannot be removed, an exception is raised.
    #[inline]
    fn remove_const(self, name: impl Into<SymbolId>) -> AnyObject {
        let name = name.into().raw();
        unsafe { AnyObject::from_raw(ruby::rb_const_remove(self.raw(), name)) }
    }

    /// Returns whether the class-level `var` is defined in `self`.
    #[inline]
    fn has_class_var(self, var: impl Into<SymbolId>) -> bool {
        let t = unsafe { ruby::rb_cvar_defined(self.raw(), var.into().raw()) };
        t == crate::util::TRUE_VALUE
    }

    /// Returns the class-level `var` in `self`.
    ///
    /// # Exception Handling
    ///
    /// If `var` is an uninitialized variable, a `NameError` exception will be
    /// raised. If you're unsure whether `var` exists, either check
    /// [`has_class_var`](#method.has_class_var) or surround a call to this
    /// method in a `protected` closure.
    ///
    /// ```
    /// use rosy::{Class, Object, Mixin, protected};
    /// # rosy::vm::init().unwrap();
    ///
    /// let class = Class::array();
    /// let error = protected(|| class.get_class_var("@@hello")).unwrap_err();
    ///
    /// assert!(error.is_name_error());
    /// ```
    #[inline]
    fn get_class_var(self, var: impl Into<SymbolId>) -> AnyObject {
        let var = var.into().raw();
        unsafe { AnyObject::from_raw(ruby::rb_cvar_get(self.raw(), var)) }
    }

    /// Sets the class-level `var` in `self` to `val`.
    #[inline]
    fn set_class_var<K, V>(self, key: K, val: V) -> Result<()>
    where
        K: Into<SymbolId>,
        V: Into<AnyObject>,
    {
        crate::protected(|| unsafe { self.set_class_var_unchecked(key, val) })
    }

    /// Sets the class-level var for `key` in `self` to `val`.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `self` is not frozen or else a `FrozenError`
    /// exception will be raised.
    #[inline]
    unsafe fn set_class_var_unchecked<K, V>(self, key: K, val: V)
    where
        K: Into<SymbolId>,
        V: Into<AnyObject>,
    {
        ruby::rb_cvar_set(self.raw(), key.into().raw(), val.into().raw());
    }

    /// Defines an read-only attribute on `self` with `name`.
    #[inline]
    fn attr_reader<N: Into<SymbolId>>(self, name: N) -> Result {
        crate::protected(|| unsafe { self.attr_reader_unchecked(name) })
    }

    /// Defines an read-only attribute on `self` with `name`.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `self` is not frozen or else a `FrozenError`
    /// exception will be raised.
    #[inline]
    unsafe fn attr_reader_unchecked<N: Into<SymbolId>>(self, name: N) {
        _attr(self.raw(), name.into(), true, false);
    }

    /// Defines a write-only attribute on `self` with `name`.
    #[inline]
    fn attr_writer<N: Into<SymbolId>>(self, name: N) -> Result {
        crate::protected(|| unsafe { self.attr_writer_unchecked(name) })
    }

    /// Defines a write-only attribute on `self` with `name`.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `self` is not frozen or else a `FrozenError`
    /// exception will be raised.
    #[inline]
    unsafe fn attr_writer_unchecked<N: Into<SymbolId>>(self, name: N) {
        _attr(self.raw(), name.into(), false, true);
    }

    /// Defines a read-write attribute on `self` with `name`.
    #[inline]
    fn attr_accessor<N: Into<SymbolId>>(self, name: N) -> Result {
        crate::protected(|| unsafe { self.attr_accessor_unchecked(name) })
    }

    /// Defines a read-write attribute on `self` with `name`.
    ///
    /// # Safety
    ///
    /// The caller must ensure that `self` is not frozen or else a `FrozenError`
    /// exception will be raised.
    #[inline]
    unsafe fn attr_accessor_unchecked<N: Into<SymbolId>>(self, name: N) {
        _attr(self.raw(), name.into(), true, true);
    }

    /// Evaluates `args` in the context of `self`.
    ///
    /// See the docs for `EvalArgs` for more info.
    ///
    /// # Safety
    ///
    /// An exception may be raised by the code or by `args` being invalid.
    #[inline]
    unsafe fn eval_unchecked(self, args: impl EvalArgs) -> AnyObject {
        args.eval_in_unchecked(self)
    }

    /// Evaluates `args` in the context of `self`, returning any raised
    /// exceptions.
    ///
    /// See the docs for `EvalArgs` for more info.
    #[inline]
    fn eval(self, args: impl EvalArgs) -> Result<AnyObject> {
        args.eval_in(self)
    }
}

impl Mixin for Class {
    #[inline]
    fn to_class(self) -> Result<Class, Module> {
        Ok(self)
    }

    #[inline]
    fn to_module(self) -> Result<Module, Class> {
        Err(self)
    }
}

impl Mixin for Module {
    #[inline]
    fn to_class(self) -> Result<Class, Module> {
        Err(self)
    }

    #[inline]
    fn to_module(self) -> Result<Module, Class> {
        Ok(self)
    }
}

/// A type that can be used as one or more arguments for evaluating code within
/// the context of a [`Mixin`](trait.Mixin.html).
///
/// See the documentation of [its implementors](#foreign-impls) for much more
/// detailed information.
pub trait EvalArgs: Sized {
    /// Evaluates `self` in the context of `mixin`, returning any thrown
    /// exceptions.
    #[inline]
    fn eval_in(self, mixin: impl Mixin) -> Result<AnyObject> {
        crate::protected(|| unsafe { self.eval_in_unchecked(mixin) })
    }

    /// Evaluates `self` in the context of `mixin`.
    ///
    /// # Safety
    ///
    /// If an exception is thrown due to an argument error or from evaluating
    /// the script itself, it should be caught.
    unsafe fn eval_in_unchecked(self, mixin: impl Mixin) -> AnyObject;
}

/// Unchecked arguments directly to the evaluation function.
impl<O: Object> EvalArgs for &[O] {
    #[inline]
    unsafe fn eval_in_unchecked(self, mixin: impl Mixin) -> AnyObject {
        let raw = ruby::rb_mod_module_eval(
            self.len() as _,
            self.as_ptr() as *const ruby::VALUE,
            mixin.raw(),
        );
        AnyObject::from_raw(raw)
    }
}

/// The script argument without any extra information.
impl EvalArgs for String {
    #[inline]
    unsafe fn eval_in_unchecked(self, mixin: impl Mixin) -> AnyObject {
        self.as_any_slice().eval_in_unchecked(mixin)
    }
}

/// The script argument as a UTF-8 string, without any extra information.
// TODO: Impl for `Into<String>` when specialization stabilizes
impl EvalArgs for &str {
    #[inline]
    unsafe fn eval_in_unchecked(self, mixin: impl Mixin) -> AnyObject {
        String::from(self).eval_in_unchecked(mixin)
    }
}

/// The script and filename arguments.
impl<S: Into<String>, F: Into<String>> EvalArgs for (S, F) {
    #[inline]
    unsafe fn eval_in_unchecked(self, mixin: impl Mixin) -> AnyObject {
        let (s, f) = self;
        [s.into(), f.into()].eval_in_unchecked(mixin)
    }
}

/// The script, filename, and line number arguments.
impl<S: Into<String>, F: Into<String>, L: Into<u32>> EvalArgs for (S, F, L) {
    #[inline]
    unsafe fn eval_in_unchecked(self, _mixin: impl Mixin) -> AnyObject {
        unimplemented!("TODO: Convert u32 to object");
    }
}

/// An error when attempting to define a [`Mixin`](trait.Mixin.html) type.
#[derive(Debug)]
pub enum DefMixinError {
    /// A class already exists with the same name in the same namespace.
    ExistingClass(Class),
    /// A module already exists with the same name in the same namespace.
    ExistingModule(Module),
    /// Some other constant already exists.
    ExistingConst(AnyObject),
    /// The given class is frozen and can't have items defined under it.
    FrozenClass(Class),
    /// The given module is frozen and can't have items defined under it.
    FrozenModule(Module),
}

impl DefMixinError {
    #[cold]
    #[inline]
    pub(crate) fn _frozen(m: impl Mixin) -> Self {
        match m.to_class() {
            Ok(class) => DefMixinError::FrozenClass(class),
            Err(module) => DefMixinError::FrozenModule(module),
        }
    }

    #[inline]
    fn _get(m: impl Mixin, name: SymbolId) -> Option<Self> {
        use ruby::value_type::*;
        use DefMixinError::*;

        let existing = _get_const(m, name)?;
        let raw = existing.raw();
        let err = match crate::util::value_built_in_type(raw) {
            Some(MODULE) => unsafe {
                ExistingModule(Module::from_raw(raw))
            },
            Some(CLASS) => unsafe {
                ExistingClass(Class::from_raw(raw))
            },
            Some(_) | None => ExistingConst(existing),
        };
        Some(err)
    }

    /// Returns the existing class that was found.
    #[inline]
    pub fn existing_class(&self) -> Option<Class> {
        match *self {
            DefMixinError::ExistingClass(c) => Some(c),
            _ => None,
        }
    }

    /// Returns the existing module that was found.
    #[inline]
    pub fn existing_module(&self) -> Option<Module> {
        match *self {
            DefMixinError::ExistingModule(m) => Some(m),
            _ => None,
        }
    }

    /// Returns the existing constant that was found.
    #[inline]
    pub fn existing_const(&self) -> Option<AnyObject> {
        match *self {
            DefMixinError::ExistingConst(m) => Some(m),
            _ => None,
        }
    }

    /// Returns the existing object that was found.
    #[inline]
    pub fn existing_object(&self) -> Option<AnyObject> {
        use DefMixinError::*;
        match *self {
            ExistingModule(m) => Some(m.into_any_object()),
            ExistingClass(c)  => Some(c.into_any_object()),
            ExistingConst(c)  => Some(c),
            _ => None,
        }
    }
}