artichoke_backend/
eval.rs

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

use scolapasta_path::os_str_to_bytes;
use spinoso_exception::{ArgumentError, Fatal, LoadError};

use crate::core::{Eval, LoadSources, Parser};
use crate::error::Error;
use crate::ffi::InterpreterExtractError;
use crate::state::parser::Context;
use crate::sys;
use crate::sys::protect;
use crate::value::Value;
use crate::Artichoke;
use crate::{exception_handler, RubyException};

impl Eval for Artichoke {
    type Value = Value;

    type Error = Error;

    fn eval(&mut self, code: &[u8]) -> Result<Self::Value, Self::Error> {
        let result = unsafe {
            let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
            let parser = state.parser.as_mut().ok_or_else(InterpreterExtractError::new)?;
            let context: *mut sys::mrbc_context = parser.context_mut();
            self.with_ffi_boundary(|mrb| protect::eval(mrb, context, code))?
        };

        let result = result.map(Value::from).map_err(Value::from);

        match result {
            Ok(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
                emit_fatal_warning!("eval returned an unreachable Ruby value");
                Err(Fatal::from("eval returned an unreachable Ruby value").into())
            }
            Ok(value) => Ok(self.protect(value)),
            Err(exception) => {
                let exception = self.protect(exception);
                Err(exception_handler::last_error(self, exception)?)
            }
        }
    }

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

    fn eval_file(&mut self, file: &Path) -> Result<Self::Value, Self::Error> {
        let context = Context::new(os_str_to_bytes(file.as_os_str())?.to_vec())
            .ok_or_else(|| ArgumentError::with_message("path name contains null byte"))?;
        self.push_context(context)?;
        let code = self
            .read_source_file_contents(file)
            .map_err(|err| {
                let mut message = b"ruby: ".to_vec();
                message.extend_from_slice(err.message().as_ref());
                if let Ok(bytes) = os_str_to_bytes(file.as_os_str()) {
                    message.extend_from_slice(b" -- ");
                    message.extend_from_slice(bytes);
                }
                LoadError::from(message)
            })?
            .into_owned();
        let result = self.eval(code.as_slice());
        self.pop_context()?;
        result
    }
}

#[cfg(test)]
mod tests {
    #[cfg(unix)]
    use std::ffi::OsStr;
    #[cfg(unix)]
    use std::os::unix::ffi::OsStrExt;
    use std::path::Path;

    use bstr::ByteSlice;

    use crate::test::prelude::*;

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

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

    #[test]
    fn root_context_is_not_pushed_after_eval() {
        let mut interp = interpreter();
        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 {
            unwrap_interpreter!(mrb, to => guard);
            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", qed::const_cstr_from_str!("NestedEval\0"), 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]
        #[allow(clippy::should_panic_without_expect)]
        // this test is known broken
        fn eval_context_is_a_stack() {
            let mut interp = interpreter();
            interp.def_file_for_type::<_, NestedEval>("nested_eval.rb").unwrap();
            let code = b"require 'nested_eval'; NestedEval.file";
            let result = interp.eval(code).unwrap();
            let result = result.try_convert_into_mut::<&str>(&mut interp).unwrap();
            assert_eq!(result, "/src/lib/nested_eval.rb");
        }
    }

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

        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_convert_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_convert_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_convert_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();
        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();
        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_convert_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "a".repeat(10));
    }

    #[test]
    fn file_magic_constant() {
        let file = if cfg!(windows) {
            "c:/artichoke/virtual_root/src/lib/source.rb"
        } else {
            "/artichoke/virtual_root/src/lib/source.rb"
        };
        let mut interp = interpreter();
        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_convert_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, file);
    }

    #[test]
    fn file_not_persistent() {
        let mut interp = interpreter();
        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_convert_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(result, "(eval)");
    }

    #[test]
    fn return_syntax_error() {
        let mut interp = interpreter();
        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());
    }

    #[test]
    fn eval_file_error_file_not_found() {
        let mut interp = interpreter();
        let err = interp.eval_file(Path::new("no/such/file.rb")).unwrap_err();
        assert_eq!("LoadError", err.name().as_ref());
        assert_eq!(
            b"ruby: file not found in virtual file system -- no/such/file.rb",
            err.message().as_ref()
        );
    }

    #[test]
    #[cfg(unix)]
    fn eval_file_error_invalid_path() {
        let mut interp = interpreter();
        let err = interp
            .eval_file(Path::new(OsStr::from_bytes(b"not/valid/utf8/\xff.rb")))
            .unwrap_err();
        assert_eq!("LoadError", err.name().as_ref());
        assert_eq!(
            b"ruby: file not found in virtual file system -- not/valid/utf8/\xFF.rb",
            err.message().as_ref()
        );
    }
}