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

use crate::convert::UnboxRubyError;
use crate::core::{TryConvertMut, Value as _};
use crate::error::Error;
use crate::platform_string::{os_str_to_bytes, os_string_to_bytes};
use crate::sys;
use crate::types::{Ruby, Rust};
use crate::value::Value;
use crate::Artichoke;

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

    fn try_convert_mut(&mut self, value: Vec<u8>) -> Result<Value, Self::Error> {
        self.try_convert_mut(value.as_slice())
    }
}

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

    fn try_convert_mut(&mut self, value: &[u8]) -> Result<Value, Self::Error> {
        // Ruby strings contain raw bytes, so we can convert from a &[u8] to a
        // `char *` and `size_t`.
        let raw = value.as_ptr().cast::<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))? };
        Ok(string.into())
    }
}

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

    fn try_convert_mut(&mut self, value: Cow<'a, [u8]>) -> Result<Value, Self::Error> {
        match value {
            Cow::Borrowed(bytes) => self.try_convert_mut(bytes),
            Cow::Owned(bytes) => self.try_convert_mut(bytes),
        }
    }
}

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

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

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

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

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

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

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

    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 = Error;

    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).cast::<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(UnboxRubyError::new(&value, Rust::Bytes).into())
        }
    }
}

#[cfg(test)]
mod tests {
    use bstr::ByteSlice;
    use quickcheck::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_convert_into_mut::<Vec<u8>>(&mut interp);
        assert!(result.is_err());
    }

    #[test]
    fn convert_with_trailing_nul() {
        let mut interp = interpreter().unwrap();
        let bytes: &[u8] = &[0];
        let value = interp.try_convert_mut(bytes).unwrap();
        let retrieved_bytes = value.try_convert_into_mut::<&[u8]>(&mut interp).unwrap();
        assert_eq!(bytes.as_bstr(), retrieved_bytes.as_bstr());

        let len = value.funcall(&mut interp, "bytesize", &[], None).unwrap();
        let len = len.try_convert_into::<usize>(&interp).unwrap();
        assert_eq!(len, 1);

        let empty = value.funcall(&mut interp, "empty?", &[], None).unwrap();
        let empty = empty.try_convert_into::<bool>(&interp).unwrap();
        assert!(!empty);

        let zero = interp.convert(0);
        let one = interp.convert(1);

        let str_bytes = value.funcall(&mut interp, "bytes", &[], None).unwrap();
        let first = str_bytes.funcall(&mut interp, "[]", &[zero], None).unwrap();
        let first = first.try_convert_into::<i64>(&interp).unwrap();
        assert_eq!(first, 0_i64);

        let slice = value.funcall(&mut interp, "byteslice", &[zero, one], None).unwrap();
        let slice = slice.try_convert_into_mut::<Option<&[u8]>>(&mut interp).unwrap();
        let expected: Option<&[u8]> = Some(&[0]);
        assert_eq!(slice, expected);
    }

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

        #[allow(clippy::needless_pass_by_value)]
        fn bytestring_borrowed(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter().unwrap();
            // Borrowed converter
            let value = interp.try_convert_mut(bytes.as_slice()).unwrap();
            let len = value.funcall(&mut interp, "bytesize", &[], None).unwrap();
            let len = len.try_convert_into::<usize>(&interp).unwrap();
            if len != bytes.len() {
                return false;
            }

            let empty = value.funcall(&mut interp, "empty?", &[], None).unwrap();
            let empty = empty.try_convert_into::<bool>(&interp).unwrap();
            if empty != bytes.is_empty() {
                return false;
            }

            let zero = interp.convert(0);
            let one = interp.convert(1);

            let str_bytes = value.funcall(&mut interp, "bytes", &[], None).unwrap();
            let first = str_bytes.funcall(&mut interp, "[]", &[zero], None).unwrap();
            let first = first.try_convert_into::<Option<i64>>(&interp).unwrap();
            if first != bytes.get(0).copied().map(i64::from) {
                return false;
            }

            let slice = value.funcall(&mut interp, "byteslice", &[zero, one], None).unwrap();
            let slice = slice.try_convert_into_mut::<Option<&[u8]>>(&mut interp).unwrap();
            if slice.unwrap_or_default() != bytes.get(0..1).unwrap_or_default() {
                return false;
            }

            let recovered: Vec<u8> = interp.try_convert_mut(value).unwrap();
            if recovered != bytes {
                return false;
            }
            true
        }

        #[allow(clippy::needless_pass_by_value)]
        fn bytestring_owned(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter().unwrap();
            // Owned converter
            let value = interp.try_convert_mut(bytes.clone()).unwrap();
            let len = value.funcall(&mut interp, "bytesize", &[], None).unwrap();
            let len = len.try_convert_into::<usize>(&interp).unwrap();
            if len != bytes.len() {
                return false;
            }

            let empty = value.funcall(&mut interp, "empty?", &[], None).unwrap();
            let empty = empty.try_convert_into::<bool>(&interp).unwrap();
            if empty != bytes.is_empty() {
                return false;
            }

            let zero = interp.convert(0);
            let one = interp.convert(1);

            let str_bytes = value.funcall(&mut interp, "bytes", &[], None).unwrap();
            let first = str_bytes.funcall(&mut interp, "[]", &[zero], None).unwrap();
            let first = first.try_convert_into::<Option<i64>>(&interp).unwrap();
            if first != bytes.get(0).copied().map(i64::from) {
                return false;
            }

            let slice = value.funcall(&mut interp, "byteslice", &[zero, one], None).unwrap();
            let slice = slice.try_convert_into_mut::<Option<&[u8]>>(&mut interp).unwrap();
            if slice.unwrap_or_default() != bytes.get(0..1).unwrap_or_default() {
                return false;
            }

            let recovered: Vec<u8> = interp.try_convert_mut(value).unwrap();
            if recovered != bytes {
                return false;
            }
            true
        }

        #[allow(clippy::needless_pass_by_value)]
        fn roundtrip(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter().unwrap();
            let value = interp.try_convert_mut(bytes.as_slice()).unwrap();
            let value = value.try_convert_into_mut::<Vec<u8>>(&mut interp).unwrap();
            value == bytes
        }

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