artichoke_backend/extn/core/thread/
mod.rs

1use std::ffi::CStr;
2
3use crate::extn::prelude::*;
4
5const THREAD_CSTR: &CStr = c"Thread";
6const MUTEX_CSTR: &CStr = c"Mutex";
7static THREAD_RUBY_SOURCE: &[u8] = include_bytes!("thread.rb");
8
9pub fn init(interp: &mut Artichoke) -> InitializeResult<()> {
10    if interp.is_class_defined::<Thread>() {
11        return Ok(());
12    }
13    if interp.is_class_defined::<Mutex>() {
14        return Ok(());
15    }
16
17    let spec = class::Spec::new("Thread", THREAD_CSTR, None, None)?;
18    interp.def_class::<Thread>(spec)?;
19    let spec = class::Spec::new("Mutex", MUTEX_CSTR, None, None)?;
20    interp.def_class::<Mutex>(spec)?;
21    // TODO: Don't add a source file and don't add an explicit require below.
22    // Instead, have thread be a default loaded feature in `mezzaluna-feature-loader`.
23    interp.def_rb_source_file("thread.rb", THREAD_RUBY_SOURCE)?;
24    // Thread is loaded by default, so eval it on interpreter initialization
25    // <https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/UnneededRequireStatement>
26    interp.eval(&b"require 'thread'"[..])?;
27
28    Ok(())
29}
30
31#[derive(Debug, Clone, Copy)]
32pub struct Thread;
33
34#[derive(Debug, Clone, Copy)]
35pub struct Mutex;
36
37#[cfg(test)]
38mod tests {
39    use crate::test::prelude::*;
40
41    const SUBJECT: &str = "Thread";
42    const FUNCTIONAL_TEST: &[u8] = include_bytes!("thread_test.rb");
43
44    #[test]
45    fn functional() {
46        let mut interp = interpreter();
47        let result = interp.eval(FUNCTIONAL_TEST);
48        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
49        let result = interp.eval(b"spec");
50        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
51    }
52}