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 log::warn;
use std::ffi::CString;
use std::rc::Rc;

use crate::convert::FromMrb;
use crate::def::{ClassLike, Define};
use crate::sys;
use crate::value::Value;
use crate::{Mrb, MrbError};

pub fn patch(interp: &Mrb) -> Result<(), MrbError> {
    let exception = interp
        .borrow_mut()
        .def_class::<Exception>("Exception", None, None);
    let scripterror = interp
        .borrow_mut()
        .def_class::<ScriptError>("ScriptError", None, None);
    scripterror
        .borrow_mut()
        .with_super_class(Rc::clone(&exception));
    scripterror
        .borrow()
        .define(interp)
        .map_err(|_| MrbError::New)?;
    let loaderror = interp
        .borrow_mut()
        .def_class::<LoadError>("LoadError", None, None);
    loaderror
        .borrow_mut()
        .with_super_class(Rc::clone(&scripterror));
    loaderror
        .borrow()
        .define(interp)
        .map_err(|_| MrbError::New)?;
    interp
        .borrow_mut()
        .def_class::<ArgumentError>("ArgumentError", None, None);
    interp
        .borrow_mut()
        .def_class::<IndexError>("IndexError", None, None);
    interp
        .borrow_mut()
        .def_class::<RuntimeError>("RuntimeError", None, None);
    interp
        .borrow_mut()
        .def_class::<SyntaxError>("SyntaxError", None, None);
    interp
        .borrow_mut()
        .def_class::<TypeError>("TypeError", None, None);
    Ok(())
}

/// Raise implementation for `Exception` structs.
///
/// **Warning**: Calling [`raise`](RubyException::raise) on an interpreter from
/// outside the mruby VM call stack will result in a segfault. Raise should only
/// be called from Rust functions that are exposed on the mruby interpreter via
/// [`class::Spec`](crate::class::Spec) and
/// [`module::Spec`](crate::module::Spec).
pub trait RubyException: 'static + Sized {
    /// Raise the `Exception` defined with this type with a message.
    ///
    /// **Warning**: This function calls [`sys::mrb_sys_raise`] which modifies
    /// the stack with `longjmp`. mruby expects raise to be called at some stack
    /// frame below an eval. If this is not the case, mruby will segfault.
    unsafe fn raise(interp: Mrb, message: &'static str) -> sys::mrb_value {
        Self::raisef::<Value>(interp, message, vec![])
    }

    unsafe fn raisef<V>(interp: Mrb, message: &'static str, format: Vec<V>) -> sys::mrb_value
    where
        Value: FromMrb<V>,
    {
        // Ensure the borrow is out of scope by the time we eval code since
        // Rust-backed files and types may need to mutably borrow the `Mrb` to
        // get access to the underlying `MrbState`.
        let mrb = interp.borrow().mrb;

        let spec = if let Some(spec) = interp.borrow().class_spec::<Self>() {
            spec
        } else {
            return sys::mrb_sys_nil_value();
        };
        let borrow = spec.borrow();
        let eclass = if let Some(rclass) = borrow.rclass(&interp) {
            rclass
        } else {
            warn!("unable to raise {}", borrow.name());
            return sys::mrb_sys_nil_value();
        };
        // message is a &'static str so it should never leak
        let message = CString::new(message);
        let message_cstring = if let Ok(message) = message {
            message
        } else {
            warn!("unable to raise {}", borrow.name());
            return sys::mrb_sys_nil_value();
        };
        let message_ptr = message_cstring.as_ptr();

        let mut formatargs = format
            .into_iter()
            .map(|item| Value::from_mrb(&interp, item).inner())
            .collect::<Vec<_>>();
        // `mrb_sys_raise` will call longjmp which will unwind the stack.
        // Anything we haven't cleaned up at this point will leak, so drop
        // everything.
        drop(borrow);
        drop(spec);
        drop(interp);
        match formatargs.len() {
            0 => {
                drop(formatargs);
                sys::mrb_raise(mrb, eclass, message_ptr)
            }
            1 => {
                let arg1 = formatargs.remove(0);
                drop(formatargs);
                sys::mrb_raisef(mrb, eclass, message_ptr, arg1)
            }
            _ => panic!("unsupported raisef format arg count. See mruby/src/extn/core/error.rs."),
        }
        unreachable!("mrb_raise will unwind the stack with longjmp");
    }
}

pub struct Exception;

impl RubyException for Exception {}

#[allow(clippy::module_name_repetitions)]
pub struct ScriptError;

impl RubyException for ScriptError {}

#[allow(clippy::module_name_repetitions)]
pub struct LoadError;

impl RubyException for LoadError {}

#[allow(clippy::module_name_repetitions)]
pub struct ArgumentError;

impl RubyException for ArgumentError {}

#[allow(clippy::module_name_repetitions)]
pub struct IndexError;

impl RubyException for IndexError {}

#[allow(clippy::module_name_repetitions)]
pub struct RuntimeError;

impl RubyException for RuntimeError {}

#[allow(clippy::module_name_repetitions)]
pub struct SyntaxError;

impl RubyException for SyntaxError {}

#[allow(clippy::module_name_repetitions)]
pub struct TypeError;

impl RubyException for TypeError {}

#[cfg(test)]
mod tests {
    use std::rc::Rc;

    use crate::def::{ClassLike, Define};
    use crate::eval::MrbEval;
    use crate::exception::Exception;
    use crate::extn::core::error::{RubyException, RuntimeError};
    use crate::file::MrbFile;
    use crate::sys;
    use crate::{Mrb, MrbError};

    struct Run;

    impl Run {
        unsafe extern "C" fn run(mrb: *mut sys::mrb_state, _slf: sys::mrb_value) -> sys::mrb_value {
            let interp = unwrap_interpreter!(mrb);
            RuntimeError::raise(interp, "something went wrong")
        }
    }

    impl MrbFile for Run {
        fn require(interp: Mrb) -> Result<(), MrbError> {
            let spec = interp.borrow_mut().def_class::<Self>("Run", None, None);
            spec.borrow_mut()
                .add_self_method("run", Self::run, sys::mrb_args_none());
            spec.borrow().define(&interp)?;
            Ok(())
        }
    }

    #[test]
    fn raise() {
        let interp = crate::interpreter().expect("mrb init");
        Run::require(Rc::clone(&interp)).unwrap();
        let value = interp.eval("Run.run").map(|_| ());
        let expected = Exception::new(
            "RuntimeError",
            "something went wrong",
            Some(vec!["(eval):1".to_owned()]),
            "(eval):1: something went wrong (RuntimeError)",
        );
        assert_eq!(value, Err(MrbError::Exec(expected.to_string())));
    }
}