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
use bstr::ByteSlice;
use std::ffi::OsStr;
use std::path::Path;

use crate::core::{Eval, LoadSources, Parser, Value as _};
use crate::error::Error;
use crate::exception_handler;
use crate::extn::core::exception::{ArgumentError, Fatal};
use crate::ffi::{self, InterpreterExtractError};
use crate::state::parser::Context;
use crate::sys;
use crate::sys::protect;
use crate::value::Value;
use crate::Artichoke;

impl Eval for Artichoke {
    type Value = Value;

    type Error = Error;

    fn eval(&mut self, code: &[u8]) -> Result<Self::Value, Self::Error> {
        trace!("Attempting eval of Ruby source");
        let result = unsafe {
            let state = self.state.as_mut().ok_or(InterpreterExtractError::new())?;
            let parser = state
                .parser
                .as_mut()
                .ok_or(InterpreterExtractError::new())?;
            let context: *mut sys::mrbc_context = parser.context_mut();
            self.with_ffi_boundary(|mrb| protect::eval(mrb, context, code))?
        };
        match result {
            Ok(value) => {
                let value = Value::from(value);
                if value.is_unreachable() {
                    // Unreachable values are internal to the mruby interpreter
                    // and interacting with them via the C API is unspecified
                    // and may result in a segfault.
                    //
                    // See: https://github.com/mruby/mruby/issues/4460
                    error!("Fatal eval returned unreachable value");
                    Err(Fatal::from("Unreachable Ruby value").into())
                } else {
                    trace!("Sucessful eval");
                    Ok(value)
                }
            }
            Err(exception) => {
                let exception = Value::from(exception);
                let debug = exception.inspect(self);
                debug!("Failed eval raised exception: {:?}", debug.as_bstr());
                Err(exception_handler::last_error(self, exception)?)
            }
        }
    }

    fn eval_os_str(&mut self, code: &OsStr) -> Result<Self::Value, Self::Error> {
        let code = ffi::os_str_to_bytes(code)?;
        self.eval(code)
    }

    fn eval_file(&mut self, file: &Path) -> Result<Self::Value, Self::Error> {
        let context = Context::new(ffi::os_str_to_bytes(file.as_os_str())?.to_vec())
            .ok_or_else(|| ArgumentError::from("path name contains null byte"))?;
        self.push_context(context)?;
        let code = self.read_source_file_contents(file)?.into_owned();
        let result = self.eval(code.as_slice());
        let _ = self.pop_context()?;
        result
    }
}

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

    #[test]
    fn root_eval_context() {
        let mut interp = interpreter().unwrap();
        let result = interp.eval(b"__FILE__").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "(eval)");
    }

    #[test]
    fn context_is_restored_after_eval() {
        let mut interp = interpreter().unwrap();
        let context = Context::new(&b"context.rb"[..]).unwrap();
        interp.push_context(context).unwrap();
        let _ = interp.eval(b"15").unwrap();
        let context = interp.peek_context().unwrap();
        let filename = context.unwrap().filename();
        assert_eq!(filename, &b"context.rb"[..]);
    }

    #[test]
    fn root_context_is_not_pushed_after_eval() {
        let mut interp = interpreter().unwrap();
        let _ = interp.eval(b"15").unwrap();
        let context = interp.peek_context().unwrap();
        assert!(context.is_none());
    }

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

        #[derive(Debug)]
        struct NestedEval;

        unsafe extern "C" fn nested_eval_file(
            mrb: *mut sys::mrb_state,
            _slf: sys::mrb_value,
        ) -> sys::mrb_value {
            let mut interp = unwrap_interpreter!(mrb);
            let mut guard = Guard::new(&mut interp);
            let result = if let Ok(value) = guard.eval(b"__FILE__") {
                value
            } else {
                Value::nil()
            };
            result.inner()
        }

        impl File for NestedEval {
            type Artichoke = Artichoke;

            type Error = Error;

            fn require(interp: &mut Artichoke) -> Result<(), Self::Error> {
                let spec = module::Spec::new(interp, "NestedEval", None)?;
                module::Builder::for_spec(interp, &spec)
                    .add_self_method("file", nested_eval_file, sys::mrb_args_none())?
                    .define()?;
                interp.def_module::<Self>(spec)?;
                Ok(())
            }
        }

        #[test]
        #[should_panic]
        // this test is known broken
        fn eval_context_is_a_stack() {
            let mut interp = interpreter().unwrap();
            interp
                .def_file_for_type::<_, NestedEval>("nested_eval.rb")
                .unwrap();
            let code = br#"require 'nested_eval'; NestedEval.file"#;
            let result = interp.eval(code).unwrap();
            let result = result.try_into_mut::<&str>(&mut interp).unwrap();
            assert_eq!(result, "/src/lib/nested_eval.rb");
        }
    }

    #[test]
    fn eval_with_context() {
        let mut interp = interpreter().unwrap();

        let context = Context::new(b"source.rb".as_ref()).unwrap();
        interp.push_context(context).unwrap();
        let result = interp.eval(b"__FILE__").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "source.rb");
        interp.pop_context().unwrap();

        let context = Context::new(b"source.rb".as_ref()).unwrap();
        interp.push_context(context).unwrap();
        let result = interp.eval(b"__FILE__").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "source.rb");
        interp.pop_context().unwrap();

        let context = Context::new(b"main.rb".as_ref()).unwrap();
        interp.push_context(context).unwrap();
        let result = interp.eval(b"__FILE__").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "main.rb");
        interp.pop_context().unwrap();
    }

    #[test]
    fn unparseable_code_returns_err_syntax_error() {
        let mut interp = interpreter().unwrap();
        let err = interp.eval(b"'a").unwrap_err();
        assert_eq!("SyntaxError", err.name().as_ref());
    }

    #[test]
    fn interpreter_is_usable_after_syntax_error() {
        let mut interp = interpreter().unwrap();
        let err = interp.eval(b"'a").unwrap_err();
        assert_eq!("SyntaxError", err.name().as_ref());
        // Ensure interpreter is usable after evaling unparseable code
        let result = interp.eval(b"'a' * 10 ").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "a".repeat(10));
    }

    #[test]
    // TODO(GH-528): fix failing tests on Windows.
    #[cfg_attr(target_os = "windows", should_panic)]
    fn file_magic_constant() {
        let mut interp = interpreter().unwrap();
        interp
            .def_rb_source_file("source.rb", &b"def file; __FILE__; end"[..])
            .unwrap();
        let result = interp.eval(b"require 'source'; file").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "/src/lib/source.rb");
    }

    #[test]
    fn file_not_persistent() {
        let mut interp = interpreter().unwrap();
        interp
            .def_rb_source_file("source.rb", &b"def file; __FILE__; end"[..])
            .unwrap();
        let result = interp.eval(b"require 'source'; __FILE__").unwrap();
        let result = result.try_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "(eval)");
    }

    #[test]
    fn return_syntax_error() {
        let mut interp = interpreter().unwrap();
        interp
            .def_rb_source_file("fail.rb", &b"def bad; 'as'.scan(; end"[..])
            .unwrap();
        let err = interp.eval(b"require 'fail'").unwrap_err();
        assert_eq!("SyntaxError", err.name().as_ref());
    }
}