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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use std::ffi::c_void;
use std::fmt;
use std::marker::PhantomData;
use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};
use std::ptr;

use crate::core::Value as _;
use crate::def::NotDefinedError;
use crate::error::Error;
use crate::extn::core::exception::TypeError;
use crate::ffi::InterpreterExtractError;
use crate::sys;
use crate::types::Ruby;
use crate::value::Value;
use crate::Artichoke;

pub struct UnboxedValueGuard<'a, T> {
    guarded: ManuallyDrop<T>,
    phantom: PhantomData<&'a mut T>,
}

impl<'a, T> fmt::Debug for UnboxedValueGuard<'a, T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("UnboxedValueGuard")
            .field("guarded", &self.guarded)
            .finish()
    }
}

impl<'a, T> UnboxedValueGuard<'a, T> {
    /// Construct a new guard around the given `T`.
    ///
    /// `UnboxedValueGuard` allows passing around a `&mut` reference without
    /// dropping the `T` when returning control to mruby C code. This is
    /// desirable because the `T` is owned by the mruby heap until the mruby
    /// garbage collector frees the `mrb_value` that holds it.
    #[must_use]
    pub fn new(value: T) -> Self {
        Self {
            guarded: ManuallyDrop::new(value),
            phantom: PhantomData,
        }
    }

    /// Get a shared reference to the inner `T`.
    #[inline]
    #[must_use]
    pub fn as_inner_ref(&self) -> &T {
        &*self.guarded
    }

    /// Get a unique reference to the inner `T`.
    ///
    /// # Safety
    ///
    /// Callers must ensure the raw parts stored in the source `mrb_value` are
    /// not invalidated OR that the raw parts are repacked before an mruby
    /// allocation occurs.
    #[inline]
    #[must_use]
    pub unsafe fn as_inner_mut(&mut self) -> &mut T {
        &mut *self.guarded
    }

    /// Take the inner `T` out of the guard.
    ///
    /// # Safety
    ///
    /// Callers must ensure that `T` is not dropped. Once `T` is taken out of
    /// the guard, it must ultimately be passed to [`ManuallyDrop`] before it is
    /// dropped.
    ///
    /// An example of safe usage is calling taking an `Array` out of the guard
    /// and then immediately calling `Array::into_raw_parts` on the returned
    /// value.
    #[inline]
    #[must_use]
    pub unsafe fn take(mut self) -> T {
        ManuallyDrop::take(&mut self.guarded)
    }
}

#[derive(Debug)]
pub struct HeapAllocated<T>(Box<T>);

impl<T> HeapAllocated<T> {
    #[must_use]
    pub fn new(obj: Box<T>) -> Self {
        Self(obj)
    }
}

impl<'a, T> AsRef<T> for UnboxedValueGuard<'a, HeapAllocated<T>> {
    fn as_ref(&self) -> &T {
        self.guarded.deref().0.as_ref()
    }
}

impl<'a, T> AsMut<T> for UnboxedValueGuard<'a, HeapAllocated<T>> {
    fn as_mut(&mut self) -> &mut T {
        self.guarded.deref_mut().0.as_mut()
    }
}

impl<'a, T> Deref for UnboxedValueGuard<'a, HeapAllocated<T>> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.guarded.deref().0.as_ref()
    }
}

impl<'a, T> DerefMut for UnboxedValueGuard<'a, HeapAllocated<T>> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        // Safety:
        //
        // `HeapAllocated` data objects are boxed and the raw box pointer is
        // stored in the `mrb_value`.
        //
        // `Deref::Target` is `T`, not `Box<T>`.
        //
        // Giving out a `&mut T` means the box pointer cannot be invalidated.
        let inner = unsafe { self.as_inner_mut() };
        inner.0.as_mut()
    }
}

pub trait HeapAllocatedData {
    const RUBY_TYPE: &'static str;
}

#[derive(Debug)]
pub struct Immediate<T>(T);

impl<T> Immediate<T> {
    pub fn new(obj: T) -> Self {
        Self(obj)
    }
}

impl<'a, T> Deref for UnboxedValueGuard<'a, Immediate<T>> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.guarded.deref().0
    }
}

impl<'a, T> DerefMut for UnboxedValueGuard<'a, Immediate<T>> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.guarded.deref_mut().0
    }
}

pub trait BoxUnboxVmValue {
    type Unboxed;
    type Guarded;

    const RUBY_TYPE: &'static str;

