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
use std::ffi::CStr;

use crate::extn::prelude::*;

const THREAD_CSTR: &CStr = qed::const_cstr_from_str!("Thread\0");
const MUTEX_CSTR: &CStr = qed::const_cstr_from_str!("Mutex\0");
static THREAD_RUBY_SOURCE: &[u8] = include_bytes!("thread.rb");

pub fn init(interp: &mut Artichoke) -> InitializeResult<()> {
    if interp.is_class_defined::<Thread>() {
        return Ok(());
    }
    if interp.is_class_defined::<Mutex>() {
        return Ok(());
    }

    let spec = class::Spec::new("Thread", THREAD_CSTR, None, None)?;
    interp.def_class::<Thread>(spec)?;
    let spec = class::Spec::new("Mutex", MUTEX_CSTR, None, None)?;
    interp.def_class::<Mutex>(spec)?;
    // TODO: Don't add a source file and don't add an explicit require below.
    // Instead, have thread be a default loaded feature in `mezzaluna-feature-loader`.
    interp.def_rb_source_file("thread.rb", THREAD_RUBY_SOURCE)?;
    // Thread is loaded by default, so eval it on interpreter initialization
    // https://www.rubydoc.info/gems/rubocop/RuboCop/Cop/Lint/UnneededRequireStatement
    interp.eval(&b"require 'thread'"[..])?;

    Ok(())
}

#[derive(Debug, Clone, Copy)]
pub struct Thread;

#[derive(Debug, Clone, Copy)]
pub struct Mutex;

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

    const SUBJECT: &str = "Thread";
    const FUNCTIONAL_TEST: &[u8] = include_bytes!("thread_test.rb");

    #[test]
    fn functional() {
        let mut interp = interpreter();
        let result = interp.eval(FUNCTIONAL_TEST);
        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
        let result = interp.eval(b"spec");
        unwrap_or_panic_with_backtrace(&mut interp, SUBJECT, result);
    }
}