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

use artichoke_core::eval::Eval;
use artichoke_core::file::File;
use artichoke_core::load::{LoadSources, Loaded, Required};
use spinoso_exception::LoadError;

use crate::error::Error;
use crate::ffi::InterpreterExtractError;
use crate::platform_string::os_str_to_bytes;
use crate::Artichoke;

const RUBY_EXTENSION: &str = "rb";

impl LoadSources for Artichoke {
    type Artichoke = Self;
    type Error = Error;
    type Exception = Error;

    fn def_file_for_type<P, T>(&mut self, path: P) -> Result<(), Self::Error>
    where
        P: AsRef<Path>,
        T: File<Artichoke = Self::Artichoke, Error = Self::Exception>,
    {
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        let path = path.as_ref();
        state.load_path_vfs.register_extension(path, T::require)?;
        Ok(())
    }

    fn def_rb_source_file<P, T>(&mut self, path: P, contents: T) -> Result<(), Self::Error>
    where
        P: AsRef<Path>,
        T: Into<Cow<'static, [u8]>>,
    {
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        let path = path.as_ref();
        state.load_path_vfs.write_file(path, contents.into())?;
        Ok(())
    }

    fn resolve_source_path<P>(&self, path: P) -> Result<Option<Vec<u8>>, Self::Error>
    where
        P: AsRef<Path>,
    {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let path = path.as_ref();
        if let Some(path) = state.load_path_vfs.resolve_file(path) {
            return Ok(Some(path));
        }
        // If the given path did not end in `.rb`, try again with a `.rb` file
        // extension.
        if !matches!(path.extension(), Some(ext) if *ext == *OsStr::new(RUBY_EXTENSION)) {
            let mut path = path.to_owned();
            path.set_extension(RUBY_EXTENSION);
            return Ok(state.load_path_vfs.resolve_file(&path));
        }
        Ok(None)
    }

    fn source_is_file<P>(&self, path: P) -> Result<bool, Self::Error>
    where
        P: AsRef<Path>,
    {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let path = path.as_ref();
        if state.load_path_vfs.is_file(path) {
            return Ok(true);
        }
        // If the given path did not end in `.rb`, try again with a `.rb` file
        // extension.
        if !matches!(path.extension(), Some(ext) if *ext == *OsStr::new(RUBY_EXTENSION)) {
            let mut path = path.to_owned();
            path.set_extension(RUBY_EXTENSION);
            if state.load_path_vfs.is_file(&path) {
                return Ok(true);
            }
        }
        Ok(false)
    }

