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
#![deny(missing_docs, warnings, intra_doc_link_resolution_failure)]
#![deny(clippy::all, clippy::pedantic)]

//! Crate `mruby-sys` is Rust bindings for mruby (currently version 2.0.1),
//! statically linked with FFI API generated by bindgen.

use std::ffi::CStr;
use std::fmt;

mod args;
#[allow(missing_docs)]
#[allow(non_camel_case_types)]
#[allow(non_upper_case_globals)]
#[allow(non_snake_case)]
#[allow(clippy::all, clippy::pedantic)]
mod ffi;

#[path = "ffi_tests.rs"]
#[cfg(test)]
mod ffi_tests;

pub use self::args::*;
pub use self::ffi::*;

/// Version metadata `String` for mruby-sys and embedded mruby.
pub fn mruby_sys_version(verbose: bool) -> String {
    if verbose {
        let engine = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_ENGINE) };
        let version = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_VERSION) };
        format!(
            "{} {} [{}]",
            engine.to_str().expect("mruby engine name"),
            version.to_str().expect("mruby engine version"),
            env!("CARGO_PKG_VERSION")
        )
    } else {
        env!("CARGO_PKG_VERSION").to_owned()
    }
}

/// Methods to describe an [`mrb_state`].
pub trait DescribeState {
    /// Wraper around [`fmt::Display`] for [`mrb_state`]. Returns Ruby engine
    /// and interpreter version. For example:
    ///
    /// ```text
    /// mruby 2.0
    /// ```
    fn info(&self) -> String;

    /// Wrapper around [`fmt::Debug`] for [`mrb_state`]. Returns Ruby engine,
    /// interpreter version, engine version, vendored revision, and
    /// [`mrb_state`] address. For example:
    ///
    /// ```text
    /// mruby 2.0 (v2.0.1 rev c078758) interpreter at 0x7f85b8800000
    /// ```
    fn debug(&self) -> String;

    /// Returns detailed interpreter version including engine version. For
    /// example:
    ///
    /// ```text
    /// 2.0 (v2.0.1)
    /// ```
    fn version(&self) -> String;

    /// Returns revision of vendored mruby source. See `build.rs`. For example:
    ///
    /// ```text
    /// c078758
    /// ```
    fn revision(&self) -> String;
}

impl DescribeState for *mut mrb_state {
    fn info(&self) -> String {
        if self.is_null() {
            "".to_owned()
        } else {
            format!("{}", unsafe { &**self })
        }
    }

    fn debug(&self) -> String {
        if self.is_null() {
            "".to_owned()
        } else {
            format!("{:?}", unsafe { &**self })
        }
    }

    fn version(&self) -> String {
        // Using the unchecked function is safe because these values are C constants
        let version = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_VERSION) };
        format!(
            "{} (v{}.{}.{})",
            version.to_str().expect("mruby engine version"),
            MRUBY_RELEASE_MAJOR,
            MRUBY_RELEASE_MINOR,
            MRUBY_RELEASE_TEENY,
        )
    }

    fn revision(&self) -> String {
        env!("MRUBY_REVISION").to_owned()
    }
}

impl fmt::Debug for mrb_state {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Using the unchecked function is safe because these values are C constants
        let engine = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_ENGINE) };
        let version = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_VERSION) };
        write!(
            f,
            "{} {} (v{}.{}.{} rev {}) interpreter at {:p}",
            engine.to_str().expect("mruby engine name"),
            version.to_str().expect("mruby engine version"),
            MRUBY_RELEASE_MAJOR,
            MRUBY_RELEASE_MINOR,
            MRUBY_RELEASE_TEENY,
            env!("MRUBY_REVISION"),
            self
        )
    }
}

impl fmt::Display for mrb_state {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        // Using the unchecked function is safe because these values are C constants
        let engine = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_ENGINE) };
        let version = unsafe { CStr::from_bytes_with_nul_unchecked(MRUBY_RUBY_VERSION) };
        write!(
            f,
            "{} {}",
            engine.to_str().expect("mruby engine name"),
            version.to_str().expect("mruby engine version"),
        )
    }
}

#[cfg(test)]
mod tests {
    use crate::{mrb_close, mrb_open, DescribeState};

    #[test]
    fn interpreter_display() {
        unsafe {
            let mrb = mrb_open();
            assert_eq!(format!("{}", *mrb), "mruby 2.0");
            assert_eq!(mrb.info(), "mruby 2.0");
            mrb_close(mrb);
        }
    }

    #[test]
    fn interpreter_debug() {
        unsafe {
            let mrb = mrb_open();
            assert_eq!(
                format!("{:?}", *mrb),
                format!("mruby 2.0 (v2.0.1 rev bc7c5d3) interpreter at {:p}", &*mrb)
            );
            assert_eq!(
                mrb.debug(),
                format!("mruby 2.0 (v2.0.1 rev bc7c5d3) interpreter at {:p}", &*mrb)
            );
            mrb_close(mrb);
        }
    }

    #[test]
    fn version() {
        unsafe {
            let mrb = mrb_open();
            assert_eq!(mrb.version(), "2.0 (v2.0.1)");
            mrb_close(mrb);
        }
    }

    #[test]
    fn revision() {
        unsafe {
            let mrb = mrb_open();
            assert_eq!(mrb.revision(), "bc7c5d3");
            mrb_close(mrb);
        }
    }
}