rune/runtime/
vm_diagnostics.rs

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
use core::ptr::NonNull;

use crate::hash::Hash;
use crate::runtime::VmResult;
use crate::Diagnostics;

pub trait VmDiagnostics {
    fn function_used(&mut self, hash: Hash, at: usize) -> VmResult<()>;

    fn vtable(&self) -> &'static VmDiagnosticsObjVtable;
}

impl VmDiagnostics for Diagnostics {
    fn function_used(&mut self, hash: Hash, at: usize) -> VmResult<()> {
        vm_try!(self.runtime_used_deprecated(at, hash));
        VmResult::Ok(())
    }

    fn vtable(&self) -> &'static VmDiagnosticsObjVtable {
        fn function_used_impl<T>(ptr: NonNull<()>, hash: Hash, at: usize) -> VmResult<()>
        where
            T: VmDiagnostics,
        {
            unsafe { VmDiagnostics::function_used(ptr.cast::<T>().as_mut(), hash, at) }
        }

        &VmDiagnosticsObjVtable {
            function_used: function_used_impl::<Self>,
        }
    }
}

#[derive(Debug)]
pub struct VmDiagnosticsObjVtable {
    function_used: unsafe fn(NonNull<()>, hash: Hash, at: usize) -> VmResult<()>,
}

#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub(crate) struct VmDiagnosticsObj {
    ptr: NonNull<()>,
    vtable: &'static VmDiagnosticsObjVtable,
}

impl VmDiagnosticsObj {
    pub(crate) fn new(trait_obj: &mut dyn VmDiagnostics) -> Self {
        let vtable = trait_obj.vtable();

        Self {
            ptr: unsafe { NonNull::new_unchecked(trait_obj as *mut _ as *mut ()) },
            vtable,
        }
    }

    pub(crate) fn function_used(&mut self, hash: Hash, at: usize) -> VmResult<()> {
        unsafe { (self.vtable.function_used)(self.ptr, hash, at) }
    }
}