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
use crate::object::Object;
use std::{cell::Cell, ops::Deref, rc::Rc};

pub struct ObjectInfo
{
    object: Box<dyn Object>,
    native_ref_info: ObjectNativeRefInfo,
}

pub struct ObjectHandle<'a>
{
    object: &'a dyn Object,
    _native_ref_info: ObjectNativeRefInfo,
}

impl<'a> Deref for ObjectHandle<'a>
{
    type Target = &'a dyn Object;
    fn deref(&self) -> &&'a dyn Object
    {
        &self.object
    }
}

pub struct ObjectNativeRefInfo
{
    // TODO: Remove Rc
    n_refs: Rc<Cell<usize>>,

    // in case n_refs becomes zero
    gc_notified: bool,
}

impl ObjectInfo
{
    pub fn new(obj: Box<dyn Object>) -> ObjectInfo
    {
        ObjectInfo {
            object: obj,
            native_ref_info: ObjectNativeRefInfo {
                n_refs: Rc::new(Cell::new(0)),
                gc_notified: false,
            },
        }
    }

    pub fn gc_notify(&mut self)
    {
        self.native_ref_info.gc_notified = true;
    }

    pub fn as_object(&self) -> &dyn Object
    {
        &*self.object
    }

    pub fn has_native_refs(&self) -> bool
    {
        if self.native_ref_info.n_refs.get() == 0 {
            false
        } else {
            true
        }
    }

    pub fn handle<'a>(&self) -> ObjectHandle<'a>
    {
        ObjectHandle {
            object: unsafe {
                ::std::mem::transmute::<&dyn Object, &'static dyn Object>(&*self.object)
            },
            _native_ref_info: self.native_ref_info.clone(),
        }
    }
}

impl Drop for ObjectInfo
{
    fn drop(&mut self)
    {
        if self.native_ref_info.n_refs.get() != 0 {
            eprintln!("Attempting to drop object with alive references");
            ::std::process::abort();
        }
    }
}

impl Clone for ObjectNativeRefInfo
{
    fn clone(&self) -> Self
    {
        self.n_refs.replace(self.n_refs.get() + 1);
        ObjectNativeRefInfo {
            n_refs: self.n_refs.clone(),
            gc_notified: false,
        }
    }
}

impl Drop for ObjectNativeRefInfo
{
    fn drop(&mut self)
    {
        let n_refs = self.n_refs.get();

        if self.gc_notified {
            assert_eq!(n_refs, 0);
        } else {
            assert!(n_refs > 0);
            self.n_refs.replace(n_refs - 1);
        }
    }
}

pub struct TypedObjectHandle<'a, T>
{
    _handle: ObjectHandle<'a>,
    value: &'a T,
}

impl<'a, T> Deref for TypedObjectHandle<'a, T>
where
    T: 'a,
{
    type Target = &'a T;
    fn deref(&self) -> &&'a T
    {
        &self.value
    }
}

impl<'a, T> TypedObjectHandle<'a, T>
where
    T: 'static,
{
    pub fn downcast_from(other: ObjectHandle<'a>) -> Option<TypedObjectHandle<'a, T>>
    {
        let value = match other.object.as_any().downcast_ref::<T>() {
            Some(v) => v,
            None => return None,
        };
        Some(TypedObjectHandle {
            _handle: other,
            value: value,
        })
    }
}