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
use std::borrow::Cow;
use std::convert::TryFrom;
use std::ffi::{OsStr, OsString};
use std::slice;

use crate::convert::UnboxRubyError;
use crate::core::{ConvertMut, TryConvertMut};
use crate::exception::Exception;
use crate::ffi;
use crate::sys;
use crate::types::{Ruby, Rust};
use crate::value::Value;
use crate::Artichoke;

impl ConvertMut<Vec<u8>, Value> for Artichoke {
    fn convert_mut(&mut self, value: Vec<u8>) -> Value {
        self.convert_mut(value.as_slice())
    }
}

impl ConvertMut<&[u8], Value> for Artichoke {
    fn convert_mut(&mut self, value: &[u8]) -> Value {
        // Ruby strings contain raw bytes, so we can convert from a &[u8] to a
        // `char *` and `size_t`.
        let raw = value.as_ptr() as *const i8;
        let len = value.len();
        // `mrb_str_new` copies the `char *` to the mruby heap so we do not have
        // to worry about the lifetime of the slice passed into this converter.
        let string = unsafe { self.with_ffi_boundary(|mrb| sys::mrb_str_new(mrb, raw, len)) };
        Value::from(string.unwrap())
    }
}

impl<'a> ConvertMut<Cow<'a, [u8]>, Value> for Artichoke {
    fn convert_mut(&mut self, value: Cow<'a, [u8]>) -> Value {
        match value {
            Cow::Borrowed(bytes) => self.convert_mut(bytes),
            Cow::Owned(bytes) => self.convert_mut(bytes),
        }
    }
}

impl TryConvertMut<OsString, Value> for Artichoke {
    type Error = Exception;

    fn try_convert_mut(&mut self, value: OsString) -> Result<Value, Self::Error> {
        let bytes = ffi::os_str_to_bytes(&*value)?;
        Ok(self.convert_mut(bytes))
    }
}

impl TryConvertMut<&OsStr, Value> for Artichoke {
    type Error = Exception;

    fn try_convert_mut(&mut self, value: &OsStr) -> Result<Value, Self::Error> {
        let bytes = ffi::os_str_to_bytes(value)?;
        Ok(self.convert_mut(bytes))
    }
}

impl<'a> TryConvertMut<Cow<'a, OsStr>, Value> for Artichoke {
    type Error = Exception;

    fn try_convert_mut(&mut self, value: Cow<'a, OsStr>) -> Result<Value, Self::Error> {
        match value {
            Cow::Borrowed(value) => {
                let bytes = ffi::os_str_to_bytes(value)?;
                Ok(self.convert_mut(bytes))
            }
            Cow::Owned(value) => {
                let bytes = ffi::os_string_to_bytes(value)?;
                Ok(self.convert_mut(bytes))
            }
        }
    }
}

impl TryConvertMut<Value, Vec<u8>> for Artichoke {
    type Error = Exception;

    fn try_convert_mut(&mut self, value: Value) -> Result<Vec<u8>, Self::Error> {
        TryConvertMut::<_, &[u8]>::try_convert_mut(self, value).map(<[_]>::to_vec)
    }
}

impl<'a> TryConvertMut<Value, &'a [u8]> for Artichoke {
    type Error = Exception;

    fn try_convert_mut(&mut self, value: Value) -> Result<&'a [u8], Self::Error> {
        if let Ruby::String = value.ruby_type() {
            let bytes = value.inner();
            unsafe {
                self.with_ffi_boundary(|mrb| {
                    let raw = sys::mrb_string_value_ptr(mrb, bytes) as *const u8;
                    let len = sys::mrb_string_value_len(mrb, bytes);
                    if let Ok(len) = usize::try_from(len) {
                        // We can return a borrowed slice because the memory is
                        // stored on the mruby heap. As long as `value` is
                        // reachable, this slice points to valid memory.
                        Ok(slice::from_raw_parts(raw, len))
                    } else {
                        Err(UnboxRubyError::new(&value, Rust::Bytes).into())
                    }
                })?
            }
        } else {
            Err(Exception::from(UnboxRubyError::new(&value, Rust::Bytes)))
        }
    }
}

#[cfg(test)]
mod tests {
    use quickcheck_macros::quickcheck;

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

    #[test]
    fn fail_convert() {
        let mut interp = interpreter().unwrap();
        // get a Ruby value that can't be converted to a primitive type.
        let value = interp.eval(b"Object.new").unwrap();
        let result = value.try_into_mut::<Vec<u8>>(&mut interp);
        assert!(result.is_err());
    }

    #[quickcheck]
    fn convert_to_vec(bytes: Vec<u8>) -> bool {
        let mut interp = interpreter().unwrap();
        let value = interp.convert_mut(bytes);
        value.ruby_type() == Ruby::String
    }

    #[quickcheck]
    fn bytestring(bytes: Vec<u8>) -> bool {
        let mut interp = interpreter().unwrap();
        // Borrowed converter
        let value = interp.convert_mut(bytes.as_slice());
        let len = value.funcall(&mut interp, "length", &[], None).unwrap();
        let len = len.try_into::<usize>(&interp).unwrap();
        if len != bytes.len() {
            return false;
        }
        let empty = value.funcall(&mut interp, "empty?", &[], None).unwrap();
        let empty = empty.try_into::<bool>(&interp).unwrap();
        if empty != bytes.is_empty() {
            return false;
        }
        let zero = interp.convert(0);
        let first = value.funcall(&mut interp, "[]", &[zero], None).unwrap();
        let first = first.try_into_mut::<Option<&[u8]>>(&mut interp).unwrap();
        if first != bytes.get(0..1) {
            return false;
        }
        let recovered: Vec<u8> = interp.try_convert_mut(value).unwrap();
        if recovered != bytes {
            return false;
        }
        // Owned converter
        let value = interp.convert_mut(bytes.to_vec());
        let len = value.funcall(&mut interp, "length", &[], None).unwrap();
        let len = len.try_into::<usize>(&interp).unwrap();
        if len != bytes.len() {
            return false;
        }
        let empty = value.funcall(&mut interp, "empty?", &[], None).unwrap();
        let empty = empty.try_into::<bool>(&interp).unwrap();
        if empty != bytes.is_empty() {
            return false;
        }
        let zero = interp.convert(0);
        let first = value.funcall(&mut interp, "[]", &[zero], None).unwrap();
        let first = first.try_into_mut::<Option<&[u8]>>(&mut interp).unwrap();
        if first != bytes.get(0..1) {
            return false;
        }
        let recovered: Vec<u8> = interp.try_convert_mut(value).unwrap();
        if recovered != bytes {
            return false;
        }
        true
    }

    #[quickcheck]
    fn roundtrip(bytes: Vec<u8>) -> bool {
        let mut interp = interpreter().unwrap();
        let value = interp.convert_mut(bytes.as_slice());
        let value = value.try_into_mut::<Vec<u8>>(&mut interp).unwrap();
        value == bytes
    }

    #[quickcheck]
    fn roundtrip_err(b: bool) -> bool {
        let mut interp = interpreter().unwrap();
        let value = interp.convert(b);
        let value = value.try_into_mut::<Vec<u8>>(&mut interp);
        value.is_err()
    }
}