    /// # Safety
    ///
    /// Implementations may return owned values. These values must not outlive
    /// the underlying `mrb_value`, which may be garbage collected by mruby.
    ///
    /// The values returned by this method should not be stored for more than
    /// the current FFI trampoline entry point.
    unsafe fn unbox_from_value<'a>(
        value: &'a mut Value,
        interp: &mut Artichoke,
    ) -> Result<UnboxedValueGuard<'a, Self::Guarded>, Error>;

    fn alloc_value(value: Self::Unboxed, interp: &mut Artichoke) -> Result<Value, Error>;

    fn box_into_value(value: Self::Unboxed, into: Value, interp: &mut Artichoke) -> Result<Value, Error>;

    fn free(data: *mut c_void);
}

impl<T> BoxUnboxVmValue for T
where
    T: HeapAllocatedData + Sized + 'static,
{
    type Unboxed = Self;
    type Guarded = HeapAllocated<Self::Unboxed>;

    const RUBY_TYPE: &'static str = <Self as HeapAllocatedData>::RUBY_TYPE;

    unsafe fn unbox_from_value<'a>(
        value: &'a mut Value,
        interp: &mut Artichoke,
    ) -> Result<UnboxedValueGuard<'a, Self::Guarded>, Error> {
        // Make sure we have a Data otherwise extraction will fail.
        if value.ruby_type() != Ruby::Data {
            let mut message = String::from("uninitialized ");
            message.push_str(Self::RUBY_TYPE);
            return Err(TypeError::from(message).into());
        }

        let mut rclass = {
            let state = interp.state.as_ref().ok_or_else(InterpreterExtractError::new)?;
            let spec = state
                .classes
                .get::<Self>()
                .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?;
            let rclass = spec.rclass();
            interp
                .with_ffi_boundary(|mrb| rclass.resolve(mrb))?
                .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?
        };

        // Sanity check that the RClass matches.
        let value_rclass = interp.with_ffi_boundary(|mrb| sys::mrb_sys_class_of_value(mrb, value.inner()))?;
        if !ptr::eq(value_rclass, rclass.as_mut()) {
            let mut message = String::from("Could not extract ");
            message.push_str(Self::RUBY_TYPE);
            message.push_str(" from receiver");
            return Err(TypeError::from(message).into());
        }

        // Copy data pointer out of the `mrb_value` box.
        let state = interp.state.as_ref().ok_or_else(InterpreterExtractError::new)?;
        let spec = state
            .classes
            .get::<Self>()
            .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?;
        let data_type = spec.data_type();
        let embedded_data_ptr =
            interp.with_ffi_boundary(|mrb| sys::mrb_data_check_get_ptr(mrb, value.inner(), data_type))?;
        if embedded_data_ptr.is_null() {
            // `Object#allocate` can be used to create `MRB_TT_DATA` without calling
            // `#initialize`. These objects will return a NULL pointer.
            let mut message = String::from("uninitialized ");
            message.push_str(Self::RUBY_TYPE);
            return Err(TypeError::from(message).into());
        }

        // Move the data pointer into a `Box`.
        let value = Box::from_raw(embedded_data_ptr.cast::<Self>());
        // `UnboxedValueGuard` ensures the `Box` wrapper will be forgotten. The
        // mruby GC is responsible for freeing the value.
        Ok(UnboxedValueGuard::new(HeapAllocated::new(value)))
    }

    fn alloc_value(value: Self::Unboxed, interp: &mut Artichoke) -> Result<Value, Error> {
        let mut rclass = {
            let state = interp.state.as_ref().ok_or_else(InterpreterExtractError::new)?;
            let spec = state
                .classes
                .get::<Self>()
                .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?;
            let rclass = spec.rclass();
            unsafe { interp.with_ffi_boundary(|mrb| rclass.resolve(mrb)) }?
                .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?
        };

        // Convert to a raw pointer.
        let data = Box::new(value);
        let ptr = Box::into_raw(data);

        // Allocate a new `mrb_value` and inject the raw data pointer.
        let state = interp.state.as_ref().ok_or_else(InterpreterExtractError::new)?;
        let spec = state
            .classes
            .get::<Self>()
            .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?;
        let data_type = spec.data_type();
        let obj = unsafe {
            interp.with_ffi_boundary(|mrb| {
                let alloc = sys::mrb_data_object_alloc(mrb, rclass.as_mut(), ptr.cast::<c_void>(), data_type);
                sys::mrb_sys_obj_value(alloc.cast::<c_void>())
            })?
        };

        Ok(interp.protect(Value::from(obj)))
    }

    fn box_into_value(value: Self::Unboxed, into: Value, interp: &mut Artichoke) -> Result<Value, Error> {
        let state = interp.state.as_ref().ok_or_else(InterpreterExtractError::new)?;
        let spec = state
            .classes
            .get::<Self>()
            .ok_or_else(|| NotDefinedError::class(Self::RUBY_TYPE))?;

        // Convert to a raw pointer.
        let data = Box::new(value);
        let ptr = Box::into_raw(data);

        // Inject the raw data pointer into the given `mrb_value`.
        let mut obj = into.inner();
        unsafe {
            sys::mrb_sys_data_init(&mut obj, ptr.cast::<c_void>(), spec.data_type());
        }
        Ok(Value::from(obj))
    }

    fn free(data: *mut c_void) {
        // Cast the raw data pointer into a pointer to `Self`.
        let data = data.cast::<Self>();
        // Convert the raw pointer back into a `Box`.
        let unboxed = unsafe { Box::from_raw(data) };
        // And free the memory.
        drop(unboxed);
    }
}

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

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

    // this struct is heap allocated.
    #[derive(Default, Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct Container(String);

    unsafe extern "C" fn container_value(mrb: *mut sys::mrb_state, slf: sys::mrb_value) -> sys::mrb_value {
        unwrap_interpreter!(mrb, to => guard);

        let mut value = Value::from(slf);
        let result = if let Ok(container) = Container::unbox_from_value(&mut value, &mut guard) {
            guard.try_convert_mut(container.0.as_bytes()).unwrap_or_default()
        } else {
            Value::nil()
        };
        result.inner()
    }

    impl HeapAllocatedData for Container {
        const RUBY_TYPE: &'static str = "Container";
    }

    // this struct is stack allocated
    #[derive(Default, Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord)]
    struct Flag(bool);

    impl HeapAllocatedData for Box<Flag> {
        const RUBY_TYPE: &'static str = "Flag";
    }

    #[test]
    fn convert_obj_roundtrip() {
        let mut interp = interpreter().unwrap();
        let spec = class::Spec::new(
            "Container",
            cstr::cstr!("Container"),
            None,
            Some(def::box_unbox_free::<Container>),
        )
        .unwrap();
        class::Builder::for_spec(&mut interp, &spec)
            .value_is_rust_object()
            .add_method("value", container_value, sys::mrb_args_none())
            .unwrap()
            .define()
            .unwrap();
        interp.def_class::<Container>(spec).unwrap();
        let obj = Container(String::from("contained string contents"));

        let mut value = Container::alloc_value(obj, &mut interp).unwrap();
        let class = value.funcall(&mut interp, "class", &[], None).unwrap();
        let class_display = class.to_s(&mut interp);
        assert_eq!(class_display.as_bstr(), b"Container".as_bstr());

        let data = unsafe { Container::unbox_from_value(&mut value, &mut interp) }.unwrap();

        let inner = data.0.as_str();
        assert_eq!(inner, "contained string contents");
        drop(data);

        let inner = value.funcall(&mut interp, "value", &[], None).unwrap();
        let inner = inner.try_convert_into_mut::<&str>(&mut interp).unwrap();
        assert_eq!(inner, "contained string contents");
    }

    #[test]
    fn convert_obj_not_data() {
        let mut interp = interpreter().unwrap();

        let spec = class::Spec::new(
            "Container",
            cstr::cstr!("Container"),
            None,
            Some(def::box_unbox_free::<Container>),
        )
        .unwrap();
        class::Builder::for_spec(&mut interp, &spec)
            .value_is_rust_object()
            .add_method("value", container_value, sys::mrb_args_none())
            .unwrap()
            .define()
            .unwrap();
        interp.def_class::<Container>(spec).unwrap();

        let spec = class::Spec::new(
            "Flag",
            cstr::cstr!("Flag"),
            None,
            Some(def::box_unbox_free::<Box<Flag>>),
        )
        .unwrap();
        class::Builder::for_spec(&mut interp, &spec)
            .value_is_rust_object()
            .define()
            .unwrap();
        interp.def_class::<Box<Flag>>(spec).unwrap();

        let mut value = interp.try_convert_mut("string").unwrap();
        let class = value.funcall(&mut interp, "class", &[], None).unwrap();
        let class_display = class.to_s(&mut interp);
        assert_eq!(class_display.as_bstr(), b"String".as_bstr());

        let data = unsafe { Container::unbox_from_value(&mut value, &mut interp) };
        assert!(data.is_err());

        let flag = Box::new(Flag::default());
        let mut value = Box::<Flag>::alloc_value(flag, &mut interp).unwrap();
        let class = value.funcall(&mut interp, "class", &[], None).unwrap();
        let class_display = class.to_s(&mut interp);
        assert_eq!(class_display.as_bstr(), b"Flag".as_bstr());

        let data = unsafe { Container::unbox_from_value(&mut value, &mut interp) };
        assert!(data.is_err());
    }
}