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
use crate::{
    garbage_collector::GcRootPtr, ArgumentReflection, GarbageCollector, Marshal,
    ReturnTypeReflection, Runtime,
};
use mun_memory::{
    gc::{Array, GcPtr, GcRuntime, HasIndirectionPtr},
    Type,
};
use std::marker::PhantomData;
use std::ptr::NonNull;
use std::sync::Arc;

/// Represents a Mun array pointer.
#[repr(transparent)]
#[derive(Clone)]
pub struct RawArray(pub(crate) GcPtr);

impl RawArray {
    /// Returns a pointer to the array memory.
    ///
    /// # Safety
    ///
    /// Dereferencing might cause undefined behavior
    pub unsafe fn get_ptr(&self) -> *const u8 {
        self.0.deref()
    }
}

/// Type-agnostic wrapper for interoperability with a Mun array. This is merely a reference to the
/// Mun array, that will be garbage collected unless it is rooted.
#[derive(Clone)]
pub struct ArrayRef<'a, T> {
    raw: RawArray,
    runtime: &'a Runtime,
    _phantom: PhantomData<T>,
}

impl<'array, T: Marshal<'array> + 'array> ArrayRef<'array, T> {
    /// Creates a `ArrayRef` that wraps a raw Mun struct.
    pub(crate) fn new<'runtime>(raw: RawArray, runtime: &'runtime Runtime) -> Self
    where
        'runtime: 'array,
    {
        Self {
            raw,
            runtime,
            _phantom: Default::default(),
        }
    }

    /// Consumes the `ArrayRef`, returning a raw Mun array.
    pub fn into_raw(self) -> RawArray {
        self.raw
    }

    /// Roots the `ArrayRef`.
    pub fn root(self) -> RootedArray<T> {
        RootedArray::new(&self.runtime.gc, self.raw)
    }

    /// Returns the type information of the array.
    pub fn type_info(&self) -> Type {
        self.runtime.gc.ptr_type(self.raw.0)
    }

    /// Returns the number of elements stored in the array
    pub fn len(&self) -> usize {
        self.runtime
            .gc
            .as_ref()
            .array(self.raw.0)
            .expect("the internal handle does not refer to an array")
            .length()
    }

    /// Returns true if this array does not contain a single element.
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns the length of the array
    pub fn capacity(&self) -> usize {
        self.runtime
            .gc
            .as_ref()
            .array(self.raw.0)
            .expect("the internal handle does not refer to an array")
            .capacity()
    }

    /// Returns an iterator to iterate over the elements of the array.
    pub fn iter(&self) -> impl Iterator<Item = T> + 'array
    where
        T: 'array,
    {
        let handle = self
            .runtime
            .gc
            .as_ref()
            .array(self.raw.0)
            .expect("type of the array value must be an array");
        let element_ty = handle.element_type();
        let runtime = self.runtime;
        handle
            .elements()
            .map(move |element_ptr| T::marshal_from_ptr(element_ptr.cast(), runtime, &element_ty))
    }
}

impl<'a, T: Marshal<'a> + ReturnTypeReflection> ReturnTypeReflection for ArrayRef<'a, T> {
    fn accepts_type(ty: &Type) -> bool {
        if let Some(arr) = ty.as_array() {
            T::accepts_type(&arr.element_type())
        } else {
            false
        }
    }

    fn type_hint() -> &'static str {
        // TODO: Improve this
        "array"
    }
}

impl<'a, T: Marshal<'a> + ArgumentReflection + 'a> ArgumentReflection for ArrayRef<'a, T> {
    fn type_info(&self, _runtime: &Runtime) -> Type {
        self.type_info()
    }
}

impl<'a, T: Marshal<'a> + 'a> Marshal<'a> for ArrayRef<'a, T> {
    type MunType = RawArray;

    fn marshal_from<'runtime>(value: Self::MunType, runtime: &'runtime Runtime) -> Self
    where
        Self: 'a,
        'runtime: 'a,
    {
        ArrayRef::new(value, runtime)
    }

    fn marshal_into(self) -> Self::MunType {
        self.raw
    }

    fn marshal_from_ptr<'runtime>(
        ptr: NonNull<Self::MunType>,
        runtime: &'runtime Runtime,
        _type_info: &Type,
    ) -> Self
    where
        Self: 'a,
        'runtime: 'a,
    {
        let handle = unsafe { *ptr.cast::<GcPtr>().as_ptr() };
        ArrayRef::new(RawArray(handle), runtime)
    }

    fn marshal_to_ptr(value: Self, mut ptr: NonNull<Self::MunType>, _type_info: &Type) {
        unsafe { *ptr.as_mut() = value.into_raw() };
    }
}

/// Type-agnostic wrapper for interoperability with a Mun struct, that has been rooted. To marshal,
/// obtain a `ArrayRef` for the `RootedArray`.
#[derive(Clone)]
pub struct RootedArray<T> {
    handle: GcRootPtr,
    _data: PhantomData<T>,
}

impl<T> RootedArray<T> {
    /// Creates a `RootedArray` that wraps a raw Mun struct.
    fn new(gc: &Arc<GarbageCollector>, raw: RawArray) -> Self {
        assert!(gc.ptr_type(raw.0).is_array());
        Self {
            handle: GcRootPtr::new(gc, raw.0),
            _data: Default::default(),
        }
    }

    /// Converts the `RootedArray` into an `ArrayRef<T>`, using an external shared reference to a
    /// `Runtime`.
    pub fn as_ref<'r>(&self, runtime: &'r Runtime) -> ArrayRef<'r, T>
    where
        T: Marshal<'r> + 'r,
    {
        assert_eq!(Arc::as_ptr(&runtime.gc), self.handle.runtime().as_ptr());
        ArrayRef::new(RawArray(self.handle.handle()), runtime)
    }
}