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

use scolapasta_path::{os_str_to_bytes, os_string_to_bytes};
use spinoso_string::String;

use crate::convert::BoxUnboxVmValue;
use crate::core::TryConvertMut;
use crate::error::Error;
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> {
        let s = String::utf8(value);
        let value = String::alloc_value(s, self)?;
        Ok(self.protect(value))
    }
}

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

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

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, mut value: Value) -> Result<Vec<u8>, Self::Error> {
        let s = unsafe { String::unbox_from_value(&mut value, self)? };
        Ok(s.clone().into_vec())
    }
}

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

    fn try_convert_mut(&mut self, mut value: Value) -> Result<&'a [u8], Self::Error> {
        self.protect(value);
        let s = unsafe { String::unbox_from_value(&mut value, self)? };
        // SAFETY: This transmute modifies the lifetime of the byte slice pulled
        // out of the boxed `String`. This requires that no garbage collections
        // that reclaim `value` occur while this slice is alive. This is
        // enforced for at least this entry from an mruby trampoline by the call
        // to `protect` above.
        //
        // FIXME: does this unbound lifetime and transmute below allow
        // extracting `&'static [u8]`?
        let slice = unsafe { mem::transmute(s.as_slice()) };
        Ok(slice)
    }
}

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

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

    #[test]
    fn fail_convert() {
        let mut interp = interpreter();
        // 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();
        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();
            let value = interp.try_convert_mut(bytes).unwrap();
            value.ruby_type() == Ruby::String
        }

        fn byte_string_borrowed(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter();
            // 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.first().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
        }

        fn byte_string_owned(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter();
            // 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.first().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
        }

        fn roundtrip(bytes: Vec<u8>) -> bool {
            let mut interp = interpreter();
            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();
            let value = interp.convert(b);
            let value = value.try_convert_into_mut::<Vec<u8>>(&mut interp);
            value.is_err()
        }
    }
}