    fn load_source<P>(&mut self, path: P) -> Result<Loaded, Self::Error>
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        // Require Rust `File` first because an File may define classes and
        // modules with `LoadSources` and Ruby files can require arbitrary
        // other files, including some child sources that may depend on these
        // module definitions.
        if let Some(hook) = state.load_path_vfs.get_extension(path) {
            // dynamic, Rust-backed `File` require
            hook(self)?;
        }
        let contents = self
            .read_source_file_contents(path)
            .map_err(|_| {
                let mut message = b"cannot load such file".to_vec();
                if let Ok(bytes) = os_str_to_bytes(path.as_os_str()) {
                    message.extend_from_slice(b" -- ");
                    message.extend_from_slice(bytes);
                }
                LoadError::from(message)
            })?
            .into_owned();
        self.eval(contents.as_ref())?;
        Ok(Loaded::Success)
    }

    fn require_source<P>(&mut self, path: P) -> Result<Required, Self::Error>
    where
        P: AsRef<Path>,
    {
        let path = path.as_ref();
        let mut alternate_path;
        let path = {
            let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
            // If a file is already required, short circuit.
            if let Some(true) = state.load_path_vfs.is_required(path) {
                return Ok(Required::AlreadyRequired);
            }
            // Require Rust `File` first because an File may define classes and
            // modules with `LoadSources` and Ruby files can require arbitrary
            // other files, including some child sources that may depend on these
            // module definitions.
            match state.load_path_vfs.get_extension(path) {
                Some(hook) => {
                    // dynamic, Rust-backed `File` require
                    hook(self)?;
                    path
                }
                None if matches!(path.extension(), Some(ext) if *ext == *OsStr::new(RUBY_EXTENSION)) => path,
                None => {
                    alternate_path = path.to_owned();
                    alternate_path.set_extension(RUBY_EXTENSION);
                    // If a file is already required, short circuit.
                    if let Some(true) = state.load_path_vfs.is_required(&alternate_path) {
                        return Ok(Required::AlreadyRequired);
                    }
                    if let Some(hook) = state.load_path_vfs.get_extension(&alternate_path) {
                        // dynamic, Rust-backed `File` require
                        hook(self)?;
                    } else {
                        // Try to load the source at the given path
                        if let Ok(contents) = self.read_source_file_contents(path) {
                            let contents = contents.into_owned();
                            self.eval(&contents)?;
                            let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
                            state.load_path_vfs.mark_required(path)?;
                            return Ok(Required::Success);
                        }
                        // else proceed with the alternate path
                    }
                    // This ensures that if we load the hook at an alternate
                    // path, we use that alternate path to load the Ruby source.
                    &alternate_path
                }
            }
        };
        let contents = self.read_source_file_contents(path)?.into_owned();
        self.eval(contents.as_ref())?;
        let state = self.state.as_deref_mut().ok_or_else(InterpreterExtractError::new)?;
        state.load_path_vfs.mark_required(path)?;
        Ok(Required::Success)
    }

    fn read_source_file_contents<P>(&self, path: P) -> Result<Cow<'_, [u8]>, Self::Error>
    where
        P: AsRef<Path>,
    {
        let state = self.state.as_deref().ok_or_else(InterpreterExtractError::new)?;
        let path = path.as_ref();
        let contents = state.load_path_vfs.read_file(path)?;
        Ok(contents.into())
    }
}

#[cfg(test)]
mod tests {
    use artichoke_core::load::{Loaded, Required};
    use bstr::ByteSlice;

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

    const NON_IDEMPOTENT_LOAD: &[u8] = br#"
module LoadSources
  class Counter
    attr_reader :c

    def initialize(c)
      @c = c
    end

    def inc!
      @c += 1
    end

    def self.instance
      @instance ||= new(10)
    end
  end
end

LoadSources::Counter.instance.inc!
    "#;

    #[test]
    fn load_has_no_memory() {
        let mut interp = interpreter();
        interp.def_rb_source_file("counter.rb", NON_IDEMPOTENT_LOAD).unwrap();

        let result = interp.load_source("./counter.rb").unwrap();
        assert_eq!(result, Loaded::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 11);

        // `Kernel#load` has no memory and will always execute
        let result = interp.load_source("./counter.rb").unwrap();
        assert_eq!(result, Loaded::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 12);
    }

    #[test]
    fn load_has_no_memory_and_ignores_loaded_features() {
        let mut interp = interpreter();
        interp.def_rb_source_file("counter.rb", NON_IDEMPOTENT_LOAD).unwrap();

        let result = interp.require_source("./counter.rb").unwrap();
        assert_eq!(result, Required::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 11);

        let result = interp.require_source("./counter.rb").unwrap();
        assert_eq!(result, Required::AlreadyRequired);

        let result = interp.load_source("./counter.rb").unwrap();
        assert_eq!(result, Loaded::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 12);

        // `Kernel#load` has no memory and will always execute
        let result = interp.load_source("./counter.rb").unwrap();
        assert_eq!(result, Loaded::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 13);
    }

    #[test]
    fn load_does_not_discover_paths_from_loaded_features() {
        let mut interp = interpreter();
        interp.def_rb_source_file("counter.rb", NON_IDEMPOTENT_LOAD).unwrap();

        let result = interp.require_source("./counter").unwrap();
        assert_eq!(result, Required::Success);
        let count = interp
            .eval(b"LoadSources::Counter.instance.c")
            .unwrap()
            .try_convert_into::<usize>(&interp)
            .unwrap();
        assert_eq!(count, 11);

        let exc = interp.load_source("./counter").unwrap_err();
        assert_eq!(exc.message().as_bstr(), b"cannot load such file -- ./counter".as_bstr());
        assert_eq!(exc.name(), "LoadError");
    }
}